Small doc fixes
[catagits/Catalyst-Plugin-Authentication.git] / lib / Catalyst / Authentication / Credential / Password.pm
CommitLineData
5c5af345 1package Catalyst::Authentication::Credential::Password;
2
3use strict;
4use warnings;
5
6use base qw/Class::Accessor::Fast/;
7
8use Scalar::Util ();
9use Catalyst::Exception ();
10use Digest ();
11
12BEGIN {
106913db 13 __PACKAGE__->mk_accessors(qw/_config realm/);
5c5af345 14}
15
16sub new {
17 my ($class, $config, $app, $realm) = @_;
18
106913db 19 my $self = { _config => $config };
5c5af345 20 bless $self, $class;
21
22 $self->realm($realm);
23
106913db 24 $self->_config->{'password_field'} ||= 'password';
25 $self->_config->{'password_type'} ||= 'clear';
26 $self->_config->{'password_hash_type'} ||= 'SHA-1';
5c5af345 27
106913db 28 my $passwordtype = $self->_config->{'password_type'};
5c5af345 29 if (!grep /$passwordtype/, ('none', 'clear', 'hashed', 'salted_hash', 'crypted', 'self_check')) {
106913db 30 Catalyst::Exception->throw(__PACKAGE__ . " used with unsupported password type: " . $self->_config->{'password_type'});
5c5af345 31 }
32 return $self;
33}
34
35sub authenticate {
36 my ( $self, $c, $realm, $authinfo ) = @_;
37
38 ## because passwords may be in a hashed format, we have to make sure that we remove the
39 ## password_field before we pass it to the user routine, as some auth modules use
40 ## all data passed to them to find a matching user...
41 my $userfindauthinfo = {%{$authinfo}};
106913db 42 delete($userfindauthinfo->{$self->_config->{'password_field'}});
5c5af345 43
44 my $user_obj = $realm->find_user($userfindauthinfo, $c);
45 if (ref($user_obj)) {
46 if ($self->check_password($user_obj, $authinfo)) {
47 return $user_obj;
48 }
49 } else {
50 $c->log->debug("Unable to locate user matching user info provided") if $c->debug;
51 return;
52 }
53}
54
55sub check_password {
56 my ( $self, $user, $authinfo ) = @_;
57
106913db 58 if ($self->_config->{'password_type'} eq 'self_check') {
59 return $user->check_password($authinfo->{$self->_config->{'password_field'}});
5c5af345 60 } else {
106913db 61 my $password = $authinfo->{$self->_config->{'password_field'}};
62 my $storedpassword = $user->get($self->_config->{'password_field'});
5c5af345 63
106913db 64 if ($self->_config->{'password_type'} eq 'none') {
5c5af345 65 return 1;
106913db 66 } elsif ($self->_config->{'password_type'} eq 'clear') {
1ae2143a 67 # FIXME - Should we warn in the $storedpassword undef case,
68 # as the user probably fluffed the config?
69 return unless defined $storedpassword;
5c5af345 70 return $password eq $storedpassword;
106913db 71 } elsif ($self->_config->{'password_type'} eq 'crypted') {
5c5af345 72 return $storedpassword eq crypt( $password, $storedpassword );
106913db 73 } elsif ($self->_config->{'password_type'} eq 'salted_hash') {
5c5af345 74 require Crypt::SaltedHash;
106913db 75 my $salt_len = $self->_config->{'password_salt_len'} ? $self->_config->{'password_salt_len'} : 0;
5c5af345 76 return Crypt::SaltedHash->validate( $storedpassword, $password,
77 $salt_len );
106913db 78 } elsif ($self->_config->{'password_type'} eq 'hashed') {
5c5af345 79
106913db 80 my $d = Digest->new( $self->_config->{'password_hash_type'} );
81 $d->add( $self->_config->{'password_pre_salt'} || '' );
5c5af345 82 $d->add($password);
106913db 83 $d->add( $self->_config->{'password_post_salt'} || '' );
5c5af345 84
85 my $computed = $d->clone()->digest;
86 my $b64computed = $d->clone()->b64digest;
87 return ( ( $computed eq $storedpassword )
88 || ( unpack( "H*", $computed ) eq $storedpassword )
89 || ( $b64computed eq $storedpassword)
90 || ( $b64computed.'=' eq $storedpassword) );
91 }
92 }
93}
94
5c5af345 95__PACKAGE__;
96
97__END__
98
99=pod
100
101=head1 NAME
102
103Catalyst::Authentication::Credential::Password - Authenticate a user
104with a password.
105
106=head1 SYNOPSIS
107
108 use Catalyst qw/
109 Authentication
110 /;
111
112 package MyApp::Controller::Auth;
113
114 sub login : Local {
115 my ( $self, $c ) = @_;
116
117 $c->authenticate( { username => $c->req->param('username'),
118 password => $c->req->param('password') });
119 }
120
121=head1 DESCRIPTION
122
123This authentication credential checker takes authentication information
124(most often a username) and a password, and attempts to validate the password
125provided against the user retrieved from the store.
126
127=head1 CONFIGURATION
128
129 # example
47074ecb 130 __PACKAGE__->config('Plugin::Authentication' =>
5c5af345 131 {
132 default_realm => 'members',
133 realms => {
134 members => {
135
136 credential => {
137 class => 'Password',
138 password_field => 'password',
139 password_type => 'hashed',
140 password_hash_type => 'SHA-1'
141 },
142 ...
143
144
145The password module is capable of working with several different password
146encryption/hashing algorithms. The one the module uses is determined by the
147credential configuration.
148
149Those who have used L<Catalyst::Plugin::Authentication> prior to the 0.10 release
150should note that the password field and type information is no longer part
151of the store configuration and is now part of the Password credential configuration.
152
153=over 4
154
155=item class
156
157The classname used for Credential. This is part of
158L<Catalyst::Plugin::Authentication> and is the method by which
159Catalyst::Authentication::Credential::Password is loaded as the
160credential validator. For this module to be used, this must be set to
161'Password'.
162
163=item password_field
164
165The field in the user object that contains the password. This will vary
166depending on the storage class used, but is most likely something like
167'password'. In fact, this is so common that if this is left out of the config,
168it defaults to 'password'. This field is obtained from the user object using
8a7bd676 169the get() method. Essentially: $user->get('passwordfieldname');
170B<NOTE> If the password_field is something other than 'password', you must
171be sure to use that same field name when calling $c->authenticate().
5c5af345 172
173=item password_type
174
175This sets the password type. Often passwords are stored in crypted or hashed
176formats. In order for the password module to verify the plaintext password
177passed in, it must be told what format the password will be in when it is retreived
178from the user object. The supported options are:
179
180=over 8
181
182=item none
183
184No password check is done. An attempt is made to retrieve the user based on
185the information provided in the $c->authenticate() call. If a user is found,
186authentication is considered to be successful.
187
188=item clear
189
190The password in user is in clear text and will be compared directly.
191
192=item self_check
193
194This option indicates that the password should be passed to the check_password()
195routine on the user object returned from the store.
196
197=item crypted
198
199The password in user is in UNIX crypt hashed format.
200
201=item salted_hash
202
203The password in user is in salted hash format, and will be validated
204using L<Crypt::SaltedHash>. If this password type is selected, you should
205also provide the B<password_salt_len> config element to define the salt length.
206
207=item hashed
208
209If the user object supports hashed passwords, they will be used in conjunction
210with L<Digest>. The following config elements affect the hashed configuration:
211
212=over 8
213
214=item password_hash_type
215
216The hash type used, passed directly to L<Digest/new>.
217
218=item password_pre_salt
219
220Any pre-salt data to be passed to L<Digest/add> before processing the password.
221
222=item password_post_salt
223
224Any post-salt data to be passed to L<Digest/add> after processing the password.
225
226=back
227
228=back
229
230=back
231
232=head1 USAGE
233
234The Password credential module is very simple to use. Once configured as
235indicated above, authenticating using this module is simply a matter of
236calling $c->authenticate() with an authinfo hashref that includes the
237B<password> element. The password element should contain the password supplied
238by the user to be authenticated, in clear text. The other information supplied
239in the auth hash is ignored by the Password module, and simply passed to the
240auth store to be used to retrieve the user. An example call follows:
241
242 if ($c->authenticate({ username => $username,
243 password => $password} )) {
244 # authentication successful
245 } else {
246 # authentication failed
247 }
248
249=head1 METHODS
250
251There are no publicly exported routines in the Password module (or indeed in
252most credential modules.) However, below is a description of the routines
253required by L<Catalyst::Plugin::Authentication> for all credential modules.
254
8a7bd676 255=head2 new( $config, $app, $realm )
5c5af345 256
257Instantiate a new Password object using the configuration hash provided in
258$config. A reference to the application is provided as the second argument.
259Note to credential module authors: new() is called during the application's
260plugin setup phase, which is before the application specific controllers are
261loaded. The practical upshot of this is that things like $c->model(...) will
262not function as expected.
263
264=head2 authenticate( $authinfo, $c )
265
266Try to log a user in, receives a hashref containing authentication information
267as the first argument, and the current context as the second.
268
269=head2 check_password( )
270
5c5af345 271=cut