| 1 | package Langertha::Knarr::Authorizer::StaticKey; | ||
| 2 | # ABSTRACT: One shared proxy key for every route and model (auth_token) | ||
| 3 | our $VERSION = '1.103'; | ||
| 4 | 60 | ▶ | use Moose; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 60 | ▶ | use Digest::SHA qw( sha256 ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 60 | ▶ | use Encode (); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 0 | ▶ | use namespace::autoclean; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 8 | |||
| 9 | with 'Langertha::Knarr::Authorizer'; | ||
| 10 | |||
| 11 | |||
| 12 | has key => ( | ||
| 13 | is => 'ro', | ||
| 14 | isa => 'Maybe[Str]', | ||
| 15 | default => sub { undef }, | ||
| 16 | ); | ||
| 17 | |||
| 18 | sub identify { | ||
| 19 | my ( $self, $http_req, $action ) = @_; | ||
| 20 | return $self->_allowed if $self->_open; | ||
| 21 | # Discovery endpoints stay anonymous so clients can introspect. | ||
| 22 | return $self->_allowed if $action eq 'a2a_card'; | ||
| 23 | # One element of the value is enough: a header sent twice arrives as one | ||
| 24 | # value joined with ', ' (k53). | ||
| 25 | for my $name ( 'Authorization', 'x-api-key' ) { | ||
| 26 | my $value = scalar $http_req->header($name); | ||
| 27 | next unless defined $value; | ||
| 28 | my $bearer = lc $name eq 'authorization'; | ||
| 29 | for my $element ( split /,/, $value ) { | ||
| 30 | $element =~ s/\A\s+|\s+\z//g; | ||
| 31 | next if $bearer && $element !~ s/\ABearer\s+//i; | ||
| 32 | return $self->_allowed if $self->_same_key( $element, $self->key ); | ||
| 33 | } | ||
| 34 | } | ||
| 35 | return { status => 401, message => 'unauthorized' }; | ||
| 36 | } | ||
| 37 | |||
| 38 | |||
| 39 | sub authorize { return } | ||
| 40 | |||
| 41 | |||
| 42 | sub visible_models { | ||
| 43 | my ( $self, $identity, $models ) = @_; | ||
| 44 | return $models; | ||
| 45 | } | ||
| 46 | |||
| 47 | |||
| 48 | sub forwards_client_key { 1 } | ||
| 49 | |||
| 50 | |||
| 51 | sub _open { | ||
| 52 | my ($self) = @_; | ||
| 53 | my $key = $self->key; | ||
| 54 | return !( defined $key && length $key ); | ||
| 55 | } | ||
| 56 | |||
| 57 | sub _allowed { return { identity => {} } } | ||
| 58 | |||
| 59 | # Equal keys, compared without an early exit: both go through SHA-256 and the | ||
| 60 | # digests are compared whole, so the time does not depend on how much of the | ||
| 61 | # key was right (k76). Callable on the class. | ||
| 62 | sub _same_key { | ||
| 63 | my ( $self, $given, $want ) = @_; | ||
| 64 | return 0 unless defined $given && defined $want; | ||
| 65 | my $g = sha256( utf8::is_utf8($given) ? Encode::encode( 'UTF-8', $given ) : $given ); | ||
| 66 | my $w = sha256( utf8::is_utf8($want) ? Encode::encode( 'UTF-8', $want ) : $want ); | ||
| 67 | my $diff = 0; | ||
| 68 | $diff |= ord( substr $g, $_, 1 ) ^ ord( substr $w, $_, 1 ) for 0 .. 31; | ||
| 69 | return $diff ? 0 : 1; | ||
| 70 | } | ||
| 71 | |||
| 72 | __PACKAGE__->meta->make_immutable; | ||
| 73 | 1; | ||
| 74 | |||
| 75 | __END__ | ||