blib/lib/Google/Auth/Credentials.pm

filter: Statementstmt 68.5% 85 / 124 Branchbran 42.3% 22 / 52 Conditioncond 41.0% 16 / 39 MC/DCmcdc 24.0% 6 / 25 Subroutinesub 88.8% 16 / 18 Podpod 0.0% 0 / 4 totaltotal 55.3% 145 / 262 CC 41 SCAR 30.2
CC
41
Cov
57%
is_expired
51.4
apply
42.0
get_token
27.4
CRAP 20.5
« 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::Credentials;
16  
17 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 15 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 15 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 15 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 15 ▶use Time::Piece;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 15 ▶use Google::Auth::Exceptions;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 15 ▶use Time::HiRes;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 15 ▶use Log::Any qw($log);
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 15 has access_token => (is => 'rw',);
28  
29 15 has expires_at => (is => 'rw',);
30  
31 has universe_domain => (
32 is => 'ro',
33 42 ▶ default => sub { 'googleapis.com' },
Subroutine __ANON__: called CC=1 SCAR=0.0
34 15 );
35  
36 15 has quota_project_id => (
37 is => 'ro',
38 required => 0,
39 );
40  
41 15 has project_id => (
42 is => 'ro',
43 required => 0,
44 );
45  
46 has _is_universe_pinned => (
47 is => 'ro',
48 12 ▶ default => sub { 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
49 15 );
50  
51 has clock_skew => (
52 is => 'ro',
53 46 ▶ default => sub { 300 },
Subroutine __ANON__: called CC=1 SCAR=0.0
54 15 );
55  
56 has is_refreshing => (
57 is => 'rw',
58 46 ▶ default => sub { 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
59 15 );
60  
61 72 ▶around BUILDARGS => sub {
Subroutine __ANON__: called CC=9 SCAR=22.5
62 72 my ($orig, $class, @args) = @_;
63  
64 # Standardize arguments to a hashref
65 72 my $args;
66 72 ▶ if (@args == 1 && ref $args[0] eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (@args == 1 and ref $args[0] eq 'HASH') { } 23 49 72
Condition66%2/3
@args == 1 and ref $args[0] eq "HASH"
!ll&&!rl&&r
49023
MC/DC50%1/2
@args == 1 and ref $args[0] eq "HASH"
@args == 1 ref $args[0] eq "HASH"
Truth table66%2/3
A and B
A= @args == 1B= ref $args[0] eq "HASH"
ABresult
0X0
100
111
67 23 $args = {%{$args[0]}};
68 } else {
69 49 $args = {@args};
70 }
71  
72 # If universe_domain is explicitly passed in code/options, it is pinned (trusted).
73 72 ▶ if (exists $args->{universe_domain}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args->{"universe_domain"}) 2 70 72
74 2 ▶ $args->{_is_universe_pinned} //= 1;
Condition50%1/2
$args->{"_is_universe_pinned"} //= 1
l!l
20
MC/DC0%0/1
$args->{"_is_universe_pinned"} //= 1
$args->{"_is_universe_pinned"}
Truth table50%1/2
A
A= $args->{"_is_universe_pinned"}
Aresult
11
00
75 }
76  
77 72 ▶ if (my $json = $args->{json_key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $json = $args->{"json_key"}) 11 61 72
78 11 ▶ $args->{quota_project_id} //= $json->{quota_project_id};
Condition33%1/3
$args->{"quota_project_id"} //= $json->{"quota_project_id"}
l!l&&r!l&&!r
0011
MC/DC0%0/2
$args->{"quota_project_id"} //= $json->{"quota_project_id"}
$args->{"quota_project_id"} $json->{"quota_project_id"}
Truth table33%1/3
A //= B
A= $args->{"quota_project_id"}B= $json->{"quota_project_id"}
ABresult
1X1
011
000
79 11 ▶ $args->{project_id} //= $json->{project_id};
Condition66%2/3
$args->{"project_id"} //= $json->{"project_id"}
l!l&&r!l&&!r
047
MC/DC50%1/2
$args->{"project_id"} //= $json->{"project_id"}
$args->{"project_id"} $json->{"project_id"}
Truth table66%2/3
A //= B
A= $args->{"project_id"}B= $json->{"project_id"}
ABresult
1X1
011
000
80 }
81  
82 72 ▶ $args->{project_id} //= $ENV{GOOGLE_CLOUD_PROJECT};
Condition66%2/3
$args->{"project_id"} //= $ENV{"GOOGLE_CLOUD_PROJECT"}
l!l&&r!l&&!r
6066
MC/DC50%1/2
$args->{"project_id"} //= $ENV{"GOOGLE_CLOUD_PROJECT"}
$args->{"project_id"} $ENV{"GOOGLE_CLOUD_PROJECT"}
Truth table66%2/3
A //= B
A= $args->{"project_id"}B= $ENV{"GOOGLE_CLOUD_PROJECT"}
ABresult
1X1
011
000
83  
84 72 return $class->$orig($args);
85 15 };
86  
87 0 ▶sub fetch_access_token {
Subroutine fetch_access_token: not called CC=1 SCAR=6.9
Pod undocumented
88 0 my ($self, %options) = @_;
89 0 Google::Auth::Error->throw(
90 'fetch_access_token must be implemented by subclasses');
91 }
92  
93 2 ▶sub is_expired {
Subroutine is_expired: called CC=8 SCAR=39.4
Pod undocumented
94 2 my ($self, %options) = @_;
95 2 my $expires = $self->expires_at;
96 2 ▶ return 1 if !defined $expires;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $expires 2 0 2
97  
98 0 ▶ my $skew = $options{clock_skew} // $self->clock_skew;
Condition0%0/3
$options{"clock_skew"} // $self->clock_skew
l!l&&r!l&&!r
000
MC/DC0%0/2
$options{"clock_skew"} // $self->clock_skew
$options{"clock_skew"} $self->clock_skew
Truth table0%0/3
A // B
A= $options{"clock_skew"}B= $self->clock_skew
ABresult
1X1
011
000
99  
100 0 my $expiry_epoch;
101 0 ▶ if ($expires =~ /^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expires =~ /^\d+$/) { } 0 0 0
102 0 $expiry_epoch = $expires;
103 } else {
104 0 my $tp = eval { Time::Piece->strptime($expires, '%Y-%m-%dT%H:%M:%SZ') };
105 0 ▶ if ($@ || !$tp) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ or not $tp) 0 0 0
Condition0%0/3
$@ or not $tp
l!l&&r!l&&!r
000
MC/DC0%0/2
$@ or not $tp
$@ not $tp
Truth table0%0/3
A or not(B)
A= $@B= not $tp
ABresult
1X1
011
000
106 0 $tp = eval { Time::Piece->strptime($expires, '%Y-%m-%dT%H:%M:%S.%fZ'); };
107 }
108 0 ▶ $expiry_epoch = $tp ? $tp->epoch : time();
Branch: 1 branch
BranchTrueFalseTotal
$tp ? : 0 0 0
109 }
110  
111 0 my $now = time;
112 0 ▶ my $expired = ($now + $skew >= $expiry_epoch) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$now + $skew >= $expiry_epoch ? : 0 0 0
113 0 $log->tracef(
114 'Token expiration check for %s: expires_at=%s (epoch=%s), skew=%d, now=%s, expired=%d',
115 ref $self, $expires, $expiry_epoch, $skew, $now, $expired);
116 0 return $expired;
117 }
118  
119 2 ▶sub get_token {
Subroutine get_token: called CC=10 SCAR=33.1
Pod undocumented
120 2 my ($self, %options) = @_;
121  
122 2 ▶ if ($self->is_refreshing) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_refreshing) 0 2 2
123 0 $log->debugf(
124 'Token refresh is already active in another thread/coroutine for %s. Yielding...',
125 ref $self
126 );
127 0 my $limit = 30;
128 0 ▶ while ($self->is_refreshing && $limit-- > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_refreshing and $limit-- > 0) 0 0 0
Condition0%0/3
$self->is_refreshing and $limit-- > 0
!ll&&!rl&&r
000
MC/DC0%0/2
$self->is_refreshing and $limit-- > 0
$self->is_refreshing $limit-- > 0
Truth table0%0/3
A and B
A= $self->is_refreshingB= $limit-- > 0
ABresult
0X0
100
111
129 0 ▶ if (exists $INC{'Coro.pm'}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $INC{'Coro.pm'}) { } 0 0 0
elsif (exists $INC{'threads.pm'}) { } 0 0 0
130 0 Coro::cede();
131 } elsif (exists $INC{'threads.pm'}) {
132 0 threads->yield();
133 } else {
134 0 Time::HiRes::sleep(0.1);
135 }
136 }
137 0 ▶ $log->debugf('Resume from lock for %s. Access token is: %s',
Branch: 1 branch
BranchTrueFalseTotal
defined $self->access_token ? : 0 0 0
138 ref $self, defined $self->access_token ? 'PRESENT' : 'MISSING');
139 0 return $self->access_token;
140 }
141  
142 2 ▶ if ($self->is_expired(%options) || !defined $self->access_token) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_expired(%options) or not defined $self->access_token) 2 0 2
Condition33%1/3
$self->is_expired(%options) or not defined $self->access_token
l!l&&r!l&&!r
200
MC/DC0%0/2
$self->is_expired(%options) or not defined $self->access_token
$self->is_expired(%options) not defined $self->access_token
Truth table33%1/3
A or not(B)
A= $self->is_expired(%options)B= not defined $self->access_token
ABresult
1X1
011
000
143 2 $log->infof(
144 'Access token is missing or expired for %s. Initiating refresh...',
145 ref $self);
146 2 $self->is_refreshing(1);
147 2 eval { $self->fetch_access_token(%options); };
148 2 my $err = $@;
149 2 $self->is_refreshing(0);
150 2 ▶ if ($err) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err) 1 1 2
151 1 $log->errorf('Failed to fetch access token for %s: %s', ref $self, $err);
152 1 die $err;
153 }
154 1 $log->infof('Successfully refreshed access token for %s.', ref $self);
155 }
156 1 return $self->access_token;
157 }
158  
159 0 ▶sub apply {
Subroutine apply: not called CC=6 SCAR=37.4
Pod undocumented
160 0 my ($self, $req_or_headers, %options) = @_;
161 0 $log->debugf('Applying credentials decoration for %s...', ref $self);
162 0 my $token = $self->get_token(%options);
163  
164 0 ▶ if (ref $req_or_headers eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $req_or_headers eq 'HASH') { } 0 0 0
elsif (eval { do { $req_or_headers->isa('HTTP::Request') } }) { } 0 0 0
165 0 $req_or_headers->{Authorization} = 'Bearer ' . $token;
166 0 ▶ $req_or_headers->{'X-Goog-User-Project'} = $self->quota_project_id
Branch: 1 branch
BranchTrueFalseTotal
if $self->quota_project_id 0 0 0
167 if $self->quota_project_id;
168 0 } elsif (eval { $req_or_headers->isa('HTTP::Request') }) {
169 0 $req_or_headers->header(Authorization => 'Bearer ' . $token);
170 0 ▶ $req_or_headers->header('X-Goog-User-Project' => $self->quota_project_id)
Branch: 1 branch
BranchTrueFalseTotal
if $self->quota_project_id 0 0 0
171 if $self->quota_project_id;
172 } else {
173 0 ▶ $log->errorf(
Condition0%0/3
ref $req_or_headers || $req_or_headers
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $req_or_headers || $req_or_headers
ref $req_or_headers $req_or_headers
Truth table0%0/3
A || B
A= ref $req_or_headersB= $req_or_headers
ABresult
1X1
011
000
174 'Invalid apply target type: %s',
175 ref $req_or_headers || $req_or_headers
176 );
177 0 Google::Auth::Error->throw(
178 'apply expected a HASH reference or HTTP::Request object');
179 }
180 0 $log->debugf('Credentials applied successfully.');
181 }
182  
183 16 ▶sub _validate_url {
Subroutine _validate_url: called CC=12 SCAR=25.6
184 16 my ($self, $url_str, $name) = @_;
185 16 ▶ return unless defined $url_str;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $url_str 0 16 16
186  
187 16 my $uri = URI->new($url_str);
188 16 my $host = $uri->host;
189  
190 16 ▶ unless ($host) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($host) 0 16 16
191 0 Google::Auth::Error->throw("Invalid URL for $name: $url_str");
192 }
193  
194 16 ▶ my $universe_domain = $self->universe_domain // 'googleapis.com';
Condition50%1/2
$self->universe_domain // "googleapis.com"
l!l
160
MC/DC0%0/1
$self->universe_domain // "googleapis.com"
$self->universe_domain
Truth table50%1/2
A
A= $self->universe_domain
Aresult
11
00
195  
196 16 my $is_valid = 0;
197 16 ▶ if ($host eq 'googleapis.com' || $host =~ /\.googleapis.com$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($host eq 'googleapis.com' or $host =~ /\.googleapis.com$/) { } 10 6 16
elsif ($host eq $universe_domain or $host =~ /\.\Q$universe_domain\E$/) { } 3 3 6
Condition66%2/3
$host eq "googleapis.com" or $host =~ /\.googleapis.com$/
l!l&&r!l&&!r
0106
MC/DC50%1/2
$host eq "googleapis.com" or $host =~ /\.googleapis.com$/
$host eq "googleapis.com" $host =~ /\.googleapis.com$/
Truth table66%2/3
A or B
A= $host eq "googleapis.com"B= $host =~ /\.googleapis.com$/
ABresult
1X1
011
000
198 10 $is_valid = 1;
199 ▶ } elsif ($host eq $universe_domain || $host =~ /\.\Q$universe_domain\E$/) {
Condition66%2/3
$host eq $universe_domain or $host =~ /\.\Q$universe_domain\E$/
l!l&&r!l&&!r
303
MC/DC50%1/2
$host eq $universe_domain or $host =~ /\.\Q$universe_domain\E$/
$host eq $universe_domain $host =~ /\.\Q$universe_domain\E$/
Truth table66%2/3
A or B
A= $host eq $universe_domainB= $host =~ /\.\Q$universe_domain\E$/
ABresult
1X1
011
000
200 3 my $is_pinned = $self->_is_universe_pinned;
201  
202 3 ▶ if ($is_pinned) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_pinned) { } 0 3 3
203 0 $is_valid = 1;
204 } else {
205 # Not pinned, came from JSON. Only allow if explicitly enabled via env var.
206 3 ▶ if (($ENV{GOOGLE_EXTERNAL_ACCOUNT_ALLOW_CUSTOM_UNIVERSES} // '0') eq '1')
Branch: 1 branch
BranchTrueFalseTotal
if (($ENV{'GOOGLE_EXTERNAL_ACCOUNT_ALLOW_CUSTOM_UNIVERSES'} // '0') eq '1') { } 1 2 3
Condition100%2/2
$ENV{"GOOGLE_EXTERNAL_ACCOUNT_ALLOW_CUSTOM_UNIVERSES"} // 0
l!l
12
MC/DC100%1/1
$ENV{"GOOGLE_EXTERNAL_ACCOUNT_ALLOW_CUSTOM_UNIVERSES"} // 0
$ENV{"GOOGLE_EXTERNAL_ACCOUNT_ALLOW_CUSTOM_UNIVERSES"}
Truth table100%2/2
A
A= $ENV{"GOOGLE_EXTERNAL_ACCOUNT_ALLOW_CUSTOM_UNIVERSES"}
Aresult
11
00
207 {
208 1 $is_valid = 1;
209 } else {
210 2 $log->warnf(
211 "Custom universe_domain %s detected in JSON but not enabled via GOOGLE_EXTERNAL_ACCOUNT_ALLOW_CUSTOM_UNIVERSES=1",
212 $universe_domain
213 );
214 }
215 }
216 }
217  
218 16 ▶ unless ($is_valid) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($is_valid) 5 11 16
219 5 $log->errorf("URL %s (%s) does not match expected domain (%s)",
220 $name, $url_str, $universe_domain);
221 5 Google::Auth::Error->throw(
222 "URL $name ($url_str) carries security violation: domain does not match $universe_domain or googleapis.com"
223 );
224 }
225 }
226  
227 15 1;
« blib/lib/Google/Auth/ExternalAccountCredentials/Aws.pm ↑ Summary blib/lib/Google/Auth/DefaultCredentials.pm »