File Coverage

blib/lib/Google/Cloud/Speech/Auth.pm
Criterion Covered Total %
statement 15 39 38.4
branch 0 10 0.0
condition 0 3 0.0
subroutine 5 9 55.5
pod 0 4 0.0
total 20 65 30.7


line stmt bran cond sub pod time code
1             package Google::Cloud::Speech::Auth;
2              
3 1     1   8 use Mojo::Base '-base';
  1         2  
  1         5  
4 1     1   619 use Mojo::Collection;
  1         3749  
  1         46  
5 1     1   455 use Mojo::JSON qw(encode_json decode_json);
  1         22193  
  1         80  
6 1     1   488 use Mojo::JWT::Google;
  1         32112  
  1         15  
7 1     1   634 use Mojo::UserAgent;
  1         211744  
  1         11  
8              
9             has scopes => sub { ['https://www.googleapis.com/auth/cloud-platform']; };
10             has grant_type => 'urn:ietf:params:oauth:grant-type:jwt-bearer';
11             has oauth_url => 'https://www.googleapis.com/oauth2/v4/token';
12             has ua => sub { Mojo::UserAgent->new; };
13             has from_json => sub { };
14             has jwt_token_enc => undef;
15             has jwt_token => undef;
16              
17             sub jwt {
18 0     0 0   my $self = shift;
19              
20 0           return Mojo::JWT::Google->new(
21             from_json => $self->from_json,
22             target => $self->oauth_url,
23             scopes => Mojo::Collection->new( $self->scopes )->flatten,
24             issue_at => time,
25             expires_in => 20,
26             );
27             }
28              
29             has token => undef;
30              
31             sub request_token {
32 0     0 0   my $self = shift;
33              
34 0           $self->jwt_token( $self->jwt );
35 0           $self->jwt_token_enc( $self->jwt_token->encode );
36              
37 0           my $tx = $self->ua->post(
38             $self->oauth_url,
39             form => {
40             grant_type => $self->grant_type,
41             assertion => $self->jwt_token_enc
42             },
43             );
44              
45 0           my $res = $tx->res;
46 0 0 0       if ( $res->is_success and $res->json('/access_token') ) {
47 0           my $token_obj = $res->json;
48 0           my $token = $token_obj->{'token_type'} . ' ' . $token_obj->{'access_token'};
49 0           $self->{'token'} = $token;
50              
51 0           return $self;
52             }
53              
54 0           my $error_obj = $tx->res->json;
55 0           die "No authorization provided: `$error_obj->{error_description}`";
56             }
57              
58             sub has_valid_token {
59 0     0 0   my $self = shift;
60 0 0         return undef unless my $token = $self->token;
61 0 0         return undef unless my $jwt = $self->jwt_token;
62             return undef
63 0 0         unless my $expires_in
64             = $self->jwt_token->issue_at + $self->jwt_token->expires_in;
65 0 0         return undef unless time < ( $expires_in - 10 );
66              
67 0           return 1;
68             }
69              
70             sub refresh {
71 0     0 0   my $self = shift;
72            
73 0           delete $self->{'token'};
74 0           $self->{'token'} = $self->_request_token;
75            
76 0           return $self;
77             }
78              
79             1;