| 1 | package GitHub::Apps::Auth; | ||||||||||||||||||||||||||||
| 2 | 4 | ▶ | use 5.008001; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 4 | our $VERSION = "0.02"; | |||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | use Class::Accessor::Lite ( | ||||||||||||||||||||||||||||
| 9 | 4 | rw => [qw/token expires _prefix _suffix/], | |||||||||||||||||||||||||||
| 10 | ro => [qw/_furl private_key app_id installation_id/], | ||||||||||||||||||||||||||||
| 11 | 4 | ▶ | ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 4 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 4 | ▶ | use Crypt::PK::RSA; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 4 | ▶ | use Crypt::JWT qw/encode_jwt/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 4 | ▶ | use Furl; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 4 | ▶ | use JSON qw/decode_json/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 4 | ▶ | 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
| |||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 2 | my $new_self = bless {}, ref $self; | |||||||||||||||||||||||||||
| 30 | 2 | %$new_self = %$self; | |||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 2 | ▶ | $reverse ? | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | $new_self->_prefix($other . $new_self->_prefix) : | ||||||||||||||||||||||||||||
| 34 | $new_self->_suffix($new_self->_suffix . $other); | ||||||||||||||||||||||||||||
| 35 | 2 | return $new_self; | |||||||||||||||||||||||||||
| 36 | }, | ||||||||||||||||||||||||||||
| 37 | 4 | ▶ | "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 | 3 | ▶ | my ($class, %args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=7 SCAR=25.2
Pod
documented
| |||||||||||||||||||||||||||||
| 41 | 3 | ▶ | if (!exists $args{private_key} || !$args{private_key}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
exists $args{"private_key"} and $args{"private_key"}
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"}
| |||||||||||||||||||||||||||||
| 42 | 0 | croak "private_key is required."; | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | 3 | ▶ | if (!exists $args{app_id} || !$args{app_id}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
exists $args{"app_id"} and $args{"app_id"}
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"}
| |||||||||||||||||||||||||||||
| 45 | 0 | croak "app_id is required."; | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | 3 | ▶ | if (!exists $args{installation_id} || !$args{installation_id}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
exists $args{"installation_id"} and $args{"installation_id"}
MC/DC0%0/2
exists $args{"installation_id"} and $args{"installation_id"}
exists $args{"installation_id"}
$args{"installation_id"}
Truth table33%1/3
A and B
A= exists $args{"installation_id"}B= $args{"installation_id"}
| |||||||||||||||||||||||||||||
| 48 | 0 | croak "installation_id is required."; | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 3 | my $pk = Crypt::PK::RSA->new($args{private_key}); | |||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | my $self = { | ||||||||||||||||||||||||||||
| 54 | private_key => $pk, | ||||||||||||||||||||||||||||
| 55 | installation_id => $args{installation_id}, | ||||||||||||||||||||||||||||
| 56 | app_id => $args{app_id}, | ||||||||||||||||||||||||||||
| 57 | 3 | expires => 0, | |||||||||||||||||||||||||||
| 58 | _furl => Furl->new, | ||||||||||||||||||||||||||||
| 59 | _prefix => "", | ||||||||||||||||||||||||||||
| 60 | _suffix => "", | ||||||||||||||||||||||||||||
| 61 | }; | ||||||||||||||||||||||||||||
| 62 | 3 | return bless $self, $class; | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | sub _generate_jwt { | ||||||||||||||||||||||||||||
| 66 | 4 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_generate_jwt: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 4 | my $jwt = encode_jwt( | |||||||||||||||||||||||||||
| 69 | payload => { | ||||||||||||||||||||||||||||
| 70 | iat => time(), | ||||||||||||||||||||||||||||
| 71 | exp => time() + 60, | ||||||||||||||||||||||||||||
| 72 | iss => $self->app_id, | ||||||||||||||||||||||||||||
| 73 | }, | ||||||||||||||||||||||||||||
| 74 | alg => "RS256", | ||||||||||||||||||||||||||||
| 75 | key => $self->private_key, | ||||||||||||||||||||||||||||
| 76 | ); | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 4 | return $jwt; | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | sub _generate_request_header { | ||||||||||||||||||||||||||||
| 82 | 4 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_generate_request_header: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 83 | 4 | my $jwt = $self->_generate_jwt(); | |||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | return [ | ||||||||||||||||||||||||||||
| 86 | 4 | Authorization => 'Bearer ' . $jwt, | |||||||||||||||||||||||||||
| 87 | Accept => "application/vnd.github.machine-man-preview+json", | ||||||||||||||||||||||||||||
| 88 | ]; | ||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | sub _fetch_access_token { | ||||||||||||||||||||||||||||
| 92 | 4 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_fetch_access_token: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 4 | my $installation_id = $self->installation_id; | |||||||||||||||||||||||||||
| 95 | 4 | my $header = $self->_generate_request_header(); | |||||||||||||||||||||||||||
| 96 | 4 | my $resp = $self->_post_to_access_token($installation_id, $header); | |||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 4 | ▶ | if (!$resp->is_success) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | 0 | croak "cannot fetch access_token: ". $resp->content; | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 4 | my $content = decode_json $resp->content; | |||||||||||||||||||||||||||
| 103 | 4 | my $token = $content->{token}; | |||||||||||||||||||||||||||
| 104 | 4 | $self->token($token); | |||||||||||||||||||||||||||
| 105 | 4 | my $expires = $content->{expires_at}; | |||||||||||||||||||||||||||
| 106 | 4 | my $tm = Time::Moment->from_string($expires); | |||||||||||||||||||||||||||
| 107 | 4 | $self->expires($tm->epoch); | |||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 4 | return $self->_prefix . $token . $self->_suffix; | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | sub _post_to_access_token { | ||||||||||||||||||||||||||||
| 113 | 0 | ▶ | my ($self, $installation_id, $header) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_post_to_access_token: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 0 | return $self->_furl->post( | |||||||||||||||||||||||||||
| 116 | "https://api.github.com/app/installations/$installation_id/access_tokens", | ||||||||||||||||||||||||||||
| 117 | $header, | ||||||||||||||||||||||||||||
| 118 | ); | ||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | sub _is_expired_token { | ||||||||||||||||||||||||||||
| 122 | 15 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_is_expired_token: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 15 | return time() > $self->expires; | |||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | sub issued_token { | ||||||||||||||||||||||||||||
| 128 | 15 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
issued_token: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 15 | ▶ | if ($self->_is_expired_token) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | 4 | return $self->_prefix . $self->_fetch_access_token . $self->_suffix; | |||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 11 | return $self->_prefix . $self->token . $self->_suffix; | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 4 | 1; | |||||||||||||||||||||||||||
| 138 | __END__ | ||||||||||||||||||||||||||||