Client/AccessToken.pm

filter: Statementstmt 100% 61 / 61 Branchbran 100% 8 / 8 Conditioncond 100% 4 / 4 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 15 / 15 Podpod 100% 5 / 5 totaltotal 100% 95 / 95 CC 7 SCAR 5.7
CC
7
Cov
100%
has_expired
3.0
verify_at_hash
3.0
has_scope
2.0
CRAP 1.8
« Client/Identity.pm ↑ Summary Client/User.pm »
1 package OIDC::Client::AccessToken;
2 9 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 9 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use MooseX::Params::Validate;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 9 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 9 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 9 ▶use Digest::SHA qw(sha256 sha384 sha512);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 9 ▶use MIME::Base64 qw(encode_base64url);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 9 ▶use List::Util qw(any);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 9 ▶use OIDC::Client::Error::TokenValidation;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 =encoding utf8
14  
15 =head1 NAME
16  
17 OIDC::Client::AccessToken - Access Token class
18  
19 =head1 DESCRIPTION
20  
21 Class representing an access token
22  
23 =head1 ATTRIBUTES
24  
25 =head2 token
26  
27 The string of the access token. Required
28  
29 =head2 token_type
30  
31 The type of the access token
32  
33 =head2 expires_at
34  
35 The expiration time of the access token (number of seconds since 1970-01-01T00:00:00Z)
36  
37 =head2 scopes
38  
39 The scopes (arrayref) of the access token
40  
41 =head2 claims
42  
43 Hashref of claims coming from the access token. Optional, as an access token
44 is not always decoded, depending on the nature of the application.
45  
46 =cut
47  
48 9 has 'token' => (
49 is => 'ro',
50 isa => 'Str',
51 required => 1,
52 );
53  
54 9 has 'token_type' => (
55 is => 'ro',
56 isa => 'Maybe[Str]',
57 required => 0,
58 );
59  
60 9 has 'expires_at' => (
61 is => 'ro',
62 isa => 'Maybe[Int]',
63 required => 0,
64 );
65  
66 9 has 'scopes' => (
67 is => 'ro',
68 isa => 'Maybe[ArrayRef[Str]]',
69 required => 0,
70 predicate => 'has_scopes',
71 );
72  
73 9 has 'claims' => (
74 is => 'ro',
75 isa => 'Maybe[HashRef]',
76 required => 0,
77 );
78  
79 =head1 METHODS
80  
81 =head2 has_scope( $expected_scope )
82  
83 my $has_scope = $access_token->has_scope($expected_scope);
84  
85 Returns whether a scope is present in the scopes of the access token.
86  
87 =cut
88  
89 4 ▶sub has_scope {
Subroutine has_scope: called CC=2 SCAR=6.9
Pod documented
90 4 my $self = shift;
91 4 my ($expected_scope) = pos_validated_list(\@_, { isa => 'Str', optional => 0 });
92  
93 4 ▶ return any { $_ eq $expected_scope } @{$self->scopes // []};
Subroutine __ANON__: called CC=1 SCAR=0.0
Condition100%2/2
$self->scopes // []
l!l
31
MC/DC100%1/1
$self->scopes // []
$self->scopes
Truth table100%2/2
A
A= $self->scopes
Aresult
11
00
94 }
95  
96  
97 =head2 has_expired( $leeway )
98  
99 my $has_expired = $access_token->has_expired($leeway);
100  
101 Returns whether the access token has expired.
102  
103 Returns undef if the C<expires_at> attribute is not defined.
104  
105 The list parameters are:
106  
107 =over 2
108  
109 =item leeway
110  
111 Number of seconds of leeway for the token to be considered expired before it actually is.
112  
113 =back
114  
115 =cut
116  
117 17 ▶sub has_expired {
Subroutine has_expired: called CC=3 SCAR=11.0
Pod documented
118 17 my $self = shift;
119 17 my ($leeway) = pos_validated_list(\@_, { isa => 'Maybe[Int]', optional => 1 });
120 17 ▶ $leeway //= 0;
Condition100%2/2
$leeway //= 0
l!l
314
MC/DC100%1/1
$leeway //= 0
$leeway
Truth table100%2/2
A
A= $leeway
Aresult
11
00
121  
122 17 ▶ return unless defined $self->expires_at;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->expires_at 1 16 17
123  
124 16 return ( $self->expires_at - $leeway ) < time;
125 }
126  
127  
128 =head2 compute_at_hash( $alg )
129  
130 my $at_hash = $access_token->compute_at_hash($alg);
131  
132 Returns the computed C<at_hash> for access token. The C<at_hash> is created by
133 hashing the access token using the algorithm specified in the I<$alg> parameter,
134 taking the left-most half of the hash, and then base64url encoding it.
135  
136 =cut
137  
138 8 ▶sub compute_at_hash {
Subroutine compute_at_hash: called CC=2 SCAR=6.9
Pod documented
139 8 my $self = shift;
140 8 my ($alg) = pos_validated_list(\@_, { isa => 'Str', optional => 0 });
141  
142 my $digest_func = {
143 HS256 => \&sha256, RS256 => \&sha256, PS256 => \&sha256, ES256 => \&sha256,
144 HS384 => \&sha384, RS384 => \&sha384, PS384 => \&sha384, ES384 => \&sha384,
145 HS512 => \&sha512, RS512 => \&sha512, PS512 => \&sha512, ES512 => \&sha512,
146 8 ▶ }->{$alg} or croak("OIDC: unsupported signing algorithm: $alg");
Branch: 1 branch
BranchTrueFalseTotal
my $digest_func = {"HS256", \&OIDC::Client::AccessToken::sha256, "RS256", \&OIDC::Client::AccessToken::sha256, "PS256", \&OIDC::Client::AccessToken::sha256, "ES256", \&OIDC::Client::AccessToken::sha256, "HS384", \&OIDC::Client::AccessToken::sha384, "RS384", \&OIDC::Client::AccessToken::sha384, "PS384", \&OIDC::Client::AccessToken::sha384, "ES384", \&OIDC::Client::AccessToken::sha384, "HS512", \&OIDC::Client::AccessToken::sha512, "RS512", \&OIDC::Client::AccessToken::sha512, "PS512", \&OIDC::Client::AccessToken::sha512, "ES512", \&OIDC::Client::AccessToken::sha512}->{$alg} or croak("OIDC: unsupported signing algorithm: $alg") 7 1 8
147  
148 7 my $digest = $digest_func->($self->token);
149 7 my $left_half = substr($digest, 0, length($digest)/2);
150 7 return encode_base64url($left_half);
151 }
152  
153  
154 =head2 verify_at_hash( $expected_at_hash, $alg )
155  
156 $access_token->verify_at_hash($expected_at_hash, $alg);
157  
158 If the value of the I<$expected_at_hash> parameter is undefined, returns a true value.
159 Throws an L<OIDC::Client::Error::TokenValidation> exception if the computed C<at_hash> for access
160 token and specified I<$alg> algorithm does not match the value of the I<$expected_at_hash> parameter.
161 Otherwise, returns a true value.
162  
163 =cut
164  
165 8 ▶sub verify_at_hash {
Subroutine verify_at_hash: called CC=3 SCAR=11.0
Pod documented
166 8 my $self = shift;
167 8 my ($expected_at_hash, $alg) = pos_validated_list(\@_, { isa => 'Maybe[Str]', optional => 0 },
168 { isa => 'Str', optional => 0 });
169 8 ▶ return 1 unless defined $expected_at_hash;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $expected_at_hash 4 4 8
170  
171 4 my $at_hash = $self->compute_at_hash($alg);
172  
173 4 ▶ $at_hash eq $expected_at_hash
Branch: 1 branch
BranchTrueFalseTotal
$at_hash eq $expected_at_hash or "OIDC::Client::Error::TokenValidation"->throw("OIDC: unexpected at_hash") 1 3 4
174 or OIDC::Client::Error::TokenValidation->throw("OIDC: unexpected at_hash");
175  
176 1 return 1;
177 }
178  
179  
180 =head2 to_hashref()
181  
182 my $access_token_href = $access_token->to_hashref();
183  
184 Returns a hashref of the access token data.
185  
186 =cut
187  
188 36 ▶sub to_hashref {
Subroutine to_hashref: called CC=1 SCAR=0.0
Pod documented
189 36 my $self = shift;
190  
191 return {
192 113 map { $_ => $self->$_ }
193 180 grep { defined $self->$_ }
194 36 map { $_->name } $self->meta->get_all_attributes
195 };
196 }
197  
198  
199 9 __PACKAGE__->meta->make_immutable;
200  
201 9 1;
« Client/Identity.pm ↑ Summary Client/User.pm »