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