lib/At.pm

filter: Statementstmt 42.1% 166 / 394 Branchbran 21.6% 42 / 194 Conditioncond 14.1% 15 / 106 MC/DCmcdc 4.7% 3 / 63 Subroutinesub 41.6% 30 / 72 Podpod 81.4% 22 / 27 totaltotal 32.4% 278 / 856 CC 161 SCAR 37.7
CC
161
Cov
32%
post
182.0
oauth_discover
156.0
get
110.0
CRAP 43.5
« lib/At/Protocol/Session.pm ↑ Summary blib/lib/At/Protocol/Firehose.pm »
1 4 ▶use v5.42;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 4 ▶use feature 'class';
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 ▶no warnings 'experimental::class', 'experimental::builtin', 'experimental::for_list'; # Be quiet.
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 #~ |---------------------------------------|
6 #~ |------3-33-----------------------------|
7 #~ |-5-55------4-44-5-55----353--3-33-/1~--|
8 #~ |---------------------335---33----------|
9 4 class At v1.8.0 {
10 4 ▶ use Carp qw[];
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶ use experimental 'try';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶ use File::ShareDir::Tiny qw[dist_dir];
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶ use JSON::PP qw[decode_json encode_json];
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 ▶ use Path::Tiny qw[path];
Subroutine BEGIN: called CC=1 SCAR=0.0
15 4 ▶ use Digest::SHA qw[sha256];
Subroutine BEGIN: called CC=1 SCAR=0.0
16 4 ▶ use MIME::Base64 qw[encode_base64url];
Subroutine BEGIN: called CC=1 SCAR=0.0
17 4 ▶ use Crypt::PK::ECC;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 4 ▶ use Crypt::PRNG qw[random_string];
Subroutine BEGIN: called CC=1 SCAR=0.0
19 4 ▶ use Time::Moment; # Internal; standardize around Zulu
Subroutine BEGIN: called CC=1 SCAR=0.0
20 4 ▶ use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 4 ▶ use warnings::register;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 4 ▶ use At::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 4 ▶ use At::Protocol::URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 4 ▶ use At::Protocol::Session;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 4 ▶ use At::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 4 field $share : reader : param = ();
27 4 field %lexicons : reader;
28 4 field $http : reader : param = ();
29 4 field $lexicon_paths_param : param(lexicon_paths) = [];
30 4 field @lexicon_paths;
31 4 field $host : param : reader //= 'bsky.social';
32 4 field $ipfs_node : param : reader //= undef;
33 4 field $bitswap;
34 1 ▶ method set_host ($new) { $host = $new }
Subroutine set_host: called CC=1 SCAR=0.0
Pod undocumented
35 4 field $session = ();
36 4 field $oauth_state;
37 4 field $dpop_key;
38 field %ratelimits : reader = ( # https://docs.bsky.app/docs/advanced-guides/rate-limits
39 global => {},
40 updateHandle => {}, # per DID
41 createSession => {}, # per handle
42 deleteAccount => {}, # by IP
43 resetPassword => {} # by IP
44 4 );
45 8 ▶ ADJUST {
Subroutine __ANON__: called CC=8 SCAR=22.3
46 8 ▶ if ($ipfs_node) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ipfs_node) 0 8 8
47 0 require InterPlanetary::Protocol::Bitswap;
48 0 $bitswap = InterPlanetary::Protocol::Bitswap->new( node => $ipfs_node );
49 }
50 8 ▶ if ( !defined $share ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $share) 7 1 8
51 7 try { $share = dist_dir('At') }
52 0 catch ($e) { $share = 'share' }
53 }
54 8 ▶ $share = path($share) unless builtin::blessed $share;
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($share) 8 0 8
55 8 ▶ @lexicon_paths = map { path($_) } ( ref $lexicon_paths_param eq 'ARRAY' ? @$lexicon_paths_param : ($lexicon_paths_param) );
Branch: 1 branch
BranchTrueFalseTotal
ref $lexicon_paths_param eq 'ARRAY' ? : 8 0 8
56 8 ▶ if ( !defined $http ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $http) 8 0 8
57 8 my $ua_class;
58 8 try {
59 8 require Mojo::UserAgent;
60 8 $ua_class = 'At::UserAgent::Mojo';
61 }
62 catch ($e) {
63 0 $ua_class = 'At::UserAgent::Tiny';
64 }
65 8 $http = $ua_class->new();
66 }
67 8 ▶ $host = 'https://' . $host unless $host =~ /^https?:/;
Branch: 1 branch
BranchTrueFalseTotal
unless $host =~ /^https?:/u 7 1 8
68 8 ▶ $host = URI->new($host) unless builtin::blessed $host;
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($host) 8 0 8
69 }
70  
71 # OAuth Implementation
72 1 ▶ method _get_dpop_key() {
Subroutine _get_dpop_key: called CC=2 SCAR=9.2
73 1 ▶ unless ($dpop_key) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dpop_key) 0 1 1
74 0 $dpop_key = Crypt::PK::ECC->new();
75 0 $dpop_key->generate_key('secp256r1');
76 }
77 1 return $dpop_key;
78 }
79  
80 # Build a DPoP header for an OAuth token endpoint request. Evaluates the
81 # proof in scalar context so a missing key yields no header (and no warning).
82 1 ▶ method _dpop_headers ( $url, $method, $skip_ath = 0 ) {
Subroutine _dpop_headers: called CC=4 SCAR=20.7
83 1 ▶ return {} unless $http->token_type eq 'DPoP';
Branch: 1 branch
BranchTrueFalseTotal
unless $http->token_type eq "DPoP" 1 0 1
84 0 my $proof = $http->_generate_dpop_proof( $url, $method, $skip_ath );
85 0 ▶ return defined $proof ? { DPoP => $proof } : {};
Branch: 1 branch
BranchTrueFalseTotal
defined $proof ? : 0 0 0
86 }
87  
88 0 ▶ method oauth_discover ($handle) {
Subroutine oauth_discover: not called CC=12 SCAR=50.5
Pod undocumented
89 0 my $res = $self->resolve_handle($handle);
90 0 ▶ if ( builtin::blessed($res) && $res->isa('At::Error') ) { $res->throw; }
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($res) and $res->isa("At::Error")) 0 0 0
Condition0%0/3
builtin::blessed($res) and $res->isa("At::Error")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($res) and $res->isa("At::Error")
builtin::blessed($res) $res->isa("At::Error")
Truth table0%0/3
A and B
A= builtin::blessed($res)B= $res->isa("At::Error")
ABresult
0X0
100
111
91 0 ▶ return unless $res && $res->{did};
Branch: 1 branch
BranchTrueFalseTotal
unless $res and $res->{"did"} 0 0 0
Condition0%0/3
$res and $res->{"did"}
!ll&&!rl&&r
000
MC/DC0%0/2
$res and $res->{"did"}
$res $res->{"did"}
Truth table0%0/3
A and B
A= $resB= $res->{"did"}
ABresult
0X0
100
111
92 0 my $pds = $self->pds_for_did( $res->{did} );
93 0 ▶ unless ($pds) { die 'Could not resolve PDS for DID: ' . $res->{did}; }
Branch: 1 branch
BranchTrueFalseTotal
unless ($pds) 0 0 0
94 0 my ($protected) = $http->get( $pds . '/.well-known/oauth-protected-resource' );
95 0 ▶ if ( builtin::blessed($protected) && $protected->isa('At::Error') ) { $protected->throw; }
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($protected) and $protected->isa("At::Error")) 0 0 0
Condition0%0/3
builtin::blessed($protected) and $protected->isa("At::Error")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($protected) and $protected->isa("At::Error")
builtin::blessed($protected) $protected->isa("At::Error")
Truth table0%0/3
A and B
A= builtin::blessed($protected)B= $protected->isa("At::Error")
ABresult
0X0
100
111
96 0 ▶ return unless $protected && $protected->{authorization_servers};
Branch: 1 branch
BranchTrueFalseTotal
unless $protected and $protected->{"authorization_servers"} 0 0 0
Condition0%0/3
$protected and $protected->{"authorization_servers"}
!ll&&!rl&&r
000
MC/DC0%0/2
$protected and $protected->{"authorization_servers"}
$protected $protected->{"authorization_servers"}
Truth table0%0/3
A and B
A= $protectedB= $protected->{"authorization_servers"}
ABresult
0X0
100
111
97 0 my $auth_server = $protected->{authorization_servers}[0];
98 0 my ($metadata) = $http->get( $auth_server . '/.well-known/oauth-authorization-server' );
99 0 ▶ if ( builtin::blessed($metadata) && $metadata->isa('At::Error') ) { $metadata->throw; }
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($metadata) and $metadata->isa("At::Error")) 0 0 0
Condition0%0/3
builtin::blessed($metadata) and $metadata->isa("At::Error")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($metadata) and $metadata->isa("At::Error")
builtin::blessed($metadata) $metadata->isa("At::Error")
Truth table0%0/3
A and B
A= builtin::blessed($metadata)B= $metadata->isa("At::Error")
ABresult
0X0
100
111
100 0 return { pds => $pds, auth_server => $auth_server, metadata => $metadata, did => $res->{did} };
101 }
102  
103 0 ▶ method oauth_start ( $handle, $client_id, $redirect_uri, $scope = 'atproto' ) {
Subroutine oauth_start: not called CC=6 SCAR=37.4
Pod documented
104 0 my $discovery = $self->oauth_discover($handle);
105 0 ▶ die 'Failed to discover OAuth metadata for ' . $handle unless $discovery;
Branch: 1 branch
BranchTrueFalseTotal
unless $discovery 0 0 0
106 0 my $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._~';
107 0 my $code_verifier = Crypt::PRNG::random_string_from( $chars, 43 );
108 0 my $code_challenge = encode_base64url( sha256($code_verifier) );
109 0 $code_challenge =~ s/=+$//;
110 0 my $state = Crypt::PRNG::random_string_from( $chars, 16 );
111 0 $oauth_state = {
112 discovery => $discovery,
113 code_verifier => $code_verifier,
114 state => $state,
115 redirect_uri => $redirect_uri,
116 client_id => $client_id,
117 handle => $handle,
118 scope => $scope
119 };
120  
121 # Prepare UA for DPoP
122 0 $http->set_tokens( undef, undef, 'DPoP', $self->_get_dpop_key() );
123 0 my $par_endpoint = $discovery->{metadata}{pushed_authorization_request_endpoint};
124 my $par_content = {
125 client_id => $client_id,
126 response_type => 'code',
127 code_challenge => $code_challenge,
128 code_challenge_method => 'S256',
129 redirect_uri => $redirect_uri,
130 state => $state,
131 scope => $scope,
132 aud => $discovery->{pds},
133 0 };
134 0 ▶ say '[DEBUG] [At] PAR request: ' . JSON::PP->new->ascii->encode($par_content) if $ENV{DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"DEBUG"} 0 0 0
135 0 my ($par_res)
136 = $http->post( $par_endpoint =>
137 { headers => $self->_dpop_headers( $par_endpoint, 'POST', 1 ), encoding => 'form', content => $par_content, skip_ath => 1 } );
138 0 ▶ die 'PAR failed: ' . ( $par_res . '' ) if builtin::blessed $par_res;
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($par_res) 0 0 0
139 0 ▶ say '[DEBUG] [At] PAR response: ' . JSON::PP->new->ascii->encode($par_res) if $ENV{DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"DEBUG"} 0 0 0
140 0 my $auth_uri = URI->new( $discovery->{metadata}{authorization_endpoint} );
141 0 $auth_uri->query_form( client_id => $client_id, request_uri => $par_res->{request_uri} );
142 0 return $auth_uri->as_string;
143 }
144  
145 0 ▶ method oauth_callback ( $code, $state ) {
Subroutine oauth_callback: not called CC=5 SCAR=34.0
Pod documented
146 0 ▶ die 'OAuth state mismatch' unless $oauth_state && $state eq $oauth_state->{state};
Branch: 1 branch
BranchTrueFalseTotal
unless $oauth_state and $state eq $oauth_state->{"state"} 0 0 0
Condition0%0/3
$oauth_state and $state eq $oauth_state->{"state"}
!ll&&!rl&&r
000
MC/DC0%0/2
$oauth_state and $state eq $oauth_state->{"state"}
$oauth_state $state eq $oauth_state->{"state"}
Truth table0%0/3
A and B
A= $oauth_stateB= $state eq $oauth_state->{"state"}
ABresult
0X0
100
111
147 0 my $token_endpoint = $oauth_state->{discovery}{metadata}{token_endpoint};
148 0 my $key = $self->_get_dpop_key();
149 my ($token_res) = $http->post(
150 $token_endpoint => {
151 headers => $self->_dpop_headers( $token_endpoint, 'POST', 1 ),
152 encoding => 'form',
153 content => {
154 grant_type => 'authorization_code',
155 code => $code,
156 client_id => $oauth_state->{client_id},
157 redirect_uri => $oauth_state->{redirect_uri},
158 code_verifier => $oauth_state->{code_verifier},
159 aud => $oauth_state->{discovery}{pds}
160 },
161 0 skip_ath => 1
162 }
163 );
164 0 ▶ die 'Token exchange failed: ' . ( $token_res . '' ) if builtin::blessed $token_res;
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($token_res) 0 0 0
165 0 ▶ say '[DEBUG] [At] Token response: ' . JSON::PP->new->ascii->encode($token_res) if $ENV{DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"DEBUG"} 0 0 0
166 $session = At::Protocol::Session->new(
167 did => $token_res->{sub},
168 accessJwt => $token_res->{access_token},
169 refreshJwt => $token_res->{refresh_token},
170 handle => $oauth_state->{handle},
171 token_type => 'DPoP',
172 dpop_key_jwk => $key->export_key_jwk('private'),
173 client_id => $oauth_state->{client_id},
174 scope => $token_res->{scope},
175 pds => $oauth_state->{discovery}{pds}
176 0 );
177 0 $self->set_host( $oauth_state->{discovery}{pds} );
178 0 $http->set_tokens( $token_res->{access_token}, $token_res->{refresh_token}, 'DPoP', $key );
179 }
180  
181 2 ▶ method oauth_refresh() {
Subroutine oauth_refresh: called CC=8 SCAR=22.3
Pod documented
182 2 ▶ return unless $session && $session->refreshJwt && $session->token_type eq 'DPoP';
Branch: 1 branch
BranchTrueFalseTotal
unless $session and $session->refreshJwt and $session->token_type eq "DPoP" 0 2 2
Condition33%2/6
$session and $session->refreshJwt and $session->token_type eq "DPoP"
!ll&&!rl&&r
002
$session and $session->refreshJwt
!ll&&!rl&&r
002
MC/DC0%0/3
$session and $session->refreshJwt and $session->token_type eq "DPoP"
$session $session->refreshJwt $session->token_type eq "DPoP"
Truth table25%1/4
A and B and C
A= $sessionB= $session->refreshJwtC= $session->token_type eq "DPoP"
ABCresult
0XX0
10X0
1100
1111
183 2 my $discovery = $self->oauth_discover( $session->handle );
184 2 ▶ return unless $discovery;
Branch: 1 branch
BranchTrueFalseTotal
unless $discovery 1 1 2
185 1 my $token_endpoint = $discovery->{metadata}{token_endpoint};
186 1 my $key = $self->_get_dpop_key();
187  
188 # Bind the DPoP key to the agent so the proof below is actually signed.
189 1 $http->set_tokens( undef, undef, 'DPoP', $key );
190 my $refresh_content = {
191 grant_type => 'refresh_token',
192 refresh_token => $session->refreshJwt,
193 client_id => $session->client_id // '',
194 aud => $discovery->{pds},
195 1 ▶ };
Condition50%1/2
$session->client_id // ""
l!l
10
MC/DC0%0/1
$session->client_id // ""
$session->client_id
Truth table50%1/2
A
A= $session->client_id
Aresult
11
00
196 1 ▶ say '[DEBUG] [At] Refresh request: ' . JSON::PP->new->ascii->encode($refresh_content) if $ENV{DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"DEBUG"} 0 1 1
197 1 my ($token_res)
198 = $http->post( $token_endpoint =>
199 { headers => $self->_dpop_headers( $token_endpoint, 'POST', 1 ), encoding => 'form', content => $refresh_content, skip_ath => 1 } );
200 1 ▶ die 'Refresh failed: ' . ( $token_res . '' ) if builtin::blessed $token_res;
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($token_res) 0 1 1
201 $session = At::Protocol::Session->new(
202 did => $token_res->{sub},
203 accessJwt => $token_res->{access_token},
204 refreshJwt => $token_res->{refresh_token},
205 handle => $session->handle,
206 token_type => 'DPoP',
207 dpop_key_jwk => $key->export_key_jwk('private'),
208 client_id => $session->client_id,
209 scope => $token_res->{scope},
210 pds => $discovery->{pds}
211 1 );
212 1 $self->set_host( $discovery->{pds} );
213 1 return $http->set_tokens( $token_res->{access_token}, $token_res->{refresh_token}, 'DPoP', $key );
214 }
215  
216 0 ▶ method collection_scope ( $collection, $action = 'create' ) {
Subroutine collection_scope: not called CC=2 SCAR=17.9
Pod documented
217 0 return "repo:$collection?action=$action";
218 }
219  
220 # Legacy Auth
221 0 ▶ method login( $identifier, $password ) {
Subroutine login: not called CC=4 SCAR=30.0
Pod documented
222 0 warnings::warnif( At => 'login() (com.atproto.server.createSession) is deprecated. Please use OAuth instead.' );
223 0 my $res = $self->post( 'com.atproto.server.createSession' => { identifier => $identifier, password => $password } );
224 0 ▶ if ( $res && !builtin::blessed($res) ) { $session = At::Protocol::Session->new(%$res); }
Branch: 1 branch
BranchTrueFalseTotal
if ($res and not builtin::blessed($res)) { } 0 0 0
Condition0%0/3
$res and not builtin::blessed($res)
!ll&&!rl&&r
000
MC/DC0%0/2
$res and not builtin::blessed($res)
$res not builtin::blessed($res)
Truth table0%0/3
A and not(B)
A= $resB= not builtin::blessed($res)
ABresult
0X0
100
111
225 0 else { $session = $res; }
226 0 ▶ return $session ? $http->set_tokens( $session->accessJwt, $session->refreshJwt, undef, undef ) : $session;
Branch: 1 branch
BranchTrueFalseTotal
$session ? : 0 0 0
227 }
228  
229 3 ▶ method resume ( $accessJwt, $refreshJwt, $token_type = 'Bearer', $dpop_key_jwk = (), $client_id = (), $handle = (), $pds = (), $scope = () ) {
Subroutine resume: called CC=22 SCAR=39.2
Pod documented
230 3 my $access = $self->_decode_token($accessJwt);
231 3 my $refresh = $self->_decode_token($refreshJwt);
232 3 ▶ return unless $access;
Branch: 1 branch
BranchTrueFalseTotal
unless $access 0 3 3
233 3 my $key;
234 3 ▶ if ( $token_type eq 'DPoP' && $dpop_key_jwk ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token_type eq "DPoP" and $dpop_key_jwk) 3 0 3
Condition33%1/3
$token_type eq "DPoP" and $dpop_key_jwk
!ll&&!rl&&r
003
MC/DC0%0/2
$token_type eq "DPoP" and $dpop_key_jwk
$token_type eq "DPoP" $dpop_key_jwk
Truth table33%1/3
A and B
A= $token_type eq "DPoP"B= $dpop_key_jwk
ABresult
0X0
100
111
235 3 $key = Crypt::PK::ECC->new();
236 3 $key->import_key( \$dpop_key_jwk );
237 3 $dpop_key = $key;
238 }
239  
240 # OAuth refresh tokens are typically opaque (un-decodable), so fall back
241 # to a refresh attempt whenever the access token has expired and any
242 # refresh token is present.
243 3 ▶ my $access_expired = defined $access->{exp} && time > $access->{exp};
Condition66%2/3
defined $access->{'exp'} && time > $access->{'exp'}
!ll&&!rl&&r
012
MC/DC50%1/2
defined $access->{'exp'} && time > $access->{'exp'}
defined $access->{'exp'} time > $access->{'exp'}
Truth table66%2/3
A && B
A= defined $access->{'exp'}B= time > $access->{'exp'}
ABresult
0X0
100
111
244 3 ▶ my $can_refresh = $refreshJwt && ( !$refresh || !defined $refresh->{exp} || time < $refresh->{exp} );
Condition33%3/9
$refreshJwt && (!($refresh && defined $refresh->{'exp'}) || time < $refresh->{'exp'})
!ll&&!rl&&r
003
not $refresh && defined $refresh->{"exp"} or time < $refresh->{"exp"}
l!l&&r!l&&!r
300
$refresh && defined $refresh->{'exp'}
!ll&&!rl&&r
300
MC/DC0%0/4
$refreshJwt && (!($refresh && defined $refresh->{'exp'}) || time < $refresh->{'exp'})
$refreshJwt $refresh defined $refresh->{'exp'} time < $refresh->{"exp"}
Truth table20%1/5
A && not(B && C) or D
A= $refreshJwtB= $refreshC= defined $refresh->{'exp'}D= time < $refresh->{"exp"}
ABCDresult
0XXX0
11100
10XX1
110X1
11111
245 3 ▶ if ( $access_expired && $can_refresh ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($access_expired and $can_refresh) 2 1 3
Condition66%2/3
$access_expired and $can_refresh
!ll&&!rl&&r
102
MC/DC50%1/2
$access_expired and $can_refresh
$access_expired $can_refresh
Truth table66%2/3
A and B
A= $access_expiredB= $can_refresh
ABresult
0X0
100
111
246 2 ▶ if ( $token_type eq 'DPoP' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token_type eq 'DPoP') { } 2 0 2
247  
248 # Seed the session first so oauth_refresh() can read handle/client_id/pds
249 $session = At::Protocol::Session->new(
250 did => $access->{sub},
251 2 accessJwt => $accessJwt,
252 refreshJwt => $refreshJwt,
253 token_type => $token_type,
254 dpop_key_jwk => $dpop_key_jwk,
255 client_id => $client_id,
256 handle => $handle,
257 pds => $pds,
258 scope => $scope
259 );
260 2 my $refreshed = $self->oauth_refresh();
261  
262 # Do not leave an expired session in place if the refresh bailed;
263 # callers should see "no session" and re-authenticate instead.
264 2 ▶ $session = () unless $refreshed;
Branch: 1 branch
BranchTrueFalseTotal
unless $refreshed 1 1 2
265 2 return $refreshed;
266 }
267 else {
268 0 my $res = $self->post( 'com.atproto.server.refreshSession' => { refreshJwt => $refreshJwt } );
269 0 ▶ if ( $res && !builtin::blessed($res) ) { $session = At::Protocol::Session->new(%$res); }
Branch: 1 branch
BranchTrueFalseTotal
if ($res and not builtin::blessed($res)) { } 0 0 0
Condition0%0/3
$res and not builtin::blessed($res)
!ll&&!rl&&r
000
MC/DC0%0/2
$res and not builtin::blessed($res)
$res not builtin::blessed($res)
Truth table0%0/3
A and not(B)
A= $resB= not builtin::blessed($res)
ABresult
0X0
100
111
270 0 else { $session = $res; }
271 0 ▶ return $session ? $http->set_tokens( $session->accessJwt, $session->refreshJwt, $token_type, $key ) : $session;
Branch: 1 branch
BranchTrueFalseTotal
$session ? : 0 0 0
272 }
273 }
274 $session = At::Protocol::Session->new(
275 did => $access->{sub},
276 1 accessJwt => $accessJwt,
277 refreshJwt => $refreshJwt,
278 token_type => $token_type,
279 dpop_key_jwk => $dpop_key_jwk,
280 client_id => $client_id,
281 handle => $handle,
282 pds => $pds,
283 scope => $scope
284 );
285 1 ▶ $self->set_host($pds) if $pds;
Branch: 1 branch
BranchTrueFalseTotal
if $pds 0 1 1
286 1 return $http->set_tokens( $accessJwt, $refreshJwt, $token_type, $key );
287 }
288  
289 6 ▶ method _decode_token ($token) {
Subroutine _decode_token: called CC=3 SCAR=11.1
290 6 ▶ return unless defined $token;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $token 0 6 6
291 4 ▶ use MIME::Base64 qw[decode_base64];
Subroutine BEGIN: called CC=1 SCAR=0.0
292 6 my ( $header, $payload, $sig ) = split /\./, $token;
293 6 ▶ return unless defined $payload;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $payload 3 3 6
294 3 $payload =~ tr[-_][+/];
295 3 try {
296 3 return decode_json decode_base64 $payload;
297 }
298 catch ($e) {
299 0 return;
300 }
301 }
302  
303 # XRPC & Lexicons
304 8 ▶ method _locate_lexicon($fqdn) {
Subroutine _locate_lexicon: called CC=15 SCAR=28.6
305 8 ▶ $fqdn // return undef;
Condition50%1/2
$fqdn // (return undef)
l!l
80
MC/DC0%0/1
$fqdn // (return undef)
$fqdn
Truth table50%1/2
A
A= $fqdn
Aresult
11
00
306 8 $fqdn =~ s/^#//; # Strip leading hash
307 8 ▶ unless ( defined $lexicons{$fqdn} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $lexicons{$fqdn}) 7 1 8
308 7 my $base_fqdn = $fqdn =~ s[#(.+)$][]r;
309 7 ▶ return undef unless $base_fqdn;
Branch: 1 branch
BranchTrueFalseTotal
unless $base_fqdn 0 7 7
310 7 my @namespace = split /\./, $base_fqdn;
311 7 ▶ return undef unless @namespace;
Branch: 1 branch
BranchTrueFalseTotal
unless @namespace 0 7 7
312 my @search = (
313 @lexicon_paths,
314 $share->child('lexicons'),
315 7 ▶ defined $ENV{HOME} ? path( $ENV{HOME}, '.cache', 'atproto', 'lexicons' ) : (),
Branch: 1 branch
BranchTrueFalseTotal
defined $ENV{'HOME'} ? : 7 0 7
316 path( 'share', 'lexicons' )
317 );
318 7 my $lex_file;
319 7 for my $dir (@search) {
320 7 ▶ next unless defined $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dir 0 7 7
321 7 my $possible = $dir->child( @namespace[ 0 .. $#namespace - 1 ], $namespace[-1] . '.json' );
322 7 ▶ if ( $possible->exists ) { $lex_file = $possible; last; }
Branch: 1 branch
BranchTrueFalseTotal
if ($possible->exists) 7 0 7
323 }
324 0 ▶ if ( !$lex_file ) { $lex_file = $self->_fetch_lexicon($base_fqdn); }
Branch: 1 branch
BranchTrueFalseTotal
unless ($lex_file) 0 7 7
325 7 ▶ if ( $lex_file && $lex_file->exists ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lex_file and $lex_file->exists) 7 0 7
Condition33%1/3
$lex_file and $lex_file->exists
!ll&&!rl&&r
007
MC/DC0%0/2
$lex_file and $lex_file->exists
$lex_file $lex_file->exists
Truth table33%1/3
A and B
A= $lex_fileB= $lex_file->exists
ABresult
0X0
100
111
326 7 my $json = decode_json $lex_file->slurp_raw;
327 7 for my $def ( keys %{ $json->{defs} } ) {
328 17 ▶ $lexicons{ $base_fqdn . ( $def eq 'main' ? '' : '#' . $def ) } = $json->{defs}{$def};
Branch: 1 branch
BranchTrueFalseTotal
$def eq 'main' ? : 7 10 17
329 17 ▶ $lexicons{ $base_fqdn . '#main' } = $json->{defs}{$def} if $def eq 'main';
Branch: 1 branch
BranchTrueFalseTotal
if $def eq "main" 7 10 17
330 }
331 }
332 }
333 8 $lexicons{$fqdn};
334 }
335  
336 0 ▶ method _fetch_lexicon($base_fqdn) {
Subroutine _fetch_lexicon: not called CC=5 SCAR=34.0
337 0 my @namespace = split /\./, $base_fqdn;
338 0 my $rel_path = join( '/', @namespace[ 0 .. $#namespace - 1 ], $namespace[-1] . '.json' );
339 0 my $url = 'https://raw.githubusercontent.com/bluesky-social/atproto/main/lexicons/' . $rel_path;
340 0 my ( $content, $headers ) = $http->get($url);
341 0 ▶ if ( $content && !builtin::blessed($content) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content and not builtin::blessed($content)) 0 0 0
Condition0%0/3
$content and not builtin::blessed($content)
!ll&&!rl&&r
000
MC/DC0%0/2
$content and not builtin::blessed($content)
$content not builtin::blessed($content)
Truth table0%0/3
A and not(B)
A= $contentB= not builtin::blessed($content)
ABresult
0X0
100
111
342 0 ▶ my $cache_dir = defined $ENV{HOME} ? path( $ENV{HOME}, '.cache', 'atproto', 'lexicons' ) : path( '.cache', 'atproto', 'lexicons' );
Branch: 1 branch
BranchTrueFalseTotal
defined $ENV{'HOME'} ? : 0 0 0
343 0 $cache_dir->mkpath;
344 0 my $lex_file = $cache_dir->child( @namespace[ 0 .. $#namespace - 1 ], $namespace[-1] . '.json' );
345 0 $lex_file->parent->mkpath;
346 0 ▶ $lex_file->spew_raw( builtin::blessed($content) ? encode_json($content) : $content );
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($content) ? : 0 0 0
347 0 return $lex_file;
348 }
349 0 return;
350 }
351  
352 0 ▶ method get( $fqdn, $args = (), $headers = {} ) {
Subroutine get: not called CC=10 SCAR=47.0
Pod documented
353 0 my $lexicon = $self->_locate_lexicon($fqdn);
354 0 ▶ my $category = $fqdn =~ /^com\.atproto\.repo\./ ? 'repo' : 'global';
Branch: 1 branch
BranchTrueFalseTotal
$fqdn =~ /^com\.atproto\.repo\./u ? : 0 0 0
355 0 ▶ my $meta = $category eq 'repo' ? ( $args->{repo} // $self->did ) : ();
Branch: 1 branch
BranchTrueFalseTotal
$category eq 'repo' ? : 0 0 0
Condition0%0/3
$args->{"repo"} // $self->did
l!l&&r!l&&!r
000
MC/DC0%0/2
$args->{"repo"} // $self->did
$args->{"repo"} $self->did
Truth table0%0/3
A // B
A= $args->{"repo"}B= $self->did
ABresult
1X1
011
000
356 0 $self->_ratecheck( $category, $meta );
357 0 ▶ my ( $content, $res_headers )
Branch: 1 branch
BranchTrueFalseTotal
defined $args ? : 0 0 0
358 = $http->get( sprintf( '%s/xrpc/%s', $host, $fqdn ), { defined $args ? ( content => $args ) : (), headers => $headers } );
359 0 $self->ratelimit_( $res_headers, $category, $meta );
360 0 ▶ if ( $lexicon && !builtin::blessed $content ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lexicon and not builtin::blessed($content)) 0 0 0
Condition0%0/3
$lexicon and not builtin::blessed($content)
!ll&&!rl&&r
000
MC/DC0%0/2
$lexicon and not builtin::blessed($content)
$lexicon not builtin::blessed($content)
Truth table0%0/3
A and not(B)
A= $lexiconB= not builtin::blessed($content)
ABresult
0X0
100
111
361 0 $content = $self->_coerce( $fqdn, $lexicon->{output}{schema}, $content );
362 }
363 0 ▶ wantarray ? ( $content, $res_headers ) : $content;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
364 }
365  
366 0 ▶ method post( $fqdn, $args = (), $headers = {} ) {
Subroutine post: not called CC=13 SCAR=52.0
Pod documented
367 0 my @namespace = split /\./, $fqdn;
368 0 my $lexicon = $self->_locate_lexicon($fqdn);
369  
370 # Categorize according to bsky specs
371 0 my $category = 'global';
372 0 my $meta = ();
373 0 ▶ if ( $fqdn =~ /^com\.atproto\.server\.createSession$/ ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($fqdn =~ /^com\.atproto\.server\.createSession$/u) { } 0 0 0
elsif ($fqdn =~ /^com\.atproto\.repo\./u) { } 0 0 0
elsif ($namespace[-1] =~ /^(updateHandle|createAccount|deleteAccount|resetPassword)$/u) { } 0 0 0
374 0 $category = 'auth';
375 0 $meta = $args->{identifier};
376 }
377 elsif ( $fqdn =~ /^com\.atproto\.repo\./ ) {
378 0 $category = 'repo';
379 0 ▶ $meta = $args->{repo} // $self->did;
Condition0%0/3
$args->{"repo"} // $self->did
l!l&&r!l&&!r
000
MC/DC0%0/2
$args->{"repo"} // $self->did
$args->{"repo"} $self->did
Truth table0%0/3
A // B
A= $args->{"repo"}B= $self->did
ABresult
1X1
011
000
380 }
381 elsif ( $namespace[-1] =~ m[^(updateHandle|createAccount|deleteAccount|resetPassword)$] ) {
382 0 $category = $namespace[-1];
383 0 ▶ $meta = $args->{did} // $args->{handle} // $args->{email};
Condition0%0/6
($args->{"did"} // $args->{"handle"}) // $args->{"email"}
l!l&&r!l&&!r
000
$args->{"did"} // $args->{"handle"}
l!l&&r!l&&!r
000
MC/DC0%0/3
($args->{"did"} // $args->{"handle"}) // $args->{"email"}
$args->{"did"} $args->{"handle"} $args->{"email"}
Truth table0%0/4
A // B // C
A= $args->{"did"}B= $args->{"handle"}C= $args->{"email"}
ABCresult
1XX1
01X1
0011
0000
384 }
385 0 $self->_ratecheck( $category, $meta );
386 0 ▶ my ( $content, $res_headers )
Branch: 1 branch
BranchTrueFalseTotal
defined $args ? : 0 0 0
387 = $http->post( sprintf( '%s/xrpc/%s', $host, $fqdn ), { defined $args ? ( content => $args ) : (), headers => $headers } );
388 0 $self->ratelimit_( $res_headers, $category, $meta );
389 0 ▶ if ( $lexicon && !builtin::blessed $content ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lexicon and not builtin::blessed($content)) 0 0 0
Condition0%0/3
$lexicon and not builtin::blessed($content)
!ll&&!rl&&r
000
MC/DC0%0/2
$lexicon and not builtin::blessed($content)
$lexicon not builtin::blessed($content)
Truth table0%0/3
A and not(B)
A= $lexiconB= not builtin::blessed($content)
ABresult
0X0
100
111
390 0 $content = $self->_coerce( $fqdn, $lexicon->{output}{schema}, $content );
391 }
392 0 ▶ return wantarray ? ( $content, $res_headers ) : $content;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
393 }
394 0 ▶ method subscribe( $id, $cb ) { $self->http->websocket( sprintf( '%s/xrpc/%s', $host, $id ), $cb ); }
Subroutine subscribe: not called CC=1 SCAR=6.9
Pod documented
395  
396 3 ▶ method firehose ( $callback, $url = () ) {
Subroutine firehose: called CC=3 SCAR=11.0
Pod documented
397 3 require At::Protocol::Firehose;
398 3 ▶ return At::Protocol::Firehose->new( at => $self, callback => $callback, defined $url ? ( url => $url ) : () );
Branch: 1 branch
BranchTrueFalseTotal
defined $url ? : 1 2 3
399 }
400  
401 # Coercion Logic
402 my %coercions = (
403 0 ▶ array => method( $namespace, $schema, $data ) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
404 0 [ map { $self->_coerce( $namespace, $schema->{items}, $_ ) } @$data ]
405 },
406 0 ▶ boolean => method( $namespace, $schema, $data ) { !!$data },
Subroutine __ANON__: not called CC=1 SCAR=6.9
407 0 ▶ bytes => method( $namespace, $schema, $data ) {$data},
Subroutine __ANON__: not called CC=1 SCAR=6.9
408 0 ▶ blob => method( $namespace, $schema, $data ) {$data},
Subroutine __ANON__: not called CC=1 SCAR=6.9
409 0 ▶ integer => method( $namespace, $schema, $data ) { int $data },
Subroutine __ANON__: not called CC=1 SCAR=6.9
410 0 ▶ object => method( $namespace, $schema, $data ) {
Subroutine __ANON__: not called CC=2 SCAR=17.9
411 0 for my ( $name, $subschema )( %{ $schema->{properties} } ) {
412 0 $data->{$name} = $self->_coerce( $namespace, $subschema, $data->{$name} );
413 }
414 0 $data;
415 },
416 0 ▶ ref => method( $namespace, $schema, $data ) {
Subroutine __ANON__: not called CC=2 SCAR=17.9
417 0 my $target_namespace = $self->_resolve_namespace( $namespace, $schema->{ref} );
418 0 my $lexicon = $self->_locate_lexicon($target_namespace);
419 0 ▶ return $data unless $lexicon;
Branch: 1 branch
BranchTrueFalseTotal
unless $lexicon 0 0 0
420 0 $self->_coerce( $target_namespace, $lexicon, $data );
421 },
422 0 ▶ union => method( $namespace, $schema, $data ) {$data},
Subroutine __ANON__: not called CC=1 SCAR=6.9
423 0 ▶ unknown => method( $namespace, $schema, $data ) {$data},
Subroutine __ANON__: not called CC=1 SCAR=6.9
424 0 ▶ string => method( $namespace, $schema, $data ) {
Subroutine __ANON__: not called CC=9 SCAR=45.0
425 0 ▶ $data // return ();
Condition0%0/2
$data // (return ())
l!l
00
MC/DC0%0/1
$data // (return ())
$data
Truth table0%0/2
A
A= $data
Aresult
11
00
426 0 ▶ if ( defined $schema->{format} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $schema->{"format"}) 0 0 0
427 0 ▶ if ( $schema->{format} eq 'uri' ) { return URI->new($data); }
Branch: 5 branches
BranchTrueFalseTotal
if ($schema->{'format'} eq 'uri') { } 0 0 0
elsif ($schema->{'format'} eq 'at-uri') { } 0 0 0
elsif ($schema->{'format'} eq 'datetime') { } 0 0 0
elsif ($schema->{'format'} eq 'did') { } 0 0 0
elsif ($schema->{'format'} eq 'handle') { } 0 0 0
428 0 elsif ( $schema->{format} eq 'at-uri' ) { return At::Protocol::URI->new($data); }
429 elsif ( $schema->{format} eq 'datetime' ) {
430 0 ▶ return $data =~ /\D/ ? Time::Moment->from_string($data) : Time::Moment->from_epoch($data);
Branch: 1 branch
BranchTrueFalseTotal
$data =~ /\D/u ? : 0 0 0
431 }
432 elsif ( $schema->{format} eq 'did' ) {
433 0 require At::Protocol::DID;
434 0 return At::Protocol::DID->new($data);
435 }
436 elsif ( $schema->{format} eq 'handle' ) {
437 0 require At::Protocol::Handle;
438 0 try { return At::Protocol::Handle->new($data); }
439 0 catch ($e) { return $data; }
440 }
441 }
442 0 $data;
443 }
444 4 );
445  
446 0 ▶ method _coerce ( $namespace, $schema, $data ) {
Subroutine _coerce: not called CC=3 SCAR=24.8
447 0 ▶ $data // return ();
Condition0%0/2
$data // (return ())
l!l
00
MC/DC0%0/1
$data // (return ())
$data
Truth table0%0/2
A
A= $data
Aresult
11
00
448 0 ▶ return $coercions{ $schema->{type} }->( $self, $namespace, $schema, $data ) if defined $coercions{ $schema->{type} };
Branch: 1 branch
BranchTrueFalseTotal
if defined $coercions{$schema->{"type"}} 0 0 0
449 0 return $data;
450 }
451  
452 0 ▶ method _resolve_namespace ( $l, $r ) {
Subroutine _resolve_namespace: not called CC=2 SCAR=17.9
453 0 ▶ return $r if $r =~ m[\.]; # Absolute (has dots)
Branch: 1 branch
BranchTrueFalseTotal
if $r =~ /\./u 0 0 0
454 0 my $base = $l =~ s[#(.+)$][]r;
455 0 return $base . '#' . $r; # Relative to base of current FQDN
456 }
457  
458 # Identity & Helpers
459 0 ▶ method did() { $session ? $session->did . '' : undef; }
Subroutine did: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$session ? : 0 0 0
460 0 ▶ method resolve_handle($handle) { $self->get( 'com.atproto.identity.resolveHandle' => { handle => $handle } ); }
Subroutine resolve_handle: not called CC=1 SCAR=6.9
Pod documented
461  
462 0 ▶ method resolve_did_to_handle ($did) {
Subroutine resolve_did_to_handle: not called CC=3 SCAR=24.8
Pod documented
463 0 my $doc = $self->resolve_did($did);
464 0 ▶ return $doc->{alsoKnownAs}[0] =~ s/^at:\/\///r if $doc && $doc->{alsoKnownAs};
Branch: 1 branch
BranchTrueFalseTotal
if $doc and $doc->{"alsoKnownAs"} 0 0 0
Condition0%0/3
$doc and $doc->{"alsoKnownAs"}
!ll&&!rl&&r
000
MC/DC0%0/2
$doc and $doc->{"alsoKnownAs"}
$doc $doc->{"alsoKnownAs"}
Truth table0%0/3
A and B
A= $docB= $doc->{"alsoKnownAs"}
ABresult
0X0
100
111
465 0 return;
466 }
467  
468 0 ▶ method upload_blob ( $data, $mime_type ) {
Subroutine upload_blob: not called CC=1 SCAR=6.9
Pod documented
469 0 $self->post( 'com.atproto.repo.uploadBlob' => { content => $data, headers => { 'Content-Type' => $mime_type } } );
470 }
471  
472 0 ▶ method create_record ( $collection, $record, $rkey = undef ) {
Subroutine create_record: not called CC=3 SCAR=24.8
Pod documented
473 0 ▶ $self->post( 'com.atproto.repo.createRecord' =>
Branch: 1 branch
BranchTrueFalseTotal
defined $rkey ? : 0 0 0
474 { repo => $self->did, collection => $collection, record => $record, defined $rkey ? ( rkey => $rkey ) : () } );
475 }
476  
477 0 ▶ method delete_record ( $collection, $rkey ) {
Subroutine delete_record: not called CC=1 SCAR=6.9
Pod documented
478 0 $self->post( 'com.atproto.repo.deleteRecord' => { repo => $self->did, collection => $collection, rkey => $rkey } );
479 }
480  
481 0 ▶ method put_record ( $collection, $rkey, $record, $swapRecord = undef ) {
Subroutine put_record: not called CC=3 SCAR=24.8
Pod documented
482 0 ▶ $self->post(
Branch: 1 branch
BranchTrueFalseTotal
defined $swapRecord ? : 0 0 0
483 'com.atproto.repo.putRecord' => {
484 repo => $self->did,
485 collection => $collection,
486 rkey => $rkey,
487 record => $record,
488 defined $swapRecord ? ( swapRecord => $swapRecord ) : ()
489 }
490 );
491 }
492  
493 0 ▶ method apply_writes ( $writes, $swapCommit = undef ) {
Subroutine apply_writes: not called CC=3 SCAR=24.8
Pod documented
494 0 ▶ $self->post(
Branch: 1 branch
BranchTrueFalseTotal
defined $swapCommit ? : 0 0 0
495 'com.atproto.repo.applyWrites' => { repo => $self->did, writes => $writes, defined $swapCommit ? ( swapCommit => $swapCommit ) : () } );
496 }
497  
498 0 ▶ method resolve_did ($did) {
Subroutine resolve_did: not called CC=3 SCAR=24.8
Pod undocumented
499 0 ▶ if ( $did =~ /^did:plc:(.+)$/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($did =~ /^did:plc:(.+)$/u) { } 0 0 0
elsif ($did =~ /^did:web:(.+)$/u) { } 0 0 0
500 0 my ($content) = $http->get( 'https://plc.directory/' . $did );
501 0 return $content;
502 }
503 elsif ( $did =~ /^did:web:(.+)$/ ) {
504 0 my $domain = $1;
505 0 $domain =~ s/:/\//g;
506 0 my ($content) = $http->get("https://$domain/.well-known/did.json");
507 0 return $content;
508 }
509 0 return;
510 }
511  
512 0 ▶ method pds_for_did ($did) {
Subroutine pds_for_did: not called CC=6 SCAR=37.4
Pod undocumented
513 0 my $doc = $self->resolve_did($did);
514 0 ▶ return unless $doc && ref $doc eq 'HASH' && $doc->{service};
Branch: 1 branch
BranchTrueFalseTotal
unless $doc and ref $doc eq "HASH" and $doc->{"service"} 0 0 0
Condition0%0/6
$doc and ref $doc eq "HASH" and $doc->{"service"}
!ll&&!rl&&r
000
$doc and ref $doc eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/3
$doc and ref $doc eq "HASH" and $doc->{"service"}
$doc ref $doc eq "HASH" $doc->{"service"}
Truth table0%0/4
A and B and C
A= $docB= ref $doc eq "HASH"C= $doc->{"service"}
ABCresult
0XX0
10X0
1100
1111
515 0 for my $service ( @{ $doc->{service} } ) {
516 0 ▶ return $service->{serviceEndpoint} if $service->{type} eq 'AtprotoPersonalDataServer';
Branch: 1 branch
BranchTrueFalseTotal
if $service->{"type"} eq "AtprotoPersonalDataServer" 0 0 0
517 }
518 0 return;
519 }
520  
521 0 ▶ method peer_id_for_did ($did) {
Subroutine peer_id_for_did: not called CC=5 SCAR=34.0
Pod documented
522 0 my $doc = $self->resolve_did($did);
523 0 ▶ return unless $doc && ref $doc eq 'HASH' && $doc->{verificationMethod};
Branch: 1 branch
BranchTrueFalseTotal
unless $doc and ref $doc eq "HASH" and $doc->{"verificationMethod"} 0 0 0
Condition0%0/6
$doc and ref $doc eq "HASH" and $doc->{"verificationMethod"}
!ll&&!rl&&r
000
$doc and ref $doc eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/3
$doc and ref $doc eq "HASH" and $doc->{"verificationMethod"}
$doc ref $doc eq "HASH" $doc->{"verificationMethod"}
Truth table0%0/4
A and B and C
A= $docB= ref $doc eq "HASH"C= $doc->{"verificationMethod"}
ABCresult
0XX0
10X0
1100
1111
524  
525 # Look for the primary signing key (usually the first one)
526 0 my $vm = $doc->{verificationMethod}[0];
527 0 ▶ my $pub_key_multibase = $vm->{publicKeyMultibase} // return;
Condition0%0/2
$vm->{"publicKeyMultibase"} // (return)
l!l
00
MC/DC0%0/1
$vm->{"publicKeyMultibase"} // (return)
$vm->{"publicKeyMultibase"}
Truth table0%0/2
A
A= $vm->{"publicKeyMultibase"}
Aresult
11
00
528  
529 # publicKeyMultibase for secp256k1 in atproto usually starts with 'z' (base58btc)
530 # and has a multicodec prefix.
531 0 require InterPlanetary::Multibase;
532 0 my $raw = InterPlanetary::Multibase->decode($pub_key_multibase);
533  
534 # For secp256k1 (0xe7 multicodec), we need to extract the 33-byte compressed key
535 # and wrap it in the libp2p Protobuf PublicKey.
536 0 require Net::Libp2p::Crypto;
537 0 require InterPlanetary::Utils;
538  
539 # Simplified: assume it's secp256k1 for now as per bsky standard
540 0 my $key_bytes = substr( $raw, 2 ); # Skip multicodec prefix (usually 2 bytes for 0xe7)
541  
542 # Wrap in libp2p Protobuf (Type 2 = Secp256k1)
543 0 my $pk_pb = pack( 'C', ( 1 << 3 ) | 0 ) . InterPlanetary::Utils::encode_varint(2);
544 0 $pk_pb .= pack( 'C', ( 2 << 3 ) | 2 ) . InterPlanetary::Utils::encode_varint( length($key_bytes) ) . $key_bytes;
545 0 return Net::Libp2p::Crypto->peer_id_from_public_key($pk_pb);
546 }
547 4 ▶ method session() { $session //= $self->get('com.atproto.server.getSession'); $session; }
Subroutine session: called CC=2 SCAR=7.1
Pod documented
Condition66%2/3
$session //= $self->get("com.atproto.server.getSession")
l!l&&r!l&&!r
301
MC/DC50%1/2
$session //= $self->get("com.atproto.server.getSession")
$session $self->get("com.atproto.server.getSession")
Truth table66%2/3
A //= B
A= $sessionB= $self->get("com.atproto.server.getSession")
ABresult
1X1
011
000
548  
549 0 ▶ method get_block ( $cid_str, $target_peer_id = undef, $did = undef ) {
Subroutine get_block: not called CC=6 SCAR=37.4
Pod documented
550 0 ▶ if ($ipfs_node) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ipfs_node) 0 0 0
551 0 my $cid = InterPlanetary::CID->from_string($cid_str);
552 0 my $data = $ipfs_node->blockstore->get($cid);
553 0 ▶ return Future->done($data) if $data;
Branch: 1 branch
BranchTrueFalseTotal
if $data 0 0 0
554 0 ▶ if ($target_peer_id) {
Branch: 1 branch
BranchTrueFalseTotal
if ($target_peer_id) 0 0 0
555  
556 #~ say "[IPFS] Block $cid_str not found locally, attempting Bitswap from $target_peer_id...";
557 return $ipfs_node->host->dial( $target_peer_id, '/ipfs/bitswap/1.2.0' )->then(
558 0 ▶ sub ($ss) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
559  
560 # The local bitswap handler needs to 'own' this stream to read the response
561 0 $bitswap->handle_stream($ss);
562 0 return $bitswap->request_block( $ss, $cid );
563 }
564 )->else(
565 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
566 0 my ($e) = @_;
567  
568 #~ say "[IPFS] Bitswap failed: $e. Falling back to HTTP...";
569 0 return $self->_get_block_http( $cid_str, $did );
570 }
571 0 );
572 }
573 }
574 0 return $self->_get_block_http( $cid_str, $did );
575 }
576  
577 0 ▶ method _get_block_http ( $cid_str, $did ) {
Subroutine _get_block_http: not called CC=2 SCAR=17.9
578  
579 # If no DID provided, we can't fallback to sync endpoints
580 0 ▶ return Future->done(undef) unless $did;
Branch: 1 branch
BranchTrueFalseTotal
unless $did 0 0 0
581  
582 #~ say "[HTTP] Fetching block $cid_str for $did via com.atproto.sync.getBlocks...";
583 # com.atproto.sync.getBlocks returns a CAR file
584 return Future->call(
585 0 ▶ sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
586 0 my $car_data = $self->get( 'com.atproto.sync.getBlocks' => { did => $did, cids => [$cid_str] } );
587 0 ▶ return undef unless $car_data;
Branch: 1 branch
BranchTrueFalseTotal
unless $car_data 0 0 0
588 0 require Archive::CAR;
589 0 require Archive::CAR::v1;
590 0 my $car = Archive::CAR::v1->new();
591 0 open my $fh, '<', \$car_data;
592 0 $car->read($fh);
593  
594 # Archive::CAR blocks is an arrayref of { cid => CIDobj, data => ... }
595 0 for my $block ( @{ $car->blocks } ) {
596  
597 # CIDs are complex objects, to_string should work for matching
598 # But the CID in the CAR might be different multibase than $cid_str
599 # For now, simplistic comparison or raw check
600 0 ▶ return $block->{data} if $block->{cid}->to_string eq $cid_str;
Branch: 1 branch
BranchTrueFalseTotal
if $block->{"cid"}->to_string eq $cid_str 0 0 0
601 }
602 0 return undef;
603 }
604 0 );
605 }
606 0 ▶ method get_repo_head ($did) { $self->get( 'com.atproto.sync.getHead' => { did => $did } ) }
Subroutine get_repo_head: not called CC=1 SCAR=6.9
Pod documented
607 0 ▶ sub _now { Time::Moment->now }
Subroutine _now: not called CC=1 SCAR=6.9
608 0 ▶ method _duration ($seconds) { $seconds || return '0 seconds'; $seconds = abs $seconds; return $seconds . ' seconds' }
Subroutine _duration: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$seconds or return "0 seconds" 0 0 0
609  
610 0 ▶ method ratelimit_ ( $headers, $type, $meta //= () ) {
Subroutine ratelimit_: not called CC=4 SCAR=30.0
Pod undocumented
611 0 my %h = map { lc($_) => $headers->{$_} } keys %$headers;
612 0 ▶ return unless exists $h{'ratelimit-limit'};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $h{"ratelimit-limit"} 0 0 0
613 my $rate = {
614 limit => $h{'ratelimit-limit'},
615 remaining => $h{'ratelimit-remaining'},
616 reset => $h{'ratelimit-reset'},
617 0 policy => $h{'ratelimit-policy'},
618 };
619 0 ▶ defined $meta ? $ratelimits{$type}{$meta} = $rate : $ratelimits{$type} = $rate;
Branch: 1 branch
BranchTrueFalseTotal
defined $meta ? : 0 0 0
620 }
621  
622 0 ▶ method _ratecheck( $type, $meta //= () ) {
Subroutine _ratecheck: not called CC=8 SCAR=42.8
623 0 ▶ my $rate = defined $meta ? $ratelimits{$type}{$meta} : $ratelimits{$type};
Branch: 1 branch
BranchTrueFalseTotal
defined $meta ? : 0 0 0
624 0 ▶ return unless $rate && $rate->{reset};
Branch: 1 branch
BranchTrueFalseTotal
unless $rate and $rate->{"reset"} 0 0 0
Condition0%0/3
$rate and $rate->{"reset"}
!ll&&!rl&&r
000
MC/DC0%0/2
$rate and $rate->{"reset"}
$rate $rate->{"reset"}
Truth table0%0/3
A and B
A= $rateB= $rate->{"reset"}
ABresult
0X0
100
111
625 0 ▶ if ( $rate->{remaining} <= 0 && time < $rate->{reset} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($rate->{'remaining'} <= 0 and time < $rate->{'reset'}) { } 0 0 0
elsif ($rate->{'remaining'} < $rate->{'limit'} * 0.1) { } 0 0 0
Condition0%0/3
$rate->{"remaining"} <= 0 and time < $rate->{"reset"}
!ll&&!rl&&r
000
MC/DC0%0/2
$rate->{"remaining"} <= 0 and time < $rate->{"reset"}
$rate->{"remaining"} <= 0 time < $rate->{"reset"}
Truth table0%0/3
A and B
A= $rate->{"remaining"} <= 0B= time < $rate->{"reset"}
ABresult
0X0
100
111
626 0 my $wait = $rate->{reset} - time;
627 0 warnings::warnif( At => "Rate limit exceeded for $type. Reset in $wait seconds." );
628 }
629 elsif ( $rate->{remaining} < ( $rate->{limit} * 0.1 ) ) {
630 0 warnings::warnif( At => "Approaching rate limit for $type ($rate->{remaining} remaining)." );
631 }
632 }
633 4 } 1;
634 __END__
« lib/At/Protocol/Session.pm ↑ Summary blib/lib/At/Protocol/Firehose.pm »