Jifty/Plugin/Authentication/Password/Action/Login.pm untested

filter: Statementstmt 0.0% 0 / 99 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 179 CC 16 SCAR 27.6
CC
16
Cov
0%
take_action
42.0
validate_password
20.0
validate_token
20.0
CRAP 15.9
1 use warnings;
2 use strict;
3  
4 =head1 NAME
5  
6 Jifty::Plugin::Authentication::Password::Action::Login - process login with password
7  
8 =cut
9  
10 package Jifty::Plugin::Authentication::Password::Action::Login;
11 use base qw/Jifty::Action/;
12 use Digest::MD5 qw(md5_hex);
13 use HTTP::Date ();
14  
15 use constant TOKEN_EXPIRE_TIME => 30;
16  
17 __PACKAGE__->mk_accessors( 'login_by' );
18  
19 =head2 new
20  
21 =cut
22  
23 sub new {
24 my $class = shift;
25 my $self = $class->SUPER::new(@_);
26 my $plugin =
27 Jifty->find_plugin('Jifty::Plugin::Authentication::Password');
28 $self->login_by( $plugin->{login_by} || 'email' );
29 return $self;
30 }
31  
32 =head2 arguments
33  
34 Return the email and password form fields
35  
36 =cut
37  
38 sub arguments {
39 my $self = shift;
40  
41 my $login_para;
42 if ( $self->login_by eq 'username' ) {
43 $login_para = {
44 username => {
45 label => _( 'Username' ),
46 mandatory => 1,
47 ajax_validates => 1,
48 }
49 };
50 }
51 else {
52 $login_para = {
53 email => {
54 label => _( 'Email' ),
55 mandatory => 1,
56 ajax_validates => 1,
57 }
58 };
59 }
60  
61 return( {
62 %$login_para,
63  
64 password => { type => 'password',
65 label => _('Password'),
66 # mandatory in some cases; see validate_password
67 mandatory => 0,
68 },
69 hashed_password => { type => 'hidden',
70 label => _('Hashed Password'),
71 },
72 remember => { type => 'checkbox',
73 label => _('Remember me?'),
74 hints => _('Your browser can remember your login for you'),
75 default => 0,
76 },
77 token => { type => 'hidden',
78 label => 'token',
79 mandatory => 0 },
80  
81 });
82  
83 }
84  
85 =head2 validate_email ADDRESS
86  
87 Makes sure that the email submitted is a legal email and that there's a user in the database with it.
88  
89 Overridden from Jifty::Action::Record.
90  
91 =cut
92  
93 sub validate_email {
94 my $self = shift;
95 my $email = shift;
96  
97 return $self->_validate_email_or_username(email => $email);
98 }
99  
100 =head2 validate_username ADDRESS
101  
102 there's a user in the database with it.
103  
104 Overridden from Jifty::Action::Record.
105  
106 =cut
107  
108 sub validate_username {
109 my $self = shift;
110 my $username = shift;
111  
112 return $self->_validate_email_or_username(username => $username);
113 }
114  
115 sub _validate_email_or_username {
116 my $self = shift;
117 my $name = shift;
118 my $value = shift;
119  
120 if ($value) {
121 my $u = $self->load_user( $value);
122 return $self->validation_error(
123 $name => _("It doesn't look like there's an account by that name.")
124 ) unless ( $u->id );
125 return $self->validation_ok($name);
126 }
127 return;
128 }
129  
130 =head2 validate_password PASSWORD
131  
132 Makes sure that the password submitted actually exists, unless there's a token and a hashed
133 password.
134  
135 Overridden from Jifty::Action::Record.
136  
137 =cut
138  
139 sub validate_password {
140 my $self = shift;
141 my $pw = shift;
142 my $token = $self->argument_value('token') ||'';
143 my $hashedpw = $self->argument_value('hashed_password') ;
144  
145  
146 if ($token eq '') { # we have no token, validate in a standard way
147 unless ( defined $pw && length $pw ) {
148 return $self->validation_error(password => "You need to fill in the 'password' field" );
149 }
150 } else { # we have a token, so we should have a hashed pw
151 my $emptypw = '';
152 my $blankhash = md5_hex("$token $emptypw");
153 if ($hashedpw eq $blankhash) {
154 return $self->validation_error(password => "You need to fill in the 'password' field" );
155 }
156
157 }
158  
159  
160 return $self->validation_ok('password');
161 }
162  
163 =head2 validate_token TOKEN
164  
165 Make sure we issued the token within the last 30 seconds, otherwise
166 time out the request.
167  
168 =cut
169  
170 sub validate_token {
171 my $self = shift;
172 my $value = shift;
173 my $token = Jifty->web->session->get('login_token');
174 Jifty->web->session->remove('login_token');
175 if ($value) {
176 if(int $value < (time - TOKEN_EXPIRE_TIME)) {
177 return $self->validation_error(token => "Your login attempt has timed out. Please try again.");
178 }
179 if ($value ne $token) {
180 return $self->validation_error(token => "That didn't work. Please try again.");
181 }
182 }
183 return $self->validation_ok("token");
184 }
185  
186 =head2 take_action
187  
188 Actually check the user's password. If it's right, log them in.
189 Otherwise, throw an error.
190  
191  
192 =cut
193  
194 sub take_action {
195 my $self = shift;
196 my $user = $self->load_user( $self->argument_value( $self->login_by ) );
197 my $password = $self->argument_value('password');
198 my $token = $self->argument_value('token') || '';
199 my $hashedpw = $self->argument_value('hashed_password');
200  
201 my $BAD_PW = _('You may have mistyped your email or password. Give it another shot.');
202  
203 if ( $token ne '' ) { # browser supports javascript, do password hashing
204 unless ( $user->id && $user->hashed_password_is( $hashedpw, $token ) )
205 {
206 $self->result->error($BAD_PW);
207 return;
208 }
209 } else { # no password hashing over the wire
210 unless ( $user->id && $user->password_is($password) ) {
211 $self->result->error($BAD_PW);
212 return;
213 }
214 }
215 unless ($user->email_confirmed) {
216 $self->result->error( _(q{You haven't confirmed your account yet.}) ); return;
217 }
218  
219 # Set up our login message
220 $self->result->message( $self->login_message($user));
221 $self->result->content( id => $user->id );
222  
223 # Actually do the signin thing.
224 Jifty->web->current_user(Jifty->app_class('CurrentUser')->new( id => $user->id));
225 Jifty->web->session->expires($self->argument_value('remember') ? HTTP::Date::time2str( time() + 31536000 ) : undef);
226 Jifty->web->session->set_cookie;
227  
228 return 1;
229 }
230  
231 =head2 load_user
232  
233 Load up and return a YourApp::User object for the user trying to log in
234  
235 =cut
236  
237 sub load_user {
238 my $self = shift;
239 my $value = shift;
240 my $user = Jifty->app_class('Model', 'User')->new(current_user => Jifty->app_class('CurrentUser')->superuser);
241  
242 # normally we use name as column name instead of usernmae
243 my $column = $self->login_by eq 'username' ? 'name' : $self->login_by;
244 $user->load_by_cols( $column => $value );
245 return $user;
246  
247 }
248  
249 =head2 login_message $user_object
250  
251 Returns the "hi, you're logged in message"
252  
253 =cut
254  
255  
256 sub login_message {
257 my $self = shift;
258 my $user = shift;
259 return _("Welcome back, %1.", $user->name);
260 }
261  
262 1;