Client/Plugin.pm

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