blib/lib/GitHub/Apps/Auth.pm

filter: Statementstmt 82.4% 80 / 97 Branchbran 60.0% 12 / 20 Conditioncond 44.4% 4 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 86.3% 19 / 22 Podpod 66.6% 2 / 3 totaltotal 74.5% 117 / 157 CC 14 SCAR 15.4
CC
14
Cov
76%
new
12.8
installations
6.0
__ANON__
3.0
CRAP 4.7
↑ Summary Build »
1 package GitHub::Apps::Auth;
2 5 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 5 our $VERSION = "0.03";
7  
8 use Class::Accessor::Lite (
9 5 rw => [qw/token expires _prefix _suffix installation_id/],
10 ro => [qw/_furl private_key app_id/],
11 5 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 5 ▶use Crypt::PK::RSA;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 5 ▶use Crypt::JWT qw/encode_jwt/;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 5 ▶use Furl;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 5 ▶use JSON qw/decode_json/;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 5 ▶use Time::Moment;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 use overload
21 0 ▶ "\"\"" => sub { shift->issued_token },
Subroutine __ANON__: not called CC=1 SCAR=6.9
22 "." => sub {
23 2 ▶ my $self = shift;
Subroutine __ANON__: called CC=3 SCAR=11.0
24 2 my $other = shift;
25 2 my $reverse = shift;
26  
27 2 ▶ $other = "" unless defined $other;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $other 0 2 2
28  
29 2 my $new_self = bless {}, ref $self;
30 2 %$new_self = %$self;
31  
32 2 ▶ $reverse ?
Branch: 1 branch
BranchTrueFalseTotal
$reverse ? : 1 1 2
33 $new_self->_prefix($other . $new_self->_prefix) :
34 $new_self->_suffix($new_self->_suffix . $other);
35 2 return $new_self;
36 },
37 5 ▶ "eq" => sub { shift->issued_token eq shift };
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
38  
39 sub new {
40 4 ▶ my ($class, %args) = @_;
Subroutine new: called CC=9 SCAR=25.5
Pod documented
41 4 ▶ if (!exists $args{private_key} || !$args{private_key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $args{"private_key"} and $args{"private_key"}) 0 4 4
Condition33%1/3
exists $args{"private_key"} and $args{"private_key"}
!ll&&!rl&&r
004
MC/DC0%0/2
exists $args{"private_key"} and $args{"private_key"}
exists $args{"private_key"} $args{"private_key"}
Truth table33%1/3
A and B
A= exists $args{"private_key"}B= $args{"private_key"}
ABresult
0X0
100
111
42 0 croak "private_key is required.";
43 }
44 4 ▶ if (!exists $args{app_id} || !$args{app_id}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $args{"app_id"} and $args{"app_id"}) 0 4 4
Condition33%1/3
exists $args{"app_id"} and $args{"app_id"}
!ll&&!rl&&r
004
MC/DC0%0/2
exists $args{"app_id"} and $args{"app_id"}
exists $args{"app_id"} $args{"app_id"}
Truth table33%1/3
A and B
A= exists $args{"app_id"}B= $args{"app_id"}
ABresult
0X0
100
111
45 0 croak "app_id is required.";
46 }
47 4 ▶ if (!$args{installation_id} && !$args{login}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args{"installation_id"} or $args{"login"}) 0 4 4
Condition66%2/3
$args{"installation_id"} or $args{"login"}
l!l&&r!l&&!r
310
MC/DC0%0/2
$args{"installation_id"} or $args{"login"}
$args{"installation_id"} $args{"login"}
Truth table66%2/3
A or B
A= $args{"installation_id"}B= $args{"login"}
ABresult
1X1
011
000
48 0 croak "must be set installation_id or login.";
49 }
50  
51 4 my $pk = Crypt::PK::RSA->new($args{private_key});
52  
53 my $klass = {
54 private_key => $pk,
55 installation_id => $args{installation_id},
56 app_id => $args{app_id},
57 4 expires => 0,
58 _furl => Furl->new,
59 _prefix => "",
60 _suffix => "",
61 };
62 4 my $self = bless $klass, $class;
63  
64 4 ▶ if (!$self->installation_id) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->installation_id) 1 3 4
65 1 my $installations = $self->installations;
66 1 ▶ if (!exists $installations->{$args{login}}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $$installations{$args{"login"}}) 0 1 1
67 0 croak $args{login} . " is not found in installations."
68 }
69 1 my $installation_id = $installations->{$args{login}};
70 1 $self->installation_id($installation_id);
71 }
72  
73 4 return $self;
74 }
75  
76 sub installations {
77 0 ▶ my $self = shift;
Subroutine installations: not called CC=2 SCAR=17.9
Pod undocumented
78  
79 0 my $header = $self->_generate_request_header();
80  
81 0 my $resp = $self->_furl->get(
82 "https://api.github.com/app/installations",
83 $header,
84 );
85 0 ▶ if (!$resp->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($resp->is_success) 0 0 0
86 0 croak "fail to fetch installations: ". $resp->content;
87 }
88  
89 0 my $content = decode_json $resp->content;
90 0 my %ids_by_account = map { $_->{account}{login} => $_->{id} } @$content;
91 0 return \%ids_by_account;
92 }
93  
94 sub _generate_jwt {
95 4 ▶ my $self = shift;
Subroutine _generate_jwt: called CC=1 SCAR=0.0
96  
97 4 my $jwt = encode_jwt(
98 payload => {
99 iat => time(),
100 exp => time() + 60,
101 iss => $self->app_id,
102 },
103 alg => "RS256",
104 key => $self->private_key,
105 );
106  
107 4 return $jwt;
108 }
109  
110 sub _generate_request_header {
111 4 ▶ my $self = shift;
Subroutine _generate_request_header: called CC=1 SCAR=0.0
112 4 my $jwt = $self->_generate_jwt();
113  
114 return [
115 4 Authorization => 'Bearer ' . $jwt,
116 Accept => "application/vnd.github.machine-man-preview+json",
117 ];
118 }
119  
120 sub _fetch_access_token {
121 4 ▶ my $self = shift;
Subroutine _fetch_access_token: called CC=2 SCAR=7.0
122  
123 4 my $installation_id = $self->installation_id;
124 4 my $header = $self->_generate_request_header();
125 4 my $resp = $self->_post_to_access_token($installation_id, $header);
126  
127 4 ▶ if (!$resp->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($resp->is_success) 0 4 4
128 0 croak "cannot fetch access_token: ". $resp->content;
129 }
130  
131 4 my $content = decode_json $resp->content;
132 4 my $token = $content->{token};
133 4 $self->token($token);
134 4 my $expires = $content->{expires_at};
135 4 my $tm = Time::Moment->from_string($expires);
136 4 $self->expires($tm->epoch);
137  
138 4 return $self->_prefix . $token . $self->_suffix;
139 }
140  
141 sub _post_to_access_token {
142 0 ▶ my ($self, $installation_id, $header) = @_;
Subroutine _post_to_access_token: not called CC=1 SCAR=6.9
143  
144 0 return $self->_furl->post(
145 "https://api.github.com/app/installations/$installation_id/access_tokens",
146 $header,
147 );
148 }
149  
150 sub _is_expired_token {
151 15 ▶ my $self = shift;
Subroutine _is_expired_token: called CC=1 SCAR=0.0
152  
153 15 return time() > $self->expires;
154 }
155  
156 sub issued_token {
157 15 ▶ my $self = shift;
Subroutine issued_token: called CC=2 SCAR=6.9
Pod documented
158  
159 15 ▶ if ($self->_is_expired_token) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_expired_token) 4 11 15
160 4 return $self->_prefix . $self->_fetch_access_token . $self->_suffix;
161 }
162  
163 11 return $self->_prefix . $self->token . $self->_suffix;
164 }
165  
166 5 1;
167 __END__
↑ Summary Build »