X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FCatalyst%2FAuthentication%2FCredential%2FOpenID.pm;h=de9a9f34b7bcbe239ef957bffafbb707c1953126;hb=47a60d4195569cb204a8b2ed47ed2b22953e3894;hp=7c2f61545e247c28196eac7e0c2b171e15de56de;hpb=a47955edd5162c38772e0ca76ecfed9ad3983c8e;p=catagits%2FCatalyst-Authentication-Credential-OpenID.git diff --git a/lib/Catalyst/Authentication/Credential/OpenID.pm b/lib/Catalyst/Authentication/Credential/OpenID.pm index 7c2f615..de9a9f3 100644 --- a/lib/Catalyst/Authentication/Credential/OpenID.pm +++ b/lib/Catalyst/Authentication/Credential/OpenID.pm @@ -1,14 +1,13 @@ package Catalyst::Authentication::Credential::OpenID; use strict; -use warnings; -no warnings "uninitialized"; +# use warnings; no warnings "uninitialized"; # for testing, not production use parent "Class::Accessor::Fast"; BEGIN { __PACKAGE__->mk_accessors(qw/ _config realm debug secret /); } -our $VERSION = "0.06"; +our $VERSION = "0.16"; use Net::OpenID::Consumer; use Catalyst::Exception (); @@ -33,8 +32,10 @@ sub new : method { ); $secret = substr($secret,0,255) if length $secret > 255; - $self->secret( $secret ); - $self->_config->{ua_class} ||= "LWPx::ParanoidAgent"; + $self->secret($secret); + # If user has no preference we prefer L::PA b/c it can prevent DoS attacks. + $self->_config->{ua_class} ||= eval "use LWPx::ParanoidAgent" ? + "LWPx::ParanoidAgent" : "LWP::UserAgent"; my $agent_class = $self->_config->{ua_class}; eval "require $agent_class" @@ -59,18 +60,42 @@ sub authenticate : method { $claimed_uri ||= $c->req->method eq 'GET' ? $c->req->query_params->{ $field } : $c->req->body_params->{ $field }; + my $csr = Net::OpenID::Consumer->new( ua => $self->_config->{ua_class}->new(%{$self->_config->{ua_args} || {}}), args => $c->req->params, consumer_secret => $self->secret, ); + if ( $self->_config->{extension_args} and $self->debug ) + { + $c->log->info("The configuration key 'extension_args' is deprecated; use 'extensions'"); + } + + my @extensions = $self->_config->{extensions} ? + @{ $self->_config->{extensions} } : $self->_config->{extension_args} ? + @{ $self->_config->{extension_args} } : (); + if ( $claimed_uri ) { my $current = $c->uri_for($c->req->uri->path); # clear query/fragment... - my $identity = $csr->claimed_identity($claimed_uri) - or Catalyst::Exception->throw($csr->err); + my $identity = $csr->claimed_identity($claimed_uri); + unless ( $identity ) + { + if ( $self->_config->{errors_are_fatal} ) + { + Catalyst::Exception->throw($csr->err); + } + else + { + $c->log->error($csr->err . " -- $claimed_uri"); + $c->detach(); + } + } + + $identity->set_extension_args(@extensions) + if @extensions; my $check_url = $identity->check_url( return_to => $current . '?openid-check=1', @@ -78,7 +103,7 @@ sub authenticate : method { delayed_return => 1, ); $c->res->redirect($check_url); - return; + $c->detach(); } elsif ( $c->req->params->{'openid-check'} ) { @@ -96,6 +121,12 @@ sub authenticate : method { # This is where we ought to build an OpenID user and verify against the spec. my $user = +{ map { $_ => scalar $identity->$_ } qw( url display rss atom foaf declared_rss declared_atom declared_foaf foafmaker ) }; + # Dude, I did not design the array as hash spec. Don't curse me [apv]. + my %flat = @extensions; + for my $key ( keys %flat ) + { + $user->{extensions}->{$key} = $identity->signed_extension_fields($key); + } my $user_obj = $realm->find_user($user, $c); @@ -105,14 +136,16 @@ sub authenticate : method { } else { - $c->log->debug("Verified OpenID identity failed to load with find_user; bad user_class? Try 'Null.'") if $c->debug; + $c->log->debug("Verified OpenID identity failed to load with find_user; bad user_class? Try 'Null.'") if $self->debug; return; } } else { - Catalyst::Exception->throw("Error validating identity: " . - $csr->err); + $self->_config->{errors_are_fatal} ? + Catalyst::Exception->throw("Error validating identity: " . $csr->err) + : + $c->log->error( $csr->err); } } return; @@ -128,7 +161,19 @@ Catalyst::Authentication::Credential::OpenID - OpenID credential for Catalyst::P =head1 VERSION -0.06 +0.16 + +=head1 BACKWARDS COMPATIBILITY CHANGES + +=head2 EXTENSION_ARGS v EXTENSIONS + +B: The extensions were previously configured under the key C. They are now configured under C. This prevents the need for double configuration but it breaks extensions in your application if you do not change the name. The old version is supported for now but may be phased out at any time. + +As previously noted, L, I have not tested the extensions. I would be grateful for any feedback or, better, tests. + +=head2 FATALS + +The problems encountered by failed OpenID operations have always been fatals in the past. This is unexpected behavior for most users as it differs from other credentials. Authentication errors here are no longer fatal. Debug/error output is improved to offset the loss of information. If for some reason you would prefer the legacy/fatal behavior, set the configuration variable C to a true value. =head1 SYNOPSIS @@ -149,8 +194,8 @@ Somewhere in myapp.conf- class OpenID + ua_class LWP::UserAgent - ua_class LWPx::ParanoidAgent @@ -163,7 +208,7 @@ Or in your myapp.yml if you're using L instead- openid: credential: class: OpenID - ua_class: LWPx::ParanoidAgent + ua_class: LWP::UserAgent In a controller, perhaps C- @@ -191,10 +236,10 @@ And a L