File Coverage

blib/lib/WebService/GoogleAPI/Client/AuthStorage.pm
Criterion Covered Total %
statement 12 26 46.1
branch 0 8 0.0
condition n/a
subroutine 4 6 66.6
pod 2 2 100.0
total 18 42 42.8


line stmt bran cond sub pod time code
1 3     3   11317 use strictures;
  3         9  
  3         25  
2              
3             package WebService::GoogleAPI::Client::AuthStorage;
4              
5             # ABSTRACT: Role for classes which store your auth credentials
6              
7             our $VERSION = '0.25'; # VERSION
8              
9 3     3   760 use Moo::Role;
  3         7  
  3         20  
10 3     3   1259 use Carp;
  3         7  
  3         238  
11 3     3   1644 use WebService::GoogleAPI::Client::AccessToken;
  3         10  
  3         1407  
12              
13              
14             has user => is => 'rw';
15              
16              
17             # this is managed by the BUILD in ::Client::UserAgent,
18             # and by the BUILD in ::Client
19             has ua => is => 'rw', weak_ref => 1;
20              
21              
22              
23              
24             requires qw/
25             scopes
26             refresh_access_token
27             get_access_token
28             /;
29              
30             around get_access_token => sub {
31             my ($orig, $self) = @_;
32             my $user = $self->user;
33             my $scopes = $self->scopes;
34              
35             my $token = $self->$orig;
36             my $class = 'WebService::GoogleAPI::Client::AccessToken';
37             return $token if ref $token eq $class;
38             return WebService::GoogleAPI::Client::AccessToken->new(
39             user => $user,
40             token => $token,
41             scopes => $scopes
42             );
43             };
44              
45              
46             sub refresh_user_token {
47 0     0 1   my ($self, $params) = @_;
48 0           my $tx =
49             $self->ua->post('https://www.googleapis.com/oauth2/v4/token' => form =>
50             { %$params, grant_type => 'refresh_token' });
51 0           my $new_token = $tx->res->json('/access_token');
52 0 0         unless ($new_token) {
53 0 0         croak "Failed to refresh access token: ", join ' - ',
54             map $tx->res->json("/$_"), qw/error error_description/
55             if $tx->res->json;
56              
57             # if the error doesn't come from google
58 0           croak "Unknown error refreshing access token";
59             }
60              
61 0           return $new_token;
62             }
63              
64              
65              
66             sub refresh_service_account_token {
67 0     0 1   my ($self, $jwt) = @_;
68 0           my $tx = $self->ua->post(
69             'https://www.googleapis.com/oauth2/v4/token' => form => $jwt->as_form_data);
70 0           my $new_token = $tx->res->json('/access_token');
71 0 0         unless ($new_token) {
72 0 0         croak "Failed to get access token: ", join ' - ',
73             map $tx->res->json("/$_"), qw/error error_description/
74             if $tx->res->json;
75              
76             # if the error doesn't come from google
77 0           croak "Unknown error getting access token";
78             }
79 0           return $new_token;
80             }
81              
82             1;
83              
84             __END__