| 1 | package Business::TrueLayer::Authenticator; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | Business::TrueLayer::Authenticator - Class to handle low level request | ||||||||||||||||||||||||||||||||||||||||||
| 6 | authentication, you probably don't need to use this and should use the | ||||||||||||||||||||||||||||||||||||||||||
| 7 | main L<Business::TrueLayer> module instead. | ||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | 8 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 8 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | 8 | ▶ | use feature qw/ signatures postderef /; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | 8 | ▶ | use Moose; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | 8 | extends 'Business::TrueLayer::Request'; | |||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | 8 | ▶ | no warnings qw/ experimental::signatures experimental::postderef /; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | 8 | ▶ | use Business::TrueLayer::Types; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | 8 | ▶ | use Try::Tiny::SmartCatch; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 23 | 8 | ▶ | use Mojo::UserAgent; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 24 | 8 | ▶ | use Carp qw/ croak /; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 25 | 8 | ▶ | use JSON; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||
| 27 | has 'scope' => ( | ||||||||||||||||||||||||||||||||||||||||||
| 28 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||||||
| 29 | isa => 'ArrayRef', | ||||||||||||||||||||||||||||||||||||||||||
| 30 | required => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 31 | 2 | ▶ | default => sub { [ qw/ payments / ] }, | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 32 | 8 | ); | |||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||
| 34 | 8 | has [ qw/ _auth_token _token_type _refresh_token / ] => ( | |||||||||||||||||||||||||||||||||||||||||
| 35 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||||||
| 36 | isa => 'Str', | ||||||||||||||||||||||||||||||||||||||||||
| 37 | required => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 38 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||
| 40 | has [ qw/ _expires_at / ] => ( | ||||||||||||||||||||||||||||||||||||||||||
| 41 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||||||
| 42 | isa => 'Int', | ||||||||||||||||||||||||||||||||||||||||||
| 43 | required => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 44 | 16 | ▶ | default => sub { time }, | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 45 | 8 | ); | |||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||
| 47 | 1 | ▶ | sub access_token ( $self ) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
access_token: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||
| 49 | 1 | return $self | |||||||||||||||||||||||||||||||||||||||||
| 50 | ->_authenticate | ||||||||||||||||||||||||||||||||||||||||||
| 51 | ->_auth_token | ||||||||||||||||||||||||||||||||||||||||||
| 52 | ; | ||||||||||||||||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||
| 55 | 16 | ▶ | sub _authenticate ( $self ) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_authenticate: called CC=9 SCAR=22.3
| |||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||
| 57 | 16 | ▶ | if ( | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
$self->_auth_token and $self->_token_type and not $self->_token_is_expired
$self->_auth_token and $self->_token_type
MC/DC33%1/3
$self->_auth_token and $self->_token_type and not $self->_token_is_expired
$self->_auth_token
$self->_token_type
not $self->_token_is_expired
Truth table50%2/4
A and B and not(C)
A= $self->_auth_tokenB= $self->_token_typeC= not $self->_token_is_expired
| |||||||||||||||||||||||||||||||||||||||||||
| 58 | $self->_auth_token | ||||||||||||||||||||||||||||||||||||||||||
| 59 | && $self->_token_type | ||||||||||||||||||||||||||||||||||||||||||
| 60 | && ! $self->_token_is_expired | ||||||||||||||||||||||||||||||||||||||||||
| 61 | ) { | ||||||||||||||||||||||||||||||||||||||||||
| 62 | 1 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||
| 65 | 15 | my $url = "https://" . $self->host . "/connect/token"; | |||||||||||||||||||||||||||||||||||||||||
| 66 | 15 | my $json = JSON->new->utf8->canonical->encode( | |||||||||||||||||||||||||||||||||||||||||
| 67 | { | ||||||||||||||||||||||||||||||||||||||||||
| 68 | grant_type => 'client_credentials', | ||||||||||||||||||||||||||||||||||||||||||
| 69 | client_id => $self->client_id, | ||||||||||||||||||||||||||||||||||||||||||
| 70 | client_secret => $self->client_secret, | ||||||||||||||||||||||||||||||||||||||||||
| 71 | scope => join( " ",$self->scope->@* ), | ||||||||||||||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||||||||||||||
| 73 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||
| 75 | 15 | my $res_content = $self->_ua_request( $url, $json ); | |||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||
| 77 | # If any of these are missing, we get "interesting" errors from Moose | ||||||||||||||||||||||||||||||||||||||||||
| 78 | # constraint violations. | ||||||||||||||||||||||||||||||||||||||||||
| 79 | 1 | for my $key ( qw/ access_token expires_in token_type refresh_token / ) { | |||||||||||||||||||||||||||||||||||||||||
| 80 | 4 | my $val = $res_content->{ $key }; | |||||||||||||||||||||||||||||||||||||||||
| 81 | 4 | ▶ | if ( !length $val ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 82 | # refresh_token is optional | ||||||||||||||||||||||||||||||||||||||||||
| 83 | next | ||||||||||||||||||||||||||||||||||||||||||
| 84 | 1 | ▶ | if $key eq 'refresh_token'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 85 | croak( "TrueLayer POST $url missing key $key - we have " | ||||||||||||||||||||||||||||||||||||||||||
| 86 | 0 | . join( ', ', map { "'$_'" } sort keys %$res_content ) ); | |||||||||||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||||||||||||
| 88 | 3 | ▶ | if( $key eq 'expires_in' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 89 | 1 | $self->_expires_at(time + $val); | |||||||||||||||||||||||||||||||||||||||||
| 90 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 91 | 2 | ▶ | my $method = $key eq 'access_token' ? '_auth_token' : "_$key"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 92 | 2 | $self->$method( $val ); | |||||||||||||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||
| 96 | 1 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||
| 99 | 2 | ▶ | sub _token_is_expired ( $self ) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_token_is_expired: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 100 | 2 | return time >= $self->_expires_at; | |||||||||||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||
| 103 | 8 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||
| 105 | # vim: ts=4:sw=4:et | ||||||||||||||||||||||||||||||||||||||||||