bin/gcloud-auth.pl

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