Client.pm

filter: Statementstmt 99.5% 220 / 221 Branchbran 88.8% 96 / 108 Conditioncond 78.3% 47 / 60 MC/DCmcdc 55.0% 22 / 40 Subroutinesub 100% 31 / 31 Podpod 100% 13 / 13 totaltotal 90.7% 429 / 473 CC 79 SCAR 24.5
CC
79
Cov
93%
get_token
26.1
auth_url
17.1
verify_jwt_token
10.0
CRAP 11.5
↑ Summary Client/Plugin.pm »
1 package OIDC::Client;
2 4 ▶use 5.020;
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 List::Util qw(first);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use Crypt::JWT ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use Mojo::URL;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 ▶use OIDC::Client::Error::TokenValidation;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 4 ▶use OIDC::Client::ApiUserAgentBuilder qw(build_api_useragent_from_token_response
Subroutine BEGIN: called CC=1 SCAR=0.0
16 4 build_api_useragent_from_token_value);
17 4 ▶use OIDC::Client::Utils qw(reach_data);
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 4 our $VERSION = '1.08'; # VERSION: generated by Dist::Zilla::Plugin::OurPkgVersion
20  
21 =encoding utf8
22  
23 =head1 NAME
24  
25 OIDC::Client - OpenID Connect Client
26  
27 =head1 SYNOPSIS
28  
29 my $client = OIDC::Client->new(
30 provider => 'my_provider',
31 id => 'my_client_id',
32 secret => 'my_client_secret',
33 provider_metadata => \%provider_metadata,
34 log => $app->log,
35 );
36  
37 # or...
38  
39 my $client = OIDC::Client->new(
40 config => $config_provider,
41 log => $app->log,
42 );
43  
44 my $authorize_url = $client->auth_url();
45 my $token_response = $client->get_token(code => $code);
46 my $claims = $client->verify_jwt_token(token => $token);
47 my $claims = $client->introspect_token(token => $token);
48 my $userinfo = $client->get_userinfo(access_token => $token);
49 my $token_response = $client->exchange_token(token => $token, audience => $audience);
50 my $ua = $client->build_api_useragent();
51 my $logout_url = $client->logout_url();
52  
53 =head1 DESCRIPTION
54  
55 Client module for OpenID Connect and OAuth 2.0 protocols.
56  
57 Use this module directly from a batch or a simple script. For use from within
58 an application, you should instead use one of these framework plugins, which
59 all use this distribution :
60  
61 =over 2
62  
63 =item * L<Mojolicious::Plugin::OIDC>
64  
65 =item * L<Catalyst::Plugin::OIDC>
66  
67 =item * L<Dancer2::Plugin::OIDC>
68  
69 =back
70  
71 =cut
72  
73 4 enum 'StoreMode' => [qw/session stash cache/];
74 4 enum 'ResponseMode' => [qw/query form_post/];
75 4 enum 'GrantType' => [qw/authorization_code client_credentials password refresh_token/];
76 4 enum 'ClientAuthMethod' => [qw/client_secret_basic client_secret_post client_secret_jwt private_key_jwt tls_client_auth none/];
77 4 enum 'TokenValidationMethod' => [qw/jwt introspection/];
78 4 enum 'CodeChallengeMethod' => [qw/plain S256/];
79  
80 4 with 'OIDC::Client::Role::LoggerWrapper';
81 4 with 'OIDC::Client::Role::AttributesManager';
82 4 with 'OIDC::Client::Role::ConfigurationChecker';
83 4 with 'OIDC::Client::Role::ClaimsValidator';
84 4 with 'OIDC::Client::Role::ClientAuthenticationHelper';
85  
86 =head1 METHODS
87  
88 =head2 BUILD
89  
90 Called after the object is created. Makes some basic checks and forces
91 the retrieval of provider metadata and kid keys.
92  
93 =cut
94  
95 71 ▶sub BUILD {
Subroutine BUILD: called CC=2 SCAR=6.9
Pod documented
96 71 my $self = shift;
97  
98 71 $self->_check_configuration();
99 70 $self->_check_client_auth_configuration();
100 68 $self->_check_audiences_configuration();
101 66 $self->_check_cache_configuration();
102  
103 65 $self->provider;
104 64 $self->id;
105  
106 64 $self->provider_metadata;
107 64 ▶ $self->kid_keys if $self->provider_metadata->{jwks_url};
Branch: 1 branch
BranchTrueFalseTotal
if $self->provider_metadata->{"jwks_url"} 8 56 64
108 }
109  
110  
111 =head2 auth_url( %args )
112  
113 my $authorize_url = $client->auth_url(%args);
114  
115 Returns a scalar or a L<Mojo::URL> object containing the initial authorization URL.
116 This is the URL to use to initiate an authorization code flow.
117  
118 The optional parameters are:
119  
120 =over 2
121  
122 =item response_mode
123  
124 Defines how tokens are sent by the provider.
125  
126 Can take one of these values:
127  
128 =over 2
129  
130 =item query
131  
132 Tokens are sent in query parameters.
133  
134 =item form_post
135  
136 Tokens are sent in a POST form.
137  
138 =back
139  
140 =item redirect_uri
141  
142 Redirection URI to which the response will be sent.
143 Can also be specified in the C<signin_redirect_uri> configuration entry.
144  
145 =item state
146  
147 String which is sent during the request to the identity provider
148 and sent back from the IDP along with the Code.
149  
150 =item nonce
151  
152 String which is sent during the request to the identity provider
153 and sent back from the IDP in the returned ID Token.
154  
155 =item scope
156  
157 Specifies the desired scope of the requested token.
158 Must be a string with space separators.
159 Can also be specified in the C<scope> configuration entry.
160  
161 =item audience
162  
163 Specifies the audience/resource that the access token is intended for.
164 Can also be specified in the C<audience> configuration entry.
165  
166 =item code_challenge
167  
168 The PKCE code challenge, derived from a code verifier. When provided,
169 it is added as the C<code_challenge> parameter in the authorization URL.
170  
171 =item code_challenge_method
172  
173 The method used to derive the C<code_challenge> from the code verifier.
174  
175 Accepted values:
176  
177 =over 2
178  
179 =item S256
180  
181 SHA-256 hash of the code verifier (recommended)
182  
183 =item plain
184  
185 The code verifier is used as-is (not recommended)
186  
187 =back
188  
189 =item extra_params
190  
191 Hashref which can be used to send extra query parameters.
192 Can also be specified in the C<authorize_endpoint_extra_params> configuration entry.
193  
194 =item want_mojo_url
195  
196 Defines whether you want this method to return a L<Mojo::URL> object
197 instead of a scalar. False by default.
198  
199 =back
200  
201 =cut
202  
203 6 ▶sub auth_url {
Subroutine auth_url: called CC=17 SCAR=28.4
Pod documented
204 6 my $self = shift;
205 6 my (%params) = validated_hash(
206 \@_,
207 response_mode => { isa => 'ResponseMode', optional => 1 },
208 redirect_uri => { isa => 'Str', optional => 1 },
209 state => { isa => 'Str', optional => 1 },
210 nonce => { isa => 'Str', optional => 1 },
211 scope => { isa => 'Str', optional => 1 },
212 audience => { isa => 'Str', optional => 1 },
213 code_challenge => { isa => 'Str', optional => 1 },
214 code_challenge_method => { isa => 'CodeChallengeMethod', optional => 1 },
215 extra_params => { isa => 'HashRef', optional => 1 },
216 want_mojo_url => { isa => 'Bool', default => 0 },
217 );
218  
219 my $authorize_url = $self->provider_metadata->{authorize_url}
220 6 ▶ or croak("OIDC: authorize url not found in provider metadata");
Branch: 1 branch
BranchTrueFalseTotal
my $authorize_url = $self->provider_metadata->{"authorize_url"} or croak("OIDC: authorize url not found in provider metadata") 5 1 6
221  
222 5 my %args = (
223 response_type => 'code',
224 client_id => $self->id,
225 );
226  
227 5 ▶ if (my $response_mode = $params{response_mode} || $self->authorize_endpoint_response_mode) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $response_mode = $params{"response_mode"} || $self->authorize_endpoint_response_mode) 0 5 5
Condition33%1/3
$params{'response_mode'} || $self->authorize_endpoint_response_mode
l!l&&r!l&&!r
005
MC/DC0%0/2
$params{'response_mode'} || $self->authorize_endpoint_response_mode
$params{'response_mode'} $self->authorize_endpoint_response_mode
Truth table33%1/3
A || B
A= $params{'response_mode'}B= $self->authorize_endpoint_response_mode
ABresult
1X1
011
000
228 0 $args{response_mode} = $response_mode;
229 }
230  
231 5 ▶ if (my $redirect_uri = $params{redirect_uri} || $self->signin_redirect_uri) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $redirect_uri = $params{"redirect_uri"} || $self->signin_redirect_uri) 4 1 5
Condition100%3/3
$params{'redirect_uri'} || $self->signin_redirect_uri
l!l&&r!l&&!r
311
MC/DC100%2/2
$params{'redirect_uri'} || $self->signin_redirect_uri
$params{'redirect_uri'} $self->signin_redirect_uri
Truth table100%3/3
A || B
A= $params{'redirect_uri'}B= $self->signin_redirect_uri
ABresult
1X1
011
000
232 4 $args{redirect_uri} = $redirect_uri;
233 }
234  
235 5 foreach my $param_name (qw/state nonce code_challenge code_challenge_method/) {
236 20 ▶ if (defined $params{$param_name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params{$param_name}) 11 9 20
237 11 $args{$param_name} = $params{$param_name};
238 }
239 }
240  
241 5 ▶ if (my $scope = $params{scope} || $self->scope) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $scope = $params{"scope"} || $self->scope) 4 1 5
Condition100%3/3
$params{'scope'} || $self->scope
l!l&&r!l&&!r
131
MC/DC100%2/2
$params{'scope'} || $self->scope
$params{'scope'} $self->scope
Truth table100%3/3
A || B
A= $params{'scope'}B= $self->scope
ABresult
1X1
011
000
242 4 $args{scope} = $scope;
243 }
244  
245 5 ▶ if (my $audience = $params{audience} || ($self->audience ne $self->id ? $self->audience : undef)) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $audience = $params{"audience"} || ($self->audience ne $self->id ? $self->audience : undef)) 2 3 5
$self->audience ne $self->id ? : 1 3 4
Condition100%3/3
$params{'audience'} || ($self->audience ne $self->id ? $self->audience : undef)
l!l&&r!l&&!r
113
MC/DC100%2/2
$params{'audience'} || ($self->audience ne $self->id ? $self->audience : undef)
$params{'audience'} ($self->audience ne $self->id ? $self->audience : undef)
Truth table100%3/3
A || B
A= $params{'audience'}B= ($self->audience ne $self->id ? $self->audience : undef)
ABresult
1X1
011
000
246 2 $args{audience} = $audience;
247 }
248  
249 5 ▶ if (my $extra_params = $params{extra_params} || $self->authorize_endpoint_extra_params) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $extra_params = $params{"extra_params"} || $self->authorize_endpoint_extra_params) 2 3 5
Condition100%3/3
$params{'extra_params'} || $self->authorize_endpoint_extra_params
l!l&&r!l&&!r
113
MC/DC100%2/2
$params{'extra_params'} || $self->authorize_endpoint_extra_params
$params{'extra_params'} $self->authorize_endpoint_extra_params
Truth table100%3/3
A || B
A= $params{'extra_params'}B= $self->authorize_endpoint_extra_params
ABresult
1X1
011
000
250 2 foreach my $param_name (keys %$extra_params) {
251 3 $args{$param_name} = $extra_params->{$param_name};
252 }
253 }
254  
255 5 my $auth_url = Mojo::URL->new($authorize_url);
256 5 $auth_url->query(%args);
257  
258 5 ▶ return $params{want_mojo_url} ? $auth_url : $auth_url->to_string;
Branch: 1 branch
BranchTrueFalseTotal
$params{'want_mojo_url'} ? : 1 4 5
259 }
260  
261  
262 =head2 get_token( %args )
263  
264 my $token_response = $client->get_token(
265 code => $code,
266 redirect_uri => q{http://yourapp/oidc/callback},
267 );
268  
269 Fetch token(s) from an OAuth2/OIDC provider and returns an
270 L<OIDC::Client::TokenResponse> object.
271  
272 This method doesn't perform any verification on the ID token.
273 Call the L</"verify_jwt_token( %args )"> method to do so.
274  
275 The optional parameters are:
276  
277 =over 2
278  
279 =item grant_type
280  
281 Specifies how the client wants to interact with the identity provider.
282 Accepted here : C<authorization_code>, C<client_credentials>, C<password>
283 or C<refresh_token>.
284 Can also be specified in the C<token_endpoint_grant_type> configuration entry.
285 Default to C<authorization_code>.
286  
287 =item auth_method
288  
289 Specifies how the client authenticates with the identity provider.
290  
291 Supported client authentication methods:
292  
293 =over 2
294  
295 =item *
296  
297 client_secret_basic (default)
298  
299 =item *
300  
301 client_secret_post
302  
303 =item *
304  
305 client_secret_jwt
306  
307 =item *
308  
309 private_key_jwt
310  
311 =item *
312  
313 tls_client_auth
314  
315 =item *
316  
317 none
318  
319 =back
320  
321 Can also be specified in the C<token_endpoint_auth_method> configuration entry
322 or the global C<client_auth_method> configuration entry.
323 Default to C<client_secret_basic>.
324  
325 =item code
326  
327 Authorization-code that is issued beforehand by the identity provider.
328 Used only for the C<authorization_code> grant-type.
329  
330 =item code_verifier
331  
332 The PKCE code verifier. The value must be exactly the same as the one
333 used to generate the C<code_challenge> sent in the authorization request.
334  
335 Optional, but required when the authorization request included a
336 C<code_challenge>.
337  
338 =item redirect_uri
339  
340 Redirection URI to which the response will be sent.
341 Can also be specified in the C<signin_redirect_uri> configuration entry.
342 Used only for the C<authorization_code> grant-type.
343  
344 =item username / password
345  
346 User credentials for authorization
347 Can also be specified in the C<username> and C<password> configuration entries.
348 Used only the for C<password> grant-type.
349  
350 =item audience
351  
352 Specifies the Relaying Party for which the token is intended.
353 Can also be specified in the C<audience> configuration entry.
354 Not used for the C<refresh_token> grant-type.
355  
356 =item scope
357  
358 Specifies the desired scope of the requested token.
359 Must be a string with space separators.
360 Can also be specified in the C<scope> configuration entry.
361 Not used for the C<authorization_code> nor the C<refresh_token> grant-type.
362  
363 =item refresh_token
364  
365 Token that can be used to renew the associated access token before it expires.
366 Used only for the C<refresh_token> grant-type.
367  
368 =item refresh_scope
369  
370 Specifies the desired scope of the requested renewed token.
371 Must be a string with space separators.
372 Used only for the C<refresh_token> grant-type.
373  
374 =back
375  
376 =cut
377  
378 18 ▶sub get_token {
Subroutine get_token: called CC=24 SCAR=32.6
Pod documented
379 18 my $self = shift;
380 18 my (%params) = validated_hash(
381 \@_,
382 grant_type => { isa => 'GrantType', optional => 1 },
383 auth_method => { isa => 'ClientAuthMethod', optional => 1 },
384 code => { isa => 'Str', optional => 1 },
385 code_verifier => { isa => 'Str', optional => 1 },
386 redirect_uri => { isa => 'Str', optional => 1 },
387 username => { isa => 'Str', optional => 1 },
388 password => { isa => 'Str', optional => 1 },
389 audience => { isa => 'Str', optional => 1 },
390 scope => { isa => 'Str', optional => 1 },
391 refresh_token => { isa => 'Str', optional => 1 },
392 refresh_scope => { isa => 'Str', optional => 1 },
393 );
394  
395 18 ▶ my $grant_type = $params{grant_type} || $self->token_endpoint_grant_type;
Condition66%2/3
$params{'grant_type'} || $self->token_endpoint_grant_type
l!l&&r!l&&!r
6120
MC/DC0%0/2
$params{'grant_type'} || $self->token_endpoint_grant_type
$params{'grant_type'} $self->token_endpoint_grant_type
Truth table66%2/3
A || B
A= $params{'grant_type'}B= $self->token_endpoint_grant_type
ABresult
1X1
011
000
396  
397 my $token_url = $self->provider_metadata->{token_url}
398 18 ▶ or croak("OIDC: token url not found in provider metadata");
Branch: 1 branch
BranchTrueFalseTotal
my $token_url = $self->provider_metadata->{"token_url"} or croak("OIDC: token url not found in provider metadata") 17 1 18
399  
400 17 ▶ my $auth_method = $params{auth_method} || $self->token_endpoint_auth_method;
Condition33%1/3
$params{'auth_method'} || $self->token_endpoint_auth_method
l!l&&r!l&&!r
0170
MC/DC0%0/2
$params{'auth_method'} || $self->token_endpoint_auth_method
$params{'auth_method'} $self->token_endpoint_auth_method
Truth table33%1/3
A || B
A= $params{'auth_method'}B= $self->token_endpoint_auth_method
ABresult
1X1
011
000
401 17 my ($headers, $form) = $self->_build_client_auth_arguments($auth_method, $token_url);
402  
403 17 $form->{grant_type} = $grant_type;
404  
405 17 ▶ if ($grant_type eq 'authorization_code') {
Branch: 3 branches
BranchTrueFalseTotal
if ($grant_type eq 'authorization_code') { } 8 9 17
elsif ($grant_type eq 'password') { } 3 6 9
elsif ($grant_type eq 'refresh_token') { } 4 2 6
406 $form->{code} = $params{code}
407 8 ▶ or croak("OIDC: code is missing");
Branch: 1 branch
BranchTrueFalseTotal
$form->{"code"} = $params{"code"} or croak("OIDC: code is missing") 8 0 8
408  
409 8 ▶ if (my $code_verifier = $params{code_verifier}) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $code_verifier = $params{'code_verifier'}) { } 3 5 8
elsif ($self->use_pkce) { } 5 0 5
410 3 $form->{code_verifier} = $code_verifier;
411 }
412 elsif ($self->use_pkce) {
413 5 $self->log_msg(warning => "OIDC: PKCE is enabled but no code_verifier provided in get_token()");
414 }
415  
416 8 ▶ if (my $redirect_uri = $params{redirect_uri} || $self->signin_redirect_uri) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $redirect_uri = $params{"redirect_uri"} || $self->signin_redirect_uri) 8 0 8
Condition66%2/3
$params{'redirect_uri'} || $self->signin_redirect_uri
l!l&&r!l&&!r
440
MC/DC0%0/2
$params{'redirect_uri'} || $self->signin_redirect_uri
$params{'redirect_uri'} $self->signin_redirect_uri
Truth table66%2/3
A || B
A= $params{'redirect_uri'}B= $self->signin_redirect_uri
ABresult
1X1
011
000
417 8 $form->{redirect_uri} = $redirect_uri;
418 }
419 }
420 elsif ($grant_type eq 'password') {
421 3 foreach my $required_field (qw/username password/) {
422 6 ▶ $form->{$required_field} = ($params{$required_field} || $self->$required_field)
Branch: 1 branch
BranchTrueFalseTotal
$form->{$required_field} = $params{$required_field} || $self->$required_field or croak("OIDC: $required_field is missing") 6 0 6
Condition66%2/3
$params{$required_field} || $self->$required_field
l!l&&r!l&&!r
240
MC/DC0%0/2
$params{$required_field} || $self->$required_field
$params{$required_field} $self->$required_field
Truth table66%2/3
A || B
A= $params{$required_field}B= $self->$required_field
ABresult
1X1
011
000
423 or croak("OIDC: $required_field is missing");
424 }
425 }
426 elsif ($grant_type eq 'refresh_token') {
427 $form->{refresh_token} = $params{refresh_token}
428 4 ▶ or croak("OIDC: refresh_token is missing");
Branch: 1 branch
BranchTrueFalseTotal
$form->{"refresh_token"} = $params{"refresh_token"} or croak("OIDC: refresh_token is missing") 4 0 4
429  
430 # Only the 'refresh_scope' method parameter should be considered here. The configuration
431 # parameter is used by Plugin.pm which knows for which audience the token is renewed
432 4 ▶ if (my $scope = $params{refresh_scope}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $scope = $params{"refresh_scope"}) 1 3 4
433 1 $form->{scope} = $scope;
434 }
435 }
436  
437 17 ▶ unless ($grant_type =~ /^(authorization_code|refresh_token)$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($grant_type =~ /^(authorization_code|refresh_token)$/u) 5 12 17
438 5 ▶ if (my $scope = ($params{scope} || $self->scope)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $scope = $params{"scope"} || $self->scope) 5 0 5
Condition66%2/3
$params{'scope'} || $self->scope
l!l&&r!l&&!r
230
MC/DC0%0/2
$params{'scope'} || $self->scope
$params{'scope'} $self->scope
Truth table66%2/3
A || B
A= $params{'scope'}B= $self->scope
ABresult
1X1
011
000
439 5 $form->{scope} = $scope;
440 }
441 }
442  
443 17 ▶ unless ($grant_type eq 'refresh_token') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($grant_type eq "refresh_token") 13 4 17
444 13 ▶ if (my $audience = $params{audience} || ($self->audience ne $self->id ? $self->audience : undef)) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $audience = $params{"audience"} || ($self->audience ne $self->id ? $self->audience : undef)) 7 6 13
$self->audience ne $self->id ? : 5 6 11
Condition100%3/3
$params{'audience'} || ($self->audience ne $self->id ? $self->audience : undef)
l!l&&r!l&&!r
256
MC/DC100%2/2
$params{'audience'} || ($self->audience ne $self->id ? $self->audience : undef)
$params{'audience'} ($self->audience ne $self->id ? $self->audience : undef)
Truth table100%3/3
A || B
A= $params{'audience'}B= ($self->audience ne $self->id ? $self->audience : undef)
ABresult
1X1
011
000
445 7 $form->{audience} = $audience;
446 }
447 }
448  
449 17 $self->log_msg(debug => 'OIDC: calling provider to get token from ' . $token_url);
450  
451 17 my $res = $self->user_agent->post($token_url, $headers, form => $form)->result;
452  
453 17 return $self->token_response_parser->parse($res);
454 }
455  
456  
457 =head2 verify_jwt_token( %args )
458  
459 my $claims = $client->verify_jwt_token(
460 token => $token,
461 expected_audience => $audience,
462 expected_nonce => $nonce,
463 );
464  
465 Checks the structure, claims and signature of the JWT token.
466 Throws an L<OIDC::Client::Error::TokenValidation> exception if an error occurs.
467 Otherwise, returns the claims (hashref).
468  
469 This method automatically manages a JWK key rotation. If a JWK key error
470 is detected during token verification, the JWK keys in memory are refreshed
471 by retrieving them again from the JWKS URL. The token is checked again, and if
472 an error occurs, an L<OIDC::Client::Error::TokenValidation> exception is thrown.
473  
474 The following claims are validated :
475  
476 =over 2
477  
478 =item "exp" (Expiration Time) claim
479  
480 Must be present and valid (in the future).
481  
482 =item "iat" (Issued At) claim
483  
484 Must be present and valid (not in the future).
485  
486 =item "nbf" (Not Before) claim
487  
488 Must be valid (not in the future) if present.
489  
490 =item "iss" (Issuer) claim
491  
492 Must be the issuer recorded in the provider metadata.
493  
494 =item "aud" (Audience) claim
495  
496 Must be the expected audience (see parameters beelow).
497  
498 =item "sub" (Subject) claim
499  
500 Must be the expected subject defined in the parameters (see beelow).
501  
502 =back
503  
504 The L<Crypt::JWT::decode_jwt()|https://metacpan.org/pod/Crypt::JWT#decode_jwt>
505 function is used to validate and decode a JWT token. Remember that you can change
506 the options transmitted to this function (see L<OIDC::Client::Config>).
507  
508 The parameters are:
509  
510 =over 2
511  
512 =item token
513  
514 The JWT token to validate.
515 Required.
516  
517 =item expected_audience
518  
519 If the token is not intended for the expected audience, an exception is thrown.
520 Default to the C<audience> configuration entry or otherwise the client id.
521  
522 =item expected_authorized_party
523  
524 If the C<azp> claim value is not the expected authorized_party, an exception is thrown
525 unless the JWT token has no C<azp> claim and the C<no_authorized_party_accepted>
526 parameter is true.
527 Optional.
528  
529 =item no_authorized_party_accepted
530  
531 When the C<expected_authorized_party> parameter is defined, prevents an exception
532 from being thrown if the JWT token does not contain an C<azp> claim.
533 Default to false.
534  
535 =item expected_subject
536  
537 If the C<sub> claim value is not the expected subject, an exception is thrown.
538 Optional.
539  
540 =item expected_nonce
541  
542 If the C<nonce> claim value is not the expected nonce, an exception is thrown
543 unless the JWT token has no C<nonce> claim and the C<no_nonce_accepted> parameter is true.
544 Optional.
545  
546 =item no_nonce_accepted
547  
548 When the C<expected_nonce> parameter is defined, prevents an exception from being
549 thrown if the JWT token does not contain a C<nonce> claim.
550 Default to false.
551  
552 =item want_header
553  
554 my ($header, $claims) = $client->verify_jwt_token(
555 token => $token,
556 want_header => 1,
557 );
558  
559 Defines whether you want the decoded header to be returned by this method.
560 False by default.
561  
562 =back
563  
564 =cut
565  
566 31 ▶sub verify_jwt_token {
Subroutine verify_jwt_token: called CC=10 SCAR=23.0
Pod documented
567 31 my $self = shift;
568 31 my (%params) = validated_hash(
569 \@_,
570 token => { isa => 'Str', optional => 0 },
571 expected_audience => { isa => 'Str', optional => 1 },
572 expected_authorized_party => { isa => 'Maybe[Str]', optional => 1 },
573 no_authorized_party_accepted => { isa => 'Bool', default => 0 },
574 expected_subject => { isa => 'Str', optional => 1 },
575 expected_nonce => { isa => 'Str', optional => 1 },
576 no_nonce_accepted => { isa => 'Bool', default => 0 },
577 max_token_age => { isa => 'Int', optional => 1 },
578 want_header => { isa => 'Bool', default => 0 },
579 );
580  
581 # checks the signature and the timestamps
582 31 my ($header, $claims) = $self->_decode_token($params{token});
583  
584 # checks the issuer
585 27 $self->_validate_issuer($claims->{iss});
586  
587 # checks the audience
588 25 $self->_validate_audience($claims->{aud}, $params{expected_audience});
589  
590 # checks the authorized party
591 22 ▶ if (exists $params{expected_authorized_party}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params{"expected_authorized_party"}) 7 15 22
592 7 ▶ unless (!exists $claims->{azp} && $params{no_authorized_party_accepted}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (not exists $claims->{"azp"} and $params{"no_authorized_party_accepted"}) 5 2 7
Condition100%3/3
not exists $claims->{"azp"} and $params{"no_authorized_party_accepted"}
!ll&&!rl&&r
322
MC/DC100%2/2
not exists $claims->{"azp"} and $params{"no_authorized_party_accepted"}
not exists $claims->{"azp"} $params{"no_authorized_party_accepted"}
Truth table100%3/3
not(A) and B
A= not exists $claims->{"azp"}B= $params{"no_authorized_party_accepted"}
ABresult
0X0
100
111
593 5 $self->_validate_authorized_party($claims->{azp}, $params{expected_authorized_party});
594 }
595 }
596  
597 # checks the subject
598 19 ▶ if (my $expected_subject = $params{expected_subject}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $expected_subject = $params{"expected_subject"}) 3 16 19
599 3 $self->_validate_subject($claims->{sub}, $expected_subject);
600 }
601  
602 # checks the nonce
603 17 ▶ if (my $expected_nonce = $params{expected_nonce}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $expected_nonce = $params{"expected_nonce"}) 5 12 17
604 5 ▶ unless (!exists $claims->{nonce} && $params{no_nonce_accepted}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (not exists $claims->{"nonce"} and $params{"no_nonce_accepted"}) 4 1 5
Condition100%3/3
not exists $claims->{"nonce"} and $params{"no_nonce_accepted"}
!ll&&!rl&&r
311
MC/DC100%2/2
not exists $claims->{"nonce"} and $params{"no_nonce_accepted"}
not exists $claims->{"nonce"} $params{"no_nonce_accepted"}
Truth table100%3/3
not(A) and B
A= not exists $claims->{"nonce"}B= $params{"no_nonce_accepted"}
ABresult
0X0
100
111
605 4 $self->_validate_nonce($claims->{nonce}, $expected_nonce);
606 }
607 }
608  
609 # checks that the token has not been issued too far away from the current time
610 15 ▶ if (my $max_token_age = $params{max_token_age}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $max_token_age = $params{"max_token_age"}) 3 12 15
611 3 $self->_validate_age($claims->{iat}, $max_token_age);
612 }
613  
614 14 ▶ return $params{want_header} ? ($header, $claims) : $claims;
Branch: 1 branch
BranchTrueFalseTotal
$params{'want_header'} ? : 2 12 14
615 }
616  
617 =head2 verify_token( %args )
618  
619 This method is DEPRECATED! Instead, use L</"verify_jwt_token( %args )">.
620  
621 =cut
622  
623 1 ▶sub verify_token {
Subroutine verify_token: called CC=1 SCAR=0.0
Pod documented
624 1 my ($self, %params) = @_;
625 1 warnings::warnif('deprecated',
626 'OIDC::Client::verify_token() is deprecated in favor of OIDC::Client::verify_jwt_token()');
627 1 return $self->verify_jwt_token(%params);
628 }
629  
630  
631 =head2 introspect_token( %args )
632  
633 my $claims = $client->introspect_token(
634 token => $token,
635 token_type_hint => 'access_token',
636 );
637  
638 Allows a Resource Server to validate a token and obtain its metadata by calling the provider's
639 introspection endpoint. Typically used when the access token is opaque (not a JWT).
640  
641 Throws an L<OIDC::Client::Error::Provider> exception if an error is returned by the provider
642 or an L<OIDC::Client::Error::TokenValidation> exception if a validation error occurs.
643 Otherwise, returns the claims.
644  
645 The parameters are:
646  
647 =over 2
648  
649 =item token
650  
651 The token to validate.
652 Required.
653  
654 =item token_type_hint
655  
656 Hint about the type of the token submitted for introspection.
657  
658 =item auth_method
659  
660 Specifies how the client authenticates with the identity provider.
661  
662 Supported client authentication methods:
663  
664 =over 2
665  
666 =item *
667  
668 client_secret_basic (default)
669  
670 =item *
671  
672 client_secret_post
673  
674 =item *
675  
676 client_secret_jwt
677  
678 =item *
679  
680 private_key_jwt
681  
682 =item *
683  
684 tls_client_auth
685  
686 =item *
687  
688 none
689  
690 =back
691  
692 Can also be specified in the C<introspection_endpoint_auth_method> configuration entry
693 or the global C<client_auth_method> configuration entry.
694 Default to C<client_secret_basic>.
695  
696 =item expected_audience
697  
698 If the C<aud> claim is present in the provider response of the introspection endpoint,
699 its value must match the expected audience, otherwise an exception is thrown.
700 Default to the C<audience> configuration entry or the client id.
701  
702 =back
703  
704 =cut
705  
706 8 ▶sub introspect_token {
Subroutine introspect_token: called CC=7 SCAR=19.5
Pod documented
707 8 my $self = shift;
708 8 my (%params) = validated_hash(
709 \@_,
710 token => { isa => 'Str', optional => 0 },
711 token_type_hint => { isa => enum([qw/access_token refresh_token/]), optional => 1 },
712 auth_method => { isa => 'ClientAuthMethod', optional => 1 },
713 expected_audience => { isa => 'Str', optional => 1 },
714 );
715  
716 my $introspection_url = $self->provider_metadata->{introspection_url}
717 8 ▶ or croak("OIDC: introspection_url not found in provider metadata");
Branch: 1 branch
BranchTrueFalseTotal
my $introspection_url = $self->provider_metadata->{"introspection_url"} or croak("OIDC: introspection_url not found in provider metadata") 8 0 8
718  
719 8 ▶ my $auth_method = $params{auth_method} || $self->introspection_endpoint_auth_method;
Condition33%1/3
$params{'auth_method'} || $self->introspection_endpoint_auth_method
l!l&&r!l&&!r
080
MC/DC0%0/2
$params{'auth_method'} || $self->introspection_endpoint_auth_method
$params{'auth_method'} $self->introspection_endpoint_auth_method
Truth table33%1/3
A || B
A= $params{'auth_method'}B= $self->introspection_endpoint_auth_method
ABresult
1X1
011
000
720 8 my ($headers, $form) = $self->_build_client_auth_arguments($auth_method, $introspection_url);
721  
722 8 $form->{token} = $params{token};
723  
724 8 ▶ if (my $token_type_hint = $params{token_type_hint}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $token_type_hint = $params{"token_type_hint"}) 1 7 8
725 1 $form->{token_type_hint} = $token_type_hint;
726 }
727  
728 8 $self->log_msg(debug => 'OIDC: calling provider to introspect token');
729 8 my $res = $self->user_agent->post($introspection_url, $headers, form => $form)->result;
730  
731 8 my $claims = $self->response_parser->parse($res);
732  
733 $claims->{active}
734 8 ▶ or OIDC::Client::Error::TokenValidation->throw("OIDC: inactive token");
Branch: 1 branch
BranchTrueFalseTotal
$claims->{"active"} or "OIDC::Client::Error::TokenValidation"->throw("OIDC: inactive token") 6 2 8
735  
736 6 ▶ if (exists $claims->{iss}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $claims->{"iss"}) 3 3 6
737 3 $self->_validate_issuer($claims->{iss});
738 }
739  
740 5 ▶ if (exists $claims->{aud}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $claims->{"aud"}) 2 3 5
741 2 $self->_validate_audience($claims->{aud}, $params{expected_audience});
742 }
743  
744 4 return $claims;
745 }
746  
747  
748 =head2 get_userinfo( %args )
749  
750 my $userinfo = $client->get_userinfo(
751 access_token => $stored_token->token,
752 token_type => $stored_token->token_type,
753 );
754  
755 Get and returns the user information from an OAuth2/OIDC provider.
756  
757 The parameters are:
758  
759 =over 2
760  
761 =item access_token
762  
763 Content of the valid access token obtained through OIDC authentication.
764  
765 =item token_type
766  
767 Optional, default to C<Bearer>.
768  
769 =back
770  
771 =cut
772  
773 2 ▶sub get_userinfo {
Subroutine get_userinfo: called CC=3 SCAR=11.1
Pod documented
774 2 my $self = shift;
775 2 my (%params) = validated_hash(
776 \@_,
777 access_token => { isa => 'Str', optional => 0 },
778 token_type => { isa => 'Maybe[Str]', optional => 1 },
779 );
780  
781 my $userinfo_url = $self->provider_metadata->{userinfo_url}
782 2 ▶ or croak("OIDC: userinfo_url not found in provider metadata");
Branch: 1 branch
BranchTrueFalseTotal
my $userinfo_url = $self->provider_metadata->{"userinfo_url"} or croak("OIDC: userinfo_url not found in provider metadata") 2 0 2
783  
784 2 ▶ my $token_type = $params{token_type} || $self->default_token_type;
Condition66%2/3
$params{'token_type'} || $self->default_token_type
l!l&&r!l&&!r
110
MC/DC0%0/2
$params{'token_type'} || $self->default_token_type
$params{'token_type'} $self->default_token_type
Truth table66%2/3
A || B
A= $params{'token_type'}B= $self->default_token_type
ABresult
1X1
011
000
785  
786 2 my $authorization = "$token_type $params{access_token}";
787  
788 2 $self->log_msg(debug => 'OIDC: calling provider to fetch userinfo');
789  
790 2 my $res = $self->user_agent->get($userinfo_url, { Authorization => $authorization })
791 ->result;
792  
793 2 return $self->response_parser->parse($res);
794 }
795  
796  
797 =head2 get_audience_for_alias( $audience_alias )
798  
799 my $audience = $client->get_audience_for_alias($audience_alias);
800  
801 Returns the audience for an alias that has been configured in the configuration
802 entry C<audience_alias>/C<$audience_alias>/C<audience>.
803  
804 =cut
805  
806 2 ▶sub get_audience_for_alias {
Subroutine get_audience_for_alias: called CC=3 SCAR=11.0
Pod documented
807 2 my $self = shift;
808 2 my ($alias) = pos_validated_list(\@_, { isa => 'Str', optional => 0 });
809  
810 2 ▶ my $audience_alias = $self->audience_alias
Branch: 1 branch
BranchTrueFalseTotal
my $audience_alias = $self->audience_alias or return 2 0 2
811 or return;
812  
813 2 ▶ my $audience_infos = $audience_alias->{$alias}
Branch: 1 branch
BranchTrueFalseTotal
my $audience_infos = $audience_alias->{$alias} or return 1 1 2
814 or return;
815  
816 1 return $audience_infos->{audience};
817 }
818  
819  
820 =head2 get_scope_for_audience( $audience )
821  
822 my $scope = $client->get_scope_for_audience($audience);
823  
824 Returns the scope for an audience that has been configured in the configuration
825 entry C<audience_alias>/C<$audience_alias>/C<scope>.
826  
827 =cut
828  
829 6 ▶sub get_scope_for_audience {
Subroutine get_scope_for_audience: called CC=3 SCAR=11.0
Pod documented
830 6 my $self = shift;
831 6 my ($audience) = pos_validated_list(\@_, { isa => 'Str', optional => 0 });
832  
833 6 ▶ my $audience_alias = $self->audience_alias
Branch: 1 branch
BranchTrueFalseTotal
my $audience_alias = $self->audience_alias or return 5 1 6
834 or return;
835  
836 5 ▶ my $audience_infos = first { $_->{audience} eq $audience } values %$audience_alias
Subroutine __ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
my $audience_infos = &first(sub { $_->{"audience"} eq $audience; } , values %$audience_alias) or return 5 0 5
837 or return;
838  
839 5 return $audience_infos->{scope};
840 }
841  
842  
843 =head2 exchange_token( %args )
844  
845 my $exchanged_token_response = $client->exchange_token(
846 token => $token,
847 audience => $audience,
848 );
849  
850 Exchanges an access token, obtained through OIDC authentication, for another access
851 token that is accepted by a different OIDC application.
852  
853 Returns a L<OIDC::Client::TokenResponse> object.
854  
855 The parameters are:
856  
857 =over 2
858  
859 =item token
860  
861 Content of the valid access token obtained through OIDC authentication.
862  
863 =item audience
864  
865 Audience of the target application.
866  
867 =item scope
868  
869 Specifies the desired scope of the requested token.
870 Must be a string with space separators.
871 Optional.
872  
873 =item auth_method
874  
875 Specifies how the client authenticates with the identity provider.
876  
877 Supported client authentication methods:
878  
879 =over 2
880  
881 =item *
882  
883 client_secret_basic (default)
884  
885 =item *
886  
887 client_secret_post
888  
889 =item *
890  
891 client_secret_jwt
892  
893 =item *
894  
895 private_key_jwt
896  
897 =item *
898  
899 tls_client_auth
900  
901 =item *
902  
903 none
904  
905 =back
906  
907 Can also be specified in the C<token_endpoint_auth_method> configuration entry
908 or the global C<client_auth_method> configuration entry.
909 Default to C<client_secret_basic>.
910  
911 =back
912  
913 =cut
914  
915 5 ▶sub exchange_token {
Subroutine exchange_token: called CC=5 SCAR=16.2
Pod documented
916 5 my $self = shift;
917 5 my (%params) = validated_hash(
918 \@_,
919 token => { isa => 'Str', optional => 0 },
920 audience => { isa => 'Str', optional => 0 },
921 scope => { isa => 'Str', optional => 1 },
922 auth_method => { isa => 'ClientAuthMethod', optional => 1 },
923 );
924  
925 my $token_url = $self->provider_metadata->{token_url}
926 5 ▶ or croak("OIDC: token url not found in provider metadata");
Branch: 1 branch
BranchTrueFalseTotal
my $token_url = $self->provider_metadata->{"token_url"} or croak("OIDC: token url not found in provider metadata") 5 0 5
927  
928 5 ▶ my $auth_method = $params{auth_method} || $self->token_endpoint_auth_method;
Condition33%1/3
$params{'auth_method'} || $self->token_endpoint_auth_method
l!l&&r!l&&!r
050
MC/DC0%0/2
$params{'auth_method'} || $self->token_endpoint_auth_method
$params{'auth_method'} $self->token_endpoint_auth_method
Truth table33%1/3
A || B
A= $params{'auth_method'}B= $self->token_endpoint_auth_method
ABresult
1X1
011
000
929 5 my ($headers, $form) = $self->_build_client_auth_arguments($auth_method, $token_url);
930  
931 5 $form->{audience} = $params{audience};
932 5 $form->{grant_type} = 'urn:ietf:params:oauth:grant-type:token-exchange';
933 5 $form->{subject_token} = $params{token};
934 5 $form->{subject_token_type} = 'urn:ietf:params:oauth:token-type:access_token';
935  
936 5 ▶ if (my $scope = ($params{scope} || $self->get_scope_for_audience($params{audience}))) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $scope = $params{"scope"} || $self->get_scope_for_audience($params{"audience"})) 2 3 5
Condition100%3/3
$params{'scope'} || $self->get_scope_for_audience($params{'audience'})
l!l&&r!l&&!r
113
MC/DC100%2/2
$params{'scope'} || $self->get_scope_for_audience($params{'audience'})
$params{'scope'} $self->get_scope_for_audience($params{'audience'})
Truth table100%3/3
A || B
A= $params{'scope'}B= $self->get_scope_for_audience($params{'audience'})
ABresult
1X1
011
000
937 2 $form->{scope} = $scope;
938 }
939  
940 5 $self->log_msg(debug => 'OIDC: calling provider to exchange token');
941  
942 5 my $res = $self->user_agent->post($token_url, $headers, form => $form)->result;
943  
944 5 return $self->token_response_parser->parse($res);
945 }
946  
947  
948 =head2 build_api_useragent( %args )
949  
950 my $ua = $client->build_api_useragent();
951  
952 Invokes the L</"get_token( %args )"> method and builds a web client (L<Mojo::UserAgent>
953 object) that will have the given access token in the authorization header for each request.
954  
955 This method can be useful if the client is configured for a password grant
956 or a client credentials grant and you simply want to build a web client to call
957 an API protected by OAuth2.
958  
959 =cut
960  
961 2 ▶sub build_api_useragent {
Subroutine build_api_useragent: called CC=2 SCAR=6.9
Pod documented
962 2 my $self = shift;
963 2 my (%params) = validated_hash(
964 \@_,
965 token => { isa => 'Str', optional => 1 }, # DEPRECATED!
966 token_type => { isa => 'Maybe[Str]', optional => 1 }, # DEPRECATED!
967 );
968  
969 2 ▶ if (my $token_value = $params{token}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $token_value = $params{"token"}) 1 1 2
970 1 warnings::warnif('deprecated',
971 q{$oidc_client->build_api_useragent(token => $token_value) is deprecated, use } .
972 q{OIDC::Client::ApiUserAgentBuilder::build_api_useragent_from_token_value($token_value) function instead});
973 1 my $token_type = $params{token_type};
974 1 return build_api_useragent_from_token_value($token_value, $token_type);
975 }
976  
977 1 my $token_response = $self->get_token();
978 1 return build_api_useragent_from_token_response($token_response);
979 }
980  
981  
982 =head2 logout_url( %args )
983  
984 my $logout_url = $client->logout_url(%args);
985  
986 URL allowing the end-user to logout.
987 Returns a scalar or a L<Mojo::URL> object which contain the logout URL.
988  
989 The optional parameters are:
990  
991 =over 2
992  
993 =item id_token
994  
995 Content of the end-user's ID token.
996  
997 =item state
998  
999 String to add to the logout request that will be included when redirecting
1000 to the C<post_logout_redirect_uri>.
1001  
1002 =item post_logout_redirect_uri
1003  
1004 Redirect URL value that indicates where to redirect the user after logout.
1005 Can also be specified in the C<post_logout_redirect_uri> configuration entry.
1006  
1007 =item extra_params
1008  
1009 Hashref which can be used to send extra query parameters.
1010  
1011 =item want_mojo_url
1012  
1013 Defines whether you want this method to return a L<Mojo::URL> object
1014 instead of a scalar. False by default.
1015  
1016 =back
1017  
1018 =cut
1019  
1020 4 ▶sub logout_url {
Subroutine logout_url: called CC=10 SCAR=23.0
Pod documented
1021 4 my $self = shift;
1022 4 my (%params) = validated_hash(
1023 \@_,
1024 id_token => { isa => 'Str', optional => 1 },
1025 state => { isa => 'Str', optional => 1 },
1026 post_logout_redirect_uri => { isa => 'Str', optional => 1 },
1027 extra_params => { isa => 'HashRef', optional => 1 },
1028 want_mojo_url => { isa => 'Bool', default => 0 },
1029 );
1030  
1031 my $end_session_url = $self->provider_metadata->{end_session_url}
1032 4 ▶ or croak("OIDC: end_session_url not found in provider metadata");
Branch: 1 branch
BranchTrueFalseTotal
my $end_session_url = $self->provider_metadata->{"end_session_url"} or croak("OIDC: end_session_url not found in provider metadata") 3 1 4
1033  
1034 3 my %args = (
1035 client_id => $self->id,
1036 );
1037  
1038 3 ▶ if (my $id_token = $params{id_token}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $id_token = $params{"id_token"}) 2 1 3
1039 2 $args{id_token_hint} = $id_token;
1040 }
1041  
1042 3 ▶ if (defined $params{state}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params{"state"}) 1 2 3
1043 1 $args{state} = $params{state};
1044 }
1045  
1046 3 ▶ if (my $redirect_uri = $params{post_logout_redirect_uri} || $self->post_logout_redirect_uri) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $redirect_uri = $params{"post_logout_redirect_uri"} || $self->post_logout_redirect_uri) 2 1 3
Condition100%3/3
$params{'post_logout_redirect_uri'} || $self->post_logout_redirect_uri
l!l&&r!l&&!r
111
MC/DC100%2/2
$params{'post_logout_redirect_uri'} || $self->post_logout_redirect_uri
$params{'post_logout_redirect_uri'} $self->post_logout_redirect_uri
Truth table100%3/3
A || B
A= $params{'post_logout_redirect_uri'}B= $self->post_logout_redirect_uri
ABresult
1X1
011
000
1047 2 $args{post_logout_redirect_uri} = $redirect_uri;
1048 }
1049  
1050 3 ▶ if (my $extra_params = $params{extra_params} || $self->logout_extra_params) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $extra_params = $params{"extra_params"} || $self->logout_extra_params) 2 1 3
Condition100%3/3
$params{'extra_params'} || $self->logout_extra_params
l!l&&r!l&&!r
111
MC/DC100%2/2
$params{'extra_params'} || $self->logout_extra_params
$params{'extra_params'} $self->logout_extra_params
Truth table100%3/3
A || B
A= $params{'extra_params'}B= $self->logout_extra_params
ABresult
1X1
011
000
1051 2 foreach my $param_name (keys %$extra_params) {
1052 2 $args{$param_name} = $extra_params->{$param_name};
1053 }
1054 }
1055  
1056 3 my $logout_url = Mojo::URL->new($end_session_url);
1057 3 $logout_url->query(%args);
1058  
1059 3 ▶ return $params{want_mojo_url} ? $logout_url : $logout_url->to_string;
Branch: 1 branch
BranchTrueFalseTotal
$params{'want_mojo_url'} ? : 1 2 3
1060 }
1061  
1062  
1063 =head2 get_claim_value( %args )
1064  
1065 my $claim_value = $client->get_claim_value(name => 'login', claims => $claims);
1066  
1067 Returns the value of a claim by its configured name.
1068  
1069 The hash parameters are:
1070  
1071 =over 2
1072  
1073 =item name
1074  
1075 Name of the claim configured in the C<claim_mapping> section.
1076  
1077 =item claims
1078  
1079 Hashref of the claims.
1080  
1081 =item optional
1082  
1083 Defines whether the wanted claim must exist in the claims.
1084  
1085 =back
1086  
1087 =cut
1088  
1089 15 ▶sub get_claim_value {
Subroutine get_claim_value: called CC=2 SCAR=6.9
Pod documented
1090 15 my $self = shift;
1091 15 my (%params) = validated_hash(
1092 \@_,
1093 name => { isa => 'Str', optional => 0 },
1094 claims => { isa => 'HashRef', optional => 0 },
1095 optional => { isa => 'Bool', default => 0 },
1096 );
1097  
1098 my $claim_key = $self->claim_mapping->{$params{name}}
1099 15 ▶ or croak("OIDC: no claim key in config for name '$params{name}'");
Branch: 1 branch
BranchTrueFalseTotal
my $claim_key = $self->claim_mapping->{$params{"name"}} or croak(qq[OIDC: no claim key in config for name '$params{"name"}']) 14 1 15
1100  
1101 14 my @path = split /\./, $claim_key;
1102  
1103 14 return reach_data($params{claims}, \@path, $params{optional});
1104 }
1105  
1106  
1107 33 ▶sub _decode_token {
Subroutine _decode_token: called CC=1 SCAR=0.0
1108 33 my ($self, $token, $has_already_update_keys) = @_;
1109  
1110 33 ▶ return try {
Subroutine __ANON__: called CC=1 SCAR=0.0
1111 33 Crypt::JWT::decode_jwt(%{ $self->jwt_decoding_options },
1112 token => $token,
1113 kid_keys => $self->kid_keys,
1114 decode_payload => 1,
1115 decode_header => 1);
1116 }
1117 6 ▶ catch {
Subroutine __ANON__: called CC=3 SCAR=11.0
1118 6 my $e = $_;
1119 6 ▶ if ($e =~ /kid_keys/i && !$has_already_update_keys) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e =~ /kid_keys/iu and not $has_already_update_keys) { } 2 4 6
Condition100%3/3
$e =~ /kid_keys/iu and not $has_already_update_keys
!ll&&!rl&&r
312
MC/DC100%2/2
$e =~ /kid_keys/iu and not $has_already_update_keys
$e =~ /kid_keys/iu not $has_already_update_keys
Truth table100%3/3
A and not(B)
A= $e =~ /kid_keys/iuB= not $has_already_update_keys
ABresult
0X0
100
111
1120 2 $self->log_msg(info => "OIDC: couldn't decode the token. Let's retry after updating the keys : $e");
1121 2 $self->_clear_kid_keys();
1122 2 return $self->_decode_token($token, 1);
1123 }
1124 else {
1125 4 OIDC::Client::Error::TokenValidation->throw("$e");
1126 }
1127 33 };
1128 }
1129  
1130  
1131 4 __PACKAGE__->meta->make_immutable;
1132  
1133  
1134 4 1;
1135  
1136 =head1 CONFIGURATION
1137  
1138 To use this module directly via a batch or script, here is the section to add
1139 to your configuration file:
1140  
1141 oidc_client:
1142 provider: provider_name
1143 id: my-app-id
1144 secret: xxxxxxxxx
1145 audience: other_app_name
1146 well_known_url: https://yourprovider.com/oauth2/.well-known/openid-configuration
1147 scope: roles
1148 token_endpoint_grant_type: password
1149 username: TECHXXXX
1150 password: xxxxxxxx
1151  
1152 This is an example, see the detailed possibilities in L<OIDC::Client::Config>.
1153  
1154 =head1 SAMPLES
1155  
1156 Here are some samples by category. Although you will have to adapt them to your needs,
1157 they should be a good starting point.
1158  
1159 =head2 API call
1160  
1161 To make an API call to another application :
1162  
1163 my $oidc_client = OIDC::Client->new(
1164 log => $self->log,
1165 config => $self->config->{oidc_client},
1166 );
1167  
1168 # Retrieving a web client (Mojo::UserAgent object)
1169 my $ua = $oidc_client->build_api_useragent();
1170  
1171 # Usual call to the API
1172 my $res = $ua->get($url)->result;
1173  
1174 Here, there is no token exchange because the audience has been configured
1175 to get the access token intended for the other application.
1176  
1177 =head2 API call with token expiration management
1178  
1179 If you need to manage token expiration because your script runs longer than
1180 the token lifetime, here is an example using Moose attributes :
1181  
1182 has 'oidc_client' => (
1183 is => 'ro',
1184 isa => 'OIDC::Client',
1185 lazy => 1,
1186 builder => '_build_oidc_client',
1187 );
1188  
1189 has 'access_token' => (
1190 is => 'ro',
1191 isa => 'OIDC::Client::AccessToken',
1192 lazy => 1,
1193 builder => '_build_access_token',
1194 clearer => '_clear_access_token',
1195 );
1196  
1197 has 'api_useragent' => (
1198 is => 'ro',
1199 isa => 'Mojo::UserAgent',
1200 lazy => 1,
1201 builder => '_build_api_useragent',
1202 clearer => '_clear_api_useragent',
1203 );
1204  
1205 sub _build_oidc_client {
1206 my $self = shift;
1207  
1208 return OIDC::Client->new(
1209 log => $self->log,
1210 config => $self->config->{oidc_client},
1211 );
1212 }
1213  
1214 sub _build_access_token {
1215 my $self = shift;
1216  
1217 my $token_response = $self->oidc_client->get_token();
1218  
1219 return build_access_token_from_token_response($token_response);
1220 }
1221  
1222 sub _build_api_useragent {
1223 my $self = shift;
1224  
1225 return build_api_useragent_from_access_token($self->access_token);
1226 }
1227  
1228 sub do_stuff {
1229 my $self = shift;
1230  
1231 if ($self->access_token->has_expired($self->oidc_client->expiration_leeway)) {
1232 $self->_clear_access_token();
1233 $self->_clear_api_useragent();
1234 }
1235  
1236 my $res = $self->api_useragent->get($self->config->{api_url})->result;
1237 ...
1238 }
1239  
1240 =head1 AUTHOR
1241  
1242 Sébastien Mourlhou
1243  
1244 =head1 COPYRIGHT AND LICENSE
1245  
1246 Copyright (C) Sébastien Mourlhou
1247  
1248 This program is free software, you can redistribute it and/or modify it under the terms of the Artistic License version 2.0.
1249  
1250 =head1 SEE ALSO
1251  
1252 =over 2
1253  
1254 =item * L<Mojolicious::Plugin::OIDC>
1255  
1256 =item * L<Catalyst::Plugin::OIDC>
1257  
1258 =item * L<Dancer2::Plugin::OIDC>
1259  
1260 =back
1261  
1262 =cut
↑ Summary Client/Plugin.pm »