VPNDetection/Oauth.pm

filter: Statementstmt 100% 223 / 223 Branchbran 97.1% 68 / 70 Conditioncond 80.6% 50 / 62 MC/DCmcdc 67.5% 25 / 37 Subroutinesub 100% 53 / 53 Podpod 57.8% 11 / 19 totaltotal 92.6% 430 / 464 CC 62 SCAR 16.4
CC
62
Cov
96%
_members
16.3
poll_device_token_p
7.3
register_p
6.0
CRAP 5.1
« VPNDetection.pm ↑ Summary VPNDetection/Error.pm »
1 package VPNDetection::Oauth;
2  
3 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 7 ▶use B ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use Digest::SHA ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 7 ▶use MIME::Base64 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 7 ▶use Mojo::Promise;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 7 ▶use Mojo::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 7 ▶use Net::SSLeay ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 7 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 7 ▶use VPNDetection::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 7 ▶use VPNDetection::OauthError;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 7 our $VERSION = '3.15.0';
20  
21 7 ▶use constant DEVICE_CODE_GRANT => 'urn:ietf:params:oauth:grant-type:device_code';
Subroutine BEGIN: called CC=1 SCAR=0.0
22 # The only PKCE method the server accepts.
23 7 ▶use constant PKCE_METHOD => 'S256';
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 # Each member a response may carry, by wire name: its type, whether it is
26 # required, and the name it is surfaced under when that differs.
27 7 my %METADATA = (
28 issuer => ['string', 1],
29 authorization_endpoint => ['string', 1],
30 token_endpoint => ['string', 1],
31 device_authorization_endpoint => ['string'],
32 revocation_endpoint => ['string'],
33 scopes_supported => ['list'],
34 response_types_supported => ['list'],
35 grant_types_supported => ['list'],
36 code_challenge_methods_supported => ['list'],
37 token_endpoint_auth_methods_supported => ['list'],
38 authorization_response_iss_parameter_supported => ['bool'],
39 client_id_metadata_document_supported => ['bool'],
40 registration_endpoint => ['string'],
41 service_documentation => ['string'],
42 );
43 7 my %DEVICE_AUTHORIZATION = (
44 device_code => ['string', 1],
45 user_code => ['string', 1],
46 verification_uri => ['string', 1],
47 verification_uri_complete => ['string'],
48 expires_in => ['int', 1],
49 interval => ['int', 1],
50 );
51 7 my %TOKEN_RESPONSE = (
52 access_token => ['string', 1],
53 token_type => ['string', 1],
54 expires_in => ['int', 1],
55 refresh_token => ['string'],
56 scope => ['string'],
57 'mslm:apikey_id' => ['string', 0, 'apikey_id'],
58 'mslm:apikey' => ['string', 0, 'apikey'],
59 );
60  
61 7 my %CLIENT_REGISTRATION = (
62 client_id => ['string', 1],
63 client_id_issued_at => ['int', 1],
64 client_name => ['string', 1],
65 redirect_uris => ['list', 1],
66 grant_types => ['list'],
67 response_types => ['list'],
68 token_endpoint_auth_method => ['string', 1],
69 scope => ['string'],
70 );
71  
72 # The authorization server's discovery document. No call here needs it: each
73 # builds on the client's base URL.
74 22 ▶sub metadata {
Subroutine metadata: called CC=1 SCAR=0.0
Pod documented
75 22 my $self = shift;
76 22 $self->_assert_blocking_ok('metadata');
77 22 return $self->{client}->_wait($self->metadata_p(@_));
78 }
79  
80 22 ▶sub metadata_p {
Subroutine metadata_p: called CC=1 SCAR=0.0
Pod undocumented
81 22 my ($self, %options) = @_;
82 22 $self->{client}->_check_options('oauth->metadata', \%options, 'timeout');
83 21 my $path = '/.well-known/oauth-authorization-server';
84 21 return $self->_send_p($path, undef, $self->{client}{retries}, \%options, \%METADATA);
85 }
86  
87 # Starts a device sign-in. It consumes nothing, so it is retried like a lookup.
88 24 ▶sub device_authorization {
Subroutine device_authorization: called CC=1 SCAR=0.0
Pod documented
89 24 my $self = shift;
90 24 $self->_assert_blocking_ok('device_authorization');
91 24 return $self->{client}->_wait($self->device_authorization_p(@_));
92 }
93  
94 25 ▶sub device_authorization_p {
Subroutine device_authorization_p: called CC=4 SCAR=13.9
Pod undocumented
95 25 my ($self, $client_id, %options) = @_;
96 25 _assert_argument('device_authorization', client_id => $client_id);
97 25 $self->{client}->_check_options('oauth->device_authorization', \%options, 'scope', 'resource', 'timeout');
98 25 my %form = (client_id => $client_id);
99 25 for my $name (qw(scope resource)) {
100 49 ▶ next unless defined $options{$name} && length $options{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $options{$name} and length $options{$name} 45 4 49
Condition100%3/3
defined $options{$name} and length $options{$name}
!ll&&!rl&&r
4324
MC/DC100%2/2
defined $options{$name} and length $options{$name}
defined $options{$name} length $options{$name}
Truth table100%3/3
A and B
A= defined $options{$name}B= length $options{$name}
ABresult
0X0
100
111
101 4 _assert_argument('device_authorization', $name => $options{$name});
102 3 $form{$name} = $options{$name};
103 }
104 return $self->_send_p(
105 24 '/oauth/device_authorization', \%form, $self->{client}{retries}, \%options, \%DEVICE_AUTHORIZATION,
106 );
107 }
108  
109 # Asks once whether the person approved. Never retried: an approved code is
110 # spent by the answer carrying the tokens, so a retry could only lose them.
111 41 ▶sub exchange_device_code {
Subroutine exchange_device_code: called CC=1 SCAR=0.0
Pod documented
112 41 my $self = shift;
113 41 $self->_assert_blocking_ok('exchange_device_code');
114 41 return $self->{client}->_wait($self->exchange_device_code_p(@_));
115 }
116  
117 41 ▶sub exchange_device_code_p {
Subroutine exchange_device_code_p: called CC=1 SCAR=0.0
Pod undocumented
118 41 my ($self, $client_id, $device_code, %options) = @_;
119 41 _assert_argument('exchange_device_code', client_id => $client_id, device_code => $device_code);
120 40 $self->{client}->_check_options('oauth->exchange_device_code', \%options, 'timeout');
121 40 return $self->_exchange_p({
122 grant_type => DEVICE_CODE_GRANT, device_code => $device_code, client_id => $client_id,
123 }, \%options);
124 }
125  
126 # The refresh token presented is spent whatever happens next, so this is never
127 # retried either.
128 13 ▶sub exchange_refresh_token {
Subroutine exchange_refresh_token: called CC=1 SCAR=0.0
Pod documented
129 13 my $self = shift;
130 13 $self->_assert_blocking_ok('exchange_refresh_token');
131 13 return $self->{client}->_wait($self->exchange_refresh_token_p(@_));
132 }
133  
134 13 ▶sub exchange_refresh_token_p {
Subroutine exchange_refresh_token_p: called CC=1 SCAR=0.0
Pod undocumented
135 13 my ($self, $client_id, $refresh_token, %options) = @_;
136 13 _assert_argument('exchange_refresh_token', client_id => $client_id, refresh_token => $refresh_token);
137 13 $self->{client}->_check_options('oauth->exchange_refresh_token', \%options, 'timeout');
138 13 return $self->_exchange_p({
139 grant_type => 'refresh_token', refresh_token => $refresh_token, client_id => $client_id,
140 }, \%options);
141 }
142  
143 # The server answers the same for any token, so the body is never read.
144 11 ▶sub revoke {
Subroutine revoke: called CC=1 SCAR=0.0
Pod documented
145 11 my $self = shift;
146 11 $self->_assert_blocking_ok('revoke');
147 11 $self->{client}->_wait($self->revoke_p(@_));
148 6 return;
149 }
150  
151 11 ▶sub revoke_p {
Subroutine revoke_p: called CC=1 SCAR=0.0
Pod undocumented
152 11 my ($self, $client_id, $token, %options) = @_;
153 11 _assert_argument('revoke', client_id => $client_id, token => $token);
154 11 $self->{client}->_check_options('oauth->revoke', \%options, 'timeout');
155 10 my %form = (token => $token, client_id => $client_id);
156 10 ▶ return $self->_send_p('/oauth/revoke', \%form, $self->{client}{retries}, \%options)->then(sub { return });
Subroutine __ANON__: called CC=1 SCAR=0.0
157 }
158  
159 # Registers a client for the authorization code flow, for an app that cannot
160 # serve a Client ID Metadata Document, and answers its client_id. Nothing is kept
161 # until a person approves the client, so it is retried like a lookup.
162 34 ▶sub register {
Subroutine register: called CC=1 SCAR=0.0
Pod documented
163 34 my $self = shift;
164 34 $self->_assert_blocking_ok('register');
165 34 return $self->{client}->_wait($self->register_p(@_));
166 }
167  
168 34 ▶sub register_p {
Subroutine register_p: called CC=6 SCAR=17.9
Pod undocumented
169 34 my ($self, $redirect_uris, %options) = @_;
170 34 ▶ Carp::croak('VPNDetection::oauth->register: expected redirect_uris as a list of at least one URI')
Branch: 1 branch
BranchTrueFalseTotal
if ref $redirect_uris ne "ARRAY" or not @$redirect_uris 3 31 34
Condition100%3/3
ref $redirect_uris ne "ARRAY" or not @$redirect_uris
l!l&&r!l&&!r
2131
MC/DC100%2/2
ref $redirect_uris ne "ARRAY" or not @$redirect_uris
ref $redirect_uris ne "ARRAY" not @$redirect_uris
Truth table100%3/3
A or not(B)
A= ref $redirect_uris ne "ARRAY"B= not @$redirect_uris
ABresult
1X1
011
000
171 if ref $redirect_uris ne 'ARRAY' || !@$redirect_uris;
172 31 $self->{client}->_check_options('oauth->register', \%options, 'client_name', 'timeout');
173 31 my @uris = @$redirect_uris;
174 31 for my $i (0 .. $#uris) {
175 33 _assert_argument('register', "redirect_uris[$i]" => $uris[$i]);
176 32 _assert_utf8('register', "redirect_uris[$i]", $uris[$i]);
177 }
178 29 my %body = (redirect_uris => [map {"$_"} @uris]);
179 29 ▶ if (defined $options{client_name} && length $options{client_name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options{"client_name"} and length $options{"client_name"}) 3 26 29
Condition100%3/3
defined $options{"client_name"} and length $options{"client_name"}
!ll&&!rl&&r
2513
MC/DC100%2/2
defined $options{"client_name"} and length $options{"client_name"}
defined $options{"client_name"} length $options{"client_name"}
Truth table100%3/3
A and B
A= defined $options{"client_name"}B= length $options{"client_name"}
ABresult
0X0
100
111
180 3 _assert_argument('register', client_name => $options{client_name});
181 3 _assert_utf8('register', 'client_name', $options{client_name});
182 2 $body{client_name} = "$options{client_name}";
183 }
184 return $self->_send_p(
185 28 '/oauth/register', undef, $self->{client}{retries}, \%options, \%CLIENT_REGISTRATION, \%body,
186 );
187 }
188  
189 # The URL to open in the person's browser for the authorization code flow. Makes
190 # no request, so there is no promise form. Every value is percent-encoded over
191 # UTF-8 with only A-Z a-z 0-9 - . _ ~ left literal, so a space is %20, never +.
192 12 ▶sub authorization_url {
Subroutine authorization_url: called CC=5 SCAR=16.1
Pod documented
193 12 my ($self, $client_id, $redirect_uri, $code_challenge, %options) = @_;
194 12 my @required = (client_id => $client_id, redirect_uri => $redirect_uri, code_challenge => $code_challenge);
195 12 _assert_argument('authorization_url', @required);
196 8 $self->{client}->_check_options('oauth->authorization_url', \%options, 'scope', 'state', 'resource');
197 7 my @params = (response_type => 'code', @required, code_challenge_method => PKCE_METHOD);
198 7 for my $name (qw(scope state resource)) {
199 21 ▶ next unless defined $options{$name} && length $options{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $options{$name} and length $options{$name} 14 7 21
Condition100%3/3
defined $options{$name} and length $options{$name}
!ll&&!rl&&r
1137
MC/DC100%2/2
defined $options{$name} and length $options{$name}
defined $options{$name} length $options{$name}
Truth table100%3/3
A and B
A= defined $options{$name}B= length $options{$name}
ABresult
0X0
100
111
200 7 _assert_argument('authorization_url', $name => $options{$name});
201 7 push @params, $name => $options{$name};
202 }
203 7 my @query;
204 7 ▶ while (my ($name, $value) = splice @params, 0, 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@params, 0, 2)) 42 6 48
205 42 push @query, "$name=" . _percent_encode('authorization_url', $name, $value);
206 }
207 6 return $self->{client}->_url('/oauth/authorize')->to_string . '?' . join('&', @query);
208 }
209  
210 # Trades the code a sign-in's redirect brought back for tokens. Never retried: the
211 # server spends the code on first read, before it checks the verifier.
212 3 ▶sub exchange_authorization_code {
Subroutine exchange_authorization_code: called CC=1 SCAR=0.0
Pod documented
213 3 my $self = shift;
214 3 $self->_assert_blocking_ok('exchange_authorization_code');
215 3 return $self->{client}->_wait($self->exchange_authorization_code_p(@_));
216 }
217  
218 3 ▶sub exchange_authorization_code_p {
Subroutine exchange_authorization_code_p: called CC=1 SCAR=0.0
Pod undocumented
219 3 my ($self, $client_id, $code, $code_verifier, $redirect_uri, %options) = @_;
220 3 _assert_argument('exchange_authorization_code',
221 client_id => $client_id, code => $code, code_verifier => $code_verifier, redirect_uri => $redirect_uri);
222 3 $self->{client}->_check_options('oauth->exchange_authorization_code', \%options, 'timeout');
223 3 return $self->_exchange_p({
224 grant_type => 'authorization_code', code => $code, redirect_uri => $redirect_uri,
225 client_id => $client_id, code_verifier => $code_verifier,
226 }, \%options);
227 }
228  
229 # A fresh PKCE pair for one sign-in, from OpenSSL's secure random source: core
230 # perl has none, and Net::SSLeay comes with IO::Socket::SSL.
231 2 ▶sub create_pkce {
Subroutine create_pkce: called CC=3 SCAR=11.8
Pod documented
232 2 my ($self) = @_;
233 2 my $bytes;
234 2 ▶ Net::SSLeay::RAND_bytes($bytes, 32) == 1 && length $bytes == 32
Branch: 1 branch
BranchTrueFalseTotal
&Net::SSLeay::RAND_bytes($bytes, 32) == 1 and length $bytes == 32 or Carp::croak("VPNDetection::oauth->create_pkce: the secure random source failed") 2 0 2
Condition33%1/3
&Net::SSLeay::RAND_bytes($bytes, 32) == 1 and length $bytes == 32
!ll&&!rl&&r
002
MC/DC0%0/2
&Net::SSLeay::RAND_bytes($bytes, 32) == 1 and length $bytes == 32
&Net::SSLeay::RAND_bytes($bytes, 32) == 1 length $bytes == 32
Truth table33%1/3
A and B
A= &Net::SSLeay::RAND_bytes($bytes, 32) == 1B= length $bytes == 32
ABresult
0X0
100
111
235 or Carp::croak('VPNDetection::oauth->create_pkce: the secure random source failed');
236 2 my $verifier = MIME::Base64::encode_base64url($bytes);
237 2 return { verifier => $verifier, challenge => _challenge($verifier), method => PKCE_METHOD };
238 }
239  
240 3 ▶sub pkce_challenge {
Subroutine pkce_challenge: called CC=1 SCAR=0.0
Pod documented
241 3 my ($self, $verifier) = @_;
242 3 _assert_argument('pkce_challenge', verifier => $verifier);
243 3 return _challenge($verifier);
244 }
245  
246 21 ▶sub poll_device_token {
Subroutine poll_device_token: called CC=1 SCAR=0.0
Pod documented
247 21 my $self = shift;
248 21 $self->_assert_blocking_ok('poll_device_token');
249 21 return $self->{client}->_wait($self->poll_device_token_p(@_));
250 }
251  
252 22 ▶sub poll_device_token_p {
Subroutine poll_device_token_p: called CC=7 SCAR=19.9
Pod undocumented
253 22 my ($self, $client_id, $device, %options) = @_;
254 22 _assert_argument('poll_device_token', client_id => $client_id);
255 Carp::croak('VPNDetection::oauth->poll_device_token: expected the device authorization hash')
256 22 ▶ unless ref $device eq 'HASH' && defined $device->{device_code} && length $device->{device_code};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $device eq "HASH" and defined $device->{"device_code"} and length $device->{"device_code"} 1 21 22
Condition66%4/6
ref $device eq "HASH" and defined $device->{"device_code"} and length $device->{"device_code"}
!ll&&!rl&&r
1021
ref $device eq "HASH" and defined $device->{"device_code"}
!ll&&!rl&&r
1021
MC/DC33%1/3
ref $device eq "HASH" and defined $device->{"device_code"} and length $device->{"device_code"}
ref $device eq "HASH" defined $device->{"device_code"} length $device->{"device_code"}
Truth table50%2/4
A and B and C
A= ref $device eq "HASH"B= defined $device->{"device_code"}C= length $device->{"device_code"}
ABCresult
0XX0
10X0
1100
1111
257 21 $self->{client}->_check_options('oauth->poll_device_token', \%options, 'timeout');
258 21 my $interval = $device->{interval};
259 my $poll = {
260 client_id => $client_id,
261 device_code => $device->{device_code},
262 options => \%options,
263 interval => defined $interval && $interval >= 1 ? $interval : 5,
264 21 ▶ deadline => $self->{now}->() + ($device->{expires_in} || 0),
Branch: 1 branch
BranchTrueFalseTotal
defined $interval && $interval >= 1 ? : 20 1 21
Condition60%3/5
defined $interval and $interval >= 1
!ll&&!rl&&r
0120
$device->{'expires_in'} || 0
l!l
210
MC/DC50%1/2
defined $interval and $interval >= 1
defined $interval $interval >= 1
MC/DC0%0/1
$device->{'expires_in'} || 0
$device->{'expires_in'}
Truth table66%2/3
A and B
A= defined $intervalB= $interval >= 1
ABresult
0X0
100
111
Truth table50%1/2
A
A= $device->{'expires_in'}
Aresult
11
00
265 };
266 21 return $self->_poll_p($poll);
267 }
268  
269 164 ▶sub _new {
Subroutine _new: called CC=1 SCAR=0.0
270 164 my ($class, $client) = @_;
271 return bless {
272 client => $client,
273 # The poll's clock and its wait, replaced together in tests.
274 now => \&Mojo::Util::steady_time,
275 1 ▶ sleep_p => sub { Mojo::Promise->timer(shift) },
Subroutine __ANON__: called CC=1 SCAR=0.0
276 164 }, $class;
277 }
278  
279 # One wait, then one exchange. Recurses through $self rather than a
280 # self-referential closure, which would be a cycle the interpreter never collects.
281 # No wait runs past the deadline: an interval that would end after it, served
282 # that way or widened by slow_down, waits only the time left, and the local
283 # expiry follows with no request sent.
284 30 ▶sub _poll_p {
Subroutine _poll_p: called CC=3 SCAR=11.0
285 30 my ($self, $poll) = @_;
286 30 my $left = $poll->{deadline} - $self->{now}->();
287 # Not above 0 is nothing left, NaN included: a hand-built lifetime that is no
288 # number would otherwise wait nothing and ask again at once, for good.
289 30 ▶ $left = 0 unless $left > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $left > 0 2 28 30
290 30 my $to_deadline = $left <= $poll->{interval};
291 30 ▶ return $self->{sleep_p}->($to_deadline ? $left : $poll->{interval})->then(sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
292 # A wait that ran to the deadline ends the poll without reading the clock
293 # again: a timer may wake before the clock has moved its whole delay, and
294 # the deadline would then read as not yet reached.
295 die VPNDetection::OauthExpiredTokenError->new(error_code => 'expired_token')
296 30 ▶ if $to_deadline || $self->{now}->() >= $poll->{deadline};
Branch: 1 branch
BranchTrueFalseTotal
if $to_deadline or $self->{"now"}->() >= $poll->{"deadline"} 7 23 30
Condition66%2/3
$to_deadline or $self->{"now"}->() >= $poll->{"deadline"}
l!l&&r!l&&!r
7023
MC/DC50%1/2
$to_deadline or $self->{"now"}->() >= $poll->{"deadline"}
$to_deadline $self->{"now"}->() >= $poll->{"deadline"}
Truth table66%2/3
A or B
A= $to_deadlineB= $self->{"now"}->() >= $poll->{"deadline"}
ABresult
1X1
011
000
297 return $self->_exchange_p({
298 grant_type => DEVICE_CODE_GRANT,
299 device_code => $poll->{device_code},
300 client_id => $poll->{client_id},
301 17 ▶ }, $poll->{options})->catch(sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
302 17 my $error = shift;
303 17 ▶ die $error unless Scalar::Util::blessed($error) && $error->isa('VPNDetection::OauthError');
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($error) and $error->isa("VPNDetection::OauthError") 5 12 17
Condition66%2/3
builtin::blessed($error) and $error->isa("VPNDetection::OauthError")
!ll&&!rl&&r
0512
MC/DC50%1/2
builtin::blessed($error) and $error->isa("VPNDetection::OauthError")
builtin::blessed($error) $error->isa("VPNDetection::OauthError")
Truth table66%2/3
A and B
A= builtin::blessed($error)B= $error->isa("VPNDetection::OauthError")
ABresult
0X0
100
111
304 # RFC 8628: slow_down widens the interval for every later request, not just the next.
305 12 ▶ if ($error->error_code eq 'slow_down') {
Branch: 2 branches
BranchTrueFalseTotal
if ($error->error_code eq 'slow_down') { } 4 8 12
elsif ($error->error_code ne 'authorization_pending') { } 3 5 8
306 4 $poll->{interval} += 5;
307 }
308 elsif ($error->error_code ne 'authorization_pending') {
309 3 die $error;
310 }
311 9 return $self->_poll_p($poll);
312 23 });
313 30 ▶ });
Branch: 1 branch
BranchTrueFalseTotal
$to_deadline ? : 7 23 30
314 }
315  
316 79 ▶sub _exchange_p {
Subroutine _exchange_p: called CC=1 SCAR=0.0
317 79 my ($self, $form, $options) = @_;
318 return $self->_send_p('/oauth/token', $form, 0, $options)
319 79 ▶ ->then(sub { _members(shift, \%TOKEN_RESPONSE) });
Subroutine __ANON__: called CC=1 SCAR=0.0
320 }
321  
322 # Every OAuth request goes through here, and none carries the API key: they are
323 # built with build_tx directly, never through the client's _headers.
324 # Given $members, an attempt is the request AND the read of its answer, so a 2xx
325 # without them is retried like a 5xx where $retries allows, its status kept:
326 # read after the retries were spent, an HTML 200 was one request (3.5.3,
327 # measured 2026-10-15).
328 162 ▶sub _send_p {
Subroutine _send_p: called CC=1 SCAR=0.0
329 162 my ($self, $path, $form, $retries, $options, $members, $json) = @_;
330 162 my $client = $self->{client};
331 162 my $url = $client->_url($path);
332 207 ▶ return $client->_retry_p($retries, sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
333 207 my $ua = $client->{ua};
334 207 my %headers = (Accept => 'application/json');
335 # Mojo's form generator sends a `+` in a value as %2B and a space as `+`.
336 207 ▶ my $tx = defined $json ? $ua->build_tx(POST => $url => \%headers => json => $json)
Branch: 2 branches
BranchTrueFalseTotal
defined $json ? : 30 177 207
defined $form ? : 137 40 177
337 : defined $form ? $ua->build_tx(POST => $url => \%headers => form => $form)
338 : $ua->build_tx(GET => $url => \%headers);
339 183 ▶ return $client->_start_p($tx, $options->{timeout})->then(sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
340 183 my $res = _refusal(shift->res);
341 143 ▶ return $members ? _members($res, $members) : $res;
Branch: 1 branch
BranchTrueFalseTotal
$members ? : 101 42 143
342 207 });
343 162 });
344 }
345  
346 # Only a 4xx whose body is a JSON object with a STRING `error` is an OAuth
347 # refusal. Every 5xx, whatever it says, is the server failing, and is retried
348 # wherever the operation retries.
349 183 ▶sub _refusal {
Subroutine _refusal: called CC=6 SCAR=17.9
350 183 my ($res) = @_;
351 183 ▶ return $res if $res->is_success;
Branch: 1 branch
BranchTrueFalseTotal
if $res->is_success 143 40 183
352 40 my $body = $res->json;
353 40 my $error = VPNDetection::Error->from_response($res->code, $res->headers, $body);
354 40 ▶ die $error if $res->code >= 500 || ref $body ne 'HASH' || !_is_string($body->{error});
Branch: 1 branch
BranchTrueFalseTotal
if $res->code >= 500 or ref $body ne "HASH" or not _is_string($body->{"error"}) 15 25 40
Condition100%6/6
$res->code >= 500 or ref $body ne "HASH" or not _is_string($body->{"error"})
l!l&&r!l&&!r
13225
$res->code >= 500 or ref $body ne "HASH"
l!l&&r!l&&!r
11227
MC/DC100%3/3
$res->code >= 500 or ref $body ne "HASH" or not _is_string($body->{"error"})
$res->code >= 500 ref $body ne "HASH" not _is_string($body->{"error"})
Truth table100%4/4
A or B or not(C)
A= $res->code >= 500B= ref $body ne "HASH"C= not _is_string($body->{"error"})
ABCresult
1XX1
01X1
0011
0000
355 die VPNDetection::OauthError->_from(
356 error_code => $body->{error},
357 error_description => _is_string($body->{error_description}) ? $body->{error_description} : undef,
358 25 ▶ status => $res->code,
Branch: 1 branch
BranchTrueFalseTotal
_is_string($body->{'error_description'}) ? : 9 16 25
359 kind => $error->kind,
360 );
361 }
362  
363 # The declared members that are present, each checked against its type, so an
364 # absent one has no key at all and an empty `scope` is present. Anything else the
365 # server sends is dropped.
366 137 ▶sub _members {
Subroutine _members: called CC=16 SCAR=27.9
367 137 my ($res, $members) = @_;
368 137 my $body = $res->json;
369 137 ▶ die VPNDetection::Error->new(
Branch: 1 branch
BranchTrueFalseTotal
unless ref $body eq "HASH" 46 91 137
370 kind => 'server_error', status => $res->code,
371 message => 'the API did not answer with a JSON object',
372 ) unless ref $body eq 'HASH';
373  
374 91 my %present;
375 91 for my $wire (sort keys %$members) {
376 534 my ($type, $required, $name) = @{ $members->{$wire} };
377 534 my $value = $body->{$wire};
378 534 ▶ if (!defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 244 290 534
379 244 ▶ die VPNDetection::Error->new(
Branch: 1 branch
BranchTrueFalseTotal
if $required 42 202 244
380 kind => 'server_error', status => $res->code, message => "the answer carried no $wire",
381 ) if $required;
382 202 next;
383 }
384 my $valid = $type eq 'string' ? _is_string($value)
385 : $type eq 'int' ? !ref $value && !_is_string($value) && $value =~ /\A-?[0-9]+\z/
386 : $type eq 'bool' ? Scalar::Util::blessed($value) && $value->isa('JSON::PP::Boolean')
387 290 ▶ : ref $value eq 'ARRAY' && !grep { !_is_string($_) } @$value;
Branch: 3 branches
BranchTrueFalseTotal
$type eq 'string' ? : 186 104 290
$type eq 'int' ? : 68 36 104
$type eq 'bool' ? : 4 32 36
Condition66%8/12
not ref $value || _is_string($value) and $value =~ /\A-?[0-9]+\z/
!ll&&!rl&&r
2066
ref $value || _is_string($value)
l!l&&r!l&&!r
0266
builtin::blessed($value) and $value->isa("JSON::PP::Boolean")
!ll&&!rl&&r
004
ref $value eq "ARRAY" and not grep({not _is_string($_);} @$value)
!ll&&!rl&&r
1130
MC/DC33%1/3
not ref $value || _is_string($value) and $value =~ /\A-?[0-9]+\z/
ref $value _is_string($value) $value =~ /\A-?[0-9]+\z/
MC/DC0%0/2
builtin::blessed($value) and $value->isa("JSON::PP::Boolean")
builtin::blessed($value) $value->isa("JSON::PP::Boolean")
MC/DC100%2/2
ref $value eq "ARRAY" and not grep({not _is_string($_);} @$value)
ref $value eq "ARRAY" not grep({not _is_string($_);} @$value)
Truth table50%2/4
not(A || B) and C
A= ref $valueB= _is_string($value)C= $value =~ /\A-?[0-9]+\z/
ABCresult
1XX0
01X0
0000
0011
Truth table33%1/3
A and B
A= builtin::blessed($value)B= $value->isa("JSON::PP::Boolean")
ABresult
0X0
100
111
Truth table100%3/3
A and not(B)
A= ref $value eq "ARRAY"B= not grep({not _is_string($_);} @$value)
ABresult
0X0
100
111
388 290 ▶ die VPNDetection::Error->new(
Branch: 1 branch
BranchTrueFalseTotal
unless $valid 5 285 290
389 kind => 'server_error', status => $res->code, message => "the answer's $wire is not a $type",
390 ) unless $valid;
391 285 ▶ $present{ defined $name ? $name : $wire } = $type eq 'bool' ? ($value ? 1 : 0) : $value;
Branch: 3 branches
BranchTrueFalseTotal
$type eq 'bool' ? : 4 281 285
$value ? : 4 0 4
defined $name ? : 3 282 285
392 }
393 44 return \%present;
394 }
395  
396 # A JSON string rather than a number: Mojo::JSON decodes the two into different
397 # kinds of scalar, and nothing else in Perl tells `"7"` from `7`.
398 34398 ▶sub _is_string {
Subroutine _is_string: called CC=5 SCAR=16.1
399 34398 my ($value) = @_;
400 34398 ▶ return 0 if !defined $value || ref $value;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $value or ref $value 17 34381 34398
Condition100%3/3
not defined $value or ref $value
l!l&&r!l&&!r
16134381
MC/DC100%2/2
not defined $value or ref $value
not defined $value ref $value
Truth table100%3/3
not(A) or B
A= not defined $valueB= ref $value
ABresult
1X1
011
000
401 34381 my $flags = B::svref_2object(\$value)->FLAGS;
402 34381 ▶ return ($flags & B::SVp_POK) && !($flags & (B::SVp_IOK | B::SVp_NOK)) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$flags & 16384 && !($flags & 12288) ? : 34116 265 34381
Condition100%3/3
$flags & 16384 and not $flags & 12288
!ll&&!rl&&r
264134116
MC/DC100%2/2
$flags & 16384 and not $flags & 12288
$flags & 16384 not $flags & 12288
Truth table100%3/3
A and not(B)
A= $flags & 16384B= not $flags & 12288
ABresult
0X0
100
111
403 }
404  
405 5 ▶sub _challenge {
Subroutine _challenge: called CC=1 SCAR=0.0
406 5 my ($verifier) = @_;
407 5 return MIME::Base64::encode_base64url(Digest::SHA::sha256(Encode::encode('UTF-8', $verifier)));
408 }
409  
410 # A value with no UTF-8 at all, such as a lone surrogate, is refused rather than
411 # sent in perl's own lax encoding.
412 42 ▶sub _percent_encode {
Subroutine _percent_encode: called CC=2 SCAR=6.9
413 42 my ($method, $name, $value) = @_;
414 42 my $bytes = eval { Encode::encode('UTF-8', $value, Encode::FB_CROAK | Encode::LEAVE_SRC) };
415 42 ▶ Carp::croak("VPNDetection::oauth->$method: $name has no UTF-8 form") unless defined $bytes;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $bytes 1 41 42
416 41 $bytes =~ s/([^A-Za-z0-9\-._~])/sprintf('%%%02X', ord $1)/ge;
417 41 return $bytes;
418 }
419  
420 # Mojo's JSON generator would send a lone surrogate in perl's own lax encoding.
421 35 ▶sub _assert_utf8 {
Subroutine _assert_utf8: called CC=2 SCAR=6.9
422 35 my ($method, $name, $value) = @_;
423 35 my $bytes = eval { Encode::encode('UTF-8', $value, Encode::FB_CROAK | Encode::LEAVE_SRC) };
424 35 ▶ Carp::croak("VPNDetection::oauth->$method: $name has no UTF-8 form") unless defined $bytes;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $bytes 2 33 35
425 }
426  
427 177 ▶sub _assert_argument {
Subroutine _assert_argument: called CC=5 SCAR=16.1
428 177 my ($method, %arguments) = @_;
429 177 for my $name (sort keys %arguments) {
430 269 my $value = $arguments{$name};
431 269 ▶ Carp::croak("VPNDetection::oauth->$method: expected $name as a string")
Branch: 1 branch
BranchTrueFalseTotal
if not defined $value or ref $value or not length $value 7 262 269
Condition100%6/6
not defined $value or ref $value or not length $value
l!l&&r!l&&!r
34262
not defined $value or ref $value
l!l&&r!l&&!r
21266
MC/DC100%3/3
not defined $value or ref $value or not length $value
not defined $value ref $value not length $value
Truth table100%4/4
not(A) or B or not(C)
A= not defined $valueB= ref $valueC= not length $value
ABCresult
1XX1
01X1
0011
0000
432 if !defined $value || ref $value || !length $value;
433 }
434 }
435  
436 169 ▶sub _assert_blocking_ok {
Subroutine _assert_blocking_ok: called CC=1 SCAR=0.0
437 169 my ($self, $method) = @_;
438 169 $self->{client}->_assert_blocking_ok("oauth->$method");
439 }
440  
441 7 1;
442  
443 __END__
« VPNDetection.pm ↑ Summary VPNDetection/Error.pm »