X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FCatalyst%2FPlugin%2FAuthentication%2FCredential%2FTypeKey.pm;h=afdc39a1e1d0a2292529fa52e2062cef0a9fbac2;hb=4332afab97ea8e5e0f92bad849d69c273fbc2086;hp=9165678e0d2b2350b474bf83e75f76674c2b50da;hpb=6f758b52c41f81819f15d2e4520c2d0e376d35ca;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 9165678..afdc39a 100644 --- a/lib/Catalyst/Plugin/Authentication/Credential/TypeKey.pm +++ b/lib/Catalyst/Plugin/Authentication/Credential/TypeKey.pm @@ -4,13 +4,13 @@ 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'; +our $VERSION = '0.2'; sub setup { my $c = shift; @@ -18,8 +18,8 @@ sub setup { my $config = $c->config->{authentication}{typekey} ||= {}; $config->{typekey_object} ||= do { - $config->{user_class} ||= - "Catalyst::Plugin::Authentication::User::Hash"; + ( $config->{user_class} ||= + "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/) { + for ( grep { exists $config->{$_} } + qw/expires key_cache key_url token version skip_expiry_check/ ) + { $typekey->$_( $config->{$_} ); } @@ -38,33 +40,45 @@ sub setup { } sub authenticate_typekey { - my ( $c, %parameters ) = @_; + my ( $c, @p ) = @_; + + 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 = delete( $parameters{typekey_object} ) + my $typekey = $p && delete( $p->{typekey_object} ) || $config->{typekey_object}; - my @fields = qw/email name nick ts sig/; - - foreach my $field (@fields) { - $parameters{$field} ||= $c->req->param($field) || return; - } + $p ||= $c->req; - if ( $typekey->verify( \%parameters ) ) { - $c->log->debug("Successfully authenticated user '$parameters{name}'.") + 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( \%parameters ); + $user = $store->get_user( $res->{name}, $p, $res ); } - else { + + if ( !$user ) { my $user_class = $config->{user_class}; - $user_class->require or die $@; - $user = $user_class->new( \%parameters ); + $user = $user_class->new($res); } $c->set_authenticated($user); @@ -74,7 +88,9 @@ sub authenticate_typekey { else { $c->log->debug( sprintf "Failed to authenticate user '%s'. Reason: '%s'", - $parameters{name}, $typekey->errstr ) + $p->{name} || $p->param("name"), + $typekey->errstr + ) if $c->debug; return; @@ -92,19 +108,26 @@ for Catalyst. =head1 SYNOPSIS - use Catalyst qw[Authentication::Credential::TypeKey]; + use Catalyst qw/Authentication::Credential::TypeKey/; - MyApp->config->{authenticate}->{typekey} = { - token => 'xxxxxxxxxxxxxxxxxxxx' + MyApp->config->{authentication}{typekey} = { + token => 'xxxxxxxxxxxxxxxxxxxx', }; sub foo : Local { my ( $self, $c ) = @_; - if ( $c->authenticate_typekey( name => $name, email => $email, ... ) ) { + if ( $c->authenticate_typekey ) { + + # you can also specify the params manually: $c->authenticate_typekey( + # name => $name, + # email => $email, + # ... + #) + # successful autentication - $c->user; # blah + $c->user; # this is set } } @@ -112,7 +135,7 @@ for Catalyst. sub auto : Private { my ( $self, $c ) = @_; - $c->authenticate_typekey; # uses $c->req->params + $c->authenticate_typekey; # uses $c->req return 1; } @@ -124,13 +147,13 @@ L. =head1 METHODS -=item authenticate_typekey %parameters +=head3 authenticate_typekey %parameters -=item authenticate_typekey +=head3 authenticate_typekey -=item EXTENDED METHODS +=head3 EXTENDED METHODS -=item setup +=head3 setup Fills the config with defaults. @@ -169,14 +192,20 @@ A store (or store name) to retrieve the user from. When a user is successfully authenticated it will call this: - $store->get_user( $parameters ); + $store->get_user( $name, $parameters, $result_of_verify ); + +Where C<$parameters> is a the hash reference passed to +L, and C<$result_of_verify> is the value returned by +L. -Where C<$parameters> is a the hash reference passed to L. +If this is unset, L will +be used instead. =item user_class -If C is not set it will use this class to instantiate an object, -calling C on the class with the same C<$parameters> hash ref. +If C or the default store returns nothing from get_user, this class +will be used to instantiate an object by calling C on the class with the +return value from L. =back