bin/gcloud-auth.pl

filter: Statementstmt 83.0% 269 / 324 Branchbran 55.5% 60 / 108 Conditioncond 48.7% 20 / 41 MC/DCmcdc 17.3% 4 / 23 Subroutinesub 100% 25 / 25 Podpod n/a totaltotal 72.5% 378 / 521 CC 69 SCAR 27.7
CC
69
Cov
74%
do_adc_login
33.7
do_login
23.3
run
16.6
CRAP 15.9
« blib/lib/Google/Auth/DefaultCredentials.pm ↑ Summary blib/lib/Google/Auth/UserAuthorizer.pm »
1 #!/usr/bin/perl
2 # Copyright 2026 Google LLC and contributors
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15  
16 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 4 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 4 ▶use Pod::Usage;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 4 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 4 ▶use JSON::MaybeXS qw(encode_json decode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
22 4 ▶use Log::Any qw($log);
Subroutine BEGIN: called CC=1 SCAR=0.0
23 4 ▶use IO::Socket::INET;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 4 ▶use IO::Select;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 4 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 4 ▶use MIME::Base64 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 4 ▶use Google::Auth;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 4 ▶use Google::Auth::ClientId;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 4 ▶use Google::Auth::UserAuthorizer;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 4 ▶use Google::Auth::Stores::FileTokenStore;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 4 ▶use Google::Auth::Exceptions;
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 4 my $VERSION = '0.01';
35  
36 # Default Scopes for GCP
37 4 my @DEFAULT_SCOPES = (
38 'openid',
39 'https://www.googleapis.com/auth/userinfo.email',
40 'https://www.googleapis.com/auth/cloud-platform',
41 'https://www.googleapis.com/auth/appengine.admin',
42 'https://www.googleapis.com/auth/sqlservice.login',
43 'https://www.googleapis.com/auth/compute',
44 'https://www.googleapis.com/auth/accounts.reauth'
45 );
46  
47 4 ▶unless (caller) {
Branch: 1 branch
BranchTrueFalseTotal
unless (caller) 0 4 4
48 0 run(@ARGV);
49 }
50  
51 16 ▶sub run {
Subroutine run: called CC=13 SCAR=28.1
52 16 my @args = @_;
53  
54 16 my $help = 0;
55 16 my $man = 0;
56 16 my $verbose = 0;
57 16 my $client_id_file;
58 my $store_dir =
59 16 ▶ File::Spec->catdir($ENV{HOME} // '.', '.google-auth-perl', 'tokens');
Condition50%1/2
$ENV{"HOME"} // "."
l!l
160
MC/DC0%0/1
$ENV{"HOME"} // "."
$ENV{"HOME"}
Truth table50%1/2
A
A= $ENV{"HOME"}
Aresult
11
00
60 16 my $port = 0; # Default auto-assign
61 16 my $timeout = 300; # Default 5 minutes for human interaction
62 16 my $no_browser = 0;
63  
64 16 local @ARGV = @args;
65  
66 16 ▶ GetOptions(
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions("help|?", \$help, "man", \$man, "verbose|v", \$verbose, "client-id-file=s", \$client_id_file, "store-dir=s", \$store_dir, "port=i", \$port, "timeout=i", \$timeout, "no-browser", \$no_browser) or pod2usage(2) 16 0 16
67 'help|?' => \$help,
68 'man' => \$man,
69 'verbose|v' => \$verbose,
70 'client-id-file=s' => \$client_id_file,
71 'store-dir=s' => \$store_dir,
72 'port=i' => \$port,
73 'timeout=i' => \$timeout,
74 'no-browser' => \$no_browser,
75 ) or pod2usage(2);
76  
77 16 ▶ if ($verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose) 0 16 16
78 0 require Log::Any::Adapter;
79 0 Log::Any::Adapter->set('Stderr', min_level => 'trace');
80 }
81  
82 16 ▶ pod2usage(1) if $help;
Branch: 1 branch
BranchTrueFalseTotal
if $help 0 16 16
83 16 ▶ pod2usage(-verbose => 2) if $man;
Branch: 1 branch
BranchTrueFalseTotal
if $man 0 16 16
84  
85 16 my $command = shift @ARGV;
86  
87 16 ▶ unless ($command) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($command) 0 16 16
88 0 pod2usage(1);
89 }
90  
91 16 ▶ if ($command eq 'login') {
Branch: 4 branches
BranchTrueFalseTotal
if ($command eq 'login') { } 7 9 16
elsif ($command eq 'application-default') { } 5 4 9
elsif ($command eq 'print-access-token') { } 2 2 4
elsif ($command eq 'revoke') { } 2 0 2
92 7 do_login(
93 client_id_file => $client_id_file,
94 store_dir => $store_dir,
95 port => $port,
96 timeout => $timeout,
97 no_browser => $no_browser,
98 );
99 } elsif ($command eq 'application-default') {
100 5 my $subcommand = shift @ARGV;
101 5 ▶ unless ($subcommand) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($subcommand) 0 5 5
102 0 die "Error: application-default requires a subcommand (e.g., login)\n";
103 }
104 5 ▶ if ($subcommand eq 'login') {
Branch: 1 branch
BranchTrueFalseTotal
if ($subcommand eq 'login') { } 5 0 5
105 5 do_adc_login(
106 client_id_file => $client_id_file,
107 store_dir => $store_dir,
108 port => $port,
109 timeout => $timeout,
110 no_browser => $no_browser,
111 );
112 } else {
113 0 die "Unsupported application-default subcommand: $subcommand\n";
114 }
115 } elsif ($command eq 'print-access-token') {
116 2 do_print_access_token(store_dir => $store_dir,);
117 } elsif ($command eq 'revoke') {
118 2 do_revoke(store_dir => $store_dir,);
119 } else {
120 0 die "Unsupported command: $command\n";
121 }
122 }
123  
124 14 ▶sub _start_listener {
Subroutine _start_listener: called CC=3 SCAR=11.8
125 14 my ($port) = @_;
126 14 ▶ my $listener = IO::Socket::INET->new(
Condition100%2/2
$port || 0
l!l
59
MC/DC100%1/1
$port || 0
$port
Truth table100%2/2
A
A= $port
Aresult
11
00
127 LocalHost => '127.0.0.1',
128 LocalPort => $port || 0, # Auto-assign if 0
129 Proto => 'tcp',
130 Listen => 1,
131 ReuseAddr => 1,
132 );
133 14 ▶ unless ($listener) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($listener) 0 14 14
134 0 $log->errorf('Failed to start loopback listener: %s', $!);
135 0 die "Error: Failed to start loopback listener: $!\n";
136 }
137 14 return $listener;
138 }
139  
140 9 ▶sub _generate_secure_token {
Subroutine _generate_secure_token: called CC=1 SCAR=0.0
141 9 my ($num_bytes) = @_;
142 9 my $raw = Google::Auth::get_secure_random_bytes($num_bytes);
143 9 my $b64 = MIME::Base64::encode_base64($raw, '');
144 9 $b64 =~ tr{+/}{-_};
145 9 $b64 =~ s/=+$//;
146 9 return $b64;
147 }
148  
149 13 ▶sub _wait_for_code {
Subroutine _wait_for_code: called CC=11 SCAR=25.4
150 13 my ($listener, $timeout, $expected_state) = @_;
151 13 ▶ $timeout //= 5; # Fallback default
Condition50%1/2
$timeout //= 5
l!l
130
MC/DC0%0/1
$timeout //= 5
$timeout
Truth table50%1/2
A
A= $timeout
Aresult
11
00
152  
153 13 my $select = IO::Select->new($listener);
154 13 my $client;
155  
156 13 ▶ if ($select->can_read($timeout)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($select->can_read($timeout)) { } 6 7 13
157 6 $client = $listener->accept();
158 } else {
159 7 $log->error('Timeout waiting for authorization code');
160 7 close $listener;
161 7 die "Error: Timeout waiting for authorization code.\n";
162 }
163  
164 6 ▶ unless ($client) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($client) 0 6 6
165 0 $log->errorf('Failed to accept connection: %s', $!);
166 0 close $listener;
167 0 die "Error: Failed to accept connection: $!\n";
168 }
169  
170 6 my $request = '';
171 6 ▶ while (<$client>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $client)) 24 0 24
172 24 $request .= $_;
173 24 ▶ last if $_ =~ /^\r\n$/; # End of headers
Branch: 1 branch
BranchTrueFalseTotal
if $_ =~ /^\r\n$/ 6 18 24
174 }
175  
176 6 $log->tracef('Received request: %s', $request);
177  
178 6 my $code;
179 my $received_state;
180 6 ▶ if ($request =~ /^GET\s+([^\s]+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($request =~ /^GET\s+([^\s]+)/) 6 0 6
181 6 my $uri = URI->new('http://127.0.0.1' . $1);
182 6 my %params = $uri->query_form;
183 6 $code = $params{code};
184 6 $received_state = $params{state};
185 }
186  
187 6 ▶ if (defined $expected_state
Branch: 1 branch
BranchTrueFalseTotal
if (defined $expected_state and !defined($received_state) || $received_state ne $expected_state) 1 5 6
Condition66%4/6
defined $expected_state and !defined($received_state) || $received_state ne $expected_state
!ll&&!rl&&r
051
not defined $received_state or $received_state ne $expected_state
l!l&&r!l&&!r
015
MC/DC33%1/3
defined $expected_state and !defined($received_state) || $received_state ne $expected_state
defined $expected_state not defined $received_state $received_state ne $expected_state
Truth table50%2/4
A and not(B) or C
A= defined $expected_stateB= not defined $received_stateC= $received_state ne $expected_state
ABCresult
0XX0
1000
11X1
1011
188 && (!defined $received_state || $received_state ne $expected_state))
189 {
190 1 $log->error('OAuth state mismatch in loopback callback');
191 1 print $client
192 "HTTP/1.1 400 Bad Request\r\nConnection: close\r\nContent-Type: text/plain\r\n\r\nError: State mismatch.\r\n";
193 1 close $client;
194 1 close $listener;
195 1 die "Error: OAuth state mismatch in callback.\n";
196 }
197  
198 5 ▶ if ($code) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code) { } 5 0 5
199 5 $log->info('Authorization code received successfully');
200 5 print $client
201 "HTTP/1.1 200 OK\r\nConnection: close\r\nContent-Type: text/plain\r\n\r\nSuccess! You can close this window.\r\n";
202 5 close $client;
203 5 close $listener;
204 5 return $code;
205 } else {
206 0 $log->error('Failed to get authorization code from callback');
207 0 print $client
208 "HTTP/1.1 400 Bad Request\r\nConnection: close\r\nContent-Type: text/plain\r\n\r\nError: No code received.\r\n";
209 0 close $client;
210 0 close $listener;
211 0 die "Error: Failed to get authorization code.\n";
212 }
213 }
214  
215 7 ▶sub do_login {
Subroutine do_login: called CC=15 SCAR=31.5
216 7 my (%options) = @_;
217 7 $log->info('Command: login initiated');
218 7 $log->trace('Entering do_login');
219  
220 7 my $client_id_file = $options{client_id_file};
221 7 my $store_dir = $options{store_dir};
222  
223 7 my $client_id;
224 7 ▶ if ($client_id_file && -f $client_id_file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($client_id_file and -f $client_id_file) { } 7 0 7
Condition33%1/3
$client_id_file and -f $client_id_file
!ll&&!rl&&r
007
MC/DC0%0/2
$client_id_file and -f $client_id_file
$client_id_file -f $client_id_file
Truth table33%1/3
A and B
A= $client_id_fileB= -f $client_id_file
ABresult
0X0
100
111
225 7 $client_id = Google::Auth::ClientId->from_file($client_id_file);
226 } else {
227 0 $log->info('Using default Gcloud Client ID');
228 0 $client_id = Google::Auth::ClientId->new(
229 id => '32555940559.apps.googleusercontent.com',
230 secret => 'ZmssLNjJy2998hD4CTg2ejr2',
231 );
232 }
233 7 my $token_store =
234 Google::Auth::Stores::FileTokenStore->new(store_dir => $store_dir);
235  
236 7 my $listener;
237 my $redirect_uri;
238  
239 7 my $use_oob = $options{no_browser};
240 7 ▶ unless ($use_oob || $options{port}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($use_oob or $options{"port"}) 4 3 7
Condition66%2/3
$use_oob or $options{"port"}
l!l&&r!l&&!r
034
MC/DC50%1/2
$use_oob or $options{"port"}
$use_oob $options{"port"}
Truth table66%2/3
A or B
A= $use_oobB= $options{"port"}
ABresult
1X1
011
000
241 4 ▶ if ($^O eq 'linux' && !$ENV{DISPLAY} && !$ENV{WAYLAND_DISPLAY}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq "linux" and not $ENV{"DISPLAY"} and not $ENV{"WAYLAND_DISPLAY"}) 0 4 4
Condition33%2/6
$^O eq "linux" and not $ENV{"DISPLAY"} and not $ENV{"WAYLAND_DISPLAY"}
!ll&&!rl&&r
400
$^O eq "linux" and not $ENV{"DISPLAY"}
!ll&&!rl&&r
040
MC/DC0%0/3
$^O eq "linux" and not $ENV{"DISPLAY"} and not $ENV{"WAYLAND_DISPLAY"}
$^O eq "linux" not $ENV{"DISPLAY"} not $ENV{"WAYLAND_DISPLAY"}
Truth table25%1/4
A and not(B) and not(C)
A= $^O eq "linux"B= not $ENV{"DISPLAY"}C= not $ENV{"WAYLAND_DISPLAY"}
ABCresult
0XX0
10X0
1100
1111
242 0 $log->info(
243 'No DISPLAY detected on Linux, defaulting to out-of-band flow');
244 0 $use_oob = 1;
245 }
246 }
247  
248 7 my $code_verifier;
249 7 ▶ if ($use_oob) {
Branch: 1 branch
BranchTrueFalseTotal
if ($use_oob) { } 0 7 7
250 0 $redirect_uri = 'https://sdk.cloud.google.com/authcode.html';
251 0 $code_verifier = _generate_secure_token(48);
252 } else {
253 7 $listener = _start_listener($options{port});
254 7 my $port = $listener->sockport();
255 7 $log->debugf('Loopback listener started on port %d', $port);
256 7 $redirect_uri = "http://127.0.0.1:$port/";
257 }
258  
259 7 my %auth_args = (
260 client_id => $client_id,
261 scope => \@DEFAULT_SCOPES,
262 token_store => $token_store,
263 callback_uri => $redirect_uri,
264 );
265 7 ▶ $auth_args{code_verifier} = $code_verifier if $code_verifier;
Branch: 1 branch
BranchTrueFalseTotal
if $code_verifier 0 7 7
266  
267 7 my $auth = Google::Auth::UserAuthorizer->new(%auth_args);
268  
269 7 my %auth_options;
270 7 ▶ $auth_options{additional_parameters} = {token_usage => 'remote'}
Branch: 1 branch
BranchTrueFalseTotal
if $use_oob 0 7 7
271 if $use_oob;
272  
273 7 my $state = _generate_secure_token(24);
274 7 $auth_options{state} = $state;
275  
276 7 my $auth_url = $auth->get_authorization_url(%auth_options);
277  
278 7 print "\nGo to the following link in your browser:\n\n $auth_url\n\n";
279  
280 7 my $code;
281 7 ▶ if ($use_oob) {
Branch: 1 branch
BranchTrueFalseTotal
if ($use_oob) { } 0 7 7
282 0 print "Enter authorization code: ";
283 0 $code = <STDIN>;
284 0 ▶ chomp $code if $code;
Branch: 1 branch
BranchTrueFalseTotal
if $code 0 0 0
285 } else {
286 7 $log->info('Waiting for authorization code...');
287 7 $code = _wait_for_code($listener, $options{timeout}, $state);
288 }
289  
290 3 ▶ if ($code) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code) 3 0 3
291 3 $log->info('Exchanging code for credentials...');
292  
293 3 my $user_id = 'default'; # TODO: use email if available
294  
295 3 eval {
296 3 $auth->get_and_store_credentials_from_code(
297 user_id => $user_id,
298 code => $code,
299 base_url => $redirect_uri,
300 );
301 };
302 3 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 3 3
303 0 $log->errorf('Failed to exchange code: %s', $@);
304 0 die "Error: Failed to exchange code: $@\n";
305 }
306  
307 3 $log->info('Login successful. Credentials saved.');
308 3 print "Login successful.\n";
309 }
310  
311 3 $log->trace('Leaving do_login');
312 }
313  
314 5 ▶sub do_adc_login {
Subroutine do_adc_login: called CC=20 SCAR=35.2
315 5 my (%options) = @_;
316 5 $log->info('Command: application-default login initiated');
317 5 $log->trace('Entering do_adc_login');
318  
319 5 my $client_id_file = $options{client_id_file};
320 5 my $store_dir = $options{store_dir};
321  
322 5 my $client_id;
323 5 ▶ if ($client_id_file && -f $client_id_file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($client_id_file and -f $client_id_file) { } 5 0 5
Condition33%1/3
$client_id_file and -f $client_id_file
!ll&&!rl&&r
005
MC/DC0%0/2
$client_id_file and -f $client_id_file
$client_id_file -f $client_id_file
Truth table33%1/3
A and B
A= $client_id_fileB= -f $client_id_file
ABresult
0X0
100
111
324 5 $client_id = Google::Auth::ClientId->from_file($client_id_file);
325 } else {
326 0 $log->info('Using default ADC Client ID');
327 0 $client_id = Google::Auth::ClientId->new(
328 id =>
329 '764086051850-6qr4p6gpi6hn506pt8ejuq83di341hur.apps.googleusercontent.com',
330 secret => 'd-FL95Q19q7MQmFpd7hHD0Ty',
331 );
332 }
333 5 my $token_store =
334 Google::Auth::Stores::FileTokenStore->new(store_dir => $store_dir);
335  
336 5 my $listener;
337 my $redirect_uri;
338  
339 5 my $use_oob = $options{no_browser};
340 5 ▶ unless ($use_oob || $options{port}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($use_oob or $options{"port"}) 3 2 5
Condition66%2/3
$use_oob or $options{"port"}
l!l&&r!l&&!r
023
MC/DC50%1/2
$use_oob or $options{"port"}
$use_oob $options{"port"}
Truth table66%2/3
A or B
A= $use_oobB= $options{"port"}
ABresult
1X1
011
000
341 3 ▶ if ($^O eq 'linux' && !$ENV{DISPLAY} && !$ENV{WAYLAND_DISPLAY}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq "linux" and not $ENV{"DISPLAY"} and not $ENV{"WAYLAND_DISPLAY"}) 0 3 3
Condition33%2/6
$^O eq "linux" and not $ENV{"DISPLAY"} and not $ENV{"WAYLAND_DISPLAY"}
!ll&&!rl&&r
300
$^O eq "linux" and not $ENV{"DISPLAY"}
!ll&&!rl&&r
030
MC/DC0%0/3
$^O eq "linux" and not $ENV{"DISPLAY"} and not $ENV{"WAYLAND_DISPLAY"}
$^O eq "linux" not $ENV{"DISPLAY"} not $ENV{"WAYLAND_DISPLAY"}
Truth table25%1/4
A and not(B) and not(C)
A= $^O eq "linux"B= not $ENV{"DISPLAY"}C= not $ENV{"WAYLAND_DISPLAY"}
ABCresult
0XX0
10X0
1100
1111
342 0 $log->info(
343 'No DISPLAY detected on Linux, defaulting to out-of-band flow');
344 0 $use_oob = 1;
345 }
346 }
347  
348 5 my $code_verifier;
349 5 ▶ if ($use_oob) {
Branch: 1 branch
BranchTrueFalseTotal
if ($use_oob) { } 0 5 5
350 0 $redirect_uri =
351 'https://sdk.cloud.google.com/applicationdefaultauthcode.html';
352 0 $code_verifier = _generate_secure_token(48);
353 } else {
354 5 $listener = _start_listener($options{port});
355 5 my $port = $listener->sockport();
356 5 $log->debugf('Loopback listener started on port %d', $port);
357 5 $redirect_uri = "http://127.0.0.1:$port/";
358 }
359  
360 5 my %auth_args = (
361 client_id => $client_id,
362 scope => \@DEFAULT_SCOPES,
363 token_store => $token_store,
364 callback_uri => $redirect_uri,
365 );
366 5 ▶ $auth_args{code_verifier} = $code_verifier if $code_verifier;
Branch: 1 branch
BranchTrueFalseTotal
if $code_verifier 0 5 5
367  
368 5 my $auth = Google::Auth::UserAuthorizer->new(%auth_args);
369  
370 5 my %auth_options;
371 5 ▶ $auth_options{additional_parameters} = {token_usage => 'remote'}
Branch: 1 branch
BranchTrueFalseTotal
if $use_oob 0 5 5
372 if $use_oob;
373  
374 5 my $state = _generate_secure_token(24);
375 5 $auth_options{state} = $state;
376  
377 5 my $auth_url = $auth->get_authorization_url(%auth_options);
378  
379 5 print "\nGo to the following link in your browser:\n\n $auth_url\n\n";
380  
381 5 my $code;
382 5 ▶ if ($use_oob) {
Branch: 1 branch
BranchTrueFalseTotal
if ($use_oob) { } 0 5 5
383 0 print "Enter authorization code: ";
384 0 $code = <STDIN>;
385 0 ▶ chomp $code if $code;
Branch: 1 branch
BranchTrueFalseTotal
if $code 0 0 0
386 } else {
387 5 $log->info('Waiting for authorization code...');
388 5 $code = _wait_for_code($listener, $options{timeout}, $state);
389 }
390  
391 2 ▶ if ($code) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code) 2 0 2
392 2 $log->info('Exchanging code for credentials...');
393  
394 2 my $creds;
395 2 eval {
396 2 $creds = $auth->get_credentials_from_code(
397 code => $code,
398 base_url => $redirect_uri,
399 );
400 };
401 2 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 2 2
402 0 $log->errorf('Failed to exchange code: %s', $@);
403 0 die "Error: Failed to exchange code: $@\n";
404 }
405  
406 2 $log->info('Code exchanged successfully.');
407  
408 2 my $adc_path = _get_adc_path();
409 2 ▶ unless ($adc_path) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($adc_path) 0 2 2
410 0 $log->error('Failed to determine ADC path');
411 0 die "Error: Failed to determine ADC path (HOME or APPDATA not set?)\n";
412 }
413  
414 2 $log->debugf('Target ADC path: %s', $adc_path);
415  
416 2 my $adc_data = {
417 type => 'authorized_user',
418 client_id => $creds->client_id,
419 client_secret => $creds->client_secret,
420 refresh_token => $creds->refresh_token,
421 };
422  
423 2 my ($volume, $directories, $file) = File::Spec->splitpath($adc_path);
424 2 my $adc_dir = File::Spec->catpath($volume, $directories, '');
425 2 ▶ if (!-d $adc_dir) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $adc_dir) 2 0 2
426 2 require File::Path;
427 2 eval { File::Path::make_path($adc_dir) };
428 2 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 2 2
429 0 $log->errorf('Failed to create ADC directory %s: %s', $adc_dir, $@);
430 0 die "Error: Failed to create ADC directory: $@\n";
431 }
432 }
433  
434 2 require Fcntl;
435 2 ▶ sysopen(my $fh, $adc_path,
Branch: 1 branch
BranchTrueFalseTotal
sysopen my $fh, $adc_path, 577, 384 or die "Error: Failed to write to ${adc_path}: $!\n" 2 0 2
436 Fcntl::O_CREAT() | Fcntl::O_WRONLY() | Fcntl::O_TRUNC(), 0600)
437 or die "Error: Failed to write to $adc_path: $!\n";
438  
439 2 print $fh encode_json($adc_data);
440 2 ▶ close($fh) or die "Error: Failed to close $adc_path: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or die "Error: Failed to close ${adc_path}: $!\n" 2 0 2
441  
442 2 $log->info('ADC Login successful. Credentials saved to ' . $adc_path);
443 2 print "Application Default Credentials saved to $adc_path\n";
444 }
445  
446 2 $log->trace('Leaving do_adc_login');
447 }
448  
449 2 ▶sub _get_adc_path {
Subroutine _get_adc_path: called CC=5 SCAR=18.3
450 2 require Google::Auth::EnvironmentVars;
451 2 my $env = Google::Auth::EnvironmentVars->new();
452  
453 2 my $home = $ENV{HOME};
454 2 ▶ if ($^O eq 'MSWin32') {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq "MSWin32") 0 2 2
455 0 $home = $ENV{APPDATA};
456 }
457  
458 2 ▶ return unless $home;
Branch: 1 branch
BranchTrueFalseTotal
unless $home 0 2 2
459  
460 2 ▶ if ($^O eq 'MSWin32') {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq 'MSWin32') { } 0 2 2
461 0 return File::Spec->catfile($home, 'gcloud',
462 'application_default_credentials.json');
463 } else {
464 2 ▶ my $config_dir =
Condition33%1/3
$env->CLOUD_SDK_CONFIG_DIR || 'File::Spec'->catdir($home, '.config')
l!l&&r!l&&!r
200
MC/DC0%0/2
$env->CLOUD_SDK_CONFIG_DIR || 'File::Spec'->catdir($home, '.config')
$env->CLOUD_SDK_CONFIG_DIR 'File::Spec'->catdir($home, '.config')
Truth table33%1/3
A || B
A= $env->CLOUD_SDK_CONFIG_DIRB= 'File::Spec'->catdir($home, '.config')
ABresult
1X1
011
000
465 $env->CLOUD_SDK_CONFIG_DIR || File::Spec->catdir($home, '.config');
466 2 return File::Spec->catfile($config_dir, 'gcloud',
467 'application_default_credentials.json');
468 }
469 }
470  
471 2 ▶sub do_print_access_token {
Subroutine do_print_access_token: called CC=4 SCAR=14.6
472 2 my (%options) = @_;
473 2 $log->info('Command: print-access-token initiated');
474 2 $log->trace('Entering do_print_access_token');
475  
476 2 my $store_dir = $options{store_dir};
477 2 my $user_id = 'default'; # TODO: allow specifying user/account
478  
479 2 require Google::Auth::Stores::FileTokenStore;
480 2 my $token_store =
481 Google::Auth::Stores::FileTokenStore->new(store_dir => $store_dir);
482  
483 2 my $json = $token_store->load($user_id);
484 2 ▶ unless ($json) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($json) 0 2 2
485 0 $log->error("No credentials found for user $user_id");
486 0 die "Error: No credentials found. Please run 'login' first.\n";
487 }
488  
489 2 require JSON::MaybeXS;
490 2 my $creds_data = JSON::MaybeXS::decode_json($json);
491  
492 2 require Google::Auth::UserRefreshCredentials;
493 my $creds = Google::Auth::UserRefreshCredentials->new(
494 client_id => $creds_data->{client_id},
495 client_secret => $creds_data->{client_secret},
496 refresh_token => $creds_data->{refresh_token},
497 2 );
498  
499 2 my $token;
500 2 eval { $token = $creds->get_token(); };
501 2 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 2 2
502 0 $log->errorf('Failed to fetch access token: %s', $@);
503 0 die "Error: Failed to fetch access token: $@\n";
504 }
505  
506 2 ▶ if ($token) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token) { } 2 0 2
507 2 print "$token\n";
508 2 $log->info('Access token printed successfully');
509 } else {
510 0 $log->error('Failed to obtain access token (empty)');
511 0 die "Error: Failed to obtain access token.\n";
512 }
513  
514 2 $log->trace('Leaving do_print_access_token');
515 }
516  
517 2 ▶sub do_revoke {
Subroutine do_revoke: called CC=5 SCAR=16.6
518 2 my (%options) = @_;
519 2 $log->info('Command: revoke initiated');
520 2 $log->trace('Entering do_revoke');
521  
522 2 my $store_dir = $options{store_dir};
523 2 my $user_id = 'default'; # TODO: allow specifying user/account
524  
525 2 require Google::Auth::Stores::FileTokenStore;
526 2 my $token_store =
527 Google::Auth::Stores::FileTokenStore->new(store_dir => $store_dir);
528  
529 2 my $json = $token_store->load($user_id);
530 2 ▶ unless ($json) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($json) 0 2 2
531 0 $log->warn("No credentials found for user $user_id to revoke");
532 0 print "No credentials found to revoke.\n";
533 0 return;
534 }
535  
536 2 require JSON::MaybeXS;
537 2 my $creds_data = JSON::MaybeXS::decode_json($json);
538  
539 2 my $refresh_token = $creds_data->{refresh_token};
540  
541 2 ▶ if ($refresh_token) {
Branch: 1 branch
BranchTrueFalseTotal
if ($refresh_token) 2 0 2
542 2 $log->info('Revoking refresh token...');
543  
544 # Google revocation endpoint
545 my $revoke_uri = $ENV{GOOGLE_AUTH_REVOKE_URI}
546 2 ▶ // 'https://oauth2.googleapis.com/revoke';
Condition50%1/2
$ENV{"GOOGLE_AUTH_REVOKE_URI"} // "https://oauth2.googleapis.com/revoke"
l!l
02
MC/DC0%0/1
$ENV{"GOOGLE_AUTH_REVOKE_URI"} // "https://oauth2.googleapis.com/revoke"
$ENV{"GOOGLE_AUTH_REVOKE_URI"}
Truth table50%1/2
A
A= $ENV{"GOOGLE_AUTH_REVOKE_URI"}
Aresult
11
00
547  
548 # We need an LWP::UserAgent or similar.
549 # Let's see if we can use Google::Auth::UserRefreshCredentials->ua or just LWP directly.
550 # UserRefreshCredentials doesn't expose UA easily without instance.
551 # Let's check Google::Auth::RetryHelper or invent one.
552  
553 2 require LWP::UserAgent;
554 2 my $ua = LWP::UserAgent->new();
555  
556 2 my $res = $ua->post($revoke_uri, {token => $refresh_token});
557  
558 2 ▶ if ($res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) { } 2 0 2
559 2 $log->info('Token revocation request successful');
560 } else {
561 0 $log->warnf('Token revocation request failed: %s', $res->status_line);
562  
563 # We proceed to delete locally anyway? Yes, common practice.
564 }
565 }
566  
567 2 $log->info('Deleting local credentials...');
568 2 $token_store->delete($user_id);
569  
570 2 print "Credentials revoked.\n";
571  
572 2 $log->trace('Leaving do_revoke');
573 }
574  
575 __END__
« blib/lib/Google/Auth/DefaultCredentials.pm ↑ Summary blib/lib/Google/Auth/UserAuthorizer.pm »