1 package Catalyst::Authentication::Credential::Password;
6 use base qw/Class::Accessor::Fast/;
9 use Catalyst::Exception ();
12 __PACKAGE__->mk_accessors(qw/_config realm/);
15 my ($class, $config, $app, $realm) = @_;
17 # Note _config is horrible back compat hackery!
18 my $self = { _config => $config };
23 $self->_config->{'password_field'} ||= 'password';
24 $self->_config->{'password_type'} ||= 'clear';
25 $self->_config->{'password_hash_type'} ||= 'SHA-1';
27 my $passwordtype = $self->_config->{'password_type'};
28 if (!grep /$passwordtype/, ('none', 'clear', 'hashed', 'salted_hash', 'crypted', 'self_check')) {
29 Catalyst::Exception->throw(__PACKAGE__ . " used with unsupported password type: " . $self->_config->{'password_type'});
35 my ( $self, $c, $realm, $authinfo ) = @_;
37 ## because passwords may be in a hashed format, we have to make sure that we remove the
38 ## password_field before we pass it to the user routine, as some auth modules use
39 ## all data passed to them to find a matching user...
40 my $userfindauthinfo = {%{$authinfo}};
41 delete($userfindauthinfo->{$self->_config->{'password_field'}});
43 my $user_obj = $realm->find_user($userfindauthinfo, $c);
45 if ($self->check_password($user_obj, $authinfo)) {
49 $c->log->debug("Unable to locate user matching user info provided") if $c->debug;
55 my ( $self, $user, $authinfo ) = @_;
57 if ($self->_config->{'password_type'} eq 'self_check') {
58 return $user->check_password($authinfo->{$self->_config->{'password_field'}});
60 my $password = $authinfo->{$self->_config->{'password_field'}};
61 my $storedpassword = $user->get($self->_config->{'password_field'});
63 if ($self->_config->{'password_type'} eq 'none') {
65 } elsif ($self->_config->{'password_type'} eq 'clear') {
66 # FIXME - Should we warn in the $storedpassword undef case,
67 # as the user probably fluffed the config?
68 return unless defined $storedpassword;
69 return $password eq $storedpassword;
70 } elsif ($self->_config->{'password_type'} eq 'crypted') {
71 return $storedpassword eq crypt( $password, $storedpassword );
72 } elsif ($self->_config->{'password_type'} eq 'salted_hash') {
73 require Crypt::SaltedHash;
74 my $salt_len = $self->_config->{'password_salt_len'} ? $self->_config->{'password_salt_len'} : 0;
75 return Crypt::SaltedHash->validate( $storedpassword, $password,
77 } elsif ($self->_config->{'password_type'} eq 'hashed') {
79 my $d = Digest->new( $self->_config->{'password_hash_type'} );
80 $d->add( $self->_config->{'password_pre_salt'} || '' );
82 $d->add( $self->_config->{'password_post_salt'} || '' );
84 my $computed = $d->clone()->digest;
85 my $b64computed = $d->clone()->b64digest;
86 return ( ( $computed eq $storedpassword )
87 || ( unpack( "H*", $computed ) eq $storedpassword )
88 || ( $b64computed eq $storedpassword)
89 || ( $b64computed.'=' eq $storedpassword) );
102 Catalyst::Authentication::Credential::Password - Authenticate a user
111 package MyApp::Controller::Auth;
114 my ( $self, $c ) = @_;
116 $c->authenticate( { username => $c->req->param('username'),
117 password => $c->req->param('password') });
122 This authentication credential checker takes authentication information
123 (most often a username) and a password, and attempts to validate the password
124 provided against the user retrieved from the store.
129 __PACKAGE__->config('Plugin::Authentication' =>
131 default_realm => 'members',
137 password_field => 'password',
138 password_type => 'hashed',
139 password_hash_type => 'SHA-1'
144 The password module is capable of working with several different password
145 encryption/hashing algorithms. The one the module uses is determined by the
146 credential configuration.
148 Those who have used L<Catalyst::Plugin::Authentication> prior to the 0.10 release
149 should note that the password field and type information is no longer part
150 of the store configuration and is now part of the Password credential configuration.
156 The classname used for Credential. This is part of
157 L<Catalyst::Plugin::Authentication> and is the method by which
158 Catalyst::Authentication::Credential::Password is loaded as the
159 credential validator. For this module to be used, this must be set to
164 The field in the user object that contains the password. This will vary
165 depending on the storage class used, but is most likely something like
166 'password'. In fact, this is so common that if this is left out of the config,
167 it defaults to 'password'. This field is obtained from the user object using
168 the get() method. Essentially: $user->get('passwordfieldname');
169 B<NOTE> If the password_field is something other than 'password', you must
170 be sure to use that same field name when calling $c->authenticate().
174 This sets the password type. Often passwords are stored in crypted or hashed
175 formats. In order for the password module to verify the plaintext password
176 passed in, it must be told what format the password will be in when it is retreived
177 from the user object. The supported options are:
183 No password check is done. An attempt is made to retrieve the user based on
184 the information provided in the $c->authenticate() call. If a user is found,
185 authentication is considered to be successful.
189 The password in user is in clear text and will be compared directly.
193 This option indicates that the password should be passed to the check_password()
194 routine on the user object returned from the store.
198 The password in user is in UNIX crypt hashed format.
202 The password in user is in salted hash format, and will be validated
203 using L<Crypt::SaltedHash>. If this password type is selected, you should
204 also provide the B<password_salt_len> config element to define the salt length.
208 If the user object supports hashed passwords, they will be used in conjunction
209 with L<Digest>. The following config elements affect the hashed configuration:
213 =item password_hash_type
215 The hash type used, passed directly to L<Digest/new>.
217 =item password_pre_salt
219 Any pre-salt data to be passed to L<Digest/add> before processing the password.
221 =item password_post_salt
223 Any post-salt data to be passed to L<Digest/add> after processing the password.
233 The Password credential module is very simple to use. Once configured as
234 indicated above, authenticating using this module is simply a matter of
235 calling $c->authenticate() with an authinfo hashref that includes the
236 B<password> element. The password element should contain the password supplied
237 by the user to be authenticated, in clear text. The other information supplied
238 in the auth hash is ignored by the Password module, and simply passed to the
239 auth store to be used to retrieve the user. An example call follows:
241 if ($c->authenticate({ username => $username,
242 password => $password} )) {
243 # authentication successful
245 # authentication failed
250 There are no publicly exported routines in the Password module (or indeed in
251 most credential modules.) However, below is a description of the routines
252 required by L<Catalyst::Plugin::Authentication> for all credential modules.
254 =head2 new( $config, $app, $realm )
256 Instantiate a new Password object using the configuration hash provided in
257 $config. A reference to the application is provided as the second argument.
258 Note to credential module authors: new() is called during the application's
259 plugin setup phase, which is before the application specific controllers are
260 loaded. The practical upshot of this is that things like $c->model(...) will
261 not function as expected.
263 =head2 authenticate( $authinfo, $c )
265 Try to log a user in, receives a hashref containing authentication information
266 as the first argument, and the current context as the second.
268 =head2 check_password( )