File Coverage

blib/lib/WebService/GoogleAPI/Client/AuthStorage/GapiJSON.pm
Criterion Covered Total %
statement 24 37 64.8
branch 3 6 50.0
condition n/a
subroutine 7 10 70.0
pod 3 6 50.0
total 37 59 62.7


line stmt bran cond sub pod time code
1 3     3   1848020 use strictures;
  3         5160  
  3         23  
2              
3             package WebService::GoogleAPI::Client::AuthStorage::GapiJSON;
4              
5             our $VERSION = '0.25'; # VERSION
6              
7             # ABSTRACT: Auth Storage Backend based on gapi.json
8              
9 3     3   2271 use Moo;
  3         23235  
  3         20  
10 3     3   5266 use Config::JSON;
  3         100311  
  3         98  
11 3     3   24 use Carp;
  3         7  
  3         1969  
12              
13             with 'WebService::GoogleAPI::Client::AuthStorage';
14              
15              
16             has 'path' => (is => 'rw', default => './gapi.json'); # default is gapi.json
17              
18              
19             has 'tokensfile' => (is => 'rw'); # Config::JSON object pointer
20              
21             # NOTE- this type of class has getters and setters b/c the implementation of
22             # getting and setting depends on what's storing
23              
24             sub BUILD {
25 3     3 0 4944 my ($self) = @_;
26 3         45 $self->tokensfile(Config::JSON->new($self->path));
27 3         6198 my $missing = grep !$_, map $self->get_from_storage($_),
28             qw/client_id client_secret/;
29 3 50       102 croak <
30             Malformed gapi.json detected. We need the client_id and client_secret in order
31             to refresh expired tokens
32             NOCLIENT
33              
34 3         18 return $self;
35             }
36              
37              
38             sub get_access_token {
39             my ($self) = @_;
40             my $value = $self->get_from_storage('access_token');
41             return $value;
42             }
43              
44              
45             sub refresh_access_token {
46 0     0 1 0 my ($self) = @_;
47 0         0 my %p = map { ($_ => $self->get_from_storage($_)) }
  0         0  
48             qw/client_id client_secret refresh_token/;
49              
50 0 0       0 croak <
51             If your credentials are missing the refresh_token - consider removing the auth at
52             https://myaccount.google.com/permissions as The oauth2 server will only ever mint one refresh
53             token at a time, and if you request another access token via the flow it will operate as if
54             you only asked for an access token.
55             MISSINGCREDS
56 0         0 my $user = $self->user;
57              
58 0         0 my $new_token = $self->refresh_user_token(\%p);
59              
60 0         0 $self->tokensfile->set("gapi/tokens/$user/access_token", $new_token);
61 0         0 return $new_token;
62             }
63              
64             sub get_token_emails_from_storage {
65 0     0 0 0 my ($self) = @_;
66 0         0 my $tokens = $self->get_from_storage('tokens');
67 0         0 return [keys %$tokens];
68             }
69              
70              
71             sub get_from_storage {
72 8     8 1 125 my ($self, $key) = @_;
73 8 100       35 if ($key =~ /_token/) {
74 2         8 return $self->tokensfile->get("gapi/tokens/${\$self->user}/$key");
  2         18  
75             } else {
76 6         36 return $self->tokensfile->get("gapi/$key");
77             }
78             }
79              
80             sub get_scopes_from_storage_as_array {
81 0     0 0 0 carp
82             'get_scopes_from_storage_as_array is being deprecated, please use the more succint scopes accessor';
83 0         0 return $_[0]->scopes;
84             }
85              
86             # NOTE - the scopes are stored as a space seperated list, and this method
87             # returns an arrayref
88             #
89              
90              
91             sub scopes {
92 2     2 1 7 my ($self) = @_;
93 2         28 return [split / /, $self->tokensfile->get('gapi/scopes')];
94             }
95              
96             9011;
97              
98             __END__