File Coverage

blib/lib/OIDC/Client/ApiUserAgentBuilder.pm
Criterion Covered Total %
statement 29 29 100.0
branch n/a
condition 2 3 66.6
subroutine 10 10 100.0
pod 3 3 100.0
total 44 45 97.7


line stmt bran cond sub pod time code
1             package OIDC::Client::ApiUserAgentBuilder;
2              
3 6     6   492522 use utf8;
  6         319  
  6         55  
4 6     6   760 use Moose;
  6         467533  
  6         52  
5 6     6   50823 use Moose::Exporter;
  6         17  
  6         37  
6 6     6   1221 use MooseX::Params::Validate;
  6         117398  
  6         115  
7 6     6   7061 use Mojo::UserAgent;
  6         2271956  
  6         125  
8 6     6   4756 use Readonly;
  6         32999  
  6         3023  
9              
10             =encoding utf8
11              
12             =head1 NAME
13              
14             OIDC::Client::ApiUserAgentBuilder - API user agent builder
15              
16             =head1 DESCRIPTION
17              
18             Exports functions that help create an API user agent (L<Mojo::UserAgent> object).
19             For each request, the user agent includes the access token in the C<Authorization> header.
20              
21             =cut
22              
23             Readonly my $DEFAULT_TOKEN_TYPE => 'Bearer';
24              
25             Moose::Exporter->setup_import_methods(as_is => [qw/build_api_useragent_from_token_response
26             build_api_useragent_from_access_token
27             build_api_useragent_from_token_value
28             /]);
29              
30              
31             =head1 FUNCTIONS
32              
33             =head2 build_api_useragent_from_token_response( $token_response )
34              
35             Builds a L<Mojo::UserAgent> object from an L<OIDC::Client::TokenResponse> object.
36              
37             =cut
38              
39             sub build_api_useragent_from_token_response {
40 3     3 1 41 my ($token_response) = pos_validated_list(\@_, { isa => 'OIDC::Client::TokenResponse', optional => 0 });
41              
42 3         1374 return build_api_useragent_from_token_value($token_response->access_token,
43             $token_response->token_type);
44             }
45              
46              
47             =head2 build_api_useragent_from_access_token( $access_token )
48              
49             Builds a L<Mojo::UserAgent> object from an L<OIDC::Client::AccessToken> object.
50              
51             =cut
52              
53             sub build_api_useragent_from_access_token {
54 6     6 1 75 my ($access_token) = pos_validated_list(\@_, { isa => 'OIDC::Client::AccessToken', optional => 0 });
55              
56 6         2553 return build_api_useragent_from_token_value($access_token->token,
57             $access_token->token_type);
58             }
59              
60              
61             =head2 build_api_useragent_from_token_value( $token_value, $token_type )
62              
63             Builds a L<Mojo::UserAgent> object from a token value (string).
64              
65             =cut
66              
67             sub build_api_useragent_from_token_value {
68 12     12 1 20258 my ($token_value, $token_type) = pos_validated_list(\@_, { isa => 'Str', optional => 0 },
69             { isa => 'Maybe[Str]', optional => 1 });
70 12   66     5968 $token_type ||= $DEFAULT_TOKEN_TYPE;
71              
72 12         185 my $ua = Mojo::UserAgent->new();
73              
74             $ua->on(start => sub {
75 6     6   11431 my ($ua, $tx) = @_;
76 6         22 $tx->req->headers->authorization("$token_type $token_value");
77 12         261 });
78              
79 12         391 return $ua;
80             }
81              
82             1;