| 1 | use warnings; | ||
| 2 | use strict; | ||
| 3 | |||
| 4 | =head1 NAME | ||
| 5 | |||
| 6 | Jifty::Plugin::Authentication::PasswordAction::GeneratePasswordToken - generate password token | ||
| 7 | |||
| 8 | =cut | ||
| 9 | |||
| 10 | package Jifty::Plugin::Authentication::Password::Action::GeneratePasswordToken; | ||
| 11 | use base qw/Jifty::Action/; | ||
| 12 | |||
| 13 | __PACKAGE__->mk_accessors( 'login_by' ); | ||
| 14 | |||
| 15 | =head1 METHODS | ||
| 16 | |||
| 17 | =head2 new | ||
| 18 | |||
| 19 | Looks up what L<Jifty::Plugin::Authentication::Password> is configured | ||
| 20 | to login via, username or email address, to know what form element to | ||
| 21 | expect. | ||
| 22 | |||
| 23 | =cut | ||
| 24 | |||
| 25 | sub new { | ||
| 26 | my $class = shift; | ||
| 27 | my $self = $class->SUPER::new(@_); | ||
| 28 | my $plugin = | ||
| 29 | Jifty->find_plugin('Jifty::Plugin::Authentication::Password'); | ||
| 30 | $self->login_by( $plugin->{login_by} || 'email' ); | ||
| 31 | return $self; | ||
| 32 | } | ||
| 33 | |||
| 34 | =head2 arguments | ||
| 35 | |||
| 36 | We need the email of the user we're fetching a token for, so we can | ||
| 37 | return the salt. | ||
| 38 | |||
| 39 | =cut | ||
| 40 | |||
| 41 | sub arguments { | ||
| 42 | my $self = shift; | ||
| 43 | return( { $self->login_by => { mandatory => 1 } }); | ||
| 44 | |||
| 45 | } | ||
| 46 | |||
| 47 | |||
| 48 | =head2 take_action | ||
| 49 | |||
| 50 | Generate a token and throw it back to the browser. Also, return the | ||
| 51 | user's password salt in $self->result->content. | ||
| 52 | |||
| 53 | =cut | ||
| 54 | |||
| 55 | sub take_action { | ||
| 56 | my $self = shift; | ||
| 57 | |||
| 58 | my $user = $self->load_user( $self->argument_value( $self->login_by ) ); | ||
| 59 | my $password = $user->_value('password'); | ||
| 60 | |||
| 61 | my $token = time(); | ||
| 62 | $self->result->content(token => $token); | ||
| 63 | $self->result->content(salt => ($password ? $password->[1] : "")); | ||
| 64 | Jifty->web->session->set(login_token => $token); | ||
| 65 | } | ||
| 66 | |||
| 67 | =head2 load_user | ||
| 68 | |||
| 69 | Load up and return a YourApp::User object for the user trying to log in | ||
| 70 | |||
| 71 | =cut | ||
| 72 | |||
| 73 | sub load_user { | ||
| 74 | my $self = shift; | ||
| 75 | my $value = shift; | ||
| 76 | my $user = Jifty->app_class('Model', 'User')->new( | ||
| 77 | current_user => Jifty->app_class('CurrentUser')->superuser | ||
| 78 | ); | ||
| 79 | |||
| 80 | # normally we use name as column name instead of usernmae | ||
| 81 | my $column = $self->login_by eq 'username' ? 'name' : $self->login_by; | ||
| 82 | $user->load_by_cols( $column => $value ); | ||
| 83 | return $user; | ||
| 84 | |||
| 85 | } | ||
| 86 | |||
| 87 | 1; |