File Coverage

blib/lib/PayProp/API/Public/Client/Authorization/ClientCredentials.pm
Criterion Covered Total %
statement 29 29 100.0
branch 2 2 100.0
condition 3 6 50.0
subroutine 9 9 100.0
pod n/a
total 43 46 93.4


line stmt bran cond sub pod time code
1             package PayProp::API::Public::Client::Authorization::ClientCredentials;
2              
3 3     3   423315 use strict;
  3         7  
  3         104  
4 3     3   13 use warnings;
  3         5  
  3         166  
5              
6 3     3   15 use Mouse;
  3         9  
  3         22  
7             extends qw/ PayProp::API::Public::Client::Authorization::Base /;
8             with qw/ PayProp::API::Public::Client::Role::Request /;
9             with qw/ PayProp::API::Public::Client::Role::Attribute::Domain /;
10              
11 3     3   1555 use MIME::Base64 qw//;
  3         4  
  3         59  
12 3     3   1646 use PayProp::API::Public::Client::Exception::Authorization;
  3         10  
  3         1274  
13              
14             has [ qw/ client secret application_user_id / ] => (
15             is => 'rw',
16             isa => 'Str',
17             required => 1,
18             );
19              
20             has '+token_type' => ( default => sub { 'Bearer' } );
21             has '+url' => ( default => sub { shift->abs_domain . '/api/oauth/access_token' } );
22             has '+storage_key' => ( default => sub { join( '|', 'ClientCredentials', $_[0]->client, $_[0]->application_user_id ) } );
23              
24             has _encoded_key => (
25             is => 'rw',
26             isa => 'Str',
27             lazy => 1,
28             builder => '_build_encoded_key',
29             );
30              
31             sub _token_request_p {
32 2     2   9624 my ( $self ) = @_;
33              
34             return $self
35             ->post_req_p({
36             headers => { Authorization => 'Basic ' . $self->_encoded_key },
37             params => {
38             grant_type => 'client_credentials',
39             application_user_id => $self->application_user_id,
40             },
41             })
42             ->then( sub {
43 2     2   31077 my ( $Transaction ) = @_;
44              
45 2         22 my $Result = $Transaction->result;
46              
47 2   50     65 my $json = $Result->json // {};
48 2         171 my $access_token = $json->{access_token};
49              
50             PayProp::API::Public::Client::Exception::Authorization->throw(
51             status_code => $Result->code,
52             errors => [
53             {
54             code => $json->{error} // 'NO_ERROR_CODE',
55 2 100 50     16 message => $json->{error_description} // 'NO_ERROR_MESSAGE',
      50        
56             },
57             ],
58             )
59             unless $access_token
60             ;
61              
62             return {
63 1         44 token => $access_token,
64             token_type => $self->token_type,
65             };
66             } )
67 2         66 ;
68             }
69              
70             sub _build_encoded_key {
71 2     2   3982 my ( $self ) = @_;
72              
73 2         82 my $encoded_key = MIME::Base64::encode_base64( $self->client . ':' . $self->secret );
74 2         10 chomp $encoded_key;
75              
76 2         28 return $encoded_key;
77             }
78              
79             sub _query_params {
80 2     2   2728 my ( $self ) = @_;
81              
82 2         20 return [qw/ grant_type application_user_id /];
83             }
84              
85             __PACKAGE__->meta->make_immutable;
86              
87             __END__