File Coverage

lib/OAuth2/Google/Plus.pm
Criterion Covered Total %
statement 40 40 100.0
branch 4 4 100.0
condition n/a
subroutine 12 12 100.0
pod 2 3 66.6
total 58 59 98.3


line stmt bran cond sub pod time code
1 1     1   70037 use strict;
  1         2  
  1         37  
2 1     1   5 use warnings;
  1         2  
  1         64  
3              
4             # ABSTRACT: simple wrapper for google+ OAuth2 API
5              
6             =head1 NAME
7              
8             OAuth2::Google::Plus
9              
10             =head1 DESCRIPTION
11              
12             This is an implementation of the google OAuth2 API. It's a rather specific
13             implementation of this specific OAuth2 provider. Small implementation details
14             differ per provider, this module attempts to abstract and document the Google version.
15              
16             =head1 SYNOPSYS
17              
18             use OAuth2::Google::Plus;
19              
20             my $plus = OAuth2::Google::Plus->new(
21             client_id => 'CLIENT ID',
22             client_secret => 'CLIENT SECRET',
23             redirect_uri => 'http://my.app.com/authorize',
24             );
25              
26             # generate the link for signup
27             my $uri = $plus->authorization_uri( redirect_url => $url_string )
28              
29             # callback returns with a code in url
30             my $access_token = $plus->authorize( $request->param('code') );
31              
32             # store $access_token somewhere safe...
33              
34             # use $authorization_token
35             my $info = OAuth2::Google::Plus::UserInfo->new( access_token => $access_token );
36              
37             =over
38              
39             =item authorization_uri
40              
41             Construct an URI object for authorization. This url should be use to provide a login
42             button to the user
43              
44             =item authorize ( authorization_code => $code )
45              
46             Use an authorization_token to retrieve an access_token from google. This access token
47             can be used to retrieve information about the user who authorized.
48              
49             =back
50              
51             =cut
52              
53             {
54             package OAuth2::Google::Plus;
55 1     1   1115 use Moo;
  1         19580  
  1         7  
56 1     1   2964 use MooX::late;
  1         40368  
  1         12  
57              
58 1     1   1313 use Carp::Assert;
  1         1400  
  1         7  
59 1     1   1282 use JSON qw|decode_json|;
  1         16476  
  1         7  
60 1     1   1609 use LWP::UserAgent;
  1         52435  
  1         44  
61 1     1   11 use URI;
  1         3  
  1         514  
62              
63             sub ENDPOINT_URL {
64 2     2 0 24 return 'https://accounts.google.com/o/oauth2';
65             }
66              
67             has client_id => (
68             is => 'ro',
69             isa => 'Str',
70             required => 1,
71             );
72              
73             has client_secret => (
74             is => 'ro',
75             isa => 'Str',
76             required => 1,
77             );
78              
79             has scope => (
80             is => 'ro',
81             isa => 'Str',
82             default => 'https://www.googleapis.com/auth/userinfo.email',
83             required => 1,
84             );
85              
86             has state => (
87             is => 'ro',
88             isa => 'Str',
89             required => 0,
90             );
91              
92             has redirect_uri => (
93             is => 'ro',
94             isa => 'Str',
95             required => 1,
96             );
97              
98             has response => (
99             is => 'ro',
100             writer => '_set_response',
101             );
102              
103             has _endpoint => (
104             is => 'ro',
105             lazy_build => 1,
106             );
107              
108             sub _build__endpoint {
109 2     2   475 return OAuth2::Google::Plus::ENDPOINT_URL();
110             }
111              
112             sub authorization_uri {
113 3     3 1 7394 my ( $self ) = @_;
114              
115 3         66 my $uri = URI->new( $self->_endpoint . '/auth' );
116              
117 3 100       17683 $uri->query_form(
118             access_type => 'offline',
119             approval_prompt => 'force',
120             client_id => $self->client_id,
121             redirect_uri => $self->redirect_uri,
122             response_type => 'code',
123             scope => $self->scope,
124             ($self->state ? (state => $self->state) : ()),
125             );
126              
127 3         770 return $uri;
128             }
129              
130             sub authorize {
131 2     2 1 2853 my ( $self, %params ) = @_;
132              
133 2         16 assert( $params{authorization_code}, 'missing named argument "authorization_code"');
134              
135 2         63 my $uri = URI->new( $self->_endpoint . '/token' );
136 2         1892 my $ua = LWP::UserAgent->new;
137              
138 2         2920 my $response = $ua->post( $uri, {
139             client_id => $self->client_id,
140             client_secret => $self->client_secret,
141             code => $params{authorization_code},
142             grant_type => 'authorization_code',
143             redirect_uri => $self->redirect_uri,
144             scope => $self->scope,
145             });
146              
147 2         27664 $self->_set_response( $response );
148              
149 2 100       9 if( $response->is_success ){
150 1         22 my $json = decode_json( $response->content );
151 1         82 return $json->{access_token};
152             }
153              
154 1         49 return;
155             }
156             }
157              
158             1;