blib/lib/Google/Auth/ServiceAccountCredentials.pm

filter: Statementstmt 76.4% 94 / 123 Branchbran 44.4% 8 / 18 Conditioncond 31.9% 15 / 47 MC/DCmcdc 14.2% 4 / 28 Subroutinesub 93.7% 15 / 16 Podpod 100% 1 / 1 totaltotal 58.8% 137 / 233 CC 28 SCAR 33.1
CC
28
Cov
62%
__ANON__
83.3
fetch_access_token
25.4
__ANON__
15.5
CRAP 27.3
« blib/lib/Google/Auth/ExternalAccountCredentials/Aws.pm ↑ Summary blib/lib/Google/Auth/DefaultCredentials.pm »
1 # Copyright 2026 Google LLC and contributors
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14  
15 package Google::Auth::ServiceAccountCredentials;
16  
17 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 3 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 3 extends 'Google::Auth::Credentials';
22  
23 3 ▶use JSON::PP;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 3 ▶use MIME::Base64 qw(encode_base64url);
Subroutine BEGIN: called CC=1 SCAR=0.0
25 3 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 3 ▶use Google::Auth;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 3 ▶use Google::Auth::Exceptions;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 3 ▶use Google::Auth::RetryHelper;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 3 ▶use Log::Any qw($log);
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 3 has json_key => (
32 is => 'ro',
33 required => 0,
34 );
35  
36 3 has private_key_id => (
37 is => 'ro',
38 required => 0,
39 );
40  
41 3 has private_key => (
42 is => 'ro',
43 required => 0,
44 );
45  
46 3 has client_email => (
47 is => 'ro',
48 required => 0,
49 );
50  
51 3 has client_id => (
52 is => 'ro',
53 required => 0,
54 );
55  
56 3 has auth_uri => (
57 is => 'ro',
58 required => 0,
59 );
60  
61 has token_uri => (
62 is => 'ro',
63 required => 0,
64 0 ▶ default => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
65 0 ▶ $ENV{GOOGLE_AUTH_TOKEN_URI} // 'https://oauth2.googleapis.com/token';
Condition0%0/2
$ENV{"GOOGLE_AUTH_TOKEN_URI"} // "https://oauth2.googleapis.com/token"
l!l
00
MC/DC0%0/1
$ENV{"GOOGLE_AUTH_TOKEN_URI"} // "https://oauth2.googleapis.com/token"
$ENV{"GOOGLE_AUTH_TOKEN_URI"}
Truth table0%0/2
A
A= $ENV{"GOOGLE_AUTH_TOKEN_URI"}
Aresult
11
00
66 },
67 3 );
68  
69 3 has auth_provider_x509_cert_url => (
70 is => 'ro',
71 required => 0,
72 );
73  
74 3 has client_x509_cert_url => (
75 is => 'ro',
76 required => 0,
77 );
78  
79 3 has scope => (
80 is => 'ro',
81 required => 0,
82 );
83  
84 has ua => (
85 is => 'ro',
86 1 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
87 1 my $ua = LWP::UserAgent->new(timeout => 10);
88 1 $ua->env_proxy;
89 1 return $ua;
90 },
91 3 );
92  
93 5 ▶around BUILDARGS => sub {
Subroutine __ANON__: called CC=11 SCAR=27.4
94 5 my ($orig, $class, @args) = @_;
95 5 my $args = $class->$orig(@args);
96  
97 5 ▶ if (my $json = $args->{json_key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $json = $args->{"json_key"}) 4 1 5
98 4 ▶ $args->{project_id} //= $json->{project_id};
Condition33%1/3
$args->{"project_id"} //= $json->{"project_id"}
l!l&&r!l&&!r
400
MC/DC0%0/2
$args->{"project_id"} //= $json->{"project_id"}
$args->{"project_id"} $json->{"project_id"}
Truth table33%1/3
A //= B
A= $args->{"project_id"}B= $json->{"project_id"}
ABresult
1X1
011
000
99 4 ▶ $args->{private_key_id} //= $json->{private_key_id};
Condition33%1/3
$args->{"private_key_id"} //= $json->{"private_key_id"}
l!l&&r!l&&!r
040
MC/DC0%0/2
$args->{"private_key_id"} //= $json->{"private_key_id"}
$args->{"private_key_id"} $json->{"private_key_id"}
Truth table33%1/3
A //= B
A= $args->{"private_key_id"}B= $json->{"private_key_id"}
ABresult
1X1
011
000
100 4 ▶ $args->{private_key} //= $json->{private_key};
Condition33%1/3
$args->{"private_key"} //= $json->{"private_key"}
l!l&&r!l&&!r
040
MC/DC0%0/2
$args->{"private_key"} //= $json->{"private_key"}
$args->{"private_key"} $json->{"private_key"}
Truth table33%1/3
A //= B
A= $args->{"private_key"}B= $json->{"private_key"}
ABresult
1X1
011
000
101 4 ▶ $args->{client_email} //= $json->{client_email};
Condition33%1/3
$args->{"client_email"} //= $json->{"client_email"}
l!l&&r!l&&!r
040
MC/DC0%0/2
$args->{"client_email"} //= $json->{"client_email"}
$args->{"client_email"} $json->{"client_email"}
Truth table33%1/3
A //= B
A= $args->{"client_email"}B= $json->{"client_email"}
ABresult
1X1
011
000
102 4 ▶ $args->{client_id} //= $json->{client_id};
Condition66%2/3
$args->{"client_id"} //= $json->{"client_id"}
l!l&&r!l&&!r
013
MC/DC50%1/2
$args->{"client_id"} //= $json->{"client_id"}
$args->{"client_id"} $json->{"client_id"}
Truth table66%2/3
A //= B
A= $args->{"client_id"}B= $json->{"client_id"}
ABresult
1X1
011
000
103 4 ▶ $args->{auth_uri} //= $json->{auth_uri};
Condition66%2/3
$args->{"auth_uri"} //= $json->{"auth_uri"}
l!l&&r!l&&!r
013
MC/DC50%1/2
$args->{"auth_uri"} //= $json->{"auth_uri"}
$args->{"auth_uri"} $json->{"auth_uri"}
Truth table66%2/3
A //= B
A= $args->{"auth_uri"}B= $json->{"auth_uri"}
ABresult
1X1
011
000
104 4 ▶ $args->{token_uri} //= $json->{token_uri};
Condition33%1/3
$args->{"token_uri"} //= $json->{"token_uri"}
l!l&&r!l&&!r
040
MC/DC0%0/2
$args->{"token_uri"} //= $json->{"token_uri"}
$args->{"token_uri"} $json->{"token_uri"}
Truth table33%1/3
A //= B
A= $args->{"token_uri"}B= $json->{"token_uri"}
ABresult
1X1
011
000
105 $args->{auth_provider_x509_cert_url} //=
106 4 ▶ $json->{auth_provider_x509_cert_url};
Condition66%2/3
$args->{"auth_provider_x509_cert_url"} //= $json->{"auth_provider_x509_cert_url"}
l!l&&r!l&&!r
013
MC/DC50%1/2
$args->{"auth_provider_x509_cert_url"} //= $json->{"auth_provider_x509_cert_url"}
$args->{"auth_provider_x509_cert_url"} $json->{"auth_provider_x509_cert_url"}
Truth table66%2/3
A //= B
A= $args->{"auth_provider_x509_cert_url"}B= $json->{"auth_provider_x509_cert_url"}
ABresult
1X1
011
000
107 4 ▶ $args->{client_x509_cert_url} //= $json->{client_x509_cert_url};
Condition66%2/3
$args->{"client_x509_cert_url"} //= $json->{"client_x509_cert_url"}
l!l&&r!l&&!r
013
MC/DC50%1/2
$args->{"client_x509_cert_url"} //= $json->{"client_x509_cert_url"}
$args->{"client_x509_cert_url"} $json->{"client_x509_cert_url"}
Truth table66%2/3
A //= B
A= $args->{"client_x509_cert_url"}B= $json->{"client_x509_cert_url"}
ABresult
1X1
011
000
108 }
109  
110 5 return $args;
111 3 };
112  
113 6 ▶sub _encode_base64url {
Subroutine _encode_base64url: called CC=1 SCAR=0.0
114 6 my ($data) = @_;
115 6 my $s = MIME::Base64::encode_base64url($data);
116 6 $s =~ s/=+$//;
117 6 return $s;
118 }
119  
120 4 ▶sub fetch_access_token {
Subroutine fetch_access_token: called CC=9 SCAR=32.3
Pod documented
121 4 my ($self, %options) = @_;
122  
123 4 my $private_key = $self->private_key;
124 4 my $client_email = $self->client_email;
125 4 my $token_uri = $self->token_uri;
126  
127 4 $self->_validate_url($token_uri, 'token_uri');
128  
129 2 ▶ if (!defined $private_key || !defined $client_email) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $private_key and defined $client_email) 0 2 2
Condition33%1/3
defined $private_key and defined $client_email
!ll&&!rl&&r
002
MC/DC0%0/2
defined $private_key and defined $client_email
defined $private_key defined $client_email
Truth table33%1/3
A and B
A= defined $private_keyB= defined $client_email
ABresult
0X0
100
111
130 0 $log->errorf(
131 'Missing private_key or client_email for ServiceAccountCredentials token exchange'
132 );
133 0 Google::Auth::Error->throw(
134 'Missing private_key or client_email to sign and fetch token');
135 }
136  
137 2 my $now = time();
138 2 my $header = {
139 alg => 'RS256',
140 typ => 'JWT',
141 };
142 2 ▶ $header->{kid} = $self->private_key_id if defined $self->private_key_id;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->private_key_id 2 0 2
143  
144 2 my $scope = $self->scope;
145 2 ▶ if (ref($scope) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $scope eq "ARRAY") 0 2 2
146 0 $scope = join(' ', @$scope);
147 }
148  
149 2 my $payload = {
150 iss => $client_email,
151 sub => $client_email,
152 aud => $token_uri,
153 exp => $now + 3600,
154 iat => $now,
155 };
156 2 ▶ $payload->{scope} = $scope if defined $scope;
Branch: 1 branch
BranchTrueFalseTotal
if defined $scope 1 1 2
157  
158 2 my $header_b64 = _encode_base64url(encode_json($header));
159 2 my $payload_b64 = _encode_base64url(encode_json($payload));
160 2 my $message = $header_b64 . '.' . $payload_b64;
161  
162 2 ▶ $log->tracef('Signing JWT assertion for service account %s (key ID: %s)...',
Condition50%1/2
$self->private_key_id // "N/A"
l!l
20
MC/DC0%0/1
$self->private_key_id // "N/A"
$self->private_key_id
Truth table50%1/2
A
A= $self->private_key_id
Aresult
11
00
163 $client_email, $self->private_key_id // 'N/A');
164 2 my $signature_raw = Google::Auth::rsa_sign_sha256($private_key, $message);
165 2 ▶ if (!defined $signature_raw) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $signature_raw) 0 2 2
166 0 $log->errorf('Failed to sign JWT assertion for service account %s',
167 $client_email);
168 0 Google::Auth::Error->throw(
169 'Failed to sign JWT assertion using private key');
170 }
171 2 my $signature_b64 = _encode_base64url($signature_raw);
172 2 my $assertion = $message . '.' . $signature_b64;
173  
174 2 my $ua = $self->ua;
175 2 my $post_body = {
176 grant_type => 'urn:ietf:params:oauth:grant-type:jwt-bearer',
177 assertion => $assertion,
178 };
179  
180 2 require URI;
181 2 my $u = URI->new('http:');
182 2 $u->query_form(%$post_body);
183 2 my $encoded_body = $u->query;
184  
185 2 $log->infof('Exchanging signed JWT assertion for access token at %s...',
186 $token_uri);
187 my $response_body = Google::Auth::RetryHelper->execute_with_retry(
188 6 ▶ sub {
Subroutine __ANON__: called CC=9 SCAR=44.2
189 6 my $future = $self->transport->request(
190 method => 'POST',
191 url => $token_uri,
192 headers => {'Content-Type' => 'application/x-www-form-urlencoded'},
193 body => $encoded_body,
194 );
195  
196 0 my ($body, $headers);
197 0 my $future_res = eval { ($body, $headers) = $future->get(); 1 };
198  
199 0 ▶ if (!$future_res) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($future_res) 0 0 0
200 0 my ($err_msg, $cat, $details) = $future->failure;
201 0 my $code = 0;
202 0 my $error_body = '';
203 0 ▶ if (ref($details) eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $details eq 'HASH') { } 0 0 0
elsif (eval { do { $details->can('code') } }) { } 0 0 0
204 0 ▶ $code = $details->{code} // 0;
Condition0%0/2
$details->{"code"} // 0
l!l
00
MC/DC0%0/1
$details->{"code"} // 0
$details->{"code"}
Truth table0%0/2
A
A= $details->{"code"}
Aresult
11
00
205 0 ▶ $error_body = $details->{body} // '';
Condition0%0/2
$details->{"body"} // ""
l!l
00
MC/DC0%0/1
$details->{"body"} // ""
$details->{"body"}
Truth table0%0/2
A
A= $details->{"body"}
Aresult
11
00
206 0 } elsif (eval { $details->can('code') }) {
207 0 ▶ $code = $details->code // 0;
Condition0%0/2
$details->code // 0
l!l
00
MC/DC0%0/1
$details->code // 0
$details->code
Truth table0%0/2
A
A= $details->code
Aresult
11
00
208 0 ▶ $error_body = $details->decoded_content // $details->content // '';
Condition0%0/5
($details->decoded_content // $details->content) // ""
l!l
00
$details->decoded_content // $details->content
l!l&&r!l&&!r
000
MC/DC0%0/2
($details->decoded_content // $details->content) // ""
$details->decoded_content $details->content
Truth table0%0/3
A // B
A= $details->decoded_contentB= $details->content
ABresult
1X1
011
000
209 }
210 0 $log->warnf('Service account token request failed at %s: status %s',
211 $token_uri, $code);
212 0 Google::Auth::Error->throw(
213 'HTTP request failed with status ' . $code . ': ' . $error_body);
214 }
215 0 return $body;
216 },
217 2 %options
218 );
219  
220 0 my $res_data = decode_json($response_body);
221 0 my $token = $res_data->{access_token};
222 0 ▶ my $expires = $res_data->{expires_in} // 3600;
Condition0%0/2
$res_data->{"expires_in"} // 3600
l!l
00
MC/DC0%0/1
$res_data->{"expires_in"} // 3600
$res_data->{"expires_in"}
Truth table0%0/2
A
A= $res_data->{"expires_in"}
Aresult
11
00
223  
224 0 $self->access_token($token);
225 0 $self->expires_at($now + $expires);
226  
227 0 return $token;
228 }
229  
230 3 1;
231  
232 __END__
« blib/lib/Google/Auth/ExternalAccountCredentials/Aws.pm ↑ Summary blib/lib/Google/Auth/DefaultCredentials.pm »