| 1 | package Net::Blossom::Server::Authorization; | ||
| 2 | |||
| 3 | 1 | ▶ | use strictures 2; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | |||
| 5 | 1 | ▶ | use Net::Blossom::_ConstructorArgs (); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 1 | ▶ | use Net::Blossom::Server::AuthorizationResult; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 1 | ▶ | use Net::Blossom::Server::Error; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 1 | ▶ | use Net::Blossom::Server::Request; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | |||
| 10 | 1 | ▶ | use Carp qw(croak); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | 1 | ▶ | use Class::Tiny qw(_domains), { |
|
Subroutine
BEGIN: called CC=1 SCAR=0.6
| |||
| 12 | 0 | ▶ | clock => sub { sub { time } }, |
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||
| 13 | 1 | clock_skew_seconds => 30, | |
| 14 | 1 | }; | |
| 15 | 1 | ▶ | use JSON (); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 16 | 1 | ▶ | use MIME::Base64 qw(decode_base64); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 17 | 0 | ▶ | use Net::Nostr::Event; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 18 | use Scalar::Util qw(blessed); | ||
| 19 | |||
| 20 | my $HEX64 = qr/\A[0-9a-f]{64}\z/; | ||
| 21 | my $DOMAIN = qr/\A[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?(?:\.[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?)*\z/; | ||
| 22 | my $JSON = JSON->new->utf8; | ||
| 23 | |||
| 24 | sub BUILDARGS { | ||
| 25 | my $class = shift; | ||
| 26 | my %args = Net::Blossom::_ConstructorArgs::normalize(@_); | ||
| 27 | my %known = map { $_ => 1 } qw(domains clock clock_skew_seconds); | ||
| 28 | my @unknown = grep { !exists $known{$_} } keys %args; | ||
| 29 | croak "unknown argument(s): " . join(', ', sort @unknown) if @unknown; | ||
| 30 | |||
| 31 | $args{domains} = [] unless defined $args{domains}; | ||
| 32 | croak "domains must be an array reference" unless ref($args{domains}) eq 'ARRAY'; | ||
| 33 | for my $domain (@{$args{domains}}) { | ||
| 34 | croak "domain must be a lowercase domain name" | ||
| 35 | unless defined $domain && !ref($domain) && _valid_domain($domain); | ||
| 36 | } | ||
| 37 | $args{_domains} = [@{$args{domains}}]; | ||
| 38 | delete $args{domains}; | ||
| 39 | |||
| 40 | return \%args; | ||
| 41 | } | ||
| 42 | |||
| 43 | sub BUILD { | ||
| 44 | my ($self) = @_; | ||
| 45 | $self->clock(sub { time }) unless defined $self->clock; | ||
| 46 | croak "clock must be a code reference" unless ref($self->clock) eq 'CODE'; | ||
| 47 | $self->clock_skew_seconds(30) unless defined $self->clock_skew_seconds; | ||
| 48 | croak "clock_skew_seconds must be a non-negative integer" | ||
| 49 | unless $self->clock_skew_seconds =~ /\A\d+\z/; | ||
| 50 | return; | ||
| 51 | } | ||
| 52 | |||
| 53 | sub domains { | ||
| 54 | my ($self) = @_; | ||
| 55 | return [@{$self->_domains}]; | ||
| 56 | } | ||
| 57 | |||
| 58 | sub authorize_request { | ||
| 59 | my ($self, $request) = @_; | ||
| 60 | my $result = $self->authorize($request); | ||
| 61 | return defined $result ? $result->pubkey : undef; | ||
| 62 | } | ||
| 63 | |||
| 64 | sub authorize { | ||
| 65 | my ($self, $request) = @_; | ||
| 66 | croak "request must be a Net::Blossom::Server::Request" | ||
| 67 | unless blessed($request) && $request->isa('Net::Blossom::Server::Request'); | ||
| 68 | |||
| 69 | my $requirements = _request_requirements($request); | ||
| 70 | return undef unless defined $requirements; | ||
| 71 | |||
| 72 | my $event = $self->_event_from_authorization_header($request->header('authorization')); | ||
| 73 | return $self->_validate_event($event, %$requirements); | ||
| 74 | } | ||
| 75 | |||
| 76 | sub _request_requirements { | ||
| 77 | my ($request) = @_; | ||
| 78 | |||
| 79 | if ($request->path eq '/upload' && ($request->method eq 'PUT' || $request->method eq 'HEAD')) { | ||
| 80 | my $sha256 = _required_sha256_header($request); | ||
| 81 | return { | ||
| 82 | action => 'upload', | ||
| 83 | sha256 => $sha256, | ||
| 84 | hash_required => 1, | ||
| 85 | }; | ||
| 86 | } | ||
| 87 | |||
| 88 | if ($request->path eq '/media' && ($request->method eq 'PUT' || $request->method eq 'HEAD')) { | ||
| 89 | my $sha256 = _required_sha256_header($request); | ||
| 90 | return { | ||
| 91 | action => 'media', | ||
| 92 | sha256 => $sha256, | ||
| 93 | hash_required => 1, | ||
| 94 | }; | ||
| 95 | } | ||
| 96 | |||
| 97 | if ($request->path eq '/mirror' && $request->method eq 'PUT') { | ||
| 98 | return { | ||
| 99 | action => 'upload', | ||
| 100 | hash_required => 1, | ||
| 101 | deferred_hash => 1, | ||
| 102 | }; | ||
| 103 | } | ||
| 104 | |||
| 105 | if ($request->path =~ m{\A/([0-9a-f]{64})(?:\.[^/]+)?\z}) { | ||
| 106 | return { | ||
| 107 | action => 'get', | ||
| 108 | sha256 => $1, | ||
| 109 | hash_required => 0, | ||
| 110 | } if $request->method eq 'GET' || $request->method eq 'HEAD'; | ||
| 111 | } | ||
| 112 | |||
| 113 | if ($request->path =~ m{\A/([0-9a-f]{64})\z}) { | ||
| 114 | return { | ||
| 115 | action => 'delete', | ||
| 116 | sha256 => $1, | ||
| 117 | hash_required => 1, | ||
| 118 | } if $request->method eq 'DELETE'; | ||
| 119 | |||
| 120 | return undef; | ||
| 121 | } | ||
| 122 | |||
| 123 | if ($request->path =~ m{\A/list/[0-9a-f]{64}\z} && $request->method eq 'GET') { | ||
| 124 | return { | ||
| 125 | action => 'list', | ||
| 126 | hash_required => 0, | ||
| 127 | }; | ||
| 128 | } | ||
| 129 | |||
| 130 | return undef; | ||
| 131 | } | ||
| 132 | |||
| 133 | sub _required_sha256_header { | ||
| 134 | my ($request) = @_; | ||
| 135 | my $sha256 = $request->header('x-sha-256'); | ||
| 136 | _unauthorized('missing X-SHA-256 header') unless defined $sha256 && length $sha256; | ||
| 137 | _unauthorized('X-SHA-256 must be 64-char lowercase hex') unless $sha256 =~ $HEX64; | ||
| 138 | return $sha256; | ||
| 139 | } | ||
| 140 | |||
| 141 | sub _event_from_authorization_header { | ||
| 142 | my ($self, $header) = @_; | ||
| 143 | _unauthorized('authorization header is required') | ||
| 144 | unless defined $header && length $header; | ||
| 145 | |||
| 146 | my ($scheme, $payload) = split /\s+/, $header, 2; | ||
| 147 | _unauthorized('expected Nostr authorization scheme') | ||
| 148 | unless defined $scheme && $scheme eq 'Nostr'; | ||
| 149 | _unauthorized('authorization payload is required') | ||
| 150 | unless defined $payload && length $payload; | ||
| 151 | _unauthorized('authorization payload must be base64url') | ||
| 152 | unless $payload =~ /\A[A-Za-z0-9_-]+\z/ && length($payload) % 4 != 1; | ||
| 153 | |||
| 154 | my $b64 = $payload; | ||
| 155 | $b64 =~ tr{-_}{+/}; | ||
| 156 | $b64 .= '=' while length($b64) % 4; | ||
| 157 | my $json = decode_base64($b64); | ||
| 158 | _unauthorized('authorization payload is empty') | ||
| 159 | unless defined $json && length $json; | ||
| 160 | |||
| 161 | my $data = eval { $JSON->decode($json) }; | ||
| 162 | _unauthorized('authorization payload must be JSON object') | ||
| 163 | if $@ || ref($data) ne 'HASH'; | ||
| 164 | |||
| 165 | my $event = eval { Net::Nostr::Event->from_wire($data) }; | ||
| 166 | _unauthorized('authorization event is invalid') if $@; | ||
| 167 | |||
| 168 | eval { $event->validate; 1 } | ||
| 169 | or _unauthorized('authorization signature is invalid'); | ||
| 170 | |||
| 171 | return $event; | ||
| 172 | } | ||
| 173 | |||
| 174 | sub _validate_event { | ||
| 175 | my ($self, $event, %requirements) = @_; | ||
| 176 | my $now = $self->clock->(); | ||
| 177 | |||
| 178 | _unauthorized('authorization kind must be 24242') | ||
| 179 | unless $event->kind == 24242; | ||
| 180 | my $clock_skew_seconds = $self->clock_skew_seconds; | ||
| 181 | my $created_at_ok = $clock_skew_seconds | ||
| 182 | ? $event->created_at <= $now + $clock_skew_seconds | ||
| 183 | : $event->created_at < $now; | ||
| 184 | _unauthorized('authorization created_at is too far in the future') | ||
| 185 | unless $created_at_ok; | ||
| 186 | |||
| 187 | my $expiration = _first_tag_value($event, 'expiration'); | ||
| 188 | _unauthorized('authorization expiration tag is required') | ||
| 189 | unless defined $expiration; | ||
| 190 | _unauthorized('authorization expiration must be a non-negative integer') | ||
| 191 | unless $expiration =~ /\A\d+\z/; | ||
| 192 | _unauthorized('authorization token is expired') | ||
| 193 | unless $expiration > $now; | ||
| 194 | |||
| 195 | my @actions = _tag_values($event, 't'); | ||
| 196 | _unauthorized('authorization action tag is required') | ||
| 197 | unless @actions; | ||
| 198 | _unauthorized('authorization action does not match request') | ||
| 199 | unless grep { $_ eq $requirements{action} } @actions; | ||
| 200 | |||
| 201 | my @servers = _tag_values($event, 'server'); | ||
| 202 | if (@servers) { | ||
| 203 | my %allowed = map { $_ => 1 } @{$self->_domains}; | ||
| 204 | _unauthorized('authorization server scope does not match') | ||
| 205 | unless grep { $allowed{$_} } @servers; | ||
| 206 | } | ||
| 207 | |||
| 208 | my @hashes = _tag_values($event, 'x'); | ||
| 209 | for my $hash (@hashes) { | ||
| 210 | _unauthorized('authorization x tag must be 64-char lowercase hex') | ||
| 211 | unless defined $hash && $hash =~ $HEX64; | ||
| 212 | } | ||
| 213 | |||
| 214 | if ($requirements{hash_required}) { | ||
| 215 | _unauthorized('authorization x tag is required') | ||
| 216 | unless @hashes; | ||
| 217 | if (!$requirements{deferred_hash}) { | ||
| 218 | _unauthorized('authorization x tag does not match request hash') | ||
| 219 | unless grep { $_ eq $requirements{sha256} } @hashes; | ||
| 220 | } | ||
| 221 | } | ||
| 222 | elsif (defined $requirements{sha256} && @hashes) { | ||
| 223 | _unauthorized('authorization x tag does not match request hash') | ||
| 224 | unless grep { $_ eq $requirements{sha256} } @hashes; | ||
| 225 | } | ||
| 226 | |||
| 227 | return Net::Blossom::Server::AuthorizationResult->new( | ||
| 228 | pubkey => $event->pubkey, | ||
| 229 | action => $requirements{action}, | ||
| 230 | hashes => \@hashes, | ||
| 231 | ); | ||
| 232 | } | ||
| 233 | |||
| 234 | sub _tag_values { | ||
| 235 | my ($event, $name) = @_; | ||
| 236 | my @values; | ||
| 237 | for my $tag (@{$event->tags}) { | ||
| 238 | next unless @$tag >= 2 && defined $tag->[0] && $tag->[0] eq $name; | ||
| 239 | push @values, $tag->[1]; | ||
| 240 | } | ||
| 241 | return @values; | ||
| 242 | } | ||
| 243 | |||
| 244 | sub _first_tag_value { | ||
| 245 | my ($event, $name) = @_; | ||
| 246 | my @values = _tag_values($event, $name); | ||
| 247 | return $values[0]; | ||
| 248 | } | ||
| 249 | |||
| 250 | sub _unauthorized { | ||
| 251 | my ($reason) = @_; | ||
| 252 | Net::Blossom::Server::Error->throw( | ||
| 253 | status => 401, | ||
| 254 | reason => $reason, | ||
| 255 | headers => { 'WWW-Authenticate' => 'Nostr' }, | ||
| 256 | ); | ||
| 257 | } | ||
| 258 | |||
| 259 | sub _valid_domain { | ||
| 260 | my ($domain) = @_; | ||
| 261 | return length($domain) <= 253 && $domain =~ $DOMAIN; | ||
| 262 | } | ||
| 263 | |||
| 264 | 1; | ||
| 265 | |||
| 266 | =pod | ||
| 267 | |||
| 268 | =head1 NAME | ||
| 269 | |||
| 270 | Net::Blossom::Server::Authorization - BUD-11 server authorization verifier | ||
| 271 | |||
| 272 | =head1 SYNOPSIS | ||
| 273 | |||
| 274 | use Net::Blossom::Server::Authorization; | ||
| 275 | |||
| 276 | my $auth = Net::Blossom::Server::Authorization->new( | ||
| 277 | domains => ['cdn.example.com'], | ||
| 278 | ); | ||
| 279 | |||
| 280 | my $pubkey = $auth->authorize_request($request); | ||
| 281 | my $result = $auth->authorize($request); | ||
| 282 | |||
| 283 | =head1 DESCRIPTION | ||
| 284 | |||
| 285 | C<Net::Blossom::Server::Authorization> validates BUD-11 Blossom authorization | ||
| 286 | tokens. It parses C<Nostr> authorization headers, verifies the Nostr event ID | ||
| 287 | and signature with C<Net::Nostr::Event>, checks the BUD-11 tags, and returns the | ||
| 288 | event pubkey. | ||
| 289 | |||
| 290 | Authorization failures throw C<Net::Blossom::Server::Error> with status C<401> | ||
| 291 | and C<WWW-Authenticate: Nostr>. | ||
| 292 | |||
| 293 | =head1 CONSTRUCTOR | ||
| 294 | |||
| 295 | =head2 new | ||
| 296 | |||
| 297 | my $auth = Net::Blossom::Server::Authorization->new(%args); | ||
| 298 | |||
| 299 | Optional arguments: | ||
| 300 | |||
| 301 | =over 4 | ||
| 302 | |||
| 303 | =item * C<domains> | ||
| 304 | |||
| 305 | Array reference of lowercase server domain names. These are used to validate | ||
| 306 | BUD-11 C<server> tags. They are domains only, not URLs. | ||
| 307 | |||
| 308 | =item * C<clock> | ||
| 309 | |||
| 310 | Code reference returning the current Unix timestamp. Defaults to C<time>. | ||
| 311 | |||
| 312 | =item * C<clock_skew_seconds> | ||
| 313 | |||
| 314 | Non-negative integer number of seconds by which C<created_at> may be ahead of | ||
| 315 | the verifier clock. Defaults to C<30>. Set to C<0> to require C<created_at> to | ||
| 316 | be strictly in the past. | ||
| 317 | |||
| 318 | =back | ||
| 319 | |||
| 320 | Unknown arguments or invalid values croak. | ||
| 321 | |||
| 322 | =head1 ACCESSORS | ||
| 323 | |||
| 324 | =head2 domains | ||
| 325 | |||
| 326 | Returns a copy array reference of configured server domains. | ||
| 327 | |||
| 328 | =head2 clock | ||
| 329 | |||
| 330 | Returns the clock code reference. | ||
| 331 | |||
| 332 | =head2 clock_skew_seconds | ||
| 333 | |||
| 334 | Returns the accepted C<created_at> clock skew in seconds. | ||
| 335 | |||
| 336 | =head1 METHODS | ||
| 337 | |||
| 338 | =head2 authorize_request | ||
| 339 | |||
| 340 | my $pubkey = $auth->authorize_request($request); | ||
| 341 | |||
| 342 | Validates the request's BUD-11 C<Authorization> header and returns the verified | ||
| 343 | event pubkey. The request must be a C<Net::Blossom::Server::Request>. | ||
| 344 | |||
| 345 | =head2 authorize | ||
| 346 | |||
| 347 | my $result = $auth->authorize($request); | ||
| 348 | |||
| 349 | Validates the request and returns a | ||
| 350 | L<Net::Blossom::Server::AuthorizationResult>. This method is useful for | ||
| 351 | endpoints such as C<PUT /mirror>, where the authorized hash is not known until | ||
| 352 | after the server fetches and hashes the origin blob. | ||
| 353 | |||
| 354 | Implemented endpoint requirements are C<GET /E<lt>sha256E<gt>>, | ||
| 355 | C<HEAD /E<lt>sha256E<gt>>, C<PUT /upload>, C<HEAD /upload>, | ||
| 356 | C<DELETE /E<lt>sha256E<gt>>, C<GET /list/E<lt>pubkeyE<gt>>, | ||
| 357 | C<PUT /mirror>, C<PUT /media>, and C<HEAD /media>. Unknown routes return | ||
| 358 | C<undef> so the server core can return its normal routing response. | ||
| 359 | |||
| 360 | =head1 INTERNAL METHODS | ||
| 361 | |||
| 362 | =head2 BUILDARGS | ||
| 363 | |||
| 364 | Normalizes constructor arguments for Class::Tiny. | ||
| 365 | |||
| 366 | =head2 BUILD | ||
| 367 | |||
| 368 | Validates the constructed object for Class::Tiny. | ||
| 369 | |||
| 370 | =cut | ||