OAuth/ResourceClient.pm

filter: Statementstmt 94.5% 87 / 92 Branchbran 76.6% 23 / 30 Conditioncond 52.7% 19 / 36 MC/DCmcdc 23.8% 5 / 21 Subroutinesub 90.0% 18 / 20 Podpod 100% 2 / 2 totaltotal 76.6% 154 / 201 CC 33 SCAR 20.1
CC
33
Cov
82%
_error_message
14.9
_request_with_session
12.1
__ANON__
9.6
CRAP 7.5
« OAuth/Identity.pm ↑ Summary OAuth.pm »
1 package
2 Mojo::ATProto::OAuth::ResourceClient;
3 7 ▶use Mojo::Base -base, -signatures;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 7 ▶use Mojo::ATProto::OAuth::DPoP qw//;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 7 ▶use Mojo::URL qw//;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 ▶use Mojo::UserAgent qw//;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use Mojo::Log qw//;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶use Mojo::Promise qw//;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 7 ▶use feature 'try';
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 7 ▶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
07
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
14  
15 7 our $VERSION = '1.03'; # VERSION
16  
17 0 ▶has 'oauth' => sub { die "oauth is required\n" }; # Mojo::ATProto::OAuth instance - only ->store, ->refresh_tokens(_p) and ->_update_stored_session(_p) are used
Subroutine __ANON__: not called CC=1 SCAR=1.2
18 10 ▶has 'ua' => sub($self) { $self->oauth->ua };
Subroutine __ANON__: called CC=1 SCAR=0.0
19 0 ▶has 'log' => sub($self) { $self->oauth->log };
Subroutine __ANON__: not called CC=1 SCAR=1.2
20  
21 # Sends an authenticated XRPC request against $did's stored session's own
22 # PDS (host_url), handling DPoP nonce rotation and access-token refresh
23 # transparently. $method is a lowercase Mojo::UserAgent verb ('get',
24 # 'post', ...), $path is the XRPC path (and query string, if any) to
25 # append to host_url, $body (if given) is sent as a JSON request body.
26 # Returns the decoded JSON response. Dies on a non-2xx response that
27 # isn't recovered by a nonce/refresh retry - see _request_with_session.
28 8 ▶sub request($self, $did, $session_id, $method, $path, $body = undef) {
Subroutine request: called CC=2 SCAR=6.9
Pod documented
29 8 my $session = $self->oauth->store->get_session($did, $session_id);
30 8 return $self->_request_with_session($session, $method, $path, $body, 1, 1);
31 }
32  
33 4 ▶sub request_p($self, $did, $session_id, $method, $path, $body = undef) {
Subroutine request_p: called CC=2 SCAR=6.9
Pod documented
34 4 ▶ return $self->oauth->store->get_session_p($did, $session_id)->then(sub($session) {
Subroutine __ANON__: called CC=1 SCAR=0.0
35 4 return $self->_request_with_session_p($session, $method, $path, $body, 1, 1);
36 4 });
37 }
38  
39 # DPoP nonce rotation (RFC 9449): a 401 accompanied by a fresh
40 # DPoP-Nonce response header means "retry with this nonce", not a real
41 # auth failure - bounded by $nonce_retries_left. A 401 with no fresh
42 # nonce means the access token itself needs refreshing, via
43 # $self->oauth->refresh_tokens(_p) (which persists the refreshed session
44 # itself) - bounded by $refresh_retries_left. Either bound reaching 0
45 # means a persistently-failing session dies cleanly instead of looping.
46 11 ▶sub _request_with_session($self, $session, $method, $path, $body, $nonce_retries_left, $refresh_retries_left) {
Subroutine _request_with_session: called CC=12 SCAR=24.9
47 11 my $url = Mojo::URL->new($session->{host_url})->path($path);
48 11 my $dpop_key = Mojo::ATProto::OAuth::DPoP->import_private_pem($session->{dpop_private_key_pem});
49 my $dpop_jwt = Mojo::ATProto::OAuth::DPoP->proof(
50 key => $dpop_key, method => $method, url => $url->to_string,
51 nonce => $session->{dpop_host_nonce}, access_token => $session->{access_token},
52 issuer => $session->{auth_server_url},
53 11 );
54 11 $self->log->debug("request: $method $url") if DEBUG;
55  
56 11 my $headers = {Authorization => 'DPoP ' . $session->{access_token}, DPoP => $dpop_jwt};
57 11 ▶ my @extra = defined($body) ? (json => $body) : ();
Branch: 1 branch
BranchTrueFalseTotal
defined $body ? : 2 9 11
58 11 my $tx = $self->ua->$method($url, $headers, @extra);
59 11 my $res = $tx->res;
60  
61 # The resource server can rotate the DPoP nonce on any response, not
62 # just a 401 - persist it either way so the next call anywhere starts
63 # from the freshest known nonce. Only the nonce is written: $session
64 # was read before this request, and writing all of it back would
65 # overwrite tokens another process may have refreshed since.
66 11 ▶ my $new_nonce = $res->headers->header('DPoP-Nonce') // '';
Condition100%2/2
$res->headers->header("DPoP-Nonce") // ""
l!l
38
MC/DC100%1/1
$res->headers->header("DPoP-Nonce") // ""
$res->headers->header("DPoP-Nonce")
Truth table100%2/2
A
A= $res->headers->header("DPoP-Nonce")
Aresult
11
00
67 11 ▶ if (length($new_nonce) && $new_nonce ne ($session->{dpop_host_nonce} // '')) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $new_nonce and $new_nonce ne ($session->{"dpop_host_nonce"} // "")) 3 8 11
Condition60%3/5
length $new_nonce and $new_nonce ne ($session->{"dpop_host_nonce"} // "")
!ll&&!rl&&r
803
$session->{"dpop_host_nonce"} // ""
l!l
30
MC/DC50%1/2
length $new_nonce and $new_nonce ne ($session->{"dpop_host_nonce"} // "")
length $new_nonce $new_nonce ne ($session->{"dpop_host_nonce"} // "")
MC/DC0%0/1
$session->{"dpop_host_nonce"} // ""
$session->{"dpop_host_nonce"}
Truth table66%2/3
A and B
A= length $new_nonceB= $new_nonce ne ($session->{"dpop_host_nonce"} // "")
ABresult
0X0
100
111
Truth table50%1/2
A
A= $session->{"dpop_host_nonce"}
Aresult
11
00
68 3 $session->{dpop_host_nonce} = $new_nonce;
69 3 $self->oauth->_update_stored_session($session->{account_did}, $session->{session_id}, {dpop_host_nonce => $new_nonce});
70 }
71  
72 11 ▶ if (($res->code // 0) == 401) {
Branch: 1 branch
BranchTrueFalseTotal
if (($res->code // 0) == 401) 4 7 11
Condition50%1/2
$res->code // 0
l!l
110
MC/DC0%0/1
$res->code // 0
$res->code
Truth table50%1/2
A
A= $res->code
Aresult
11
00
73 4 ▶ if (length($new_nonce) && $nonce_retries_left > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $new_nonce and $nonce_retries_left > 0) 1 3 4
Condition66%2/3
length $new_nonce and $nonce_retries_left > 0
!ll&&!rl&&r
301
MC/DC50%1/2
length $new_nonce and $nonce_retries_left > 0
length $new_nonce $nonce_retries_left > 0
Truth table66%2/3
A and B
A= length $new_nonceB= $nonce_retries_left > 0
ABresult
0X0
100
111
74 1 $self->log->debug('request: retrying with fresh DPoP-Nonce') if DEBUG;
75 1 return $self->_request_with_session($session, $method, $path, $body, $nonce_retries_left - 1, $refresh_retries_left);
76 }
77 3 ▶ if ($refresh_retries_left > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($refresh_retries_left > 0) 2 1 3
78 2 $self->log->debug('request: refreshing access token and retrying') if DEBUG;
79 2 $session = $self->oauth->refresh_tokens($session);
80 2 return $self->_request_with_session($session, $method, $path, $body, $nonce_retries_left, $refresh_retries_left - 1);
81 }
82 1 die "request failed (HTTP 401): session could not be refreshed\n";
83 }
84  
85 7 ▶ die $self->_error_message($tx, $res) unless $res->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->is_success 1 6 7
86 6 return $res->json;
87 }
88  
89 6 ▶sub _request_with_session_p($self, $session, $method, $path, $body, $nonce_retries_left, $refresh_retries_left) {
Subroutine _request_with_session_p: called CC=2 SCAR=7.2
90 6 my $url = Mojo::URL->new($session->{host_url})->path($path);
91 6 my $dpop_key = Mojo::ATProto::OAuth::DPoP->import_private_pem($session->{dpop_private_key_pem});
92 my $dpop_jwt = Mojo::ATProto::OAuth::DPoP->proof(
93 key => $dpop_key, method => $method, url => $url->to_string,
94 nonce => $session->{dpop_host_nonce}, access_token => $session->{access_token},
95 issuer => $session->{auth_server_url},
96 6 );
97 6 $self->log->debug("request_p: $method $url") if DEBUG;
98  
99 6 my $headers = {Authorization => 'DPoP ' . $session->{access_token}, DPoP => $dpop_jwt};
100 6 ▶ my @extra = defined($body) ? (json => $body) : ();
Branch: 1 branch
BranchTrueFalseTotal
defined $body ? : 0 6 6
101 6 my $ua_method = "${method}_p";
102 6 ▶ return $self->ua->$ua_method($url, $headers, @extra)->then(sub($tx) {
Subroutine __ANON__: called CC=5 SCAR=17.1
103 6 my $res = $tx->res;
104 6 ▶ my $new_nonce = $res->headers->header('DPoP-Nonce') // '';
Condition100%2/2
$res->headers->header("DPoP-Nonce") // ""
l!l
15
MC/DC100%1/1
$res->headers->header("DPoP-Nonce") // ""
$res->headers->header("DPoP-Nonce")
Truth table100%2/2
A
A= $res->headers->header("DPoP-Nonce")
Aresult
11
00
105  
106 6 my $nonce_saved_p = Mojo::Promise->resolve;
107 6 ▶ if (length($new_nonce) && $new_nonce ne ($session->{dpop_host_nonce} // '')) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $new_nonce and $new_nonce ne ($session->{"dpop_host_nonce"} // "")) 1 5 6
Condition60%3/5
length $new_nonce and $new_nonce ne ($session->{"dpop_host_nonce"} // "")
!ll&&!rl&&r
501
$session->{"dpop_host_nonce"} // ""
l!l
10
MC/DC50%1/2
length $new_nonce and $new_nonce ne ($session->{"dpop_host_nonce"} // "")
length $new_nonce $new_nonce ne ($session->{"dpop_host_nonce"} // "")
MC/DC0%0/1
$session->{"dpop_host_nonce"} // ""
$session->{"dpop_host_nonce"}
Truth table66%2/3
A and B
A= length $new_nonceB= $new_nonce ne ($session->{"dpop_host_nonce"} // "")
ABresult
0X0
100
111
Truth table50%1/2
A
A= $session->{"dpop_host_nonce"}
Aresult
11
00
108 1 $session->{dpop_host_nonce} = $new_nonce;
109 1 $nonce_saved_p = $self->oauth->_update_stored_session_p($session->{account_did}, $session->{session_id}, {dpop_host_nonce => $new_nonce});
110 }
111  
112 6 ▶ return $nonce_saved_p->then(sub {
Subroutine __ANON__: called CC=7 SCAR=22.6
113 6 ▶ if (($res->code // 0) == 401) {
Branch: 1 branch
BranchTrueFalseTotal
if (($res->code // 0) == 401) 2 4 6
Condition50%1/2
$res->code // 0
l!l
60
MC/DC0%0/1
$res->code // 0
$res->code
Truth table50%1/2
A
A= $res->code
Aresult
11
00
114 2 ▶ if (length($new_nonce) && $nonce_retries_left > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $new_nonce and $nonce_retries_left > 0) 0 2 2
Condition33%1/3
length $new_nonce and $nonce_retries_left > 0
!ll&&!rl&&r
200
MC/DC0%0/2
length $new_nonce and $nonce_retries_left > 0
length $new_nonce $nonce_retries_left > 0
Truth table33%1/3
A and B
A= length $new_nonceB= $nonce_retries_left > 0
ABresult
0X0
100
111
115 0 $self->log->debug('request_p: retrying with fresh DPoP-Nonce') if DEBUG;
116 0 return $self->_request_with_session_p($session, $method, $path, $body, $nonce_retries_left - 1, $refresh_retries_left);
117 }
118 2 ▶ if ($refresh_retries_left > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($refresh_retries_left > 0) 2 0 2
119 2 $self->log->debug('request_p: refreshing access token and retrying') if DEBUG;
120 2 ▶ return $self->oauth->refresh_tokens_p($session)->then(sub($refreshed) {
Subroutine __ANON__: called CC=1 SCAR=0.0
121 2 return $self->_request_with_session_p($refreshed, $method, $path, $body, $nonce_retries_left, $refresh_retries_left - 1);
122 2 });
123 }
124 0 die "request_p failed (HTTP 401): session could not be refreshed\n";
125 }
126  
127 4 ▶ die $self->_error_message($tx, $res) unless $res->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->is_success 0 4 4
128 4 return $res->json;
129 6 });
130 6 });
131 }
132  
133 # Extracts the XRPC response's machine-readable `error` field (e.g.
134 # 'InvalidSwap') alongside the human-readable `message`, so a caller can
135 # distinguish error *kinds* (e.g. a swapRecord conflict) without needing
136 # a blessed exception type - matches this library's "no models" rule.
137 1 ▶sub _error_message($self, $tx, $res) {
Subroutine _error_message: called CC=8 SCAR=27.0
138 1 my $json_body = eval { $res->json };
139 1 ▶ my $message = (ref($json_body) eq 'HASH' && defined($json_body->{message})) ? $json_body->{message} : ($tx->error->{message} // 'unknown error');
Branch: 1 branch
BranchTrueFalseTotal
ref $json_body eq 'HASH' && defined $json_body->{'message'} ? : 1 0 1
Condition20%1/5
ref $json_body eq "HASH" and defined $json_body->{"message"}
!ll&&!rl&&r
001
$tx->error->{"message"} // "unknown error"
l!l
00
MC/DC0%0/2
ref $json_body eq "HASH" and defined $json_body->{"message"}
ref $json_body eq "HASH" defined $json_body->{"message"}
MC/DC0%0/1
$tx->error->{"message"} // "unknown error"
$tx->error->{"message"}
Truth table33%1/3
A and B
A= ref $json_body eq "HASH"B= defined $json_body->{"message"}
ABresult
0X0
100
111
Truth table0%0/2
A
A= $tx->error->{"message"}
Aresult
11
00
140 1 ▶ my $xrpc_error = (ref($json_body) eq 'HASH' && defined($json_body->{error})) ? $json_body->{error} : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $json_body eq 'HASH' && defined $json_body->{'error'} ? : 1 0 1
Condition33%1/3
ref $json_body eq "HASH" and defined $json_body->{"error"}
!ll&&!rl&&r
001
MC/DC0%0/2
ref $json_body eq "HASH" and defined $json_body->{"error"}
ref $json_body eq "HASH" defined $json_body->{"error"}
Truth table33%1/3
A and B
A= ref $json_body eq "HASH"B= defined $json_body->{"error"}
ABresult
0X0
100
111
141 1 ▶ return 'request failed (HTTP ' . ($res->code // 'connection error')
Branch: 1 branch
BranchTrueFalseTotal
defined $xrpc_error ? : 1 0 1
Condition50%1/2
$res->code // "connection error"
l!l
10
MC/DC0%0/1
$res->code // "connection error"
$res->code
Truth table50%1/2
A
A= $res->code
Aresult
11
00
142 . (defined($xrpc_error) ? ", xrpc_error=$xrpc_error" : '') . "): $message\n";
143 }
144  
145 7 1;
146  
147 __END__
« OAuth/Identity.pm ↑ Summary OAuth.pm »