doc fix in C::P::Auth + debug messages in C::P::Auth::Cred::Password
[catagits/Catalyst-Plugin-Authentication.git] / lib / Catalyst / Plugin / Authentication / Credential / Password.pm
1 #!/usr/bin/perl
2
3 package Catalyst::Plugin::Authentication::Credential::Password;
4
5 use strict;
6 use warnings;
7
8 use Scalar::Util        ();
9 use Catalyst::Exception ();
10 use Digest              ();
11
12 sub login {
13     my ( $c, $user, $password ) = @_;
14
15     for ( $c->request ) {
16         unless ( $user ||= $_->param("login")
17             || $_->param("user")
18             || $_->param("username") )
19         {
20             $c->log->debug(
21                 "Can't login a user without a user object or user ID param");
22             return;
23         }
24
25         unless ( $password ||= $_->param("password")
26             || $_->param("passwd")
27             || $_->param("pass") )
28         {
29             $c->log->debug("Can't login a user without a password");
30             return;
31         }
32     }
33
34     unless ( Scalar::Util::blessed($user)
35         and $user->isa("Catalyst:::Plugin::Authentication::User") )
36     {
37         if ( my $user_obj = $c->get_user($user) ) {
38             $user = $user_obj;
39         }
40         else {
41             $c->log->debug("User '$user' doesn't exist in the default store")
42               if $c->debug;
43             return;
44         }
45     }
46
47     if ( $c->_check_password( $user, $password ) ) {
48         $c->set_authenticated($user);
49         $c->log->debug("Successfully authenticated user '$user'.")
50           if $c->debug;
51         return 1;
52     }
53     else {
54         $c->log->debug(
55             "Failed to authenticate user '$user'. Reason: 'Incorrect password'"
56           )
57           if $c->debug;
58         return;
59     }
60 }
61
62 sub _check_password {
63     my ( $c, $user, $password ) = @_;
64
65     if ( $user->supports(qw/password clear/) ) {
66         return $user->password eq $password;
67     }
68     elsif ( $user->supports(qw/password crypted/) ) {
69         my $crypted = $user->crypted_password;
70         return $crypted eq crypt( $password, $crypted );
71     }
72     elsif ( $user->supports(qw/password hashed/) ) {
73
74         my $d = Digest->new( $user->hash_algorithm );
75         $d->add( $user->password_pre_salt || '' );
76         $d->add($password);
77         $d->add( $user->password_post_salt || '' );
78
79         my $stored   = $user->hashed_password;
80         my $computed = $d->digest;
81
82         return ( ( $computed eq $stored )
83               || ( unpack( "H*", $computed ) eq $stored ) );
84     }
85     elsif ( $user->supports(qw/password salted_hash/) ) {
86         require Crypt::SaltedHash;
87
88         my $salt_len =
89           $user->can("password_salt_len") ? $user->password_salt_len : 0;
90
91         return Crypt::SaltedHash->validate( $user->hashed_password, $password,
92             $salt_len );
93     }
94     elsif ( $user->supports(qw/password self_check/) ) {
95
96         # while somewhat silly, this is to prevent code duplication
97         return $user->check_password($password);
98
99     }
100     else {
101         Catalyst::Exception->throw(
102                 "The user object $user does not support any "
103               . "known password authentication mechanism." );
104     }
105 }
106
107 __PACKAGE__;
108
109 __END__
110
111 =pod
112
113 =head1 NAME
114
115 Catalyst::Plugin::Authentication::Credential::Password - Authenticate a user
116 with a password.
117
118 =head1 SYNOPSIS
119
120     use Catalyst qw/
121       Authentication
122       Authentication::Store::Foo
123       Authentication::Credential::Password
124       /;
125
126     sub login : Local {
127         my ( $self, $c ) = @_;
128
129         $c->login( $c->req->param('username'), $c->req->param('password') );
130     }
131
132 =head1 DESCRIPTION
133
134 This authentication credential checker takes a username (or userid) and a 
135 password, and tries various methods of comparing a password based on what 
136 the chosen store's user objects support:
137
138 =over 4
139
140 =item clear text password
141
142 If the user has clear a clear text password it will be compared directly.
143
144 =item crypted password
145
146 If UNIX crypt hashed passwords are supported, they will be compared using
147 perl's builtin C<crypt> function.
148
149 =item hashed password
150
151 If the user object supports hashed passwords, they will be used in conjunction
152 with L<Digest>.
153
154 =back
155
156 =head1 METHODS
157
158 =over 4
159
160 =item login $username, $password
161
162 Try to log a user in.
163
164 C<$username> can be a string (e.g. retrieved from a form) or an object. 
165 If the object is a L<Catalyst::Plugin::Authentication::User> it will be used 
166 as is. Otherwise C<< $c->get_user >> is used to retrieve it.
167
168 C<$password> is a string.
169
170 If C<$username> or C<$password> are not provided, the query parameters 
171 C<login>, C<user>, C<username> and C<password>, C<passwd>, C<pass> will 
172 be tried instead.
173
174 =back
175
176 =head1 RELATED USAGE
177
178 After the user is logged in, the user object for the current logged in user 
179 can be retrieved from the context using the C<< $c->user >> method.
180
181 The current user can be logged out again by calling the C<< $c->logout >> 
182 method.
183
184 =head1 SUPPORTING THIS PLUGIN
185
186 For a User class to support credential verification using this plugin, it
187 needs to indicate what sort of password a given user supports 
188 by implementing the C<supported_features> method in one or many of the 
189 following ways:
190
191 =head2 Clear Text Passwords
192
193 Predicate:
194
195         $user->supported_features(qw/password clear/);
196
197 Expected methods:
198
199 =over 4
200
201 =item password
202
203 Returns the user's clear text password as a string to be compared with C<eq>.
204
205 =back
206
207 =head2 Crypted Passwords
208
209 Predicate:
210
211         $user->supported_features(qw/password crypted/);
212
213 Expected methods:
214
215 =over 4
216
217 =item crypted_password
218
219 Return's the user's crypted password as a string, with the salt as the first two chars.
220
221 =back
222
223 =head2 Hashed Passwords
224
225 Predicate:
226
227         $user->supported_features(qw/password hashed/);
228
229 Expected methods:
230
231 =over 4
232
233 =item hashed_password
234
235 Return's the hash of the user's password as B<binary>.
236
237 =item hash_algorithm
238
239 Returns a string suitable for feeding into L<Digest/new>.
240
241 =item password_pre_salt
242
243 =item password_post_salt
244
245 Returns a string to be hashed before/after the user's password. Typically only
246 a pre-salt is used.
247
248 =back
249
250 =head2 Crypt::SaltedHash Passwords
251
252 Predicate:
253
254         $user->supported_features(qw/password salted_hash/);
255
256 Expected methods:
257
258 =over 4
259
260 =item hashed_password
261
262 Returns the hash of the user's password as returned from L<Crypt-SaltedHash>->generate.
263
264 =back
265
266 Optional methods:
267
268 =over 4
269
270 =item password_salt_len
271
272 Returns the length of salt used to generate the salted hash.
273
274 =back
275
276 =cut
277
278