| 1 | 2 | ▶ | use strictures; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 2 | |||||||||||
| 3 | package WebService::GoogleAPI::Client::AuthStorage; | ||||||||||
| 4 | |||||||||||
| 5 | # ABSTRACT: Role for classes which store your auth credentials | ||||||||||
| 6 | |||||||||||
| 7 | 2 | our $VERSION = '0.27'; # VERSION | |||||||||
| 8 | |||||||||||
| 9 | 2 | ▶ | use Moo::Role; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 2 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 2 | ▶ | use WebService::GoogleAPI::Client::AccessToken; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | |||||||||||
| 13 | |||||||||||
| 14 | 2 | has user => is => 'rw'; | |||||||||
| 15 | |||||||||||
| 16 | |||||||||||
| 17 | # this is managed by the BUILD in ::Client::UserAgent, | ||||||||||
| 18 | # and by the BUILD in ::Client | ||||||||||
| 19 | 2 | has ua => is => 'rw', weak_ref => 1; | |||||||||
| 20 | |||||||||||
| 21 | |||||||||||
| 22 | |||||||||||
| 23 | |||||||||||
| 24 | 2 | requires qw/ | |||||||||
| 25 | scopes | ||||||||||
| 26 | refresh_access_token | ||||||||||
| 27 | get_access_token | ||||||||||
| 28 | /; | ||||||||||
| 29 | |||||||||||
| 30 | 2 | ▶ | around get_access_token => sub { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||
| 31 | 2 | my ($orig, $self) = @_; | |||||||||
| 32 | 2 | my $user = $self->user; | |||||||||
| 33 | 2 | my $scopes = $self->scopes; | |||||||||
| 34 | |||||||||||
| 35 | 2 | my $token = $self->$orig; | |||||||||
| 36 | 2 | my $class = 'WebService::GoogleAPI::Client::AccessToken'; | |||||||||
| 37 | 2 | ▶ | return $token if ref $token eq $class; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 38 | 2 | return WebService::GoogleAPI::Client::AccessToken->new( | |||||||||
| 39 | user => $user, | ||||||||||
| 40 | token => $token, | ||||||||||
| 41 | scopes => $scopes | ||||||||||
| 42 | ); | ||||||||||
| 43 | 2 | }; | |||||||||
| 44 | |||||||||||
| 45 | |||||||||||
| 46 | 0 | ▶ | sub refresh_user_token { | ||||||||
|
Subroutine
refresh_user_token: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||
| 47 | 0 | 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 | ▶ | unless ($new_token) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 52 | 0 | ▶ | croak "Failed to refresh access token: ", join ' - ', map $tx->res->json("/$_"), qw/error error_description/ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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 | 0 | ▶ | sub refresh_service_account_token { | ||||||||
|
Subroutine
refresh_service_account_token: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||
| 64 | 0 | 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 | ▶ | unless ($new_token) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 68 | 0 | ▶ | croak "Failed to get access token: ", join ' - ', map $tx->res->json("/$_"), qw/error error_description/ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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 | 2 | 1; | |||||||||
| 77 | |||||||||||
| 78 | __END__ | ||||||||||