blib/lib/Google/Auth/WebUserAuthorizer.pm

filter: Statementstmt 65.9% 58 / 88 Branchbran 35.2% 12 / 34 Conditioncond 36.3% 8 / 22 MC/DCmcdc 15.3% 2 / 13 Subroutinesub 85.7% 12 / 14 Podpod 0.0% 0 / 6 totaltotal 51.9% 92 / 177 CC 26 SCAR 21.5
CC
26
Cov
54%
get_authorization_url
30.0
_get_param
10.4
validate_callback_state
8.1
CRAP 8.6
« blib/lib/Google/Auth/RetryHelper.pm ↑ Summary blib/lib/Google/Auth/ClientId.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::WebUserAuthorizer;
16  
17 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 extends 'Google::Auth::UserAuthorizer';
19  
20 1 ▶use JSON::MaybeXS;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use Google::Auth::Exceptions;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 1 my $coder = JSON::MaybeXS->new->ascii->pretty->allow_nonref;
25  
26 1 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
27 1 STATE_PARAM => 'state',
28 AUTH_CODE_KEY => 'code',
29 ERROR_CODE_KEY => 'error',
30 SESSION_ID_KEY => 'session_id',
31 CALLBACK_STATE_KEY => 'g-auth-callback',
32 CURRENT_URI_KEY => 'current_uri',
33 XSRF_KEY => 'g-xsrf-token',
34 SCOPE_KEY => 'scope',
35 1 };
36  
37 # Generic request handling: assumes $request is a hash reference with
38 # 'session', 'url', and 'parameters' keys, or objects supporting those methods.
39  
40 4 ▶sub _get_session {
Subroutine _get_session: called CC=3 SCAR=15.1
41 4 my ($request) = @_;
42 4 ▶ if (ref($request) eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $request eq 'HASH') { } 4 0 4
elsif ($request->can('session')) { } 0 0 0
43 4 return $request->{session};
44 } elsif ($request->can('session')) {
45 0 return $request->session;
46 }
47 0 return;
48 }
49  
50 1 ▶sub _get_url {
Subroutine _get_url: called CC=3 SCAR=15.1
51 1 my ($request) = @_;
52 1 ▶ if (ref($request) eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $request eq 'HASH') { } 1 0 1
elsif ($request->can('url')) { } 0 0 0
53 1 return $request->{url};
54 } elsif ($request->can('url')) {
55 0 return $request->url;
56 }
57 0 return;
58 }
59  
60 8 ▶sub _get_param {
Subroutine _get_param: called CC=5 SCAR=23.4
61 8 my ($request, $key) = @_;
62 8 ▶ if (ref($request) eq 'HASH') {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $request eq 'HASH') { } 8 0 8
elsif ($request->can('param')) { } 0 0 0
elsif ($request->can('parameters')) { } 0 0 0
63 8 ▶ return $request->{parameters}->{$key} // $request->{$key};
Condition66%2/3
$request->{"parameters"}{$key} // $request->{$key}
l!l&&r!l&&!r
404
MC/DC50%1/2
$request->{"parameters"}{$key} // $request->{$key}
$request->{"parameters"}{$key} $request->{$key}
Truth table66%2/3
A // B
A= $request->{"parameters"}{$key}B= $request->{$key}
ABresult
1X1
011
000
64 } elsif ($request->can('param')) {
65 0 return $request->param($key);
66 } elsif ($request->can('parameters')) {
67 0 return $request->parameters->{$key};
68 }
69 0 return;
70 }
71  
72 2 ▶sub handle_auth_callback_deferred {
Subroutine handle_auth_callback_deferred: called CC=2 SCAR=7.0
Pod undocumented
73 2 my ($class, $request) = @_;
74 2 my ($callback_state, $redirect_uri) =
75 $class->extract_callback_state($request);
76 2 $class->validate_callback_state($callback_state, $request);
77  
78 1 my $session = _get_session($request);
79 1 ▶ Google::Auth::Error->throw('Sessions must be enabled') unless $session;
Branch: 1 branch
BranchTrueFalseTotal
unless $session 0 1 1
80  
81 1 $session->{+CALLBACK_STATE_KEY} = $coder->encode($callback_state);
82 1 return $redirect_uri;
83 }
84  
85 0 ▶sub handle_auth_callback {
Subroutine handle_auth_callback: not called CC=1 SCAR=6.9
Pod undocumented
86 0 my ($self, $user_id, $request) = @_;
87  
88 0 my ($callback_state, $redirect_uri) = $self->extract_callback_state($request);
89 0 $self->validate_callback_state($callback_state, $request);
90  
91 my $creds = $self->get_and_store_credentials_from_code(
92 user_id => $user_id,
93 code => $callback_state->{+AUTH_CODE_KEY},
94 0 scope => $callback_state->{+SCOPE_KEY},
95 base_url => _get_url($request),
96 );
97  
98 0 return ($creds, $redirect_uri);
99 }
100  
101 0 ▶sub get_authorization_url {
Subroutine get_authorization_url: not called CC=5 SCAR=34.0
Pod undocumented
102 0 my ($self, %options) = @_;
103  
104 0 my $request = $options{request};
105 0 ▶ Google::Auth::Error->throw('Request is required') unless $request;
Branch: 1 branch
BranchTrueFalseTotal
unless $request 0 0 0
106  
107 0 my $session = _get_session($request);
108 0 ▶ Google::Auth::Error->throw('Sessions must be enabled') unless $session;
Branch: 1 branch
BranchTrueFalseTotal
unless $session 0 0 0
109  
110 0 ▶ my $state = $options{state} // {};
Condition0%0/2
$options{"state"} // {}
l!l
00
MC/DC0%0/1
$options{"state"} // {}
$options{"state"}
Truth table0%0/2
A
A= $options{"state"}
Aresult
11
00
111 0 ▶ my $redirect_to = $options{redirect_to} // _get_url($request);
Condition0%0/3
$options{"redirect_to"} // _get_url($request)
l!l&&r!l&&!r
000
MC/DC0%0/2
$options{"redirect_to"} // _get_url($request)
$options{"redirect_to"} _get_url($request)
Truth table0%0/3
A // B
A= $options{"redirect_to"}B= _get_url($request)
ABresult
1X1
011
000
112  
113 # Generate XSRF token
114 0 require Google::Auth;
115 0 my $random_bytes = Google::Auth::get_secure_random_bytes(32);
116 0 my $xsrf_token = unpack('H*', $random_bytes);
117 0 $session->{+XSRF_KEY} = $xsrf_token;
118  
119 0 my $state_data = {
120 %$state,
121 SESSION_ID_KEY() => $xsrf_token,
122 CURRENT_URI_KEY() => $redirect_to,
123 };
124  
125 0 $options{state} = $coder->encode($state_data);
126 0 $options{base_url} = _get_url($request);
127  
128 0 return $self->SUPER::get_authorization_url(%options);
129 }
130  
131 1 ▶sub get_credentials {
Subroutine get_credentials: called CC=4 SCAR=15.5
Pod undocumented
132 1 my ($self, $user_id, $request, $scope) = @_;
133  
134 1 ▶ my $session = $request ? _get_session($request) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$request ? : 1 0 1
135  
136 1 ▶ if ($session && exists $session->{+CALLBACK_STATE_KEY}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($session and exists $session->{"g-auth-callback"}) 1 0 1
Condition33%1/3
$session and exists $session->{"g-auth-callback"}
!ll&&!rl&&r
001
MC/DC0%0/2
$session and exists $session->{"g-auth-callback"}
$session exists $session->{"g-auth-callback"}
Truth table33%1/3
A and B
A= $sessionB= exists $session->{"g-auth-callback"}
ABresult
0X0
100
111
137 1 my $state_json = delete $session->{+CALLBACK_STATE_KEY};
138 1 my $callback_state = $coder->decode($state_json);
139  
140 return $self->get_and_store_credentials_from_code(
141 user_id => $user_id,
142 code => $callback_state->{+AUTH_CODE_KEY},
143 1 scope => $callback_state->{+SCOPE_KEY},
144 base_url => _get_url($request),
145 );
146 }
147  
148 0 return $self->SUPER::get_credentials($user_id, $scope);
149 }
150  
151 2 ▶sub extract_callback_state {
Subroutine extract_callback_state: called CC=4 SCAR=15.0
Pod undocumented
152 2 my ($class, $request) = @_;
153  
154 2 ▶ my $state_json = _get_param($request, STATE_PARAM) // '{}';
Condition50%1/2
_get_param($request, "state") // "{}"
l!l
20
MC/DC0%0/1
_get_param($request, "state") // "{}"
_get_param($request, "state")
Truth table50%1/2
A
A= _get_param($request, "state")
Aresult
11
00
155 2 my $state = eval { $coder->decode($state_json) };
156 2 ▶ if ($@ || ref($state) ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ or ref $state ne "HASH") 0 2 2
Condition33%1/3
$@ or ref $state ne "HASH"
l!l&&r!l&&!r
002
MC/DC0%0/2
$@ or ref $state ne "HASH"
$@ ref $state ne "HASH"
Truth table33%1/3
A or B
A= $@B= ref $state ne "HASH"
ABresult
1X1
011
000
157 0 Google::Auth::Error->throw('Invalid state parameter in callback request');
158 }
159  
160 2 my $redirect_uri = $state->{+CURRENT_URI_KEY};
161  
162 my $callback_state = {
163 AUTH_CODE_KEY() => _get_param($request, AUTH_CODE_KEY),
164 ERROR_CODE_KEY() => _get_param($request, ERROR_CODE_KEY),
165 2 SESSION_ID_KEY() => $state->{+SESSION_ID_KEY},
166 SCOPE_KEY() => _get_param($request, SCOPE_KEY),
167 };
168  
169 2 return ($callback_state, $redirect_uri);
170 }
171  
172 2 ▶sub validate_callback_state {
Subroutine validate_callback_state: called CC=7 SCAR=20.9
Pod undocumented
173 2 my ($self, $state, $request) = @_;
174  
175 Google::Auth::Error->throw('Missing authorization code in request')
176 2 ▶ unless defined $state->{+AUTH_CODE_KEY};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $state->{"code"} 0 2 2
177  
178 2 ▶ if ($state->{+ERROR_CODE_KEY}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state->{"error"}) 0 2 2
179 Google::Auth::Error->throw(
180 0 'Authorization error: ' . $state->{+ERROR_CODE_KEY});
181 }
182  
183 2 my $session = _get_session($request);
184 2 ▶ Google::Auth::Error->throw('Sessions must be enabled') unless $session;
Branch: 1 branch
BranchTrueFalseTotal
unless $session 0 2 2
185  
186 2 my $stored_xsrf = $session->{+XSRF_KEY};
187 2 my $received_xsrf = $state->{+SESSION_ID_KEY};
188  
189 2 ▶ if ( !defined $stored_xsrf
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $stored_xsrf && defined $received_xsrf or $stored_xsrf ne $received_xsrf) 1 1 2
Condition50%3/6
not defined $stored_xsrf && defined $received_xsrf or $stored_xsrf ne $received_xsrf
l!l&&r!l&&!r
011
defined $stored_xsrf && defined $received_xsrf
!ll&&!rl&&r
002
MC/DC33%1/3
not defined $stored_xsrf && defined $received_xsrf or $stored_xsrf ne $received_xsrf
defined $stored_xsrf defined $received_xsrf $stored_xsrf ne $received_xsrf
Truth table50%2/4
not(A && B) or C
A= defined $stored_xsrfB= defined $received_xsrfC= $stored_xsrf ne $received_xsrf
ABCresult
0XX1
10X1
1111
1100
190 || !defined $received_xsrf
191 || $stored_xsrf ne $received_xsrf)
192 {
193 1 Google::Auth::Error->throw('State token does not match expected value');
194 }
195  
196 1 delete $session->{+XSRF_KEY};
197 }
198  
199 package Google::Auth::WebUserAuthorizer::CallbackApp;
200  
201 # To be implemented if we want a turnkey Plack app similar to Ruby's rack app.
202 # For now, WebUserAuthorizer provides the helpers to build one easily.
203  
204 1 1;
« blib/lib/Google/Auth/RetryHelper.pm ↑ Summary blib/lib/Google/Auth/ClientId.pm »