X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FCatalyst%2FPlugin%2FAuthentication%2FCredential%2FTypeKey.pm;h=4e6ecd2267b6f36cb20bc8148f951039d5c4ca36;hb=97676d77bae8c376a2189468209e29735656cda6;hp=ba4511c9d797db710e28b76de57b81b2ee809abe;hpb=8a4cd23ab3f06a2f14e366fc0629bba279959e7f;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 ba4511c..4e6ecd2 100644 --- a/lib/Catalyst/Plugin/Authentication/Credential/TypeKey.pm +++ b/lib/Catalyst/Plugin/Authentication/Credential/TypeKey.pm @@ -1,62 +1,81 @@ package Catalyst::Plugin::Authentication::Credential::TypeKey; use strict; +use warnings; + use Authen::TypeKey; use Carp (); use File::Spec; +use Catalyst::Utils (); +use NEXT; +use UNIVERSAL::require; our $VERSION = '0.1'; -our $PARAMETERS = qw[ - email - name - nick - ts - sig -]; +sub setup { + my $c = shift; -sub authenticate_typekey { - my ( $c, $email, $name, $nick, $ts, $sig, $options ) = @_; + my $config = $c->config->{authentication}{typekey} ||= {}; - unless ( @_ == 6 || ( @_ == 7 && ref($options) eq 'HASH' ) ) { - Carp::croak('usage: $c->authenticate_typekey( $email, $name, $nick, $ts, $sig [, \%options ] )'); - } + $config->{typekey_object} ||= do { + ( $config->{user_class} ||= + "Catalyst::Plugin::Authentication::User::Hash" )->require; - unless ( @_ == 7 ) { - $options = {}; - } + $config->{key_cache} ||= + File::Spec->catfile( Catalyst::Utils::class2tempdir( $c, 1 ), + 'regkeys.txt' ); - my $config = $c->config->{authenticate}->{typekey}; - - my $token = $options->{token} || $config->{token} || undef; - my $expires = $options->{expires} || $config->{expires} || 0; - my $version = $options->{version} || $config->{version} || 1.1; - my $cache = $options->{cache} || $config->{cache} || File::Spec->catfile( File::Spec->tmpdir, 'regkeys.txt' ); - my $keys = $options->{keys} || $config->{keys} || 'http://www.typekey.com/extras/regkeys.txt'; - - my $typekey = Authen::TypeKey->new; - $typekey->expires($expires); - $typekey->key_cache($cache); - $typekey->key_url($keys); - $typekey->token($token); - $typekey->version($version); - - my $parameters = { - email => $email, - name => $name, - nick => $nick, - ts => $ts, - sig => $sig + my $typekey = Authen::TypeKey->new; + + for (grep { exists $config->{$_} } qw/expires key_cache key_url token version skip_expiry_check/) { + $typekey->$_( $config->{$_} ); + } + + $typekey; }; - unless ( $typekey->verify($parameters) ) { - my $error = $typekey->errstr; - $c->log->debug(qq/Failed to authenticate user '$name'. Reason: '$error'/); - return 0; + $c->NEXT::setup(@_); +} + +sub authenticate_typekey { + my ( $c, @p ) = @_; + my $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 ) ) { + $c->log->debug("Successfully authenticated user '$res->{name}'.") + if $c->debug; + + my $user; + + if ( my $store = $config->{auth_store} ) { + $store = $c->get_auth_store($store) unless ref $store; + $user = $store->get_user( $p, $res ); + } + + if ( !$user ) { + my $user_class = $config->{user_class}; + $user = $user_class->new( $res ); + } + + $c->set_authenticated($user); + + return 1; } + else { + $c->log->debug( + sprintf "Failed to authenticate user '%s'. Reason: '%s'", + $p->{name} || $p->param("name"), $typekey->errstr ) + if $c->debug; - $c->log->debug( qq/Successfully authenticated user '$name'./); - return 1; + return; + } } 1; @@ -66,30 +85,139 @@ __END__ =head1 NAME Catalyst::Plugin::Authentication::Credential::TypeKey - TypeKey Authentication +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', }; - if ( $c->authenticate_typekey( $email, $name, $nick, $ts, $sig ) ) { - # successful autentication - } + sub foo : Local { + my ( $self, $c ) = @_; + + if ( $c->authenticate_typekey ) { + + # you can also specify the params manually: $c->authenticate_typekey( + # name => $name, + # email => $email, + # ... + #) + + # successful autentication + + $c->user; # this is set + } + } + + + sub auto : Private { + my ( $self, $c ) = @_; + + $c->authenticate_typekey; # uses $c->req + + return 1; + } + +=head1 TYPEKEY BROKED-NESS + +Please watch: + + http://rt.cpan.org/NoAuth/Bugs.html?Dist=Authen-TypeKey + +I could only get this to properly work with TypeKey version 1 (not 1.1). + +To get around this problem configure the plugin to use version 1: + + __PACKAGE__->config( + authentication => { + typekey => { + version => 1, + token => ..., # doesn't really matter in version 1 + }, + }, + ); =head1 DESCRIPTION -TypeKey Authentication. +This module integrates L with +L. + +=head1 METHODS + +=item authenticate_typekey %parameters + +=item authenticate_typekey + +=item EXTENDED METHODS + +=item setup + +Fills the config with defaults. + +=head1 CONFIGURATION + +C<<$c->config->{autentication}{typekey}>> is a hash with these fields (all can +be left out): + +=over 4 + +=item typekey_object + +If this field does not exist an L object will be created based +on the other param and put here. + +=item expires + +=item key_url + +=item token + +=item version + +See L for all of these. If they aren't specified +L's defaults will be used. + +=item key_cache + +Also see L. + +Defaults to C under L. + +=item auth_store + +A store (or store name) to retrieve the user from. + +When a user is successfully authenticated it will call this: + + $store->get_user( $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. + +If this is unset, L will +be used instead. + +=item user_class + +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 =head1 SEE ALSO -L, L. +L, L, L. =head1 AUTHOR -Christian Hansen, C +Christian Hansen + +Yuval Kogman, C =head1 LICENSE