blib/lib/Google/Auth/ServiceAccountCredentials.pm

filter: Statementstmt 94.3% 100 / 106 Branchbran 78.5% 11 / 14 Conditioncond 52.7% 19 / 36 MC/DCmcdc 30.4% 7 / 23 Subroutinesub 93.7% 15 / 16 Podpod 0.0% 0 / 1 totaltotal 77.5% 152 / 196 CC 21 SCAR 20.0
CC
21
Cov
83%
__ANON__
13.8
fetch_access_token
9.5
__ANON__
6.0
CRAP 7.4
« blib/lib/Google/Auth/RetryHelper.pm ↑ Summary blib/lib/Google/Auth/ImpersonatedServiceAccountCredentials.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 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 4 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 4 extends 'Google::Auth::Credentials';
22  
23 4 ▶use JSON::PP;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 4 ▶use MIME::Base64 qw(encode_base64url);
Subroutine BEGIN: called CC=1 SCAR=0.0
25 4 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 4 ▶use Google::Auth;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 4 ▶use Google::Auth::Exceptions;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 4 ▶use Google::Auth::RetryHelper;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 4 ▶use Log::Any qw($log);
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 4 has json_key => (
32 is => 'ro',
33 required => 0,
34 );
35  
36 4 has private_key_id => (
37 is => 'ro',
38 required => 0,
39 );
40  
41 4 has private_key => (
42 is => 'ro',
43 required => 0,
44 );
45  
46 4 has client_email => (
47 is => 'ro',
48 required => 0,
49 );
50  
51 4 has client_id => (
52 is => 'ro',
53 required => 0,
54 );
55  
56 4 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 4 );
68  
69 4 has auth_provider_x509_cert_url => (
70 is => 'ro',
71 required => 0,
72 );
73  
74 4 has client_x509_cert_url => (
75 is => 'ro',
76 required => 0,
77 );
78  
79 4 has scope => (
80 is => 'ro',
81 required => 0,
82 );
83  
84 has ua => (
85 is => 'ro',
86 2 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
87 2 my $ua = LWP::UserAgent->new(timeout => 10);
88 2 $ua->env_proxy;
89 2 return $ua;
90 },
91 4 );
92  
93 6 ▶around BUILDARGS => sub {
Subroutine __ANON__: called CC=11 SCAR=26.3
94 6 my ($orig, $class, @args) = @_;
95 6 my $args = $class->$orig(@args);
96  
97 6 ▶ if (my $json = $args->{json_key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $json = $args->{"json_key"}) 4 2 6
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};
Condition66%2/3
$args->{"private_key_id"} //= $json->{"private_key_id"}
l!l&&r!l&&!r
031
MC/DC50%1/2
$args->{"private_key_id"} //= $json->{"private_key_id"}
$args->{"private_key_id"} $json->{"private_key_id"}
Truth table66%2/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};
Condition66%2/3
$args->{"token_uri"} //= $json->{"token_uri"}
l!l&&r!l&&!r
031
MC/DC50%1/2
$args->{"token_uri"} //= $json->{"token_uri"}
$args->{"token_uri"} $json->{"token_uri"}
Truth table66%2/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 6 return $args;
111 4 };
112  
113 9 ▶sub _encode_base64url {
Subroutine _encode_base64url: called CC=1 SCAR=0.0
114 9 my ($data) = @_;
115 9 my $s = MIME::Base64::encode_base64url($data);
116 9 $s =~ s/=+$//;
117 9 return $s;
118 }
119  
120 4 ▶sub fetch_access_token {
Subroutine fetch_access_token: called CC=9 SCAR=22.5
Pod undocumented
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 3 ▶ if (!defined $private_key || !defined $client_email) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $private_key and defined $client_email) 0 3 3
Condition33%1/3
defined $private_key and defined $client_email
!ll&&!rl&&r
003
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 3 my $now = time();
138 3 my $header = {
139 alg => 'RS256',
140 typ => 'JWT',
141 };
142 3 ▶ $header->{kid} = $self->private_key_id if defined $self->private_key_id;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->private_key_id 2 1 3
143  
144 3 my $scope = $self->scope;
145 3 ▶ if (ref($scope) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $scope eq "ARRAY") 0 3 3
146 0 $scope = join(' ', @$scope);
147 }
148  
149 3 my $payload = {
150 iss => $client_email,
151 sub => $client_email,
152 aud => $token_uri,
153 exp => $now + 3600,
154 iat => $now,
155 };
156 3 ▶ $payload->{scope} = $scope if defined $scope;
Branch: 1 branch
BranchTrueFalseTotal
if defined $scope 1 2 3
157  
158 3 my $header_b64 = _encode_base64url(encode_json($header));
159 3 my $payload_b64 = _encode_base64url(encode_json($payload));
160 3 my $message = $header_b64 . '.' . $payload_b64;
161  
162 3 ▶ $log->tracef('Signing JWT assertion for service account %s (key ID: %s)...',
Condition100%2/2
$self->private_key_id // "N/A"
l!l
21
MC/DC100%1/1
$self->private_key_id // "N/A"
$self->private_key_id
Truth table100%2/2
A
A= $self->private_key_id
Aresult
11
00
163 $client_email, $self->private_key_id // 'N/A');
164 3 my $signature_raw = Google::Auth::rsa_sign_sha256($private_key, $message);
165 3 ▶ if (!defined $signature_raw) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $signature_raw) 0 3 3
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 3 my $signature_b64 = _encode_base64url($signature_raw);
172 3 my $assertion = $message . '.' . $signature_b64;
173  
174 3 my $ua = $self->ua;
175 3 my $post_body = {
176 grant_type => 'urn:ietf:params:oauth:grant-type:jwt-bearer',
177 assertion => $assertion,
178 };
179  
180 3 $log->infof('Exchanging signed JWT assertion for access token at %s...',
181 $token_uri);
182 my $response = Google::Auth::RetryHelper->execute_with_retry(
183 4 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
184 4 my $res = $ua->post(
185 $token_uri,
186 'Content-Type' => 'application/x-www-form-urlencoded',
187 'Content' => $post_body
188 );
189 4 ▶ if (!$res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res->is_success) 2 2 4
190 2 $log->warnf('Service account token request failed at %s: status %s',
191 $token_uri, $res->code);
192 2 Google::Auth::Error->throw('HTTP request failed with status ' .
193 $res->code . ': ' . $res->decoded_content);
194 }
195 2 return $res;
196 },
197 3 %options
198 );
199  
200 2 my $res_data = decode_json($response->decoded_content);
201 2 my $token = $res_data->{access_token};
202 2 ▶ my $expires = $res_data->{expires_in} // 3600;
Condition50%1/2
$res_data->{"expires_in"} // 3600
l!l
20
MC/DC0%0/1
$res_data->{"expires_in"} // 3600
$res_data->{"expires_in"}
Truth table50%1/2
A
A= $res_data->{"expires_in"}
Aresult
11
00
203  
204 2 $self->access_token($token);
205 2 $self->expires_at($now + $expires);
206  
207 2 return $token;
208 }
209  
210 4 1;
« blib/lib/Google/Auth/RetryHelper.pm ↑ Summary blib/lib/Google/Auth/ImpersonatedServiceAccountCredentials.pm »