Support receiving user objects with authenticate_typekey
Yuval Kogman [Sun, 27 Nov 2005 20:46:57 +0000 (20:46 +0000)]
lib/Catalyst/Plugin/Authentication/Credential/TypeKey.pm
t/basic.t

index 4e6ecd2..b07eae6 100644 (file)
@@ -4,11 +4,11 @@ use strict;
 use warnings;
 
 use Authen::TypeKey;
-use Carp ();
 use File::Spec;
 use Catalyst::Utils ();
 use NEXT;
 use UNIVERSAL::require;
+use Scalar::Util ();
 
 our $VERSION = '0.1';
 
@@ -19,7 +19,7 @@ sub setup {
 
     $config->{typekey_object} ||= do {
         ( $config->{user_class} ||=
-          "Catalyst::Plugin::Authentication::User::Hash" )->require;
+              "Catalyst::Plugin::Authentication::User::Hash" )->require;
 
         $config->{key_cache} ||=
           File::Spec->catfile( Catalyst::Utils::class2tempdir( $c, 1 ),
@@ -27,7 +27,9 @@ sub setup {
 
         my $typekey = Authen::TypeKey->new;
 
-        for (grep { exists $config->{$_} } qw/expires key_cache key_url token version skip_expiry_check/) {
+        for ( grep { exists $config->{$_} }
+            qw/expires key_cache key_url token version skip_expiry_check/ )
+        {
             $typekey->$_( $config->{$_} );
         }
 
@@ -39,29 +41,44 @@ sub setup {
 
 sub authenticate_typekey {
     my ( $c, @p ) = @_;
-       my $p = @p ? { @p } : undef;
+
+    my ( $user, $p );
+    if ( @p == 1 ) {
+        if ( Scalar::Util::blessed( $p[0] ) ) {
+            $user = $p[0];
+            Catalyst::Exception->throw(
+                    "Attempted to authenticate user object, but "
+                  . "user doesnt't support 'typekey_credentials'" )
+              unless $user->supports(qw/typekey_credentials/);
+            $p = $user->typekey_credentials;
+        }
+        else {
+            $p = $p[0];
+        }
+    }
+    else {
+        $p = @p ? {@p} : undef;
+    }
 
     my $config = $c->config->{authentication}{typekey};
 
     my $typekey = $p && delete( $p->{typekey_object} )
       || $config->{typekey_object};
 
-       $p ||= $c->req;
-         
-    if ( my $res = $typekey->verify( $p ) ) {
+    $p ||= $c->req;
+
+    if ( my $res = $typekey->verify($p) ) {
         $c->log->debug("Successfully authenticated user '$res->{name}'.")
           if $c->debug;
 
-        my $user;
-
-        if ( my $store = $config->{auth_store} ) {
+        if ( !$user and my $store = $config->{auth_store} ) {
             $store = $c->get_auth_store($store) unless ref $store;
             $user = $store->get_user( $p, $res );
         }
 
-               if ( !$user ) {
+        if ( !$user ) {
             my $user_class = $config->{user_class};
-            $user = $user_class->new( $res );
+            $user = $user_class->new($res);
         }
 
         $c->set_authenticated($user);
@@ -71,7 +88,9 @@ sub authenticate_typekey {
     else {
         $c->log->debug(
             sprintf "Failed to authenticate user '%s'. Reason: '%s'",
-            $p->{name} || $p->param("name"), $typekey->errstr )
+            $p->{name} || $p->param("name"),
+            $typekey->errstr
+          )
           if $c->debug;
 
         return;
index 3ec92c0..3164863 100644 (file)
--- a/t/basic.t
+++ b/t/basic.t
@@ -16,7 +16,7 @@ BEGIN { use_ok( $m = "Catalyst::Plugin::Authentication::Credential::TypeKey" ) }
 
 # from 01-verify.t in Authen-TypeKey-0.04
 my %user = (
-       ts    => '1091163746',
+    ts    => '1091163746',
     email => 'bentwo@stupidfool.org',
     name  => 'Melody',
     nick  => 'foobar baz',
@@ -27,18 +27,23 @@ $req->set_always( params => {} );
 $req->mock( param => sub { $_[0]->params->{ $_[1] } } );
 
 my $tk = Test::MockObject->new;
-$tk->mock("verify", sub {
-       my ( $self, $p ) = @_;
-
-       if ( blessed($p) ) {
-               return \%user if ( $p->param("sig") );
-       } else {
-               return \%user if ( $p->{sig} );
-       }
-});
+$tk->mock(
+    "verify",
+    sub {
+        my ( $self, $p ) = @_;
+
+        if ( blessed($p) ) {
+            return \%user if ( $p->param("sig") );
+        }
+        else {
+            return \%user if ( $p->{sig} );
+        }
+    }
+);
 
 my $store = Test::MockObject->new;
-$store->mock( get_user => sub { shift; Catalyst::Plugin::Authentication::User::Hash->new( @_ ) } );
+$store->mock( get_user =>
+      sub { shift; Catalyst::Plugin::Authentication::User::Hash->new(@_) } );
 
 my $c = Test::MockObject::Extends->new($m);
 $c->set_always( config => {} );
@@ -71,10 +76,9 @@ ok( !$c->called("set_authenticated"), "nothing was authenticated" );
 
 $_->clear for $c, $tk;
 
-%{ $req->params } = my %vars = (
-    %user,
-       sig   => 'GWwAIXbkb2xNrQO2e/r2LDl14ek=:U5+tDsPM0+EXeKzFWsosizG7+VU=',
-);
+%{ $req->params } = my %vars =
+  ( %user, sig => 'GWwAIXbkb2xNrQO2e/r2LDl14ek=:U5+tDsPM0+EXeKzFWsosizG7+VU=',
+  );
 
 lives_ok {
     $c->authenticate_typekey;
@@ -96,21 +100,33 @@ lives_ok {
 
 $tk->called_ok("verify");
 $c->called_ok( "set_authenticated", "authenticated" );
-$store->called_ok( "get_user", "user retrieved from store" );
-
+$store->called_ok( "get_user",      "user retrieved from store" );
 
 $_->clear for $c, $tk, $store;
 
 $tk->set_false("verify");
 
-
 lives_ok {
     $c->authenticate_typekey(%vars);
   }
   "can try to auth with args";
 
 $tk->called_ok("verify");
-ok( !$c->called( "set_authenticated" ), "authenticated" );
-ok( !$store->called( "get_user" ), "no user retrieved from store");
+ok( !$c->called("set_authenticated"), "authenticated" );
+ok( !$store->called("get_user"),      "no user retrieved from store" );
+
+$c->logout;
 
+$tk->set_true("verify");
+$c->clear;
+
+ok(
+    $c->authenticate_typekey(
+        my $user = Catalyst::Plugin::Authentication::User::Hash->new(
+            typekey_credentials => { %vars }
+        )
+    ),
+    "can authenticate with user object"
+);
 
+$c->called_ok("set_authenticated");