| 1 | package Catalyst::Plugin::Authentication::Credential::OpenID; | ||
| 2 | |||
| 3 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | our $VERSION = '0.03'; | ||
| 6 | |||
| 7 | 0 | ▶ | use Net::OpenID::Consumer; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 8 | use LWPx::ParanoidAgent; | ||
| 9 | use UNIVERSAL::require; | ||
| 10 | |||
| 11 | sub setup { | ||
| 12 | my $c = shift; | ||
| 13 | my $config = $c->config->{authentication}->{openid} ||= {}; | ||
| 14 | ( $config->{user_class} | ||
| 15 | ||= "Catalyst::Plugin::Authentication::User::Hash" )->require; | ||
| 16 | $c->NEXT::setup(@_); | ||
| 17 | } | ||
| 18 | |||
| 19 | sub authenticate_openid { | ||
| 20 | my($c, $uri) = @_; | ||
| 21 | |||
| 22 | my $config = $c->config->{authentication}->{openid}; | ||
| 23 | |||
| 24 | my $csr = Net::OpenID::Consumer->new( | ||
| 25 | ua => LWPx::ParanoidAgent->new, | ||
| 26 | args => $c->req->params, | ||
| 27 | consumer_secret => sub { $_[0] }, | ||
| 28 | ); | ||
| 29 | |||
| 30 | my @try_params = qw( openid_url openid_identifier claimed_uri ); | ||
| 31 | if ($uri ||= (grep defined, @{$c->req->params}{@try_params})[0]) { | ||
| 32 | my $current = $c->req->uri; | ||
| 33 | $current->query(undef); # no query | ||
| 34 | my $identity = $csr->claimed_identity($uri) | ||
| 35 | or Catalyst::Exception->throw($csr->err); | ||
| 36 | my $check_url = $identity->check_url( | ||
| 37 | return_to => $current . '?openid-check=1', | ||
| 38 | trust_root => $current, | ||
| 39 | delayed_return => 1, | ||
| 40 | ); | ||
| 41 | $c->res->redirect($check_url); | ||
| 42 | return 0; | ||
| 43 | } elsif ($c->req->param('openid-check')) { | ||
| 44 | if (my $setup_url = $csr->user_setup_url) { | ||
| 45 | $c->res->redirect($setup_url); | ||
| 46 | return 0; | ||
| 47 | } elsif ($csr->user_cancel) { | ||
| 48 | return 0; | ||
| 49 | } elsif (my $identity = $csr->verified_identity) { | ||
| 50 | my $user = +{ map { $_ => scalar $identity->$_ } | ||
| 51 | qw( url display rss atom foaf declared_rss declared_atom declared_foaf foafmaker ) }; | ||
| 52 | |||
| 53 | my $store = $config->{store} || $c->default_auth_store; | ||
| 54 | if ( $store | ||
| 55 | and my $store_user | ||
| 56 | = $store->get_user( $user->{url}, $user ) ) { | ||
| 57 | $c->set_authenticated($store_user); | ||
| 58 | } else { | ||
| 59 | $user = $config->{user_class}->new($user); | ||
| 60 | $c->set_authenticated($user); | ||
| 61 | } | ||
| 62 | return 1; | ||
| 63 | } else { | ||
| 64 | Catalyst::Exception->throw("Error validating identity: " . | ||
| 65 | $csr->err); | ||
| 66 | } | ||
| 67 | } else { | ||
| 68 | return 0; | ||
| 69 | } | ||
| 70 | } | ||
| 71 | |||
| 72 | 1; | ||
| 73 | __END__ | ||