OAuth/Resolver.pm

filter: Statementstmt 55.2% 58 / 105 Branchbran 73.0% 38 / 52 Conditioncond 33.3% 26 / 78 MC/DCmcdc 13.5% 5 / 37 Subroutinesub 65.0% 13 / 20 Podpod n/a totaltotal 47.9% 140 / 292 CC 58 SCAR 30.7
CC
58
Cov
52%
_extract_auth_server_url
56.0
_validate_auth_server_metadata
30.0
__ANON__
20.0
CRAP 21.5
↑ Summary OAuth/Identity.pm »
1 package
2 Mojo::ATProto::OAuth::Resolver;
3 8 ▶use Mojo::Base -base, -signatures;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 8 ▶use Mojo::UserAgent qw//;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 8 ▶use Mojo::URL qw//;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 8 ▶use Mojo::Log qw//;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 # Resource-server (PDS) -> auth-server discovery, plus auth-server
10 # metadata fetch+validation.
11 8 ▶use constant DEBUG => $ENV{MOJO_OAUTH_DEBUG} || 0;
Subroutine BEGIN: called CC=2 SCAR=7.1
Condition50%1/2
$ENV{'MOJO_OAUTH_DEBUG'} || 0
l!l
08
MC/DC0%0/1
$ENV{'MOJO_OAUTH_DEBUG'} || 0
$ENV{'MOJO_OAUTH_DEBUG'}
Truth table50%1/2
A
A= $ENV{'MOJO_OAUTH_DEBUG'}
Aresult
11
00
12  
13 8 our $VERSION = '1.03'; # VERSION
14  
15 0 ▶has 'ua' => sub($self) {
Subroutine __ANON__: not called CC=2 SCAR=8.7
16 0 my $ua = Mojo::UserAgent->new(request_timeout => 10);
17 8 ▶ no strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 0 ▶ $ua->transactor->name('Mojo::ATProto::OAuth/' . $VERSION || 'dev');
Condition0%0/2
'Mojo::ATProto::OAuth/' . $VERSION || 'dev'
l!l
00
MC/DC0%0/1
'Mojo::ATProto::OAuth/' . $VERSION || 'dev'
'Mojo::ATProto::OAuth/' . $VERSION
Truth table0%0/2
A
A= 'Mojo::ATProto::OAuth/' . $VERSION
Aresult
11
00
19 8 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 0 return $ua;
21 8 };
22 0 ▶has 'log' => sub { Mojo::Log->new };
Subroutine __ANON__: not called CC=1 SCAR=1.2
23  
24 # Resolves a resource-server (PDS) URL to its auth-server URL, via the
25 # protected-resource metadata document. They may be the same server -
26 # atproto PDSes are commonly their own auth server, but don't have to
27 # be (e.g. an entryway-fronted PDS).
28 2 ▶sub resolve_auth_server_url($self, $host_url) {
Subroutine resolve_auth_server_url: called CC=4 SCAR=26.9
29 2 $self->log->debug("Resolver: resolving auth-server URL for $host_url") if DEBUG;
30 2 my $doc_url = $self->_protected_resource_url($host_url);
31 0 my $tx = $self->ua->get($doc_url);
32 0 my $res = $tx->result;
33 0 $self->log->debug('Resolver: protected-resource response status=' . ($res->code // 'connection error')) if DEBUG;
34 0 ▶ die "fetching protected resource document failed: " . ($res->message // 'connection error') . "\n"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $res->code and $res->code == 200 0 0 0
Condition0%0/5
defined $res->code and $res->code == 200
!ll&&!rl&&r
000
$res->message // "connection error"
l!l
00
MC/DC0%0/2
defined $res->code and $res->code == 200
defined $res->code $res->code == 200
MC/DC0%0/1
$res->message // "connection error"
$res->message
Truth table0%0/3
A and B
A= defined $res->codeB= $res->code == 200
ABresult
0X0
100
111
Truth table0%0/2
A
A= $res->message
Aresult
11
00
35 unless defined($res->code) && $res->code == 200;
36 0 my $auth_server_url = $self->_extract_auth_server_url($res->json);
37 0 $self->log->debug("Resolver: $host_url -> auth-server $auth_server_url") if DEBUG;
38 0 return $auth_server_url;
39 }
40  
41 # Non-blocking counterpart
42 0 ▶sub resolve_auth_server_url_p($self, $host_url) {
Subroutine resolve_auth_server_url_p: not called CC=1 SCAR=6.9
43 0 $self->log->debug("Resolver: resolving auth-server URL for $host_url (async)") if DEBUG;
44 0 my $doc_url = $self->_protected_resource_url($host_url);
45 0 ▶ return $self->ua->get_p($doc_url)->then(sub($tx) {
Subroutine __ANON__: not called CC=4 SCAR=30.0
46 0 my $res = $tx->result;
47 0 $self->log->debug('Resolver: protected-resource response status=' . ($res->code // 'connection error')) if DEBUG;
48 0 ▶ die "fetching protected resource document failed: " . ($res->message // 'connection error') . "\n"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $res->code and $res->code == 200 0 0 0
Condition0%0/5
defined $res->code and $res->code == 200
!ll&&!rl&&r
000
$res->message // "connection error"
l!l
00
MC/DC0%0/2
defined $res->code and $res->code == 200
defined $res->code $res->code == 200
MC/DC0%0/1
$res->message // "connection error"
$res->message
Truth table0%0/3
A and B
A= defined $res->codeB= $res->code == 200
ABresult
0X0
100
111
Truth table0%0/2
A
A= $res->message
Aresult
11
00
49 unless defined($res->code) && $res->code == 200;
50 0 my $auth_server_url = $self->_extract_auth_server_url($res->json);
51 0 $self->log->debug("Resolver: $host_url -> auth-server $auth_server_url") if DEBUG;
52 0 return $auth_server_url;
53 0 });
54 }
55  
56 2 ▶sub _protected_resource_url($self, $host_url) {
Subroutine _protected_resource_url: called CC=5 SCAR=18.6
57 2 my $u = Mojo::URL->new($host_url);
58 2 ▶ die "not a valid public host URL: $host_url\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $u->scheme eq "https" and length($u->host // "") and not $u->port 2 0 2
Condition62%5/8
$u->scheme eq "https" and length($u->host // "") and not $u->port
!ll&&!rl&&r
110
$u->scheme eq "https" and length($u->host // "")
!ll&&!rl&&r
101
$u->host // ""
l!l
10
MC/DC0%0/3
$u->scheme eq "https" and length($u->host // "") and not $u->port
$u->scheme eq "https" $u->host not $u->port
Truth table50%2/4
A and B and not(C)
A= $u->scheme eq "https"B= $u->hostC= not $u->port
ABCresult
0XX0
10X0
1100
1111
59 unless $u->scheme eq 'https' && length($u->host // '') && !$u->port;
60 0 return Mojo::URL->new(sprintf('https://%s/.well-known/oauth-protected-resource', $u->host));
61 }
62  
63 0 ▶sub _extract_auth_server_url($self, $body) {
Subroutine _extract_auth_server_url: not called CC=7 SCAR=40.3
64 0 ▶ my @servers = @{$body->{authorization_servers} // []};
Condition0%0/2
$body->{"authorization_servers"} // []
l!l
00
MC/DC0%0/1
$body->{"authorization_servers"} // []
$body->{"authorization_servers"}
Truth table0%0/2
A
A= $body->{"authorization_servers"}
Aresult
11
00
65 0 ▶ die "no auth server URL in protected resource document\n" unless @servers;
Branch: 1 branch
BranchTrueFalseTotal
unless @servers 0 0 0
66  
67 0 my $auth_url = $servers[0];
68 0 my $au = Mojo::URL->new($auth_url);
69 0 ▶ die "not a valid public auth server URL: $auth_url\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $au->scheme eq "https" and length($au->host // "") and not $au->port 0 0 0
Condition0%0/8
$au->scheme eq "https" and length($au->host // "") and not $au->port
!ll&&!rl&&r
000
$au->scheme eq "https" and length($au->host // "")
!ll&&!rl&&r
000
$au->host // ""
l!l
00
MC/DC0%0/3
$au->scheme eq "https" and length($au->host // "") and not $au->port
$au->scheme eq "https" $au->host not $au->port
Truth table0%0/4
A and B and not(C)
A= $au->scheme eq "https"B= $au->hostC= not $au->port
ABCresult
0XX0
10X0
1100
1111
70 unless $au->scheme eq 'https' && length($au->host // '') && !$au->port;
71  
72 0 return $auth_url;
73 }
74  
75 # Resolves an auth-server URL to its full metadata document
76 1 ▶sub resolve_auth_server_metadata($self, $server_url) {
Subroutine resolve_auth_server_metadata: called CC=4 SCAR=27.1
77 1 $self->log->debug("Resolver: fetching auth-server metadata for $server_url") if DEBUG;
78 1 my $doc_url = $self->_auth_server_metadata_url($server_url);
79 0 my $tx = $self->ua->get($doc_url);
80 0 my $res = $tx->result;
81 0 $self->log->debug('Resolver: auth-server metadata response status=' . ($res->code // 'connection error')) if DEBUG;
82 0 ▶ die "fetching auth server metadata failed: " . ($res->message // 'connection error') . "\n"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $res->code and $res->code == 200 0 0 0
Condition0%0/5
defined $res->code and $res->code == 200
!ll&&!rl&&r
000
$res->message // "connection error"
l!l
00
MC/DC0%0/2
defined $res->code and $res->code == 200
defined $res->code $res->code == 200
MC/DC0%0/1
$res->message // "connection error"
$res->message
Truth table0%0/3
A and B
A= defined $res->codeB= $res->code == 200
ABresult
0X0
100
111
Truth table0%0/2
A
A= $res->message
Aresult
11
00
83 unless defined($res->code) && $res->code == 200;
84  
85 0 my $meta = $res->json;
86 0 $self->_validate_auth_server_metadata($meta, $server_url);
87 $self->log->debug("Resolver: auth-server metadata for $server_url validated ok "
88 0 . "(par_endpoint=" . ($meta->{pushed_authorization_request_endpoint} // '?') . ")") if DEBUG;
89 0 return $meta;
90 }
91  
92 0 ▶sub resolve_auth_server_metadata_p($self, $server_url) {
Subroutine resolve_auth_server_metadata_p: not called CC=1 SCAR=6.9
93 0 $self->log->debug("Resolver: fetching auth-server metadata for $server_url (async)") if DEBUG;
94 0 my $doc_url = $self->_auth_server_metadata_url($server_url);
95 0 ▶ return $self->ua->get_p($doc_url)->then(sub($tx) {
Subroutine __ANON__: not called CC=4 SCAR=30.0
96 0 my $res = $tx->result;
97 0 $self->log->debug('Resolver: auth-server metadata response status=' . ($res->code // 'connection error')) if DEBUG;
98 0 ▶ die "fetching auth server metadata failed: " . ($res->message // 'connection error') . "\n"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $res->code and $res->code == 200 0 0 0
Condition0%0/5
defined $res->code and $res->code == 200
!ll&&!rl&&r
000
$res->message // "connection error"
l!l
00
MC/DC0%0/2
defined $res->code and $res->code == 200
defined $res->code $res->code == 200
MC/DC0%0/1
$res->message // "connection error"
$res->message
Truth table0%0/3
A and B
A= defined $res->codeB= $res->code == 200
ABresult
0X0
100
111
Truth table0%0/2
A
A= $res->message
Aresult
11
00
99 unless defined($res->code) && $res->code == 200;
100  
101 0 my $meta = $res->json;
102 0 $self->_validate_auth_server_metadata($meta, $server_url);
103 $self->log->debug("Resolver: auth-server metadata for $server_url validated ok "
104 0 . "(par_endpoint=" . ($meta->{pushed_authorization_request_endpoint} // '?') . ")") if DEBUG;
105 0 return $meta;
106 0 });
107 }
108  
109 1 ▶sub _auth_server_metadata_url($self, $server_url) {
Subroutine _auth_server_metadata_url: called CC=5 SCAR=23.8
110 1 my $u = Mojo::URL->new($server_url);
111 1 ▶ die "not a valid public host URL: $server_url\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $u->scheme eq "https" and length($u->host // "") and not $u->port 1 0 1
Condition25%2/8
$u->scheme eq "https" and length($u->host // "") and not $u->port
!ll&&!rl&&r
100
$u->scheme eq "https" and length($u->host // "")
!ll&&!rl&&r
100
$u->host // ""
l!l
00
MC/DC0%0/3
$u->scheme eq "https" and length($u->host // "") and not $u->port
$u->scheme eq "https" $u->host not $u->port
Truth table25%1/4
A and B and not(C)
A= $u->scheme eq "https"B= $u->hostC= not $u->port
ABCresult
0XX0
10X0
1100
1111
112 unless $u->scheme eq 'https' && length($u->host // '') && !$u->port;
113 0 return Mojo::URL->new(sprintf('https://%s/.well-known/oauth-authorization-server', $u->host));
114 }
115  
116 105 ▶sub _contains($self, $list, $value) {
Subroutine _contains: called CC=2 SCAR=7.1
117 105 ▶ return grep { $_ eq $value } @{$list // []};
Condition50%1/2
$list // []
l!l
1050
MC/DC0%0/1
$list // []
$list
Truth table50%1/2
A
A= $list
Aresult
11
00
118 }
119  
120 20 ▶sub _validate_auth_server_metadata($self, $meta, $server_url) {
Subroutine _validate_auth_server_metadata: called CC=29 SCAR=34.0
121 20 ▶ die "invalid auth server metadata: empty issuer\n" unless length($meta->{issuer} // '');
Branch: 1 branch
BranchTrueFalseTotal
unless length($meta->{"issuer"} // "") 1 19 20
Condition50%1/2
$meta->{"issuer"} // ""
l!l
200
MC/DC0%0/1
$meta->{"issuer"} // ""
$meta->{"issuer"}
Truth table50%1/2
A
A= $meta->{"issuer"}
Aresult
11
00
122  
123 19 my $iss = Mojo::URL->new($meta->{issuer});
124 19 ▶ die "invalid auth server metadata: issuer URL\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $iss->scheme eq "https" and not $iss->port and not length $iss->path->to_string and not length($iss->fragment // "") and not $iss->query->to_string 1 18 19
Condition57%8/14
$iss->scheme eq "https" and not $iss->port and not length $iss->path->to_string and not length($iss->fragment // "") and not $iss->query->to_string
!ll&&!rl&&r
1018
$iss->scheme eq "https" and not $iss->port and not length $iss->path->to_string and not length($iss->fragment // "")
!ll&&!rl&&r
1018
$iss->scheme eq "https" and not $iss->port and not length $iss->path->to_string
!ll&&!rl&&r
0118
$iss->scheme eq "https" and not $iss->port
!ll&&!rl&&r
0019
$iss->fragment // ""
l!l
018
MC/DC20%1/5
$iss->scheme eq "https" and not $iss->port and not length $iss->path->to_string and not length($iss->fragment // "") and not $iss->query->to_string
$iss->scheme eq "https" not $iss->port not length $iss->path->to_string $iss->fragment not $iss->query->to_string
Truth table33%2/6
A and not(B) and not(C) and not(D) and not(E)
A= $iss->scheme eq "https"B= not $iss->portC= not length $iss->path->to_stringD= $iss->fragmentE= not $iss->query->to_string
ABCDEresult
0XXXX0
10XXX0
110XX0
1111X0
111000
111011
125 unless $iss->scheme eq 'https'
126 && !$iss->port
127 && !length($iss->path->to_string)
128 && !length($iss->fragment // '')
129 && !$iss->query->to_string;
130  
131 18 my $srv = Mojo::URL->new($server_url);
132 18 ▶ die "invalid auth server metadata: issuer must match request URL\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $iss->scheme eq $srv->scheme and $iss->host eq $srv->host 1 17 18
Condition66%2/3
$iss->scheme eq $srv->scheme and $iss->host eq $srv->host
!ll&&!rl&&r
0117
MC/DC50%1/2
$iss->scheme eq $srv->scheme and $iss->host eq $srv->host
$iss->scheme eq $srv->scheme $iss->host eq $srv->host
Truth table66%2/3
A and B
A= $iss->scheme eq $srv->schemeB= $iss->host eq $srv->host
ABresult
0X0
100
111
133 unless $iss->scheme eq $srv->scheme && $iss->host eq $srv->host;
134  
135 17 ▶ my $ae = Mojo::URL->new($meta->{authorization_endpoint} // '');
Condition50%1/2
$meta->{"authorization_endpoint"} // ""
l!l
170
MC/DC0%0/1
$meta->{"authorization_endpoint"} // ""
$meta->{"authorization_endpoint"}
Truth table50%1/2
A
A= $meta->{"authorization_endpoint"}
Aresult
11
00
136 17 ▶ die "invalid auth server metadata: invalid authorization_endpoint\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $ae->scheme eq "https" 1 16 17
137 unless $ae->scheme eq 'https';
138  
139 die "invalid auth server metadata: response_types_supported must include 'code'\n"
140 16 ▶ unless $self->_contains($meta->{response_types_supported}, 'code');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_contains($meta->{"response_types_supported"}, "code") 1 15 16
141 die "invalid auth server metadata: grant_types_supported must include 'authorization_code'\n"
142 15 ▶ unless $self->_contains($meta->{grant_types_supported}, 'authorization_code');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_contains($meta->{"grant_types_supported"}, "authorization_code") 1 14 15
143 die "invalid auth server metadata: grant_types_supported must include 'refresh_token'\n"
144 14 ▶ unless $self->_contains($meta->{grant_types_supported}, 'refresh_token');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_contains($meta->{"grant_types_supported"}, "refresh_token") 1 13 14
145 die "invalid auth server metadata: code_challenge_methods_supported must include 'S256'\n"
146 13 ▶ unless $self->_contains($meta->{code_challenge_methods_supported}, 'S256');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_contains($meta->{"code_challenge_methods_supported"}, "S256") 1 12 13
147 die "invalid auth server metadata: token_endpoint_auth_methods_supported must include 'none'\n"
148 12 ▶ unless $self->_contains($meta->{token_endpoint_auth_methods_supported}, 'none');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_contains($meta->{"token_endpoint_auth_methods_supported"}, "none") 1 11 12
149 die "invalid auth server metadata: token_endpoint_auth_methods_supported must include 'private_key_jwt'\n"
150 11 ▶ unless $self->_contains($meta->{token_endpoint_auth_methods_supported}, 'private_key_jwt');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_contains($meta->{"token_endpoint_auth_methods_supported"}, "private_key_jwt") 1 10 11
151 die "invalid auth server metadata: token_endpoint_auth_signing_alg_values_supported must include 'ES256'\n"
152 10 ▶ unless $self->_contains($meta->{token_endpoint_auth_signing_alg_values_supported}, 'ES256');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_contains($meta->{"token_endpoint_auth_signing_alg_values_supported"}, "ES256") 1 9 10
153 die "invalid auth server metadata: scopes_supported must include 'atproto'\n"
154 9 ▶ unless $self->_contains($meta->{scopes_supported}, 'atproto');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_contains($meta->{"scopes_supported"}, "atproto") 1 8 9
155 die "invalid auth server metadata: authorization_response_iss_parameter_supported must be true\n"
156 8 ▶ unless $meta->{authorization_response_iss_parameter_supported};
Branch: 1 branch
BranchTrueFalseTotal
unless $meta->{"authorization_response_iss_parameter_supported"} 1 7 8
157 die "invalid auth server metadata: require_pushed_authorization_requests must be true\n"
158 7 ▶ unless $meta->{require_pushed_authorization_requests};
Branch: 1 branch
BranchTrueFalseTotal
unless $meta->{"require_pushed_authorization_requests"} 1 6 7
159 die "invalid auth server metadata: pushed_authorization_request_endpoint is required\n"
160 6 ▶ unless length($meta->{pushed_authorization_request_endpoint} // '');
Branch: 1 branch
BranchTrueFalseTotal
unless length($meta->{"pushed_authorization_request_endpoint"} // "") 1 5 6
Condition100%2/2
$meta->{"pushed_authorization_request_endpoint"} // ""
l!l
51
MC/DC100%1/1
$meta->{"pushed_authorization_request_endpoint"} // ""
$meta->{"pushed_authorization_request_endpoint"}
Truth table100%2/2
A
A= $meta->{"pushed_authorization_request_endpoint"}
Aresult
11
00
161 die "invalid auth server metadata: dpop_signing_alg_values_supported must include 'ES256'\n"
162 5 ▶ unless $self->_contains($meta->{dpop_signing_alg_values_supported}, 'ES256');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_contains($meta->{"dpop_signing_alg_values_supported"}, "ES256") 1 4 5
163 die "invalid auth server metadata: require_request_uri_registration must be undefined or true\n"
164 4 ▶ if exists($meta->{require_request_uri_registration}) && !$meta->{require_request_uri_registration};
Branch: 1 branch
BranchTrueFalseTotal
if exists $meta->{"require_request_uri_registration"} and not $meta->{"require_request_uri_registration"} 1 3 4
Condition100%3/3
exists $meta->{"require_request_uri_registration"} and not $meta->{"require_request_uri_registration"}
!ll&&!rl&&r
211
MC/DC100%2/2
exists $meta->{"require_request_uri_registration"} and not $meta->{"require_request_uri_registration"}
exists $meta->{"require_request_uri_registration"} not $meta->{"require_request_uri_registration"}
Truth table100%3/3
A and not(B)
A= exists $meta->{"require_request_uri_registration"}B= not $meta->{"require_request_uri_registration"}
ABresult
0X0
100
111
165 die "invalid auth server metadata: client_id_metadata_document_supported must be true\n"
166 3 ▶ unless $meta->{client_id_metadata_document_supported};
Branch: 1 branch
BranchTrueFalseTotal
unless $meta->{"client_id_metadata_document_supported"} 1 2 3
167  
168 2 return 1;
169 }
170  
171 8 1;
↑ Summary OAuth/Identity.pm »