X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FCatalyst%2FPlugin%2FAuthentication%2FCredential%2FTypeKey.pm;h=b07eae63b1ca501cf3f41c13af2ac771eb35cfeb;hb=209fb56bd9d1baa3021db6fafa2e0b438c58efef;hp=4e6ecd2267b6f36cb20bc8148f951039d5c4ca36;hpb=97676d77bae8c376a2189468209e29735656cda6;p=catagits%2FCatalyst-Authentication-Credential-HTTP-Proxy.git diff --git a/lib/Catalyst/Plugin/Authentication/Credential/TypeKey.pm b/lib/Catalyst/Plugin/Authentication/Credential/TypeKey.pm index 4e6ecd2..b07eae6 100644 --- a/lib/Catalyst/Plugin/Authentication/Credential/TypeKey.pm +++ b/lib/Catalyst/Plugin/Authentication/Credential/TypeKey.pm @@ -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;