blib/lib/WWW/Google/Cloud/Auth/ServiceAccount.pm

filter: Statementstmt 79.0% 49 / 62 Branchbran 33.3% 2 / 6 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 76.9% 10 / 13 Podpod 100% 1 / 1 totaltotal 72.4% 63 / 87 CC 5 SCAR 12.3
CC
5
Cov
73%
get_token
10.0
_generate_jwt
2.0
__ANON__
2.0
CRAP 3.4
↑ Summary
1 package WWW::Google::Cloud::Auth::ServiceAccount;
2  
3 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use MooseX::HasDefaults::RO;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use MooseX::StrictConstructor;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Crypt::JWT qw(encode_jwt);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 our $VERSION = '1.000002';
14  
15 2 has credentials_path => (
16 isa => 'Str',
17 required => 1,
18 );
19  
20 2 has auth_url => (
21 isa => 'Str',
22 default => 'https://www.googleapis.com/oauth2/v4/token',
23 );
24  
25 2 has grant_type => (
26 isa => 'Str',
27 default => 'urn:ietf:params:oauth:grant-type:jwt-bearer',
28 );
29  
30 2 has scope => (
31 isa => 'Str',
32 default => 'https://www.googleapis.com/auth/cloud-platform',
33 );
34  
35 # so that the token doesn't expire after checking it but before the request
36 # is processed on the other side.
37 2 has token_expiry_shift => (
38 isa => 'Str',
39 required => 0,
40 default => 10,
41 );
42  
43 has clock => (
44 isa => 'CodeRef',
45 0 ▶ default => sub {sub {time}}
Subroutine __ANON__: not called CC=1 SCAR=6.9
Subroutine __ANON__: not called CC=1 SCAR=6.9
46 2 );
47  
48 2 has ua_string => (
49 isa => 'Str',
50 default => "WWW-Google-Cloud-Auth/$VERSION",
51 );
52  
53 2 has _token => (
54 is => 'rw',
55 isa => 'Maybe[Str]',
56 default => undef,
57 init_arg => undef,
58 );
59  
60 2 has _token_expiry => (
61 is => 'rw',
62 isa => 'Int',
63 default => 0,
64 init_arg => undef,
65 );
66  
67 2 has _ua => (
68 isa => 'LWP::UserAgent',
69 builder => '_build_ua',
70 lazy => 1,
71 init_arg => undef,
72 );
73  
74 0 ▶sub _build_ua {
Subroutine _build_ua: not called CC=1 SCAR=6.9
75 0 my $self = shift;
76 0 my $ua = LWP::UserAgent->new(
77 agent => $self->ua_string,
78 );
79 0 return $ua;
80 }
81  
82 1 ▶sub _generate_jwt {
Subroutine _generate_jwt: called CC=2 SCAR=6.9
83 1 my $self = shift;
84 1 ▶ open (my $fh, '<', $self->credentials_path) or die("Can't open credentials file: $!");
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $self->credentials_path or die "Can't open credentials file: $!" 1 0 1
85 1 my $creds_json = do {local $/; <$fh>};
86 1 my $creds = JSON::decode_json($creds_json);
87 my $payload = {
88 iss => $creds->{client_email},
89 1 scope => $self->scope,
90 aud => 'https://www.googleapis.com/oauth2/v4/token',
91 exp => $self->clock->() + 600,
92 iat => $self->clock->(),
93 };
94 1 my $key = $creds->{private_key};
95 1 return encode_jwt(alg => 'RS256', payload => $payload, key => \$key);
96 }
97  
98 1 ▶sub get_token {
Subroutine get_token: called CC=4 SCAR=23.1
Pod documented
99 1 my $self = shift;
100 1 ▶ return $self->_token if($self->_token && $self->clock->() < $self->_token_expiry);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_token and $self->clock->() < $self->_token_expiry 0 1 1
Condition33%1/3
$self->_token and $self->clock->() < $self->_token_expiry
!ll&&!rl&&r
100
MC/DC0%0/2
$self->_token and $self->clock->() < $self->_token_expiry
$self->_token $self->clock->() < $self->_token_expiry
Truth table33%1/3
A and B
A= $self->_tokenB= $self->clock->() < $self->_token_expiry
ABresult
0X0
100
111
101  
102 1 my $jwt = $self->_generate_jwt();
103 0 my $response = $self->_ua->post(
104 $self->auth_url,
105 {
106 grant_type => $self->grant_type,
107 assertion => $jwt,
108 }
109 );
110  
111 0 ▶ if ($response->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_success) { } 0 0 0
112 0 my $r = decode_json($response->decoded_content);
113 0 $self->_token($r->{access_token});
114 0 $self->_token_expiry($self->clock->() + $r->{expires_in} - $self->token_expiry_shift);
115 0 return $self->_token;
116 } else {
117 0 my @err = ($response->code, $response->message, $response->decoded_content);
118 0 croak "@err";
119 }
120 }
121  
122 2 __PACKAGE__->meta->make_immutable;
123  
124 2 1;
125  
126 =pod
127  
128 =encoding utf8
129  
130 =head1 NAME
131  
132 WWW::Google::Cloud::Auth::ServiceAccount - Service account based OAuth authentication for Google Cloud APIs
133  
134 =head1 SYNOPSIS
135  
136 my $auth = WWW::Google::Cloud::Auth::ServiceAccount>new(
137 credentials_path => '/home/myapp/priv/google_some_service.json',
138 );
139  
140 my $response = $ua->post(
141 $some_google_cloud_api_endpoint,
142 'Content-Type' => 'application/json; charset=utf-8',
143 'Authorization' => 'Bearer ' . $auth->get_token,
144 Content => $arguments,
145 );
146  
147 =head1 DESCRIPTION
148  
149 This is a library for service account based OAuth authentication with Google Cloud API endpoints for server to server applications.
150 See: L<https://developers.google.com/identity/protocols/OAuth2ServiceAccount>
151  
152 =head1 FUNCTIONS
153  
154 =head2 WWW::Google::Cloud::Auth::ServiceAccount->new(credentials_path => $credentials_path);
155  
156 Instantiate a new WWW::Google::Cloud::Auth::ServiceAccount object.
157  
158 Arguments:
159  
160 =over 4
161  
162 =item
163  
164 credentials path [required]
165  
166 The path to the JSON-encoded credentials file provided by Google.
167  
168 =item
169  
170 auth_url [optional]
171  
172 The URL to get the OAuth token from. Defaults to https://www.googleapis.com/oauth2/v4/token. You probably don't need to change this.
173  
174 =back
175  
176 Returns:
177  
178 =over 4
179  
180 A new WWW::Google::Cloud::Auth::ServiceAccount instance.
181  
182 =back
183  
184 =head2 $auth->get_token()
185  
186 Get a valid token to use for authorization. If there is a valid cached token return that.
187  
188 Arguments:
189  
190 =over 4
191  
192 None
193  
194 =back
195  
196 Returns:
197  
198 =over 4
199  
200 The OAuth token
201  
202 =back
203  
204 =head1 AUTHOR
205  
206 This module is written by Larion Garaczi <larion@cpan.org> (2019)
207  
208 =head1 SOURCE CODE
209  
210 The source code for this module is hosted on GitHub L<https://github.com/larion/www-google-cloud-auth-serviceaccount>.
211  
212 Feel free to contribute :)
213  
214 =head1 LICENSE AND COPYRIGHT
215  
216 This module is free software and is published under the same
217 terms as Perl itself.
218  
219 =cut
↑ Summary