InternetData/Oauth.pm

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