« blib/lib/Langertha/Knarr/Handler/Router.pm
↑ Summary
blib/lib/Langertha/Knarr/Protocol/OpenAI.pm »
| 1 | package Langertha::Knarr::Authorizer::Token; | ||
| 2 | # ABSTRACT: One short-lived token per model, checked against a token store | ||
| 3 | our $VERSION = '1.103'; | ||
| 4 | use Moose; | ||
| 5 | use Future; | ||
| 6 | use Log::Any qw( $log ); | ||
| 7 | use Scalar::Util qw( weaken ); | ||
| 8 | use namespace::autoclean; | ||
| 9 | |||
| 10 | with 'Langertha::Knarr::Authorizer'; | ||
| 11 | |||
| 12 | |||
| 13 | has store => ( | ||
| 14 | is => 'ro', | ||
| 15 | does => 'Langertha::Knarr::TokenStore', | ||
| 16 | required => 1, | ||
| 17 | ); | ||
| 18 | |||
| 19 | has match_model => ( | ||
| 20 | is => 'ro', | ||
| 21 | isa => 'Bool', | ||
| 22 | default => 1, | ||
| 23 | ); | ||
| 24 | |||
| 25 | # Hits by token hash. Dropped whole on every invalidation. | ||
| 26 | has _cache => ( | ||
| 27 | is => 'ro', | ||
| 28 | default => sub { {} }, | ||
| 29 | ); | ||
| 30 | |||
| 31 | # Counts invalidations: a lookup that started before one must not put its | ||
| 32 | # row into the new cache. | ||
| 33 | has _generation => ( | ||
| 34 | is => 'rw', | ||
| 35 | default => 0, | ||
| 36 | ); | ||
| 37 | |||
| 38 | # The process that started the store: once per process (k82). | ||
| 39 | has _started_pid => ( | ||
| 40 | is => 'rw', | ||
| 41 | default => 0, | ||
| 42 | ); | ||
| 43 | |||
| 44 | sub BUILD { | ||
| 45 | my ($self) = @_; | ||
| 46 | weaken( my $weak = $self ); | ||
| 47 | $self->store->on_invalidate( sub { | ||
| 48 | my ($reason) = @_; | ||
| 49 | my $self = $weak or return; | ||
| 50 | $self->clear_cache; | ||
| 51 | $log->debugf( 'token check: cache dropped (%s)', $reason // 'invalidate' ); | ||
| 52 | } ); | ||
| 53 | } | ||
| 54 | |||
| 55 | |||
| 56 | sub start { | ||
| 57 | my ($self) = @_; | ||
| 58 | return if $self->_started_pid == $$; | ||
| 59 | $self->_started_pid($$); | ||
| 60 | $self->store->start; | ||
| 61 | return; | ||
| 62 | } | ||
| 63 | |||
| 64 | |||
| 65 | sub clear_cache { | ||
| 66 | my ($self) = @_; | ||
| 67 | %{ $self->_cache } = (); | ||
| 68 | $self->_generation( $self->_generation + 1 ); | ||
| 69 | return; | ||
| 70 | } | ||
| 71 | |||
| 72 | |||
| 73 | sub identify_f { | ||
| 74 | my ( $self, $http_req, $action ) = @_; | ||
| 75 | return Future->done( $self->_anonymous ) if $action eq 'a2a_card'; | ||
| 76 | my ( $hash, $refused ) = $self->_token_hash($http_req); | ||
| 77 | return Future->done($refused) if $refused; | ||
| 78 | if ( my $result = $self->_cached($hash) ) { | ||
| 79 | return Future->done($result); | ||
| 80 | } | ||
| 81 | # A miss asks the store, which connects on its first lookup and fails | ||
| 82 | # every lookup while it is not available. | ||
| 83 | my $generation = $self->_generation; | ||
| 84 | weaken( my $weak = $self ); | ||
| 85 | my $f = eval { $self->store->lookup_f($hash) } | ||
| 86 | // Future->fail( 'lookup did not start', 'token_store' ); | ||
| 87 | # The store's failure text stays with the store: it logs what it may. | ||
| 88 | return $f->then( sub { | ||
| 89 | my ($row) = @_; | ||
| 90 | my $self = $weak or return Future->done( __PACKAGE__->_unavailable('authorizer gone') ); | ||
| 91 | return Future->done( $self->_found( $hash, $row, $generation ) ); | ||
| 92 | } )->else( sub { | ||
| 93 | return Future->done( __PACKAGE__->_unavailable('lookup failed') ); | ||
| 94 | } ); | ||
| 95 | } | ||
| 96 | |||
| 97 | |||
| 98 | sub identify { | ||
| 99 | my ( $self, $http_req, $action ) = @_; | ||
| 100 | return $self->_anonymous if $action eq 'a2a_card'; | ||
| 101 | my ( $hash, $refused ) = $self->_token_hash($http_req); | ||
| 102 | return $refused if $refused; | ||
| 103 | my $row = eval { $self->store->lookup($hash) }; | ||
| 104 | return $self->_unavailable('lookup failed') if $@; | ||
| 105 | return $self->_found( $hash, $row ); | ||
| 106 | } | ||
| 107 | |||
| 108 | |||
| 109 | sub authorize { | ||
| 110 | my ( $self, $identity, $action, $model ) = @_; | ||
| 111 | return ( 403, 'forbidden: not available with token auth' ) if $action eq 'manifest'; | ||
| 112 | return unless $action eq 'chat' || $action eq 'show'; | ||
| 113 | return unless $self->match_model; | ||
| 114 | my $allowed = $identity->{model}; | ||
| 115 | return if defined $model && defined $allowed && $model eq $allowed; | ||
| 116 | $log->debug('token check: model not allowed for this token'); | ||
| 117 | return ( 403, 'forbidden: model not allowed for this token' ); | ||
| 118 | } | ||
| 119 | |||
| 120 | |||
| 121 | sub visible_models { | ||
| 122 | my ( $self, $identity, $models ) = @_; | ||
| 123 | my $model = $identity->{model}; | ||
| 124 | return [] unless defined $model && length $model; | ||
| 125 | return [ { id => $model, object => 'model' } ]; | ||
| 126 | } | ||
| 127 | |||
| 128 | |||
| 129 | sub forwards_client_key { 0 } | ||
| 130 | |||
| 131 | sub _anonymous { return { identity => {} } } | ||
| 132 | |||
| 133 | sub _unauthorized { | ||
| 134 | my ( $self, $reason ) = @_; | ||
| 135 | $log->debugf( 'token check: %s', $reason ); | ||
| 136 | return { status => 401, message => 'unauthorized' }; | ||
| 137 | } | ||
| 138 | |||
| 139 | sub _unavailable { | ||
| 140 | my ( $self, $reason ) = @_; | ||
| 141 | $log->debugf( 'token check: %s', $reason ); | ||
| 142 | return { status => 503, message => 'token check unavailable' }; | ||
| 143 | } | ||
| 144 | |||
| 145 | # The hash of the one token the request presents, or ( undef, refusal ). | ||
| 146 | sub _token_hash { | ||
| 147 | my ( $self, $http_req ) = @_; | ||
| 148 | my %tokens; | ||
| 149 | for my $name ( 'Authorization', 'x-api-key' ) { | ||
| 150 | my $value = scalar $http_req->header($name); | ||
| 151 | next unless defined $value; | ||
| 152 | my $bearer = lc $name eq 'authorization'; | ||
| 153 | for my $element ( split /,/, $value ) { | ||
| 154 | $element =~ s/\A\s+|\s+\z//g; | ||
| 155 | next if $bearer && $element !~ s/\ABearer\s+//i; | ||
| 156 | next unless length $element; | ||
| 157 | $tokens{$element} = 1; | ||
| 158 | } | ||
| 159 | } | ||
| 160 | return ( undef, $self->_unauthorized('no token') ) unless %tokens; | ||
| 161 | return ( undef, $self->_unauthorized('more than one token') ) if keys %tokens > 1; | ||
| 162 | my ($token) = keys %tokens; | ||
| 163 | return $self->store->hash_token($token); | ||
| 164 | } | ||
| 165 | |||
| 166 | # The identity of a cached row that has not expired. An expired one leaves. | ||
| 167 | # A store that lost its connection has dropped the cache by then; should | ||
| 168 | # one say nothing, a hit is still refused while it is not available. | ||
| 169 | sub _cached { | ||
| 170 | my ( $self, $hash ) = @_; | ||
| 171 | my $row = $self->_cache->{$hash} or return; | ||
| 172 | return $self->_unavailable('store not available') unless $self->store->available; | ||
| 173 | return $self->_identity($row) if $self->_live($row); | ||
| 174 | delete $self->_cache->{$hash}; | ||
| 175 | return $self->_unauthorized('expired token'); | ||
| 176 | } | ||
| 177 | |||
| 178 | # A looked-up row as the identify result; cached when no invalidation came | ||
| 179 | # in since the lookup started ($generation; undef: not cached). | ||
| 180 | sub _found { | ||
| 181 | my ( $self, $hash, $row, $generation ) = @_; | ||
| 182 | return $self->_unauthorized('unknown token') unless ref $row eq 'HASH'; | ||
| 183 | return $self->_unauthorized('expired token') unless $self->_live($row); | ||
| 184 | $self->_cache->{$hash} = $row | ||
| 185 | if defined $generation && $generation == $self->_generation; | ||
| 186 | return $self->_identity($row); | ||
| 187 | } | ||
| 188 | |||
| 189 | sub _live { | ||
| 190 | my ( $self, $row ) = @_; | ||
| 191 | return defined $row->{expires} && $row->{expires} > time; | ||
| 192 | } | ||
| 193 | |||
| 194 | sub _identity { | ||
| 195 | my ( $self, $row ) = @_; | ||
| 196 | return { identity => { | ||
| 197 | model => $row->{model}, | ||
| 198 | provider => $row->{provider}, | ||
| 199 | expires => $row->{expires}, | ||
| 200 | } }; | ||
| 201 | } | ||
| 202 | |||
| 203 | __PACKAGE__->meta->make_immutable; | ||
| 204 | 1; | ||
| 205 | |||
| 206 | __END__ |