« blib/lib/Google/Auth/IDTokens/Verifier.pm
↑ Summary
blib/lib/Google/Auth/ExternalAccountCredentials.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::ImpersonatedServiceAccountCredentials; | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 1 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | 1 | extends 'Google::Auth::Credentials'; | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 1 | ▶ | use JSON::PP; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | 1 | ▶ | use LWP::UserAgent; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | 1 | ▶ | use Google::Auth::Exceptions; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 26 | 1 | ▶ | use Google::Auth::RetryHelper; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | 1 | ▶ | use Log::Any qw($log); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 1 | has base_credentials => ( | |||||||||||||||||||||||||||
| 30 | is => 'ro', | ||||||||||||||||||||||||||||
| 31 | required => 0, | ||||||||||||||||||||||||||||
| 32 | ); | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 1 | has source_credentials => ( | |||||||||||||||||||||||||||
| 35 | is => 'ro', | ||||||||||||||||||||||||||||
| 36 | required => 0, | ||||||||||||||||||||||||||||
| 37 | ); | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 1 | has impersonation_url => ( | |||||||||||||||||||||||||||
| 40 | is => 'ro', | ||||||||||||||||||||||||||||
| 41 | required => 1, | ||||||||||||||||||||||||||||
| 42 | ); | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 1 | has scope => ( | |||||||||||||||||||||||||||
| 45 | is => 'ro', | ||||||||||||||||||||||||||||
| 46 | required => 1, | ||||||||||||||||||||||||||||
| 47 | ); | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | has ua => ( | ||||||||||||||||||||||||||||
| 50 | is => 'ro', | ||||||||||||||||||||||||||||
| 51 | 0 | ▶ | default => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 52 | 0 | my $ua = LWP::UserAgent->new(timeout => 10); | |||||||||||||||||||||||||||
| 53 | 0 | $ua->env_proxy; | |||||||||||||||||||||||||||
| 54 | 0 | return $ua; | |||||||||||||||||||||||||||
| 55 | }, | ||||||||||||||||||||||||||||
| 56 | 1 | ); | |||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 1 | ▶ | around BUILDARGS => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=10 SCAR=38.9
| |||||||||||||||||||||||||||||
| 59 | 1 | my ($orig, $class, @args) = @_; | |||||||||||||||||||||||||||
| 60 | 1 | my $args = $class->$orig(@args); | |||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 1 | ▶ | if (my $json = $args->{json_key}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | 0 | ▶ | $args->{impersonation_url} //= $json->{service_account_impersonation_url}; | ||||||||||||||||||||||||||
|
Condition0%0/3
$args->{"impersonation_url"} //= $json->{"service_account_impersonation_url"}
MC/DC0%0/2
$args->{"impersonation_url"} //= $json->{"service_account_impersonation_url"}
$args->{"impersonation_url"}
$json->{"service_account_impersonation_url"}
Truth table0%0/3
A //= B
A= $args->{"impersonation_url"}B= $json->{"service_account_impersonation_url"}
| |||||||||||||||||||||||||||||
| 64 | 0 | ▶ | $args->{scope} //= $json->{scopes}; | ||||||||||||||||||||||||||
|
Condition0%0/3
$args->{"scope"} //= $json->{"scopes"}
MC/DC0%0/2
$args->{"scope"} //= $json->{"scopes"}
$args->{"scope"}
$json->{"scopes"}
Truth table0%0/3
A //= B
A= $args->{"scope"}B= $json->{"scopes"}
| |||||||||||||||||||||||||||||
| 65 | 0 | ▶ | if (my $source_creds_info = $json->{source_credentials}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 0 | ▶ | if ($source_creds_info->{type} eq 'impersonated_service_account') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | 0 | Google::Auth::Error->throw( | |||||||||||||||||||||||||||
| 68 | 'Source credentials can\'t be of type impersonated_service_account, use delegates to chain impersonation.' | ||||||||||||||||||||||||||||
| 69 | ); | ||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | 0 | require Google::Auth::DefaultCredentials; | |||||||||||||||||||||||||||
| 72 | $args->{source_credentials} //= | ||||||||||||||||||||||||||||
| 73 | 0 | ▶ | Google::Auth::DefaultCredentials->make_creds( | ||||||||||||||||||||||||||
|
Condition0%0/3
$args->{"source_credentials"} //= "Google::Auth::DefaultCredentials"->make_creds("json_key", $source_creds_info)
MC/DC0%0/2
$args->{"source_credentials"} //= "Google::Auth::DefaultCredentials"->make_creds("json_key", $source_creds_info)
$args->{"source_credentials"}
"Google::Auth::DefaultCredentials"->make_creds("json_key", $source_creds_info)
Truth table0%0/3
A //= B
A= $args->{"source_credentials"}B= "Google::Auth::DefaultCredentials"->make_creds("json_key", $source_creds_info)
| |||||||||||||||||||||||||||||
| 74 | json_key => $source_creds_info,); | ||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 1 | ▶ | if ( !defined $args->{source_credentials} | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $args->{"source_credentials"} or defined $args->{"base_credentials"}
MC/DC0%0/2
defined $args->{"source_credentials"} or defined $args->{"base_credentials"}
defined $args->{"source_credentials"}
defined $args->{"base_credentials"}
Truth table33%1/3
A or B
A= defined $args->{"source_credentials"}B= defined $args->{"base_credentials"}
| |||||||||||||||||||||||||||||
| 79 | && !defined $args->{base_credentials}) | ||||||||||||||||||||||||||||
| 80 | { | ||||||||||||||||||||||||||||
| 81 | 0 | Google::Auth::Error->throw( | |||||||||||||||||||||||||||
| 82 | 'Missing required option: either source_credentials or base_credentials must be provided' | ||||||||||||||||||||||||||||
| 83 | ); | ||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 1 | ▶ | $args->{source_credentials} //= $args->{base_credentials}; | ||||||||||||||||||||||||||
|
Condition33%1/3
$args->{"source_credentials"} //= $args->{"base_credentials"}
MC/DC0%0/2
$args->{"source_credentials"} //= $args->{"base_credentials"}
$args->{"source_credentials"}
$args->{"base_credentials"}
Truth table33%1/3
A //= B
A= $args->{"source_credentials"}B= $args->{"base_credentials"}
| |||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 1 | return $args; | |||||||||||||||||||||||||||
| 89 | 1 | }; | |||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 1 | ▶ | sub BUILD { | ||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=2 SCAR=7.6
Pod
undocumented
| |||||||||||||||||||||||||||||
| 92 | 1 | my ($self) = @_; | |||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 1 | my $source_creds = $self->source_credentials; | |||||||||||||||||||||||||||
| 95 | 1 | ▶ | if ($source_creds->isa('Google::Auth::ImpersonatedServiceAccountCredentials')) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | { | ||||||||||||||||||||||||||||
| 97 | 0 | Google::Auth::Error->throw( | |||||||||||||||||||||||||||
| 98 | 'Source credentials can\'t be of type impersonated_service_account, use delegates to chain impersonation.' | ||||||||||||||||||||||||||||
| 99 | ); | ||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 1 | ▶ | sub fetch_access_token { | ||||||||||||||||||||||||||
|
Subroutine
fetch_access_token: called CC=6 SCAR=30.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 104 | 1 | my ($self, %options) = @_; | |||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 1 | $self->_validate_url($self->impersonation_url, 'impersonation_url'); | |||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 1 | my $source_creds = $self->source_credentials; | |||||||||||||||||||||||||||
| 109 | 1 | my $source_token; | |||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 1 | ▶ | if ($source_creds->can('get_token')) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 112 | 0 | $log->tracef('Fetching source credentials token using get_token...'); | |||||||||||||||||||||||||||
| 113 | 0 | $source_token = $source_creds->get_token(%options); | |||||||||||||||||||||||||||
| 114 | ▶ | } elsif ($source_creds->can('access_token') | |||||||||||||||||||||||||||
|
Condition33%1/3
$source_creds->can("access_token") and defined $source_creds->access_token
MC/DC0%0/2
$source_creds->can("access_token") and defined $source_creds->access_token
$source_creds->can("access_token")
defined $source_creds->access_token
Truth table33%1/3
A and B
A= $source_creds->can("access_token")B= defined $source_creds->access_token
| |||||||||||||||||||||||||||||
| 115 | && defined $source_creds->access_token) | ||||||||||||||||||||||||||||
| 116 | { | ||||||||||||||||||||||||||||
| 117 | 1 | $log->tracef('Using cached access_token from source credentials...'); | |||||||||||||||||||||||||||
| 118 | 1 | $source_token = $source_creds->access_token; | |||||||||||||||||||||||||||
| 119 | } elsif ($source_creds->can('fetch_access_token')) { | ||||||||||||||||||||||||||||
| 120 | 0 | $log->tracef( | |||||||||||||||||||||||||||
| 121 | 'Fetching source credentials token using fetch_access_token...'); | ||||||||||||||||||||||||||||
| 122 | 0 | $source_token = $source_creds->fetch_access_token(%options); | |||||||||||||||||||||||||||
| 123 | } else { | ||||||||||||||||||||||||||||
| 124 | 0 | $log->errorf( | |||||||||||||||||||||||||||
| 125 | 'Source credentials do not contain a valid token retrieval mechanism'); | ||||||||||||||||||||||||||||
| 126 | 0 | Google::Auth::Error->throw( | |||||||||||||||||||||||||||
| 127 | 'Source credentials do not have a valid access token or fetch_access_token method' | ||||||||||||||||||||||||||||
| 128 | ); | ||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 1 | ▶ | my $req_body = encode_json({ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | scope => ref $self->scope eq 'ARRAY' ? $self->scope : [$self->scope]}); | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 1 | my $ua = $self->ua; | |||||||||||||||||||||||||||
| 135 | 1 | $log->infof('Requesting impersonated access token from %s...', | |||||||||||||||||||||||||||
| 136 | $self->impersonation_url); | ||||||||||||||||||||||||||||
| 137 | my $response_body = Google::Auth::RetryHelper->execute_with_retry( | ||||||||||||||||||||||||||||
| 138 | 3 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=9 SCAR=44.2
| |||||||||||||||||||||||||||||
| 139 | 3 | my $future = $self->transport->request( | |||||||||||||||||||||||||||
| 140 | method => 'POST', | ||||||||||||||||||||||||||||
| 141 | url => $self->impersonation_url, | ||||||||||||||||||||||||||||
| 142 | headers => { | ||||||||||||||||||||||||||||
| 143 | 'Content-Type' => 'application/json', | ||||||||||||||||||||||||||||
| 144 | 'Authorization' => 'Bearer ' . $source_token, | ||||||||||||||||||||||||||||
| 145 | }, | ||||||||||||||||||||||||||||
| 146 | body => $req_body, | ||||||||||||||||||||||||||||
| 147 | ); | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 0 | my ($body, $headers); | |||||||||||||||||||||||||||
| 150 | 0 | my $future_res = eval { ($body, $headers) = $future->get(); 1 }; | |||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | 0 | ▶ | if (!$future_res) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 153 | 0 | my ($err_msg, $cat, $details) = $future->failure; | |||||||||||||||||||||||||||
| 154 | 0 | my $code = 0; | |||||||||||||||||||||||||||
| 155 | 0 | my $error_body = ''; | |||||||||||||||||||||||||||
| 156 | 0 | ▶ | if (ref($details) eq 'HASH') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 157 | 0 | ▶ | $code = $details->{code} // 0; | ||||||||||||||||||||||||||
|
Condition0%0/2
$details->{"code"} // 0
MC/DC0%0/1
$details->{"code"} // 0
$details->{"code"}
Truth table0%0/2
A
A= $details->{"code"}
| |||||||||||||||||||||||||||||
| 158 | 0 | ▶ | $error_body = $details->{body} // ''; | ||||||||||||||||||||||||||
|
Condition0%0/2
$details->{"body"} // ""
MC/DC0%0/1
$details->{"body"} // ""
$details->{"body"}
Truth table0%0/2
A
A= $details->{"body"}
| |||||||||||||||||||||||||||||
| 159 | 0 | } elsif (eval { $details->can('code') }) { | |||||||||||||||||||||||||||
| 160 | 0 | ▶ | $code = $details->code // 0; | ||||||||||||||||||||||||||
|
Condition0%0/2
$details->code // 0
MC/DC0%0/1
$details->code // 0
$details->code
Truth table0%0/2
A
A= $details->code
| |||||||||||||||||||||||||||||
| 161 | 0 | ▶ | $error_body = $details->decoded_content // $details->content // ''; | ||||||||||||||||||||||||||
|
Condition0%0/5
($details->decoded_content // $details->content) // ""
$details->decoded_content // $details->content
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
| |||||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||
| 163 | 0 | $log->warnf('Impersonated token exchange failed: status %s', $code); | |||||||||||||||||||||||||||
| 164 | 0 | Google::Auth::Error->throw( | |||||||||||||||||||||||||||
| 165 | 'Service account impersonation failed with status ' . | ||||||||||||||||||||||||||||
| 166 | $code . ': ' . | ||||||||||||||||||||||||||||
| 167 | $error_body); | ||||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | 0 | return $body; | |||||||||||||||||||||||||||
| 170 | }, | ||||||||||||||||||||||||||||
| 171 | 1 | %options | |||||||||||||||||||||||||||
| 172 | ); | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | 0 | my $res_data = decode_json($response_body); | |||||||||||||||||||||||||||
| 175 | 0 | $self->access_token($res_data->{accessToken}); | |||||||||||||||||||||||||||
| 176 | 0 | $self->expires_at(_parse_rfc3339_epoch($res_data->{expireTime})); | |||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | 0 | return $res_data->{accessToken}; | |||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | 0 | ▶ | sub _parse_rfc3339_epoch { | ||||||||||||||||||||||||||
|
Subroutine
_parse_rfc3339_epoch: not called CC=8 SCAR=42.8
| |||||||||||||||||||||||||||||
| 182 | 0 | my ($ts) = @_; | |||||||||||||||||||||||||||
| 183 | 0 | ▶ | return unless defined $ts; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 184 | 0 | ▶ | return $ts + 0 if !ref($ts) && $ts =~ /\A\d+(?:\.\d+)?\z/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not ref $ts and $ts =~ /\A\d+(?:\.\d+)?\z/
MC/DC0%0/2
not ref $ts and $ts =~ /\A\d+(?:\.\d+)?\z/
not ref $ts
$ts =~ /\A\d+(?:\.\d+)?\z/
Truth table0%0/3
not(A) and B
A= not ref $tsB= $ts =~ /\A\d+(?:\.\d+)?\z/
| |||||||||||||||||||||||||||||
| 185 | 0 | ▶ | if ($ts =~ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 186 | /\A(\d{4})-(\d{2})-(\d{2})[Tt ](\d{2}):(\d{2}):(\d{2})(?:\.\d+)?([Zz]|[+-]\d{2}:\d{2})\z/ | ||||||||||||||||||||||||||||
| 187 | ) | ||||||||||||||||||||||||||||
| 188 | { | ||||||||||||||||||||||||||||
| 189 | 0 | my ($y, $mon, $d, $h, $min, $s, $tz) = ($1, $2, $3, $4, $5, $6, $7); | |||||||||||||||||||||||||||
| 190 | 0 | require Time::Local; | |||||||||||||||||||||||||||
| 191 | 0 | my $epoch = Time::Local::timegm($s, $min, $h, $d, $mon - 1, $y); | |||||||||||||||||||||||||||
| 192 | 0 | ▶ | if (uc($tz) ne 'Z' && $tz =~ /\A([+-])(\d{2}):(\d{2})\z/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
uc $tz ne "Z" and $tz =~ /\A([+-])(\d{2}):(\d{2})\z/
MC/DC0%0/2
uc $tz ne "Z" and $tz =~ /\A([+-])(\d{2}):(\d{2})\z/
uc $tz ne "Z"
$tz =~ /\A([+-])(\d{2}):(\d{2})\z/
Truth table0%0/3
A and B
A= uc $tz ne "Z"B= $tz =~ /\A([+-])(\d{2}):(\d{2})\z/
| |||||||||||||||||||||||||||||
| 193 | 0 | my $offset = ($2 * 3600) + ($3 * 60); | |||||||||||||||||||||||||||
| 194 | 0 | ▶ | $epoch += ($1 eq '+') ? -$offset : $offset; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||
| 196 | 0 | return $epoch; | |||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||
| 198 | 0 | Google::Auth::Error->throw('Invalid RFC 3339 expireTime: ' . $ts); | |||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | 1 | 1; | |||||||||||||||||||||||||||