| 1 | package Langertha::Knarr::TokenStore::Memory; | ||||||||||
| 2 | # ABSTRACT: In-memory token store for tests of token_auth | ||||||||||
| 3 | 1 | our $VERSION = '1.103'; | |||||||||
| 4 | 1 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 1 | ▶ | use Carp qw( croak ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 1 | ▶ | use Future; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | |||||||||||
| 9 | 1 | has available => ( | |||||||||
| 10 | is => 'ro', | ||||||||||
| 11 | isa => 'Bool', | ||||||||||
| 12 | default => 1, | ||||||||||
| 13 | writer => '_set_available', | ||||||||||
| 14 | ); | ||||||||||
| 15 | |||||||||||
| 16 | 1 | has lookups => ( | |||||||||
| 17 | is => 'ro', | ||||||||||
| 18 | isa => 'Int', | ||||||||||
| 19 | default => 0, | ||||||||||
| 20 | writer => '_set_lookups', | ||||||||||
| 21 | ); | ||||||||||
| 22 | |||||||||||
| 23 | has _rows => ( | ||||||||||
| 24 | is => 'ro', | ||||||||||
| 25 | isa => 'HashRef', | ||||||||||
| 26 | 7 | ▶ | default => sub { {} }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 27 | 1 | ); | |||||||||
| 28 | |||||||||||
| 29 | # after the attributes: the role requires the available accessor | ||||||||||
| 30 | 1 | with 'Langertha::Knarr::TokenStore'; | |||||||||
| 31 | |||||||||||
| 32 | 7 | ▶ | sub _hash_of { | ||||||||
|
Subroutine
_hash_of: called CC=3 SCAR=11.0
| |||||||||||
| 33 | 7 | my ($self, %args) = @_; | |||||||||
| 34 | 7 | ▶ | return lc $args{hash} if defined $args{hash}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 35 | 6 | ▶ | return $self->hash_token($args{token}) if defined $args{token}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 36 | 2 | croak __PACKAGE__.": token or hash required"; | |||||||||
| 37 | } | ||||||||||
| 38 | |||||||||||
| 39 | 6 | ▶ | sub add { | ||||||||
|
Subroutine
add: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 40 | 6 | my ($self, %args) = @_; | |||||||||
| 41 | 6 | ▶ | croak __PACKAGE__."->add: model required" unless defined $args{model}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 42 | $self->_rows->{ $self->_hash_of(%args) } = { | ||||||||||
| 43 | model => $args{model}, | ||||||||||
| 44 | provider => $args{provider}, | ||||||||||
| 45 | 5 | ▶ | expires => defined $args{expires} ? $args{expires} : time + 900, | ||||||||
|
Branch: 1 branch
| |||||||||||
| 46 | }; | ||||||||||
| 47 | 4 | return; | |||||||||
| 48 | } | ||||||||||
| 49 | |||||||||||
| 50 | 2 | ▶ | sub revoke { | ||||||||
|
Subroutine
revoke: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 51 | 2 | my ($self, %args) = @_; | |||||||||
| 52 | 2 | delete $self->_rows->{ $self->_hash_of(%args) }; | |||||||||
| 53 | 1 | return; | |||||||||
| 54 | } | ||||||||||
| 55 | |||||||||||
| 56 | 4 | ▶ | sub notify { $_[0]->invalidate('notify') } | ||||||||
|
Subroutine
notify: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 57 | |||||||||||
| 58 | 1 | ▶ | sub disconnect { | ||||||||
|
Subroutine
disconnect: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 59 | 1 | my ($self) = @_; | |||||||||
| 60 | 1 | $self->_set_available(0); | |||||||||
| 61 | 1 | $self->invalidate('disconnect'); | |||||||||
| 62 | 1 | return; | |||||||||
| 63 | } | ||||||||||
| 64 | |||||||||||
| 65 | 1 | ▶ | sub reconnect { $_[0]->_set_available(1); return } | ||||||||
|
Subroutine
reconnect: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 66 | |||||||||||
| 67 | 11 | ▶ | sub _find { | ||||||||
|
Subroutine
_find: called CC=3 SCAR=11.0
| |||||||||||
| 68 | 11 | my ($self, $hash) = @_; | |||||||||
| 69 | 11 | $self->_set_lookups( $self->lookups + 1 ); | |||||||||
| 70 | 11 | ▶ | my $row = $self->_rows->{ lc $hash } or return; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 71 | 9 | ▶ | return unless $row->{expires} > time; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 72 | 7 | return { %$row }; | |||||||||
| 73 | } | ||||||||||
| 74 | |||||||||||
| 75 | 9 | ▶ | sub lookup_f { | ||||||||
|
Subroutine
lookup_f: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 76 | 9 | my ($self, $hash) = @_; | |||||||||
| 77 | 9 | ▶ | return Future->fail("token store unavailable\n", 'token_store') | ||||||||
|
Branch: 1 branch
| |||||||||||
| 78 | unless $self->available; | ||||||||||
| 79 | 7 | return Future->done( scalar $self->_find($hash) ); | |||||||||
| 80 | } | ||||||||||
| 81 | |||||||||||
| 82 | 5 | ▶ | sub lookup { | ||||||||
|
Subroutine
lookup: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 83 | 5 | my ($self, $hash) = @_; | |||||||||
| 84 | 5 | ▶ | croak "token store unavailable" unless $self->available; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 85 | 4 | return scalar $self->_find($hash); | |||||||||
| 86 | } | ||||||||||
| 87 | |||||||||||
| 88 | 1 | __PACKAGE__->meta->make_immutable; | |||||||||
| 89 | 1 | 1; | |||||||||
| 90 | |||||||||||
| 91 | __END__ | ||||||||||