lib/Net/Google/OAuth.pm

filter: Statementstmt 31.4% 28 / 89 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 27 MC/DCmcdc 0.0% 0 / 17 Subroutinesub 52.9% 9 / 17 Podpod 100% 6 / 6 totaltotal 25.6% 43 / 168 CC 18 SCAR 28.3
CC
18
Cov
22%
generateAccessToken
42.0
__exchangeCodeToToken
30.0
getTokenInfo
20.0
CRAP 17.0
↑ Summary blib/lib/Net/Google/OAuth.pm »
1 package Net::Google::OAuth;
2  
3 1 our $VERSION = '0.02';
4  
5 1 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use JSON::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Carp qw/carp croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 sub new {
17 0 ▶ my ($class, %opt) = @_;
Subroutine new: not called CC=3 SCAR=24.8
Pod documented
18 0 my $self = {};
19 0 ▶ $self->{client_secret} = $opt{-client_secret} || croak "You must specify '-client_secret' param";
Condition0%0/3
$opt{'-client_secret'} || croak(q[You must specify '-client_secret' param])
l!l&&r!l&&!r
000
MC/DC0%0/2
$opt{'-client_secret'} || croak(q[You must specify '-client_secret' param])
$opt{'-client_secret'} croak(q[You must specify '-client_secret' param])
Truth table0%0/3
A || B
A= $opt{'-client_secret'}B= croak(q[You must specify '-client_secret' param])
ABresult
1X1
011
000
20 0 ▶ $self->{client_id} = $opt{-client_id} || croak "You must specify '-client_id' param";
Condition0%0/3
$opt{'-client_id'} || croak(q[You must specify '-client_id' param])
l!l&&r!l&&!r
000
MC/DC0%0/2
$opt{'-client_id'} || croak(q[You must specify '-client_id' param])
$opt{'-client_id'} croak(q[You must specify '-client_id' param])
Truth table0%0/3
A || B
A= $opt{'-client_id'}B= croak(q[You must specify '-client_id' param])
ABresult
1X1
011
000
21 0 $self->{token} = {};
22 0 $self->{ua} = LWP::UserAgent->new();
23  
24 # Get list of OpenId services
25 0 __getOpenIdServices($self);
26  
27 0 return bless $self, $class;
28 }
29  
30  
31 sub generateAccessToken {
32 0 ▶ my ($self, %opt) = @_;
Subroutine generateAccessToken: not called CC=6 SCAR=37.4
Pod documented
33 0 ▶ $self->{scope} = $opt{-scope} || croak "You must specify '-scope' param";
Condition0%0/3
$opt{'-scope'} || croak(q[You must specify '-scope' param])
l!l&&r!l&&!r
000
MC/DC0%0/2
$opt{'-scope'} || croak(q[You must specify '-scope' param])
$opt{'-scope'} croak(q[You must specify '-scope' param])
Truth table0%0/3
A || B
A= $opt{'-scope'}B= croak(q[You must specify '-scope' param])
ABresult
1X1
011
000
34 0 ▶ $self->{email} = $opt{-email} || croak "You must specify '-email' param";
Condition0%0/3
$opt{'-email'} || croak(q[You must specify '-email' param])
l!l&&r!l&&!r
000
MC/DC0%0/2
$opt{'-email'} || croak(q[You must specify '-email' param])
$opt{'-email'} croak(q[You must specify '-email' param])
Truth table0%0/3
A || B
A= $opt{'-email'}B= croak(q[You must specify '-email' param])
ABresult
1X1
011
000
35 0 $self->{scope} = 'https://www.googleapis.com/auth/' . $self->{scope};
36  
37  
38 my $param = {
39 'client_id' => $self->{client_id},
40 'response_type' => 'code',
41 'scope' => $self->{scope},
42 'redirect_uri' => 'http://localhost:8000',
43 'state' => 'uniq_state_' . int(rand() * 100000),
44 'login_hint' => $self->{email},
45 0 'nonce' => int(rand() * 1000000) . '-' . int(rand() * 1000000) . '-' . int(rand() * 1000000),
46 'access_type' => 'offline',
47 };
48  
49 0 my $uri = URI->new($self->{services}->{authorization_endpoint});
50 0 $uri->query_form($param);
51
52 0 print STDOUT "Please open this URL in your browser: \n", "\x1b[4;34m", $uri->as_string, "\x1b[0m", "\n";
53 0 print STDOUT "Insert redirected address from browser here:\n";
54 0 my $response_step1 = <STDIN>;
55 0 $response_step1 =~ s/\r|\n//g;
56  
57 0 ▶ $uri = URI->new($response_step1) or croak "Can't parse response: $response_step1";
Branch: 1 branch
BranchTrueFalseTotal
$uri = "URI"->new($response_step1) or croak("Can't parse response: $response_step1") 0 0 0
58 0 my %query_form = $uri->query_form();
59 0 ▶ my $code_step1 = $query_form{code} // croak "Can't get 'code' from response url";
Condition0%0/3
$query_form{"code"} // croak("Can't get 'code' from response url")
l!l&&r!l&&!r
000
MC/DC0%0/2
$query_form{"code"} // croak("Can't get 'code' from response url")
$query_form{"code"} croak("Can't get 'code' from response url")
Truth table0%0/3
A // B
A= $query_form{"code"}B= croak("Can't get 'code' from response url")
ABresult
1X1
011
000
60  
61 0 my $token = $self->__exchangeCodeToToken(
62 -code => $code_step1,
63 -grant_type => 'authorization_code',
64 );
65  
66 0 for my $key (keys %$token) {
67 0 $self->{token}->{$key} = $token->{$key};
68 }
69  
70 0 return 1;
71 }
72  
73 sub getTokenInfo {
74 0 ▶ my ($self, %opt) = @_;
Subroutine getTokenInfo: not called CC=4 SCAR=30.0
Pod documented
75 0 ▶ my $access_token = $opt{-access_token} || $self->getAccessToken() || croak "You must specify '-access_token'";
Condition0%0/6
$opt{'-access_token'} || $self->getAccessToken || croak(q[You must specify '-access_token'])
l!l&&r!l&&!r
000
$opt{"-access_token"} or $self->getAccessToken
l!l&&r!l&&!r
000
MC/DC0%0/3
$opt{'-access_token'} || $self->getAccessToken || croak(q[You must specify '-access_token'])
$opt{"-access_token"} $self->getAccessToken croak(q[You must specify '-access_token'])
Truth table0%0/4
A or B || C
A= $opt{"-access_token"}B= $self->getAccessTokenC= croak(q[You must specify '-access_token'])
ABCresult
1XX1
01X1
0011
0000
76  
77 0 my $request = $self->{ua}->get('https://www.googleapis.com/oauth2/v2/tokeninfo?access_token=' . $access_token);
78 0 my $response_code = $request->code;
79 0 ▶ croak "Can't getTokenInfo about token: $access_token. Code: $response_code" if $response_code != 200;
Branch: 1 branch
BranchTrueFalseTotal
if $response_code != 200 0 0 0
80  
81 0 my $response = decode_json($request->content);
82  
83 0 return $response;
84 }
85  
86  
87  
88 sub refreshToken {
89 0 ▶ my ($self, %opt) = @_;
Subroutine refreshToken: not called CC=3 SCAR=24.8
Pod documented
90 0 ▶ my $refresh_token = $opt{-refresh_token} || croak "You must specify '-refresh_token' param";
Condition0%0/3
$opt{'-refresh_token'} || croak(q[You must specify '-refresh_token' param])
l!l&&r!l&&!r
000
MC/DC0%0/2
$opt{'-refresh_token'} || croak(q[You must specify '-refresh_token' param])
$opt{'-refresh_token'} croak(q[You must specify '-refresh_token' param])
Truth table0%0/3
A || B
A= $opt{'-refresh_token'}B= croak(q[You must specify '-refresh_token' param])
ABresult
1X1
011
000
91 0 my $token = $self->__exchangeCodeToToken(
92 -code => $refresh_token,
93 -grant_type => 'refresh_token',
94 );
95  
96 0 for my $key (keys %$token) {
97 0 $self->{token}->{$key} = $token->{$key};
98 }
99  
100 0 return 1;
101 }
102  
103 sub __exchangeCodeToToken{
104 #Exchange code or refresh token to AccessToken
105 0 ▶ my ($self, %opt) = @_;
Subroutine __exchangeCodeToToken: not called CC=5 SCAR=34.0
106 0 my $code = $opt{-code};
107 0 ▶ my $grant_type = $opt{-grant_type} || croak "You must specify '-grant_type' param";
Condition0%0/3
$opt{'-grant_type'} || croak(q[You must specify '-grant_type' param])
l!l&&r!l&&!r
000
MC/DC0%0/2
$opt{'-grant_type'} || croak(q[You must specify '-grant_type' param])
$opt{'-grant_type'} croak(q[You must specify '-grant_type' param])
Truth table0%0/3
A || B
A= $opt{'-grant_type'}B= croak(q[You must specify '-grant_type' param])
ABresult
1X1
011
000
108  
109 # Exchange code to token
110 my $param = {
111 'client_id' => $self->{client_id},
112 'client_secret' => $self->{client_secret},
113 0 'redirect_uri' => 'http://localhost:8000',
114 'grant_type' => $grant_type,
115 'access_type' => 'offline',
116 };
117 0 ▶ if ($grant_type eq 'authorization_code') {
Branch: 2 branches
BranchTrueFalseTotal
if ($grant_type eq 'authorization_code') { } 0 0 0
elsif ($grant_type eq 'refresh_token') { } 0 0 0
118 0 $param->{code} = $code;
119 }
120 elsif ($grant_type eq 'refresh_token') {
121 0 $param->{refresh_token} = $code;
122 }
123 else {
124 0 croak "Param '-grant_type' must contain values: 'authorization_code' or 'refresh_token'";
125 }
126  
127 my $response = $self->{ua}->post(
128 $self->{services}->{token_endpoint},
129 0 $param,
130 );
131 0 my $response_code = $response->code;
132 0 ▶ croak "Can't get token. Code: $response_code" if $response_code != 200;
Branch: 1 branch
BranchTrueFalseTotal
if $response_code != 200 0 0 0
133  
134 0 my $token = decode_json($response->content);
135  
136 0 return $token;
137 }
138  
139 sub __getOpenIdServices {
140 0 ▶ my ($self) = @_;
Subroutine __getOpenIdServices: not called CC=2 SCAR=17.9
141  
142 0 my $request = $self->{ua}->get('https://accounts.google.com/.well-known/openid-configuration');
143 0 my $response_code = $request->code;
144 0 ▶ croak "Can't get list of OpenId services" if $response_code != 200;
Branch: 1 branch
BranchTrueFalseTotal
if $response_code != 200 0 0 0
145  
146 0 my $response = decode_json($request->content);
147  
148 0 $self->{services} = $response;
149  
150 0 return 1;
151 }
152  
153 ################### ACESSORS #########################
154 sub getAccessToken {
155 0 ▶ return $_[0]->{token}->{access_token};
Subroutine getAccessToken: not called CC=1 SCAR=6.9
Pod documented
156 }
157  
158 sub getRefreshToken {
159 0 ▶ return $_[0]->{token}->{refresh_token};
Subroutine getRefreshToken: not called CC=1 SCAR=6.9
Pod documented
160 }
161  
162  
163 =head1 NAME
164  
165 B<Net::Google::OAuth> - Simple Google oauth api module
166  
167 =head1 SYNOPSIS
168  
169 This module get acess_token and refresh_token from google oath
170 use Net::Google::OAuth;
171  
172 #Create oauth object. You need set client_id and client_secret value. Client_id and client_secret you can get on google, when register your app.
173 my $oauth = Net::Google::OAuth->new(
174 -client_id => $CLIENT_ID,
175 -client_secret => $CLIENT_SECRET,
176 );
177 #Generate link with request access token. This link you must copy to your browser and run.
178 $oauth->generateAccessToken(
179 -scope => 'drive',
180 -email => 'youremail@gmail.com',
181 );
182 print "Access token: ", $oauth->getAccessToken(), "\n";
183 print "Refresh token: ", $oauth->getRefreshToken, "\n";
184  
185 =head1 METHODS
186  
187 =head2 new(%opt)
188  
189 Create L<Net::Google::OAuth> object
190  
191 %opt:
192 -client_id => Your app client id (Get from google when register your app)
193 -client_secret => Your app client secret (Get from google when register your app)
194  
195 =head2 generateAccessToken(%opt)
196  
197 Generate link with request access token This link you must copy to your browser and go it. Redirect answer you must copy to console. Return 1 if success, die in otherwise
198  
199 %opt
200 -scope => Request access to scope (e.g. 'drive')
201 -email => Your gmail email
202  
203 =head2 refreshToken(%opt)
204  
205 Get access token through refresh_token. Return 1 if success, die in otherwise
206  
207 %opt:
208 -refresh_token => Your refresh token value (you can get refresh token after run method generateAccessToken() via getter getRefreshToken())
209  
210 =head2 getTokenInfo(%opt)
211  
212 Get info about access token (access_type, audience, expires_in, issued_to, scope). Return hashref of result or die in otherwise
213  
214 %opt:
215 -access_token => Value of access_token (default use value returned by method getRefreshToken())
216 Example:
217 my $token_info = $oauth->getTokenInfo( -access_token => $access_token );
218 $token_info:
219 {
220 access_type "offline",
221 audience "593952972427-e6dr18ua0leurrjt1num.apps.googleusercontent.com",
222 expires_in 3558,
223 issued_to "593952972427-e6dr18ua0leurrjtum.apps.googleusercontent.com",
224 scope "https://www.googleapis.com/auth/drive"
225 }
226  
227  
228 =head2 getAccessToken()
229  
230 Return access token value
231  
232 =head2 getRefreshToken()
233  
234 Return refresh token value
235  
236 =head1 DEPENDENCE
237  
238 L<LWP::UserAgent>, L<JSON::XS>, L<URI>, L<HTTP::Request>
239  
240 =head1 AUTHORS
241  
242 =over 4
243  
244 =item *
245  
246 Pavel Andryushin <vrag867@gmail.com>
247  
248 =back
249  
250 =head1 COPYRIGHT AND LICENSE
251  
252 This software is copyright (c) 2018 by Pavel Andryushin.
253  
254 This is free software; you can redistribute it and/or modify it under
255 the same terms as the Perl 5 programming language system itself.
256  
257 =cut
258  
259 1 1;
↑ Summary blib/lib/Net/Google/OAuth.pm »