1 package Catalyst::Authentication::Credential::OpenID;
4 use base "Class::Accessor::Fast";
6 __PACKAGE__->mk_accessors(qw/ _config realm debug secret /);
10 use Net::OpenID::Consumer;
11 use Catalyst::Exception ();
14 my ( $class, $config, $c, $realm ) = @_;
15 my $self = { _config => { %{ $config },
21 # 2.0 spec says "SHOULD" be named "openid_identifier."
22 $self->_config->{openid_field} ||= "openid_identifier";
24 $self->debug( $self->_config->{debug} );
26 my $secret = $self->_config->{consumer_secret} ||= join("+",
29 sort keys %{ $c->config }
32 $secret = substr($secret,0,255) if length $secret > 255;
33 $self->secret($secret);
34 # If user has no preference we prefer L::PA b/c it can prevent DoS attacks.
35 $self->_config->{ua_class} ||= eval "use LWPx::ParanoidAgent" ?
36 "LWPx::ParanoidAgent" : "LWP::UserAgent";
38 my $agent_class = $self->_config->{ua_class};
39 eval "require $agent_class"
40 or Catalyst::Exception->throw("Could not 'require' user agent class " .
41 $self->_config->{ua_class});
43 $c->log->debug("Setting consumer secret: " . $secret) if $self->debug;
49 my ( $self, $c, $realm, $authinfo ) = @_;
51 $c->log->debug("authenticate() called from " . $c->request->uri) if $self->debug;
53 my $field = $self->{_config}->{openid_field};
55 my $claimed_uri = $authinfo->{ $field };
57 # Its security related so we want to be explicit about GET/POST param retrieval.
58 $claimed_uri ||= $c->req->method eq 'GET' ?
59 $c->req->query_params->{ $field } : $c->req->body_params->{ $field };
62 my $csr = Net::OpenID::Consumer->new(
63 ua => $self->_config->{ua_class}->new(%{$self->_config->{ua_args} || {}}),
64 args => $c->req->params,
65 consumer_secret => $self->secret,
68 if ( $self->_config->{extension_args} and $self->debug )
70 $c->log->info("The configuration key 'extension_args' is deprecated; use 'extensions'");
73 my @extensions = $self->_config->{extensions} ?
74 @{ $self->_config->{extensions} } : $self->_config->{extension_args} ?
75 @{ $self->_config->{extension_args} } : ();
79 my $current = $c->uri_for($c->req->uri->path); # clear query/fragment...
81 my $identity = $csr->claimed_identity($claimed_uri);
84 if ( $self->_config->{errors_are_fatal} )
86 Catalyst::Exception->throw($csr->err);
90 $c->log->error($csr->err . " -- $claimed_uri");
95 $identity->set_extension_args(@extensions)
98 my $check_url = $identity->check_url(
99 return_to => $current . '?openid-check=1',
100 trust_root => $current,
103 $c->res->redirect($check_url);
106 elsif ( $c->req->params->{'openid-check'} )
108 if ( my $setup_url = $csr->user_setup_url )
110 $c->res->redirect($setup_url);
113 elsif ( $csr->user_cancel )
117 elsif ( my $identity = $csr->verified_identity )
119 # This is where we ought to build an OpenID user and verify against the spec.
120 my $user = +{ map { $_ => scalar $identity->$_ }
121 qw( url display rss atom foaf declared_rss declared_atom declared_foaf foafmaker ) };
122 # Dude, I did not design the array as hash spec. Don't curse me [apv].
123 my %flat = @extensions;
124 for my $key ( keys %flat )
126 $user->{extensions}->{$key} = $identity->signed_extension_fields($key);
129 my $user_obj = $realm->find_user($user, $c);
137 $c->log->debug("Verified OpenID identity failed to load with find_user; bad user_class? Try 'Null.'") if $self->debug;
143 $self->_config->{errors_are_fatal} ?
144 Catalyst::Exception->throw("Error validating identity: " . $csr->err)
146 $c->log->error( $csr->err);
158 Catalyst::Authentication::Credential::OpenID - OpenID credential for Catalyst::Plugin::Authentication framework.
164 =head1 BACKWARDS COMPATIBILITY CHANGES
166 =head2 EXTENSION_ARGS v EXTENSIONS
168 B<NB>: The extensions were previously configured under the key C<extension_args>. They are now configured under C<extensions>. 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.
170 As previously noted, L</EXTENSIONS TO OPENID>, I have not tested the extensions. I would be grateful for any feedback or, better, tests.
174 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<errors_are_fatal> to a true value.
183 Session::Store::FastMmap
184 Session::State::Cookie
187 Somewhere in myapp.conf-
189 <Plugin::Authentication>
195 ua_class LWP::UserAgent
199 </Plugin::Authentication>
201 Or in your myapp.yml if you're using L<YAML> instead-
203 Plugin::Authentication:
204 default_realm: openid
209 ua_class: LWP::UserAgent
211 In a controller, perhaps C<Root::openid>-
216 if ( $c->authenticate() )
218 $c->flash(message => "You signed in with OpenID!");
219 $c->res->redirect( $c->uri_for('/') );
223 # Present OpenID form.
227 And a L<Template> to match in C<openid.tt>-
229 <form action="[% c.uri_for('/openid') %]" method="GET" name="openid">
230 <input type="text" name="openid_identifier" class="openid" />
231 <input type="submit" value="Sign in with OpenID" />
236 This is the B<third> OpenID related authentication piece for
237 L<Catalyst>. The first E<mdash> L<Catalyst::Plugin::Authentication::OpenID>
238 by Benjamin Trott E<mdash> was deprecated by the second E<mdash>
239 L<Catalyst::Plugin::Authentication::Credential::OpenID> by Tatsuhiko
240 Miyagawa E<mdash> and this is an attempt to deprecate both by conforming to
241 the newish, at the time of this module's inception, realm-based
242 authentication in L<Catalyst::Plugin::Authentication>.
244 1. Catalyst::Plugin::Authentication::OpenID
245 2. Catalyst::Plugin::Authentication::Credential::OpenID
246 3. Catalyst::Authentication::Credential::OpenID
248 The benefit of this version is that you can use an arbitrary number of
249 authentication systems in your L<Catalyst> application and configure
250 and call all of them in the same way.
252 Note that both earlier versions of OpenID authentication use the method
253 C<authenticate_openid()>. This module uses C<authenticate()> and
254 relies on you to specify the realm. You can specify the realm as the
255 default in the configuration or inline with each
256 C<authenticate()> call; more below.
258 This module functions quite differently internally from the others.
259 See L<Catalyst::Plugin::Authentication::Internals> for more about this
266 =item $c->authenticate({},"your_openid_realm");
268 Call to authenticate the user via OpenID. Returns false if
269 authorization is unsuccessful. Sets the user into the session and
270 returns the user object if authentication succeeds.
272 You can see in the call above that the authentication hash is empty.
273 The implicit OpenID parameter is, as the 2.0 specification says it
274 SHOULD be, B<openid_identifier>. You can set it anything you like in
275 your realm configuration, though, under the key C<openid_field>. If
276 you call C<authenticate()> with the empty info hash and no configured
277 C<openid_field> then only C<openid_identifier> is checked.
279 It implicitly does this (sort of, it checks the request method too)-
281 my $claimed_uri = $c->req->params->{openid_identifier};
282 $c->authenticate({openid_identifier => $claimed_uri});
284 =item Catalyst::Authentication::Credential::OpenID->new()
286 You will never call this. Catalyst does it for you. The only important
287 thing you might like to know about it is that it merges its realm
288 configuration with its configuration proper. If this doesn't mean
289 anything to you, don't worry.
295 Currently the only supported user class is L<Catalyst::Plugin::Authentication::User::Hash>.
301 =item $c->user->display
309 =item $c->user->declared_rss
311 =item $c->user->declared_atom
313 =item $c->user->declared_foaf
315 =item $c->user->foafmaker
319 See L<Net::OpenID::VerifiedIdentity> for details.
323 Catalyst authentication is now configured entirely from your
324 application's configuration. Do not, for example, put
325 C<Credential::OpenID> into your C<use Catalyst ...> statement.
326 Instead, tell your application that in one of your authentication
327 realms you will use the credential.
329 In your application the following will give you two different
330 authentication realms. One called "members" which authenticates with
331 clear text passwords and one called "openid" which uses... uh, OpenID.
335 "Plugin::Authentication" => {
336 default_realm => "members",
341 password_field => "password",
342 password_type => "clear"
348 password => "l4s4v3n7ur45",
359 consumer_secret => "Don't bother setting",
360 ua_class => "LWP::UserAgent",
361 # whitelist is only relevant for LWPx::ParanoidAgent
363 whitelisted_hosts => [qw/ 127.0.0.1 localhost /],
366 'http://openid.net/extensions/sreg/1.1',
369 optional => 'fullname,nickname,timezone',
378 This is the same configuration in the default L<Catalyst> configuration format from L<Config::General>.
381 <Plugin::Authentication>
382 default_realm members
389 password l4s4v3n7ur45
394 password_field password
406 whitelisted_hosts 127.0.0.1
407 whitelisted_hosts localhost
409 consumer_secret Don't bother setting
410 ua_class LWP::UserAgent
412 http://openid.net/extensions/sreg/1.1
414 optional fullname,nickname,timezone
419 </Plugin::Authentication>
421 And now, the same configuration in L<YAML>. B<NB>: L<YAML> is whitespace sensitive.
424 Plugin::Authentication:
425 default_realm: members
430 password_field: password
436 password: l4s4v3n7ur45
442 consumer_secret: Don't bother setting
443 ua_class: LWP::UserAgent
445 # whitelist is only relevant for LWPx::ParanoidAgent
450 - http://openid.net/extensions/sreg/1.1
452 optional: fullname,nickname,timezone
454 B<NB>: There is no OpenID store yet.
456 =head2 EXTENSIONS TO OPENID
458 The Simple Registration--L<http://openid.net/extensions/sreg/1.1>--(SREG) extension to OpenID is supported in the L<Net::OpenID> family now. Experimental support for it is included here as of v0.12. SREG is the only supported extension in OpenID 1.1. It's experimental in the sense it's a new interface and barely tested. Support for OpenID extensions is here to stay.
460 =head2 MORE ON CONFIGURATION
464 =item ua_args and ua_class
466 L<LWPx::ParanoidAgent> is the default agent E<mdash> C<ua_class> E<mdash> if it's available, L<LWP::UserAgent> if not. You don't have to set
467 it. I recommend that you do B<not> override it. You can with any well behaved L<LWP::UserAgent>. You probably should not.
468 L<LWPx::ParanoidAgent> buys you many defenses and extra security checks. When you allow your application users freedom to initiate external
469 requests, you open an avenue for DoS (denial of service) attacks. L<LWPx::ParanoidAgent> defends against this. L<LWP::UserAgent> and any
470 regular subclass of it will not.
472 =item consumer_secret
474 The underlying L<Net::OpenID::Consumer> object is seeded with a secret. If it's important to you to set your own, you can. The default uses
475 this package name + its version + the sorted configuration keys of your Catalyst application (chopped at 255 characters if it's longer).
476 This should generally be superior to any fixed string.
482 Option to suppress fatals.
484 Support more of the new methods in the L<Net::OpenID> kit.
486 There are some interesting implications with this sort of setup. Does a user aggregate realms or can a user be signed in under more than one
487 realm? The documents could contain a recipe of the self-answering OpenID end-point that is in the tests.
489 Debug statements need to be both expanded and limited via realm configuration.
491 Better diagnostics in errors. Debug info at all consumer calls.
493 Roles from provider domains? Mapped? Direct? A generic "openid" auto_role?
497 To Benjamin Trott (L<Catalyst::Plugin::Authentication::OpenID>), Tatsuhiko Miyagawa (L<Catalyst::Plugin::Authentication::Credential::OpenID>), Brad Fitzpatrick for the great OpenID stuff, Martin Atkins for picking up the code to handle OpenID 2.0, and Jay Kuri and everyone else who has made Catalyst such a wonderful framework.
499 Menno Blom provided a bug fix and the hook to use OpenID extensions.
501 =head1 LICENSE AND COPYRIGHT
503 Copyright (c) 2008-2009, Ashley Pond V C<< <ashley@cpan.org> >>. Some of Tatsuhiko Miyagawa's work is reused here.
505 This module is free software; you can redistribute it and modify it under the same terms as Perl itself. See L<perlartistic>.
507 =head1 DISCLAIMER OF WARRANTY
509 Because this software is licensed free of charge, there is no warranty for the software, to the extent permitted by applicable law. Except
510 when otherwise stated in writing the copyright holders and other parties provide the software "as is" without warranty of any kind, either
511 expressed or implied, including, but not limited to, the implied warranties of merchantability and fitness for a particular purpose. The
512 entire risk as to the quality and performance of the software is with you. Should the software prove defective, you assume the cost of all
513 necessary servicing, repair, or correction.
515 In no event unless required by applicable law or agreed to in writing will any copyright holder, or any other party who may modify or
516 redistribute the software as permitted by the above license, be liable to you for damages, including any general, special, incidental, or
517 consequential damages arising out of the use or inability to use the software (including but not limited to loss of data or data being
518 rendered inaccurate or losses sustained by you or third parties or a failure of the software to operate with any other software), even if
519 such holder or other party has been advised of the possibility of such damages.
527 L<Net::OpenID::Server>, L<Net::OpenID::VerifiedIdentity>, L<Net::OpenID::Consumer>, L<http://openid.net/>,
528 L<http://openid.net/developers/specs/>, and L<http://openid.net/extensions/sreg/1.1>.
530 =item Catalyst Authentication
532 L<Catalyst>, L<Catalyst::Plugin::Authentication>, L<Catalyst::Manual::Tutorial::Authorization>, and
533 L<Catalyst::Manual::Tutorial::Authentication>.
535 =item Catalyst Configuration
537 L<Catalyst::Plugin::ConfigLoader>, L<Config::General>, and L<YAML>.
541 L<Catalyst::Manual::Tutorial>, L<Template>, L<LWPx::ParanoidAgent>.