root/Catalyst-Plugin-Authentication-Credential-AOL/trunk/lib/Catalyst/Plugin/Authentication/Credential/AOL.pm

Revision 2210 (checked in by miyagawa, 13 years ago)

Checking in changes prior to tagging of version 0.01. Changelog diff is:

=== Changes
==================================================================
--- Changes (revision 6511)
+++ Changes (local)
@@ -1,4 +1,4 @@

Revision history for Perl extension Catalyst::Plugin::Authentication::Credential
AOL

- 0.01 Tue Apr 17 16:58:30 2007
+0.01 Tue Apr 17 16:58:30 2007

- original version

Line 
1 package Catalyst::Plugin::Authentication::Credential::AOL;
2
3 use strict;
4 our $VERSION = '0.01';
5
6 use UNIVERSAL::require;
7 use JSON::Any;
8
9 sub setup {
10     my $c = shift;
11     my $config = $c->config->{authentication}->{aol} ||= {};
12     ( $config->{user_class}
13         ||=  "Catalyst::Plugin::Authentication::User::Hash" )->require;
14     $c->NEXT::setup(@_);
15 }
16
17 sub authenticate_aol {
18     my $c = shift;
19
20     my $config = $c->config->{authentication}->{aol};
21
22     my $current = $c->req->uri;
23     $current->query(undef); # no query
24
25     if ($c->req->params->{token_a}) {
26         my $uri = URI->new("http://api.screenname.aol.com/auth/getInfo");
27         $uri->query_form(
28             devId => $config->{devId},
29             f     => 'json',
30             # referer needs to be an actual Referer: header, but since
31             # we do redirection, it doesn't match with succUrl :/
32             referer => $current->as_string,
33             a     => $c->req->params->{token_a},
34         );
35
36         my $ua = LWP::UserAgent->new(agent => "Catalyst::Plugin::Authentication::Credential::AOL/$VERSION");
37         my $res = $ua->get($uri);
38         unless ($res->is_success) {
39             $c->log->info("Authentication failure: HTTP error " . $res->status_line);
40             return;
41         }
42
43         my $data = JSON::Any->jsonToObj($res->content);
44         unless (($data->{response}->{statusCode} || '') eq '200') {
45             $c->log->info("Authentication failure: " . $data->{response}->{statusCode});
46             return;
47         }
48
49         my $user = $data->{response}->{data}->{userData};
50         $c->log->debug("Successfully authenticated user '$user->{loginId}'.")
51             if $c->debug;
52
53         my $store = $config->{store} || $c->default_auth_store;
54         if ( $store
55              and my $store_user
56              = $store->get_user( $user->{loginId}, $user ) ) {
57             $c->set_authenticated($store_user);
58         } else {
59             $user = $config->{user_class}->new($user);
60             $c->set_authenticated($user);
61         }
62
63         return 1;
64     } else {
65         my $uri = URI->new("http://api.screenname.aol.com/auth/login");
66         $uri->query_form(
67             devId => $config->{devId},
68             succUrl => $current,
69         );
70
71         $c->res->redirect($uri);
72         return;
73     }
74 }
75
76 1;
77 __END__
78
79 =for stopwords OpenID OpenAuth
80
81 =head1 NAME
82
83 Catalyst::Plugin::Authentication::Credential::AOL - AOL OpenAuth credential
84
85 =head1 SYNOPSIS
86
87   use Catalyst qw/
88       Authentication
89       Authentication::Credential::AOL
90       Session
91       Session::Store::FastMmap
92       Session::State::Cookie
93   /;
94
95   MyApp->config(
96       authentication => {
97           use_session => 1, # default 1. see C::P::Authentication
98           aol => {
99               devId => 'AOL_DEVELOPER_TOKEN',
100           },
101       },
102   );
103
104   sub login_aol : local {
105       my($self, $c) = @_;
106
107       if ($c->authenticate_aol) {
108           # login succeed
109           $c->res->redirect("/");
110       }
111
112       # login failed
113   }
114
115   # in your templates
116   <a href="[% c.uri_for('/login_aol') | html %]">Sign in via AOL</a>
117
118 =head1 DESCRIPTION
119
120 Catalyst::Plugin::Authentication::Credential::AOL is a Catalyst
121 Authentication credential plugin for AOL OpenAuth. Since AOL does
122 OpenID you can just use OpenID credential, but OpenAuth gives more
123 granular control over authentication.
124
125 =head1 AUTHOR
126
127 Tatsuhiko Miyagawa E<lt>miyagawa@bulknews.netE<gt>
128
129 =head1 LICENSE
130
131 This library is free software; you can redistribute it and/or modify
132 it under the same terms as Perl itself.
133
134 =head1 SEE ALSO
135
136 L<Catalyst::Plugin::Authentication>,
137 L<Catalyst::Plugin::Authentication::Credential::OpenID>,
138 L<http://dev.aol.com/openauth>
139
140 =cut
Note: See TracBrowser for help on using the browser.