Bug fixes and test from nilsonsfj - Cheers!
Matt S Trout [Tue, 17 Jul 2007 16:59:23 +0000 (16:59 +0000)]
r53844@cain (orig r6509):  jayk | 2007-07-06 02:16:21 +0000

lib/Catalyst/Plugin/Authentication/Credential/Password.pm
lib/Catalyst/Plugin/Authentication/Store/Minimal.pm
lib/Catalyst/Plugin/Authentication/User.pm
lib/Catalyst/Plugin/Authentication/User/Hash.pm
t/AuthRealmTestApp.pm [new file with mode: 0644]
t/live_app_realms.t [new file with mode: 0644]

index 0baa52e..ee7ca7e 100644 (file)
@@ -25,7 +25,7 @@ sub new {
     $self->_config->{'password_hash_type'} ||= 'SHA-1';
     
     my $passwordtype = $self->_config->{'password_type'};
-    if (!grep /$passwordtype/, ('clear', 'hashed', 'salted_hash', 'crypted', 'self_check')) {
+    if (!grep /$passwordtype/, ('none', 'clear', 'hashed', 'salted_hash', 'crypted', 'self_check')) {
         Catalyst::Exception->throw(__PACKAGE__ . " used with unsupported password type: " . $self->_config->{'password_type'});
     }
     return $self;
index 1840377..95029cc 100644 (file)
@@ -42,6 +42,11 @@ sub find_user {
     my $user = $self->userhash->{$id};
 
     if ( ref $user ) {
+        if (     Scalar::Util::blessed($user) 
+             and $user->isa('Catalyst::Plugin::Authentication::User::Hash') ) 
+        {
+            return $user;
+        }
         if ( ref $user eq "HASH" ) {
             $user->{id} ||= $id;
             return bless $user, "Catalyst::Plugin::Authentication::User::Hash";
index efdd6e5..b12f848 100644 (file)
@@ -49,7 +49,7 @@ sub get {
     my ($self, $field) = @_;
     
     my $object;
-    if ($object = $self->get_object && $object->can($field)) {
+    if ($object = $self->get_object and $object->can($field)) {
         return $object->$field();
     } else {
         return undef;
index ffb8300..1d72bc7 100644 (file)
@@ -19,6 +19,9 @@ sub AUTOLOAD {
     $self->_accessor( $key, @_ );
 }
 
+# this class effectively handles any method calls
+sub can { 1 }
+
 sub id {
     my $self = shift;
     $self->_accessor( "id", @_ );
diff --git a/t/AuthRealmTestApp.pm b/t/AuthRealmTestApp.pm
new file mode 100644 (file)
index 0000000..9467275
--- /dev/null
@@ -0,0 +1,115 @@
+package AuthRealmTestApp;
+use warnings;
+use strict;
+
+use Catalyst qw/Authentication/;
+
+use Test::More;
+use Test::Exception;
+
+our $members = {
+    bob => {
+        password => "s00p3r"
+    },
+    william => {
+        password => "s3cr3t"
+    }
+};
+
+our $admins = {
+    joe => {
+        password => "31337"
+    }
+};
+
+sub moose : Local {
+       my ( $self, $c ) = @_;
+
+       ok(!$c->user, "no user");
+
+    while ( my ($user, $info) = each %$members ) {
+        
+        ok( 
+            $c->authenticate( 
+                { username => $user, password => $info->{password} }, 
+                'members' 
+            ), 
+            "user $user authentication" 
+        );
+
+        # check existing realms
+        ok( $c->user_in_realm('members'), "user in members realm");
+        ok(!$c->user_in_realm('admins'),  "user not in admins realm");
+
+        # check an invalid realm
+        ok(!$c->user_in_realm('foobar'), "user not in foobar realm");
+
+        # check if we've got the right user
+        is( $c->user, $info, "user object is in proper place");
+
+        $c->logout;
+
+           # sanity check
+        ok(!$c->user, "no more user after logout");
+
+    }
+
+    while ( my ($user, $info) = each %$admins ) {
+        
+        ok( 
+            $c->authenticate( 
+                { username => $user, password => $info->{password} }, 
+                'admins' 
+            ), 
+            "user $user authentication" 
+        );
+
+        # check existing realms
+        ok(!$c->user_in_realm('members'), "user not in members realm");
+        ok( $c->user_in_realm('admins'),  "user in admins realm");
+
+        # check an invalid realm
+        ok(!$c->user_in_realm('foobar'), "user not in foobar realm");
+
+        # check if we've got the right user
+        is( $c->user, $info, "user object is in proper place");
+
+        $c->logout;
+
+           # sanity check
+        ok(!$c->user, "no more user after logout");
+
+    }
+
+       $c->res->body( "ok" );
+}
+
+__PACKAGE__->config->{authentication} = {  
+    default_realm => 'members',
+    realms => {
+        members => {
+            credential => {
+                class => 'Password',
+                password_field => 'password',
+                password_type => 'clear'
+            },
+            store => {
+                class => 'Minimal',
+                users => $members             
+            }
+        },
+        admins => {
+            credential => {
+                class => 'Password',
+                password_field => 'password',
+                password_type => 'clear'
+            },
+            store => {
+                class => 'Minimal',
+                users => $admins               
+            }
+        }
+    }
+};
+
+__PACKAGE__->setup;
diff --git a/t/live_app_realms.t b/t/live_app_realms.t
new file mode 100644 (file)
index 0000000..8773483
--- /dev/null
@@ -0,0 +1,15 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+use Test::More;
+
+BEGIN {
+    plan "no_plan";
+}
+
+use lib 't/lib';
+use Catalyst::Test qw/AuthRealmTestApp/;
+
+ok(get("/moose"), "get ok");