| 1 | package Authen::GoogleAccount; | ||
| 2 | |||
| 3 | 2 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 2 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | =head1 NAME | ||
| 7 | |||
| 8 | Authen::GoogleAccount - Simple Authentication with Google Account | ||
| 9 | |||
| 10 | =head1 VERSION | ||
| 11 | |||
| 12 | Version 0.02 | ||
| 13 | |||
| 14 | =cut | ||
| 15 | |||
| 16 | our $VERSION = '0.02'; | ||
| 17 | |||
| 18 | =head1 SYNOPSIS | ||
| 19 | |||
| 20 | # step 1 | ||
| 21 | # redirect to goole to get token | ||
| 22 | use CGI; | ||
| 23 | use Authen::GoogleAccount; | ||
| 24 | my $q = CGI->new; | ||
| 25 | my $ga = Authen::GoogleAccount->new; | ||
| 26 | |||
| 27 | # set callback url to verify token | ||
| 28 | my $next = "http://www.example.com/googleauth.cgi"; | ||
| 29 | my $uri_to_login = $ga->uri_to_login($next); | ||
| 30 | |||
| 31 | print $q->redirect($uri_to_login); | ||
| 32 | |||
| 33 | |||
| 34 | |||
| 35 | # step 2 | ||
| 36 | # user will be redirected to http://www.example.com/googleauth.cgi?token=(token) | ||
| 37 | # get token with CGI.pm and give it to verify() | ||
| 38 | use CGI; | ||
| 39 | use Authen::GoogleAccount; | ||
| 40 | |||
| 41 | my $google_base_data_api_key = "fwioe2fqwoajieqawerq123ae..."; | ||
| 42 | |||
| 43 | my $q = CGI->new; | ||
| 44 | my $ga = Authen::GoogleAccount->new( | ||
| 45 | key => $google_base_data_api_key, | ||
| 46 | ); | ||
| 47 | |||
| 48 | my $token = $q->param('token'); | ||
| 49 | |||
| 50 | $ga->verify($token) or die $ga->errstr; | ||
| 51 | print "login succeeded\n"; | ||
| 52 | print $ga->name, " ", $ga->email, "\n"; | ||
| 53 | #"email" may be unique. | ||
| 54 | |||
| 55 | |||
| 56 | |||
| 57 | =head1 FUNCTIONS | ||
| 58 | |||
| 59 | =head2 new(key => $google_base_data_api_key) | ||
| 60 | |||
| 61 | Creates a new object. Requires Google Base data API Key. L<http://code.google.com/apis/base/signup.html> | ||
| 62 | |||
| 63 | =head2 uri_to_login($next) | ||
| 64 | |||
| 65 | Creates a URI to login Google Account. | ||
| 66 | |||
| 67 | User will be redirected to $next with token after a successful login. | ||
| 68 | |||
| 69 | =head2 verify($token) | ||
| 70 | |||
| 71 | Verifies given token and returns true when the token is successfully verified. | ||
| 72 | |||
| 73 | =head2 name | ||
| 74 | |||
| 75 | Returns user name. | ||
| 76 | |||
| 77 | =head2 email | ||
| 78 | |||
| 79 | Returns user email("anon-~~~~@base.google.com"). It may be unique. | ||
| 80 | |||
| 81 | =head2 errstr | ||
| 82 | |||
| 83 | Returns error message. | ||
| 84 | |||
| 85 | =head2 delete_item | ||
| 86 | |||
| 87 | =head2 get_item | ||
| 88 | |||
| 89 | =head2 post_item | ||
| 90 | |||
| 91 | =head2 upgrade_to_session_token | ||
| 92 | |||
| 93 | =head2 revoke_session_token | ||
| 94 | |||
| 95 | =head2 init | ||
| 96 | |||
| 97 | =head1 AUTHOR | ||
| 98 | |||
| 99 | Hogeist, C<< <mahito at cpan.org> >>, L<http://www.ornithopter.jp/> | ||
| 100 | |||
| 101 | =head1 BUGS | ||
| 102 | |||
| 103 | Please report any bugs or feature requests to | ||
| 104 | C<bug-authen-googleaccount at rt.cpan.org>, or through the web interface at | ||
| 105 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Authen-GoogleAccount>. | ||
| 106 | I will be notified, and then you'll automatically be notified of progress on | ||
| 107 | your bug as I make changes. | ||
| 108 | |||
| 109 | =head1 SUPPORT | ||
| 110 | |||
| 111 | You can find documentation for this module with the perldoc command. | ||
| 112 | |||
| 113 | perldoc Authen::GoogleAccount | ||
| 114 | |||
| 115 | You can also look for information at: | ||
| 116 | |||
| 117 | =over 4 | ||
| 118 | |||
| 119 | =item * AnnoCPAN: Annotated CPAN documentation | ||
| 120 | |||
| 121 | L<http://annocpan.org/dist/Authen-GoogleAccount> | ||
| 122 | |||
| 123 | =item * CPAN Ratings | ||
| 124 | |||
| 125 | L<http://cpanratings.perl.org/d/Authen-GoogleAccount> | ||
| 126 | |||
| 127 | =item * RT: CPAN's request tracker | ||
| 128 | |||
| 129 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Authen-GoogleAccount> | ||
| 130 | |||
| 131 | =item * Search CPAN | ||
| 132 | |||
| 133 | L<http://search.cpan.org/dist/Authen-GoogleAccount> | ||
| 134 | |||
| 135 | =back | ||
| 136 | |||
| 137 | |||
| 138 | =head1 COPYRIGHT & LICENSE | ||
| 139 | |||
| 140 | Copyright 2007 Hogeist, all rights reserved. | ||
| 141 | |||
| 142 | This program is free software; you can redistribute it and/or modify it | ||
| 143 | under the same terms as Perl itself. | ||
| 144 | |||
| 145 | =cut | ||
| 146 | |||
| 147 | |||
| 148 | |||
| 149 | |||
| 150 | |||
| 151 | |||
| 152 | |||
| 153 | |||
| 154 | |||
| 155 | |||
| 156 | |||
| 157 | |||
| 158 | |||
| 159 | |||
| 160 | |||
| 161 | |||
| 162 | 2 | ▶ | use URI::Escape; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 163 | 2 | ▶ | use LWP::UserAgent; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 164 | 2 | ▶ | use HTTP::Request; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 165 | 2 | ▶ | use Data::Dumper; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 166 | #use Smart::Comments; | ||
| 167 | 2 | ▶ | use base qw(Class::Accessor::Fast); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 168 | |||
| 169 | __PACKAGE__->mk_accessors( qw/session_token base_key enable_session errstr name email/ ); | ||
| 170 | |||
| 171 | |||
| 172 | my $insert_xml = << "END_OF_INSERT_XML"; | ||
| 173 | <?xml version='1.0'?> | ||
| 174 | <entry xmlns='http://www.w3.org/2005/Atom' | ||
| 175 | xmlns:g='http://base.google.com/ns/1.0'> | ||
| 176 | <category scheme='http://base.google.com/categories/itemtypes' term='Jobs'/> | ||
| 177 | <title type='text'>Authen::GoogleAccount Temporary Data</title> | ||
| 178 | <content type='xhtml'>It will be deleted automatically.</content> | ||
| 179 | <link rel='alternate' type='text/html' href='http://search.cpan.org/dist/Authen-GoogleAccount'/> | ||
| 180 | <g:item_type type='text'>Jobs</g:item_type> | ||
| 181 | <g:hoge type='text'>hoge hoge</g:hoge> | ||
| 182 | </entry> | ||
| 183 | END_OF_INSERT_XML | ||
| 184 | |||
| 185 | |||
| 186 | sub new { | ||
| 187 | my $class = shift; | ||
| 188 | my $self = {}; | ||
| 189 | bless $self, $class; | ||
| 190 | $self->init(@_); | ||
| 191 | return $self; | ||
| 192 | } | ||
| 193 | |||
| 194 | sub init { | ||
| 195 | my $self = shift; | ||
| 196 | my %fields = @_; | ||
| 197 | |||
| 198 | $self->base_key($fields{key}); | ||
| 199 | $self->enable_session(1); | ||
| 200 | } | ||
| 201 | |||
| 202 | sub uri_to_login { | ||
| 203 | my $self = shift; | ||
| 204 | my ($next) = @_; | ||
| 205 | my $scope = 'http://www.google.com/base/'; | ||
| 206 | |||
| 207 | return 'https://www.google.com/accounts/AuthSubRequest' | ||
| 208 | . '?scope=' . uri_escape($scope) | ||
| 209 | . '&session=' . 1 | ||
| 210 | . '&next=' . uri_escape($next); | ||
| 211 | } | ||
| 212 | |||
| 213 | sub verify { | ||
| 214 | my $self = shift; | ||
| 215 | my $token = shift; | ||
| 216 | my $debug = shift; | ||
| 217 | |||
| 218 | |||
| 219 | |||
| 220 | if($self->enable_session){ | ||
| 221 | if(!$debug){ | ||
| 222 | $self->upgrade_to_session_token($token) or return 0; | ||
| 223 | } | ||
| 224 | else{ | ||
| 225 | $self->session_token($token); | ||
| 226 | } | ||
| 227 | |||
| 228 | my $item = $self->post_item() or return 0; | ||
| 229 | $self->get_item($item) or return 0; | ||
| 230 | |||
| 231 | $self->revoke_session_token() if(!$debug); | ||
| 232 | |||
| 233 | return 1; | ||
| 234 | } | ||
| 235 | else{ | ||
| 236 | #depreciated... | ||
| 237 | my $ua = LWP::UserAgent->new(); | ||
| 238 | my $res = $ua->get( | ||
| 239 | 'https://www.google.com/accounts/AuthSubTokenInfo', | ||
| 240 | 'Authorization' => 'AuthSub token="' . $token . '"', | ||
| 241 | ); | ||
| 242 | if ($res->is_success){ | ||
| 243 | return 1; | ||
| 244 | } | ||
| 245 | else{ | ||
| 246 | $self->errstr( $res->message ); | ||
| 247 | return 0; | ||
| 248 | } | ||
| 249 | } | ||
| 250 | } | ||
| 251 | |||
| 252 | |||
| 253 | sub upgrade_to_session_token { | ||
| 254 | my $self = shift; | ||
| 255 | my $token = shift; | ||
| 256 | |||
| 257 | my $ua = LWP::UserAgent->new(); | ||
| 258 | my $res = $ua->get( | ||
| 259 | 'https://www.google.com/accounts/AuthSubSessionToken', | ||
| 260 | 'Content-Type' => 'application/x-www-form-urlencoded', | ||
| 261 | 'Authorization' => 'AuthSub token="' . $token . '"', | ||
| 262 | ); | ||
| 263 | if ($res->is_success and $res->content =~ /^Token=(.+)$/){ | ||
| 264 | $self->session_token($1); | ||
| 265 | return 1; | ||
| 266 | } | ||
| 267 | else{ | ||
| 268 | $self->errstr("failure of getting session token.($token)"); | ||
| 269 | return 0; | ||
| 270 | } | ||
| 271 | } | ||
| 272 | |||
| 273 | sub revoke_session_token { | ||
| 274 | my $self = shift; | ||
| 275 | |||
| 276 | my $ua = LWP::UserAgent->new(); | ||
| 277 | my $res = $ua->get( | ||
| 278 | 'https://www.google.com/accounts/AuthSubRevokeToken', | ||
| 279 | 'Content-Type' => 'application/x-www-form-urlencoded', | ||
| 280 | 'Authorization' => 'AuthSub token="' . $self->session_token . '"', | ||
| 281 | ); | ||
| 282 | if ($res->is_success){ | ||
| 283 | return 1; | ||
| 284 | } | ||
| 285 | else{ | ||
| 286 | return 0; | ||
| 287 | } | ||
| 288 | } | ||
| 289 | |||
| 290 | sub post_item { | ||
| 291 | my $self = shift; | ||
| 292 | |||
| 293 | my $ua = LWP::UserAgent->new(); | ||
| 294 | my $req = HTTP::Request->new( | ||
| 295 | 'POST', | ||
| 296 | 'http://www.google.com/base/feeds/items/', | ||
| 297 | ); | ||
| 298 | $req->header('Authorization' => 'AuthSub token="' . $self->session_token . '"'); | ||
| 299 | $req->header('X-Google-Key' => "key=" . $self->base_key); | ||
| 300 | $req->header('Content-Type' => "application/atom+xml"); | ||
| 301 | $req->content($insert_xml); | ||
| 302 | |||
| 303 | |||
| 304 | |||
| 305 | my $res = $ua->request($req); | ||
| 306 | if ($res->is_success){ | ||
| 307 | $res->content =~ m{<id>http://www.google.com/base/feeds/items/(\d+)</id>}; | ||
| 308 | return $1; | ||
| 309 | } | ||
| 310 | else{ | ||
| 311 | $self->errstr( $res->message ); | ||
| 312 | return 0; | ||
| 313 | } | ||
| 314 | } | ||
| 315 | |||
| 316 | sub get_item { | ||
| 317 | my $self = shift; | ||
| 318 | my $item = shift; | ||
| 319 | |||
| 320 | my $ua = LWP::UserAgent->new(); | ||
| 321 | my $res = $ua->get( | ||
| 322 | 'http://www.google.com/base/feeds/items/' . $item, | ||
| 323 | 'Authorization' => 'AuthSub token="' . $self->session_token . '"', | ||
| 324 | 'X-Google-Key' => "key=" . $self->base_key, | ||
| 325 | 'Content-Type' => "application/atom+xml", | ||
| 326 | ); | ||
| 327 | if ($res->is_success){ | ||
| 328 | $res->content =~ m{<author><name>(.+?)</name><email>(.+?)</email></author>}; | ||
| 329 | $self->name($1); | ||
| 330 | $self->email($2); | ||
| 331 | return 1; | ||
| 332 | } | ||
| 333 | else{ | ||
| 334 | $self->errstr( $res->message ); | ||
| 335 | return 0; | ||
| 336 | } | ||
| 337 | |||
| 338 | } | ||
| 339 | |||
| 340 | |||
| 341 | sub delete_item { | ||
| 342 | my $self = shift; | ||
| 343 | my $item = shift; | ||
| 344 | |||
| 345 | my $ua = LWP::UserAgent->new(); | ||
| 346 | my $req = HTTP::Request->new( | ||
| 347 | 'DELETE', | ||
| 348 | 'http://www.google.com/base/feeds/items/' . $item, | ||
| 349 | ); | ||
| 350 | $req->header('Authorization' => 'AuthSub token="' . $self->session_token . '"'); | ||
| 351 | $req->header('X-Google-Key' => "key=" . $self->base_key); | ||
| 352 | $req->header('Content-Type' => "application/atom+xml"); | ||
| 353 | |||
| 354 | |||
| 355 | my $res = $ua->request($req); | ||
| 356 | if ($res->is_success){ | ||
| 357 | return 1; | ||
| 358 | } | ||
| 359 | else{ | ||
| 360 | $self->errstr( $res->message ); | ||
| 361 | return 0; | ||
| 362 | } | ||
| 363 | |||
| 364 | } | ||
| 365 | |||
| 366 | |||
| 367 | |||
| 368 | |||
| 369 | |||
| 370 | |||
| 371 | |||
| 372 | |||
| 373 | 1; # End of Authen::GoogleAccount | ||