line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Google::Cloud::Speech::Auth; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
6
|
use Mojo::Base '-base'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
5
|
|
4
|
1
|
|
|
1
|
|
468
|
use Mojo::Collection; |
|
1
|
|
|
|
|
76903
|
|
|
1
|
|
|
|
|
49
|
|
5
|
1
|
|
|
1
|
|
369
|
use Mojo::JSON qw(encode_json decode_json); |
|
1
|
|
|
|
|
11595
|
|
|
1
|
|
|
|
|
70
|
|
6
|
1
|
|
|
1
|
|
366
|
use Mojo::JWT::Google; |
|
1
|
|
|
|
|
18510
|
|
|
1
|
|
|
|
|
7
|
|
7
|
1
|
|
|
1
|
|
436
|
use Mojo::UserAgent; |
|
1
|
|
|
|
|
140242
|
|
|
1
|
|
|
|
|
7
|
|
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
|
0
|
0
|
|
|
|
if ( my $res = $tx->success and $tx->res->json('/access_token') ) { |
46
|
0
|
|
|
|
|
|
my $token_obj = $res->json; |
47
|
0
|
|
|
|
|
|
my $token = $token_obj->{'token_type'} . ' ' . $token_obj->{'access_token'}; |
48
|
0
|
|
|
|
|
|
$self->{'token'} = $token; |
49
|
|
|
|
|
|
|
|
50
|
0
|
|
|
|
|
|
return $self; |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
|
53
|
0
|
|
|
|
|
|
my $error_obj = $tx->res->json; |
54
|
0
|
|
|
|
|
|
die "No authorization provided: `$error_obj->{error_description}`"; |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
sub has_valid_token { |
58
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
59
|
0
|
0
|
|
|
|
|
return undef unless my $token = $self->token; |
60
|
0
|
0
|
|
|
|
|
return undef unless my $jwt = $self->jwt_token; |
61
|
|
|
|
|
|
|
return undef |
62
|
0
|
0
|
|
|
|
|
unless my $expires_in |
63
|
|
|
|
|
|
|
= $self->jwt_token->issue_at + $self->jwt_token->expires_in; |
64
|
0
|
0
|
|
|
|
|
return undef unless time < ( $expires_in - 10 ); |
65
|
|
|
|
|
|
|
|
66
|
0
|
|
|
|
|
|
return 1; |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
sub refresh { |
70
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
71
|
|
|
|
|
|
|
|
72
|
0
|
|
|
|
|
|
delete $self->{'token'}; |
73
|
0
|
|
|
|
|
|
$self->{'token'} = $self->_request_token; |
74
|
|
|
|
|
|
|
|
75
|
0
|
|
|
|
|
|
return $self; |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
1; |