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