blib/lib/Google/Auth/UserAuthorizer.pm

filter: Statementstmt 54.4% 55 / 101 Branchbran 25.0% 8 / 32 Conditioncond 26.3% 5 / 19 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 68.4% 13 / 19 Podpod 0.0% 0 / 5 totaltotal 43.0% 81 / 188 CC 24 SCAR 23.1
CC
24
Cov
45%
get_credentials
30.0
_redirect_uri_for
19.0
store_credentials
12.0
CRAP 10.0
« blib/lib/Google/Auth/ComputeEngine.pm ↑ Summary blib/lib/Google/Auth/ExternalAccountCredentials.pm »
1 # Copyright 2022 Google LLC and contributors
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14  
15 package Google::Auth::UserAuthorizer;
16  
17 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 2 ▶use Google::Auth::Exceptions;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 2 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 2 ▶use JSON::MaybeXS;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 2 ▶use Digest::SHA qw(sha256);
Subroutine BEGIN: called CC=1 SCAR=0.0
26 2 ▶use MIME::Base64 qw(encode_base64url);
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 2 has client_id => (
29 is => 'ro',
30 required => 1,
31 );
32  
33 2 has scope => (
34 is => 'ro',
35 required => 1,
36 );
37  
38 2 has token_store => (
39 is => 'ro',
40 required => 1,
41 );
42  
43 has callback_uri => (
44 is => 'ro',
45 0 ▶ default => sub { '/oauth2callback' },
Subroutine __ANON__: not called CC=1 SCAR=6.9
46 2 );
47  
48 2 has code_verifier => (
49 is => 'rw',
50 required => 0,
51 );
52  
53 has auth_endpoint => (
54 is => 'ro',
55 2 ▶ default => sub {
Subroutine __ANON__: called CC=2 SCAR=7.6
56 $ENV{GOOGLE_AUTH_AUTH_ENDPOINT}
57 2 ▶ // 'https://accounts.google.com/o/oauth2/auth';
Condition50%1/2
$ENV{"GOOGLE_AUTH_AUTH_ENDPOINT"} // "https://accounts.google.com/o/oauth2/auth"
l!l
02
MC/DC0%0/1
$ENV{"GOOGLE_AUTH_AUTH_ENDPOINT"} // "https://accounts.google.com/o/oauth2/auth"
$ENV{"GOOGLE_AUTH_AUTH_ENDPOINT"}
Truth table50%1/2
A
A= $ENV{"GOOGLE_AUTH_AUTH_ENDPOINT"}
Aresult
11
00
58 },
59 2 );
60  
61 has token_uri => (
62 is => 'ro',
63 2 ▶ default => sub {
Subroutine __ANON__: called CC=2 SCAR=7.6
64 2 ▶ $ENV{GOOGLE_AUTH_TOKEN_URI} // 'https://oauth2.googleapis.com/token';
Condition50%1/2
$ENV{"GOOGLE_AUTH_TOKEN_URI"} // "https://oauth2.googleapis.com/token"
l!l
02
MC/DC0%0/1
$ENV{"GOOGLE_AUTH_TOKEN_URI"} // "https://oauth2.googleapis.com/token"
$ENV{"GOOGLE_AUTH_TOKEN_URI"}
Truth table50%1/2
A
A= $ENV{"GOOGLE_AUTH_TOKEN_URI"}
Aresult
11
00
65 },
66 2 );
67  
68 2 ▶around BUILDARGS => sub {
Subroutine __ANON__: called CC=5 SCAR=20.4
69 2 my ($orig, $class, @args) = @_;
70  
71 # Standardize arguments to a hashref
72 2 my $args;
73 2 ▶ if (@args == 1 && ref $args[0] eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (@args == 1 and ref $args[0] eq 'HASH') { } 0 2 2
Condition33%1/3
@args == 1 and ref $args[0] eq "HASH"
!ll&&!rl&&r
200
MC/DC0%0/2
@args == 1 and ref $args[0] eq "HASH"
@args == 1 ref $args[0] eq "HASH"
Truth table33%1/3
A and B
A= @args == 1B= ref $args[0] eq "HASH"
ABresult
0X0
100
111
74 0 $args = {%{$args[0]}};
75 } else {
76 2 $args = {@args};
77 }
78  
79 2 ▶ if ($args->{scope} && !ref($args->{scope})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"scope"} and not ref $args->{"scope"}) 0 2 2
Condition33%1/3
$args->{"scope"} and not ref $args->{"scope"}
!ll&&!rl&&r
020
MC/DC0%0/2
$args->{"scope"} and not ref $args->{"scope"}
$args->{"scope"} not ref $args->{"scope"}
Truth table33%1/3
A and not(B)
A= $args->{"scope"}B= not ref $args->{"scope"}
ABresult
0X0
100
111
80 0 $args->{scope} = [split(/\s+/, $args->{scope})];
81 }
82 2 return $class->$orig($args);
83 2 };
84  
85 2 ▶sub get_authorization_url {
Subroutine get_authorization_url: called CC=7 SCAR=22.7
Pod undocumented
86 2 my ($self, %options) = @_;
87  
88 2 ▶ my $scope = $options{scope} // $self->scope;
Condition33%1/3
$options{"scope"} // $self->scope
l!l&&r!l&&!r
020
MC/DC0%0/2
$options{"scope"} // $self->scope
$options{"scope"} $self->scope
Truth table33%1/3
A // B
A= $options{"scope"}B= $self->scope
ABresult
1X1
011
000
89 2 ▶ $scope = join(' ', @$scope) if ref($scope) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $scope eq "ARRAY" 2 0 2
90  
91 2 my $redirect_uri = $self->_redirect_uri_for($options{base_url});
92  
93 2 my $uri = URI->new($self->auth_endpoint);
94 2 my %params = (
95 client_id => $self->client_id->id,
96 redirect_uri => $redirect_uri,
97 response_type => 'code',
98 scope => $scope,
99 access_type => 'offline',
100 prompt => 'consent',
101 );
102  
103 2 ▶ $params{state} = $options{state} if defined $options{state};
Branch: 1 branch
BranchTrueFalseTotal
if defined $options{"state"} 2 0 2
104 2 ▶ $params{login_hint} = $options{login_hint} if defined $options{login_hint};
Branch: 1 branch
BranchTrueFalseTotal
if defined $options{"login_hint"} 0 2 2
105  
106 2 ▶ if ($self->code_verifier) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->code_verifier) 0 2 2
107 $params{code_challenge} =
108 0 $self->_generate_code_challenge($self->code_verifier);
109 0 $params{code_challenge_method} = 'S256';
110 }
111  
112 2 ▶ if ($options{additional_parameters}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"additional_parameters"}) 0 2 2
113 0 %params = (%params, %{$options{additional_parameters}});
114 }
115  
116 2 $uri->query_form(%params);
117 2 return $uri->as_string;
118 }
119  
120 0 ▶sub get_credentials {
Subroutine get_credentials: not called CC=5 SCAR=34.0
Pod undocumented
121 0 my ($self, $user_id, $scope) = @_;
122  
123 0 my $saved_token = $self->token_store->load($user_id);
124 0 ▶ return unless $saved_token;
Branch: 1 branch
BranchTrueFalseTotal
unless $saved_token 0 0 0
125  
126 0 my $data = decode_json($saved_token);
127  
128 0 ▶ if ($data->{client_id} ne $self->client_id->id) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{"client_id"} ne $self->client_id->id) 0 0 0
129 0 Google::Auth::Error->throw('Mismatched client ID');
130 }
131  
132 0 require Google::Auth::UserRefreshCredentials;
133 my $creds = Google::Auth::UserRefreshCredentials->new(
134 client_id => $self->client_id->id,
135 client_secret => $self->client_id->secret,
136 scope => $data->{scope} // $self->scope,
137 access_token => $data->{access_token},
138 refresh_token => $data->{refresh_token},
139 expires_at => $data->{expiration_time_millis}
140 0 ▶ ? $data->{expiration_time_millis} / 1000
Branch: 1 branch
BranchTrueFalseTotal
$data->{'expiration_time_millis'} ? : 0 0 0
Condition0%0/3
$data->{"scope"} // $self->scope
l!l&&r!l&&!r
000
MC/DC0%0/2
$data->{"scope"} // $self->scope
$data->{"scope"} $self->scope
Truth table0%0/3
A // B
A= $data->{"scope"}B= $self->scope
ABresult
1X1
011
000
141 : undef,
142 );
143  
144 # TODO: monitor credentials for refresh to update store?
145 # In Ruby: return monitor_credentials user_id, credentials if credentials.includes_scope? scope
146  
147 0 return $creds;
148 }
149  
150 0 ▶sub get_credentials_from_code {
Subroutine get_credentials_from_code: not called CC=2 SCAR=17.9
Pod undocumented
151 0 my ($self, %options) = @_;
152  
153 0 my $user_id = $options{user_id};
154 0 my $code = $options{code};
155 0 ▶ my $scope = $options{scope} // $self->scope;
Condition0%0/3
$options{"scope"} // $self->scope
l!l&&r!l&&!r
000
MC/DC0%0/2
$options{"scope"} // $self->scope
$options{"scope"} $self->scope
Truth table0%0/3
A // B
A= $options{"scope"}B= $self->scope
ABresult
1X1
011
000
156 0 my $base_url = $options{base_url};
157  
158 0 my $redirect_uri = $self->_redirect_uri_for($base_url);
159  
160 0 require Google::Auth::UserRefreshCredentials;
161 0 my $creds = Google::Auth::UserRefreshCredentials->new(
162 client_id => $self->client_id->id,
163 client_secret => $self->client_id->secret,
164 scope => $scope,
165 code => $code,
166 redirect_uri => $redirect_uri,
167 code_verifier => $self->code_verifier,
168 token_uri => $self->token_uri,
169 );
170  
171 0 $creds->get_token(); # This will trigger fetch_access_token with code
172  
173 # Clear code and redirect_uri as they are not needed for refresh
174 # But we can't clear them if they are ro/rw without accessors.
175 # They are rw/ro respectively.
176  
177 0 return $creds;
178 }
179  
180 0 ▶sub get_and_store_credentials_from_code {
Subroutine get_and_store_credentials_from_code: not called CC=1 SCAR=6.9
Pod undocumented
181 0 my ($self, %options) = @_;
182 0 my $creds = $self->get_credentials_from_code(%options);
183 0 $self->store_credentials($options{user_id}, $creds);
184 0 return $creds;
185 }
186  
187 0 ▶sub store_credentials {
Subroutine store_credentials: not called CC=3 SCAR=24.8
Pod undocumented
188 0 my ($self, $user_id, $credentials) = @_;
189  
190 0 ▶ my $data = {
Branch: 1 branch
BranchTrueFalseTotal
$credentials->expires_at ? : 0 0 0
191 client_id => $credentials->client_id,
192 access_token => $credentials->access_token,
193 refresh_token => $credentials->refresh_token,
194 scope => $credentials->scope,
195 expiration_time_millis => $credentials->expires_at
196 ? $credentials->expires_at * 1000
197 : undef,
198 };
199 0 ▶ $data->{client_secret} = $credentials->client_secret
Branch: 1 branch
BranchTrueFalseTotal
if $credentials->can("client_secret") 0 0 0
200 if $credentials->can('client_secret');
201  
202 0 $self->token_store->store($user_id, encode_json($data));
203 0 return $credentials;
204 }
205  
206 2 ▶sub _redirect_uri_for {
Subroutine _redirect_uri_for: called CC=5 SCAR=29.4
207 2 my ($self, $base_url) = @_;
208  
209 2 ▶ return $self->callback_uri if $self->callback_uri =~ /^https?:\/\//;
Branch: 1 branch
BranchTrueFalseTotal
if $self->callback_uri =~ m[^https?://] 2 0 2
210 0 ▶ return $self->callback_uri
Branch: 1 branch
BranchTrueFalseTotal
if $self->callback_uri eq "urn:ietf:wg:oauth:2.0:oob" 0 0 0
211 if $self->callback_uri eq 'urn:ietf:wg:oauth:2.0:oob';
212 0 ▶ return $self->callback_uri if $self->callback_uri eq 'postmessage';
Branch: 1 branch
BranchTrueFalseTotal
if $self->callback_uri eq "postmessage" 0 0 0
213  
214 0 ▶ if (!$base_url) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($base_url) 0 0 0
215 0 Google::Auth::Error->throw(
216 'Absolute base url required for relative callback url');
217 }
218  
219 0 my $uri = URI->new($base_url);
220  
221 # Resolve relative path
222 0 $uri->path($self->callback_uri)
223 ; # This might overwrite path completely if callback_uri starts with /
224 0 return $uri->as_string;
225 }
226  
227 0 ▶sub _generate_code_challenge {
Subroutine _generate_code_challenge: not called CC=1 SCAR=6.9
228 0 my ($self, $verifier) = @_;
229 0 my $hash = sha256($verifier);
230 0 return encode_base64url($hash);
231 }
232  
233 2 1;
234  
« blib/lib/Google/Auth/ComputeEngine.pm ↑ Summary blib/lib/Google/Auth/ExternalAccountCredentials.pm »