| 1 | package Net::Google::AuthSub::Once; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | our $VERSION = '0.1.0'; | ||
| 6 | |||
| 7 | use URI; | ||
| 8 | use URI::QueryParam; | ||
| 9 | |||
| 10 | use Crypt::Random 'makerandom'; | ||
| 11 | use Crypt::OpenSSL::RSA; | ||
| 12 | use File::Slurp 'read_file'; | ||
| 13 | use MIME::Base64; | ||
| 14 | |||
| 15 | sub new { | ||
| 16 | my ($klass, $options) = @_; | ||
| 17 | my $self = bless {}, $klass; | ||
| 18 | $self->{private_key_filename} = $options->{private_key_filename}; | ||
| 19 | return $self; | ||
| 20 | } | ||
| 21 | |||
| 22 | sub get_authorization_url { | ||
| 23 | my ($self, $next_url) = @_; | ||
| 24 | my $google_url = URI->new("http://www.google.com/accounts/AuthSubRequest"); | ||
| 25 | $google_url->query_param('next' => $next_url); | ||
| 26 | $google_url->query_param('scope' => 'http://www.google.com/m8/feeds/contacts'); | ||
| 27 | $google_url->query_param('session' => 0); | ||
| 28 | $google_url->query_param('secure' => 1); | ||
| 29 | return $google_url; | ||
| 30 | } | ||
| 31 | |||
| 32 | sub sign_request { | ||
| 33 | my ($self, $request, $url, $token) = @_; | ||
| 34 | |||
| 35 | my $nonce = makerandom(Size => 64); | ||
| 36 | my $timestamp = time; | ||
| 37 | my $data = "GET $url $timestamp $nonce"; | ||
| 38 | |||
| 39 | my $private_key = Crypt::OpenSSL::RSA->new_private_key(scalar read_file($self->{'private_key_filename'})); | ||
| 40 | |||
| 41 | my $sig = encode_base64($private_key->sign($data)); | ||
| 42 | |||
| 43 | my $auth = qq{AuthSub token="$token" sigalg="rsa-sha1" data="$data" sig="$sig"}; | ||
| 44 | $request->header('Authorization', $auth); | ||
| 45 | |||
| 46 | return; | ||
| 47 | } | ||
| 48 | |||
| 49 | 1; | ||
| 50 | |||
| 51 | |||
| 52 | =head1 NAME | ||
| 53 | |||
| 54 | Net::Google::AuthSub::Once - Make one secure authenticated request to a Google service | ||
| 55 | |||
| 56 | =head1 SYNOPSYS | ||
| 57 | |||
| 58 | my $auth = Net::Google::AuthSub::Once->new(); | ||
| 59 | redirect_to($auth->get_authorization_url('http://example.com/your-next-url')); | ||
| 60 | |||
| 61 | # Then after the response comes back | ||
| 62 | |||
| 63 | # Make a request to the Google service | ||
| 64 | my $auth = Net::Google::AuthSub::Once->new({ private_key_filename => 'filename' }); | ||
| 65 | my $request = HTTP::Request->new(GET => 'http://www.google.com/...'); | ||
| 66 | $auth->sign_request($request); | ||
| 67 | my $resp = $ua->request($request); | ||
| 68 | |||
| 69 | =head1 DESCRIPTION | ||
| 70 | |||
| 71 | The nice thing about this module is that you don't need to know the Google | ||
| 72 | login details of the user of your applications. You can make a secure request | ||
| 73 | to a Google service in their place. | ||
| 74 | |||
| 75 | You must add your domain on Google for using secure requests. This module only | ||
| 76 | supports secure requests. L<https://www.google.com/accounts/ManageDomains> | ||
| 77 | |||
| 78 | Google has some information about create the private key file you need. | ||
| 79 | |||
| 80 | L<http://code.google.com/apis/gdata/docs/auth/authsub.html#Registered> | ||
| 81 | |||
| 82 | =head1 METHODS | ||
| 83 | |||
| 84 | =head2 CLASS->new($options) | ||
| 85 | |||
| 86 | =over 4 | ||
| 87 | |||
| 88 | =item * private_key_filename | ||
| 89 | |||
| 90 | The filename of a private key file. | ||
| 91 | |||
| 92 | =back | ||
| 93 | |||
| 94 | |||
| 95 | =head2 $self->get_authorization_url($next_url) | ||
| 96 | |||
| 97 | Returns the authorization url that you need to redirect to. Next_url is the url that | ||
| 98 | google will redirect you to after the request was authorized. | ||
| 99 | |||
| 100 | =head2 $self->sign_request($request, $url, $token) | ||
| 101 | |||
| 102 | Signs the HTTP::Request. | ||
| 103 | |||
| 104 | =head1 AUTHOR | ||
| 105 | |||
| 106 | Peter Stuifzand E<lt>peter@stuifzand.euE<gt> | ||
| 107 | |||
| 108 | =head1 COPYRIGHT | ||
| 109 | |||
| 110 | Copyright, 2010 - Peter Stuifzand | ||
| 111 | |||
| 112 | Released under the same terms as Perl itself | ||
| 113 | |||
| 114 | =cut | ||
| 115 |