« blib/script/gcloud-auth.pl
↑ Summary
blib/lib/Google/Auth/ExternalAccountCredentials/Pluggable.pm »
| 1 | package Google::Auth::UserRefreshCredentials; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 2 | extends 'Google::Auth::Credentials'; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use JSON::PP; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use LWP::UserAgent; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use Google::Auth; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 2 | ▶ | use Google::Auth::Exceptions; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 2 | ▶ | use Google::Auth::RetryHelper; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 2 | ▶ | use Log::Any qw($log); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 2 | has json_key => ( | |||||||||||||||||||||||||||
| 17 | is => 'ro', | ||||||||||||||||||||||||||||
| 18 | required => 0, | ||||||||||||||||||||||||||||
| 19 | ); | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 2 | has client_id => ( | |||||||||||||||||||||||||||
| 22 | is => 'ro', | ||||||||||||||||||||||||||||
| 23 | required => 0, | ||||||||||||||||||||||||||||
| 24 | ); | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 2 | has client_secret => ( | |||||||||||||||||||||||||||
| 27 | is => 'ro', | ||||||||||||||||||||||||||||
| 28 | required => 0, | ||||||||||||||||||||||||||||
| 29 | ); | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 2 | has refresh_token => ( | |||||||||||||||||||||||||||
| 32 | is => 'rw', | ||||||||||||||||||||||||||||
| 33 | required => 0, | ||||||||||||||||||||||||||||
| 34 | ); | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 2 | has code => ( | |||||||||||||||||||||||||||
| 37 | is => 'rw', | ||||||||||||||||||||||||||||
| 38 | required => 0, | ||||||||||||||||||||||||||||
| 39 | ); | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 2 | has redirect_uri => ( | |||||||||||||||||||||||||||
| 42 | is => 'ro', | ||||||||||||||||||||||||||||
| 43 | required => 0, | ||||||||||||||||||||||||||||
| 44 | ); | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | has token_uri => ( | ||||||||||||||||||||||||||||
| 47 | is => 'ro', | ||||||||||||||||||||||||||||
| 48 | required => 0, | ||||||||||||||||||||||||||||
| 49 | 4 | ▶ | default => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
| |||||||||||||||||||||||||||||
| 50 | 4 | ▶ | $ENV{GOOGLE_AUTH_TOKEN_URI} || 'https://oauth2.googleapis.com/token'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | }, | ||||||||||||||||||||||||||||
| 52 | 2 | ); | |||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 2 | has code_verifier => ( | |||||||||||||||||||||||||||
| 55 | is => 'ro', | ||||||||||||||||||||||||||||
| 56 | required => 0, | ||||||||||||||||||||||||||||
| 57 | ); | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 2 | has scope => ( | |||||||||||||||||||||||||||
| 60 | is => 'ro', | ||||||||||||||||||||||||||||
| 61 | required => 0, | ||||||||||||||||||||||||||||
| 62 | ); | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | has ua => ( | ||||||||||||||||||||||||||||
| 65 | is => 'ro', | ||||||||||||||||||||||||||||
| 66 | 4 | ▶ | default => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 67 | 4 | my $ua = LWP::UserAgent->new(timeout => 10); | |||||||||||||||||||||||||||
| 68 | 4 | $ua->env_proxy; | |||||||||||||||||||||||||||
| 69 | 4 | return $ua; | |||||||||||||||||||||||||||
| 70 | }, | ||||||||||||||||||||||||||||
| 71 | 2 | ); | |||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 4 | ▶ | around BUILDARGS => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=7 SCAR=34.4
| |||||||||||||||||||||||||||||
| 74 | 4 | my ($orig, $class, @args) = @_; | |||||||||||||||||||||||||||
| 75 | 4 | my $args = $class->$orig(@args); | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 4 | ▶ | if (my $json = $args->{json_key}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | 0 | ▶ | $args->{client_id} //= $json->{client_id}; | ||||||||||||||||||||||||||
|
Condition0%0/3
$args->{"client_id"} //= $json->{"client_id"}
MC/DC0%0/2
$args->{"client_id"} //= $json->{"client_id"}
$args->{"client_id"}
$json->{"client_id"}
Truth table0%0/3
A //= B
A= $args->{"client_id"}B= $json->{"client_id"}
| |||||||||||||||||||||||||||||
| 79 | 0 | ▶ | $args->{client_secret} //= $json->{client_secret}; | ||||||||||||||||||||||||||
|
Condition0%0/3
$args->{"client_secret"} //= $json->{"client_secret"}
MC/DC0%0/2
$args->{"client_secret"} //= $json->{"client_secret"}
$args->{"client_secret"}
$json->{"client_secret"}
Truth table0%0/3
A //= B
A= $args->{"client_secret"}B= $json->{"client_secret"}
| |||||||||||||||||||||||||||||
| 80 | 0 | ▶ | $args->{refresh_token} //= $json->{refresh_token}; | ||||||||||||||||||||||||||
|
Condition0%0/3
$args->{"refresh_token"} //= $json->{"refresh_token"}
MC/DC0%0/2
$args->{"refresh_token"} //= $json->{"refresh_token"}
$args->{"refresh_token"}
$json->{"refresh_token"}
Truth table0%0/3
A //= B
A= $args->{"refresh_token"}B= $json->{"refresh_token"}
| |||||||||||||||||||||||||||||
| 81 | 0 | ▶ | $args->{token_uri} //= $json->{token_uri} if defined $json->{token_uri}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$args->{"token_uri"} //= $json->{"token_uri"}
MC/DC0%0/2
$args->{"token_uri"} //= $json->{"token_uri"}
$args->{"token_uri"}
$json->{"token_uri"}
Truth table0%0/3
A //= B
A= $args->{"token_uri"}B= $json->{"token_uri"}
| |||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 4 | return $args; | |||||||||||||||||||||||||||
| 85 | 2 | }; | |||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 0 | ▶ | sub fetch_access_token { | ||||||||||||||||||||||||||
|
Subroutine
fetch_access_token: not called CC=9 SCAR=45.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 88 | 0 | my ($self, %options) = @_; | |||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 0 | my $client_id = $self->client_id; | |||||||||||||||||||||||||||
| 91 | 0 | my $client_secret = $self->client_secret; | |||||||||||||||||||||||||||
| 92 | 0 | my $refresh_token = $self->refresh_token; | |||||||||||||||||||||||||||
| 93 | 0 | my $token_uri = $self->token_uri; | |||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 0 | $self->_validate_url($token_uri, 'token_uri'); | |||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 0 | ▶ | if (!defined $client_id || !defined $client_secret) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $client_id and defined $client_secret
MC/DC0%0/2
defined $client_id and defined $client_secret
defined $client_id
defined $client_secret
Truth table0%0/3
A and B
A= defined $client_idB= defined $client_secret
| |||||||||||||||||||||||||||||
| 98 | 0 | $log->errorf( | |||||||||||||||||||||||||||
| 99 | 'Missing client_id or client_secret for UserRefreshCredentials token exchange' | ||||||||||||||||||||||||||||
| 100 | ); | ||||||||||||||||||||||||||||
| 101 | 0 | Google::Auth::Error->throw( | |||||||||||||||||||||||||||
| 102 | 'Missing client_id or client_secret to fetch token'); | ||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 0 | my $post_body; | |||||||||||||||||||||||||||
| 106 | 0 | ▶ | if ($self->code) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 107 | 0 | ▶ | if (!defined $self->redirect_uri) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | 0 | Google::Auth::Error->throw( | |||||||||||||||||||||||||||
| 109 | 'Missing redirect_uri for authorization_code grant'); | ||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | $post_body = { | ||||||||||||||||||||||||||||
| 112 | 0 | 'grant_type' => 'authorization_code', | |||||||||||||||||||||||||||
| 113 | 'client_id' => $client_id, | ||||||||||||||||||||||||||||
| 114 | 'client_secret' => $client_secret, | ||||||||||||||||||||||||||||
| 115 | 'code' => $self->code, | ||||||||||||||||||||||||||||
| 116 | 'redirect_uri' => $self->redirect_uri, | ||||||||||||||||||||||||||||
| 117 | }; | ||||||||||||||||||||||||||||
| 118 | 0 | ▶ | $post_body->{code_verifier} = $self->code_verifier | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | if $self->code_verifier; | ||||||||||||||||||||||||||||
| 120 | } elsif ($refresh_token) { | ||||||||||||||||||||||||||||
| 121 | 0 | $post_body = { | |||||||||||||||||||||||||||
| 122 | 'grant_type' => 'refresh_token', | ||||||||||||||||||||||||||||
| 123 | 'client_id' => $client_id, | ||||||||||||||||||||||||||||
| 124 | 'client_secret' => $client_secret, | ||||||||||||||||||||||||||||
| 125 | 'refresh_token' => $refresh_token, | ||||||||||||||||||||||||||||
| 126 | }; | ||||||||||||||||||||||||||||
| 127 | } else { | ||||||||||||||||||||||||||||
| 128 | 0 | Google::Auth::Error->throw('Missing refresh_token or code to fetch token'); | |||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 0 | my $ua = $self->ua; | |||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 0 | require URI; | |||||||||||||||||||||||||||
| 134 | 0 | my $u = URI->new('http:'); | |||||||||||||||||||||||||||
| 135 | 0 | $u->query_form(%$post_body); | |||||||||||||||||||||||||||
| 136 | 0 | my $encoded_body = $u->query; | |||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | my $response_body = Google::Auth::RetryHelper->execute_with_retry( | ||||||||||||||||||||||||||||
| 139 | 0 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=9 SCAR=45.0
| |||||||||||||||||||||||||||||
| 140 | 0 | my $future = $self->transport->request( | |||||||||||||||||||||||||||
| 141 | method => 'POST', | ||||||||||||||||||||||||||||
| 142 | url => $token_uri, | ||||||||||||||||||||||||||||
| 143 | headers => {'Content-Type' => 'application/x-www-form-urlencoded'}, | ||||||||||||||||||||||||||||
| 144 | body => $encoded_body, | ||||||||||||||||||||||||||||
| 145 | ); | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 0 | my ($body, $headers); | |||||||||||||||||||||||||||
| 148 | 0 | my $future_res = eval { ($body, $headers) = $future->get(); 1 }; | |||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 0 | ▶ | if (!$future_res) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 151 | 0 | my ($err_msg, $cat, $details) = $future->failure; | |||||||||||||||||||||||||||
| 152 | 0 | my $code = 0; | |||||||||||||||||||||||||||
| 153 | 0 | my $error_body = ''; | |||||||||||||||||||||||||||
| 154 | 0 | ▶ | if (ref($details) eq 'HASH') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 155 | 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"}
| |||||||||||||||||||||||||||||
| 156 | 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"}
| |||||||||||||||||||||||||||||
| 157 | 0 | } elsif (eval { $details->can('code') }) { | |||||||||||||||||||||||||||
| 158 | 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
| |||||||||||||||||||||||||||||
| 159 | 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
| |||||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | 0 | $log->warnf('Token request failed at %s: status %s', $token_uri, $code); | |||||||||||||||||||||||||||
| 162 | 0 | Google::Auth::Error->throw( | |||||||||||||||||||||||||||
| 163 | 'HTTP request failed with status ' . $code . ': ' . $error_body); | ||||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | 0 | return $body; | |||||||||||||||||||||||||||
| 166 | }, | ||||||||||||||||||||||||||||
| 167 | 0 | %options | |||||||||||||||||||||||||||
| 168 | ); | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | 0 | my $res_data = decode_json($response_body); | |||||||||||||||||||||||||||
| 171 | 0 | my $token = $res_data->{access_token}; | |||||||||||||||||||||||||||
| 172 | 0 | ▶ | my $expires = $res_data->{expires_in} // 3600; | ||||||||||||||||||||||||||
|
Condition0%0/2
$res_data->{"expires_in"} // 3600
MC/DC0%0/1
$res_data->{"expires_in"} // 3600
$res_data->{"expires_in"}
Truth table0%0/2
A
A= $res_data->{"expires_in"}
| |||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | 0 | $self->access_token($token); | |||||||||||||||||||||||||||
| 175 | 0 | $self->expires_at(time() + $expires); | |||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 0 | ▶ | if ($res_data->{refresh_token}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 178 | 0 | $self->refresh_token($res_data->{refresh_token}); | |||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | 0 | return $token; | |||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | 2 | 1; | |||||||||||||||||||||||||||