1 package Catalyst::Authentication::Credential::OpenID;
3 # use warnings; no warnings "uninitialized"; # for testing, not production
4 use parent "Class::Accessor::Fast";
7 __PACKAGE__->mk_accessors(qw/ _config realm debug secret /);
10 our $VERSION = "0.14_03";
12 use Net::OpenID::Consumer;
13 use Catalyst::Exception ();
16 my ( $class, $config, $c, $realm ) = @_;
17 my $self = { _config => { %{ $config },
23 # 2.0 spec says "SHOULD" be named "openid_identifier."
24 $self->_config->{openid_field} ||= "openid_identifier";
26 $self->debug( $self->_config->{debug} );
28 my $secret = $self->_config->{consumer_secret} ||= join("+",
31 sort keys %{ $c->config }
34 $secret = substr($secret,0,255) if length $secret > 255;
35 $self->secret($secret);
36 # If user has no preference we prefer L::PA b/c it can prevent DoS attacks.
37 $self->_config->{ua_class} ||= eval "use LWPx::ParanoidAgent" ?
38 "LWPx::ParanoidAgent" : "LWP::UserAgent";
40 my $agent_class = $self->_config->{ua_class};
41 eval "require $agent_class"
42 or Catalyst::Exception->throw("Could not 'require' user agent class " .
43 $self->_config->{ua_class});
45 $c->log->debug("Setting consumer secret: " . $secret) if $self->debug;
50 sub authenticate : method {
51 my ( $self, $c, $realm, $authinfo ) = @_;
53 $c->log->debug("authenticate() called from " . $c->request->uri) if $self->debug;
55 my $field = $self->{_config}->{openid_field};
57 my $claimed_uri = $authinfo->{ $field };
59 # Its security related so we want to be explicit about GET/POST param retrieval.
60 $claimed_uri ||= $c->req->method eq 'GET' ?
61 $c->req->query_params->{ $field } : $c->req->body_params->{ $field };
63 my $csr = Net::OpenID::Consumer->new(
64 ua => $self->_config->{ua_class}->new(%{$self->_config->{ua_args} || {}}),
65 args => $c->req->params,
66 consumer_secret => $self->secret,
69 my @extensions = $self->_config->{extensions} ?
70 @{ $self->_config->{extensions} } : $self->_config->{extension_args} ?
71 @{ $self->_config->{extension_args} } : ();
75 my $current = $c->uri_for($c->req->uri->path); # clear query/fragment...
77 my $identity = $csr->claimed_identity($claimed_uri);
80 Catalyst::Exception->throw($csr->err);
83 $identity->set_extension_args(@extensions)
86 my $check_url = $identity->check_url(
87 return_to => $current . '?openid-check=1',
88 trust_root => $current,
91 $c->res->redirect($check_url);
94 elsif ( $c->req->params->{'openid-check'} )
96 if ( my $setup_url = $csr->user_setup_url )
98 $c->res->redirect($setup_url);
101 elsif ( $csr->user_cancel )
105 elsif ( my $identity = $csr->verified_identity )
107 # This is where we ought to build an OpenID user and verify against the spec.
108 my $user = +{ map { $_ => scalar $identity->$_ }
109 qw( url display rss atom foaf declared_rss declared_atom declared_foaf foafmaker ) };
110 # Dude, I did not design the array as hash spec. Don't curse me [apv].
111 my %flat = @extensions;
112 for my $key ( keys %flat )
114 $user->{extensions}->{$key} = $identity->signed_extension_fields($key);
117 my $user_obj = $realm->find_user($user, $c);
125 $c->log->debug("Verified OpenID identity failed to load with find_user; bad user_class? Try 'Null.'") if $c->debug;
131 Catalyst::Exception->throw("Error validating identity: " .
144 Catalyst::Authentication::Credential::OpenID - OpenID credential for Catalyst::Plugin::Authentication framework.
150 =head1 BACKWARDS COMPATIBILITY CHANGE
152 B<NB>: The extenstions 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.
154 As previously noted, L</EXTENSIONS TO OPENID>, I have not tested the extensions. I would be grateful for any feedback or, better, tests.
163 Session::Store::FastMmap
164 Session::State::Cookie
167 Somewhere in myapp.conf-
169 <Plugin::Authentication>
176 ua_class LWP::UserAgent
179 </Plugin::Authentication>
181 Or in your myapp.yml if you're using L<YAML> instead-
183 Plugin::Authentication:
184 default_realm: openid
189 ua_class: LWP::UserAgent
191 In a controller, perhaps C<Root::openid>-
196 if ( $c->authenticate() )
198 $c->flash(message => "You signed in with OpenID!");
199 $c->res->redirect( $c->uri_for('/') );
203 # Present OpenID form.
207 And a L<Template> to match in C<openid.tt>-
209 <form action="[% c.uri_for('/openid') %]" method="GET" name="openid">
210 <input type="text" name="openid_identifier" class="openid" />
211 <input type="submit" value="Sign in with OpenID" />
216 This is the B<third> OpenID related authentication piece for
217 L<Catalyst>. The first E<mdash> L<Catalyst::Plugin::Authentication::OpenID>
218 by Benjamin Trott E<mdash> was deprecated by the second E<mdash>
219 L<Catalyst::Plugin::Authentication::Credential::OpenID> by Tatsuhiko
220 Miyagawa E<mdash> and this is an attempt to deprecate both by conforming to
221 the newish, at the time of this module's inception, realm-based
222 authentication in L<Catalyst::Plugin::Authentication>.
224 1. Catalyst::Plugin::Authentication::OpenID
225 2. Catalyst::Plugin::Authentication::Credential::OpenID
226 3. Catalyst::Authentication::Credential::OpenID
228 The benefit of this version is that you can use an arbitrary number of
229 authentication systems in your L<Catalyst> application and configure
230 and call all of them in the same way.
232 Note that both earlier versions of OpenID authentication use the method
233 C<authenticate_openid()>. This module uses C<authenticate()> and
234 relies on you to specify the realm. You can specify the realm as the
235 default in the configuration or inline with each
236 C<authenticate()> call; more below.
238 This module functions quite differently internally from the others.
239 See L<Catalyst::Plugin::Authentication::Internals> for more about this
246 =item $c->authenticate({},"your_openid_realm");
248 Call to authenticate the user via OpenID. Returns false if
249 authorization is unsuccessful. Sets the user into the session and
250 returns the user object if authentication succeeds.
252 You can see in the call above that the authentication hash is empty.
253 The implicit OpenID parameter is, as the 2.0 specification says it
254 SHOULD be, B<openid_identifier>. You can set it anything you like in
255 your realm configuration, though, under the key C<openid_field>. If
256 you call C<authenticate()> with the empty info hash and no configured
257 C<openid_field> then only C<openid_identifier> is checked.
259 It implicitly does this (sort of, it checks the request method too)-
261 my $claimed_uri = $c->req->params->{openid_identifier};
262 $c->authenticate({openid_identifier => $claimed_uri});
264 =item Catalyst::Authentication::Credential::OpenID->new()
266 You will never call this. Catalyst does it for you. The only important
267 thing you might like to know about it is that it merges its realm
268 configuration with its configuration proper. If this doesn't mean
269 anything to you, don't worry.
275 Currently the only supported user class is L<Catalyst::Plugin::Authentication::User::Hash>.
281 =item $c->user->display
289 =item $c->user->declared_rss
291 =item $c->user->declared_atom
293 =item $c->user->declared_foaf
295 =item $c->user->foafmaker
299 See L<Net::OpenID::VerifiedIdentity> for details.
303 Catalyst authentication is now configured entirely from your
304 application's configuration. Do not, for example, put
305 C<Credential::OpenID> into your C<use Catalyst ...> statement.
306 Instead, tell your application that in one of your authentication
307 realms you will use the credential.
309 In your application the following will give you two different
310 authentication realms. One called "members" which authenticates with
311 clear text passwords and one called "openid" which uses... uh, OpenID.
315 "Plugin::Authentication" => {
316 default_realm => "members",
321 password_field => "password",
322 password_type => "clear"
328 password => "l4s4v3n7ur45",
334 consumer_secret => "Don't bother setting",
335 ua_class => "LWP::UserAgent",
337 whitelisted_hosts => [qw/ 127.0.0.1 localhost /],
346 'http://openid.net/extensions/sreg/1.1',
349 optional => 'fullname,nickname,timezone',
357 This is the same configuration in the default L<Catalyst> configuration format from L<Config::General>.
360 <Plugin::Authentication>
361 default_realm members
368 password l4s4v3n7ur45
373 password_field password
380 whitelisted_hosts 127.0.0.1
381 whitelisted_hosts localhost
383 consumer_secret Don't bother setting
384 ua_class LWP::UserAgent
392 http://openid.net/extensions/sreg/1.1
394 optional fullname,nickname,timezone
398 </Plugin::Authentication>
400 And now, the same configuration in L<YAML>. B<NB>: L<YAML> is whitespace sensitive.
403 Plugin::Authentication:
404 default_realm: members
409 password_field: password
415 password: l4s4v3n7ur45
421 consumer_secret: Don't bother setting
422 ua_class: LWP::UserAgent
428 - http://openid.net/extensions/sreg/1.1
430 optional: fullname,nickname,timezone
432 B<NB>: There is no OpenID store yet.
434 =head2 EXTENSIONS TO OPENID
436 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.
438 =head2 MORE ON CONFIGURATION
440 These are set in your realm. See above.
444 =item ua_args and ua_class
446 L<LWPx::ParanoidAgent> is the default agent E<mdash> C<ua_class>
447 E<mdash> if it's available, L<LWP::UserAgent> if not. You don't have
448 to set it. I recommend that you do B<not> override it. You can with
449 any well behaved L<LWP::UserAgent>. You probably should not.
450 L<LWPx::ParanoidAgent> buys you many defenses and extra security
451 checks. When you allow your application users freedom to initiate
452 external requests, you open an avenue for DoS (denial of service)
453 attacks. L<LWPx::ParanoidAgent> defends against this.
454 L<LWP::UserAgent> and any regular subclass of it will not.
456 =item consumer_secret
458 The underlying L<Net::OpenID::Consumer> object is seeded with a
459 secret. If it's important to you to set your own, you can. The default
460 uses this package name + its version + the sorted configuration keys
461 of your Catalyst application (chopped at 255 characters if it's
462 longer). This should generally be superior to any fixed string.
468 Option to suppress fatals.
470 Support more of the new methods in the L<Net::OpenID> kit.
472 There are some interesting implications with this sort of setup. Does
473 a user aggregate realms or can a user be signed in under more than one
474 realm? The documents could contain a recipe of the self-answering
475 OpenID end-point that is in the tests.
477 Debug statements need to be both expanded and limited via realm
480 Better diagnostics in errors. Debug info at all consumer calls.
482 Roles from provider domains? Mapped? Direct? A generic "openid" auto_role?
486 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.
488 Menno Blom provided a bug fix and the hook to use OpenID extensions.
490 =head1 LICENSE AND COPYRIGHT
492 Copyright (c) 2008-2009, Ashley Pond V C<< <ashley@cpan.org> >>. Some of Tatsuhiko Miyagawa's work is reused here.
494 This module is free software; you can redistribute it and modify it under the same terms as Perl itself. See L<perlartistic>.
496 =head1 DISCLAIMER OF WARRANTY
498 Because this software is licensed free of charge, there is no warranty
499 for the software, to the extent permitted by applicable law. Except when
500 otherwise stated in writing the copyright holders and other parties
501 provide the software "as is" without warranty of any kind, either
502 expressed or implied, including, but not limited to, the implied
503 warranties of merchantability and fitness for a particular purpose. The
504 entire risk as to the quality and performance of the software is with
505 you. Should the software prove defective, you assume the cost of all
506 necessary servicing, repair, or correction.
508 In no event unless required by applicable law or agreed to in writing
509 will any copyright holder, or any other party who may modify or
510 redistribute the software as permitted by the above license, be
511 liable to you for damages, including any general, special, incidental,
512 or consequential damages arising out of the use or inability to use
513 the software (including but not limited to loss of data or data being
514 rendered inaccurate or losses sustained by you or third parties or a
515 failure of the software to operate with any other software), even if
516 such holder or other party has been advised of the possibility of
525 L<Net::OpenID::Server>, L<Net::OpenID::VerifiedIdentity>, L<Net::OpenID::Consumer>, L<http://openid.net/>, L<http://openid.net/developers/specs/>, and L<http://openid.net/extensions/sreg/1.1>.
527 =item Catalyst Authentication
529 L<Catalyst>, L<Catalyst::Plugin::Authentication>, L<Catalyst::Manual::Tutorial::Authorization>, and L<Catalyst::Manual::Tutorial::Authentication>.
531 =item Catalyst Configuration
533 L<Catalyst::Plugin::ConfigLoader>, L<Config::General>, and L<YAML>.
537 L<Catalyst::Manual::Tutorial>, L<Template>, L<LWPx::ParanoidAgent>.