Plus.pm

filter: Statementstmt 100% 49 / 49 Branchbran 100% 4 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 12 / 12 Podpod 66.6% 2 / 3 totaltotal 98.5% 67 / 68 CC 3 SCAR 2.5
CC
3
Cov
100%
authorization_uri
2.0
authorize
2.0
BEGIN
1.0
CRAP 1.3
↑ Summary Plus/UserInfo.pm »
1 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
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 ▶ use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
56 1 ▶ use MooX::late;
Subroutine BEGIN: called CC=1 SCAR=0.0
57  
58 1 ▶ use Carp::Assert;
Subroutine BEGIN: called CC=1 SCAR=0.0
59 1 ▶ use JSON qw|decode_json|;
Subroutine BEGIN: called CC=1 SCAR=0.0
60 1 ▶ use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
61 1 ▶ use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
62  
63 2 ▶ sub ENDPOINT_URL {
Subroutine ENDPOINT_URL: called CC=1 SCAR=0.0
Pod undocumented
64 2 return 'https://accounts.google.com/o/oauth2';
65 }
66  
67 1 has client_id => (
68 is => 'ro',
69 isa => 'Str',
70 required => 1,
71 );
72  
73 1 has client_secret => (
74 is => 'ro',
75 isa => 'Str',
76 required => 1,
77 );
78  
79 1 has scope => (
80 is => 'ro',
81 isa => 'Str',
82 default => 'https://www.googleapis.com/auth/userinfo.email',
83 required => 1,
84 );
85  
86 1 has state => (
87 is => 'ro',
88 isa => 'Str',
89 required => 0,
90 );
91  
92 1 has redirect_uri => (
93 is => 'ro',
94 isa => 'Str',
95 required => 1,
96 );
97  
98 1 has response => (
99 is => 'ro',
100 writer => '_set_response',
101 );
102  
103 1 has _endpoint => (
104 is => 'ro',
105 lazy_build => 1,
106 );
107  
108 2 ▶ sub _build__endpoint {
Subroutine _build__endpoint: called CC=1 SCAR=0.0
109 2 return OAuth2::Google::Plus::ENDPOINT_URL();
110 }
111  
112 3 ▶ sub authorization_uri {
Subroutine authorization_uri: called CC=2 SCAR=6.9
Pod documented
113 3 my ( $self ) = @_;
114  
115 3 my $uri = URI->new( $self->_endpoint . '/auth' );
116  
117 3 ▶ $uri->query_form(
Branch: 1 branch
BranchTrueFalseTotal
$self->state ? : 1 2 3
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 return $uri;
128 }
129  
130 2 ▶ sub authorize {
Subroutine authorize: called CC=2 SCAR=6.9
Pod documented
131 2 my ( $self, %params ) = @_;
132  
133 2 assert( $params{authorization_code}, 'missing named argument "authorization_code"');
134  
135 2 my $uri = URI->new( $self->_endpoint . '/token' );
136 2 my $ua = LWP::UserAgent->new;
137  
138 my $response = $ua->post( $uri, {
139 client_id => $self->client_id,
140 client_secret => $self->client_secret,
141 code => $params{authorization_code},
142 2 grant_type => 'authorization_code',
143 redirect_uri => $self->redirect_uri,
144 scope => $self->scope,
145 });
146  
147 2 $self->_set_response( $response );
148  
149 2 ▶ if( $response->is_success ){
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_success) 1 1 2
150 1 my $json = decode_json( $response->content );
151 1 return $json->{access_token};
152 }
153  
154 1 return;
155 }
156 }
157  
158 1 1;
↑ Summary Plus/UserInfo.pm »