Show
Ignore:
Timestamp:
11/21/08 20:27:21 (5 years ago)
Author:
masaki
Message:

* テスト移動
* Net::OpenID::Consumer 1.02 を使う
* extension 周りを変更して SREG 特別扱いをやめた
* ドキュメントを本家からパクった

Location:
lang/perl/Catalyst-Authentication-Credential-OpenID/trunk
Files:
2 added
4 modified
3 moved

Legend:

Unmodified
Added
Removed
  • lang/perl/Catalyst-Authentication-Credential-OpenID/trunk/MANIFEST

    r7265 r24569  
    22inc/Module/AutoInstall.pm 
    33inc/Module/Install.pm 
     4inc/Module/Install/AuthorTests.pm 
    45inc/Module/Install/AutoInstall.pm 
    56inc/Module/Install/Base.pm 
     
    1819inc/Test/Builder/Module.pm 
    1920inc/Test/More.pm 
     21inc/Test/use/ok.pm 
    2022lib/Catalyst/Authentication/Credential/OpenID.pm 
    2123Makefile.PL 
     
    2426README 
    2527t/00_compile.t 
     28xt/01_pod.t 
     29xt/02_podcoverage.t 
     30xt/03_podspell.t 
  • lang/perl/Catalyst-Authentication-Credential-OpenID/trunk/Makefile.PL

    r15895 r24569  
    11use inc::Module::Install; 
    2 name('Catalyst-Authentication-Credential-OpenID'); 
    3 all_from('lib/Catalyst/Authentication/Credential/OpenID.pm'); 
     2name 'Catalyst-Authentication-Credential-OpenID'; 
     3all_from 'lib/Catalyst/Authentication/Credential/OpenID.pm'; 
    44 
    5 requires('Catalyst::Plugin::Authentication', '0.10006'); 
    6 requires('Catalyst::Utils'); 
    7 requires('Class::Accessor::Fast'); 
    8 requires('Net::OpenID::Consumer'); 
     5requires 'Catalyst::Plugin::Authentication' => 0.10008; 
     6requires 'Catalyst::Utils'; 
     7requires 'parent'; 
     8requires 'Class::Accessor::Fast'; 
     9requires 'Net::OpenID::Consumer' => 1.02; 
    910 
    10 build_requires('Test::More'); 
     11tests 't/*.t'; 
     12test_requires 'Test::More'; 
     13test_requires 'Test::use::ok'; 
     14author_tests 'xt'; 
    1115use_test_base; 
    1216 
  • lang/perl/Catalyst-Authentication-Credential-OpenID/trunk/lib/Catalyst/Authentication/Credential/OpenID.pm

    r16912 r24569  
    33use strict; 
    44use warnings; 
    5 use base 'Class::Accessor::Fast'; 
     5use parent 'Class::Accessor::Fast'; 
    66use Catalyst::Utils; 
    7 use Net::OpenID::Consumer; 
     7use Net::OpenID::Consumer 1.02; 
    88 
    99our $VERSION = '0.02'; 
     
    1818    }); 
    1919 
    20     $self->config->{openid_field} = 'openid_identifier'; 
     20    $self->config->{openid_field} ||= 'openid_identifier'; 
    2121 
    2222    my $secret = $self->config->{consumer_secret} || 
     
    3131    $self->csr($csr); 
    3232 
    33     $self; 
     33    return $self; 
    3434} 
    3535 
     
    3737    my ($self, $c, $realm, $authinfo) = @_; 
    3838 
     39    $c->log->debug("authenticate() called from " . $c->req->uri) if $c->debug; 
     40 
     41    my $req = $c->req; 
     42 
    3943    my $field = $self->config->{openid_field}; 
    40     my $claimed_uri = $authinfo->{$field} || $c->req->params->{$field}; 
     44    my $claimed_uri = $authinfo->{$field} 
     45        || $req->method eq 'GET' ? $req->query_params->{$field} : $req->body_params->{$field}; 
    4146 
    4247    my $csr = $self->csr; 
    43     $csr->required_root($c->req->base); 
    44     $csr->args($c->req->params); 
     48    $csr->required_root($req->base); 
     49    $csr->args($req->params); 
    4550    $csr->cache($c->cache) if $c->can('cache'); 
    4651 
     
    5257        } 
    5358 
    54         # set extension 
     59        # set extension args 
    5560        while (my ($ns, $args) = each %{ $self->config->{extension_args} || {} }) { 
    5661            $identity->set_extension_args($ns, $args); 
    5762        } 
    5863 
    59         my $return_to = $c->req->uri->clone; 
     64        my $return_to = $req->uri->clone; 
     65        $return_to->fragment(undef); 
    6066        $return_to->query_form(openid_check => 1); 
    6167        my $check_url = $identity->check_url( 
    6268            return_to      => $return_to, 
    63             trust_root     => $c->req->base, 
     69            trust_root     => $req->base, 
    6470            delayed_return => 1, 
    6571        ); 
    6672 
    6773        $c->res->redirect($check_url); 
    68         return; 
     74        $c->detach; 
    6975    } 
    70     elsif ($c->req->param('openid_check')) { 
    71         if (my $setup_url = $csr->user_setup_url) { 
    72             $c->res->redirect($setup_url); 
    73             return; 
    74         } 
    75         elsif ($csr->user_cancel) { 
    76             $c->log->debug('User cancel authentication') if $c->debug; 
    77             return; 
    78         } 
    79         elsif (my $identity = $csr->verified_identity) { 
    80             my $userinfo = +{ map { $_ => scalar $identity->$_ } 
    81                 qw( url display rss atom foaf declared_rss declared_atom declared_foaf foafmaker ) }; 
    82  
    83             # override display using sreg nickname 
    84             my $sreg_ns = 'http://openid.net/extensions/sreg/1.1'; 
    85             if (my $sreg = $identity->extension_fields($sreg_ns)) { 
    86                 $userinfo->{display} = $sreg->{nickname} || $sreg->{fullname} || $userinfo->{display}; 
    87             } 
    88  
    89             $userinfo = Catalyst::Utils::merge_hashes($authinfo, $userinfo); 
    90             my $user = $realm->find_user($userinfo, $c); 
    91             if (ref($user)) { 
    92                 return $user; 
    93             } 
    94             else { 
    95                 $c->log->error("Unable to locate user matching user info provided"); 
    96                 return; 
    97             } 
    98         } 
    99         else { 
    100             $c->log->error("Error validating identity: " . $csr->err); 
    101             return; 
    102         } 
    103     } 
    104     else { 
    105         return; 
     76    elsif ($c->req->params->{openid_check}) { 
     77        my $user; 
     78 
     79        $csr->handle_server_response( 
     80            not_openid => sub { 
     81                $c->log->error('Not an OpenID Message'); 
     82            }, 
     83            setup_required => sub { 
     84                my $setup_url = shift; 
     85                $c->res->redirect($setup_url); 
     86                $c->detach; 
     87            }, 
     88            cancelled => sub { 
     89                $c->log->debug('User cancel authentication') if $c->debug; 
     90            }, 
     91            verified => sub { 
     92                my $vident = shift; 
     93                my $userinfo = +{ map { $_ => scalar $vident->$_ } qw( 
     94                    url display rss atom foaf declared_rss declared_atom declared_foaf foafmaker 
     95                )}; 
     96 
     97                # get extensions args 
     98                my @ns = keys %{ $self->config->{extension_args} || {} }; 
     99                if (@ns) { 
     100                    $userinfo->{extensions} = {}; 
     101 
     102                    for my $ns (@ns) { 
     103                        my $args = $vident->extension_fields($ns) || {}; 
     104                        $userinfo->{extensions}->{$ns} = $args; 
     105                    } 
     106                } 
     107 
     108                $userinfo = Catalyst::Utils::merge_hashes($authinfo, $userinfo); 
     109 
     110                $user = $realm->find_user($userinfo, $c); 
     111                unless (ref $user) { 
     112                    $c->log->error("Unable to locate user matching user info provided"); 
     113                } 
     114            }, 
     115            error => sub { 
     116                my ($code, $text) = @_; 
     117                $c->log->error("Error validating identity: $code: $text"); 
     118            }, 
     119        ); 
     120 
     121        return $user if ref $user; 
    106122    } 
    107123} 
     
    122138    __PACKAGE__->config('Plugin::Authentication' => { 
    123139        default_realm => 'members', 
    124         realms => { 
    125             members => { 
    126                 credential => { 
    127                     class => 'OpenID', 
    128                 }, 
    129                 store => { 
    130                     # ... 
    131                 }, 
     140        members => { 
     141            credential => { 
     142                class => 'OpenID', 
     143            }, 
     144            store => { 
     145                # ... 
    132146            }, 
    133147        }, 
     
    157171you should use L<Catalyst::Plugin::Authentication::Credential::OpenID>. 
    158172 
     173=head1 METHODS 
     174 
     175=head2 new 
     176 
     177You will never call this. Catalyst does it for you. The only important 
     178thing you might like to know about it is that it merges its realm 
     179configuration with its configuration proper. If this doesn't mean 
     180anything to you, don't worry. 
     181 
     182=head2 authenticate 
     183 
     184Call to authenticate the user via OpenID. Returns false if 
     185authorization is unsuccessful. Sets the user into the session and 
     186returns the user object if authentication succeeds. 
     187 
     188You can see in the call above that the authentication hash is empty. 
     189The implicit OpenID parameter is, as the 2.0 specification says it 
     190SHOULD be, B<openid_identifier>. You can set it anything you like in 
     191your realm configuration, though, under the key C<openid_field>. If 
     192you call C<authenticate()> with the empty info hash and no configured 
     193C<openid_field> then only C<openid_identifier> is checked. 
     194 
     195It implicitly does this (sort of, it checks the request method too)- 
     196 
     197  my $claimed_uri = $c->req->params->{openid_identifier}; 
     198  $c->authenticate({ openid_identifier => $claimed_uri }); 
     199 
    159200=head1 CONFIGURATION 
    160201 
     202Catalyst authentication is now configured entirely from your 
     203application's configuration. Do not, for example, put 
     204C<Credential::OpenID> into your C<use Catalyst ...> statement. 
     205Instead, tell your application that in one of your authentication 
     206realms you will use the credential. 
     207 
     208  __PACKAGE__->config( 
     209      name => 'MyApp', 
     210      'Plugin::Authentication' => { 
     211          default_realm => 'openid', 
     212          openid => { 
     213              credential => { 
     214                  class => 'OpenID', 
     215                  consumer_secret => "Don't bother setting", 
     216                  ua_class => 'LWP::UserAgent', 
     217                  ua_args => { 
     218                      env_proxy  => 1, 
     219                      parse_head => 0, 
     220                  }, 
     221                  # OpenID Extension 
     222                  extension_args => { 
     223                      'http://openid.net/extensions/sreg/1.1' => { 
     224                          required => 'nickname,email', 
     225                      }, 
     226                  }, 
     227              }, 
     228              store => { 
     229                  class => 'Null', 
     230              }, 
     231          }, 
     232      }, 
     233  ); 
     234 
    161235=over 4 
    162236 
    163 =item class 
    164  
    165 The classname used for Credential. For this module to be used, 
    166 this must be set to "OpenID". 
    167  
    168 =item identity_field 
    169  
    170 The field name in the Store class that contains the identity. 
    171 Default value is "username". 
    172  
    173 If authentication succeeded, 
    174 this field in $authinfo is automatically set, 
    175 and this module call find_user() in Realm class. 
    176  
    177 =back 
    178  
    179 =head1 METHODS 
    180  
    181 =over 4 
    182  
    183 =item new 
    184  
    185 =item authenticate 
     237=item ua_args and ua_class 
     238 
     239L<LWPx::ParanoidAgent> is the default agent E<mdash> C<ua_class>. You don't 
     240have to set it. I recommend that you do B<not> override it. You can 
     241with any well behaved L<LWP::UserAgent>. You probably should not. 
     242L<LWPx::ParanoidAgent> buys you many defenses and extra security 
     243checks. When you allow your application users freedom to initiate 
     244external requests, you open a big avenue for DoS (denial of service) 
     245attacks. L<LWPx::ParanoidAgent> defends against this. 
     246L<LWP::UserAgent> and any regular subclass of it will not. 
     247 
     248=item consumer_secret 
     249 
     250The underlying L<Net::OpenID::Consumer> object is seeded with a 
     251secret. If it's important to you to set your own, you can. The default 
     252uses this package name + its version + the sorted configuration keys 
     253of your Catalyst application (chopped at 255 characters if it's 
     254longer). This should generally be superior to any fixed string. 
     255 
     256=item extension_args 
     257 
     258set OpenID extension (eg. SREG) parameters for request. 
     259 
     260in credential section of config: 
     261 
     262  credential => { 
     263      class => 'OpenID', 
     264      extension_args => { 
     265          'http://openid.net/extensions/sreg/1.1' => { 
     266              required => 'nickname,email', 
     267          }, 
     268          # $namespace => { 
     269          #    $key1 => $value1, 
     270          #    $key2 => $value2, 
     271          #    ... 
     272          # }, 
     273      }, 
     274  }, 
     275 
     276This means (OpenID Request): 
     277 
     278  # openid.ns.sreg       = http://openid.net/extensions/sreg/1.1 
     279  # openid.sreg.required = nickname,email 
     280 
     281and your controller: 
     282 
     283  if (my $user = $c->authenticate) { 
     284      my $sreg     = $user->extensions->{'http://openid.net/extensions/sreg/1.1'}; 
     285      my $nickname = $sreg->{nickname}; 
     286      my $email    = $sreg->{email}; 
     287  } 
     288 
     289This means (OpenID Response): 
     290 
     291  # openid.ns.sreg       = http://openid.net/extensions/sreg/1.1 
     292  # openid.sreg.nickname = foo 
     293  # openid.sreg.email    = foo@example.com 
    186294 
    187295=back 
     
    198306=head1 SEE ALSO 
    199307 
    200 L<Catalyst::Plugin::Authentication>, L<Catalyst::Plugin::Authentication::Credential::OpenID> 
     308L<Catalyst>, L<Catalyst::Plugin::Authentication>, 
     309L<Catalyst::Plugin::Authentication::Credential::OpenID>, 
     310L<Net::OpenID::Consumer>, L<Net::OpenID::VerifiedIdentity> 
    201311 
    202312=cut 
  • lang/perl/Catalyst-Authentication-Credential-OpenID/trunk/t/00_compile.t

    r7265 r24569  
    22use Test::More tests => 1; 
    33 
    4 BEGIN { use_ok 'Catalyst::Authentication::Credential::OpenID' } 
     4use ok 'Catalyst::Authentication::Credential::OpenID';