Test + fixes for new auth draft
Yuval Kogman [Fri, 4 Nov 2005 02:31:15 +0000 (02:31 +0000)]
lib/Catalyst/Plugin/Authentication.pm
lib/Catalyst/Plugin/Authentication/Credential/Password.pm
lib/Catalyst/Plugin/Authentication/Store/Minimal.pm
lib/Catalyst/Plugin/Authentication/User/Hash.pm
t/04_authentication.t [new file with mode: 0644]
t/05_password.t [new file with mode: 0644]
t/live_app.t [new file with mode: 0644]

index 8f78848..211f609 100644 (file)
@@ -2,18 +2,16 @@
 
 package Catalyst::Plugin::Authentication;
 
-use base qw/Class::Accessor::Fast/;
+use base qw/Class::Accessor::Fast Class::Data::Inheritable/;
 
-BEGIN { __PACKAGE__->mk_accessors(qw/user/) }
+BEGIN {
+    __PACKAGE__->mk_accessors(qw/user/);
+    __PACKAGE__->mk_classdata(qw/default_auth_store/);
+}
 
 use strict;
 use warnings;
 
-sub default_auth_store {
-       my $c = shift;
-       $c->config->{authentication}{store};
-}
-
 sub set_authenticated {
     my ( $c, $user ) = @_;
 
@@ -32,7 +30,12 @@ sub logout {
     my $c = shift;
 
     $c->user(undef);
-    delete @{ $c->session }{qw/__user __user_class/};
+
+    if (    $c->isa("Catalyst::Plugin::Session")
+        and $c->config->{authentication}{use_session} )
+    {
+        delete @{ $c->session }{qw/__user __user_class/};
+    }
 }
 
 sub get_user {
@@ -72,6 +75,8 @@ sub setup {
         use_session => 1,
         %$cfg,
     );
+
+    $c->NEXT::setup(@_);
 }
 
 __PACKAGE__;
index f3c0934..2992a78 100644 (file)
@@ -10,7 +10,7 @@ use Catalyst::Exception ();
 use Digest              ();
 
 sub login {
-    my ( $self, $c, $user, $password ) = @_;
+    my ( $c, $user, $password ) = @_;
     $user = $c->get_user($user)
       unless Scalar::Util::blessed($user)
       and $user->isa("Catalyst:::Plugin::Authentication::User");
@@ -39,7 +39,7 @@ sub _check_password {
         $d->add( $user->password_pre_salt || '' );
         $d->add($password);
         $d->add( $user->password_post_salt || '' );
-        return $c->digest eq $user->hashed_password;
+        return $d->digest eq $user->hashed_password;
     }
     else {
         Catalyst::Exception->throw(
@@ -112,6 +112,67 @@ $password is a string.
 
 =back
 
+=head1 SUPPORTING THIS PLUGIN
+
+=head2 Clear Text Passwords
+
+Predicate:
+
+       $user->supports(qw/password clear/);
+
+Expected methods:
+
+=over 4
+
+=item password
+
+Returns the user's clear text password as a string to be compared with C<eq>.
+
+=back
+
+=head2 Crypted Passwords
+
+Predicate:
+
+       $user->supports(qw/password crypted/);
+
+Expected methods:
+
+=over 4
+
+=item crypted_password
+
+Return's the user's crypted password as a string, with the salt as the first two chars.
+
+=back
+
+=head2 Hashed Passwords
+
+Predicate:
+
+       $user->supports(qw/password hashed/);
+
+Expected methods:
+
+=over 4
+
+=item hashed_passwords
+
+Return's the hash of the user's password as B<binary>.
+
+=item hash_algorithm
+
+Returns a string suitable for feeding into L<Digest/new>.
+
+=item password_pre_salt
+
+=item password_post_salt
+
+Returns a string to be hashed before/after the user's password. Typically only
+a pre-salt is used.
+
+=back
+
 =cut
 
 
index af17d4b..2dfb066 100644 (file)
@@ -10,10 +10,13 @@ use Catalyst::Plugin::Authentication::Store::Minimal::Backend;
 sub setup {
     my $c = shift;
 
-    $c->config->{authentication}{store} =
-      Catalyst::Plugin::Authentication::Store::Minimal::Backend->new(
-        $c->config->{authentication}{users} );
+    $c->default_auth_store(
+        Catalyst::Plugin::Authentication::Store::Minimal::Backend->new(
+            $c->config->{authentication}{users}
+        )
+    );
 
+       $c->NEXT::setup(@_);
 }
 
 __PACKAGE__;
index 0b7bb55..62a0898 100644 (file)
@@ -24,7 +24,7 @@ my %features = (
     password => {
         clear   => ["password"],
         crypted => ["crypted_password"],
-        hashed  => ["hashed_password hash_algorithm"],
+        hashed  => [qw/hashed_password hash_algorithm/],
     },
     session => 1,
 );
diff --git a/t/04_authentication.t b/t/04_authentication.t
new file mode 100644 (file)
index 0000000..c3bf72a
--- /dev/null
@@ -0,0 +1,11 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+use Test::More 'no_plan';
+
+
+my $m; BEGIN { use_ok($m = "Catalyst::Plugin::Authentication") }
+
+can_ok( $m, $_ ) for qw/user logout/;
diff --git a/t/05_password.t b/t/05_password.t
new file mode 100644 (file)
index 0000000..43f0b37
--- /dev/null
@@ -0,0 +1,13 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+use Test::More 'no_plan';
+
+
+my $m; BEGIN { use_ok($m = "Catalyst::Plugin::Authentication::Credential::Password") }
+
+can_ok($m, "login");
+
+
diff --git a/t/live_app.t b/t/live_app.t
new file mode 100644 (file)
index 0000000..8802f04
--- /dev/null
@@ -0,0 +1,66 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+use Test::More 'no_plan';
+
+{
+       package AuthTestApp;
+       use Catalyst qw/
+               Authentication
+               Authentication::Store::Minimal
+               Authentication::Credential::Password
+       /;
+
+       use Test::More;
+       use Test::Exception;
+
+       use Digest::MD5 qw/md5/;
+
+       our $users;
+
+       sub moose : Local {
+               my ( $self, $c ) = @_;
+
+               ok(!$c->user, "no user");
+               ok($c->login( "foo", "s3cr3t" ), "can login with clear");
+               is( $c->user, $users->{foo}, "user object is in proper place");
+               $c->logout;
+
+               ok(!$c->user, "no more user, after logout");
+
+               ok($c->login( "bar", "s3cr3t" ), "can login with crypted");
+               is( $c->user, $users->{bar}, "user object is in proper place");
+               $c->logout;
+
+               ok($c->login("gorch", "s3cr3t"), "can login with hashed");
+               is( $c->user, $users->{gorch}, "user object is in proper place");
+               $c->logout;
+
+               ok(!$c->login( "bar", "bad pass" ), "can't login with bad password");
+               ok(!$c->user, "no user");
+
+               throws_ok { $c->login( "baz", "foo" ) } qr/support.*mechanism/, "can't login without any supported mech";
+       }
+
+       __PACKAGE__->config->{authentication}{users} = $users = {
+               foo => {
+                       password => "s3cr3t",
+               },
+               bar => {
+                       crypted_password => crypt("s3cr3t", "x8"),
+               },
+               gorch => {
+                       hashed_password => md5("s3cr3t"),
+                       hash_algorithm => "MD5",
+               },
+               baz => {},
+       };
+
+       __PACKAGE__->setup;
+}
+
+use Catalyst::Test qw/AuthTestApp/;
+
+get("/moose");