File Coverage

blib/lib/OIDC/Client/AccessTokenBuilder.pm
Criterion Covered Total %
statement 39 39 100.0
branch 21 22 95.4
condition n/a
subroutine 12 12 100.0
pod 2 2 100.0
total 74 75 98.6


line stmt bran cond sub pod time code
1             package OIDC::Client::AccessTokenBuilder;
2              
3 4     4   570859 use utf8;
  4         404  
  4         38  
4 4     4   846 use Moose;
  4         573866  
  4         37  
5 4     4   36370 use Moose::Exporter;
  4         10  
  4         43  
6 4     4   1072 use MooseX::Params::Validate;
  4         78993  
  4         48  
7 4     4   2447 use List::Util qw(first);
  4         11  
  4         426  
8 4     4   391 use OIDC::Client::AccessToken;
  4         19  
  4         194  
9 4     4   2478 use OIDC::Client::Utils qw(get_values_from_space_delimited_string);
  4         17  
  4         18  
10              
11             =encoding utf8
12              
13             =head1 NAME
14              
15             OIDC::Client::AccessTokenBuilder - AccessToken object builder
16              
17             =head1 DESCRIPTION
18              
19             Exports functions that help create an L<OIDC::Client::AccessToken> object.
20              
21             =cut
22              
23             Moose::Exporter->setup_import_methods(as_is => [qw/build_access_token_from_token_response
24             build_access_token_from_claims/]);
25              
26              
27             =head1 FUNCTIONS
28              
29             =head2 build_access_token_from_token_response( $token_response )
30              
31             Builds an L<OIDC::Client::AccessToken> object from an L<OIDC::Client::TokenResponse> object.
32              
33             =cut
34              
35             sub build_access_token_from_token_response {
36 28     28 1 2834 my ($token_response) = pos_validated_list(\@_, { isa => 'OIDC::Client::TokenResponse', optional => 0 });
37              
38 28         6786 my $token_type = $token_response->token_type;
39 28 100       3145 my $expires_at = defined $token_response->expires_in ? _get_time() + $token_response->expires_in
40             : undef;
41 28 100       941 my $scopes = defined $token_response->scope ? get_values_from_space_delimited_string($token_response->scope)
42             : undef;
43              
44 28 100       1041 return OIDC::Client::AccessToken->new(
    100          
    100          
45             token => $token_response->access_token,
46             defined $token_type ? (token_type => $token_type) : (),
47             defined $expires_at ? (expires_at => $expires_at) : (),
48             defined $scopes ? (scopes => $scopes ) : (),
49             );
50             }
51              
52              
53             =head2 build_access_token_from_claims( $claims, $token )
54              
55             Builds an L<OIDC::Client::AccessToken> object from claims (hashref) and token (string).
56              
57             =cut
58              
59             sub build_access_token_from_claims {
60 10     10 1 19809 my ($claims, $token) = pos_validated_list(\@_, { isa => 'HashRef', optional => 0 },
61             { isa => 'Str', optional => 0 });
62              
63 10         4700 my $token_type = $claims->{token_type};
64 10         27 my $expires_at = $claims->{exp};
65 10         39 my $scopes = _get_scopes_from_claims($claims);
66              
67 10 100       514 return OIDC::Client::AccessToken->new(
    100          
    100          
68             token => $token,
69             claims => $claims,
70             defined $token_type ? (token_type => $token_type) : (),
71             defined $expires_at ? (expires_at => $expires_at) : (),
72             defined $scopes ? (scopes => $scopes ) : (),
73             );
74             }
75              
76             sub _get_scopes_from_claims {
77 10     10   58 my ($claims) = pos_validated_list(\@_, { isa => 'HashRef', optional => 0 });
78              
79 10 100   17   2442 my $claim_name = first { defined $claims->{$_} } qw( scp scope )
  17         78  
80             or return;
81              
82 5         27 my $scopes = $claims->{$claim_name};
83              
84 5 100       20 unless (ref $scopes) {
85 1         6 return get_values_from_space_delimited_string($scopes);
86             }
87              
88 4 50       54 return ref $scopes eq 'ARRAY' ? $scopes : [];
89             }
90              
91             # to be mocked in tests
92             sub _get_time {
93 18     18   563 return time();
94             }
95              
96             1;