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

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

import Credential::AOL

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 =head1 NAME
80
81 Catalyst::Plugin::Authentication::Credential::AOL -
82
83 =head1 SYNOPSIS
84
85   use Catalyst::Plugin::Authentication::Credential::AOL;
86
87 =head1 DESCRIPTION
88
89 Catalyst::Plugin::Authentication::Credential::AOL is
90
91 =head1 AUTHOR
92
93 Tatsuhiko Miyagawa E<lt>miyagawa@bulknews.netE<gt>
94
95 =head1 LICENSE
96
97 This library is free software; you can redistribute it and/or modify
98 it under the same terms as Perl itself.
99
100 =head1 SEE ALSO
101
102 L<Catalyst::Plugin::Authentication>, L<http://dev.aol.com/openauth>
103
104 =cut
Note: See TracBrowser for help on using the browser.