| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package WebService::Akeneo::Auth; |
|
2
|
|
|
|
|
|
|
$WebService::Akeneo::Auth::VERSION = '0.001'; |
|
3
|
3
|
|
|
3
|
|
35
|
use v5.38; |
|
|
3
|
|
|
|
|
10
|
|
|
4
|
|
|
|
|
|
|
|
|
5
|
3
|
|
|
3
|
|
16
|
use Object::Pad; |
|
|
3
|
|
|
|
|
5
|
|
|
|
3
|
|
|
|
|
2034
|
|
|
6
|
3
|
|
|
3
|
|
2183
|
use Mojo::URL; |
|
|
3
|
|
|
|
|
1066332
|
|
|
|
3
|
|
|
|
|
28
|
|
|
7
|
3
|
|
|
3
|
|
358
|
use Time::HiRes 'time'; |
|
|
3
|
|
|
|
|
7
|
|
|
|
3
|
|
|
|
|
31
|
|
|
8
|
3
|
|
|
3
|
|
300
|
use MIME::Base64 (); |
|
|
3
|
|
|
|
|
8
|
|
|
|
3
|
|
|
|
|
89
|
|
|
9
|
|
|
|
|
|
|
|
|
10
|
3
|
|
|
3
|
|
2269
|
use WebService::Akeneo::HTTPError; |
|
|
3
|
|
|
|
|
11
|
|
|
|
3
|
|
|
|
|
723
|
|
|
11
|
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
class WebService::Akeneo::Auth 0.001; |
|
13
|
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
field $config :param; # WebService::Akeneo::Config |
|
15
|
|
|
|
|
|
|
field $ua :param; # Mojo::UserAgent |
|
16
|
|
|
|
|
|
|
field $token; |
|
17
|
|
|
|
|
|
|
field $refresh; |
|
18
|
|
|
|
|
|
|
field $expires_at = 0; |
|
19
|
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
method token_info { { access_token => $token, refresh_token => $refresh, expires_at => $expires_at } } |
|
21
|
|
|
|
|
|
|
method bearer { $token } |
|
22
|
|
|
|
|
|
|
|
|
23
|
0
|
|
|
0
|
|
|
method _basic_auth ($id,$secret) { MIME::Base64::encode_base64("$id:$secret", '') } |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
|
|
25
|
0
|
|
|
0
|
0
|
|
method authenticate () { |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
26
|
0
|
|
|
|
|
|
my $url = Mojo::URL->new($config->base_url . '/api/oauth/v1/token'); |
|
27
|
0
|
|
|
|
|
|
my $tx = $ua->post($url => { Authorization => 'Basic ' . $self->_basic_auth($config->client_id, $config->client_secret) } |
|
28
|
|
|
|
|
|
|
=> form => { grant_type => 'password', username => $config->username, password => $config->password, scope => $config->scope }); |
|
29
|
0
|
|
|
|
|
|
my $res = $tx->result; |
|
30
|
0
|
0
|
0
|
|
|
|
WebService::Akeneo::HTTPError->new(code=>$res->code,message=>$res->message,body=>($res->body//''))->throw if $res->is_error; |
|
31
|
0
|
|
0
|
|
|
|
my $j = $res->json; $token = $j->{access_token}; $refresh = $j->{refresh_token}//$refresh; $expires_at = time + ($j->{expires_in}//3600); |
|
|
0
|
|
0
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
32
|
0
|
|
|
|
|
|
1; |
|
33
|
|
|
|
|
|
|
} |
|
34
|
|
|
|
|
|
|
|
|
35
|
0
|
|
|
0
|
0
|
|
method refresh_if_needed () { |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
36
|
0
|
0
|
0
|
|
|
|
return 1 if $token && time < $expires_at - 30; |
|
37
|
0
|
0
|
|
|
|
|
return $self->refresh_token if $refresh; |
|
38
|
0
|
|
|
|
|
|
return $self->authenticate; |
|
39
|
|
|
|
|
|
|
} |
|
40
|
|
|
|
|
|
|
|
|
41
|
0
|
|
|
0
|
0
|
|
method refresh_token () { |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
42
|
0
|
0
|
|
|
|
|
die 'No refresh_token' unless $refresh; |
|
43
|
0
|
|
|
|
|
|
my $url = Mojo::URL->new($config->base_url . '/api/oauth/v1/token'); |
|
44
|
0
|
|
|
|
|
|
my $tx = $ua->post($url => { Authorization => 'Basic ' . $self->_basic_auth($config->client_id, $config->client_secret) } |
|
45
|
|
|
|
|
|
|
=> form => { grant_type => 'refresh_token', refresh_token => $refresh, scope => $config->scope }); |
|
46
|
0
|
|
|
|
|
|
my $res = $tx->result; |
|
47
|
0
|
0
|
0
|
|
|
|
WebService::Akeneo::HTTPError->new(code=>$res->code,message=>$res->message,body=>($res->body//''))->throw if $res->is_error; |
|
48
|
0
|
|
0
|
|
|
|
my $j = $res->json; $token = $j->{access_token}; $refresh = $j->{refresh_token}//$refresh; $expires_at = time + ($j->{expires_in}//3600); |
|
|
0
|
|
0
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
49
|
0
|
|
|
|
|
|
1; |
|
50
|
|
|
|
|
|
|
} |
|
51
|
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
1; |