Add some dwim to Credential::Password (arg less $c->login will mine params), fix...
Yuval Kogman [Tue, 8 Nov 2005 16:23:37 +0000 (16:23 +0000)]
lib/Catalyst/Plugin/Authentication.pm
lib/Catalyst/Plugin/Authentication/Credential/Password.pm
t/04_authentication.t

index 38f2df0..91f57f2 100644 (file)
@@ -72,7 +72,7 @@ sub prepare {
 sub setup {
     my $c = shift;
 
-    my $cfg = $c->config->{authentication};
+    my $cfg = $c->config->{authentication} || {};
 
     %$cfg = (
         use_session => 1,
index 76a3104..4601109 100644 (file)
@@ -11,6 +11,19 @@ use Digest              ();
 
 sub login {
     my ( $c, $user, $password ) = @_;
+
+    for ( $c->request ) {
+             $user ||= $_->param("login")
+          || $c->param("user")
+          || $c->param("username")
+          || Catalyst::Exception->throw("Can't determine username for login");
+
+             $password ||= $_->param("password")
+          || $c->param("passwd")
+          || $c->param("pass")
+          || Catalyst::Exception->throw("Can't determine password for login");
+    }
+
     $user = $c->get_user($user)
       unless Scalar::Util::blessed($user)
       and $user->isa("Catalyst:::Plugin::Authentication::User");
@@ -42,8 +55,9 @@ sub _check_password {
         return $d->digest eq $user->hashed_password;
     }
     elsif ( $user->supports(qw/password self_check/) ) {
-               # while somewhat silly, this is to prevent code duplication
-               return $c->user->check_password( $password );
+
+        # while somewhat silly, this is to prevent code duplication
+        return $user->check_password($password);
     }
     else {
         Catalyst::Exception->throw(
@@ -60,7 +74,7 @@ __END__
 
 =head1 NAME
 
-Catalyst:::Plugin::Authentication::Credential::Password - Authenticate a user
+Catalyst::Plugin::Authentication::Credential::Password - Authenticate a user
 with a password.
 
 =head1 SYNOPSIS
@@ -106,13 +120,18 @@ with L<Digest>.
 
 =item login $user, $password
 
+=item login
+
 Try to log a user in.
 
-$user can be an ID or object. If it isa
-L<Catalyst:::Plugin::Authentication::User> it will be used as is. Otherwise
+C<$user> can be an ID or object. If it isa
+L<Catalyst::Plugin::Authentication::User> it will be used as is. Otherwise
 C<< $c->get_user >> is used to retrieve it.
 
-$password is a string.
+C<$password> is a string.
+
+If C<$user> or C<$password> are not provided the parameters C<login>, C<user>,
+C<username> and C<password>, C<passwd>, C<pass> will be tried instead.
 
 =back
 
index c3bf72a..f357c73 100644 (file)
@@ -8,4 +8,4 @@ use Test::More 'no_plan';
 
 my $m; BEGIN { use_ok($m = "Catalyst::Plugin::Authentication") }
 
-can_ok( $m, $_ ) for qw/user logout/;
+can_ok( $m, $_ ) for qw/user logout set_authenticated/;