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
CommitLineData
a90296d4 1#!/usr/bin/perl
2
3package Catalyst::Plugin::Authentication::Credential::Password;
4
5use strict;
6use warnings;
7
8use Scalar::Util ();
9use Catalyst::Exception ();
10use Digest ();
11
12sub login {
13 my ( $c, $user, $password ) = @_;
14
15 for ( $c->request ) {
a93f1197 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 }
a90296d4 32 }
33
a93f1197 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 }
a90296d4 46
47 if ( $c->_check_password( $user, $password ) ) {
48 $c->set_authenticated($user);
a93f1197 49 $c->log->debug("Successfully authenticated user '$user'.")
50 if $c->debug;
a90296d4 51 return 1;
52 }
53 else {
a93f1197 54 $c->log->debug(
55 "Failed to authenticate user '$user'. Reason: 'Incorrect password'"
56 )
57 if $c->debug;
a90296d4 58 return;
59 }
60}
61
62sub _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
115Catalyst::Plugin::Authentication::Credential::Password - Authenticate a user
116with 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
9b09fd1c 134This authentication credential checker takes a username (or userid) and a
135password, and tries various methods of comparing a password based on what
a90296d4 136the chosen store's user objects support:
137
138=over 4
139
140=item clear text password
141
142If the user has clear a clear text password it will be compared directly.
143
144=item crypted password
145
146If UNIX crypt hashed passwords are supported, they will be compared using
147perl's builtin C<crypt> function.
148
149=item hashed password
150
151If the user object supports hashed passwords, they will be used in conjunction
152with L<Digest>.
153
154=back
155
156=head1 METHODS
157
158=over 4
159
160=item login $username, $password
161
162Try to log a user in.
163
a2fcf979 164C<$username> can be a string (e.g. retrieved from a form) or an object.
9b09fd1c 165If the object is a L<Catalyst::Plugin::Authentication::User> it will be used
a90296d4 166as is. Otherwise C<< $c->get_user >> is used to retrieve it.
167
168C<$password> is a string.
169
d1481e8f 170If C<$username> or C<$password> are not provided, the query parameters
9b09fd1c 171C<login>, C<user>, C<username> and C<password>, C<passwd>, C<pass> will
172be tried instead.
173
174=back
175
176=head1 RELATED USAGE
177
178After the user is logged in, the user object for the current logged in user
179can be retrieved from the context using the C<< $c->user >> method.
180
181The current user can be logged out again by calling the C<< $c->logout >>
182method.
183
a90296d4 184=head1 SUPPORTING THIS PLUGIN
9b09fd1c 185
186For a User class to support credential verification using this plugin, it
187needs to indicate what sort of password a given user supports
188by implementing the C<supported_features> method in one or many of the
189following ways:
190
a90296d4 191=head2 Clear Text Passwords
192
193Predicate:
194
195 $user->supported_features(qw/password clear/);
196
197Expected methods:
198
199=over 4
200
201=item password
202
203Returns the user's clear text password as a string to be compared with C<eq>.
204
205=back
206
207=head2 Crypted Passwords
208
209Predicate:
210
211 $user->supported_features(qw/password crypted/);
212
213Expected methods:
214
215=over 4
216
217=item crypted_password
218
219Return'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
225Predicate:
226
227 $user->supported_features(qw/password hashed/);
228
229Expected methods:
230
231=over 4
232
233=item hashed_password
234
235Return's the hash of the user's password as B<binary>.
236
237=item hash_algorithm
238
239Returns a string suitable for feeding into L<Digest/new>.
240
241=item password_pre_salt
242
243=item password_post_salt
244
245Returns a string to be hashed before/after the user's password. Typically only
246a pre-salt is used.
247
248=back
249
250=head2 Crypt::SaltedHash Passwords
251
252Predicate:
253
254 $user->supported_features(qw/password salted_hash/);
255
256Expected methods:
257
258=over 4
259
260=item hashed_password
261
262Returns the hash of the user's password as returned from L<Crypt-SaltedHash>->generate.
263
264=back
265
266Optional methods:
267
268=over 4
269
270=item password_salt_len
271
272Returns the length of salt used to generate the salted hash.
273
274=back
275
276=cut
277
278