| 1 | package Net::Blossom::Server::AuthorizationResult; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use strictures 2; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use Net::Blossom::_ConstructorArgs (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use Net::Blossom::Server::Error; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use Class::Tiny qw(pubkey action _hashes); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | 2 | my $HEX64 = qr/\A[0-9a-f]{64}\z/; | |||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | 10 | ▶ | sub BUILDARGS { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BUILDARGS: called CC=8 SCAR=21.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | 10 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||
| 15 | 10 | my %args = Net::Blossom::_ConstructorArgs::normalize(@_); | |||||||||||||||||||||||||||||||||||||||||
| 16 | 10 | my %known = map { $_ => 1 } qw(pubkey action hashes); | |||||||||||||||||||||||||||||||||||||||||
| 17 | 10 | my @unknown = grep { !exists $known{$_} } keys %args; | |||||||||||||||||||||||||||||||||||||||||
| 18 | 10 | ▶ | croak "unknown argument(s): " . join(', ', sort @unknown) if @unknown; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | 10 | ▶ | $args{hashes} = [] unless defined $args{hashes}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 21 | 10 | ▶ | croak "hashes must be an array reference" unless ref($args{hashes}) eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 22 | 9 | for my $hash (@{$args{hashes}}) { | |||||||||||||||||||||||||||||||||||||||||
| 23 | 4 | ▶ | croak "hashes must contain 64-char lowercase hex values" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%3/6
defined $hash and not ref $hash and $hash =~ /$HEX64/
defined $hash and not ref $hash
MC/DC33%1/3
defined $hash and not ref $hash and $hash =~ /$HEX64/
defined $hash
not ref $hash
$hash =~ /$HEX64/
Truth table50%2/4
A and not(B) and C
A= defined $hashB= not ref $hashC= $hash =~ /$HEX64/
| |||||||||||||||||||||||||||||||||||||||||||
| 24 | unless defined $hash && !ref($hash) && $hash =~ $HEX64; | ||||||||||||||||||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||||||||||||||||
| 26 | 8 | $args{_hashes} = [@{$args{hashes}}]; | |||||||||||||||||||||||||||||||||||||||||
| 27 | 8 | delete $args{hashes}; | |||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||
| 29 | 8 | return \%args; | |||||||||||||||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||
| 32 | 8 | ▶ | sub BUILD { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 33 | 8 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 34 | 8 | ▶ | croak "pubkey is required" unless defined $self->pubkey; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 35 | 7 | ▶ | croak "pubkey must be a scalar" if ref($self->pubkey); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 36 | 7 | ▶ | croak "pubkey must be 64-char lowercase hex" unless $self->pubkey =~ $HEX64; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 37 | 6 | ▶ | croak "action is required" unless defined $self->action; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 38 | 5 | ▶ | croak "action must be a scalar" if ref($self->action); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 39 | 4 | ▶ | croak "action is required" unless length $self->action; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 40 | 4 | return; | |||||||||||||||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||
| 43 | 4 | ▶ | sub hashes { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
hashes: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 44 | 4 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 45 | 4 | return [@{$self->_hashes}]; | |||||||||||||||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||
| 48 | 6 | ▶ | sub require_hash { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
require_hash: called CC=12 SCAR=25.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 49 | 6 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 50 | 6 | my ($sha256, %opts) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 51 | 6 | my %known = map { $_ => 1 } qw(status reason); | |||||||||||||||||||||||||||||||||||||||||
| 52 | 6 | my @unknown = grep { !exists $known{$_} } keys %opts; | |||||||||||||||||||||||||||||||||||||||||
| 53 | 6 | ▶ | croak "unknown option(s): " . join(', ', sort @unknown) if @unknown; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||
| 55 | 6 | ▶ | croak "sha256 must be 64-char lowercase hex" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%3/6
defined $sha256 and not ref $sha256 and $sha256 =~ /$HEX64/
defined $sha256 and not ref $sha256
MC/DC33%1/3
defined $sha256 and not ref $sha256 and $sha256 =~ /$HEX64/
defined $sha256
not ref $sha256
$sha256 =~ /$HEX64/
Truth table50%2/4
A and not(B) and C
A= defined $sha256B= not ref $sha256C= $sha256 =~ /$HEX64/
| |||||||||||||||||||||||||||||||||||||||||||
| 56 | unless defined $sha256 && !ref($sha256) && $sha256 =~ $HEX64; | ||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||
| 58 | 5 | ▶ | $opts{status} = 401 unless defined $opts{status}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 59 | croak "status must be an HTTP status code" | ||||||||||||||||||||||||||||||||||||||||||
| 60 | 5 | ▶ | unless !ref($opts{status}) && $opts{status} =~ /\A[1-5][0-9][0-9]\z/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not ref $opts{"status"} and $opts{"status"} =~ /\A[1-5][0-9][0-9]\z/
MC/DC50%1/2
not ref $opts{"status"} and $opts{"status"} =~ /\A[1-5][0-9][0-9]\z/
not ref $opts{"status"}
$opts{"status"} =~ /\A[1-5][0-9][0-9]\z/
Truth table66%2/3
not(A) and B
A= not ref $opts{"status"}B= $opts{"status"} =~ /\A[1-5][0-9][0-9]\z/
| |||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||
| 62 | $opts{reason} = 'authorization x tag does not match request hash' | ||||||||||||||||||||||||||||||||||||||||||
| 63 | 4 | ▶ | unless defined $opts{reason}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 64 | 4 | ▶ | croak "reason must be a scalar" if ref($opts{reason}); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||
| 66 | 3 | ▶ | return 1 if grep { $_ eq $sha256 } @{$self->_hashes}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||
| 68 | 2 | my %headers; | |||||||||||||||||||||||||||||||||||||||||
| 69 | 2 | ▶ | $headers{'WWW-Authenticate'} = 'Nostr' if $opts{status} == 401; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 70 | Net::Blossom::Server::Error->throw( | ||||||||||||||||||||||||||||||||||||||||||
| 71 | status => $opts{status}, | ||||||||||||||||||||||||||||||||||||||||||
| 72 | reason => $opts{reason}, | ||||||||||||||||||||||||||||||||||||||||||
| 73 | 2 | headers => \%headers, | |||||||||||||||||||||||||||||||||||||||||
| 74 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||
| 77 | 2 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||
| 79 | =pod | ||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||
| 81 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||
| 83 | Net::Blossom::Server::AuthorizationResult - Verified Blossom authorization data | ||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||
| 85 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||
| 87 | my $result = $auth->authorize($request); | ||||||||||||||||||||||||||||||||||||||||||
| 88 | my $pubkey = $result->pubkey; | ||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||
| 90 | $result->require_hash($sha256, status => 409); | ||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||
| 92 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||
| 94 | C<Net::Blossom::Server::AuthorizationResult> is returned by | ||||||||||||||||||||||||||||||||||||||||||
| 95 | L<Net::Blossom::Server::Authorization> after a BUD-11 token has been parsed, | ||||||||||||||||||||||||||||||||||||||||||
| 96 | signature-checked, and matched to the request action. | ||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||
| 98 | The object carries the verified event C<pubkey>, the matched action, and any | ||||||||||||||||||||||||||||||||||||||||||
| 99 | validated C<x> tag hashes. Mirror handling uses those hashes after the server | ||||||||||||||||||||||||||||||||||||||||||
| 100 | downloads and hashes the remote blob. | ||||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||
| 102 | =head1 CONSTRUCTOR | ||||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||
| 104 | =head2 new | ||||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||
| 106 | my $result = Net::Blossom::Server::AuthorizationResult->new(%args); | ||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||
| 108 | Required arguments are C<pubkey> and C<action>. Optional C<hashes> is an array | ||||||||||||||||||||||||||||||||||||||||||
| 109 | reference of lowercase 64-character SHA-256 hashes. Unknown arguments or invalid | ||||||||||||||||||||||||||||||||||||||||||
| 110 | values croak. | ||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||
| 112 | =head1 ACCESSORS | ||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||
| 114 | =head2 pubkey | ||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||
| 116 | Returns the verified event pubkey. | ||||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||
| 118 | =head2 action | ||||||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||||||
| 120 | Returns the verified Blossom action. | ||||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||
| 122 | =head2 hashes | ||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||
| 124 | Returns a copy array reference of authorized C<x> tag hashes. | ||||||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||||||
| 126 | =head1 METHODS | ||||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||
| 128 | =head2 require_hash | ||||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||
| 130 | $result->require_hash($sha256); | ||||||||||||||||||||||||||||||||||||||||||
| 131 | $result->require_hash($sha256, status => 409, reason => $reason); | ||||||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||||||
| 133 | Returns true when C<$sha256> is present in the authorized hash list. Otherwise | ||||||||||||||||||||||||||||||||||||||||||
| 134 | throws L<Net::Blossom::Server::Error>. The default status is C<401> with a | ||||||||||||||||||||||||||||||||||||||||||
| 135 | C<WWW-Authenticate: Nostr> challenge. Supplying another status, such as the | ||||||||||||||||||||||||||||||||||||||||||
| 136 | BUD-04 mirror C<409>, suppresses that challenge. | ||||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||
| 138 | =head1 INTERNAL METHODS | ||||||||||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||||||||
| 140 | =head2 BUILDARGS | ||||||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||||||
| 142 | Normalizes constructor arguments for Class::Tiny. | ||||||||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||||||
| 144 | =head2 BUILD | ||||||||||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||||||||||
| 146 | Validates the constructed object for Class::Tiny. | ||||||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||||||
| 148 | =cut | ||||||||||||||||||||||||||||||||||||||||||