File Coverage

blib/lib/WebService/GoogleAPI/Client/AuthStorage/ServiceAccount.pm
Criterion Covered Total %
statement 12 27 44.4
branch 0 4 0.0
condition n/a
subroutine 4 7 57.1
pod 2 2 100.0
total 18 40 45.0


line stmt bran cond sub pod time code
1 3     3   23 use strictures;
  3         26  
  3         27  
2              
3             package WebService::GoogleAPI::Client::AuthStorage::ServiceAccount;
4              
5             our $VERSION = '0.27'; # VERSION
6              
7             # ABSTRACT: Manage access tokens from a service account file
8              
9              
10 3     3   648 use Moo;
  3         7  
  3         17  
11 3     3   1061 use Carp;
  3         11  
  3         171  
12 3     3   1439 use Mojo::JWT::Google;
  3         14925  
  3         26  
13              
14              
15             has scopes => is => 'rw',
16             coerce => sub {
17             my $arg = shift;
18             return [ split / /, $arg ] unless ref $arg eq 'ARRAY';
19             return $arg;
20             },
21             default => sub { [] };
22              
23              
24             has user => is => 'rw',
25             coerce => sub { $_[0] || '' },
26             default => '';
27              
28             with 'WebService::GoogleAPI::Client::AuthStorage';
29              
30              
31             has path => is => 'rw',
32             required => 1,
33             trigger => 1;
34              
35             sub _trigger_path {
36 0     0     my ($self) = @_;
37 0           $self->jwt(Mojo::JWT::Google->new(from_json => $self->path));
38             }
39              
40              
41              
42             has jwt => is => 'rw';
43              
44              
45             has tokens => is => 'ro',
46             default => sub { {} };
47              
48              
49             sub get_access_token {
50             my ($self) = @_;
51             my $token = $self->tokens->{ $self->scopes_string }{ $self->user };
52             return $self->refresh_access_token unless $token;
53             return $token;
54             }
55              
56              
57             sub refresh_access_token {
58 0     0 1   my ($self) = @_;
59 0 0         croak "Can't get a token without a set of scopes" unless @{ $self->scopes };
  0            
60              
61 0           $self->jwt->scopes($self->scopes);
62 0 0         if ($self->user) {
63 0           $self->jwt->user_as($self->user);
64             } else {
65 0           $self->jwt->user_as(undef);
66             }
67              
68 0           my $new_token = $self->refresh_service_account_token($self->jwt);
69              
70 0           $self->tokens->{ $self->scopes_string }{ $self->user } = $new_token;
71 0           return $new_token;
72             }
73              
74              
75             sub scopes_string {
76 0     0 1   my ($self) = @_;
77 0           return join ' ', @{ $self->scopes };
  0            
78             }
79              
80              
81             9001
82              
83             __END__