| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package App::Toodledo::TokenCache; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 8 |  |  | 8 |  | 55 | use Moose; | 
|  | 8 |  |  |  |  | 17 |  | 
|  | 8 |  |  |  |  | 76 |  | 
| 4 | 8 |  |  | 8 |  | 52715 | use MooseX::Method::Signatures; | 
|  | 8 |  |  |  |  | 20 |  | 
|  | 8 |  |  |  |  | 90 |  | 
| 5 | 8 |  |  | 8 |  | 1750 | use YAML qw(LoadFile DumpFile); | 
|  | 8 |  |  |  |  | 18 |  | 
|  | 8 |  |  |  |  | 812 |  | 
| 6 |  |  |  |  |  |  | with 'MooseX::Log::Log4perl'; | 
| 7 |  |  |  |  |  |  |  | 
| 8 |  |  |  |  |  |  | has filename => ( is => 'rw', isa => 'Str' ); | 
| 9 |  |  |  |  |  |  |  | 
| 10 |  |  |  |  |  |  | has token_info_ref => ( is => 'rw', isa => 'HashRef[App::Toodledo::TokenInfo]', | 
| 11 |  |  |  |  |  |  | default => sub { {} } ); | 
| 12 |  |  |  |  |  |  |  | 
| 13 | 8 |  |  | 8 |  | 2318443 | method new_from_file ( $class: Str $file! ) { | 
| 14 |  |  |  |  |  |  | if ( -r $file && -f $file ) | 
| 15 |  |  |  |  |  |  | { | 
| 16 |  |  |  |  |  |  | my $token_info_ref = LoadFile( $file ); | 
| 17 |  |  |  |  |  |  | $class->new->log->debug( "Loaded token cache from $file"); | 
| 18 |  |  |  |  |  |  | _prune_deadwood( $token_info_ref ); | 
| 19 |  |  |  |  |  |  | return $class->new( filename => $file, token_info_ref => $token_info_ref ); | 
| 20 |  |  |  |  |  |  | } | 
| 21 |  |  |  |  |  |  | else | 
| 22 |  |  |  |  |  |  | { | 
| 23 |  |  |  |  |  |  | return $class->new( filename => $file ); | 
| 24 |  |  |  |  |  |  | } | 
| 25 |  |  |  |  |  |  | } | 
| 26 |  |  |  |  |  |  |  | 
| 27 |  |  |  |  |  |  |  | 
| 28 |  |  |  |  |  |  | sub _prune_deadwood | 
| 29 |  |  |  |  |  |  | { | 
| 30 | 0 |  |  | 0 |  |  | my $token_info_ref = shift; | 
| 31 |  |  |  |  |  |  |  | 
| 32 | 0 |  |  |  |  |  | for my $key ( keys %$token_info_ref ) | 
| 33 |  |  |  |  |  |  | { | 
| 34 | 0 | 0 |  |  |  |  | $token_info_ref->{$key}->is_still_good or delete $token_info_ref->{$key}; | 
| 35 |  |  |  |  |  |  | } | 
| 36 |  |  |  |  |  |  | } | 
| 37 |  |  |  |  |  |  |  | 
| 38 |  |  |  |  |  |  |  | 
| 39 | 8 |  |  | 8 |  | 373612 | method save_to_file ( Str $filename! ) { | 
| 40 |  |  |  |  |  |  | $self->log->debug("Saved token cache to $filename"); | 
| 41 |  |  |  |  |  |  | DumpFile( $filename, $self->token_info_ref ); | 
| 42 |  |  |  |  |  |  | } | 
| 43 |  |  |  |  |  |  |  | 
| 44 |  |  |  |  |  |  |  | 
| 45 | 8 |  |  | 8 |  | 174466 | method save () { | 
| 46 |  |  |  |  |  |  | $self->save_to_file( $self->filename ); | 
| 47 |  |  |  |  |  |  | } | 
| 48 |  |  |  |  |  |  |  | 
| 49 |  |  |  |  |  |  |  | 
| 50 | 8 |  |  | 8 |  | 1140056 | method add_token_info ( Str :$user_id!, Str :$app_id!, Str :$token! ) { | 
| 51 |  |  |  |  |  |  | my $key = $self->_make_key( $user_id, $app_id ); | 
| 52 |  |  |  |  |  |  | my $token_info = App::Toodledo::TokenInfo->new( token => $token ); | 
| 53 |  |  |  |  |  |  | $self->token_info_ref->{$key} = $token_info; | 
| 54 |  |  |  |  |  |  | } | 
| 55 |  |  |  |  |  |  |  | 
| 56 |  |  |  |  |  |  |  | 
| 57 | 8 |  |  | 8 |  | 839269 | method valid_token ( Str :$user_id!, Str :$app_id! ) { | 
| 58 |  |  |  |  |  |  | my $key = $self->_make_key( $user_id, $app_id ); | 
| 59 |  |  |  |  |  |  | my $token_info = $self->token_info_ref->{$key} or return; | 
| 60 |  |  |  |  |  |  | $token_info->is_still_good or return; | 
| 61 |  |  |  |  |  |  | $token_info; | 
| 62 |  |  |  |  |  |  | } | 
| 63 |  |  |  |  |  |  |  | 
| 64 |  |  |  |  |  |  |  | 
| 65 | 8 |  |  | 8 |  | 548689 | method _make_key ( Str $user_id!, Str $app_id! ) { | 
| 66 |  |  |  |  |  |  | "$user_id.$app_id"; | 
| 67 |  |  |  |  |  |  | } | 
| 68 |  |  |  |  |  |  |  | 
| 69 |  |  |  |  |  |  |  | 
| 70 |  |  |  |  |  |  | package App::Toodledo::TokenInfo; | 
| 71 |  |  |  |  |  |  |  | 
| 72 | 8 |  |  | 8 |  | 1312 | use Moose; | 
|  | 8 |  |  |  |  | 19 |  | 
|  | 8 |  |  |  |  | 65 |  | 
| 73 | 8 |  |  | 8 |  | 51348 | use MooseX::Method::Signatures; | 
|  | 8 |  |  |  |  | 21 |  | 
|  | 8 |  |  |  |  | 68 |  | 
| 74 | 8 |  |  | 8 |  | 1873 | use MooseX::ClassAttribute; | 
|  | 8 |  |  |  |  | 21 |  | 
|  | 8 |  |  |  |  | 72 |  | 
| 75 |  |  |  |  |  |  |  | 
| 76 |  |  |  |  |  |  |  | 
| 77 |  |  |  |  |  |  | class_has Max_Token_Life => ( is => 'rw', default => 3600 * 3 );  # 3 hours | 
| 78 |  |  |  |  |  |  |  | 
| 79 |  |  |  |  |  |  | has creation_time => ( is => 'rw', isa => 'Int', default => sub { time } ); | 
| 80 |  |  |  |  |  |  | has token         => ( is => 'rw', isa => 'Str' ); | 
| 81 |  |  |  |  |  |  |  | 
| 82 |  |  |  |  |  |  |  | 
| 83 | 8 |  |  | 8 |  | 197734 | method is_still_good () { | 
| 84 |  |  |  |  |  |  | time - $self->creation_time < __PACKAGE__->Max_Token_Life; | 
| 85 |  |  |  |  |  |  | } | 
| 86 |  |  |  |  |  |  |  | 
| 87 |  |  |  |  |  |  |  | 
| 88 |  |  |  |  |  |  | 1; | 
| 89 |  |  |  |  |  |  |  | 
| 90 |  |  |  |  |  |  | __END__ | 
| 91 |  |  |  |  |  |  |  | 
| 92 |  |  |  |  |  |  | =head1 NAME | 
| 93 |  |  |  |  |  |  |  | 
| 94 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 95 |  |  |  |  |  |  |  | 
| 96 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 97 |  |  |  |  |  |  |  | 
| 98 |  |  |  |  |  |  | =head1 AUTHOR | 
| 99 |  |  |  |  |  |  |  | 
| 100 |  |  |  |  |  |  | Peter Scott C<cpan at psdt.com> | 
| 101 |  |  |  |  |  |  |  | 
| 102 |  |  |  |  |  |  | =cut |