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   10308 use strictures;
  3         9  
  3         26  
2              
3             package WebService::GoogleAPI::Client::AuthStorage;
4              
5             # ABSTRACT: Role for classes which store your auth credentials
6              
7             our $VERSION = '0.27'; # VERSION
8              
9 3     3   752 use Moo::Role;
  3         9  
  3         19  
10 3     3   1180 use Carp;
  3         13  
  3         195  
11 3     3   1313 use WebService::GoogleAPI::Client::AccessToken;
  3         11  
  3         1742  
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 = $self->ua->post(
49             'https://www.googleapis.com/oauth2/v4/token' => form => { %$params, grant_type => 'refresh_token' });
50 0           my $new_token = $tx->res->json('/access_token');
51 0 0         unless ($new_token) {
52 0 0         croak "Failed to refresh access token: ", join ' - ', map $tx->res->json("/$_"), qw/error error_description/
53             if $tx->res->json;
54             # if the error doesn't come from google
55 0           croak "Unknown error refreshing access token";
56             }
57              
58 0           return $new_token;
59             }
60              
61              
62              
63             sub refresh_service_account_token {
64 0     0 1   my ($self, $jwt) = @_;
65 0           my $tx = $self->ua->post('https://www.googleapis.com/oauth2/v4/token' => form => $jwt->as_form_data);
66 0           my $new_token = $tx->res->json('/access_token');
67 0 0         unless ($new_token) {
68 0 0         croak "Failed to get access token: ", join ' - ', map $tx->res->json("/$_"), qw/error error_description/
69             if $tx->res->json;
70             # if the error doesn't come from google
71 0           croak "Unknown error getting access token";
72             }
73 0           return $new_token;
74             }
75              
76             1;
77              
78             __END__