| 1 | package | ||||||||||||||||||||||||||||||
| 2 | Mojo::ATProto::OAuth::Identity; | ||||||||||||||||||||||||||||||
| 3 | 9 | ▶ | use Mojo::Base -base, -signatures; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||
| 5 | 9 | ▶ | use Mojo::UserAgent qw//; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 6 | 9 | ▶ | use Mojo::URL qw//; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 7 | 9 | ▶ | use Mojo::Promise qw//; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 8 | 9 | ▶ | use Mojo::IOLoop qw//; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 9 | 9 | ▶ | use Mojo::Log qw//; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 10 | 9 | ▶ | use Net::DNS::Resolver qw//; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 11 | 9 | ▶ | use feature 'try'; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||
| 13 | 9 | ▶ | use constant DEBUG => $ENV{MOJO_OAUTH_DEBUG} || 0; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=7.1
Condition50%1/2
$ENV{'MOJO_OAUTH_DEBUG'} || 0
MC/DC0%0/1
$ENV{'MOJO_OAUTH_DEBUG'} || 0
$ENV{'MOJO_OAUTH_DEBUG'}
Truth table50%1/2
A
A= $ENV{'MOJO_OAUTH_DEBUG'}
| |||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||
| 15 | 9 | our $VERSION = '1.03'; # VERSION | |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||
| 17 | 0 | ▶ | has 'plc_url' => sub { return 'https://plc.directory' }; | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||
| 18 | 1 | ▶ | has 'dns' => sub { Net::DNS::Resolver->new }; | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 19 | 0 | ▶ | has 'log' => sub { Mojo::Log->new }; | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||
| 20 | 0 | ▶ | has 'ua' => sub($self) { | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=8.7
| |||||||||||||||||||||||||||||||
| 21 | 0 | my $ua = Mojo::UserAgent->new(request_timeout => 10); | |||||||||||||||||||||||||||||
| 22 | 9 | ▶ | no strict; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 23 | 0 | ▶ | $ua->transactor->name('Mojo::ATProto::OAuth/' . $VERSION || 'dev'); | ||||||||||||||||||||||||||||
|
Condition0%0/2
'Mojo::ATProto::OAuth/' . $VERSION || 'dev'
MC/DC0%0/1
'Mojo::ATProto::OAuth/' . $VERSION || 'dev'
'Mojo::ATProto::OAuth/' . $VERSION
Truth table0%0/2
A
A= 'Mojo::ATProto::OAuth/' . $VERSION
| |||||||||||||||||||||||||||||||
| 24 | 9 | ▶ | use strict; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 25 | 0 | return $ua; | |||||||||||||||||||||||||||||
| 26 | 9 | }; | |||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||
| 28 | # --- Handle -> DID resolution --- | ||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||
| 30 | # Parses a Net::DNS::Packet answer for a "did=<did>" TXT record. Shared | ||||||||||||||||||||||||||||||
| 31 | # by the sync and async DNS lookups - the only difference between them | ||||||||||||||||||||||||||||||
| 32 | # is how the answer packet gets obtained, never how it's read. | ||||||||||||||||||||||||||||||
| 33 | 0 | ▶ | sub _parse_txt_answer($self, $answer) { | ||||||||||||||||||||||||||||
|
Subroutine
_parse_txt_answer: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||||
| 34 | 0 | ▶ | return undef unless $answer; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 35 | 0 | for my $rr ($answer->answer) { | |||||||||||||||||||||||||||||
| 36 | 0 | ▶ | next unless $rr->type eq 'TXT'; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 37 | 0 | my $txt = join('', $rr->txtdata); | |||||||||||||||||||||||||||||
| 38 | 0 | ▶ | return $1 if $txt =~ /^did=(did:[a-z]+:[a-zA-Z0-9._:%-]+)$/; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||||
| 40 | 0 | return undef; | |||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||
| 43 | # Validates a well-known response body as a bare DID string - shared by | ||||||||||||||||||||||||||||||
| 44 | # the sync and async HTTP well-known lookups. | ||||||||||||||||||||||||||||||
| 45 | 0 | ▶ | sub _valid_did_body($self, $body) { | ||||||||||||||||||||||||||||
|
Subroutine
_valid_did_body: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||||
| 46 | 0 | ▶ | return undef unless defined $body; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 47 | 0 | ▶ | return undef if length($body) > 2048; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 48 | 0 | $body =~ s/^\s+|\s+$//g; | |||||||||||||||||||||||||||||
| 49 | 0 | ▶ | return undef unless $body =~ /^did:[a-z]+:[a-zA-Z0-9._:%-]+$/; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 50 | 0 | return $body; | |||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||
| 53 | # DNS TXT lookup at _atproto.<handle>, looking for a "did=<did>" record. | ||||||||||||||||||||||||||||||
| 54 | # Returns undef (not an error) if nothing is found - a handle without a | ||||||||||||||||||||||||||||||
| 55 | # DNS TXT record is expected to fall back to HTTP well-known, not a | ||||||||||||||||||||||||||||||
| 56 | # failure in itself. | ||||||||||||||||||||||||||||||
| 57 | 0 | ▶ | sub resolve_handle_dns($self, $handle) { | ||||||||||||||||||||||||||||
|
Subroutine
resolve_handle_dns: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||
| 58 | 0 | my $answer = $self->dns->search("_atproto.$handle", 'TXT'); | |||||||||||||||||||||||||||||
| 59 | 0 | my $did = $self->_parse_txt_answer($answer); | |||||||||||||||||||||||||||||
| 60 | 0 | $self->log->debug("Identity: DNS TXT lookup for _atproto.$handle -> " . ($did // 'no record')) if DEBUG; | |||||||||||||||||||||||||||||
| 61 | 0 | return $did; | |||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||
| 64 | # Async variant of resolve_handle_dns - Net::DNS::Native doesn't handle | ||||||||||||||||||||||||||||||
| 65 | # TXT records, so we need to use Net::DNS::Resolver here instead and | ||||||||||||||||||||||||||||||
| 66 | # tie it into IOLoop | ||||||||||||||||||||||||||||||
| 67 | 1 | ▶ | sub resolve_handle_dns_p($self, $handle) { | ||||||||||||||||||||||||||||
|
Subroutine
resolve_handle_dns_p: called CC=2 SCAR=12.4
Pod
undocumented
| |||||||||||||||||||||||||||||||
| 68 | 1 | my $promise = Mojo::Promise->new; | |||||||||||||||||||||||||||||
| 69 | 1 | my $sock = $self->dns->bgsend("_atproto.$handle", 'TXT'); | |||||||||||||||||||||||||||||
| 70 | 1 | ▶ | return Mojo::Promise->resolve(undef) unless $sock; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||
| 72 | 0 | my $reactor = Mojo::IOLoop->singleton->reactor; | |||||||||||||||||||||||||||||
| 73 | $reactor->io( | ||||||||||||||||||||||||||||||
| 74 | 0 | ▶ | $sock => sub { | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||
| 75 | 0 | $reactor->remove($sock); | |||||||||||||||||||||||||||||
| 76 | 0 | my $answer = $self->dns->bgread($sock); | |||||||||||||||||||||||||||||
| 77 | 0 | close($sock); | |||||||||||||||||||||||||||||
| 78 | 0 | my $did = $self->_parse_txt_answer($answer); | |||||||||||||||||||||||||||||
| 79 | 0 | $self->log->debug("Identity: DNS TXT lookup (async) for _atproto.$handle -> " . ($did // 'no record')) if DEBUG; | |||||||||||||||||||||||||||||
| 80 | 0 | $promise->resolve($did); | |||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||||
| 82 | 0 | )->watch($sock, 1, 0); | |||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||
| 84 | 0 | return $promise; | |||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||
| 87 | # HTTP well-known fallback: https://<handle>/.well-known/atproto-did, | ||||||||||||||||||||||||||||||
| 88 | # expected body is the bare DID string, nothing else. 2048-byte cap | ||||||||||||||||||||||||||||||
| 89 | # matches indigo's own limit - this is untrusted response data. | ||||||||||||||||||||||||||||||
| 90 | 0 | ▶ | sub resolve_handle_well_known($self, $handle) { | ||||||||||||||||||||||||||||
|
Subroutine
resolve_handle_well_known: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||
| 91 | 0 | $self->log->debug("Identity: HTTP well-known lookup for $handle") if DEBUG; | |||||||||||||||||||||||||||||
| 92 | 0 | my $tx = $self->ua->get(sprintf('https://%s/.well-known/atproto-did', $handle)); | |||||||||||||||||||||||||||||
| 93 | 0 | my $res = $tx->result; | |||||||||||||||||||||||||||||
| 94 | 0 | $self->log->debug('Identity: well-known response status=' . ($res->code // 'connection error')) if DEBUG; | |||||||||||||||||||||||||||||
| 95 | 0 | ▶ | return undef unless $res->is_success; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 96 | 0 | return $self->_valid_did_body($res->body); | |||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||
| 99 | 0 | ▶ | sub resolve_handle_well_known_p($self, $handle) { | ||||||||||||||||||||||||||||
|
Subroutine
resolve_handle_well_known_p: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||
| 100 | 0 | $self->log->debug("Identity: HTTP well-known lookup (async) for $handle") if DEBUG; | |||||||||||||||||||||||||||||
| 101 | return $self->ua->get_p(sprintf('https://%s/.well-known/atproto-did', $handle)) | ||||||||||||||||||||||||||||||
| 102 | 0 | ▶ | ->then(sub ($tx) { | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||
| 103 | 0 | my $res = $tx->result; | |||||||||||||||||||||||||||||
| 104 | 0 | $self->log->debug('Identity: well-known response status=' . ($res->code // 'connection error')) if DEBUG; | |||||||||||||||||||||||||||||
| 105 | 0 | ▶ | return undef unless $res->is_success; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 106 | 0 | return $self->_valid_did_body($res->body); | |||||||||||||||||||||||||||||
| 107 | 0 | }); | |||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||
| 110 | 0 | ▶ | sub resolve_handle($self, $handle) { | ||||||||||||||||||||||||||||
|
Subroutine
resolve_handle: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||||
| 111 | 0 | $handle = lc($handle); | |||||||||||||||||||||||||||||
| 112 | 0 | $self->log->debug("Identity: resolving handle '$handle' to a DID") if DEBUG; | |||||||||||||||||||||||||||||
| 113 | 0 | try { | |||||||||||||||||||||||||||||
| 114 | 0 | ▶ | if(my $did = $self->resolve_handle_dns($handle)) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 115 | 0 | $self->log->debug("Identity: '$handle' resolved via DNS -> $did") if DEBUG; | |||||||||||||||||||||||||||||
| 116 | 0 | return $did; | |||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||
| 118 | 0 | ▶ | if(my $did = $self->resolve_handle_well_known($handle)) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 119 | 0 | $self->log->debug("Identity: '$handle' resolved via well-known -> $did") if DEBUG; | |||||||||||||||||||||||||||||
| 120 | 0 | return $did; | |||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||||
| 122 | 0 | die "could not resolve handle '$handle' to a DID (DNS and HTTP well-known both failed)\n" | |||||||||||||||||||||||||||||
| 123 | } catch($ex) { | ||||||||||||||||||||||||||||||
| 124 | 0 | die "could not resolve handle '$handle' to a DID (exception: $ex)\n"; | |||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||
| 128 | 0 | ▶ | sub resolve_handle_p ($self, $handle) { | ||||||||||||||||||||||||||||
|
Subroutine
resolve_handle_p: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||
| 129 | 0 | $handle = lc($handle); | |||||||||||||||||||||||||||||
| 130 | 0 | $self->log->debug("Identity: resolving handle '$handle' to a DID (async)") if DEBUG; | |||||||||||||||||||||||||||||
| 131 | 0 | ▶ | return $self->resolve_handle_dns_p($handle)->then(sub($did) { | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||
| 132 | 0 | ▶ | if(defined $did) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 133 | 0 | $self->log->debug("Identity: '$handle' resolved via DNS -> $did") if DEBUG; | |||||||||||||||||||||||||||||
| 134 | 0 | return $did; | |||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||||
| 136 | 0 | ▶ | return $self->resolve_handle_well_known_p($handle)->then(sub($did) { | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||
| 137 | 0 | ▶ | die "could not resolve handle '$handle' to a DID (DNS and HTTP well-known both failed)\n" | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 138 | unless defined $did; | ||||||||||||||||||||||||||||||
| 139 | 0 | $self->log->debug("Identity: '$handle' resolved via well-known -> $did") if DEBUG; | |||||||||||||||||||||||||||||
| 140 | 0 | return $did; | |||||||||||||||||||||||||||||
| 141 | 0 | }); | |||||||||||||||||||||||||||||
| 142 | 0 | }); | |||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||
| 145 | # --- DID -> DID document resolution --- | ||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||
| 147 | 0 | ▶ | sub resolve_did($self, $did) { | ||||||||||||||||||||||||||||
|
Subroutine
resolve_did: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||||
| 148 | 0 | ▶ | return $self->_resolve_did_plc($did) if $did =~ /^did:plc:/; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 149 | 0 | ▶ | return $self->_resolve_did_web($did) if $did =~ /^did:web:/; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 150 | 0 | die "unsupported DID method: $did\n"; | |||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||
| 153 | 1 | ▶ | sub resolve_did_p($self, $did) { | ||||||||||||||||||||||||||||
|
Subroutine
resolve_did_p: called CC=3 SCAR=11.7
Pod
undocumented
| |||||||||||||||||||||||||||||||
| 154 | 1 | ▶ | return $self->_resolve_did_plc_p($did) if $did =~ /^did:plc:/; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 155 | 1 | ▶ | return $self->_resolve_did_web_p($did) if $did =~ /^did:web:/; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 156 | 1 | return Mojo::Promise->reject("unsupported DID method: $did\n"); | |||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||
| 159 | 0 | ▶ | sub _resolve_did_plc($self, $did) { | ||||||||||||||||||||||||||||
|
Subroutine
_resolve_did_plc: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||
| 160 | 0 | $self->log->debug("Identity: PLC directory lookup for $did") if DEBUG; | |||||||||||||||||||||||||||||
| 161 | 0 | my $tx = $self->ua->get(sprintf('%s/%s', $self->plc_url, $did)); | |||||||||||||||||||||||||||||
| 162 | 0 | my $res = $tx->result; | |||||||||||||||||||||||||||||
| 163 | 0 | $self->log->debug('Identity: PLC directory response status=' . ($res->code // 'connection error')) if DEBUG; | |||||||||||||||||||||||||||||
| 164 | 0 | ▶ | die "PLC directory lookup failed for $did: " . ($res->message // 'connection error') . "\n" | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$res->message // "connection error"
MC/DC0%0/1
$res->message // "connection error"
$res->message
Truth table0%0/2
A
A= $res->message
| |||||||||||||||||||||||||||||||
| 165 | unless $res->is_success; | ||||||||||||||||||||||||||||||
| 166 | 0 | return $res->json; | |||||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||
| 169 | 0 | ▶ | sub _resolve_did_plc_p($self, $did) { | ||||||||||||||||||||||||||||
|
Subroutine
_resolve_did_plc_p: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||
| 170 | 0 | $self->log->debug("Identity: PLC directory lookup (async) for $did") if DEBUG; | |||||||||||||||||||||||||||||
| 171 | 0 | ▶ | return $self->ua->get_p(sprintf('%s/%s', $self->plc_url, $did))->then(sub($tx) { | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||
| 172 | 0 | my $res = $tx->result; | |||||||||||||||||||||||||||||
| 173 | 0 | $self->log->debug('Identity: PLC directory response status=' . ($res->code // 'connection error')) if DEBUG; | |||||||||||||||||||||||||||||
| 174 | 0 | ▶ | die "PLC directory lookup failed for $did: " . ($res->message // 'connection error') . "\n" | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$res->message // "connection error"
MC/DC0%0/1
$res->message // "connection error"
$res->message
Truth table0%0/2
A
A= $res->message
| |||||||||||||||||||||||||||||||
| 175 | unless $res->is_success; | ||||||||||||||||||||||||||||||
| 176 | 0 | return $res->json; | |||||||||||||||||||||||||||||
| 177 | 0 | }); | |||||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||
| 180 | 0 | ▶ | sub _resolve_did_web($self, $did) { | ||||||||||||||||||||||||||||
|
Subroutine
_resolve_did_web: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||
| 181 | 0 | my $hostname = $self->_did_web_hostname($did); | |||||||||||||||||||||||||||||
| 182 | 0 | $self->log->debug("Identity: did:web lookup for $did (host=$hostname)") if DEBUG; | |||||||||||||||||||||||||||||
| 183 | 0 | my $tx = $self->ua->get(sprintf('https://%s/.well-known/did.json', $hostname)); | |||||||||||||||||||||||||||||
| 184 | 0 | my $res = $tx->result; | |||||||||||||||||||||||||||||
| 185 | 0 | $self->log->debug('Identity: did:web response status=' . ($res->code // 'connection error')) if DEBUG; | |||||||||||||||||||||||||||||
| 186 | 0 | ▶ | die "did:web lookup failed for $did: " . ($res->message // 'connection error') . "\n" | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$res->message // "connection error"
MC/DC0%0/1
$res->message // "connection error"
$res->message
Truth table0%0/2
A
A= $res->message
| |||||||||||||||||||||||||||||||
| 187 | unless $res->is_success; | ||||||||||||||||||||||||||||||
| 188 | 0 | return $res->json; | |||||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||
| 191 | 0 | ▶ | sub _resolve_did_web_p($self, $did) { | ||||||||||||||||||||||||||||
|
Subroutine
_resolve_did_web_p: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||
| 192 | 0 | my $hostname = $self->_did_web_hostname($did); | |||||||||||||||||||||||||||||
| 193 | 0 | $self->log->debug("Identity: did:web lookup (async) for $did (host=$hostname)") if DEBUG; | |||||||||||||||||||||||||||||
| 194 | return $self->ua->get_p(sprintf('https://%s/.well-known/did.json', $hostname)) | ||||||||||||||||||||||||||||||
| 195 | 0 | ▶ | ->then(sub ($tx) { | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||
| 196 | 0 | my $res = $tx->result; | |||||||||||||||||||||||||||||
| 197 | 0 | $self->log->debug('Identity: did:web response status=' . ($res->code // 'connection error')) if DEBUG; | |||||||||||||||||||||||||||||
| 198 | 0 | ▶ | die "did:web lookup failed for $did: " . ($res->message // 'connection error') . "\n" | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$res->message // "connection error"
MC/DC0%0/1
$res->message // "connection error"
$res->message
Truth table0%0/2
A
A= $res->message
| |||||||||||||||||||||||||||||||
| 199 | unless $res->is_success; | ||||||||||||||||||||||||||||||
| 200 | 0 | return $res->json; | |||||||||||||||||||||||||||||
| 201 | 0 | }); | |||||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||
| 204 | 0 | ▶ | sub _did_web_hostname($self, $did) { | ||||||||||||||||||||||||||||
|
Subroutine
_did_web_hostname: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||
| 205 | 0 | my ($hostname) = $did =~ /^did:web:(.+)$/; | |||||||||||||||||||||||||||||
| 206 | 0 | ▶ | die "malformed did:web: $did\n" unless defined $hostname; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 207 | 0 | ▶ | die "did:web identifier not a simple hostname: $hostname\n" if $hostname =~ /:/; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 208 | 0 | return $hostname; | |||||||||||||||||||||||||||||
| 209 | } | ||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||
| 211 | # --- DID document -> identity hashref --- | ||||||||||||||||||||||||||||||
| 212 | # Identity shape: | ||||||||||||||||||||||||||||||
| 213 | # { did, handle, also_known_as => [...], | ||||||||||||||||||||||||||||||
| 214 | # services => { <fragment> => {type, url} }, | ||||||||||||||||||||||||||||||
| 215 | # keys => { <fragment> => {type, public_key_multibase} } } | ||||||||||||||||||||||||||||||
| 216 | # `handle` is always undef here - only a caller that has bi-directionally | ||||||||||||||||||||||||||||||
| 217 | # verified it (see lookup(), below) should ever set it. | ||||||||||||||||||||||||||||||
| 218 | 6 | ▶ | sub _parse_identity($self, $doc) { | ||||||||||||||||||||||||||||
|
Subroutine
_parse_identity: called CC=15 SCAR=28.2
| |||||||||||||||||||||||||||||||
| 219 | 6 | my %services; | |||||||||||||||||||||||||||||
| 220 | 6 | ▶ | for my $svc (@{$doc->{service} // []}) { | ||||||||||||||||||||||||||||
|
Condition100%2/2
$doc->{"service"} // []
MC/DC100%1/1
$doc->{"service"} // []
$doc->{"service"}
Truth table100%2/2
A
A= $doc->{"service"}
| |||||||||||||||||||||||||||||||
| 221 | 5 | ▶ | my ($frag) = ($svc->{id} // '') =~ /#(.+)$/; | ||||||||||||||||||||||||||||
|
Condition50%1/2
$svc->{"id"} // ""
MC/DC0%0/1
$svc->{"id"} // ""
$svc->{"id"}
Truth table50%1/2
A
A= $svc->{"id"}
| |||||||||||||||||||||||||||||||
| 222 | 5 | ▶ | next unless defined $frag; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 223 | 5 | ▶ | next if exists $services{$frag}; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 224 | 4 | $services{$frag} = {type => $svc->{type}, url => $svc->{serviceEndpoint}}; | |||||||||||||||||||||||||||||
| 225 | } | ||||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||||
| 227 | 6 | my %keys; | |||||||||||||||||||||||||||||
| 228 | 6 | ▶ | for my $vm (@{$doc->{verificationMethod} // []}) { | ||||||||||||||||||||||||||||
|
Condition100%2/2
$doc->{"verificationMethod"} // []
MC/DC100%1/1
$doc->{"verificationMethod"} // []
$doc->{"verificationMethod"}
Truth table100%2/2
A
A= $doc->{"verificationMethod"}
| |||||||||||||||||||||||||||||||
| 229 | 3 | ▶ | my ($frag) = ($vm->{id} // '') =~ /#(.+)$/; | ||||||||||||||||||||||||||||
|
Condition50%1/2
$vm->{"id"} // ""
MC/DC0%0/1
$vm->{"id"} // ""
$vm->{"id"}
Truth table50%1/2
A
A= $vm->{"id"}
| |||||||||||||||||||||||||||||||
| 230 | 3 | ▶ | next unless defined $frag; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 231 | 3 | ▶ | next if exists $keys{$frag}; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 232 | 3 | ▶ | next unless ($vm->{controller} // '') eq ($doc->{id} // ''); | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%2/4
$vm->{"controller"} // ""
$doc->{"id"} // ""
MC/DC0%0/1
$vm->{"controller"} // ""
$vm->{"controller"}
MC/DC0%0/1
$doc->{"id"} // ""
$doc->{"id"}
Truth table50%1/2
A
A= $vm->{"controller"}
Truth table50%1/2
A
A= $doc->{"id"}
| |||||||||||||||||||||||||||||||
| 233 | 2 | $keys{$frag} = {type => $vm->{type}, public_key_multibase => $vm->{publicKeyMultibase}}; | |||||||||||||||||||||||||||||
| 234 | } | ||||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||||
| 236 | return { | ||||||||||||||||||||||||||||||
| 237 | did => $doc->{id}, | ||||||||||||||||||||||||||||||
| 238 | handle => undef, | ||||||||||||||||||||||||||||||
| 239 | 6 | ▶ | also_known_as => $doc->{alsoKnownAs} // [], | ||||||||||||||||||||||||||||
|
Condition50%1/2
$doc->{"alsoKnownAs"} // []
MC/DC0%0/1
$doc->{"alsoKnownAs"} // []
$doc->{"alsoKnownAs"}
Truth table50%1/2
A
A= $doc->{"alsoKnownAs"}
| |||||||||||||||||||||||||||||||
| 240 | services => \%services, | ||||||||||||||||||||||||||||||
| 241 | keys => \%keys, | ||||||||||||||||||||||||||||||
| 242 | }; | ||||||||||||||||||||||||||||||
| 243 | } | ||||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||||
| 245 | # Extracts an at:// handle URI from also_known_as, if present. Not | ||||||||||||||||||||||||||||||
| 246 | # trusted on its own - see lookup()'s bidirectional check. | ||||||||||||||||||||||||||||||
| 247 | 4 | ▶ | sub _declared_handle($self, $identity) { | ||||||||||||||||||||||||||||
|
Subroutine
_declared_handle: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||
| 248 | 4 | for my $aka (@{$identity->{also_known_as}}) { | |||||||||||||||||||||||||||||
| 249 | 4 | ▶ | return lc($1) if $aka =~ m{^at://(.+)$} && length($1); | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$aka =~ m[^at://(.+)$]u and length $1
MC/DC50%1/2
$aka =~ m[^at://(.+)$]u and length $1
$aka =~ m[^at://(.+)$]u
length $1
Truth table66%2/3
A and B
A= $aka =~ m[^at://(.+)$]uB= length $1
| |||||||||||||||||||||||||||||||
| 250 | } | ||||||||||||||||||||||||||||||
| 251 | 1 | return undef; | |||||||||||||||||||||||||||||
| 252 | } | ||||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||||
| 254 | # The PDS service endpoint URL for a resolved identity hashref | ||||||||||||||||||||||||||||||
| 255 | 12 | ▶ | sub pds_endpoint($self, $identity) { | ||||||||||||||||||||||||||||
|
Subroutine
pds_endpoint: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||
| 256 | 12 | return $identity->{services}->{atproto_pds}->{url}; | |||||||||||||||||||||||||||||
| 257 | } | ||||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||||
| 259 | # --- The main entrypoint: bidirectionally-verified lookup --- | ||||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||||
| 261 | # Accepts a handle or a DID, returns a fully verified identity hashref. | ||||||||||||||||||||||||||||||
| 262 | 0 | ▶ | sub lookup($self, $identifier) { | ||||||||||||||||||||||||||||
|
Subroutine
lookup: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||
| 263 | 0 | ▶ | return $self->_lookup_did($identifier) if $identifier =~ /^did:/; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 264 | 0 | return $self->_lookup_handle($identifier); | |||||||||||||||||||||||||||||
| 265 | } | ||||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||||
| 267 | # Handle -> DID -> DID doc -> the doc must declare this same handle | ||||||||||||||||||||||||||||||
| 268 | # back via alsoKnownAs, or the identity is untrustworthy (someone | ||||||||||||||||||||||||||||||
| 269 | # else's DID document could otherwise be presented for any handle). | ||||||||||||||||||||||||||||||
| 270 | 0 | ▶ | sub _lookup_handle($self, $handle) { | ||||||||||||||||||||||||||||
|
Subroutine
_lookup_handle: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||
| 271 | 0 | $handle = lc($handle); | |||||||||||||||||||||||||||||
| 272 | 0 | my $did = $self->resolve_handle($handle); | |||||||||||||||||||||||||||||
| 273 | 0 | my $doc = $self->resolve_did($did); | |||||||||||||||||||||||||||||
| 274 | 0 | my $identity = $self->_parse_identity($doc); | |||||||||||||||||||||||||||||
| 275 | 0 | my $declared = $self->_declared_handle($identity); | |||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||||
| 277 | 0 | ▶ | die "could not verify handle/DID match: DID document for $did declares no handle\n" | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 278 | unless defined $declared; | ||||||||||||||||||||||||||||||
| 279 | 0 | ▶ | die "handle mismatch: DID document for $did declares '$declared', requested '$handle'\n" | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 280 | unless $declared eq $handle; | ||||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||||
| 282 | 0 | $identity->{handle} = $declared; | |||||||||||||||||||||||||||||
| 283 | 0 | $self->log->debug("Identity: lookup('$handle') verified - did=$did") if DEBUG; | |||||||||||||||||||||||||||||
| 284 | 0 | return $identity; | |||||||||||||||||||||||||||||
| 285 | } | ||||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||||
| 287 | # DID -> DID doc -> if a handle is declared, resolve *that* handle | ||||||||||||||||||||||||||||||
| 288 | # forward and confirm it points back to the same DID. A DID with no | ||||||||||||||||||||||||||||||
| 289 | # declared handle, or one that fails to verify, still resolves - it | ||||||||||||||||||||||||||||||
| 290 | # just carries no verified handle (identity->{handle} stays undef), | ||||||||||||||||||||||||||||||
| 291 | # same as indigo's handle.invalid convention. | ||||||||||||||||||||||||||||||
| 292 | 0 | ▶ | sub _lookup_did($self, $did) { | ||||||||||||||||||||||||||||
|
Subroutine
_lookup_did: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||||
| 293 | 0 | my $doc = $self->resolve_did($did); | |||||||||||||||||||||||||||||
| 294 | 0 | my $identity = $self->_parse_identity($doc); | |||||||||||||||||||||||||||||
| 295 | 0 | my $declared = $self->_declared_handle($identity); | |||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||||
| 297 | 0 | ▶ | if (defined $declared) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 298 | 0 | my $resolved_did; | |||||||||||||||||||||||||||||
| 299 | 0 | try { | |||||||||||||||||||||||||||||
| 300 | 0 | $resolved_did = $self->resolve_handle($declared); | |||||||||||||||||||||||||||||
| 301 | } catch($ex) { | ||||||||||||||||||||||||||||||
| 302 | 0 | $self->log->debug('Identity: declared handle resolution exception: ', $ex) if DEBUG; | |||||||||||||||||||||||||||||
| 303 | 0 | $resolved_did = undef; | |||||||||||||||||||||||||||||
| 304 | } | ||||||||||||||||||||||||||||||
| 305 | 0 | ▶ | $identity->{handle} = (defined($resolved_did) && $resolved_did eq $did) ? $declared : undef; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $resolved_did and $resolved_did eq $did
MC/DC0%0/2
defined $resolved_did and $resolved_did eq $did
defined $resolved_did
$resolved_did eq $did
Truth table0%0/3
A and B
A= defined $resolved_didB= $resolved_did eq $did
| |||||||||||||||||||||||||||||||
| 306 | $self->log->debug("Identity: lookup('$did') declared handle '$declared' - " | ||||||||||||||||||||||||||||||
| 307 | 0 | . (defined($identity->{handle}) ? 'verified' : 'failed to verify, discarded')) if DEBUG; | |||||||||||||||||||||||||||||
| 308 | } else { | ||||||||||||||||||||||||||||||
| 309 | 0 | $self->log->debug("Identity: lookup('$did') - no declared handle") if DEBUG; | |||||||||||||||||||||||||||||
| 310 | } | ||||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||||
| 312 | 0 | return $identity; | |||||||||||||||||||||||||||||
| 313 | } | ||||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||||
| 315 | # Async variant of lookup() - reuses the same pure, non-IO parsing/ | ||||||||||||||||||||||||||||||
| 316 | # verification helpers (_parse_identity, _declared_handle) as the sync | ||||||||||||||||||||||||||||||
| 317 | # path; only the transport (resolve_handle_p/resolve_did_p) differs. | ||||||||||||||||||||||||||||||
| 318 | 1 | ▶ | sub lookup_p($self, $identifier) { | ||||||||||||||||||||||||||||
|
Subroutine
lookup_p: called CC=2 SCAR=9.2
| |||||||||||||||||||||||||||||||
| 319 | 1 | ▶ | return $self->_lookup_did_p($identifier) if $identifier =~ /^did:/; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 320 | 0 | return $self->_lookup_handle_p($identifier); | |||||||||||||||||||||||||||||
| 321 | } | ||||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||||
| 323 | 0 | ▶ | sub _lookup_handle_p($self, $handle) { | ||||||||||||||||||||||||||||
|
Subroutine
_lookup_handle_p: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||
| 324 | 0 | $handle = lc($handle); | |||||||||||||||||||||||||||||
| 325 | 0 | ▶ | return $self->resolve_handle_p($handle)->then(sub ($did) { | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||
| 326 | 0 | ▶ | return $self->resolve_did_p($did)->then(sub ($doc) { | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||
| 327 | 0 | my $identity = $self->_parse_identity($doc); | |||||||||||||||||||||||||||||
| 328 | 0 | my $declared = $self->_declared_handle($identity); | |||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||||
| 330 | 0 | ▶ | die "could not verify handle/DID match: DID document for $did declares no handle\n" | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 331 | unless defined $declared; | ||||||||||||||||||||||||||||||
| 332 | 0 | ▶ | die "handle mismatch: DID document for $did declares '$declared', requested '$handle'\n" | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 333 | unless $declared eq $handle; | ||||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||||
| 335 | 0 | $identity->{handle} = $declared; | |||||||||||||||||||||||||||||
| 336 | 0 | $self->log->debug("Identity: lookup_p('$handle') verified - did=$did") if DEBUG; | |||||||||||||||||||||||||||||
| 337 | 0 | return $identity; | |||||||||||||||||||||||||||||
| 338 | 0 | }); | |||||||||||||||||||||||||||||
| 339 | 0 | }); | |||||||||||||||||||||||||||||
| 340 | } | ||||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||||
| 342 | 1 | ▶ | sub _lookup_did_p($self, $did) { | ||||||||||||||||||||||||||||
|
Subroutine
_lookup_did_p: called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||
| 343 | 1 | ▶ | return $self->resolve_did_p($did)->then(sub ($doc) { | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=9.6
| |||||||||||||||||||||||||||||||
| 344 | 1 | my $identity = $self->_parse_identity($doc); | |||||||||||||||||||||||||||||
| 345 | 1 | my $declared = $self->_declared_handle($identity); | |||||||||||||||||||||||||||||
| 346 | 1 | ▶ | unless (defined $declared) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 347 | 0 | $self->log->debug("Identity: lookup_p('$did') - no declared handle") if DEBUG; | |||||||||||||||||||||||||||||
| 348 | 0 | return $identity; | |||||||||||||||||||||||||||||
| 349 | } | ||||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||||
| 351 | return $self->resolve_handle_p($declared)->then( | ||||||||||||||||||||||||||||||
| 352 | 0 | ▶ | sub ($resolved_did) { | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||
| 353 | 0 | ▶ | $identity->{handle} = ($resolved_did eq $did) ? $declared : undef; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 354 | $self->log->debug("Identity: lookup_p('$did') declared handle '$declared' - " | ||||||||||||||||||||||||||||||
| 355 | 0 | . (defined($identity->{handle}) ? 'verified' : 'failed to verify, discarded')) if DEBUG; | |||||||||||||||||||||||||||||
| 356 | 0 | return $identity; | |||||||||||||||||||||||||||||
| 357 | }, | ||||||||||||||||||||||||||||||
| 358 | 1 | ▶ | sub ($err) { | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 359 | # resolve_handle_p rejects (rather than resolving undef) | ||||||||||||||||||||||||||||||
| 360 | # when both DNS and well-known fail - that's still just | ||||||||||||||||||||||||||||||
| 361 | # "couldn't verify," not a reason to fail the whole | ||||||||||||||||||||||||||||||
| 362 | # lookup, same as the sync path's eval-and-ignore. | ||||||||||||||||||||||||||||||
| 363 | 1 | $identity->{handle} = undef; | |||||||||||||||||||||||||||||
| 364 | 1 | return $identity; | |||||||||||||||||||||||||||||
| 365 | } | ||||||||||||||||||||||||||||||
| 366 | 1 | ); | |||||||||||||||||||||||||||||
| 367 | 1 | }); | |||||||||||||||||||||||||||||
| 368 | } | ||||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||||
| 370 | 9 | 1; | |||||||||||||||||||||||||||||