| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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"} // "."
MC/DC0%0/1
$ENV{"HOME"} // "."
$ENV{"HOME"}
Truth table50%1/2
A
A= $ENV{"HOME"}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 83 | 16 | ▶ | pod2usage(-verbose => 2) if $man; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||
| 85 | 16 | my $command = shift @ARGV; | |||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||
| 87 | 16 | ▶ | unless ($command) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 88 | 0 | pod2usage(1); | |||||||||||||||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||
| 91 | 16 | ▶ | if ($command eq 'login') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 102 | 0 | die "Error: application-default requires a subcommand (e.g., login)\n"; | |||||||||||||||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||||||||||||||
| 104 | 5 | ▶ | if ($subcommand eq 'login') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
MC/DC100%1/1
$port || 0
$port
Truth table100%2/2
A
A= $port
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
MC/DC0%0/1
$timeout //= 5
$timeout
Truth table50%1/2
A
A= $timeout
| |||||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||||
| 153 | 13 | my $select = IO::Select->new($listener); | |||||||||||||||||||||||||||||||||||||||||
| 154 | 13 | my $client; | |||||||||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||||||||||
| 156 | 13 | ▶ | if ($select->can_read($timeout)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 172 | 24 | $request .= $_; | |||||||||||||||||||||||||||||||||||||||||
| 173 | 24 | ▶ | last if $_ =~ /^\r\n$/; # End of headers | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%4/6
defined $expected_state and !defined($received_state) || $received_state ne $expected_state
not defined $received_state or $received_state ne $expected_state
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
$client_id_file and -f $client_id_file
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
$use_oob or $options{"port"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 241 | 4 | ▶ | if ($^O eq 'linux' && !$ENV{DISPLAY} && !$ENV{WAYLAND_DISPLAY}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
$^O eq "linux" and not $ENV{"DISPLAY"} and not $ENV{"WAYLAND_DISPLAY"}
$^O eq "linux" and not $ENV{"DISPLAY"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 282 | 0 | print "Enter authorization code: "; | |||||||||||||||||||||||||||||||||||||||||
| 283 | 0 | $code = <STDIN>; | |||||||||||||||||||||||||||||||||||||||||
| 284 | 0 | ▶ | chomp $code if $code; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
$client_id_file and -f $client_id_file
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
$use_oob or $options{"port"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 341 | 3 | ▶ | if ($^O eq 'linux' && !$ENV{DISPLAY} && !$ENV{WAYLAND_DISPLAY}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
$^O eq "linux" and not $ENV{"DISPLAY"} and not $ENV{"WAYLAND_DISPLAY"}
$^O eq "linux" and not $ENV{"DISPLAY"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 383 | 0 | print "Enter authorization code: "; | |||||||||||||||||||||||||||||||||||||||||
| 384 | 0 | $code = <STDIN>; | |||||||||||||||||||||||||||||||||||||||||
| 385 | 0 | ▶ | chomp $code if $code; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 426 | 2 | require File::Path; | |||||||||||||||||||||||||||||||||||||||||
| 427 | 2 | eval { File::Path::make_path($adc_dir) }; | |||||||||||||||||||||||||||||||||||||||||
| 428 | 2 | ▶ | if ($@) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 455 | 0 | $home = $ENV{APPDATA}; | |||||||||||||||||||||||||||||||||||||||||
| 456 | } | ||||||||||||||||||||||||||||||||||||||||||
| 457 | |||||||||||||||||||||||||||||||||||||||||||
| 458 | 2 | ▶ | return unless $home; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 459 | |||||||||||||||||||||||||||||||||||||||||||
| 460 | 2 | ▶ | if ($^O eq 'MSWin32') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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')
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')
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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"
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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||||||||||