Client/Plugin.pm

filter: Statementstmt 99.7% 331 / 332 Branchbran 90.0% 135 / 150 Conditioncond 72.5% 29 / 40 MC/DCmcdc 45.8% 11 / 24 Subroutinesub 100% 56 / 56 Podpod 100% 20 / 20 totaltotal 93.5% 582 / 622 CC 91 SCAR 17.4
CC
91
Cov
95%
get_token
13.0
refresh_token
12.0
get_valid_access_token
11.1
CRAP 5.7
« Client.pm ↑ Summary Client/Role/ClientAuthenticationHelper.pm »
1 package OIDC::Client::Plugin;
2 4 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use MooseX::Params::Validate;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 4 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use Module::Load qw(load);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use Mojo::URL;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use CHI;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 ▶use OIDC::Client::AccessToken;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 4 ▶use OIDC::Client::AccessTokenBuilder qw(build_access_token_from_token_response
Subroutine BEGIN: called CC=1 SCAR=0.0
16 4 build_access_token_from_claims);
17 4 ▶use OIDC::Client::ApiUserAgentBuilder qw(build_api_useragent_from_access_token);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 4 ▶use OIDC::Client::Utils ();
Subroutine BEGIN: called CC=1 SCAR=0.0
19 4 ▶use OIDC::Client::Identity;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 4 ▶use OIDC::Client::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 4 ▶use OIDC::Client::Error::Authentication;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 4 ▶use OIDC::Client::Error::Provider;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 4 with 'OIDC::Client::Role::LoggerWrapper';
25  
26 =encoding utf8
27  
28 =head1 NAME
29  
30 OIDC::Client::Plugin - Main module for the plugins
31  
32 =head1 DESCRIPTION
33  
34 Main module instanciated for the current request by an application plugin
35 (for example: L<Mojolicious::Plugin::OIDC>).
36  
37 It contains all the methods available in the application.
38  
39 =cut
40  
41 4 enum 'RedirectType' => [qw/login logout/];
42  
43 4 has 'request_params' => (
44 is => 'ro',
45 isa => 'HashRef',
46 required => 1,
47 );
48  
49 4 has 'request_headers' => (
50 is => 'ro',
51 isa => 'HashRef',
52 required => 1,
53 );
54  
55 4 has 'session' => (
56 is => 'ro',
57 isa => 'HashRef',
58 required => 1,
59 traits => ['OIDC::Client::Trait::HashStore'],
60 );
61  
62 4 has 'stash' => (
63 is => 'ro',
64 isa => 'HashRef',
65 required => 1,
66 traits => ['OIDC::Client::Trait::HashStore'],
67 );
68  
69 4 has 'redirect' => (
70 is => 'ro',
71 isa => 'CodeRef',
72 required => 1,
73 );
74  
75 4 has 'client' => (
76 is => 'ro',
77 isa => 'OIDC::Client',
78 required => 1,
79 );
80  
81 has 'base_url' => (
82 is => 'ro',
83 97 ▶ isa => subtype(as 'Str', where { /^http/ || /^$/ }),
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
/^http/u or /^$/u 97 0 97
84 required => 1,
85 );
86  
87 4 has 'current_url' => (
88 is => 'ro',
89 isa => 'Str',
90 required => 1,
91 );
92  
93 4 has 'login_redirect_uri' => (
94 is => 'ro',
95 isa => 'Maybe[Str]',
96 lazy => 1,
97 builder => '_build_login_redirect_uri',
98 );
99  
100 4 has 'logout_redirect_uri' => (
101 is => 'ro',
102 isa => 'Maybe[Str]',
103 lazy => 1,
104 builder => '_build_logout_redirect_uri',
105 );
106  
107 4 has 'is_base_url_local' => (
108 is => 'ro',
109 isa => 'Bool',
110 lazy => 1,
111 builder => '_build_is_base_url_local',
112 );
113  
114 4 has 'audience_cache' => (
115 is => 'ro',
116 isa => 'CHI::Driver',
117 lazy => 1,
118 builder => '_build_audience_cache',
119 );
120  
121 has 'after_touching_session' => (
122 is => 'ro',
123 isa => 'CodeRef',
124 94 ▶ default => sub { sub {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
125 4 );
126  
127 70 ▶sub _build_is_base_url_local { return shift->base_url =~ m[^https?://localhost\b] }
Subroutine _build_is_base_url_local: called CC=1 SCAR=0.0
128 8 ▶sub _build_login_redirect_uri { return shift->_build_redirect_uri_from_path('login') }
Subroutine _build_login_redirect_uri: called CC=1 SCAR=0.0
129 2 ▶sub _build_logout_redirect_uri { return shift->_build_redirect_uri_from_path('logout') }
Subroutine _build_logout_redirect_uri: called CC=1 SCAR=0.0
130  
131 10 ▶sub _build_redirect_uri_from_path {
Subroutine _build_redirect_uri_from_path: called CC=3 SCAR=11.0
132 10 my $self = shift;
133 10 my ($redirect_type) = pos_validated_list(\@_, { isa => 'RedirectType', optional => 0 });
134  
135 10 ▶ my $redirect_path = $redirect_type eq 'login' ? $self->client->signin_redirect_path
Branch: 2 branches
BranchTrueFalseTotal
my $redirect_path = $redirect_type eq "login" ? $self->client->signin_redirect_path : $self->client->logout_redirect_path or return 6 4 10
$redirect_type eq 'login' ? : 8 2 10
136 : $self->client->logout_redirect_path
137 or return;
138  
139 6 my $base = Mojo::URL->new($self->base_url);
140  
141 6 return Mojo::URL->new($redirect_path)->base($base)->to_abs()->to_string();
142 }
143  
144 2 ▶sub _build_audience_cache {
Subroutine _build_audience_cache: called CC=1 SCAR=0.0
145 2 my $self = shift;
146  
147 2 my $cache_config = $self->client->cache_config;
148 2 my $provider = $self->client->provider;
149  
150 2 return CHI->new(%$cache_config, namespace => "OIDC-${provider}-Audience");
151 }
152  
153 68 ▶after write_session => sub { shift->after_touching_session->() };
Subroutine __ANON__: called CC=1 SCAR=0.0
154 12 ▶after delete_session => sub { shift->after_touching_session->() };
Subroutine __ANON__: called CC=1 SCAR=0.0
155  
156 =head1 METHODS
157  
158 =head2 redirect_to_authorize( %args )
159  
160 $c->oidc->redirect_to_authorize();
161  
162 Redirect the browser to the authorize URL to initiate an authorization code flow.
163  
164 The C<state> parameter contains a generated random string but other data can be added.
165  
166 Another random string is generated and included in the C<nonce> parameter. This helps
167 prevent replay attacks by binding the resulting ID token to the user's session.
168  
169 When PKCE is enabled, a code verifier/challenge pair is generated. The verifier is
170 stored in the session, and the challenge/challenge methode are included as parameters
171 in the authorization request.
172  
173 The optional hash parameters are:
174  
175 =over 2
176  
177 =item target_url
178  
179 Specifies the URL to redirect to at the end of the authorization code flow.
180 Default to the current URL (attribute C<current_url>).
181  
182 =item redirect_uri
183  
184 Specifies the URL that the provider uses to redirect the user's browser back to
185 the application after the user has been authenticated.
186 Default to the URL built from the C<signin_redirect_path> configuration entry.
187  
188 =item extra_params
189  
190 Hashref which can be used to send extra query parameters.
191  
192 =item other_state_params
193  
194 List (arrayref) of strings to add before the auto-generated random string to build
195 the C<state> parameter. All these strings are separated by a comma.
196  
197 =back
198  
199 =cut
200  
201 5 ▶sub redirect_to_authorize {
Subroutine redirect_to_authorize: called CC=8 SCAR=20.8
Pod documented
202 5 my $self = shift;
203 5 my %params = validated_hash(
204 \@_,
205 target_url => { isa => 'Str', optional => 1 },
206 redirect_uri => { isa => 'Str', optional => 1 },
207 extra_params => { isa => 'HashRef', optional => 1 },
208 other_state_params => { isa => 'ArrayRef[Str]', optional => 1 },
209 );
210  
211 5 my $nonce = OIDC::Client::Utils::generate_nonce();
212 5 ▶ my $state = join ',', (@{$params{other_state_params} || []}, OIDC::Client::Utils::generate_state());
Condition100%2/2
$params{'other_state_params'} || []
l!l
14
MC/DC100%1/1
$params{'other_state_params'} || []
$params{'other_state_params'}
Truth table100%2/2
A
A= $params{'other_state_params'}
Aresult
11
00
213  
214 5 my %args = (
215 nonce => $nonce,
216 state => $state,
217 );
218  
219 5 my $code_verifier;
220 5 ▶ if ($self->client->use_pkce) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->client->use_pkce) 4 1 5
221 4 $self->log_msg(debug => "OIDC: generating code_verifier and code_challenge for PKCE");
222 4 $code_verifier = OIDC::Client::Utils::generate_code_verifier();
223 4 my $code_challenge_method = $self->client->pkce_code_challenge_method;
224 4 $args{code_challenge} = OIDC::Client::Utils::generate_code_challenge($code_verifier, $code_challenge_method);
225 4 $args{code_challenge_method} = $code_challenge_method;
226 }
227  
228 5 ▶ if (my $redirect_uri = $params{redirect_uri} || $self->login_redirect_uri) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $redirect_uri = $params{"redirect_uri"} || $self->login_redirect_uri) 3 2 5
Condition66%2/3
$params{'redirect_uri'} || $self->login_redirect_uri
l!l&&r!l&&!r
032
MC/DC50%1/2
$params{'redirect_uri'} || $self->login_redirect_uri
$params{'redirect_uri'} $self->login_redirect_uri
Truth table66%2/3
A || B
A= $params{'redirect_uri'}B= $self->login_redirect_uri
ABresult
1X1
011
000
229 3 $args{redirect_uri} = $redirect_uri;
230 }
231  
232 5 ▶ if (my $extra_params = $params{extra_params}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $extra_params = $params{"extra_params"}) 1 4 5
233 1 $args{extra_params} = $extra_params;
234 }
235  
236 5 my $authorize_url = $self->client->auth_url(%args);
237  
238 $self->write_session(['oidc_auth', $state], {
239 nonce => $nonce,
240 provider => $self->client->provider,
241 5 ▶ target_url => $params{target_url} ? $params{target_url} : $self->current_url,
Branch: 2 branches
BranchTrueFalseTotal
$params{'target_url'} ? : 1 4 5
$code_verifier ? : 4 1 5
242 $code_verifier ? (code_verifier => $code_verifier) : (),
243 });
244  
245 5 $self->log_msg(debug => "OIDC: redirecting to provider : $authorize_url");
246 5 $self->redirect->($authorize_url);
247 }
248  
249  
250 =head2 get_token( %args )
251  
252 my $identity = $c->oidc->get_token();
253  
254 Checks that the state parameter received from the provider is identical
255 to the state parameter sent with the authorize URL.
256  
257 From a code received from the provider, executes a request to get the token(s).
258  
259 Checks the ID token if present.
260 Checks the access token's C<at_hash> against the ID token's C<at_hash> claim if present.
261  
262 Stores the token(s) in the session, stash or cache depending on your configured
263 C<store_mode> (see L<OIDC::Client::Config>).
264  
265 Returns the stored L<OIDC::Client::Identity> object.
266  
267 The optional hash parameters are:
268  
269 =over 2
270  
271 =item redirect_uri
272  
273 Specifies the URL that the provider uses to redirect the user's browser back to
274 the application after the user has been authenticated.
275 Default to the URL built from the C<signin_redirect_path> configuration entry.
276  
277 =back
278  
279 =cut
280  
281 21 ▶sub get_token {
Subroutine get_token: called CC=13 SCAR=25.7
Pod documented
282 21 my $self = shift;
283 21 my %params = validated_hash(
284 \@_,
285 redirect_uri => { isa => 'Str', optional => 1 },
286 );
287  
288 21 $self->log_msg(debug => 'OIDC: getting token');
289  
290 21 my ($token_response, $auth_data);
291  
292 21 ▶ if ($self->client->token_endpoint_grant_type eq 'authorization_code') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->client->token_endpoint_grant_type eq 'authorization_code') { } 14 7 21
293 14 ▶ if ($self->request_params->{error}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->request_params->{"error"}) 1 13 14
294 1 OIDC::Client::Error::Provider->throw({response_parameters => $self->request_params});
295 }
296 13 $auth_data = $self->_extract_auth_data();
297 9 my $code_verifier = $auth_data->{code_verifier}; # PKCE
298 9 ▶ my $redirect_uri = $params{redirect_uri} || $self->login_redirect_uri;
Condition100%3/3
$params{'redirect_uri'} || $self->login_redirect_uri
l!l&&r!l&&!r
621
MC/DC100%2/2
$params{'redirect_uri'} || $self->login_redirect_uri
$params{'redirect_uri'} $self->login_redirect_uri
Truth table100%3/3
A || B
A= $params{'redirect_uri'}B= $self->login_redirect_uri
ABresult
1X1
011
000
299 $token_response = $self->client->get_token(
300 code => $self->request_params->{code},
301 9 ▶ $code_verifier ? (code_verifier => $code_verifier) : (),
Branch: 2 branches
BranchTrueFalseTotal
$code_verifier ? : 3 6 9
$redirect_uri ? : 8 1 9
302 $redirect_uri ? (redirect_uri => $redirect_uri) : (),
303 );
304 }
305 else {
306 7 $token_response = $self->client->get_token();
307 }
308  
309 16 my ($id_token_header, $id_token_claims);
310  
311 16 ▶ if (my $id_token = $token_response->id_token) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $id_token = $token_response->id_token) { } 8 8 16
elsif (($self->client->scope // '') =~ /\bopenid\b/u) { } 0 8 8
312 ($id_token_header, $id_token_claims) = $self->client->verify_jwt_token(
313 token => $id_token,
314 expected_audience => $self->client->id,
315 expected_authorized_party => $self->client->id,
316 no_authorized_party_accepted => 1,
317 max_token_age => $self->client->max_id_token_age,
318 8 ▶ $auth_data ? (expected_nonce => $auth_data->{nonce}) : (),
Branch: 1 branch
BranchTrueFalseTotal
$auth_data ? : 7 1 8
319 want_header => 1,
320 );
321 7 $self->_store_identity(
322 id_token => $id_token,
323 claims => $id_token_claims,
324 );
325 7 $self->log_msg(debug => "OIDC: identity has been stored");
326 }
327 ▶ elsif (($self->client->scope // '') =~ /\bopenid\b/) {
Condition50%1/2
$self->client->scope // ""
l!l
08
MC/DC0%0/1
$self->client->scope // ""
$self->client->scope
Truth table50%1/2
A
A= $self->client->scope
Aresult
11
00
328 0 OIDC::Client::Error::Authentication->throw(
329 "OIDC: no ID token returned by the provider ?"
330 );
331 }
332  
333 15 ▶ if ($token_response->access_token) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token_response->access_token) 11 4 15
334 11 my $access_token = build_access_token_from_token_response($token_response);
335 $access_token->verify_at_hash($id_token_claims->{at_hash}, $id_token_header->{alg})
336 11 ▶ if $id_token_claims;
Branch: 1 branch
BranchTrueFalseTotal
if $id_token_claims 3 8 11
337 10 $self->store_access_token($access_token);
338 10 $self->log_msg(debug => "OIDC: access token has been stored");
339 }
340  
341 14 ▶ if (my $refresh_token = $token_response->refresh_token) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $refresh_token = $token_response->refresh_token) 4 10 14
342 4 $self->store_refresh_token($refresh_token);
343 4 $self->log_msg(debug => "OIDC: refresh token has been stored");
344 }
345  
346 14 return $self->get_stored_identity();
347 }
348  
349  
350 =head2 refresh_token( $audience_alias )
351  
352 my $stored_access_token = $c->oidc->refresh_token( $audience_alias );
353  
354 Refreshes an access and/or ID token (usually because it has expired) for the default audience
355 (token for the current application) or for the audience corresponding to a given alias
356 (exchanged token for another application).
357  
358 Checks the ID token if it has been renewed.
359 Checks the access token's C<at_hash> against the ID token's C<at_hash> claim if present.
360  
361 Stores the renewed token(s) and returns the new L<OIDC::Client::AccessToken> object.
362  
363 Throws an error if no refresh token has been stored for the audience.
364  
365 The optional list parameters are:
366  
367 =over 2
368  
369 =item audience_alias
370  
371 Alias configured for the audience of the other application.
372  
373 =back
374  
375 =cut
376  
377 14 ▶sub refresh_token {
Subroutine refresh_token: called CC=12 SCAR=24.9
Pod documented
378 14 my $self = shift;
379 14 my ($audience_alias) = pos_validated_list(\@_, { isa => 'Maybe[Str]', optional => 1 });
380  
381 14 ▶ my $audience = $audience_alias ? $self->client->get_audience_for_alias($audience_alias)
Branch: 2 branches
BranchTrueFalseTotal
my $audience = $audience_alias ? $self->client->get_audience_for_alias($audience_alias) : $self->client->audience or croak("OIDC: no audience for alias '${audience_alias}'") 13 1 14
$audience_alias ? : 5 9 14
382 : $self->client->audience
383 or croak("OIDC: no audience for alias '$audience_alias'");
384  
385 13 $self->log_msg(debug => "OIDC: refreshing token for audience $audience");
386  
387 13 ▶ my $refresh_token = $self->get_stored_refresh_token($audience_alias)
Branch: 1 branch
BranchTrueFalseTotal
my $refresh_token = $self->get_stored_refresh_token($audience_alias) or "OIDC::Client::Error"->throw("OIDC: no refresh token has been stored") 12 1 13
388 or OIDC::Client::Error->throw("OIDC: no refresh token has been stored");
389  
390 12 ▶ my $refresh_scope = $audience_alias ? undef
Branch: 1 branch
BranchTrueFalseTotal
$audience_alias ? : 4 8 12
391 : $self->client->refresh_scope;
392  
393 12 ▶ my $token_response = $self->client->get_token(
Branch: 1 branch
BranchTrueFalseTotal
$refresh_scope ? : 1 11 12
394 grant_type => 'refresh_token',
395 refresh_token => $refresh_token,
396 $refresh_scope ? (refresh_scope => $refresh_scope) : (),
397 );
398  
399 11 my ($id_token_header, $id_token_claims);
400  
401 11 ▶ if (my $id_token = $token_response->id_token) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $id_token = $token_response->id_token) 3 8 11
402 3 ▶ my $identity = $self->get_stored_identity()
Branch: 1 branch
BranchTrueFalseTotal
my $identity = $self->get_stored_identity or "OIDC::Client::Error"->throw("OIDC: no identity has been stored") 3 0 3
403 or OIDC::Client::Error->throw("OIDC: no identity has been stored");
404  
405 3 my $expected_subject = $identity->subject;
406 3 my $expected_nonce = $identity->claims->{nonce};
407 ($id_token_header, $id_token_claims) = $self->client->verify_jwt_token(
408 token => $id_token,
409 expected_subject => $expected_subject,
410 expected_audience => $self->client->id,
411 expected_authorized_party => $identity->claims->{azp},
412 3 ▶ max_token_age => $self->client->max_id_token_age,
Branch: 1 branch
BranchTrueFalseTotal
$expected_nonce ? : 3 0 3
413 $expected_nonce
414 ? (expected_nonce => $expected_nonce,
415 no_nonce_accepted => 1)
416 : (),
417 want_header => 1,
418 );
419 3 $self->_store_identity(
420 id_token => $id_token,
421 claims => $id_token_claims,
422 );
423 3 $self->log_msg(debug => "OIDC: identity has been renewed and stored");
424 }
425  
426 11 ▶ if ($token_response->access_token) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token_response->access_token) 10 1 11
427 10 my $access_token = build_access_token_from_token_response($token_response);
428 $access_token->verify_at_hash($id_token_claims->{at_hash}, $id_token_header->{alg})
429 10 ▶ if $id_token_claims;
Branch: 1 branch
BranchTrueFalseTotal
if $id_token_claims 2 8 10
430 9 $self->store_access_token($access_token, $audience_alias);
431 9 $self->log_msg(debug => "OIDC: access token has been renewed and stored");
432 }
433  
434 10 ▶ if (my $refresh_token = $token_response->refresh_token) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $refresh_token = $token_response->refresh_token) 10 0 10
435 10 $self->store_refresh_token($refresh_token, $audience_alias);
436 10 $self->log_msg(debug => "OIDC: refresh token has been renewed and stored");
437 }
438  
439 10 return $self->get_stored_access_token($audience_alias);
440 }
441  
442  
443 =head2 exchange_token( $audience_alias )
444  
445 my $stored_exchanged_token = $c->oidc->exchange_token($audience_alias);
446  
447 Exchange the access token received during the user's login, for an access token
448 that is accepted by a different OIDC application and stores it.
449  
450 Stores and returns an L<OIDC::Client::AccessToken> object.
451  
452 The list parameters are:
453  
454 =over 2
455  
456 =item audience_alias
457  
458 Alias configured for the audience of the other application.
459  
460 =back
461  
462 =cut
463  
464 9 ▶sub exchange_token {
Subroutine exchange_token: called CC=2 SCAR=6.9
Pod documented
465 9 my $self = shift;
466 9 my ($audience_alias) = pos_validated_list(\@_, { isa => 'Str', optional => 0 });
467  
468 9 $self->log_msg(debug => 'OIDC: exchanging token');
469  
470 9 my $audience = $self->_get_audience_from_alias($audience_alias);
471  
472 8 my $access_token = $self->get_valid_access_token();
473  
474 7 my $exchanged_token_response = $self->client->exchange_token(
475 token => $access_token->token,
476 audience => $audience,
477 );
478  
479 6 my $exchanged_access_token = build_access_token_from_token_response($exchanged_token_response);
480 6 $self->store_access_token($exchanged_access_token, $audience_alias);
481 6 $self->log_msg(debug => "OIDC: access token has been exchanged and stored");
482  
483 6 ▶ if (my $refresh_token = $exchanged_token_response->refresh_token) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $refresh_token = $exchanged_token_response->refresh_token) 5 1 6
484 5 $self->store_refresh_token($refresh_token, $audience_alias);
485 }
486  
487 6 return $exchanged_access_token;
488 }
489  
490  
491 =head2 verify_token()
492  
493 my $access_token = $c->oidc->verify_token();
494  
495 Verifies the access token received in the Authorization header of the current request.
496 Throws an exception if an error occurs. Otherwise, stores an L<OIDC::Client::AccessToken> object
497 and returns it.
498  
499 Depending on the C<token_validation_method> configuration entry, the token is validated
500 either by expecting it is a JWT token or by using the introspection endpoint.
501  
502 To bypass the token verification in local environment, you can configure the C<mocked_access_token>
503 entry (hashref) to be used to create an L<OIDC::Client::AccessToken> object that will be returned
504 by this method.
505  
506 =cut
507  
508 11 ▶sub verify_token {
Subroutine verify_token: called CC=5 SCAR=16.1
Pod documented
509 11 my $self = shift;
510  
511 11 ▶ if ($self->is_base_url_local and my $mocked_access_token = $self->client->mocked_access_token) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_base_url_local and my $mocked_access_token = $self->client->mocked_access_token) 1 10 11
Condition66%2/3
$self->is_base_url_local and my $mocked_access_token = $self->client->mocked_access_token
!ll&&!rl&&r
1001
MC/DC50%1/2
$self->is_base_url_local and my $mocked_access_token = $self->client->mocked_access_token
$self->is_base_url_local my $mocked_access_token = $self->client->mocked_access_token
Truth table66%2/3
A and B
A= $self->is_base_url_localB= my $mocked_access_token = $self->client->mocked_access_token
ABresult
0X0
100
111
512 1 return OIDC::Client::AccessToken->new($mocked_access_token);
513 }
514  
515 10 ▶ my $token = $self->get_token_from_authorization_header()
Branch: 1 branch
BranchTrueFalseTotal
my $token = $self->get_token_from_authorization_header or "OIDC::Client::Error"->throw("OIDC: no token in authorization header") 8 2 10
516 or OIDC::Client::Error->throw("OIDC: no token in authorization header");
517  
518 8 ▶ my $claims = $self->client->token_validation_method eq 'jwt'
Branch: 1 branch
BranchTrueFalseTotal
$self->client->token_validation_method eq 'jwt' ? : 7 1 8
519 ? $self->client->verify_jwt_token(token => $token)
520 : $self->client->introspect_token(token => $token, token_type_hint => 'access_token');
521  
522 7 my $access_token = build_access_token_from_claims($claims, $token);
523 7 $self->store_access_token($access_token);
524  
525 7 return $access_token;
526 }
527  
528  
529 =head2 get_token_from_authorization_header()
530  
531 my $token = $c->oidc->get_token_from_authorization_header();
532  
533 Returns the token received in the Authorization header of the current request,
534 or returns undef if there is no token in this header.
535  
536 =cut
537  
538 12 ▶sub get_token_from_authorization_header {
Subroutine get_token_from_authorization_header: called CC=2 SCAR=6.9
Pod documented
539 12 my $self = shift;
540  
541 my $authorization = $self->request_headers->{Authorization}
542 12 ▶ or return;
Branch: 1 branch
BranchTrueFalseTotal
my $authorization = $self->request_headers->{"Authorization"} or return 10 2 12
543  
544 10 my $token_type = $self->client->default_token_type;
545  
546 10 my ($token) = $authorization =~ /^$token_type\s+([^\s]+)/i;
547  
548 10 return $token;
549 }
550  
551  
552 =head2 get_userinfo()
553  
554 my $userinfo = $c->oidc->get_userinfo();
555  
556 Returns the user informations from the userinfo endpoint.
557  
558 This method should only be invoked when an access token has been stored.
559  
560 To mock the userinfo returned by this method in local environment, you can configure
561 the C<mocked_userinfo> entry (hashref).
562  
563 =cut
564  
565 4 ▶sub get_userinfo {
Subroutine get_userinfo: called CC=3 SCAR=11.0
Pod documented
566 4 my $self = shift;
567  
568 4 ▶ if ($self->is_base_url_local and my $mocked_userinfo = $self->client->mocked_userinfo) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_base_url_local and my $mocked_userinfo = $self->client->mocked_userinfo) 1 3 4
Condition66%2/3
$self->is_base_url_local and my $mocked_userinfo = $self->client->mocked_userinfo
!ll&&!rl&&r
301
MC/DC50%1/2
$self->is_base_url_local and my $mocked_userinfo = $self->client->mocked_userinfo
$self->is_base_url_local my $mocked_userinfo = $self->client->mocked_userinfo
Truth table66%2/3
A and B
A= $self->is_base_url_localB= my $mocked_userinfo = $self->client->mocked_userinfo
ABresult
0X0
100
111
569 1 return $mocked_userinfo;
570 }
571  
572 3 my $stored_access_token = $self->get_valid_access_token();
573  
574 3 return $self->client->get_userinfo(
575 access_token => $stored_access_token->token,
576 token_type => $stored_access_token->token_type,
577 );
578 }
579  
580  
581 =head2 build_user_from_userinfo( $user_class )
582  
583 my $user = $c->oidc->build_user_from_userinfo();
584  
585 Gets the user informations calling the provider (see L</"get_userinfo()">)
586 and returns a user object (L<OIDC::Client::User> by default) from this user
587 informations.
588  
589 The C<claim_mapping> configuration entry is used to map user information to
590 user attributes.
591  
592 The optional list parameters are:
593  
594 =over 2
595  
596 =item user_class
597  
598 Class to be used to instantiate the user object.
599 Default to L<OIDC::Client::User>.
600  
601 =back
602  
603 =cut
604  
605 1 ▶sub build_user_from_userinfo {
Subroutine build_user_from_userinfo: called CC=1 SCAR=0.0
Pod documented
606 1 my $self = shift;
607 1 my ($user_class) = pos_validated_list(\@_, { isa => 'Str', default => 'OIDC::Client::User' });
608  
609 1 my $userinfo = $self->get_userinfo();
610  
611 1 return $self->build_user_from_claims($userinfo, $user_class);
612 }
613  
614  
615 =head2 build_user_from_claims( $claims, $user_class )
616  
617 my $user = $c->oidc->build_user_from_claims($claims);
618  
619 Returns a user object (L<OIDC::Client::User> by default) based on provided claims.
620  
621 The C<claim_mapping> configuration entry is used to map claim keys to user attributes.
622  
623 This method can be useful, for example, if the access token is a JWT token that has just been
624 verified with the L<verify_token()> method and already contains the relevant information
625 without having to call the C<userinfo> endpoint.
626  
627 The list parameters are:
628  
629 =over 2
630  
631 =item claims
632  
633 Hashref of claims.
634  
635 =item user_class
636  
637 Optional class to be used to instantiate the user object.
638 Default to L<OIDC::Client::User>.
639  
640 =back
641  
642 =cut
643  
644 5 ▶sub build_user_from_claims {
Subroutine build_user_from_claims: called CC=3 SCAR=11.0
Pod documented
645 5 my $self = shift;
646 5 my ($claims, $user_class) = pos_validated_list(\@_, { isa => 'HashRef', optional => 0 },
647 { isa => 'Str', default => 'OIDC::Client::User' });
648 5 load($user_class);
649  
650 5 my $mapping = $self->client->claim_mapping;
651 5 my $role_prefix = $self->client->role_prefix;
652  
653 return $user_class->new(
654 (
655 map {
656 5 my $val = $self->client->get_claim_value(name => $_, claims => $claims, optional => 1);
657 25 ▶ defined $val ? ($_ => $val) : ();
Branch: 2 branches
BranchTrueFalseTotal
defined $val ? : 20 5 25
defined $role_prefix ? : 5 0 5
658 }
659 keys %$mapping
660 ),
661 defined $role_prefix ? (role_prefix => $role_prefix) : (),
662 );
663 }
664  
665  
666 =head2 build_user_from_identity( $user_class )
667  
668 my $user = $c->oidc->build_user_from_identity();
669  
670 Returns a user object (L<OIDC::Client::User> by default) from the claims
671 of the stored identity.
672  
673 The C<claim_mapping> configuration entry is used to map identity claim keys
674 to user attributes.
675  
676 This method should only be invoked when an identity has been stored, i.e.
677 when an authorisation flow has completed and an ID token has been returned
678 by the provider.
679  
680 The optional list parameters are:
681  
682 =over 2
683  
684 =item user_class
685  
686 Class to be used to instantiate the user object.
687 Default to L<OIDC::Client::User>.
688  
689 =back
690  
691 =cut
692  
693 1 ▶sub build_user_from_identity {
Subroutine build_user_from_identity: called CC=2 SCAR=7.0
Pod documented
694 1 my $self = shift;
695 1 my ($user_class) = pos_validated_list(\@_, { isa => 'Str', default => 'OIDC::Client::User' });
696 1 load($user_class);
697  
698 1 ▶ my $identity = $self->get_stored_identity()
Branch: 1 branch
BranchTrueFalseTotal
my $identity = $self->get_stored_identity or "OIDC::Client::Error"->throw("OIDC: no identity has been stored") 1 0 1
699 or OIDC::Client::Error->throw("OIDC: no identity has been stored");
700  
701 1 return $self->build_user_from_claims($identity->claims, $user_class);
702 }
703  
704  
705 =head2 build_api_useragent( $audience_alias )
706  
707 my $ua = $c->oidc->build_api_useragent( $audience_alias );
708  
709 Builds a web client (L<Mojo::UserAgent> object) to perform requests
710 on another application with security context propagation.
711  
712 The appropriate access token will be added in the authorization header
713 of each request.
714  
715 The list parameters are:
716  
717 =over 2
718  
719 =item audience_alias
720  
721 Optional alias configured for the audience of the other application.
722 If this parameter is missing, the default audience (current application) is used.
723  
724 =back
725  
726 =cut
727  
728 4 ▶sub build_api_useragent {
Subroutine build_api_useragent: called CC=1 SCAR=0.0
Pod documented
729 4 my $self = shift;
730 4 my ($audience_alias) = pos_validated_list(\@_, { isa => 'Str', optional => 1 });
731  
732 4 my $access_token = $self->get_valid_access_token($audience_alias);
733  
734 4 return build_api_useragent_from_access_token($access_token);
735 }
736  
737  
738 =head2 redirect_to_logout( %args )
739  
740 $c->oidc->redirect_to_logout();
741  
742 Redirect the browser to the logout URL.
743  
744 The optional hash parameters are:
745  
746 =over 2
747  
748 =item with_id_token
749  
750 Specifies whether the stored id token should be sent to the provider.
751 Default to the C<logout_with_id_token> configuration entry or true by default.
752  
753 =item target_url
754  
755 Specifies the URL to redirect to after the browser is redirected to the logout
756 callback URL.
757  
758 =item post_logout_redirect_uri
759  
760 Specifies the URL that the provider uses to redirect the user's browser back to
761 the application after the logout has been performed.
762 Default to the URL built from the C<logout_redirect_path> configuration entry.
763  
764 =item extra_params
765  
766 Hashref which can be used to send extra query parameters.
767  
768 =item other_state_params
769  
770 List (arrayref) of strings to add before the auto-generated random string to build
771 the C<state> parameter. All these strings are separated by a comma.
772  
773 =back
774  
775 =cut
776  
777 2 ▶sub redirect_to_logout {
Subroutine redirect_to_logout: called CC=9 SCAR=22.1
Pod documented
778 2 my $self = shift;
779 2 my %params = validated_hash(
780 \@_,
781 with_id_token => { isa => 'Bool', optional => 1 },
782 target_url => { isa => 'Str', optional => 1 },
783 post_logout_redirect_uri => { isa => 'Str', optional => 1 },
784 extra_params => { isa => 'HashRef', optional => 1 },
785 other_state_params => { isa => 'ArrayRef[Str]', optional => 1 },
786 );
787  
788 2 ▶ my $state = join ',', (@{$params{other_state_params} || []}, OIDC::Client::Utils::generate_state());
Condition100%2/2
$params{'other_state_params'} || []
l!l
11
MC/DC100%1/1
$params{'other_state_params'} || []
$params{'other_state_params'}
Truth table100%2/2
A
A= $params{'other_state_params'}
Aresult
11
00
789  
790 2 my %args = (
791 state => $state,
792 );
793  
794 2 ▶ if ($params{with_id_token} // $self->client->logout_with_id_token // 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (($params{"with_id_token"} // $self->client->logout_with_id_token) // 1) 1 1 2
Condition80%4/5
($params{"with_id_token"} // $self->client->logout_with_id_token) // 1
l!l
11
$params{"with_id_token"} // $self->client->logout_with_id_token
l!l&&r!l&&!r
101
MC/DC50%1/2
($params{"with_id_token"} // $self->client->logout_with_id_token) // 1
$params{"with_id_token"} $self->client->logout_with_id_token
Truth table66%2/3
A // B
A= $params{"with_id_token"}B= $self->client->logout_with_id_token
ABresult
1X1
011
000
795 1 ▶ my $identity = $self->get_stored_identity()
Branch: 1 branch
BranchTrueFalseTotal
my $identity = $self->get_stored_identity or "OIDC::Client::Error"->throw("OIDC: no identity has been stored") 1 0 1
796 or OIDC::Client::Error->throw("OIDC: no identity has been stored");
797 1 $args{id_token} = $identity->token;
798 }
799  
800 2 ▶ if (my $redirect_uri = $params{post_logout_redirect_uri} || $self->logout_redirect_uri) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $redirect_uri = $params{"post_logout_redirect_uri"} || $self->logout_redirect_uri) 2 0 2
Condition66%2/3
$params{'post_logout_redirect_uri'} || $self->logout_redirect_uri
l!l&&r!l&&!r
110
MC/DC0%0/2
$params{'post_logout_redirect_uri'} || $self->logout_redirect_uri
$params{'post_logout_redirect_uri'} $self->logout_redirect_uri
Truth table66%2/3
A || B
A= $params{'post_logout_redirect_uri'}B= $self->logout_redirect_uri
ABresult
1X1
011
000
801 2 $args{post_logout_redirect_uri} = $redirect_uri;
802 }
803  
804 2 ▶ if (my $extra_params = $params{extra_params}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $extra_params = $params{"extra_params"}) 1 1 2
805 1 $args{extra_params} = $extra_params;
806 }
807  
808 2 my $logout_url = $self->client->logout_url(%args);
809  
810 $self->write_session(['oidc_logout', $state], {
811 provider => $self->client->provider,
812 target_url => $params{target_url},
813 2 });
814  
815 2 $self->log_msg(debug => "OIDC: redirecting to idp for log out : $logout_url");
816 2 $self->redirect->($logout_url);
817 }
818  
819  
820 =head2 get_valid_access_token( $audience_alias )
821  
822 my $valid_access_token = $c->oidc->get_valid_access_token( $audience_alias );
823  
824 When an audience alias is specified and no access token has been stored for the audience,
825 returns the execution of the L</"exchange_token( $audience_alias )"> method.
826  
827 Retrieves the stored L<OIDC::Client::AccessToken> object for the default audience
828 (current application) or for the audience corresponding to the given alias.
829  
830 If this token has not expired, returns it, otherwise returns the execution
831 of the L</"refresh_token( $audience_alias )"> method.
832  
833 If the refresh failed and the audience alias is specified, finally returns
834 the execution of the L</"exchange_token( $audience_alias )"> method.
835  
836 The optional list parameters are:
837  
838 =over 2
839  
840 =item audience_alias
841  
842 Alias configured for the audience of the other application.
843  
844 =back
845  
846 In local environment, if the C<mocked_access_token> entry (hashref) is configured,
847 it is used to create an L<OIDC::Client::AccessToken> object that will be returned
848 by this method.
849  
850 =cut
851  
852 31 ▶sub get_valid_access_token {
Subroutine get_valid_access_token: called CC=11 SCAR=24.0
Pod documented
853 31 my $self = shift;
854 31 my ($audience_alias) = pos_validated_list(\@_, { isa => 'Maybe[Str]', optional => 1 });
855  
856 31 my $stored_access_token = $self->get_stored_access_token($audience_alias);
857  
858 30 ▶ unless ($stored_access_token) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($stored_access_token) 6 24 30
859 6 ▶ if ($audience_alias) {
Branch: 1 branch
BranchTrueFalseTotal
if ($audience_alias) 3 3 6
860 3 return $self->exchange_token($audience_alias);
861 }
862 3 $self->get_token();
863 2 ▶ return $self->get_stored_access_token()
Condition66%2/3
$self->get_stored_access_token || 'OIDC::Client::Error'->throw('OIDC: access token has not been retrieved from token endpoint')
l!l&&r!l&&!r
110
MC/DC0%0/2
$self->get_stored_access_token || 'OIDC::Client::Error'->throw('OIDC: access token has not been retrieved from token endpoint')
$self->get_stored_access_token 'OIDC::Client::Error'->throw('OIDC: access token has not been retrieved from token endpoint')
Truth table33%1/3
A || B
A= $self->get_stored_access_tokenB= 'OIDC::Client::Error'->throw('OIDC: access token has not been retrieved from token endpoint')
ABresult
1X1
011
000
864 || OIDC::Client::Error->throw("OIDC: access token has not been retrieved from token endpoint");
865 }
866  
867 24 ▶ my $audience = $audience_alias ? $self->_get_audience_from_alias($audience_alias)
Branch: 1 branch
BranchTrueFalseTotal
$audience_alias ? : 8 16 24
868 : $self->client->audience;
869  
870 24 ▶ unless ($stored_access_token->expires_at) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($stored_access_token->expires_at) 12 12 24
871 12 $self->log_msg(debug => "OIDC: no expiration time for the access token with '$audience' audience. Hoping it's still valid.");
872 12 return $stored_access_token;
873 }
874  
875 12 ▶ unless ($stored_access_token->has_expired($self->client->expiration_leeway)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($stored_access_token->has_expired($self->client->expiration_leeway)) 2 10 12
876 2 $self->log_msg(debug => "OIDC: access token for audience '$audience' has been retrieved from store");
877 2 return $stored_access_token;
878 }
879  
880 10 $self->log_msg(debug => "OIDC: access token has expired for audience '$audience'");
881  
882 10 ▶ if ($self->get_stored_refresh_token($audience_alias)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->get_stored_refresh_token($audience_alias)) 7 3 10
883 7 ▶ my $renewed_access_token = try {
Subroutine __ANON__: called CC=2 SCAR=7.6
884 7 ▶ $self->refresh_token($audience_alias)
Branch: 1 branch
BranchTrueFalseTotal
$self->refresh_token($audience_alias) or "OIDC::Client::Error"->throw("OIDC: access token has not been refreshed") 6 0 6
885 or OIDC::Client::Error->throw("OIDC: access token has not been refreshed");
886 }
887 1 ▶ catch {
Subroutine __ANON__: called CC=1 SCAR=0.0
888 1 $self->log_msg(debug => "OIDC: error refreshing access token for audience '$audience' : $_");
889 1 return;
890 7 };
891 7 ▶ return $renewed_access_token if $renewed_access_token;
Branch: 1 branch
BranchTrueFalseTotal
if $renewed_access_token 6 1 7
892 }
893  
894 4 ▶ if ($audience_alias) {
Branch: 1 branch
BranchTrueFalseTotal
if ($audience_alias) { } 2 2 4
895 2 return $self->exchange_token($audience_alias);
896 }
897 else {
898 2 $self->get_token();
899 1 ▶ return $self->get_stored_access_token()
Condition33%1/3
$self->get_stored_access_token || 'OIDC::Client::Error'->throw('OIDC: access token has not been retrieved from token endpoint')
l!l&&r!l&&!r
100
MC/DC0%0/2
$self->get_stored_access_token || 'OIDC::Client::Error'->throw('OIDC: access token has not been retrieved from token endpoint')
$self->get_stored_access_token 'OIDC::Client::Error'->throw('OIDC: access token has not been retrieved from token endpoint')
Truth table33%1/3
A || B
A= $self->get_stored_access_tokenB= 'OIDC::Client::Error'->throw('OIDC: access token has not been retrieved from token endpoint')
ABresult
1X1
011
000
900 || OIDC::Client::Error->throw("OIDC: access token has not been retrieved from token endpoint");
901 }
902 }
903  
904  
905 =head2 get_valid_identity()
906  
907 my $identity = $c->oidc->get_valid_identity();
908  
909 Executes the L</"get_stored_identity()"> method to get the stored
910 L<OIDC::Client::Identity> object.
911  
912 Returns undef if no identity has been stored or if the stored identity has expired
913 including the configured leeway.
914  
915 Otherwise, returns the stored L<OIDC::Client::Identity> object.
916  
917 =cut
918  
919 6 ▶sub get_valid_identity {
Subroutine get_valid_identity: called CC=3 SCAR=11.0
Pod documented
920 6 my $self = shift;
921  
922 6 ▶ my $stored_identity = $self->get_stored_identity()
Branch: 1 branch
BranchTrueFalseTotal
my $stored_identity = $self->get_stored_identity or return 3 3 6
923 or return;
924  
925 3 ▶ return if $stored_identity->has_expired($self->client->expiration_leeway);
Branch: 1 branch
BranchTrueFalseTotal
if $stored_identity->has_expired($self->client->expiration_leeway) 1 2 3
926  
927 2 return $stored_identity;
928 }
929  
930  
931 =head2 get_stored_identity()
932  
933 my $identity = $c->oidc->get_stored_identity();
934  
935 Returns undef if no identity has been stored. Otherwise, returns the stored
936 L<OIDC::Client::Identity> object, even if the identity has expired.
937  
938 By default, the C<expires_at> attribute comes directly from the C<exp> claim but
939 if the C<identity_expires_in> configuration entry is specified, it is added to the current
940 time (when the ID token is retrieved) to force an expiration time.
941  
942 To bypass the OIDC flow in local environment, you can configure the C<mocked_identity>
943 entry (hashref) to be used to create an L<OIDC::Client::Identity> object that will
944 be returned by this method.
945  
946 =cut
947  
948 30 ▶sub get_stored_identity {
Subroutine get_stored_identity: called CC=4 SCAR=13.9
Pod documented
949 30 my $self = shift;
950  
951 30 ▶ if ($self->is_base_url_local and my $mocked_identity = $self->client->mocked_identity) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_base_url_local and my $mocked_identity = $self->client->mocked_identity) 1 29 30
Condition66%2/3
$self->is_base_url_local and my $mocked_identity = $self->client->mocked_identity
!ll&&!rl&&r
2901
MC/DC50%1/2
$self->is_base_url_local and my $mocked_identity = $self->client->mocked_identity
$self->is_base_url_local my $mocked_identity = $self->client->mocked_identity
Truth table66%2/3
A and B
A= $self->is_base_url_localB= my $mocked_identity = $self->client->mocked_identity
ABresult
0X0
100
111
952 1 return OIDC::Client::Identity->new($mocked_identity);
953 }
954  
955 29 my $audience = $self->client->id;
956 29 ▶ my $identity = $self->_get_audience_store($audience, 'identity')
Branch: 1 branch
BranchTrueFalseTotal
my $identity = $self->_get_audience_store($audience, "identity") or return 16 13 29
957 or return;
958  
959 16 return OIDC::Client::Identity->new($identity);
960 }
961  
962  
963 10 ▶sub _store_identity {
Subroutine _store_identity: called CC=5 SCAR=16.1
964 10 my $self = shift;
965 10 my %params = validated_hash(
966 \@_,
967 id_token => { isa => 'Str', optional => 0 },
968 claims => { isa => 'HashRef', optional => 0 },
969 );
970  
971 10 my $subject = $params{claims}->{sub};
972 10 ▶ defined $subject
Branch: 1 branch
BranchTrueFalseTotal
defined $subject or "OIDC::Client::Error::Authentication"->throw("OIDC: the 'sub' claim is not defined") 10 0 10
973 or OIDC::Client::Error::Authentication->throw("OIDC: the 'sub' claim is not defined");
974  
975 my %identity = (
976 subject => $subject,
977 claims => $params{claims},
978 token => $params{id_token},
979 10 );
980  
981 10 my $expires_in = $self->client->identity_expires_in;
982 10 ▶ if (defined $expires_in) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $expires_in) { } 2 8 10
983 2 ▶ if ($expires_in != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expires_in != 0) 1 1 2
984 1 $identity{expires_at} = time + $expires_in;
985 }
986 }
987 else {
988 $identity{expires_at} = $params{claims}->{exp}
989 8 ▶ or $self->log_msg(warning => "OIDC: no 'exp' claim in the ID token");
Branch: 1 branch
BranchTrueFalseTotal
$identity{"expires_at"} = $params{"claims"}{"exp"} or $self->log_msg("warning", "OIDC: no 'exp' claim in the ID token") 8 0 8
990 }
991  
992 10 my $audience = $self->client->id;
993  
994 # not stored as Identity object because we can't rely on the session engine to preserve its type
995 10 $self->_set_audience_store($audience, 'identity', \%identity);
996 }
997  
998  
999 =head2 get_stored_access_token( $audience_alias )
1000  
1001 my $access_token = $c->oidc->get_stored_access_token();
1002  
1003 Returns the stored L<OIDC::Client::AccessToken> object for the specified audience alias,
1004 even if this token has expired.
1005  
1006 The optional list parameters are:
1007  
1008 =over 2
1009  
1010 =item audience_alias
1011  
1012 Alias configured for the audience of the other application.
1013  
1014 =back
1015  
1016 In local environment, if the C<mocked_access_token> entry (hashref) is configured,
1017 it is used to create an L<OIDC::Client::AccessToken> object that will be returned
1018 by this method.
1019  
1020 =cut
1021  
1022 48 ▶sub get_stored_access_token {
Subroutine get_stored_access_token: called CC=5 SCAR=16.1
Pod documented
1023 48 my $self = shift;
1024 48 my ($audience_alias) = pos_validated_list(\@_, { isa => 'Maybe[Str]', optional => 1 });
1025  
1026 48 ▶ my $audience = $audience_alias ? $self->_get_audience_from_alias($audience_alias)
Branch: 1 branch
BranchTrueFalseTotal
$audience_alias ? : 15 33 48
1027 : $self->client->audience;
1028  
1029 47 ▶ if ($self->is_base_url_local and my $mocked_access_token = $self->client->mocked_access_token) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_base_url_local and my $mocked_access_token = $self->client->mocked_access_token) 2 45 47
Condition66%2/3
$self->is_base_url_local and my $mocked_access_token = $self->client->mocked_access_token
!ll&&!rl&&r
4502
MC/DC50%1/2
$self->is_base_url_local and my $mocked_access_token = $self->client->mocked_access_token
$self->is_base_url_local my $mocked_access_token = $self->client->mocked_access_token
Truth table66%2/3
A and B
A= $self->is_base_url_localB= my $mocked_access_token = $self->client->mocked_access_token
ABresult
0X0
100
111
1030 2 return OIDC::Client::AccessToken->new($mocked_access_token);
1031 }
1032  
1033 45 ▶ my $access_token = $self->_get_audience_store($audience, 'access_token')
Branch: 1 branch
BranchTrueFalseTotal
my $access_token = $self->_get_audience_store($audience, "access_token") or return 37 8 45
1034 or return;
1035  
1036 37 return OIDC::Client::AccessToken->new($access_token);
1037 }
1038  
1039  
1040 =head2 store_access_token( $access_token, $audience_alias )
1041  
1042 $c->oidc->store_access_token($access_token);
1043  
1044 Stores an L<OIDC::Client::AccessToken> object in the session, stash or cache
1045 depending on your configured C<store_mode> (see L<OIDC::Client::Config>).
1046  
1047 =cut
1048  
1049 34 ▶sub store_access_token {
Subroutine store_access_token: called CC=2 SCAR=6.9
Pod documented
1050 34 my $self = shift;
1051 34 my ($access_token, $audience_alias) = pos_validated_list(\@_, { isa => 'OIDC::Client::AccessToken', optional => 0 },
1052 { isa => 'Maybe[Str]', optional => 1 });
1053  
1054 34 ▶ my $audience = $audience_alias ? $self->_get_audience_from_alias($audience_alias)
Branch: 1 branch
BranchTrueFalseTotal
$audience_alias ? : 10 24 34
1055 : $self->client->audience;
1056  
1057 # stored as a hashref because we can't rely on the session engine to preserve the object type
1058 34 $self->_set_audience_store($audience, 'access_token', $access_token->to_hashref);
1059 }
1060  
1061  
1062 =head2 get_stored_refresh_token( $audience_alias )
1063  
1064 my $refresh_token = $c->oidc->get_stored_refresh_token();
1065  
1066 Returns the stored refresh token (string) for the specified audience alias.
1067  
1068 The optional list parameters are:
1069  
1070 =over 2
1071  
1072 =item audience_alias
1073  
1074 Alias configured for the audience of the other application.
1075  
1076 =back
1077  
1078 =cut
1079  
1080 23 ▶sub get_stored_refresh_token {
Subroutine get_stored_refresh_token: called CC=2 SCAR=6.9
Pod documented
1081 23 my $self = shift;
1082 23 my ($audience_alias) = pos_validated_list(\@_, { isa => 'Maybe[Str]', optional => 1 });
1083  
1084 23 ▶ my $audience = $audience_alias ? $self->_get_audience_from_alias($audience_alias)
Branch: 1 branch
BranchTrueFalseTotal
$audience_alias ? : 8 15 23
1085 : $self->client->audience;
1086  
1087 23 return $self->_get_audience_store($audience, 'refresh_token');
1088 }
1089  
1090  
1091 =head2 store_refresh_token( $refresh_token, $audience_alias )
1092  
1093 $c->oidc->store_refresh_token($refresh_token);
1094  
1095 Stores the refresh token value in the session, stash or cache depending
1096 on your configured C<store_mode> (see L<OIDC::Client::Config>).
1097  
1098 =cut
1099  
1100 21 ▶sub store_refresh_token {
Subroutine store_refresh_token: called CC=2 SCAR=6.9
Pod documented
1101 21 my $self = shift;
1102 21 my ($refresh_token, $audience_alias) = pos_validated_list(\@_, { isa => 'Str', optional => 0 },
1103 { isa => 'Maybe[Str]', optional => 1 });
1104  
1105 21 ▶ my $audience = $audience_alias ? $self->_get_audience_from_alias($audience_alias)
Branch: 1 branch
BranchTrueFalseTotal
$audience_alias ? : 9 12 21
1106 : $self->client->audience;
1107  
1108 21 $self->_set_audience_store($audience, 'refresh_token', $refresh_token);
1109 }
1110  
1111  
1112 59 ▶sub _get_audience_from_alias {
Subroutine _get_audience_from_alias: called CC=2 SCAR=6.9
1113 59 my $self = shift;
1114 59 my ($audience_alias) = pos_validated_list(\@_, { isa => 'Str', optional => 0 });
1115  
1116 59 ▶ my $audience = $self->client->get_audience_for_alias($audience_alias)
Branch: 1 branch
BranchTrueFalseTotal
my $audience = $self->client->get_audience_for_alias($audience_alias) or croak("OIDC: no audience for alias '${audience_alias}'") 57 2 59
1117 or croak("OIDC: no audience for alias '$audience_alias'");
1118  
1119 57 return $audience;
1120 }
1121  
1122  
1123 13 ▶sub _extract_auth_data {
Subroutine _extract_auth_data: called CC=3 SCAR=11.0
1124 13 my $self = shift;
1125  
1126 my $state = $self->request_params->{state}
1127 13 ▶ or OIDC::Client::Error::Authentication->throw("OIDC: no state parameter in request");
Branch: 1 branch
BranchTrueFalseTotal
my $state = $self->request_params->{"state"} or "OIDC::Client::Error::Authentication"->throw("OIDC: no state parameter in request") 10 3 13
1128  
1129 10 ▶ my $auth_data = $self->delete_session(['oidc_auth', $state])
Branch: 1 branch
BranchTrueFalseTotal
my $auth_data = $self->delete_session(["oidc_auth", $state]) or "OIDC::Client::Error::Authentication"->throw("OIDC: no authorisation data for state : '${state}'") 9 1 10
1130 or OIDC::Client::Error::Authentication->throw("OIDC: no authorisation data for state : '$state'");
1131  
1132 9 return $auth_data;
1133 }
1134  
1135  
1136 97 ▶sub _get_audience_store {
Subroutine _get_audience_store: called CC=5 SCAR=16.2
1137 97 my ($self, $audience, $key) = @_;
1138  
1139 97 my $store_mode = $self->client->store_mode;
1140  
1141 97 my $audience_store;
1142 97 ▶ if ($store_mode eq 'cache') {
Branch: 1 branch
BranchTrueFalseTotal
if ($store_mode eq 'cache') { } 1 96 97
1143 1 $audience_store = $self->audience_cache->get($audience);
1144 }
1145 else {
1146 96 ▶ my $meth = $store_mode eq 'session' ? 'read_session' : 'read_stash';
Branch: 1 branch
BranchTrueFalseTotal
$store_mode eq 'session' ? : 96 0 96
1147 96 my @path = ('oidc', 'provider', $self->client->provider, 'audience', $audience);
1148 96 $audience_store = $self->$meth(\@path);
1149 }
1150  
1151 97 ▶ $audience_store ||= {};
Condition100%2/2
$audience_store ||= {}
l!l
8512
MC/DC100%1/1
$audience_store ||= {}
$audience_store
Truth table100%2/2
A
A= $audience_store
Aresult
11
00
1152 97 ▶ return $key ? $audience_store->{$key} : $audience_store;
Branch: 1 branch
BranchTrueFalseTotal
$key ? : 97 0 97
1153 }
1154  
1155  
1156 65 ▶sub _set_audience_store {
Subroutine _set_audience_store: called CC=3 SCAR=11.0
1157 65 my ($self, $audience, $key, $value) = @_;
1158  
1159 65 my $store_mode = $self->client->store_mode;
1160  
1161 65 ▶ if ($store_mode eq 'cache') {
Branch: 1 branch
BranchTrueFalseTotal
if ($store_mode eq 'cache') { } 1 64 65
1162 1 my $audience_store = $self->audience_cache->get($audience);
1163 1 $audience_store->{$key} = $value;
1164 1 $self->audience_cache->set($audience, $audience_store);
1165 }
1166 else {
1167 64 ▶ my $meth = $store_mode eq 'session' ? 'write_session' : 'write_stash';
Branch: 1 branch
BranchTrueFalseTotal
$store_mode eq 'session' ? : 61 3 64
1168 64 my @path = ('oidc', 'provider', $self->client->provider, 'audience', $audience, $key);
1169 64 $self->$meth(\@path, $value);
1170 }
1171 }
1172  
1173  
1174 =head2 delete_stored_data()
1175  
1176 $c->oidc->delete_stored_data();
1177  
1178 Delete the tokens and other data stored in the session, stash or cache
1179 depending on your configured C<store_mode> (see L<OIDC::Client::Config>).
1180  
1181 Note that only the data from the current provider is deleted.
1182  
1183 =cut
1184  
1185 3 ▶sub delete_stored_data {
Subroutine delete_stored_data: called CC=3 SCAR=11.0
Pod documented
1186 3 my $self = shift;
1187  
1188 3 my $store_mode = $self->client->store_mode;
1189  
1190 3 ▶ if ($store_mode eq 'cache') {
Branch: 1 branch
BranchTrueFalseTotal
if ($store_mode eq 'cache') { } 1 2 3
1191 1 $self->audience_cache->clear();
1192 }
1193 else {
1194 2 ▶ my $meth = $store_mode eq 'session' ? 'delete_session' : 'delete_stash';
Branch: 1 branch
BranchTrueFalseTotal
$store_mode eq 'session' ? : 2 0 2
1195 2 my @path = ('oidc', 'provider', $self->client->provider);
1196 2 $self->$meth(\@path);
1197 }
1198  
1199 3 return;
1200 }
1201  
1202  
1203 4 __PACKAGE__->meta->make_immutable;
1204  
1205 4 1;
« Client.pm ↑ Summary Client/Role/ClientAuthenticationHelper.pm »