blib/lib/WWW/FCM/HTTP/V1/OAuth.pm

filter: Statementstmt 87.3% 76 / 87 Branchbran 36.3% 8 / 22 Conditioncond 39.3% 13 / 33 MC/DCmcdc 4.7% 1 / 21 Subroutinesub 100% 14 / 14 Podpod 0.0% 0 / 3 totaltotal 62.2% 112 / 180 SCAR 37.5
CC
24
Cov
68%
get_token
19.9
new
14.4
request
7.6
CRAP 42.3
↑ Summary blib/lib/WWW/FCM/HTTP/V1.pm »
1 package WWW::FCM::HTTP::V1::OAuth;
2 3 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 our $VERSION = "0.01";
7  
8 use Class::Accessor::Lite (
9 3 new => 0,
10 ro => [qw/ua jwt_config scopes grant_type timeout expires_in/],
11 rw => [qw/token_url cache/],
12 3 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 3 ▶use JSON qw(encode_json decode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use JSON::WebToken;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 ▶use Furl;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 3 ▶use HTTP::Status qw(:constants);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 ▶use Carp qw(carp croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 3 our $DEFAULT_TOKEN_URL = "https://accounts.google.com/o/oauth2/token";
21 3 our $DEFAULT_GRANT_TYPE = "urn:ietf:params:oauth:grant-type:jwt-bearer";
22 3 our $DEFAULT_TIMEOUT = 5;
23 3 our $DEFAULT_EXPIRES_IN = 3600;
24  
25 sub new {
26 4 ▶ my ($class, %args) = @_;
Subroutine new: called CC=9 SCAR=26.7
Pod undocumented
27 4 ▶ $args{grant_type} ||= $DEFAULT_GRANT_TYPE;
Condition33%1/3
$args{"grant_type"} ||= $WWW::FCM::HTTP::V1::OAuth::DEFAULT_GRANT_TYPE
l!l&&r!l&&!r
040
MC/DC0%0/2
$args{"grant_type"} ||= $WWW::FCM::HTTP::V1::OAuth::DEFAULT_GRANT_TYPE
$args{"grant_type"} $WWW::FCM::HTTP::V1::OAuth::DEFAULT_GRANT_TYPE
Truth table33%1/3
A ||= B
A= $args{"grant_type"}B= $WWW::FCM::HTTP::V1::OAuth::DEFAULT_GRANT_TYPE
ABresult
1X1
011
000
28 4 ▶ $args{timeout} ||= $DEFAULT_TIMEOUT;
Condition33%1/3
$args{"timeout"} ||= $WWW::FCM::HTTP::V1::OAuth::DEFAULT_TIMEOUT
l!l&&r!l&&!r
040
MC/DC0%0/2
$args{"timeout"} ||= $WWW::FCM::HTTP::V1::OAuth::DEFAULT_TIMEOUT
$args{"timeout"} $WWW::FCM::HTTP::V1::OAuth::DEFAULT_TIMEOUT
Truth table33%1/3
A ||= B
A= $args{"timeout"}B= $WWW::FCM::HTTP::V1::OAuth::DEFAULT_TIMEOUT
ABresult
1X1
011
000
29 4 ▶ $args{scopes} ||= [];
Condition100%2/2
$args{"scopes"} ||= []
l!l
31
MC/DC100%1/1
$args{"scopes"} ||= []
$args{"scopes"}
Truth table100%2/2
A
A= $args{"scopes"}
Aresult
11
00
30 4 ▶ $args{jwt_config} ||= _jwt_config_from_json($args{api_key_json}, $args{scopes});
Condition33%1/3
$args{"jwt_config"} ||= _jwt_config_from_json($args{"api_key_json"}, $args{"scopes"})
l!l&&r!l&&!r
040
MC/DC0%0/2
$args{"jwt_config"} ||= _jwt_config_from_json($args{"api_key_json"}, $args{"scopes"})
$args{"jwt_config"} _jwt_config_from_json($args{"api_key_json"}, $args{"scopes"})
Truth table33%1/3
A ||= B
A= $args{"jwt_config"}B= _jwt_config_from_json($args{"api_key_json"}, $args{"scopes"})
ABresult
1X1
011
000
31 4 ▶ $args{token_url} ||= $args{jwt_config}->{token_url};
Condition33%1/3
$args{"token_url"} ||= $args{"jwt_config"}{"token_url"}
l!l&&r!l&&!r
040
MC/DC0%0/2
$args{"token_url"} ||= $args{"jwt_config"}{"token_url"}
$args{"token_url"} $args{"jwt_config"}{"token_url"}
Truth table33%1/3
A ||= B
A= $args{"token_url"}B= $args{"jwt_config"}{"token_url"}
ABresult
1X1
011
000
32 4 ▶ $args{ua} ||= Furl->new(timeout => $args{timeout});
Condition33%1/3
$args{"ua"} ||= "Furl"->new("timeout", $args{"timeout"})
l!l&&r!l&&!r
040
MC/DC0%0/2
$args{"ua"} ||= "Furl"->new("timeout", $args{"timeout"})
$args{"ua"} "Furl"->new("timeout", $args{"timeout"})
Truth table33%1/3
A ||= B
A= $args{"ua"}B= "Furl"->new("timeout", $args{"timeout"})
ABresult
1X1
011
000
33 4 ▶ $args{cache} ||= undef; # if you will cache the oAuth token, create get/set/delete method
Condition50%1/2
$args{"cache"} ||= undef
l!l
04
MC/DC0%0/1
$args{"cache"} ||= undef
$args{"cache"}
Truth table50%1/2
A
A= $args{"cache"}
Aresult
11
00
34 4 ▶ $args{expires_in} ||= $DEFAULT_EXPIRES_IN; # for cache
Condition33%1/3
$args{"expires_in"} ||= $WWW::FCM::HTTP::V1::OAuth::DEFAULT_EXPIRES_IN
l!l&&r!l&&!r
040
MC/DC0%0/2
$args{"expires_in"} ||= $WWW::FCM::HTTP::V1::OAuth::DEFAULT_EXPIRES_IN
$args{"expires_in"} $WWW::FCM::HTTP::V1::OAuth::DEFAULT_EXPIRES_IN
Truth table33%1/3
A ||= B
A= $args{"expires_in"}B= $WWW::FCM::HTTP::V1::OAuth::DEFAULT_EXPIRES_IN
ABresult
1X1
011
000
35  
36 4 bless \%args, $class;
37 }
38  
39 sub request {
40 4 ▶ my ($self, %args) = @_;
Subroutine request: called CC=5 SCAR=20.3
Pod undocumented
41  
42 4 my $token = $self->get_token(force_refresh => $args{retry});
43  
44 my $res = $self->ua->request(
45 method => $args{method},
46 url => $args{uri},
47 headers => [
48 'Content-Type' => $args{content_type} || 'application/json; UTF-8',
49 'Authorization' => sprintf("Bearer %s", $token->{access_token}),
50 ],
51 3 ▶ content => $args{content} || (),
Condition40%2/5
$args{'content_type'} || 'application/json; UTF-8'
l!l
03
$args{'content'} || ()
l!l&&r!l&&!r
300
MC/DC0%0/1
$args{'content_type'} || 'application/json; UTF-8'
$args{'content_type'}
MC/DC0%0/2
$args{'content'} || ()
$args{'content'} ()
Truth table50%1/2
A
A= $args{'content_type'}
Aresult
11
00
Truth table33%1/3
A || B
A= $args{'content'}B= ()
ABresult
1X1
011
000
52 );
53  
54 3 ▶ if ($res->code == HTTP_UNAUTHORIZED && !$args{retry}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->code == 401 and not $args{"retry"}) 0 3 3
Condition33%1/3
$res->code == 401 and not $args{"retry"}
!ll&&!rl&&r
300
MC/DC0%0/2
$res->code == 401 and not $args{"retry"}
$res->code == 401 not $args{"retry"}
Truth table33%1/3
A and not(B)
A= $res->code == 401B= not $args{"retry"}
ABresult
0X0
100
111
55 0 $args{retry} = 1;
56 0 return $self->request(%args);
57 }
58  
59 3 return $res;
60 }
61  
62 sub _jwt_config_from_json {
63 4 ▶ my ($json, $scopes) = @_;
Subroutine _jwt_config_from_json: called CC=2 SCAR=7.6
64 4 my $secret = decode_json($json);
65  
66 return +{
67 client_email => $secret->{client_email},
68 private_key => $secret->{private_key},
69 private_key_id => $secret->{private_key_id},
70 scopes => $scopes,
71 4 ▶ token_url => $secret->{token_url} || $DEFAULT_TOKEN_URL,
Condition33%1/3
$secret->{'token_url'} || $WWW::FCM::HTTP::V1::OAuth::DEFAULT_TOKEN_URL
l!l&&r!l&&!r
040
MC/DC0%0/2
$secret->{'token_url'} || $WWW::FCM::HTTP::V1::OAuth::DEFAULT_TOKEN_URL
$secret->{'token_url'} $WWW::FCM::HTTP::V1::OAuth::DEFAULT_TOKEN_URL
Truth table33%1/3
A || B
A= $secret->{'token_url'}B= $WWW::FCM::HTTP::V1::OAuth::DEFAULT_TOKEN_URL
ABresult
1X1
011
000
72 };
73 }
74  
75 sub get_token {
76 4 ▶ my ($self, %args) = @_;
Subroutine get_token: called CC=9 SCAR=29.9
Pod undocumented
77  
78 4 my $cache_key = $self->jwt_config->{private_key_id};
79 4 ▶ if ($self->cache) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->cache) 0 4 4
80 0 ▶ if ($args{force_refresh}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'force_refresh'}) { } 0 0 0
81 0 $self->cache->delete($cache_key);
82 } else {
83 0 my $token_cache = $self->cache->get($cache_key);
84 0 ▶ return $token_cache if $token_cache;
Branch: 1 branch
BranchTrueFalseTotal
if $token_cache 0 0 0
85 }
86 }
87  
88 4 my $claims = $self->_construct_claims($self->jwt_config);
89 4 my $jwt = JSON::WebToken->encode($claims, $self->jwt_config->{private_key}, 'RS256');
90 4 my $res = $self->ua->post(
91 $self->token_url,
92 ['Content-Type' => 'application/x-www-form-urlencoded'],
93 ['grant_type' => $self->grant_type, assertion => $jwt],
94 );
95 4 ▶ unless ($res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res->is_success) 1 3 4
96 1 croak sprintf('Failed to get access token. %s [req] %s [res] %s', $res->status_line, $res->request->request_line, $res->content)
97 }
98  
99 3 my $res_content;
100 3 eval {
101 3 $res_content = decode_json($res->content);
102 };
103 3 ▶ if ( my $e = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = $@) 0 3 3
104 0 croak sprintf('decode_json error. %s', $e),
105 }
106  
107 3 ▶ if ($self->cache) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->cache) 0 3 3
108 0 eval {
109 0 ▶ $self->cache->set($cache_key, $res_content, $self->expires_in) if $res_content;
Branch: 1 branch
BranchTrueFalseTotal
if $res_content 0 0 0
110 };
111 0 ▶ if ( my $e = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = $@) 0 0 0
112 0 carp sprintf('Failed to set cache. %s %s', $e),
113 }
114 }
115  
116 3 return $res_content;
117 }
118  
119 sub _construct_claims {
120 4 ▶ my ($self, $config) = @_;
Subroutine _construct_claims: called CC=3 SCAR=11.0
121 4 my $result = +{};
122 4 my $now = time;
123 4 $result->{iss} = $config->{client_email};
124 4 $result->{scope} = join(' ', @{$config->{scopes}});
125 4 $result->{aud} = $config->{token_url};
126 4 $result->{iat} = $now;
127 4 $result->{exp} = $now + $self->expires_in;
128 4 ▶ $result->{sub} = $config->{subject} if defined $config->{subject};
Branch: 1 branch
BranchTrueFalseTotal
if defined $config->{"subject"} 0 4 4
129 # prn is the old name of sub. Keep setting it
130 # to be compatible with legacy OAuth 2.0 providers.
131 4 ▶ $result->{prn} = $config->{subject} if defined $config->{subject};
Branch: 1 branch
BranchTrueFalseTotal
if defined $config->{"subject"} 0 4 4
132  
133 4 return $result;
134 }
135  
136 3 1;
↑ Summary blib/lib/WWW/FCM/HTTP/V1.pm »