| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Mojolicious::Plugin::Google::Cloud::UserAgent; |
|
2
|
1
|
|
|
1
|
|
558
|
use Mojo::Base 'Mojolicious::Plugin'; |
|
|
1
|
|
|
|
|
1
|
|
|
|
1
|
|
|
|
|
6
|
|
|
3
|
1
|
|
|
1
|
|
588
|
use Mojo::JWT::Google; |
|
|
1
|
|
|
|
|
2778
|
|
|
|
1
|
|
|
|
|
7
|
|
|
4
|
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
our $VERSION = '0.03'; |
|
6
|
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
has oauth_url => 'https://www.googleapis.com/oauth2/v4/token'; |
|
8
|
|
|
|
|
|
|
has grant_type => 'urn:ietf:params:oauth:grant-type:jwt-bearer'; |
|
9
|
|
|
|
|
|
|
has web_token => undef; |
|
10
|
|
|
|
|
|
|
has web_token_enc => undef; |
|
11
|
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub register { |
|
13
|
3
|
|
|
3
|
1
|
773
|
my ($self, $app, $config) = @_; |
|
14
|
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
$app->helper( |
|
16
|
|
|
|
|
|
|
jwt => sub { |
|
17
|
0
|
|
|
0
|
|
0
|
my $c = shift; |
|
18
|
0
|
|
0
|
|
|
0
|
my $scopes = shift // $config->{scopes}; |
|
19
|
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
Mojo::JWT::Google->new( |
|
21
|
|
|
|
|
|
|
from_json => $config->{gcp_auth_file}, |
|
22
|
|
|
|
|
|
|
target => $config->{oauth_url} // $self->oauth_url, |
|
23
|
|
|
|
|
|
|
scopes => Mojo::Collection->new($scopes)->flatten, |
|
24
|
|
|
|
|
|
|
issue_at => time, |
|
25
|
0
|
|
0
|
|
|
0
|
expires_in => $config->{duration} // 3600 |
|
|
|
|
0
|
|
|
|
|
|
26
|
|
|
|
|
|
|
); |
|
27
|
|
|
|
|
|
|
} |
|
28
|
3
|
|
|
|
|
16
|
); |
|
29
|
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
$app->helper( |
|
31
|
|
|
|
|
|
|
gcp_ua => sub { |
|
32
|
0
|
|
|
0
|
|
|
my $c = shift; |
|
33
|
0
|
|
|
|
|
|
my $err_cb = pop; |
|
34
|
0
|
|
|
|
|
|
my $cb = pop; |
|
35
|
0
|
|
|
|
|
|
my @args = @_; |
|
36
|
|
|
|
|
|
|
|
|
37
|
0
|
0
|
0
|
|
|
|
if (!$self->web_token |
|
38
|
|
|
|
|
|
|
or ($self->web_token->issue_at + $self->web_token->expires_in) < time) |
|
39
|
|
|
|
|
|
|
{ |
|
40
|
0
|
|
|
|
|
|
$self->web_token($c->jwt); |
|
41
|
0
|
|
|
|
|
|
$self->web_token_enc($self->web_token->encode); |
|
42
|
|
|
|
|
|
|
} |
|
43
|
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
Mojo::IOLoop::Delay->new->steps( |
|
45
|
|
|
|
|
|
|
sub { |
|
46
|
0
|
|
|
|
|
|
my $d = shift; |
|
47
|
|
|
|
|
|
|
$c->ua->post( |
|
48
|
|
|
|
|
|
|
$self->oauth_url, |
|
49
|
|
|
|
|
|
|
form => { |
|
50
|
0
|
|
0
|
|
|
|
grant_type => $config->{grant_type} // $self->grant_type, |
|
51
|
|
|
|
|
|
|
assertion => $self->web_token_enc |
|
52
|
|
|
|
|
|
|
}, |
|
53
|
|
|
|
|
|
|
$d->begin |
|
54
|
|
|
|
|
|
|
); |
|
55
|
|
|
|
|
|
|
}, |
|
56
|
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
sub { |
|
58
|
0
|
|
|
|
|
|
my $d = shift; |
|
59
|
0
|
|
|
|
|
|
my $tx = pop; |
|
60
|
|
|
|
|
|
|
|
|
61
|
0
|
0
|
|
|
|
|
if ($tx->res->json('/access_token')) { |
|
62
|
0
|
|
|
|
|
|
$d->pass($tx->res->json); |
|
63
|
|
|
|
|
|
|
} |
|
64
|
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
else { |
|
66
|
0
|
|
|
|
|
|
$d->remaining([$err_cb]); |
|
67
|
0
|
|
|
|
|
|
$d->pass($tx); |
|
68
|
|
|
|
|
|
|
} |
|
69
|
|
|
|
|
|
|
}, |
|
70
|
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
sub { |
|
72
|
0
|
|
|
|
|
|
my $d = shift; |
|
73
|
0
|
|
|
|
|
|
my $token = pop; |
|
74
|
|
|
|
|
|
|
|
|
75
|
0
|
|
|
|
|
|
my $tx = $c->ua->transactor->tx(@args); |
|
76
|
0
|
|
|
|
|
|
$tx->req->headers->header(Authorization => $token->{token_type} . ' ' . $token->{access_token}); |
|
77
|
0
|
|
|
|
|
|
$c->ua->start($tx, $d->begin); |
|
78
|
|
|
|
|
|
|
}, |
|
79
|
|
|
|
|
|
|
|
|
80
|
0
|
|
|
|
|
|
$cb |
|
81
|
|
|
|
|
|
|
); |
|
82
|
|
|
|
|
|
|
} |
|
83
|
3
|
|
|
|
|
54
|
); |
|
84
|
|
|
|
|
|
|
} |
|
85
|
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
1; |
|
87
|
|
|
|
|
|
|
__END__ |