OAuth.pm

filter: Statementstmt 92.3% 400 / 433 Branchbran 67.9% 121 / 178 Conditioncond 54.8% 74 / 135 MC/DCmcdc 23.9% 17 / 71 Subroutinesub 86.8% 86 / 99 Podpod 100% 17 / 17 totaltotal 76.6% 715 / 933 CC 150 SCAR 19.9
CC
150
Cov
80%
_describe_start_opts
56.0
_validate_callback_params
22.5
_resolve_start_p
17.9
CRAP 7.3
« OAuth/ResourceClient.pm ↑ Summary OAuth/DPoP.pm »
1 package Mojo::ATProto::OAuth;
2 7 ▶use Mojo::Base -base, -signatures;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 7 ▶use Mojo::UserAgent qw//;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 7 ▶use Mojo::URL qw//;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 7 ▶use Mojo::Promise qw//;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 ▶use Mojo::Log qw//;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use Mojo::Loader qw/load_class/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶use Mojo::ATProto::OAuth::ClientMetadata qw//;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 7 ▶use Mojo::ATProto::OAuth::DPoP qw//;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 7 ▶use Mojo::ATProto::OAuth::Identity qw//;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 7 ▶use Mojo::ATProto::OAuth::Resolver qw//;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 7 ▶use Mojo::ATProto::OAuth::ResourceClient qw//;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 7 ▶use Scalar::Util qw/blessed/;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 7 ▶use feature 'try';
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 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
20  
21 7 our $VERSION = '1.03'; # VERSION
22  
23 0 ▶has 'ua' => sub {
Subroutine __ANON__: not called CC=2 SCAR=8.7
24 0 my $ua = Mojo::UserAgent->new(request_timeout => 10);
25 7 ▶ no strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 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
27 7 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 0 return $ua;
29 7 };
30 0 ▶has 'client_id' => sub { die "client_id is required\n" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
31 0 ▶has 'callback_url' => sub { die "callback_url is required\n" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
32 7 has 'private_key' => undef; # Crypt::PK::ECC, confidential clients only
33 7 has 'key_id' => undef;
34 7 has 'loopback' => 0; # true for new_localhost() clients - see below
35 0 ▶has 'identity' => sub { Mojo::ATProto::OAuth::Identity->new };
Subroutine __ANON__: not called CC=1 SCAR=1.2
36 0 ▶has 'resolver' => sub { Mojo::ATProto::OAuth::Resolver->new };
Subroutine __ANON__: not called CC=1 SCAR=1.2
37 0 ▶has 'log' => sub { Mojo::Log->new(level => $ENV{MOJO_LOG_LEVEL} || 'info') };
Subroutine __ANON__: not called CC=2 SCAR=13.0
Condition0%0/2
$ENV{'MOJO_LOG_LEVEL'} || 'info'
l!l
00
MC/DC0%0/1
$ENV{'MOJO_LOG_LEVEL'} || 'info'
$ENV{'MOJO_LOG_LEVEL'}
Truth table0%0/2
A
A= $ENV{'MOJO_LOG_LEVEL'}
Aresult
11
00
38 1 ▶has 'client' => sub($self) { Mojo::ATProto::OAuth::ResourceClient->new(oauth => $self) };
Subroutine __ANON__: called CC=1 SCAR=0.0
39  
40 39 ▶sub is_confidential ($self) {
Subroutine is_confidential: called CC=2 SCAR=7.2
Pod documented
41 39 ▶ return defined($self->private_key) && defined($self->key_id);
Condition66%2/3
defined $self->private_key && defined $self->key_id
!ll&&!rl&&r
3702
MC/DC50%1/2
defined $self->private_key && defined $self->key_id
defined $self->private_key defined $self->key_id
Truth table66%2/3
A && B
A= defined $self->private_keyB= defined $self->key_id
ABresult
0X0
100
111
42 }
43  
44 135 ▶sub store ($self, @value) {
Subroutine store: called CC=10 SCAR=23.0
Pod documented
45 # this is basically straight up setter
46 135 ▶ if (@value) {
Branch: 1 branch
BranchTrueFalseTotal
if (@value) 2 133 135
47 2 $self->{store} = $value[0];
48 2 return $self;
49 }
50  
51 133 my $store = $self->{store};
52 133 ▶ return undef unless defined $store; # no store supplied, at all, by symbolic name, module name, or actual instantiated class
Branch: 1 branch
BranchTrueFalseTotal
unless defined $store 2 131 133
53 131 ▶ return $store if blessed($store); # if it's an instantiated class, return it (getter)
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($store) 120 11 131
54  
55 # and here we come to the meat and veg
56 11 my $class;
57 my @args;
58 11 ▶ if(ref($store) eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $store eq 'ARRAY') { } 6 5 11
elsif (ref $store) { } 1 4 5
59 # first element will be the class , the rest will be arguments -
60 # list-assign rather than shift(@$store), since $store is the
61 # same arrayref the caller handed us (not a copy) - shifting it
62 # would mutate their spec in place, corrupting it for reuse
63 6 my ($temp, @rest) = @$store;
64 6 ▶ $class = ($temp =~ /::/) ? $temp : sprintf('Mojo::ATProto::OAuth::SessionStore::%s', $temp);
Branch: 1 branch
BranchTrueFalseTotal
$temp =~ /::/u ? : 1 5 6
65 6 @args = @rest;
66 } elsif(ref($store)) {
67 # not the right kind of ref, time to blow up
68 1 die 'store must be a scalar or arrayref', "\n";
69 } else {
70 # scalar
71 4 ▶ $class = ($store =~ /::/) ? $store : sprintf('Mojo::ATProto::OAuth::SessionStore::%s', $store);
Branch: 1 branch
BranchTrueFalseTotal
$store =~ /::/u ? : 0 4 4
72 }
73 10 my $e = load_class($class);
74 10 ▶ if ($e) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e) 1 9 10
75 # Mojo::Loader::load_class returns a plain true value (not a
76 # ref) for "no such class", and a Mojo::Exception (a ref) only
77 # for a real error (e.g. a syntax error) inside a class that
78 1 ▶ my $reason = ref($e) ? "$e" : 'no such session store class';
Branch: 1 branch
BranchTrueFalseTotal
ref $e ? : 0 1 1
79 1 die "store: could not load session store class $class: $reason\n";
80 }
81 9 return $self->{store} = $class->new(@args);
82 }
83  
84 # Custom accessor (rather than a plain `has`) for the same reason as
85 # `store` above - Mojo::Base's constructor bypasses accessor methods
86 # entirely, so a raw un-normalized value can land in $self->{scopes} via
87 # `->new(scopes => ...)` as easily as via `->scopes(...)`; normalizing
88 # only here, in the getter, covers both regardless of how it got set.
89 19 ▶sub scopes ($self, @value) {
Subroutine scopes: called CC=3 SCAR=11.0
Pod documented
90 19 ▶ if (@value) {
Branch: 1 branch
BranchTrueFalseTotal
if (@value) 1 18 19
91 1 $self->{scopes} = $value[0];
92 1 return $self;
93 }
94 18 ▶ return _normalize_scopes($self->{scopes}) // ['atproto'];
Condition100%2/2
_normalize_scopes($self->{"scopes"}) // ["atproto"]
l!l
171
MC/DC100%1/1
_normalize_scopes($self->{"scopes"}) // ["atproto"]
_normalize_scopes($self->{"scopes"})
Truth table100%2/2
A
A= _normalize_scopes($self->{"scopes"})
Aresult
11
00
95 }
96  
97 # Accepts a scopes value in either form this module accepts - an
98 # arrayref of individual scope tokens (['atproto', 'account:email']) or
99 # a single space-separated string ('atproto account:email') - and
100 # normalizes it to the arrayref-of-tokens form every other part of this
101 # module expects (join(' ', @$scopes) for wire transmission, per-token
102 # union/dedup for scope upgrades). undef stays undef, so callers can
103 # keep using `//` for their own default.
104 38 ▶sub _normalize_scopes($scopes) {
Subroutine _normalize_scopes: called CC=3 SCAR=11.0
105 38 ▶ return undef unless defined $scopes;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $scopes 14 24 38
106 24 ▶ return $scopes if ref($scopes) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $scopes eq "ARRAY" 19 5 24
107 5 return [split(' ', $scopes)];
108 }
109  
110 3 ▶sub new_localhost ($class, %args) {
Subroutine new_localhost: called CC=5 SCAR=16.8
Pod documented
111 3 ▶ my $callback_url = $args{callback_url} // die "new_localhost: 'callback_url' required\n";
Condition50%1/2
$args{"callback_url"} // die("new_localhost: 'callback_url' required\n")
l!l
30
MC/DC0%0/1
$args{"callback_url"} // die("new_localhost: 'callback_url' required\n")
$args{"callback_url"}
Truth table50%1/2
A
A= $args{"callback_url"}
Aresult
11
00
112 3 ▶ my $scopes = _normalize_scopes($args{scopes}) // ['atproto'];
Condition50%1/2
_normalize_scopes($args{"scopes"}) // ["atproto"]
l!l
30
MC/DC0%0/1
_normalize_scopes($args{"scopes"}) // ["atproto"]
_normalize_scopes($args{"scopes"})
Truth table50%1/2
A
A= _normalize_scopes($args{"scopes"})
Aresult
11
00
113  
114 3 my $client_id = Mojo::URL->new('http://localhost')->query({
115 redirect_uri => $callback_url,
116 scope => join(' ', @$scopes),
117 })->to_string;
118  
119 return $class->new(
120 client_id => $client_id,
121 callback_url => $callback_url,
122 scopes => $scopes,
123 loopback => 1,
124 (defined($args{ua}) ? (ua => $args{ua}) : ()),
125 3 ▶ (defined($args{store}) ? (store => $args{store}) : ()),
Branch: 2 branches
BranchTrueFalseTotal
defined $args{'ua'} ? : 1 2 3
defined $args{'store'} ? : 0 3 3
126 );
127 }
128  
129 1 ▶sub client_metadata ($self) {
Subroutine client_metadata: called CC=3 SCAR=17.3
Pod documented
130 1 ▶ die "client_metadata: loopback clients don't serve a client metadata document\n" if $self->loopback;
Branch: 1 branch
BranchTrueFalseTotal
if $self->loopback 1 0 1
131  
132 0 ▶ return Mojo::ATProto::OAuth::ClientMetadata->build(
Branch: 1 branch
BranchTrueFalseTotal
$self->is_confidential ? : 0 0 0
133 client_id => $self->client_id,
134 callback_url => $self->callback_url,
135 scopes => $self->scopes,
136 ($self->is_confidential ? (private_key => $self->private_key, key_id => $self->key_id) : ()),
137 );
138 }
139  
140 # The `client_assertion_type`/`client_assertion` form fields every
141 # confidential-client request (PAR, token exchange, refresh) needs -
142 # empty for a public client, so callers can unconditionally merge this
143 # in rather than each repeating an `if ($self->is_confidential)` guard.
144 37 ▶sub _client_assertion_params ($self, $audience) {
Subroutine _client_assertion_params: called CC=2 SCAR=6.9
145 37 ▶ return {} unless $self->is_confidential;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_confidential 36 1 37
146 return {
147 1 client_assertion_type => 'urn:ietf:params:oauth:client-assertion-type:jwt-bearer',
148 client_assertion => Mojo::ATProto::OAuth::DPoP->client_assertion(
149 key => $self->private_key,
150 key_id => $self->key_id,
151 client_id => $self->client_id,
152 audience => $audience,
153 ),
154 };
155 }
156  
157 # Builds the PAR (Pushed Authorization Request) form body, minus the
158 # DPoP proof (added per-attempt by the caller, since the DPoP nonce can
159 # change between attempts).
160 17 ▶sub _par_body ($self, $auth_meta, $state, $code_challenge, $scopes, $login_hint) {
Subroutine _par_body: called CC=3 SCAR=11.0
161 my $body = {
162 client_id => $self->client_id,
163 state => $state,
164 redirect_uri => $self->callback_url,
165 scope => join(' ', @$scopes),
166 response_type => 'code',
167 code_challenge => $code_challenge,
168 code_challenge_method => 'S256',
169 17 %{$self->_client_assertion_params($auth_meta->{issuer})},
170 };
171 17 ▶ $body->{login_hint} = $login_hint if length($login_hint // '');
Branch: 1 branch
BranchTrueFalseTotal
if length($login_hint // "") 11 6 17
Condition100%2/2
$login_hint // ""
l!l
116
MC/DC100%1/1
$login_hint // ""
$login_hint
Truth table100%2/2
A
A= $login_hint
Aresult
11
00
172  
173 17 return $body;
174 }
175  
176 9 ▶sub _parse_auth_error_reason ($self, $res) {
Subroutine _parse_auth_error_reason: called CC=3 SCAR=12.0
177 9 try {
178 9 my $body = $res->json;
179 9 ▶ return 'unknown' unless ref($body) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $body eq "HASH" 0 9 9
180 9 ▶ return $body->{error} // 'unknown';
Condition50%1/2
$body->{"error"} // "unknown"
l!l
90
MC/DC0%0/1
$body->{"error"} // "unknown"
$body->{"error"}
Truth table50%1/2
A
A= $body->{"error"}
Aresult
11
00
181 } catch($ex) {
182 0 return 'unknown (exception decoding body)';
183 }
184 }
185  
186 # Shared DPoP-nonce-retry POST helper (RFC 9449: the first attempt with
187 # no known nonce is expected to be rejected with a fresh one to retry
188 # with) - used by both send_auth_request (PAR) and
189 # send_initial_token_request (token exchange), matching indigo's own
190 # identical 2-attempt loop in both SendAuthRequest and
191 # SendInitialTokenRequest. Returns ($res, $final_dpop_nonce); does not
192 # itself validate the final response status - callers differ on what a
193 # success response looks like (200 vs. 200/201).
194 24 ▶sub _post_dpop_retry($self, %args) {
Subroutine _post_dpop_retry: called CC=9 SCAR=22.0
195 24 my $url = $args{url};
196 24 my $body = $args{body};
197 24 my $key = $args{key};
198 24 ▶ my $nonce = $args{nonce} // '';
Condition100%2/2
$args{"nonce"} // ""
l!l
1113
MC/DC100%1/1
$args{"nonce"} // ""
$args{"nonce"}
Truth table100%2/2
A
A= $args{"nonce"}
Aresult
11
00
199 24 ▶ my $label = $args{label} // 'request';
Condition50%1/2
$args{"label"} // "request"
l!l
240
MC/DC0%0/1
$args{"label"} // "request"
$args{"label"}
Truth table50%1/2
A
A= $args{"label"}
Aresult
11
00
200  
201 24 my $res;
202 24 for my $attempt (1 .. 2) {
203 27 $self->log->debug("$label: attempt $attempt POST $url (nonce=" . (length($nonce) ? 'yes' : 'no') . ')') if DEBUG;
204 27 my $dpop_jwt = Mojo::ATProto::OAuth::DPoP->proof(key => $key, method => 'POST', url => $url, nonce => $nonce);
205 27 my $tx = $self->ua->post($url => {'DPoP' => $dpop_jwt } => form => $body);
206 27 $res = $tx->res;
207 27 $self->log->debug("$label: attempt $attempt response status=" . ($res->code // 'connection error')) if DEBUG;
208  
209 27 ▶ my $new_nonce = $res->headers->header('DPoP-Nonce') // '';
Condition100%2/2
$res->headers->header("DPoP-Nonce") // ""
l!l
720
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
210 27 ▶ $nonce = $new_nonce if length($new_nonce);
Branch: 1 branch
BranchTrueFalseTotal
if length $new_nonce 7 20 27
211  
212 27 ▶ if ($res->code == 400 && length($new_nonce)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->code == 400 and length $new_nonce) 3 24 27
Condition100%3/3
$res->code == 400 and length $new_nonce
!ll&&!rl&&r
2133
MC/DC100%2/2
$res->code == 400 and length $new_nonce
$res->code == 400 length $new_nonce
Truth table100%3/3
A and B
A= $res->code == 400B= length $new_nonce
ABresult
0X0
100
111
213 3 my $reason = $self->_parse_auth_error_reason($res);
214 3 ▶ if ($reason eq 'use_dpop_nonce') {
Branch: 1 branch
BranchTrueFalseTotal
if ($reason eq "use_dpop_nonce") 3 0 3
215 3 $self->log->debug("$label: retrying with fresh DPoP-Nonce") if DEBUG;
216 3 next;
217 }
218 0 die "$label failed (HTTP 400): $reason\n";
219 }
220 24 last;
221 }
222  
223 24 return ($res, $nonce);
224 }
225  
226 13 ▶sub _post_dpop_retry_p($self, %args) {
Subroutine _post_dpop_retry_p: called CC=3 SCAR=11.0
227 13 my $url = $args{url};
228 13 my $body = $args{body};
229 13 my $key = $args{key};
230 13 ▶ my $label = $args{label} // 'request';
Condition50%1/2
$args{"label"} // "request"
l!l
130
MC/DC0%0/1
$args{"label"} // "request"
$args{"label"}
Truth table50%1/2
A
A= $args{"label"}
Aresult
11
00
231  
232 13 my $attempt;
233 14 ▶ $attempt = sub($nonce, $attempts_left) {
Subroutine __ANON__: called CC=1 SCAR=0.0
234 14 $self->log->debug("$label: POST $url (nonce=" . (length($nonce) ? 'yes' : 'no') . ", attempts_left=$attempts_left)") if DEBUG;
235 14 my $dpop_jwt = Mojo::ATProto::OAuth::DPoP->proof(key => $key, method => 'POST', url => $url, nonce => $nonce);
236 14 ▶ return $self->ua->post_p($url => {'DPoP' => $dpop_jwt} => form => $body)->then(sub ($tx) {
Subroutine __ANON__: called CC=7 SCAR=19.6
237 14 my $res = $tx->res;
238 14 $self->log->debug("$label: response status=" . ($res->code // 'connection error')) if DEBUG;
239 14 ▶ my $new_nonce = $res->headers->header('DPoP-Nonce') // '';
Condition100%2/2
$res->headers->header("DPoP-Nonce") // ""
l!l
68
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
240 14 ▶ $nonce = $new_nonce if length($new_nonce);
Branch: 1 branch
BranchTrueFalseTotal
if length $new_nonce 6 8 14
241  
242 14 ▶ if ($res->code == 400 && length($new_nonce) && $attempts_left > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->code == 400 and length $new_nonce and $attempts_left > 1) 1 13 14
Condition83%5/6
$res->code == 400 and length $new_nonce and $attempts_left > 1
!ll&&!rl&&r
1301
$res->code == 400 and length $new_nonce
!ll&&!rl&&r
1121
MC/DC66%2/3
$res->code == 400 and length $new_nonce and $attempts_left > 1
$res->code == 400 length $new_nonce $attempts_left > 1
Truth table75%3/4
A and B and C
A= $res->code == 400B= length $new_nonceC= $attempts_left > 1
ABCresult
0XX0
10X0
1100
1111
243 1 my $reason = $self->_parse_auth_error_reason($res);
244 1 ▶ if ($reason eq 'use_dpop_nonce') {
Branch: 1 branch
BranchTrueFalseTotal
if ($reason eq "use_dpop_nonce") 1 0 1
245 1 $self->log->debug("$label: retrying with fresh DPoP-Nonce") if DEBUG;
246 1 return $attempt->($nonce, $attempts_left - 1);
247 }
248 0 die "$label failed (HTTP 400): $reason\n";
249 }
250  
251 13 return ($res, $nonce);
252 14 });
253 13 };
254 13 ▶ return $attempt->($args{nonce} // '', 2);
Condition100%2/2
$args{"nonce"} // ""
l!l
94
MC/DC100%1/1
$args{"nonce"} // ""
$args{"nonce"}
Truth table100%2/2
A
A= $args{"nonce"}
Aresult
11
00
255 }
256  
257 # Sends the PAR request that kicks off an authorization flow. Returns an
258 # AuthRequestData-equivalent hashref: state, auth_server_url, scopes,
259 # pkce_verifier, request_uri, auth_server_token_endpoint,
260 # auth_server_revocation_endpoint, dpop_authserver_nonce,
261 # dpop_private_key_pem - everything a store needs to persist and later
262 # exchange for tokens via send_initial_token_request(_p) below.
263 #
264 # $auth_meta is the hashref Mojo::ATProto::OAuth::Resolver::
265 # resolve_auth_server_metadata(_p) already validated. Low-level: doesn't
266 # persist anything or resolve an identity - see start_auth_flow(_p) for
267 # the full orchestration.
268 13 ▶sub send_auth_request($self, $auth_meta, %opts) {
Subroutine send_auth_request: called CC=6 SCAR=18.1
Pod documented
269 13 ▶ my $scopes = _normalize_scopes($opts{scopes}) // $self->scopes;
Condition66%2/3
_normalize_scopes($opts{"scopes"}) // $self->scopes
l!l&&r!l&&!r
2110
MC/DC0%0/2
_normalize_scopes($opts{"scopes"}) // $self->scopes
_normalize_scopes($opts{"scopes"}) $self->scopes
Truth table66%2/3
A // B
A= _normalize_scopes($opts{"scopes"})B= $self->scopes
ABresult
1X1
011
000
270 13 my $login_hint = $opts{login_hint};
271  
272 13 $self->log->debug("send_auth_request: issuer=$auth_meta->{issuer} scopes=[" . join(',', @$scopes) . ']') if DEBUG;
273  
274 13 my $par_url = $auth_meta->{pushed_authorization_request_endpoint};
275 13 my $state = Mojo::ATProto::OAuth::DPoP->secure_random_base64(16);
276 13 my $pkce_verifier = Mojo::ATProto::OAuth::DPoP->secure_random_base64(48);
277 13 my $code_challenge = Mojo::ATProto::OAuth::DPoP->s256_challenge($pkce_verifier);
278 13 my $dpop_key = Mojo::ATProto::OAuth::DPoP->generate_keypair;
279 13 my $body = $self->_par_body($auth_meta, $state, $code_challenge, $scopes, $login_hint);
280  
281 13 my ($res, $dpop_nonce) = $self->_post_dpop_retry(url => $par_url, body => $body, key => $dpop_key, label => 'PAR request');
282  
283 13 ▶ die "PAR request failed (HTTP " . $res->code . "): " . $self->_parse_auth_error_reason($res) . "\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $res->code == 200 or $res->code == 201 1 12 13
Condition66%2/3
$res->code == 200 or $res->code == 201
l!l&&r!l&&!r
0121
MC/DC50%1/2
$res->code == 200 or $res->code == 201
$res->code == 200 $res->code == 201
Truth table66%2/3
A or B
A= $res->code == 200B= $res->code == 201
ABresult
1X1
011
000
284 unless $res->code == 200 || $res->code == 201;
285  
286 12 my $par_resp = $res->json;
287 12 ▶ die "PAR response missing request_uri\n" unless length($par_resp->{request_uri} // '');
Branch: 1 branch
BranchTrueFalseTotal
unless length($par_resp->{"request_uri"} // "") 0 12 12
Condition50%1/2
$par_resp->{"request_uri"} // ""
l!l
120
MC/DC0%0/1
$par_resp->{"request_uri"} // ""
$par_resp->{"request_uri"}
Truth table50%1/2
A
A= $par_resp->{"request_uri"}
Aresult
11
00
288  
289 12 $self->log->debug("send_auth_request: PAR succeeded, state=$state") if DEBUG;
290  
291 return {
292 state => $state,
293 auth_server_url => $auth_meta->{issuer},
294 scopes => $scopes,
295 pkce_verifier => $pkce_verifier,
296 request_uri => $par_resp->{request_uri},
297 auth_server_token_endpoint => $auth_meta->{token_endpoint},
298 auth_server_revocation_endpoint => $auth_meta->{revocation_endpoint},
299 12 dpop_authserver_nonce => $dpop_nonce,
300 dpop_private_key_pem => Mojo::ATProto::OAuth::DPoP->export_private_pem($dpop_key),
301 };
302 }
303  
304 4 ▶sub send_auth_request_p($self, $auth_meta, %opts) {
Subroutine send_auth_request_p: called CC=2 SCAR=7.1
Pod documented
305 4 ▶ my $scopes = _normalize_scopes($opts{scopes}) // $self->scopes;
Condition66%2/3
_normalize_scopes($opts{"scopes"}) // $self->scopes
l!l&&r!l&&!r
220
MC/DC0%0/2
_normalize_scopes($opts{"scopes"}) // $self->scopes
_normalize_scopes($opts{"scopes"}) $self->scopes
Truth table66%2/3
A // B
A= _normalize_scopes($opts{"scopes"})B= $self->scopes
ABresult
1X1
011
000
306 4 my $login_hint = $opts{login_hint};
307  
308 4 $self->log->debug("send_auth_request_p: issuer=$auth_meta->{issuer} scopes=[" . join(',', @$scopes) . ']') if DEBUG;
309  
310 4 my $par_url = $auth_meta->{pushed_authorization_request_endpoint};
311 4 my $state = Mojo::ATProto::OAuth::DPoP->secure_random_base64(16);
312 4 my $pkce_verifier = Mojo::ATProto::OAuth::DPoP->secure_random_base64(48);
313 4 my $code_challenge = Mojo::ATProto::OAuth::DPoP->s256_challenge($pkce_verifier);
314 4 my $dpop_key = Mojo::ATProto::OAuth::DPoP->generate_keypair;
315 4 my $body = $self->_par_body($auth_meta, $state, $code_challenge, $scopes, $login_hint);
316  
317 4 ▶ return $self->_post_dpop_retry_p(url => $par_url, body => $body, key => $dpop_key, label => 'PAR request')->then(sub ($res, $dpop_nonce) {
Subroutine __ANON__: called CC=5 SCAR=18.1
318 4 ▶ die "PAR request failed (HTTP " . $res->code . "): " . $self->_parse_auth_error_reason($res) . "\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $res->code == 200 or $res->code == 201 0 4 4
Condition33%1/3
$res->code == 200 or $res->code == 201
l!l&&r!l&&!r
040
MC/DC0%0/2
$res->code == 200 or $res->code == 201
$res->code == 200 $res->code == 201
Truth table33%1/3
A or B
A= $res->code == 200B= $res->code == 201
ABresult
1X1
011
000
319 unless $res->code == 200 || $res->code == 201;
320  
321 4 my $par_resp = $res->json;
322 4 ▶ die "PAR response missing request_uri\n" unless length($par_resp->{request_uri} // '');
Branch: 1 branch
BranchTrueFalseTotal
unless length($par_resp->{"request_uri"} // "") 0 4 4
Condition50%1/2
$par_resp->{"request_uri"} // ""
l!l
40
MC/DC0%0/1
$par_resp->{"request_uri"} // ""
$par_resp->{"request_uri"}
Truth table50%1/2
A
A= $par_resp->{"request_uri"}
Aresult
11
00
323  
324 4 $self->log->debug("send_auth_request_p: PAR succeeded, state=$state") if DEBUG;
325  
326 return {
327 state => $state,
328 auth_server_url => $auth_meta->{issuer},
329 scopes => $scopes,
330 pkce_verifier => $pkce_verifier,
331 request_uri => $par_resp->{request_uri},
332 auth_server_token_endpoint => $auth_meta->{token_endpoint},
333 auth_server_revocation_endpoint => $auth_meta->{revocation_endpoint},
334 4 dpop_authserver_nonce => $dpop_nonce,
335 dpop_private_key_pem => Mojo::ATProto::OAuth::DPoP->export_private_pem($dpop_key),
336 };
337 4 });
338 }
339  
340 # Exchanges an authorization code for tokens. $info is the AuthRequestData-equivalent
341 # hashref from send_auth_request(_p)/a store lookup - reuses its DPoP
342 # keypair (RFC 9449 requires the same key for every proof tied to one
343 # authorization attempt) and PKCE verifier. Returns a TokenResponse-
344 # equivalent hashref (sub, scope, access_token, refresh_token) plus the
345 # final dpop_authserver_nonce, for the caller to persist.
346 7 ▶sub send_initial_token_request($self, $auth_code, $info) {
Subroutine send_initial_token_request: called CC=2 SCAR=6.9
Pod documented
347 7 $self->log->debug("send_initial_token_request: exchanging code for tokens (state=$info->{state})") if DEBUG;
348  
349 my $body = {
350 client_id => $self->client_id,
351 redirect_uri => $self->callback_url,
352 grant_type => 'authorization_code',
353 code => $auth_code,
354 code_verifier => $info->{pkce_verifier},
355 7 %{$self->_client_assertion_params($info->{auth_server_url})},
356 };
357  
358 7 my $dpop_key = Mojo::ATProto::OAuth::DPoP->import_private_pem($info->{dpop_private_key_pem});
359 my ($res, $dpop_nonce) = $self->_post_dpop_retry(
360 url => $info->{auth_server_token_endpoint}, body => $body, key => $dpop_key,
361 7 nonce => $info->{dpop_authserver_nonce}, label => 'initial token request',
362 );
363  
364 7 ▶ die "initial token request failed (HTTP " . $res->code . "): " . $self->_parse_auth_error_reason($res) . "\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $res->code == 200 0 7 7
365 unless $res->code == 200;
366  
367 7 my $token_resp = $res->json;
368 7 $token_resp->{dpop_authserver_nonce} = $dpop_nonce;
369 7 $self->log->debug("send_initial_token_request: succeeded, sub=" . ($token_resp->{sub} // '?') . " scope=" . ($token_resp->{scope} // '')) if DEBUG;
370 7 return $token_resp;
371 }
372  
373 2 ▶sub send_initial_token_request_p($self, $auth_code, $info) {
Subroutine send_initial_token_request_p: called CC=1 SCAR=0.0
Pod documented
374 2 $self->log->debug("send_initial_token_request_p: exchanging code for tokens (state=$info->{state})") if DEBUG;
375  
376 my $body = {
377 client_id => $self->client_id,
378 redirect_uri => $self->callback_url,
379 grant_type => 'authorization_code',
380 code => $auth_code,
381 code_verifier => $info->{pkce_verifier},
382 2 %{$self->_client_assertion_params($info->{auth_server_url})},
383 };
384  
385 2 my $dpop_key = Mojo::ATProto::OAuth::DPoP->import_private_pem($info->{dpop_private_key_pem});
386 return $self->_post_dpop_retry_p(
387 url => $info->{auth_server_token_endpoint}, body => $body, key => $dpop_key,
388 nonce => $info->{dpop_authserver_nonce}, label => 'initial token request',
389 2 ▶ )->then(sub ($res, $dpop_nonce) {
Subroutine __ANON__: called CC=2 SCAR=7.0
390 2 ▶ die "initial token request failed (HTTP " . $res->code . "): " . $self->_parse_auth_error_reason($res) . "\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $res->code == 200 0 2 2
391 unless $res->code == 200;
392  
393 2 my $token_resp = $res->json;
394 2 $token_resp->{dpop_authserver_nonce} = $dpop_nonce;
395 2 $self->log->debug("send_initial_token_request_p: succeeded, sub=" . ($token_resp->{sub} // '?') . " scope=" . ($token_resp->{scope} // '')) if DEBUG;
396 2 return $token_resp;
397 2 });
398 }
399  
400 # Builds the ClientSessionData-equivalent hashref to persist, once a
401 # token exchange has succeeded and the account DID has been verified
402 # against the identity that vouches for it (pure - no I/O, shared by
403 # both process_callback and process_callback_p). `client_state`/`extra`
404 # ride through from $info untouched (whatever start_auth_flow(_p) was
405 # given, if anything) - this library never interprets either one, it's
406 # purely opaque pass-through for the caller (e.g. a plugin) to unpack
407 # on the other side.
408 9 ▶sub _build_session_data($self, $info, $token_resp, $account_did, $handle, $host_url) {
Subroutine _build_session_data: called CC=2 SCAR=7.6
409 return {
410 account_did => $account_did,
411 handle => $handle,
412 session_id => $info->{state},
413 host_url => $host_url,
414 auth_server_url => $info->{auth_server_url},
415 auth_server_token_endpoint => $info->{auth_server_token_endpoint},
416 auth_server_revocation_endpoint => $info->{auth_server_revocation_endpoint},
417 scopes => [split(/ /, $token_resp->{scope} // '')],
418 access_token => $token_resp->{access_token},
419 refresh_token => $token_resp->{refresh_token},
420 dpop_authserver_nonce => $token_resp->{dpop_authserver_nonce},
421 dpop_host_nonce => $token_resp->{dpop_authserver_nonce}, # bootstrap host nonce from authserver
422 dpop_private_key_pem => $info->{dpop_private_key_pem},
423 client_state => $info->{client_state},
424 extra => $info->{extra},
425 9 ▶ };
Condition50%1/2
$token_resp->{"scope"} // ""
l!l
90
MC/DC0%0/1
$token_resp->{"scope"} // ""
$token_resp->{"scope"}
Truth table50%1/2
A
A= $token_resp->{"scope"}
Aresult
11
00
426 }
427  
428 # If this auth request was a scope upgrade (see start_scope_upgrade(_p)
429 # below), collapse the just-issued session data onto the *existing*
430 # session_id it's upgrading - so the customer's browser session is
431 # undisturbed - and union its scopes with what's already stored, rather
432 # than narrowing to just this exchange's own granted scope set. A no-op
433 # (returns $session_data unchanged) for an ordinary login.
434 7 ▶sub _apply_scope_upgrade_merge($self, $info, $session_data) {
Subroutine _apply_scope_upgrade_merge: called CC=4 SCAR=14.0
435 7 ▶ return $session_data unless length($info->{upgrade_session_id} // '');
Branch: 1 branch
BranchTrueFalseTotal
unless length($info->{"upgrade_session_id"} // "") 6 1 7
Condition100%2/2
$info->{"upgrade_session_id"} // ""
l!l
16
MC/DC100%1/1
$info->{"upgrade_session_id"} // ""
$info->{"upgrade_session_id"}
Truth table100%2/2
A
A= $info->{"upgrade_session_id"}
Aresult
11
00
436  
437 1 $self->log->debug("_apply_scope_upgrade_merge: landing on existing session_id=$info->{upgrade_session_id}") if DEBUG;
438 1 my $existing;
439 1 try {
440 1 $existing = $self->store->get_session($session_data->{account_did}, $info->{upgrade_session_id});
441 } catch($ex) {
442 0 $existing = undef;
443 }
444 1 ▶ $session_data->{scopes} = $self->_union_scopes($existing->{scopes}, $session_data->{scopes}) if $existing;
Branch: 1 branch
BranchTrueFalseTotal
if $existing 1 0 1
445 1 $session_data->{session_id} = $info->{upgrade_session_id};
446 1 return $session_data;
447 }
448  
449 2 ▶sub _apply_scope_upgrade_merge_p($self, $info, $session_data) {
Subroutine _apply_scope_upgrade_merge_p: called CC=3 SCAR=11.1
450 2 ▶ return Mojo::Promise->resolve($session_data) unless length($info->{upgrade_session_id} // '');
Branch: 1 branch
BranchTrueFalseTotal
unless length($info->{"upgrade_session_id"} // "") 1 1 2
Condition100%2/2
$info->{"upgrade_session_id"} // ""
l!l
11
MC/DC100%1/1
$info->{"upgrade_session_id"} // ""
$info->{"upgrade_session_id"}
Truth table100%2/2
A
A= $info->{"upgrade_session_id"}
Aresult
11
00
451  
452 1 $self->log->debug("_apply_scope_upgrade_merge_p: landing on existing session_id=$info->{upgrade_session_id}") if DEBUG;
453 1 ▶ return $self->store->get_session_p($session_data->{account_did}, $info->{upgrade_session_id})->then(sub ($existing) {
Subroutine __ANON__: called CC=1 SCAR=0.0
454 1 $session_data->{scopes} = $self->_union_scopes($existing->{scopes}, $session_data->{scopes});
455 1 $session_data->{session_id} = $info->{upgrade_session_id};
456 1 return $session_data;
457 0 ▶ })->catch(sub ($err) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
458 # No prior session found under that id (shouldn't normally
459 # happen - the upgrade flow only ever starts from an existing
460 # one) - still land under the upgrade target id, just without a
461 # scope merge to fall back on.
462 0 $session_data->{session_id} = $info->{upgrade_session_id};
463 0 return $session_data;
464 1 });
465 }
466  
467 4 ▶sub _union_scopes($self, $a, $b) {
Subroutine _union_scopes: called CC=3 SCAR=11.3
468 4 ▶ my %union = map { $_ => 1 } (@{$a // []}, @{$b // []});
Condition50%2/4
$a // []
l!l
40
$b // []
l!l
40
MC/DC0%0/1
$a // []
$a
MC/DC0%0/1
$b // []
$b
Truth table50%1/2
A
A= $a
Aresult
11
00
Truth table50%1/2
A
A= $b
Aresult
11
00
469 4 return [sort keys %union];
470 }
471  
472 10 ▶sub _validate_callback_params($self, $info, $params) {
Subroutine _validate_callback_params: called CC=10 SCAR=31.1
473 10 ▶ if (length($params->{error} // '')) {
Branch: 1 branch
BranchTrueFalseTotal
if (length($params->{"error"} // "")) 0 10 10
Condition50%1/2
$params->{"error"} // ""
l!l
010
MC/DC0%0/1
$params->{"error"} // ""
$params->{"error"}
Truth table50%1/2
A
A= $params->{"error"}
Aresult
11
00
474 0 my $msg = "OAuth request callback error: $params->{error}";
475 0 ▶ $msg .= ": $params->{error_description}" if length($params->{error_description} // '');
Branch: 1 branch
BranchTrueFalseTotal
if length($params->{"error_description"} // "") 0 0 0
Condition0%0/2
$params->{"error_description"} // ""
l!l
00
MC/DC0%0/1
$params->{"error_description"} // ""
$params->{"error_description"}
Truth table0%0/2
A
A= $params->{"error_description"}
Aresult
11
00
476 0 die "$msg\n";
477 }
478  
479 10 ▶ my $authserver_url = $params->{iss} // '';
Condition50%1/2
$params->{"iss"} // ""
l!l
100
MC/DC0%0/1
$params->{"iss"} // ""
$params->{"iss"}
Truth table50%1/2
A
A= $params->{"iss"}
Aresult
11
00
480 10 ▶ my $auth_code = $params->{code} // '';
Condition50%1/2
$params->{"code"} // ""
l!l
100
MC/DC0%0/1
$params->{"code"} // ""
$params->{"code"}
Truth table50%1/2
A
A= $params->{"code"}
Aresult
11
00
481 10 ▶ die "missing required query param\n" unless length($authserver_url) && length($auth_code);
Branch: 1 branch
BranchTrueFalseTotal
unless length $authserver_url and length $auth_code 0 10 10
Condition33%1/3
length $authserver_url and length $auth_code
!ll&&!rl&&r
0010
MC/DC0%0/2
length $authserver_url and length $auth_code
length $authserver_url length $auth_code
Truth table33%1/3
A and B
A= length $authserver_urlB= length $auth_code
ABresult
0X0
100
111
482 10 ▶ die "callback iss doesn't match request info\n" unless $info->{auth_server_url} eq $authserver_url;
Branch: 1 branch
BranchTrueFalseTotal
unless $info->{"auth_server_url"} eq $authserver_url 1 9 10
483  
484 9 return ($authserver_url, $auth_code);
485 }
486  
487 # High-level helper for starting a new session
488 # resolves an identity to auth-server metadata, sends the PAR request,
489 # persists the auth request via `store`, and returns the URL the user
490 # should be redirected to (browser) to approve the auth flow. Requires
491 # `store` to be configured.
492 #
493 # %opts, exactly one of:
494 # identifier - an atproto handle/DID, or an https:// auth-server URL
495 # directly (skips identity resolution entirely until the
496 # callback, same dual-mode as always).
497 # did + handle + host_url - all three together, when the caller has
498 # already resolved identity itself (e.g. to make a
499 # pre-auth decision - reading a public repo record to
500 # pick a scope set, say) and there's no reason to pay
501 # for a second identity->lookup(_p) here just to re-derive
502 # the same did/handle. Auth-server discovery (host_url ->
503 # auth_server_url) still always happens regardless of
504 # which mode is used - that's a separate step from
505 # identity resolution, not something a caller would
506 # plausibly have pre-computed.
507 # Plus, independent of the above:
508 # scopes (optional, arrayref or space-separated string) - overrides $self->scopes for just
509 # this call, falls back to the client's configured
510 # default when omitted.
511 # client_state (optional hashref) - opaque, never inspected here;
512 # persisted on the auth request and handed back untouched
513 # inside process_callback(_p)'s result. Intended for
514 # things like a post-login redirect target.
515 # extra (optional hashref) - same opaque-pass-through treatment
516 # as client_state, but conventionally used for data that
517 # belongs in the caller's own session metadata once
518 # login completes (e.g. a pre-auth decision worth
519 # remembering) rather than being callback-routing data.
520 # This library draws no distinction between the two
521 # beyond "two separate opaque slots" - what each is used
522 # for is entirely up to the caller.
523 7 ▶sub start_auth_flow($self, %opts) {
Subroutine start_auth_flow: called CC=11 SCAR=24.7
Pod documented
524 7 ▶ die "start_auth_flow: 'store' must be configured\n" unless defined($self->store);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->store 0 7 7
525 7 ▶ $opts{scopes} = _normalize_scopes($opts{scopes}) if defined($opts{scopes});
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts{"scopes"} 0 7 7
526 7 $self->log->debug('start_auth_flow: ' . _describe_start_opts(%opts)) if DEBUG;
527  
528 7 my ($did, $handle, $host_url, $auth_server_url) = $self->_resolve_start(%opts);
529 7 $self->log->debug('start_auth_flow: resolved did=' . ($did // '(bare auth-server URL)') . " handle=" . ($handle // '?') . " auth_server=$auth_server_url") if DEBUG;
530  
531 7 my $auth_meta = $self->resolver->resolve_auth_server_metadata($auth_server_url);
532 7 ▶ my $info = $self->send_auth_request($auth_meta, login_hint => ($handle // $did // $opts{identifier}), ($opts{scopes} ? (scopes => $opts{scopes}) : ()));
Branch: 1 branch
BranchTrueFalseTotal
$opts{'scopes'} ? : 0 7 7
Condition66%4/6
($handle // $did) // $opts{"identifier"}
l!l&&r!l&&!r
610
$handle // $did
l!l&&r!l&&!r
601
MC/DC0%0/3
($handle // $did) // $opts{"identifier"}
$handle $did $opts{"identifier"}
Truth table50%2/4
A // B // C
A= $handleB= $didC= $opts{"identifier"}
ABCresult
1XX1
01X1
0011
0000
533 7 ▶ $info->{account_did} = $did if defined($did);
Branch: 1 branch
BranchTrueFalseTotal
if defined $did 6 1 7
534 7 ▶ $info->{handle} = $handle if defined($handle);
Branch: 1 branch
BranchTrueFalseTotal
if defined $handle 6 1 7
535 7 ▶ $info->{host_url} = $host_url if defined($host_url);
Branch: 1 branch
BranchTrueFalseTotal
if defined $host_url 6 1 7
536 7 ▶ $info->{client_state} = $opts{client_state} if defined($opts{client_state});
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts{"client_state"} 0 7 7
537 7 ▶ $info->{extra} = $opts{extra} if defined($opts{extra});
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts{"extra"} 0 7 7
538  
539 7 $self->store->save_auth_request($info);
540 7 $self->log->debug("start_auth_flow: auth request persisted, state=$info->{state}") if DEBUG;
541 7 return $self->_authorization_redirect_url($auth_meta, $info);
542 }
543  
544 1 ▶sub start_auth_flow_p($self, %opts) {
Subroutine start_auth_flow_p: called CC=3 SCAR=12.0
Pod documented
545 1 ▶ die "start_auth_flow_p: 'store' must be configured\n" unless defined($self->store);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->store 0 1 1
546 1 ▶ $opts{scopes} = _normalize_scopes($opts{scopes}) if defined($opts{scopes});
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts{"scopes"} 0 1 1
547 1 $self->log->debug('start_auth_flow_p: ' . _describe_start_opts(%opts)) if DEBUG;
548  
549 1 ▶ return $self->_resolve_start_p(%opts)->then(sub ($did, $handle, $host_url, $auth_server_url) {
Subroutine __ANON__: called CC=1 SCAR=0.4
550 1 $self->log->debug('start_auth_flow_p: resolved did=' . ($did // '(bare auth-server URL)') . " handle=" . ($handle // '?') . " auth_server=$auth_server_url") if DEBUG;
551 1 ▶ return $self->resolver->resolve_auth_server_metadata_p($auth_server_url)->then(sub ($auth_meta) {
Subroutine __ANON__: called CC=4 SCAR=15.7
552 1 ▶ return $self->send_auth_request_p($auth_meta, login_hint => ($handle // $did // $opts{identifier}), ($opts{scopes} ? (scopes => $opts{scopes}) : ()))->then(sub ($info) {
Subroutine __ANON__: called CC=6 SCAR=19.1
553 1 ▶ $info->{account_did} = $did if defined($did);
Branch: 1 branch
BranchTrueFalseTotal
if defined $did 0 1 1
554 1 ▶ $info->{handle} = $handle if defined($handle);
Branch: 1 branch
BranchTrueFalseTotal
if defined $handle 0 1 1
555 1 ▶ $info->{host_url} = $host_url if defined($host_url);
Branch: 1 branch
BranchTrueFalseTotal
if defined $host_url 0 1 1
556 1 ▶ $info->{client_state} = $opts{client_state} if defined($opts{client_state});
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts{"client_state"} 0 1 1
557 1 ▶ $info->{extra} = $opts{extra} if defined($opts{extra});
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts{"extra"} 0 1 1
558 1 ▶ return $self->store->save_auth_request_p($info)->then(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
559 1 $self->log->debug("start_auth_flow_p: auth request persisted, state=$info->{state}") if DEBUG;
560 1 return $self->_authorization_redirect_url($auth_meta, $info);
561 1 });
562 1 ▶ });
Branch: 1 branch
BranchTrueFalseTotal
$opts{'scopes'} ? : 0 1 1
Condition33%2/6
($handle // $did) // $opts{"identifier"}
l!l&&r!l&&!r
010
$handle // $did
l!l&&r!l&&!r
001
MC/DC0%0/3
($handle // $did) // $opts{"identifier"}
$handle $did $opts{"identifier"}
Truth table25%1/4
A // B // C
A= $handleB= $didC= $opts{"identifier"}
ABCresult
1XX1
01X1
0011
0000
563 1 });
564 1 });
565 }
566  
567 0 ▶sub _describe_start_opts(%opts) {
Subroutine _describe_start_opts: not called CC=7 SCAR=40.3
568 0 return 'identifier=' . $opts{identifier} . ($opts{scopes} ? ' scopes=[' . join(',', @{$opts{scopes}}) . ']' : '')
569 0 ▶ if defined($opts{identifier});
Branch: 2 branches
BranchTrueFalseTotal
if defined $opts{"identifier"} 0 0 0
$opts{'scopes'} ? : 0 0 0
570 return 'did=' . ($opts{did} // '?') . ' handle=' . ($opts{handle} // '?') . ' host_url=' . ($opts{host_url} // '?')
571 0 ▶ . ($opts{scopes} ? ' scopes=[' . join(',', @{$opts{scopes}}) . ']' : '');
Branch: 1 branch
BranchTrueFalseTotal
$opts{'scopes'} ? : 0 0 0
Condition0%0/6
$opts{"did"} // "?"
l!l
00
$opts{"handle"} // "?"
l!l
00
$opts{"host_url"} // "?"
l!l
00
MC/DC0%0/1
$opts{"did"} // "?"
$opts{"did"}
MC/DC0%0/1
$opts{"handle"} // "?"
$opts{"handle"}
MC/DC0%0/1
$opts{"host_url"} // "?"
$opts{"host_url"}
Truth table0%0/2
A
A= $opts{"did"}
Aresult
11
00
Truth table0%0/2
A
A= $opts{"handle"}
Aresult
11
00
Truth table0%0/2
A
A= $opts{"host_url"}
Aresult
11
00
572 }
573  
574 10 ▶sub _authorization_redirect_url($self, $auth_meta, $info) {
Subroutine _authorization_redirect_url: called CC=1 SCAR=0.0
575 return Mojo::URL->new($auth_meta->{authorization_endpoint})->query({
576 client_id => $self->client_id,
577 request_uri => $info->{request_uri},
578 10 })->to_string;
579 }
580  
581 # Returns (did, handle, host_url, auth_server_url) - did/handle/host_url
582 # are undef together for the bare-https://-URL entry mode (identity
583 # genuinely isn't known yet, resolved later in process_callback(_p)
584 # instead), populated together in every other mode. See
585 # start_auth_flow(_p)'s own header for what %opts recognizes.
586 7 ▶sub _resolve_start($self, %opts) {
Subroutine _resolve_start: called CC=8 SCAR=24.3
587 7 ▶ if (defined($opts{did}) && defined($opts{handle}) && defined($opts{host_url})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"did"} and defined $opts{"handle"} and defined $opts{"host_url"}) 0 7 7
Condition33%2/6
defined $opts{"did"} and defined $opts{"handle"} and defined $opts{"host_url"}
!ll&&!rl&&r
700
defined $opts{"did"} and defined $opts{"handle"}
!ll&&!rl&&r
700
MC/DC0%0/3
defined $opts{"did"} and defined $opts{"handle"} and defined $opts{"host_url"}
defined $opts{"did"} defined $opts{"handle"} defined $opts{"host_url"}
Truth table25%1/4
A and B and C
A= defined $opts{"did"}B= defined $opts{"handle"}C= defined $opts{"host_url"}
ABCresult
0XX0
10X0
1100
1111
588 0 return ($opts{did}, $opts{handle}, $opts{host_url}, $self->resolver->resolve_auth_server_url($opts{host_url}));
589 }
590  
591 7 ▶ my $identifier = $opts{identifier} // die "start_auth_flow: 'identifier' or 'did'+'handle'+'host_url' required\n";
Condition50%1/2
$opts{"identifier"} // die("start_auth_flow: 'identifier' or 'did'+'handle'+'host_url' required\n")
l!l
70
MC/DC0%0/1
$opts{"identifier"} // die("start_auth_flow: 'identifier' or 'did'+'handle'+'host_url' required\n")
$opts{"identifier"}
Truth table50%1/2
A
A= $opts{"identifier"}
Aresult
11
00
592 7 ▶ return (undef, undef, undef, $identifier) if $identifier =~ m{^https://};
Branch: 1 branch
BranchTrueFalseTotal
if $identifier =~ m[^https://]u 1 6 7
593  
594 6 my $identity = $self->identity->lookup($identifier);
595 6 my $host_url = $self->identity->pds_endpoint($identity);
596 6 ▶ die "identity does not link to an atproto host (PDS)\n" unless length($host_url // '');
Branch: 1 branch
BranchTrueFalseTotal
unless length($host_url // "") 0 6 6
Condition50%1/2
$host_url // ""
l!l
60
MC/DC0%0/1
$host_url // ""
$host_url
Truth table50%1/2
A
A= $host_url
Aresult
11
00
597  
598 6 return ($identity->{did}, $identity->{handle}, $host_url, $self->resolver->resolve_auth_server_url($host_url));
599 }
600  
601 1 ▶sub _resolve_start_p($self, %opts) {
Subroutine _resolve_start_p: called CC=6 SCAR=28.9
602 1 ▶ if (defined($opts{did}) && defined($opts{handle}) && defined($opts{host_url})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"did"} and defined $opts{"handle"} and defined $opts{"host_url"}) 0 1 1
Condition33%2/6
defined $opts{"did"} and defined $opts{"handle"} and defined $opts{"host_url"}
!ll&&!rl&&r
100
defined $opts{"did"} and defined $opts{"handle"}
!ll&&!rl&&r
100
MC/DC0%0/3
defined $opts{"did"} and defined $opts{"handle"} and defined $opts{"host_url"}
defined $opts{"did"} defined $opts{"handle"} defined $opts{"host_url"}
Truth table25%1/4
A and B and C
A= defined $opts{"did"}B= defined $opts{"handle"}C= defined $opts{"host_url"}
ABCresult
0XX0
10X0
1100
1111
603 0 ▶ return $self->resolver->resolve_auth_server_url_p($opts{host_url})->then(sub ($auth_server_url) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
604 0 return ($opts{did}, $opts{handle}, $opts{host_url}, $auth_server_url);
605 0 });
606 }
607  
608 1 ▶ my $identifier = $opts{identifier} // return Mojo::Promise->reject("start_auth_flow_p: 'identifier' or 'did'+'handle'+'host_url' required\n");
Condition50%1/2
$opts{"identifier"} // (return "Mojo::Promise"->reject("start_auth_flow_p: 'identifier' or 'did'+'handle'+'host_url' required\n"))
l!l
10
MC/DC0%0/1
$opts{"identifier"} // (return "Mojo::Promise"->reject("start_auth_flow_p: 'identifier' or 'did'+'handle'+'host_url' required\n"))
$opts{"identifier"}
Truth table50%1/2
A
A= $opts{"identifier"}
Aresult
11
00
609 1 ▶ return Mojo::Promise->resolve(undef, undef, undef, $identifier) if $identifier =~ m{^https://};
Branch: 1 branch
BranchTrueFalseTotal
if $identifier =~ m[^https://]u 1 0 1
610  
611 0 ▶ return $self->identity->lookup_p($identifier)->then(sub ($identity) {
Subroutine __ANON__: not called CC=3 SCAR=24.8
612 0 my $host_url = $self->identity->pds_endpoint($identity);
613 0 ▶ die "identity does not link to an atproto host (PDS)\n" unless length($host_url // '');
Branch: 1 branch
BranchTrueFalseTotal
unless length($host_url // "") 0 0 0
Condition0%0/2
$host_url // ""
l!l
00
MC/DC0%0/1
$host_url // ""
$host_url
Truth table0%0/2
A
A= $host_url
Aresult
11
00
614  
615 0 ▶ return $self->resolver->resolve_auth_server_url_p($host_url)->then(sub ($auth_server_url) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
616 0 return ($identity->{did}, $identity->{handle}, $host_url, $auth_server_url);
617 0 });
618 0 });
619 }
620  
621 # High-level helper for completing the auth flow (indigo's
622 # ProcessCallback): verifies callback query params ($params, a plain
623 # hashref of the callback request's query parameters) against the
624 # persisted auth request, exchanges the code for tokens, verifies the
625 # account identity, persists the resulting session via `store`, and
626 # returns the ClientSessionData-equivalent hashref. Requires `store` to
627 # be configured.
628 8 ▶sub process_callback($self, $params) {
Subroutine process_callback: called CC=10 SCAR=23.2
Pod documented
629 8 ▶ die "process_callback: 'store' must be configured\n" unless defined($self->store);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->store 0 8 8
630  
631 8 ▶ my $state = $params->{state} // die "missing state query param\n";
Condition50%1/2
$params->{"state"} // die("missing state query param\n")
l!l
80
MC/DC0%0/1
$params->{"state"} // die("missing state query param\n")
$params->{"state"}
Truth table50%1/2
A
A= $params->{"state"}
Aresult
11
00
632 8 $self->log->debug("process_callback: state=$state iss=" . ($params->{iss} // '?')) if DEBUG;
633 8 my $info = $self->store->get_auth_request($state);
634  
635 8 my ($authserver_url, $auth_code) = $self->_validate_callback_params($info, $params);
636 7 my $token_resp = $self->send_initial_token_request($auth_code, $info);
637  
638 7 my ($account_did, $handle, $host_url);
639 7 ▶ if (length($info->{account_did} // '')) {
Branch: 1 branch
BranchTrueFalseTotal
if (length($info->{'account_did'} // '')) { } 6 1 7
Condition100%2/2
$info->{"account_did"} // ""
l!l
61
MC/DC100%1/1
$info->{"account_did"} // ""
$info->{"account_did"}
Truth table100%2/2
A
A= $info->{"account_did"}
Aresult
11
00
640 6 $account_did = $info->{account_did};
641 6 ▶ die "token subject didn't match original DID\n" unless $token_resp->{sub} eq $account_did;
Branch: 1 branch
BranchTrueFalseTotal
unless $token_resp->{"sub"} eq $account_did 0 6 6
642 6 ▶ if (length($info->{host_url} // '')) {
Branch: 1 branch
BranchTrueFalseTotal
if (length($info->{'host_url'} // '')) { } 5 1 6
Condition100%2/2
$info->{"host_url"} // ""
l!l
51
MC/DC100%1/1
$info->{"host_url"} // ""
$info->{"host_url"}
Truth table100%2/2
A
A= $info->{"host_url"}
Aresult
11
00
643 # Already resolved and persisted at start_auth_flow(_p) time
644 # (either from the identifier, or handed in pre-resolved) -
645 # reusing it avoids a third identity lookup for what's purely
646 # descriptive metadata at this point; the token subject match
647 # above is what actually re-confirms the account. Not every
648 # known-DID auth request has this though - start_scope_upgrade
649 # doesn't set it on its own auth-request rows - hence the
650 # fallback below, unchanged from before this option existed.
651 5 $handle = $info->{handle};
652 5 $host_url = $info->{host_url};
653 } else {
654 1 my $identity = $self->identity->lookup($account_did);
655 1 $handle = $identity->{handle};
656 1 $host_url = $self->identity->pds_endpoint($identity);
657 }
658 6 $self->log->debug("process_callback: known-DID path, account_did=$account_did") if DEBUG;
659 } else {
660 1 ▶ $account_did = $token_resp->{sub} // die "token response missing sub\n";
Condition50%1/2
$token_resp->{"sub"} // die("token response missing sub\n")
l!l
10
MC/DC0%0/1
$token_resp->{"sub"} // die("token response missing sub\n")
$token_resp->{"sub"}
Truth table50%1/2
A
A= $token_resp->{"sub"}
Aresult
11
00
661 1 my $identity = $self->identity->lookup($account_did);
662 1 $handle = $identity->{handle};
663 1 $host_url = $self->identity->pds_endpoint($identity);
664 1 my $resolved = $self->resolver->resolve_auth_server_url($host_url);
665 1 ▶ die "token subject auth server did not match original\n" unless $resolved eq $authserver_url;
Branch: 1 branch
BranchTrueFalseTotal
unless $resolved eq $authserver_url 0 1 1
666 1 $self->log->debug("process_callback: bare-URL entry path, resolved account_did=$account_did") if DEBUG;
667 }
668  
669 7 my $session_data = $self->_build_session_data($info, $token_resp, $account_did, $handle, $host_url);
670 7 $session_data = $self->_apply_scope_upgrade_merge($info, $session_data);
671 7 $self->store->save_session($session_data);
672 7 $self->log->debug("process_callback: session saved, account_did=$account_did session_id=$session_data->{session_id}") if DEBUG;
673  
674 # Non-fatal on failure to clean up, matching indigo's own
675 # ProcessCallback (log-and-continue) - the session itself is already
676 # safely persisted at this point; a leftover auth-request row is
677 # inert, not a correctness problem.
678 7 try {
679 7 $self->store->delete_auth_request($state);
680 } catch($ex) {
681 0 $self->log->warn("failed to delete auth request info for state=$state: $ex");
682 }
683 7 return $session_data;
684 }
685  
686 2 ▶sub process_callback_p($self, $params) {
Subroutine process_callback_p: called CC=3 SCAR=11.2
Pod documented
687 2 ▶ die "process_callback_p: 'store' must be configured\n" unless defined($self->store);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->store 0 2 2
688  
689 2 ▶ my $state = $params->{state} // return Mojo::Promise->reject("missing state query param\n");
Condition50%1/2
$params->{"state"} // (return "Mojo::Promise"->reject("missing state query param\n"))
l!l
20
MC/DC0%0/1
$params->{"state"} // (return "Mojo::Promise"->reject("missing state query param\n"))
$params->{"state"}
Truth table50%1/2
A
A= $params->{"state"}
Aresult
11
00
690 2 $self->log->debug("process_callback_p: state=$state iss=" . ($params->{iss} // '?')) if DEBUG;
691  
692 2 ▶ return $self->store->get_auth_request_p($state)->then(sub ($info) {
Subroutine __ANON__: called CC=1 SCAR=0.1
693 2 my ($authserver_url, $auth_code) = $self->_validate_callback_params($info, $params);
694  
695 2 ▶ return $self->send_initial_token_request_p($auth_code, $info)->then(sub ($token_resp) {
Subroutine __ANON__: called CC=7 SCAR=20.1
696 2 ▶ if (length($info->{account_did} // '')) {
Branch: 1 branch
BranchTrueFalseTotal
if (length($info->{"account_did"} // "")) 1 1 2
Condition100%2/2
$info->{"account_did"} // ""
l!l
11
MC/DC100%1/1
$info->{"account_did"} // ""
$info->{"account_did"}
Truth table100%2/2
A
A= $info->{"account_did"}
Aresult
11
00
697 1 my $account_did = $info->{account_did};
698 1 ▶ die "token subject didn't match original DID\n" unless $token_resp->{sub} eq $account_did;
Branch: 1 branch
BranchTrueFalseTotal
unless $token_resp->{"sub"} eq $account_did 0 1 1
699 1 $self->log->debug("process_callback_p: known-DID path, account_did=$account_did") if DEBUG;
700  
701 # See process_callback's identical branch for why this is
702 # conditional - start_scope_upgrade_p's own auth-request
703 # rows don't persist host_url, so they still fall back to
704 # a fresh lookup here exactly as before this option existed.
705 return Mojo::Promise->resolve($info->{handle}, $info->{host_url})
706 0 ▶ ->then(sub ($handle, $host_url) { return $self->_finish_callback_p($state, $info, $token_resp, $account_did, $handle, $host_url) })
Subroutine __ANON__: not called CC=1 SCAR=6.9
707 1 ▶ if length($info->{host_url} // '');
Branch: 1 branch
BranchTrueFalseTotal
if length($info->{"host_url"} // "") 0 1 1
Condition50%1/2
$info->{"host_url"} // ""
l!l
01
MC/DC0%0/1
$info->{"host_url"} // ""
$info->{"host_url"}
Truth table50%1/2
A
A= $info->{"host_url"}
Aresult
11
00
708  
709 1 ▶ return $self->identity->lookup_p($account_did)->then(sub ($identity) {
Subroutine __ANON__: called CC=1 SCAR=0.0
710 1 return $self->_finish_callback_p($state, $info, $token_resp, $account_did, $identity->{handle}, $self->identity->pds_endpoint($identity));
711 1 });
712 }
713  
714 1 ▶ my $account_did = $token_resp->{sub} // die "token response missing sub\n";
Condition50%1/2
$token_resp->{"sub"} // die("token response missing sub\n")
l!l
10
MC/DC0%0/1
$token_resp->{"sub"} // die("token response missing sub\n")
$token_resp->{"sub"}
Truth table50%1/2
A
A= $token_resp->{"sub"}
Aresult
11
00
715 1 ▶ return $self->identity->lookup_p($account_did)->then(sub ($identity) {
Subroutine __ANON__: called CC=1 SCAR=0.0
716 1 my $host_url = $self->identity->pds_endpoint($identity);
717 1 ▶ return $self->resolver->resolve_auth_server_url_p($host_url)->then(sub ($resolved) {
Subroutine __ANON__: called CC=2 SCAR=7.1
718 1 ▶ die "token subject auth server did not match original\n" unless $resolved eq $authserver_url;
Branch: 1 branch
BranchTrueFalseTotal
unless $resolved eq $authserver_url 0 1 1
719 1 $self->log->debug("process_callback_p: bare-URL entry path, resolved account_did=$account_did") if DEBUG;
720 1 return $self->_finish_callback_p($state, $info, $token_resp, $account_did, $identity->{handle}, $host_url);
721 1 });
722 1 });
723 2 });
724 2 });
725 }
726  
727 2 ▶sub _finish_callback_p($self, $state, $info, $token_resp, $account_did, $handle, $host_url) {
Subroutine _finish_callback_p: called CC=1 SCAR=0.0
728 2 my $session_data = $self->_build_session_data($info, $token_resp, $account_did, $handle, $host_url);
729  
730 2 ▶ return $self->_apply_scope_upgrade_merge_p($info, $session_data)->then(sub ($merged) {
Subroutine __ANON__: called CC=1 SCAR=0.0
731 2 $session_data = $merged;
732 2 return $self->store->save_session_p($session_data);
733 2 ▶ })->then(sub {
Subroutine __ANON__: called CC=1 SCAR=0.4
734 2 $self->log->debug("_finish_callback_p: session saved, account_did=$account_did session_id=$session_data->{session_id}") if DEBUG;
735 0 ▶ return $self->store->delete_auth_request_p($state)->catch(sub ($err) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
736 0 $self->log->warn("failed to delete auth request info for state=$state: $err");
737 2 });
738 2 ▶ })->then(sub { return $session_data });
Subroutine __ANON__: called CC=1 SCAR=0.0
739 }
740  
741 # Starts a scope-upgrade authorization for an already-known, already-
742 # verified session; seamlessly request a broader scope set without a
743 # full re-login, merging the result back into the *existing* session
744 # (see _apply_scope_upgrade_merge(_p) above) rather than replacing it.
745 # $session is the existing stored session hashref; $additional_scopes is
746 # what's newly needed. The PAR request actually asks for the union of
747 # the session's current scopes and these, so a repeated upgrade request
748 # for the same additional scope is idempotent rather than narrowing what
749 # gets asked for. Returns the redirect URL, same as start_auth_flow.
750 1 ▶sub start_scope_upgrade($self, $session, $additional_scopes) {
Subroutine start_scope_upgrade: called CC=2 SCAR=6.9
Pod documented
751 1 ▶ die "start_scope_upgrade: 'store' must be configured\n" unless defined($self->store);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->store 0 1 1
752  
753 1 my $scopes = $self->_union_scopes($session->{scopes}, $additional_scopes);
754 1 $self->log->debug("start_scope_upgrade: session_id=$session->{session_id} requesting scopes=[" . join(',', @$scopes) . ']') if DEBUG;
755 1 my $auth_meta = $self->resolver->resolve_auth_server_metadata($session->{auth_server_url});
756 1 my $info = $self->send_auth_request($auth_meta, scopes => $scopes, login_hint => $session->{account_did});
757 1 $info->{account_did} = $session->{account_did};
758 1 $info->{upgrade_session_id} = $session->{session_id};
759  
760 1 $self->store->save_auth_request($info);
761  
762 1 return $self->_authorization_redirect_url($auth_meta, $info);
763 }
764  
765 1 ▶sub start_scope_upgrade_p($self, $session, $additional_scopes) {
Subroutine start_scope_upgrade_p: called CC=2 SCAR=6.9
Pod documented
766 1 ▶ die "start_scope_upgrade_p: 'store' must be configured\n" unless defined($self->store);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->store 0 1 1
767  
768 1 my $scopes = $self->_union_scopes($session->{scopes}, $additional_scopes);
769 1 $self->log->debug("start_scope_upgrade_p: session_id=$session->{session_id} requesting scopes=[" . join(',', @$scopes) . ']') if DEBUG;
770  
771 1 ▶ return $self->resolver->resolve_auth_server_metadata_p($session->{auth_server_url})->then(sub ($auth_meta) {
Subroutine __ANON__: called CC=1 SCAR=0.0
772 1 ▶ return $self->send_auth_request_p($auth_meta, scopes => $scopes, login_hint => $session->{account_did})->then(sub ($info) {
Subroutine __ANON__: called CC=1 SCAR=0.0
773 1 $info->{account_did} = $session->{account_did};
774 1 $info->{upgrade_session_id} = $session->{session_id};
775 1 ▶ return $self->store->save_auth_request_p($info)->then(sub { return $self->_authorization_redirect_url($auth_meta, $info) });
Subroutine __ANON__: called CC=1 SCAR=0.0
776 1 });
777 1 });
778 }
779  
780 # Uses the stored refresh token to mint a new access token, without
781 # involving the user.
782 # Reuses the session's own DPoP key - RFC 9449 requires the same key for
783 # every proof across one authorization's lifetime, refreshing never
784 # generates a new one. Persists the new tokens via `store` and returns
785 # the session.
786 #
787 # The auth server rotates the refresh token on every use and rejects a
788 # replayed one (ATProto auth servers may revoke the whole session for
789 # it), so a refresh token must never be presented twice - not even by
790 # two processes sharing one stored session. The refresh therefore runs
791 # under the store's per-session lock, against a fresh read of the row:
792 # if that row's refresh_token no longer matches the caller's copy,
793 # another process already refreshed and its result is returned instead
794 # of refreshing again. As a backstop for a store that can't lock, an
795 # invalid_grant is answered by re-reading the row once and returning it
796 # if its refresh_token has changed in the meantime.
797 5 ▶sub refresh_tokens($self, $session) {
Subroutine refresh_tokens: called CC=4 SCAR=13.9
Pod documented
798 5 ▶ die "refresh_tokens: 'store' must be configured\n" unless defined($self->store);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->store 0 5 5
799 5 $self->log->debug("refresh_tokens: account_did=$session->{account_did} session_id=$session->{session_id}") if DEBUG;
800  
801 5 my ($account_did, $session_id, $seen_refresh_token) = @{$session}{qw/account_did session_id refresh_token/};
802 5 my $refreshed;
803 5 try {
804 5 ▶ $refreshed = $self->_with_locked_session($account_did, $session_id, sub($current) {
Subroutine __ANON__: called CC=2 SCAR=6.9
805 5 ▶ return $current if $self->_refreshed_elsewhere($seen_refresh_token, $current);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_refreshed_elsewhere($seen_refresh_token, $current) 1 4 5
806 4 return $self->_send_refresh_request($current);
807 5 });
808 } catch($ex) {
809 2 ▶ die $ex unless $ex =~ /: invalid_grant\n\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $ex =~ /: invalid_grant\n\z/u 0 2 2
810 2 my $current = $self->store->get_session($account_did, $session_id);
811 2 ▶ die $ex unless $self->_refreshed_elsewhere($seen_refresh_token, $current);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_refreshed_elsewhere($seen_refresh_token, $current) 1 1 2
812 1 $refreshed = $current;
813 }
814  
815 4 %$session = %$refreshed;
816 4 return $session;
817 }
818  
819 10 ▶sub refresh_tokens_p($self, $session) {
Subroutine refresh_tokens_p: called CC=2 SCAR=6.9
Pod documented
820 10 ▶ die "refresh_tokens_p: 'store' must be configured\n" unless defined($self->store);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->store 0 10 10
821 10 $self->log->debug("refresh_tokens_p: account_did=$session->{account_did} session_id=$session->{session_id}") if DEBUG;
822  
823 10 my ($account_did, $session_id, $seen_refresh_token) = @{$session}{qw/account_did session_id refresh_token/};
824 10 ▶ return $self->_with_locked_session_p($account_did, $session_id, sub($current) {
Subroutine __ANON__: called CC=2 SCAR=6.9
825 10 ▶ return $current if $self->_refreshed_elsewhere($seen_refresh_token, $current);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_refreshed_elsewhere($seen_refresh_token, $current) 3 7 10
826 7 return $self->_send_refresh_request_p($current);
827 2 ▶ })->catch(sub($ex) {
Subroutine __ANON__: called CC=2 SCAR=7.0
828 2 ▶ die $ex unless $ex =~ /: invalid_grant\n\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $ex =~ /: invalid_grant\n\z/u 0 2 2
829 2 ▶ return $self->store->get_session_p($account_did, $session_id)->then(sub($current) {
Subroutine __ANON__: called CC=2 SCAR=6.9
830 2 ▶ die $ex unless $self->_refreshed_elsewhere($seen_refresh_token, $current);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_refreshed_elsewhere($seen_refresh_token, $current) 1 1 2
831 1 return $current;
832 2 });
833 9 ▶ })->then(sub($refreshed) {
Subroutine __ANON__: called CC=1 SCAR=0.0
834 9 %$session = %$refreshed;
835 9 return $session;
836 10 });
837 }
838  
839 # True if $current (a fresh read of the stored session) carries a
840 # different refresh token than the one the caller started with - i.e.
841 # someone else has already refreshed this session.
842 19 ▶sub _refreshed_elsewhere($self, $seen_refresh_token, $current) {
Subroutine _refreshed_elsewhere: called CC=4 SCAR=14.3
843 19 ▶ return 0 if ($current->{refresh_token} // '') eq ($seen_refresh_token // '');
Branch: 1 branch
BranchTrueFalseTotal
if ($current->{"refresh_token"} // "") eq ($seen_refresh_token // "") 13 6 19
Condition50%2/4
$current->{"refresh_token"} // ""
l!l
190
$seen_refresh_token // ""
l!l
190
MC/DC0%0/1
$current->{"refresh_token"} // ""
$current->{"refresh_token"}
MC/DC0%0/1
$seen_refresh_token // ""
$seen_refresh_token
Truth table50%1/2
A
A= $current->{"refresh_token"}
Aresult
11
00
Truth table50%1/2
A
A= $seen_refresh_token
Aresult
11
00
844 6 $self->log->debug("refresh_tokens: already refreshed elsewhere, session_id=$current->{session_id}") if DEBUG;
845 6 return 1;
846 }
847  
848 # Performs the actual refresh-token grant for $session and persists only
849 # the fields it changes. Must only run under the store's session lock -
850 # see refresh_tokens.
851 4 ▶sub _send_refresh_request($self, $session) {
Subroutine _send_refresh_request: called CC=1 SCAR=0.0
852 4 my ($body, $dpop_key) = $self->_refresh_request_args($session);
853 my ($res, $dpop_nonce) = $self->_post_dpop_retry(
854 url => $session->{auth_server_token_endpoint}, body => $body, key => $dpop_key,
855 4 nonce => $session->{dpop_authserver_nonce}, label => 'token refresh',
856 );
857 4 my $fields = $self->_refreshed_fields($res, $dpop_nonce);
858 2 $self->_update_stored_session($session->{account_did}, $session->{session_id}, $fields);
859 2 $self->log->debug("refresh_tokens: succeeded and persisted, session_id=$session->{session_id}") if DEBUG;
860 2 return {%$session, %$fields};
861 }
862  
863 7 ▶sub _send_refresh_request_p($self, $session) {
Subroutine _send_refresh_request_p: called CC=1 SCAR=0.0
864 7 my ($body, $dpop_key) = $self->_refresh_request_args($session);
865 return $self->_post_dpop_retry_p(
866 url => $session->{auth_server_token_endpoint}, body => $body, key => $dpop_key,
867 nonce => $session->{dpop_authserver_nonce}, label => 'token refresh',
868 7 ▶ )->then(sub ($res, $dpop_nonce) {
Subroutine __ANON__: called CC=1 SCAR=0.0
869 7 my $fields = $self->_refreshed_fields($res, $dpop_nonce);
870 5 ▶ return $self->_update_stored_session_p($session->{account_did}, $session->{session_id}, $fields)->then(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
871 5 $self->log->debug("refresh_tokens_p: succeeded and persisted, session_id=$session->{session_id}") if DEBUG;
872 5 return {%$session, %$fields};
873 5 });
874 7 });
875 }
876  
877 11 ▶sub _refresh_request_args($self, $session) {
Subroutine _refresh_request_args: called CC=1 SCAR=0.0
878 my $body = {
879 client_id => $self->client_id,
880 grant_type => 'refresh_token',
881 refresh_token => $session->{refresh_token},
882 11 %{$self->_client_assertion_params($session->{auth_server_url})},
883 };
884 11 return ($body, Mojo::ATProto::OAuth::DPoP->import_private_pem($session->{dpop_private_key_pem}));
885 }
886  
887 # Validates a refresh-token grant response and returns the session fields
888 # it changes.
889 11 ▶sub _refreshed_fields($self, $res, $dpop_nonce) {
Subroutine _refreshed_fields: called CC=2 SCAR=6.9
890 11 ▶ die "token refresh failed (HTTP " . $res->code . "): " . $self->_parse_auth_error_reason($res) . "\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $res->code == 200 4 7 11
891 unless $res->code == 200;
892 7 my $token_resp = $res->json;
893 return {
894 access_token => $token_resp->{access_token},
895 refresh_token => $token_resp->{refresh_token},
896 7 dpop_authserver_nonce => $dpop_nonce,
897 };
898 }
899  
900 # Store access for the two session-concurrency methods
901 # (update_session/lock_session) that a duck-typed store written against
902 # an earlier version of the store interface may not implement. Such a
903 # store falls back to the old behaviour: a read-modify-write save, and a
904 # plain fresh read with no locking - both still racy, which is exactly
905 # what the new methods fix. Also used by
906 # Mojo::ATProto::OAuth::ResourceClient for its DPoP nonce saves.
907 5 ▶sub _update_stored_session($self, $account_did, $session_id, $fields) {
Subroutine _update_stored_session: called CC=2 SCAR=6.9
908 5 my $store = $self->store;
909 5 ▶ return $store->update_session($account_did, $session_id, $fields) if $store->can('update_session');
Branch: 1 branch
BranchTrueFalseTotal
if $store->can("update_session") 4 1 5
910 1 $store->save_session({%{$store->get_session($account_did, $session_id)}, %$fields});
911 1 return;
912 }
913  
914 6 ▶sub _update_stored_session_p($self, $account_did, $session_id, $fields) {
Subroutine _update_stored_session_p: called CC=2 SCAR=6.9
915 6 my $store = $self->store;
916 6 ▶ return $store->update_session_p($account_did, $session_id, $fields) if $store->can('update_session_p');
Branch: 1 branch
BranchTrueFalseTotal
if $store->can("update_session_p") 5 1 6
917 1 ▶ return $store->get_session_p($account_did, $session_id)->then(sub($current) {
Subroutine __ANON__: called CC=1 SCAR=0.0
918 1 return $store->save_session_p({%$current, %$fields});
919 1 });
920 }
921  
922 5 ▶sub _with_locked_session($self, $account_did, $session_id, $code) {
Subroutine _with_locked_session: called CC=2 SCAR=6.9
923 5 my $store = $self->store;
924 5 ▶ return $store->lock_session($account_did, $session_id, $code) if $store->can('lock_session');
Branch: 1 branch
BranchTrueFalseTotal
if $store->can("lock_session") 2 3 5
925 3 return $code->($store->get_session($account_did, $session_id));
926 }
927  
928 10 ▶sub _with_locked_session_p($self, $account_did, $session_id, $code) {
Subroutine _with_locked_session_p: called CC=2 SCAR=6.9
929 10 my $store = $self->store;
930 10 ▶ return $store->lock_session_p($account_did, $session_id, $code) if $store->can('lock_session_p');
Branch: 1 branch
BranchTrueFalseTotal
if $store->can("lock_session_p") 7 3 10
931 3 return $store->get_session_p($account_did, $session_id)->then($code);
932 }
933  
934 7 1;
935  
936 __END__
« OAuth/ResourceClient.pm ↑ Summary OAuth/DPoP.pm »