| 1 | package Plack::Middleware::OIDC; | ||||||||||||||||||||||||||||||||||||||
| 2 | 9 | ▶ | use 5.020; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 3 | 9 | ▶ | use utf8; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 4 | 9 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 5 | 9 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 6 | 9 | ▶ | use parent 'Plack::Middleware'; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||
| 8 | 9 | ▶ | use feature 'signatures'; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 9 | 9 | ▶ | no warnings 'experimental::signatures'; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||
| 11 | 9 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 12 | 9 | ▶ | use Clone qw(clone); | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 13 | 9 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 14 | 9 | ▶ | use Try::Tiny; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 15 | 9 | ▶ | use URI (); | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 16 | 9 | ▶ | use OIDC::Client; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 17 | 9 | ▶ | use OIDC::Client::Plugin; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 18 | 9 | ▶ | use OIDC::Client::Error; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 19 | 9 | ▶ | use Plack::Middleware::OIDC::Redirect; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 20 | 9 | ▶ | use Plack::Middleware::OIDC::Logger; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 21 | 9 | ▶ | use Plack::Request; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 22 | 9 | ▶ | use Plack::Util::Accessor qw(config log); | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||
| 24 | 9 | our $VERSION = '1.01'; # VERSION: generated by Dist::Zilla::Plugin::OurPkgVersion | |||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||
| 26 | =encoding utf8 | ||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||
| 28 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||
| 30 | Plack::Middleware::OIDC - OpenID Connect integration for PSGI/Plack applications | ||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||
| 32 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||
| 34 | use Plack::Builder; | ||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||
| 36 | my $oidc_config = { | ||||||||||||||||||||||||||||||||||||||
| 37 | default_provider => 'my_provider', | ||||||||||||||||||||||||||||||||||||||
| 38 | authentication_error_path => '/error', | ||||||||||||||||||||||||||||||||||||||
| 39 | provider => { | ||||||||||||||||||||||||||||||||||||||
| 40 | my_provider => { | ||||||||||||||||||||||||||||||||||||||
| 41 | id => 'my-app-id', | ||||||||||||||||||||||||||||||||||||||
| 42 | secret => 'xxxxxxxxx', | ||||||||||||||||||||||||||||||||||||||
| 43 | well_known_url => 'https://yourprovider.com/.well-known/openid-configuration', | ||||||||||||||||||||||||||||||||||||||
| 44 | signin_redirect_path => '/oidc/login/callback', | ||||||||||||||||||||||||||||||||||||||
| 45 | logout_redirect_path => '/oidc/logout/callback', | ||||||||||||||||||||||||||||||||||||||
| 46 | scope => 'openid profile roles email', | ||||||||||||||||||||||||||||||||||||||
| 47 | }, | ||||||||||||||||||||||||||||||||||||||
| 48 | }, | ||||||||||||||||||||||||||||||||||||||
| 49 | }; | ||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||
| 51 | builder { | ||||||||||||||||||||||||||||||||||||||
| 52 | enable 'Session'; | ||||||||||||||||||||||||||||||||||||||
| 53 | enable 'OIDC', config => $oidc_config; | ||||||||||||||||||||||||||||||||||||||
| 54 | $app; | ||||||||||||||||||||||||||||||||||||||
| 55 | }; | ||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||
| 57 | # in the wrapped application : | ||||||||||||||||||||||||||||||||||||||
| 58 | sub { | ||||||||||||||||||||||||||||||||||||||
| 59 | my $env = shift; | ||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||
| 61 | if (my $identity = $env->{oidc}->get_valid_identity()) { | ||||||||||||||||||||||||||||||||||||||
| 62 | # authenticated : $identity->subject, $identity->claims, ... | ||||||||||||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||||||||||||
| 64 | else { | ||||||||||||||||||||||||||||||||||||||
| 65 | # throws an internal exception which Plack::Middleware::OIDC | ||||||||||||||||||||||||||||||||||||||
| 66 | # catches and turns into a real 302 response | ||||||||||||||||||||||||||||||||||||||
| 67 | $env->{oidc}->redirect_to_authorize(); | ||||||||||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||||||||
| 69 | }; | ||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||
| 71 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||
| 73 | This middleware makes it easy to integrate the OpenID Connect protocol | ||||||||||||||||||||||||||||||||||||||
| 74 | into a PSGI/Plack application. It enables applications to act as an | ||||||||||||||||||||||||||||||||||||||
| 75 | OpenID Connect Relying Party (RP) for user authentication and as an | ||||||||||||||||||||||||||||||||||||||
| 76 | OAuth 2.0 Resource Server by validating Access Tokens issued by an | ||||||||||||||||||||||||||||||||||||||
| 77 | external Authorization Server or OpenID Provider. It does not implement | ||||||||||||||||||||||||||||||||||||||
| 78 | an OpenID Provider. | ||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||
| 80 | It is built on the L<OIDC::Client> distribution and is the PSGI/Plack | ||||||||||||||||||||||||||||||||||||||
| 81 | counterpart of L<Mojolicious::Plugin::OIDC>, L<Catalyst::Plugin::OIDC> | ||||||||||||||||||||||||||||||||||||||
| 82 | and L<Dancer2::Plugin::OIDC>. It is meant for applications that do not | ||||||||||||||||||||||||||||||||||||||
| 83 | use one of these frameworks (L<Kelp> framework is compatible with this | ||||||||||||||||||||||||||||||||||||||
| 84 | middleware from v2.23, hand-written PSGI applications, ...). If you use | ||||||||||||||||||||||||||||||||||||||
| 85 | one of the three frameworks above, prefer its dedicated plugin, which | ||||||||||||||||||||||||||||||||||||||
| 86 | is better integrated. | ||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||
| 88 | This documentation focuses on what is specific to Plack. The methods | ||||||||||||||||||||||||||||||||||||||
| 89 | available on the objects exposed to your application are documented in | ||||||||||||||||||||||||||||||||||||||
| 90 | L<OIDC::Client::Plugin>, and the configuration entries in | ||||||||||||||||||||||||||||||||||||||
| 91 | L<OIDC::Client::Config>. | ||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||
| 93 | =head1 HOW IT WORKS | ||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||
| 95 | =head2 Configuration | ||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||
| 97 | Plack has no built-in configuration mechanism, so the configuration is | ||||||||||||||||||||||||||||||||||||||
| 98 | not looked up in a file: the hashref (same structure as for the other | ||||||||||||||||||||||||||||||||||||||
| 99 | plugins, see L<OIDC::Client::Config>) is passed explicitly to C<enable>, | ||||||||||||||||||||||||||||||||||||||
| 100 | under the C<config> key. How you load it (L<Config::Any>, YAML, JSON, | ||||||||||||||||||||||||||||||||||||||
| 101 | environment variables, a plain hash in F<app.psgi>...) is up to you. | ||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||
| 103 | =head2 Routing | ||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||
| 105 | At startup, for each configured provider, the middleware reads the | ||||||||||||||||||||||||||||||||||||||
| 106 | C<signin_redirect_path> and C<logout_redirect_path> configuration entries | ||||||||||||||||||||||||||||||||||||||
| 107 | and handles the matching requests itself, without passing them on to | ||||||||||||||||||||||||||||||||||||||
| 108 | your application. There is nothing to declare in your application for | ||||||||||||||||||||||||||||||||||||||
| 109 | these two callback routes. | ||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||
| 111 | These paths are compared with C<PATH_INFO>, so they are relative to the | ||||||||||||||||||||||||||||||||||||||
| 112 | point where the application is mounted (for example under | ||||||||||||||||||||||||||||||||||||||
| 113 | L<Plack::App::URLMap>). The C<redirect_uri> sent to the provider is built | ||||||||||||||||||||||||||||||||||||||
| 114 | from the base URL of the request, which includes C<SCRIPT_NAME>, so both | ||||||||||||||||||||||||||||||||||||||
| 115 | stay consistent. | ||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||
| 117 | Two providers cannot use the same callback path: this is detected at | ||||||||||||||||||||||||||||||||||||||
| 118 | startup and raises an exception. | ||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||
| 120 | =head2 Access to the OIDC::Client::Plugin instances | ||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||
| 122 | For every other request, the middleware creates one L<OIDC::Client::Plugin> | ||||||||||||||||||||||||||||||||||||||
| 123 | instance per configured provider and exposes them to the downstream | ||||||||||||||||||||||||||||||||||||||
| 124 | application through C<$env> : | ||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||
| 126 | =over 2 | ||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||
| 128 | =item C<< $env->{oidc} >> | ||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||
| 130 | Instance for the default provider (see L</"Default provider">). | ||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||
| 132 | =item C<< $env->{"oidc.$provider_name"} >> | ||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||
| 134 | Instance dedicated to the provider named C<$provider_name>. When | ||||||||||||||||||||||||||||||||||||||
| 135 | C<$provider_name> is the default provider, it is the very same object as | ||||||||||||||||||||||||||||||||||||||
| 136 | C<< $env->{oidc} >>. | ||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||
| 138 | =back | ||||||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||||
| 140 | This is the equivalent of the C<< $c->oidc >> and | ||||||||||||||||||||||||||||||||||||||
| 141 | C<< $c->oidc('my_provider') >> helpers of the other plugins, and the | ||||||||||||||||||||||||||||||||||||||
| 142 | key was deliberately named C<oidc> to stay aligned with them. The | ||||||||||||||||||||||||||||||||||||||
| 143 | instances are created on every request (no I/O is involved, they only | ||||||||||||||||||||||||||||||||||||||
| 144 | wrap the request, the session and the already built L<OIDC::Client> | ||||||||||||||||||||||||||||||||||||||
| 145 | objects). | ||||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||||
| 147 | =head2 Default provider | ||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||
| 149 | If only one provider is configured, it is the default one. Otherwise, | ||||||||||||||||||||||||||||||||||||||
| 150 | the C<default_provider> configuration entry is used. If several providers | ||||||||||||||||||||||||||||||||||||||
| 151 | are configured and C<default_provider> is not set, C<< $env->{oidc} >> | ||||||||||||||||||||||||||||||||||||||
| 152 | is not populated and only the C<< $env->{"oidc.$provider_name"} >> keys | ||||||||||||||||||||||||||||||||||||||
| 153 | are available. | ||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||
| 155 | =head2 Redirects from application code | ||||||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||||||
| 157 | Methods like C<redirect_to_authorize()> and C<redirect_to_logout()> of | ||||||||||||||||||||||||||||||||||||||
| 158 | L<OIDC::Client::Plugin> are meant to redirect the browser immediately. | ||||||||||||||||||||||||||||||||||||||
| 159 | A PSGI application has no mutable response object that application code | ||||||||||||||||||||||||||||||||||||||
| 160 | could alter at any point, so these methods throw an internal | ||||||||||||||||||||||||||||||||||||||
| 161 | L<Plack::Middleware::OIDC::Redirect> exception instead. The middleware | ||||||||||||||||||||||||||||||||||||||
| 162 | catches it around the call to your application and turns it into a real | ||||||||||||||||||||||||||||||||||||||
| 163 | C<302> response. From your application's point of view, calling one of | ||||||||||||||||||||||||||||||||||||||
| 164 | these methods therefore ends the request with a redirect, as with the | ||||||||||||||||||||||||||||||||||||||
| 165 | other plugins. | ||||||||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||||||||
| 167 | This means these methods must not be called inside an C<eval> or C<try> | ||||||||||||||||||||||||||||||||||||||
| 168 | block that would swallow the exception without rethrowing it. See also | ||||||||||||||||||||||||||||||||||||||
| 169 | L</LIMITATIONS>. | ||||||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||||||
| 171 | If your application is built on a framework that catches the exceptions | ||||||||||||||||||||||||||||||||||||||
| 172 | thrown by your code, the redirection only reaches this middleware if the | ||||||||||||||||||||||||||||||||||||||
| 173 | framework rethrows the exceptions implementing an C<as_psgi()> method (a | ||||||||||||||||||||||||||||||||||||||
| 174 | convention also used by L<Plack::Middleware::HTTPExceptions>). The exception | ||||||||||||||||||||||||||||||||||||||
| 175 | implements it : see L<Plack::Middleware::OIDC::Redirect/as_psgi()>. | ||||||||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||||||||
| 177 | =head2 Session and storage modes | ||||||||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||||||||
| 179 | By default (C<store_mode> set to C<session>, see L<OIDC::Client::Config>), | ||||||||||||||||||||||||||||||||||||||
| 180 | the tokens, the identity and the transient data of the authorization | ||||||||||||||||||||||||||||||||||||||
| 181 | flow (state, nonce, PKCE code verifier) are stored in | ||||||||||||||||||||||||||||||||||||||
| 182 | C<< $env->{'psgix.session'} >>. A session middleware such as | ||||||||||||||||||||||||||||||||||||||
| 183 | L<Plack::Middleware::Session> must therefore be enabled B<before> | ||||||||||||||||||||||||||||||||||||||
| 184 | C<Plack::Middleware::OIDC> : | ||||||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||||||
| 186 | builder { | ||||||||||||||||||||||||||||||||||||||
| 187 | enable 'Session'; | ||||||||||||||||||||||||||||||||||||||
| 188 | enable 'OIDC', config => $oidc_config; | ||||||||||||||||||||||||||||||||||||||
| 189 | $app; | ||||||||||||||||||||||||||||||||||||||
| 190 | }; | ||||||||||||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||||||||||
| 192 | If no session is found, an exception is raised. | ||||||||||||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||||||||||||
| 194 | With the C<stash> storage mode, nothing is kept between requests: the | ||||||||||||||||||||||||||||||||||||||
| 195 | stash is a hashref scoped to the current request (C<< $env->{'oidc.stash'} >>). | ||||||||||||||||||||||||||||||||||||||
| 196 | This is the mode to use for a pure Resource Server (see | ||||||||||||||||||||||||||||||||||||||
| 197 | L</"Protecting a web service">), which does not need any session | ||||||||||||||||||||||||||||||||||||||
| 198 | middleware. | ||||||||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||||||||
| 200 | =head2 Behind a reverse proxy | ||||||||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||||||||
| 202 | The URLs given to the provider (C<redirect_uri>, post logout redirect) are | ||||||||||||||||||||||||||||||||||||||
| 203 | built from the request itself. If your application runs behind a reverse | ||||||||||||||||||||||||||||||||||||||
| 204 | proxy which terminates TLS, enable L<Plack::Middleware::ReverseProxy> | ||||||||||||||||||||||||||||||||||||||
| 205 | (or an equivalent) B<before> this middleware so that the scheme and the | ||||||||||||||||||||||||||||||||||||||
| 206 | host are the public ones. | ||||||||||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||||||||||
| 208 | =head2 Authentication errors | ||||||||||||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||||||||||||
| 210 | If an L<OIDC::Client::Error> exception occurs while handling the login | ||||||||||||||||||||||||||||||||||||||
| 211 | callback (invalid state, error returned by the provider, token | ||||||||||||||||||||||||||||||||||||||
| 212 | verification failure...): | ||||||||||||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||||||||||||
| 214 | =over 2 | ||||||||||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||||||||||
| 216 | =item * | ||||||||||||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||||||||||||
| 218 | if the C<authentication_error_path> configuration entry is set, the | ||||||||||||||||||||||||||||||||||||||
| 219 | browser is redirected to this path and the error message is stored in | ||||||||||||||||||||||||||||||||||||||
| 220 | the session, so that the error page can display it (see | ||||||||||||||||||||||||||||||||||||||
| 221 | L</"Reading an authentication error message">) ; | ||||||||||||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||||||||||||
| 223 | =item * | ||||||||||||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||||||||||||
| 225 | otherwise, the exception is rethrown. | ||||||||||||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||||||||||||
| 227 | =back | ||||||||||||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||||||||||||
| 229 | The message behaves like a framework's flash : it is readable in | ||||||||||||||||||||||||||||||||||||||
| 230 | C<< $env->{'psgix.session'}{oidc_flash}{error_message} >> during the | ||||||||||||||||||||||||||||||||||||||
| 231 | request that follows the redirect, and only during that request. The | ||||||||||||||||||||||||||||||||||||||
| 232 | middleware removes it automatically, there is nothing to clean up. This | ||||||||||||||||||||||||||||||||||||||
| 233 | requires a session, so the message is not available with the C<stash> | ||||||||||||||||||||||||||||||||||||||
| 234 | storage mode alone. | ||||||||||||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||||||||||||
| 236 | =head1 CONFIGURATION | ||||||||||||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||||||||||||
| 238 | =head2 config | ||||||||||||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||||||||||||
| 240 | Hashref with the same structure as the configuration of the other | ||||||||||||||||||||||||||||||||||||||
| 241 | framework plugins. See L<OIDC::Client::Config> for the complete | ||||||||||||||||||||||||||||||||||||||
| 242 | reference. | ||||||||||||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||||||||||||
| 244 | =head2 log | ||||||||||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||||||||
| 246 | Optional logger object, which must implement the C<debug>, C<info>, | ||||||||||||||||||||||||||||||||||||||
| 247 | C<error> and C<warning> (or C<warn>) methods. Defaults to an instance of | ||||||||||||||||||||||||||||||||||||||
| 248 | L<Plack::Middleware::OIDC::Logger>, which writes to | ||||||||||||||||||||||||||||||||||||||
| 249 | C<< $env->{'psgix.logger'} >> when available and to C<STDERR> otherwise. | ||||||||||||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||||||||||||
| 251 | =head1 SAMPLES | ||||||||||||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||||||||||||
| 253 | =head2 Protecting routes | ||||||||||||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||||||||||||
| 255 | A small inline middleware, placed after C<OIDC>, can require an | ||||||||||||||||||||||||||||||||||||||
| 256 | authenticated user for the whole application except some public paths : | ||||||||||||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||||||||||||
| 258 | builder { | ||||||||||||||||||||||||||||||||||||||
| 259 | enable 'Session'; | ||||||||||||||||||||||||||||||||||||||
| 260 | enable 'OIDC', config => $oidc_config; | ||||||||||||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||||||||||||
| 262 | enable sub { | ||||||||||||||||||||||||||||||||||||||
| 263 | my $next = shift; | ||||||||||||||||||||||||||||||||||||||
| 264 | return sub { | ||||||||||||||||||||||||||||||||||||||
| 265 | my $env = shift; | ||||||||||||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||||||||||||
| 267 | return $next->($env) if $env->{PATH_INFO} =~ m{^/(?:public|assets)/}; | ||||||||||||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||||||||||||
| 269 | $env->{oidc}->get_valid_identity() | ||||||||||||||||||||||||||||||||||||||
| 270 | or $env->{oidc}->redirect_to_authorize(); | ||||||||||||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||||||||||||
| 272 | return $next->($env); | ||||||||||||||||||||||||||||||||||||||
| 273 | }; | ||||||||||||||||||||||||||||||||||||||
| 274 | }; | ||||||||||||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||||||||||||
| 276 | $app; | ||||||||||||||||||||||||||||||||||||||
| 277 | }; | ||||||||||||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||||||||||||
| 279 | Once the user is authenticated and sent back to the originally requested | ||||||||||||||||||||||||||||||||||||||
| 280 | URL, the identity is available in any layer of the application : | ||||||||||||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||||||||||||
| 282 | my $identity = $env->{oidc}->get_valid_identity(); | ||||||||||||||||||||||||||||||||||||||
| 283 | my $login = $identity->subject; | ||||||||||||||||||||||||||||||||||||||
| 284 | my $email = $identity->claims->{email}; | ||||||||||||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||||||||||||
| 286 | =head2 Several providers | ||||||||||||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||||||||||||
| 288 | my $oidc_config = { | ||||||||||||||||||||||||||||||||||||||
| 289 | default_provider => 'my_provider', | ||||||||||||||||||||||||||||||||||||||
| 290 | provider => { | ||||||||||||||||||||||||||||||||||||||
| 291 | my_provider => { | ||||||||||||||||||||||||||||||||||||||
| 292 | ... | ||||||||||||||||||||||||||||||||||||||
| 293 | signin_redirect_path => '/oidc/login/callback', | ||||||||||||||||||||||||||||||||||||||
| 294 | logout_redirect_path => '/oidc/logout/callback', | ||||||||||||||||||||||||||||||||||||||
| 295 | }, | ||||||||||||||||||||||||||||||||||||||
| 296 | my_other_provider => { | ||||||||||||||||||||||||||||||||||||||
| 297 | ... | ||||||||||||||||||||||||||||||||||||||
| 298 | signin_redirect_path => '/oidc/other/login/callback', | ||||||||||||||||||||||||||||||||||||||
| 299 | logout_redirect_path => '/oidc/other/logout/callback', | ||||||||||||||||||||||||||||||||||||||
| 300 | }, | ||||||||||||||||||||||||||||||||||||||
| 301 | }, | ||||||||||||||||||||||||||||||||||||||
| 302 | }; | ||||||||||||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||||||||||||
| 304 | # default provider | ||||||||||||||||||||||||||||||||||||||
| 305 | $env->{oidc}->get_valid_identity(); | ||||||||||||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||||||||||||
| 307 | # explicit provider | ||||||||||||||||||||||||||||||||||||||
| 308 | $env->{'oidc.my_other_provider'}->get_valid_identity(); | ||||||||||||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||||||||||||
| 310 | Each provider needs its own callback paths. | ||||||||||||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||||||||||||
| 312 | =head2 Logging out | ||||||||||||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||||||||||||
| 314 | if ($env->{PATH_INFO} eq '/logout') { | ||||||||||||||||||||||||||||||||||||||
| 315 | $env->{oidc}->redirect_to_logout(target_url => 'https://myapp.example.com/'); | ||||||||||||||||||||||||||||||||||||||
| 316 | } | ||||||||||||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||||||||||||
| 318 | The browser is sent to the provider, then back to the | ||||||||||||||||||||||||||||||||||||||
| 319 | C<logout_redirect_path> route handled by the middleware, which removes | ||||||||||||||||||||||||||||||||||||||
| 320 | the data stored for the user and redirects to C<target_url> (the base | ||||||||||||||||||||||||||||||||||||||
| 321 | URL of the application by default). | ||||||||||||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||||||||||||
| 323 | =head2 Protecting a web service | ||||||||||||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||||||||||||
| 325 | For an application acting as an OAuth 2.0 Resource Server, use the | ||||||||||||||||||||||||||||||||||||||
| 326 | C<stash> storage mode, so that no session is required, and verify the | ||||||||||||||||||||||||||||||||||||||
| 327 | access token received in the C<Authorization> header : | ||||||||||||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||||||||||||
| 329 | my $oidc_config = { | ||||||||||||||||||||||||||||||||||||||
| 330 | provider => { | ||||||||||||||||||||||||||||||||||||||
| 331 | my_provider => { | ||||||||||||||||||||||||||||||||||||||
| 332 | id => 'my-api-id', | ||||||||||||||||||||||||||||||||||||||
| 333 | secret => 'xxxxxxxxx', | ||||||||||||||||||||||||||||||||||||||
| 334 | well_known_url => 'https://yourprovider.com/.well-known/openid-configuration', | ||||||||||||||||||||||||||||||||||||||
| 335 | store_mode => 'stash', | ||||||||||||||||||||||||||||||||||||||
| 336 | token_validation_method => 'jwt', | ||||||||||||||||||||||||||||||||||||||
| 337 | }, | ||||||||||||||||||||||||||||||||||||||
| 338 | }, | ||||||||||||||||||||||||||||||||||||||
| 339 | }; | ||||||||||||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||||||||||||
| 341 | builder { | ||||||||||||||||||||||||||||||||||||||
| 342 | enable 'OIDC', config => $oidc_config; | ||||||||||||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||||||||||||
| 344 | sub { | ||||||||||||||||||||||||||||||||||||||
| 345 | my $env = shift; | ||||||||||||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||||||||||||
| 347 | my $access_token = try { | ||||||||||||||||||||||||||||||||||||||
| 348 | $env->{oidc}->verify_token(); | ||||||||||||||||||||||||||||||||||||||
| 349 | } | ||||||||||||||||||||||||||||||||||||||
| 350 | catch { | ||||||||||||||||||||||||||||||||||||||
| 351 | $log->warn("Token validation : $_"); | ||||||||||||||||||||||||||||||||||||||
| 352 | return; | ||||||||||||||||||||||||||||||||||||||
| 353 | } or return [401, ['Content-Type' => 'application/json', | ||||||||||||||||||||||||||||||||||||||
| 354 | 'WWW-Authenticate' => 'Bearer'], | ||||||||||||||||||||||||||||||||||||||
| 355 | ['{"error":"invalid_token"}']]; | ||||||||||||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||||||||||||
| 357 | $access_token->has_scope('expected_scope') | ||||||||||||||||||||||||||||||||||||||
| 358 | or return [403, ['Content-Type' => 'application/json'], | ||||||||||||||||||||||||||||||||||||||
| 359 | ['{"error":"insufficient_scopes"}']]; | ||||||||||||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||||||||||||
| 361 | ... | ||||||||||||||||||||||||||||||||||||||
| 362 | }; | ||||||||||||||||||||||||||||||||||||||
| 363 | }; | ||||||||||||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||||||||||||
| 365 | When the C<token_validation_method> is C<jwt>, the provider's signing keys | ||||||||||||||||||||||||||||||||||||||
| 366 | are fetched when the application starts. | ||||||||||||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||||||||||||
| 368 | =head2 Calling another web service | ||||||||||||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||||||||||||
| 370 | To call another application with the security context of the current | ||||||||||||||||||||||||||||||||||||||
| 371 | request, the access token is automatically added to every request made | ||||||||||||||||||||||||||||||||||||||
| 372 | with the web client returned by C<build_api_useragent()> : | ||||||||||||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||||||||||||
| 374 | my $ua = $env->{oidc}->build_api_useragent('other_app_audience_alias'); | ||||||||||||||||||||||||||||||||||||||
| 375 | my $res = $ua->get('https://other-app.example.com/api/things')->result; | ||||||||||||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||||||||||||
| 377 | See L<OIDC::Client::Plugin/build_api_useragent> for the optional audience | ||||||||||||||||||||||||||||||||||||||
| 378 | alias. | ||||||||||||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||||||||||||
| 380 | =head2 Reading an authentication error message | ||||||||||||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||||||||||||
| 382 | When C<authentication_error_path> is configured, the error page can | ||||||||||||||||||||||||||||||||||||||
| 383 | read the message stored by the middleware. It is only available during | ||||||||||||||||||||||||||||||||||||||
| 384 | the request that follows the redirect : | ||||||||||||||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||||||||||||
| 386 | if ($env->{PATH_INFO} eq '/error') { | ||||||||||||||||||||||||||||||||||||||
| 387 | my $message = $env->{'psgix.session'}{oidc_flash}{error_message} | ||||||||||||||||||||||||||||||||||||||
| 388 | || 'Unknown error'; | ||||||||||||||||||||||||||||||||||||||
| 389 | ... | ||||||||||||||||||||||||||||||||||||||
| 390 | } | ||||||||||||||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||||||||||||
| 392 | C<error_message> is diagnostic information and must not be rendered | ||||||||||||||||||||||||||||||||||||||
| 393 | verbatim to an untrusted user. | ||||||||||||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||||||||||||
| 395 | =head1 DIFFERENCES WITH THE OTHER FRAMEWORK PLUGINS | ||||||||||||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||||||||||||
| 397 | =over 2 | ||||||||||||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||||||||||||
| 399 | =item * | ||||||||||||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||||||||||||
| 401 | The configuration is passed to C<enable> instead of being read from the | ||||||||||||||||||||||||||||||||||||||
| 402 | framework's configuration. | ||||||||||||||||||||||||||||||||||||||
| 403 | |||||||||||||||||||||||||||||||||||||||
| 404 | =item * | ||||||||||||||||||||||||||||||||||||||
| 405 | |||||||||||||||||||||||||||||||||||||||
| 406 | The C<< $c->oidc >> helper becomes C<< $env->{oidc} >> and | ||||||||||||||||||||||||||||||||||||||
| 407 | C<< $c->oidc($provider) >> becomes C<< $env->{"oidc.$provider"} >>. | ||||||||||||||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||||||||||||
| 409 | =item * | ||||||||||||||||||||||||||||||||||||||
| 410 | |||||||||||||||||||||||||||||||||||||||
| 411 | The callback routes are handled directly by the middleware, there is no | ||||||||||||||||||||||||||||||||||||||
| 412 | route to declare. | ||||||||||||||||||||||||||||||||||||||
| 413 | |||||||||||||||||||||||||||||||||||||||
| 414 | =item * | ||||||||||||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||||||||||||
| 416 | Redirections requested by the application are implemented with an | ||||||||||||||||||||||||||||||||||||||
| 417 | exception (see L</"Redirects from application code">). | ||||||||||||||||||||||||||||||||||||||
| 418 | |||||||||||||||||||||||||||||||||||||||
| 419 | =item * | ||||||||||||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||||||||||||
| 421 | There is no C<flash> helper : the authentication error message is read | ||||||||||||||||||||||||||||||||||||||
| 422 | directly from the session, under the C<oidc_flash> key (see | ||||||||||||||||||||||||||||||||||||||
| 423 | L</"Authentication errors">). | ||||||||||||||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||||||||||||
| 425 | =back | ||||||||||||||||||||||||||||||||||||||
| 426 | |||||||||||||||||||||||||||||||||||||||
| 427 | =head1 LIMITATIONS | ||||||||||||||||||||||||||||||||||||||
| 428 | |||||||||||||||||||||||||||||||||||||||
| 429 | =over 2 | ||||||||||||||||||||||||||||||||||||||
| 430 | |||||||||||||||||||||||||||||||||||||||
| 431 | =item * | ||||||||||||||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||||||||||||
| 433 | The redirect exception is only caught while the application runs | ||||||||||||||||||||||||||||||||||||||
| 434 | synchronously. Calling C<redirect_to_authorize()> or C<redirect_to_logout()> | ||||||||||||||||||||||||||||||||||||||
| 435 | from within the callback of a delayed or streaming PSGI response is not | ||||||||||||||||||||||||||||||||||||||
| 436 | supported. | ||||||||||||||||||||||||||||||||||||||
| 437 | |||||||||||||||||||||||||||||||||||||||
| 438 | =item * | ||||||||||||||||||||||||||||||||||||||
| 439 | |||||||||||||||||||||||||||||||||||||||
| 440 | Only the C<signin_redirect_path> and C<logout_redirect_path> entries are | ||||||||||||||||||||||||||||||||||||||
| 441 | taken into account for the callback routes, like in the other plugins. | ||||||||||||||||||||||||||||||||||||||
| 442 | The C<signin_redirect_uri> and C<post_logout_redirect_uri> entries are | ||||||||||||||||||||||||||||||||||||||
| 443 | not supported. | ||||||||||||||||||||||||||||||||||||||
| 444 | |||||||||||||||||||||||||||||||||||||||
| 445 | =item * | ||||||||||||||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||||||||||||
| 447 | A session middleware must be enabled before this middleware when the | ||||||||||||||||||||||||||||||||||||||
| 448 | C<session> storage mode is used. The exception is raised on every request | ||||||||||||||||||||||||||||||||||||||
| 449 | going through the middleware, not only on the OIDC routes. | ||||||||||||||||||||||||||||||||||||||
| 450 | |||||||||||||||||||||||||||||||||||||||
| 451 | =item * | ||||||||||||||||||||||||||||||||||||||
| 452 | |||||||||||||||||||||||||||||||||||||||
| 453 | The authentication error message is available during the first request | ||||||||||||||||||||||||||||||||||||||
| 454 | going through the middleware after the redirect, whichever it is (for | ||||||||||||||||||||||||||||||||||||||
| 455 | example a request for a favicon or a parallel request made by the browser | ||||||||||||||||||||||||||||||||||||||
| 456 | would use it up). | ||||||||||||||||||||||||||||||||||||||
| 457 | |||||||||||||||||||||||||||||||||||||||
| 458 | =back | ||||||||||||||||||||||||||||||||||||||
| 459 | |||||||||||||||||||||||||||||||||||||||
| 460 | =head1 METHODS | ||||||||||||||||||||||||||||||||||||||
| 461 | |||||||||||||||||||||||||||||||||||||||
| 462 | =head2 prepare_app() | ||||||||||||||||||||||||||||||||||||||
| 463 | |||||||||||||||||||||||||||||||||||||||
| 464 | Called once when the middleware is built. Depending on the configuration, | ||||||||||||||||||||||||||||||||||||||
| 465 | creates and keeps in memory one L<OIDC::Client> (stateless object) per | ||||||||||||||||||||||||||||||||||||||
| 466 | configured provider, and builds the internal table used to intercept the | ||||||||||||||||||||||||||||||||||||||
| 467 | callback routes. | ||||||||||||||||||||||||||||||||||||||
| 468 | |||||||||||||||||||||||||||||||||||||||
| 469 | =cut | ||||||||||||||||||||||||||||||||||||||
| 470 | |||||||||||||||||||||||||||||||||||||||
| 471 | 13 | ▶ | sub prepare_app ($self) { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
prepare_app: called CC=12 SCAR=25.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 472 | |||||||||||||||||||||||||||||||||||||||
| 473 | 13 | ▶ | my $config = $self->config || {}; | ||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->config || {}
MC/DC0%0/1
$self->config || {}
$self->config
Truth table50%1/2
A
A= $self->config
| |||||||||||||||||||||||||||||||||||||||
| 474 | |||||||||||||||||||||||||||||||||||||||
| 475 | 13 | $self->{_oidc_config} = $config; | |||||||||||||||||||||||||||||||||||||
| 476 | 13 | ▶ | $self->{_oidc_log} = $self->log || Plack::Middleware::OIDC::Logger->new(); | ||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$self->log || 'Plack::Middleware::OIDC::Logger'->new
MC/DC0%0/2
$self->log || 'Plack::Middleware::OIDC::Logger'->new
$self->log
'Plack::Middleware::OIDC::Logger'->new
Truth table33%1/3
A || B
A= $self->logB= 'Plack::Middleware::OIDC::Logger'->new
| |||||||||||||||||||||||||||||||||||||||
| 477 | |||||||||||||||||||||||||||||||||||||||
| 478 | 13 | my (%client_by_provider, %path_map); | |||||||||||||||||||||||||||||||||||||
| 479 | |||||||||||||||||||||||||||||||||||||||
| 480 | 13 | ▶ | foreach my $provider_name (keys %{ $config->{provider} || {} }) { | ||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$config->{'provider'} || {}
MC/DC0%0/1
$config->{'provider'} || {}
$config->{'provider'}
Truth table50%1/2
A
A= $config->{'provider'}
| |||||||||||||||||||||||||||||||||||||||
| 481 | 17 | ▶ | my $config_provider = clone($config->{provider}{$provider_name} || {}); | ||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$config->{'provider'}{$provider_name} || {}
MC/DC0%0/1
$config->{'provider'}{$provider_name} || {}
$config->{'provider'}{$provider_name}
Truth table50%1/2
A
A= $config->{'provider'}{$provider_name}
| |||||||||||||||||||||||||||||||||||||||
| 482 | 17 | $config_provider->{provider} = $provider_name; | |||||||||||||||||||||||||||||||||||||
| 483 | |||||||||||||||||||||||||||||||||||||||
| 484 | $client_by_provider{$provider_name} = OIDC::Client->new( | ||||||||||||||||||||||||||||||||||||||
| 485 | config => $config_provider, | ||||||||||||||||||||||||||||||||||||||
| 486 | log => $self->{_oidc_log}, | ||||||||||||||||||||||||||||||||||||||
| 487 | 17 | ); | |||||||||||||||||||||||||||||||||||||
| 488 | |||||||||||||||||||||||||||||||||||||||
| 489 | 17 | foreach my $action_type (qw/ login logout /) { | |||||||||||||||||||||||||||||||||||||
| 490 | my $path = $action_type eq 'login' ? $config_provider->{signin_redirect_path} | ||||||||||||||||||||||||||||||||||||||
| 491 | : $config_provider->{logout_redirect_path} | ||||||||||||||||||||||||||||||||||||||
| 492 | 33 | ▶ | or next; | ||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||
| 493 | |||||||||||||||||||||||||||||||||||||||
| 494 | 29 | ▶ | if (my $existing_route = $path_map{$path}) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 495 | 2 | croak("OIDC: path '$path' is already used by provider '$existing_route->{provider}' " | |||||||||||||||||||||||||||||||||||||
| 496 | . "($existing_route->{action}), cannot also use it for provider '$provider_name' " | ||||||||||||||||||||||||||||||||||||||
| 497 | . "($action_type)"); | ||||||||||||||||||||||||||||||||||||||
| 498 | } | ||||||||||||||||||||||||||||||||||||||
| 499 | |||||||||||||||||||||||||||||||||||||||
| 500 | 27 | $path_map{$path} = { provider => $provider_name, action => $action_type }; | |||||||||||||||||||||||||||||||||||||
| 501 | } | ||||||||||||||||||||||||||||||||||||||
| 502 | } | ||||||||||||||||||||||||||||||||||||||
| 503 | |||||||||||||||||||||||||||||||||||||||
| 504 | 11 | ▶ | if ($config->{default_provider} && !$client_by_provider{$config->{default_provider}}) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$config->{"default_provider"} and not $client_by_provider{$config->{"default_provider"}}
MC/DC100%2/2
$config->{"default_provider"} and not $client_by_provider{$config->{"default_provider"}}
$config->{"default_provider"}
not $client_by_provider{$config->{"default_provider"}}
Truth table100%3/3
A and not(B)
A= $config->{"default_provider"}B= not $client_by_provider{$config->{"default_provider"}}
| |||||||||||||||||||||||||||||||||||||||
| 505 | 1 | croak("OIDC: default_provider '$config->{default_provider}' is not configured"); | |||||||||||||||||||||||||||||||||||||
| 506 | } | ||||||||||||||||||||||||||||||||||||||
| 507 | |||||||||||||||||||||||||||||||||||||||
| 508 | 10 | $self->{_oidc_client_by_provider} = \%client_by_provider; | |||||||||||||||||||||||||||||||||||||
| 509 | 10 | $self->{_oidc_path_map} = \%path_map; | |||||||||||||||||||||||||||||||||||||
| 510 | |||||||||||||||||||||||||||||||||||||||
| 511 | 10 | return; | |||||||||||||||||||||||||||||||||||||
| 512 | } | ||||||||||||||||||||||||||||||||||||||
| 513 | |||||||||||||||||||||||||||||||||||||||
| 514 | |||||||||||||||||||||||||||||||||||||||
| 515 | =head2 call( $env ) | ||||||||||||||||||||||||||||||||||||||
| 516 | |||||||||||||||||||||||||||||||||||||||
| 517 | Called for every request. If the request path matches a configured | ||||||||||||||||||||||||||||||||||||||
| 518 | callback route, handles it directly. Otherwise, injects the | ||||||||||||||||||||||||||||||||||||||
| 519 | L<OIDC::Client::Plugin> instances into C<$env> and delegates to the | ||||||||||||||||||||||||||||||||||||||
| 520 | downstream application, converting any L<Plack::Middleware::OIDC::Redirect> | ||||||||||||||||||||||||||||||||||||||
| 521 | exception raised by the application into a real C<302> response. | ||||||||||||||||||||||||||||||||||||||
| 522 | |||||||||||||||||||||||||||||||||||||||
| 523 | =cut | ||||||||||||||||||||||||||||||||||||||
| 524 | |||||||||||||||||||||||||||||||||||||||
| 525 | 29 | ▶ | sub call ($self, $env) { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
call: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 526 | |||||||||||||||||||||||||||||||||||||||
| 527 | 29 | ▶ | if ($self->{_oidc_log}->isa('Plack::Middleware::OIDC::Logger')) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 528 | 29 | $self->{_oidc_log}->set_env($env); | |||||||||||||||||||||||||||||||||||||
| 529 | } | ||||||||||||||||||||||||||||||||||||||
| 530 | |||||||||||||||||||||||||||||||||||||||
| 531 | 29 | $self->_rotate_flash($env); | |||||||||||||||||||||||||||||||||||||
| 532 | |||||||||||||||||||||||||||||||||||||||
| 533 | 29 | ▶ | if (my $route = $self->{_oidc_path_map}{ $env->{PATH_INFO} }) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 534 | return $route->{action} eq 'login' ? $self->_login_callback($env, $route->{provider}) | ||||||||||||||||||||||||||||||||||||||
| 535 | 5 | ▶ | : $self->_logout_callback($env, $route->{provider}); | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 536 | } | ||||||||||||||||||||||||||||||||||||||
| 537 | |||||||||||||||||||||||||||||||||||||||
| 538 | 24 | $self->_inject_oidc_instances($env); | |||||||||||||||||||||||||||||||||||||
| 539 | |||||||||||||||||||||||||||||||||||||||
| 540 | 23 | ▶ | return try { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 541 | 23 | $self->app->($env); | |||||||||||||||||||||||||||||||||||||
| 542 | } | ||||||||||||||||||||||||||||||||||||||
| 543 | 7 | ▶ | catch { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||
| 544 | 7 | my $e = $_; | |||||||||||||||||||||||||||||||||||||
| 545 | 7 | ▶ | if (blessed($e) && $e->isa('Plack::Middleware::OIDC::Redirect')) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
builtin::blessed($e) and $e->isa("Plack::Middleware::OIDC::Redirect")
MC/DC50%1/2
builtin::blessed($e) and $e->isa("Plack::Middleware::OIDC::Redirect")
builtin::blessed($e)
$e->isa("Plack::Middleware::OIDC::Redirect")
Truth table66%2/3
A and B
A= builtin::blessed($e)B= $e->isa("Plack::Middleware::OIDC::Redirect")
| |||||||||||||||||||||||||||||||||||||||
| 546 | 6 | $e->as_psgi(); | |||||||||||||||||||||||||||||||||||||
| 547 | } | ||||||||||||||||||||||||||||||||||||||
| 548 | else { | ||||||||||||||||||||||||||||||||||||||
| 549 | 1 | die $e; | |||||||||||||||||||||||||||||||||||||
| 550 | } | ||||||||||||||||||||||||||||||||||||||
| 551 | 23 | }; | |||||||||||||||||||||||||||||||||||||
| 552 | } | ||||||||||||||||||||||||||||||||||||||
| 553 | |||||||||||||||||||||||||||||||||||||||
| 554 | |||||||||||||||||||||||||||||||||||||||
| 555 | # Flash lifecycle, run at the start of every request going through the | ||||||||||||||||||||||||||||||||||||||
| 556 | # middleware : the data stored under 'oidc_flash_new' during the previous | ||||||||||||||||||||||||||||||||||||||
| 557 | # request becomes readable as 'oidc_flash' for this request only, and any | ||||||||||||||||||||||||||||||||||||||
| 558 | # older 'oidc_flash' is dropped. | ||||||||||||||||||||||||||||||||||||||
| 559 | 29 | ▶ | sub _rotate_flash ($self, $env) { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_rotate_flash: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||
| 560 | |||||||||||||||||||||||||||||||||||||||
| 561 | 29 | ▶ | my $session = $env->{'psgix.session'} | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 562 | or return; | ||||||||||||||||||||||||||||||||||||||
| 563 | |||||||||||||||||||||||||||||||||||||||
| 564 | 19 | ▶ | if (exists $session->{oidc_flash_new}) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 565 | 2 | $session->{oidc_flash} = delete $session->{oidc_flash_new}; | |||||||||||||||||||||||||||||||||||||
| 566 | } | ||||||||||||||||||||||||||||||||||||||
| 567 | else { | ||||||||||||||||||||||||||||||||||||||
| 568 | 17 | delete $session->{oidc_flash}; | |||||||||||||||||||||||||||||||||||||
| 569 | } | ||||||||||||||||||||||||||||||||||||||
| 570 | |||||||||||||||||||||||||||||||||||||||
| 571 | 19 | return; | |||||||||||||||||||||||||||||||||||||
| 572 | } | ||||||||||||||||||||||||||||||||||||||
| 573 | |||||||||||||||||||||||||||||||||||||||
| 574 | |||||||||||||||||||||||||||||||||||||||
| 575 | # Builds and injects one OIDC::Client::Plugin instance per configured | ||||||||||||||||||||||||||||||||||||||
| 576 | # provider into $env, plus the $env->{oidc} shortcut for the default one. | ||||||||||||||||||||||||||||||||||||||
| 577 | 24 | ▶ | sub _inject_oidc_instances ($self, $env) { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_inject_oidc_instances: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||
| 578 | |||||||||||||||||||||||||||||||||||||||
| 579 | 24 | my $default_provider_name = $self->_resolve_default_provider_name(); | |||||||||||||||||||||||||||||||||||||
| 580 | |||||||||||||||||||||||||||||||||||||||
| 581 | 24 | foreach my $provider_name (keys %{ $self->{_oidc_client_by_provider} }) { | |||||||||||||||||||||||||||||||||||||
| 582 | 24 | my $plugin = $self->_build_plugin_instance($env, $provider_name); | |||||||||||||||||||||||||||||||||||||
| 583 | |||||||||||||||||||||||||||||||||||||||
| 584 | 23 | $env->{"oidc.$provider_name"} = $plugin; | |||||||||||||||||||||||||||||||||||||
| 585 | 23 | ▶ | $env->{oidc} = $plugin | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $default_provider_name and $provider_name eq $default_provider_name
MC/DC100%2/2
defined $default_provider_name and $provider_name eq $default_provider_name
defined $default_provider_name
$provider_name eq $default_provider_name
Truth table100%3/3
A and B
A= defined $default_provider_nameB= $provider_name eq $default_provider_name
| |||||||||||||||||||||||||||||||||||||||
| 586 | if defined $default_provider_name && $provider_name eq $default_provider_name; | ||||||||||||||||||||||||||||||||||||||
| 587 | } | ||||||||||||||||||||||||||||||||||||||
| 588 | |||||||||||||||||||||||||||||||||||||||
| 589 | 23 | return; | |||||||||||||||||||||||||||||||||||||
| 590 | } | ||||||||||||||||||||||||||||||||||||||
| 591 | |||||||||||||||||||||||||||||||||||||||
| 592 | |||||||||||||||||||||||||||||||||||||||
| 593 | 24 | ▶ | sub _resolve_default_provider_name ($self) { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_resolve_default_provider_name: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||
| 594 | |||||||||||||||||||||||||||||||||||||||
| 595 | 24 | ▶ | if (my $default_provider_name = $self->{_oidc_config}{default_provider}) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 596 | 9 | return $default_provider_name; | |||||||||||||||||||||||||||||||||||||
| 597 | } | ||||||||||||||||||||||||||||||||||||||
| 598 | |||||||||||||||||||||||||||||||||||||||
| 599 | 15 | my @provider_names = keys %{ $self->{_oidc_client_by_provider} }; | |||||||||||||||||||||||||||||||||||||
| 600 | |||||||||||||||||||||||||||||||||||||||
| 601 | 15 | ▶ | return $provider_names[0] if @provider_names == 1; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 602 | 3 | return; | |||||||||||||||||||||||||||||||||||||
| 603 | } | ||||||||||||||||||||||||||||||||||||||
| 604 | |||||||||||||||||||||||||||||||||||||||
| 605 | |||||||||||||||||||||||||||||||||||||||
| 606 | 29 | ▶ | sub _build_plugin_instance ($self, $env, $provider_name) { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_plugin_instance: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||
| 607 | |||||||||||||||||||||||||||||||||||||||
| 608 | 29 | ▶ | my $client = $self->{_oidc_client_by_provider}{$provider_name} | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 609 | or croak("OIDC: no client for provider '$provider_name'"); | ||||||||||||||||||||||||||||||||||||||
| 610 | |||||||||||||||||||||||||||||||||||||||
| 611 | 29 | ▶ | if ($client->store_mode ne 'stash' && !$env->{'psgix.session'}) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$client->store_mode ne "stash" and not $env->{"psgix.session"}
MC/DC100%2/2
$client->store_mode ne "stash" and not $env->{"psgix.session"}
$client->store_mode ne "stash"
not $env->{"psgix.session"}
Truth table100%3/3
A and not(B)
A= $client->store_mode ne "stash"B= not $env->{"psgix.session"}
| |||||||||||||||||||||||||||||||||||||||
| 612 | 1 | croak("OIDC: no session found in \$env->{'psgix.session'} - " | |||||||||||||||||||||||||||||||||||||
| 613 | . "enable Plack::Middleware::Session (or an equivalent middleware) " | ||||||||||||||||||||||||||||||||||||||
| 614 | . "before Plack::Middleware::OIDC"); | ||||||||||||||||||||||||||||||||||||||
| 615 | } | ||||||||||||||||||||||||||||||||||||||
| 616 | |||||||||||||||||||||||||||||||||||||||
| 617 | 28 | ▶ | $env->{'oidc.stash'} ||= {}; | ||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$env->{"oidc.stash"} ||= {}
MC/DC100%1/1
$env->{"oidc.stash"} ||= {}
$env->{"oidc.stash"}
Truth table100%2/2
A
A= $env->{"oidc.stash"}
| |||||||||||||||||||||||||||||||||||||||
| 618 | |||||||||||||||||||||||||||||||||||||||
| 619 | 28 | my $req = Plack::Request->new($env); | |||||||||||||||||||||||||||||||||||||
| 620 | |||||||||||||||||||||||||||||||||||||||
| 621 | return OIDC::Client::Plugin->new( | ||||||||||||||||||||||||||||||||||||||
| 622 | log => $self->{_oidc_log}, | ||||||||||||||||||||||||||||||||||||||
| 623 | request_params => $self->_extract_request_params($req), | ||||||||||||||||||||||||||||||||||||||
| 624 | request_headers => $self->_extract_request_headers($req), | ||||||||||||||||||||||||||||||||||||||
| 625 | session => $env->{'psgix.session'} || {}, | ||||||||||||||||||||||||||||||||||||||
| 626 | stash => $env->{'oidc.stash'}, | ||||||||||||||||||||||||||||||||||||||
| 627 | 5 | ▶ | redirect => sub { die Plack::Middleware::OIDC::Redirect->new(url => $_[0]); }, | ||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 628 | 28 | ▶ | client => $client, | ||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$env->{'psgix.session'} || {}
MC/DC100%1/1
$env->{'psgix.session'} || {}
$env->{'psgix.session'}
Truth table100%2/2
A
A= $env->{'psgix.session'}
| |||||||||||||||||||||||||||||||||||||||
| 629 | base_url => $req->base->as_string, | ||||||||||||||||||||||||||||||||||||||
| 630 | current_url => $req->uri->as_string, | ||||||||||||||||||||||||||||||||||||||
| 631 | ); | ||||||||||||||||||||||||||||||||||||||
| 632 | } | ||||||||||||||||||||||||||||||||||||||
| 633 | |||||||||||||||||||||||||||||||||||||||
| 634 | |||||||||||||||||||||||||||||||||||||||
| 635 | 28 | ▶ | sub _extract_request_params ($self, $req) { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_extract_request_params: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||
| 636 | |||||||||||||||||||||||||||||||||||||||
| 637 | # Hash::MultiValue->keys returns one entry per value (duplicates for | ||||||||||||||||||||||||||||||||||||||
| 638 | # multi-valued keys), so unique them first | ||||||||||||||||||||||||||||||||||||||
| 639 | 28 | my $parameters = $req->parameters; | |||||||||||||||||||||||||||||||||||||
| 640 | 28 | my %seen_key; | |||||||||||||||||||||||||||||||||||||
| 641 | 28 | my @unique_keys = grep { !$seen_key{$_}++ } $parameters->keys; | |||||||||||||||||||||||||||||||||||||
| 642 | |||||||||||||||||||||||||||||||||||||||
| 643 | 28 | my %params; | |||||||||||||||||||||||||||||||||||||
| 644 | 28 | foreach my $key (@unique_keys) { | |||||||||||||||||||||||||||||||||||||
| 645 | 12 | my @values = $parameters->get_all($key); | |||||||||||||||||||||||||||||||||||||
| 646 | 12 | ▶ | $params{$key} = @values > 1 ? \@values | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 647 | : $values[0]; | ||||||||||||||||||||||||||||||||||||||
| 648 | } | ||||||||||||||||||||||||||||||||||||||
| 649 | |||||||||||||||||||||||||||||||||||||||
| 650 | 28 | return \%params; | |||||||||||||||||||||||||||||||||||||
| 651 | } | ||||||||||||||||||||||||||||||||||||||
| 652 | |||||||||||||||||||||||||||||||||||||||
| 653 | |||||||||||||||||||||||||||||||||||||||
| 654 | 28 | ▶ | sub _extract_request_headers ($self, $req) { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_extract_request_headers: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 655 | |||||||||||||||||||||||||||||||||||||||
| 656 | 28 | my %headers; | |||||||||||||||||||||||||||||||||||||
| 657 | 28 | ▶ | $req->headers->scan(sub { $headers{ $_[0] } = $_[1] }); | ||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 658 | |||||||||||||||||||||||||||||||||||||||
| 659 | 28 | return \%headers; | |||||||||||||||||||||||||||||||||||||
| 660 | } | ||||||||||||||||||||||||||||||||||||||
| 661 | |||||||||||||||||||||||||||||||||||||||
| 662 | |||||||||||||||||||||||||||||||||||||||
| 663 | # Builds a PSGI redirect response | ||||||||||||||||||||||||||||||||||||||
| 664 | 5 | ▶ | sub _psgi_redirect ($self, $url) { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_psgi_redirect: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 665 | 5 | return Plack::Middleware::OIDC::Redirect->new(url => $url)->as_psgi(); | |||||||||||||||||||||||||||||||||||||
| 666 | } | ||||||||||||||||||||||||||||||||||||||
| 667 | |||||||||||||||||||||||||||||||||||||||
| 668 | |||||||||||||||||||||||||||||||||||||||
| 669 | # code executed on callback after authentication attempt | ||||||||||||||||||||||||||||||||||||||
| 670 | 4 | ▶ | sub _login_callback ($self, $env, $provider_name) { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_login_callback: called CC=5 SCAR=17.3
| |||||||||||||||||||||||||||||||||||||||
| 671 | |||||||||||||||||||||||||||||||||||||||
| 672 | 4 | my $req = Plack::Request->new($env); | |||||||||||||||||||||||||||||||||||||
| 673 | 4 | my $state = $req->parameters->get('state'); | |||||||||||||||||||||||||||||||||||||
| 674 | 4 | my $session = $env->{'psgix.session'}; | |||||||||||||||||||||||||||||||||||||
| 675 | |||||||||||||||||||||||||||||||||||||||
| 676 | # oidc_auth is routing/application state. # OIDC::Client::Plugin::get_token() remains responsible | ||||||||||||||||||||||||||||||||||||||
| 677 | # for validating the OIDC state/nonce/PKCE flow. | ||||||||||||||||||||||||||||||||||||||
| 678 | 4 | ▶ | my $auth_data = ($state && $session) ? $session->{oidc_auth}{$state} | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$state and $session
MC/DC0%0/2
$state and $session
$state
$session
Truth table33%1/3
A and B
A= $stateB= $session
| |||||||||||||||||||||||||||||||||||||||
| 679 | : undef; | ||||||||||||||||||||||||||||||||||||||
| 680 | |||||||||||||||||||||||||||||||||||||||
| 681 | my $plugin = $self->_build_plugin_instance( | ||||||||||||||||||||||||||||||||||||||
| 682 | $env, | ||||||||||||||||||||||||||||||||||||||
| 683 | 4 | ▶ | ($auth_data && $auth_data->{provider}) || $provider_name, | ||||||||||||||||||||||||||||||||||||
|
Condition66%4/6
$auth_data && $auth_data->{'provider'} || $provider_name
$auth_data and $auth_data->{"provider"}
MC/DC0%0/3
$auth_data && $auth_data->{'provider'} || $provider_name
$auth_data
$auth_data->{"provider"}
$provider_name
Truth table40%2/5
A and B || C
A= $auth_dataB= $auth_data->{"provider"}C= $provider_name
| |||||||||||||||||||||||||||||||||||||||
| 684 | ); | ||||||||||||||||||||||||||||||||||||||
| 685 | |||||||||||||||||||||||||||||||||||||||
| 686 | 4 | ▶ | return try { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=8.1
| |||||||||||||||||||||||||||||||||||||||
| 687 | 4 | $plugin->get_token(); | |||||||||||||||||||||||||||||||||||||
| 688 | 1 | ▶ | $self->_redirect_response($env, $auth_data && $auth_data->{target_url}); | ||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$auth_data && $auth_data->{'target_url'}
MC/DC0%0/2
$auth_data && $auth_data->{'target_url'}
$auth_data
$auth_data->{'target_url'}
Truth table33%1/3
A && B
A= $auth_dataB= $auth_data->{'target_url'}
| |||||||||||||||||||||||||||||||||||||||
| 689 | } | ||||||||||||||||||||||||||||||||||||||
| 690 | 3 | ▶ | catch { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 691 | 3 | my $e = $_; | |||||||||||||||||||||||||||||||||||||
| 692 | 3 | $self->_handle_authentication_error($env, $e); | |||||||||||||||||||||||||||||||||||||
| 693 | 4 | }; | |||||||||||||||||||||||||||||||||||||
| 694 | } | ||||||||||||||||||||||||||||||||||||||
| 695 | |||||||||||||||||||||||||||||||||||||||
| 696 | |||||||||||||||||||||||||||||||||||||||
| 697 | # code executed on callback after user logout | ||||||||||||||||||||||||||||||||||||||
| 698 | 1 | ▶ | sub _logout_callback ($self, $env, $provider_name) { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_logout_callback: called CC=6 SCAR=21.8
| |||||||||||||||||||||||||||||||||||||||
| 699 | |||||||||||||||||||||||||||||||||||||||
| 700 | 1 | my $req = Plack::Request->new($env); | |||||||||||||||||||||||||||||||||||||
| 701 | 1 | my $state = $req->parameters->get('state'); | |||||||||||||||||||||||||||||||||||||
| 702 | 1 | my $session = $env->{'psgix.session'}; | |||||||||||||||||||||||||||||||||||||
| 703 | |||||||||||||||||||||||||||||||||||||||
| 704 | 1 | ▶ | my $logout_data = ($state && $session) ? delete $session->{oidc_logout}{$state} | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$state and $session
MC/DC0%0/2
$state and $session
$state
$session
Truth table33%1/3
A and B
A= $stateB= $session
| |||||||||||||||||||||||||||||||||||||||
| 705 | : undef; | ||||||||||||||||||||||||||||||||||||||
| 706 | |||||||||||||||||||||||||||||||||||||||
| 707 | my $plugin = $self->_build_plugin_instance( | ||||||||||||||||||||||||||||||||||||||
| 708 | $env, | ||||||||||||||||||||||||||||||||||||||
| 709 | 1 | ▶ | ($logout_data && $logout_data->{provider}) || $provider_name, | ||||||||||||||||||||||||||||||||||||
|
Condition33%2/6
$logout_data && $logout_data->{'provider'} || $provider_name
$logout_data and $logout_data->{"provider"}
MC/DC0%0/3
$logout_data && $logout_data->{'provider'} || $provider_name
$logout_data
$logout_data->{"provider"}
$provider_name
Truth table20%1/5
A and B || C
A= $logout_dataB= $logout_data->{"provider"}C= $provider_name
| |||||||||||||||||||||||||||||||||||||||
| 710 | ); | ||||||||||||||||||||||||||||||||||||||
| 711 | 1 | $plugin->delete_stored_data(); | |||||||||||||||||||||||||||||||||||||
| 712 | |||||||||||||||||||||||||||||||||||||||
| 713 | 1 | ▶ | return $self->_redirect_response($env, $logout_data && $logout_data->{target_url}); | ||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$logout_data && $logout_data->{'target_url'}
MC/DC0%0/2
$logout_data && $logout_data->{'target_url'}
$logout_data
$logout_data->{'target_url'}
Truth table33%1/3
A && B
A= $logout_dataB= $logout_data->{'target_url'}
| |||||||||||||||||||||||||||||||||||||||
| 714 | } | ||||||||||||||||||||||||||||||||||||||
| 715 | |||||||||||||||||||||||||||||||||||||||
| 716 | |||||||||||||||||||||||||||||||||||||||
| 717 | 2 | ▶ | sub _redirect_response ($self, $env, $target_url = undef) { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_redirect_response: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||||||||||||
| 718 | 2 | my $req = Plack::Request->new($env); | |||||||||||||||||||||||||||||||||||||
| 719 | 2 | ▶ | return $self->_psgi_redirect($target_url || $req->base->as_string); | ||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$target_url || $req->base->as_string
MC/DC0%0/2
$target_url || $req->base->as_string
$target_url
$req->base->as_string
Truth table66%2/3
A || B
A= $target_urlB= $req->base->as_string
| |||||||||||||||||||||||||||||||||||||||
| 720 | } | ||||||||||||||||||||||||||||||||||||||
| 721 | |||||||||||||||||||||||||||||||||||||||
| 722 | |||||||||||||||||||||||||||||||||||||||
| 723 | 3 | ▶ | sub _handle_authentication_error ($self, $env, $e) { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_handle_authentication_error: called CC=5 SCAR=17.8
| |||||||||||||||||||||||||||||||||||||||
| 724 | |||||||||||||||||||||||||||||||||||||||
| 725 | 3 | ▶ | die $e | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
builtin::blessed($e) and $e->isa("OIDC::Client::Error")
MC/DC0%0/2
builtin::blessed($e) and $e->isa("OIDC::Client::Error")
builtin::blessed($e)
$e->isa("OIDC::Client::Error")
Truth table33%1/3
A and B
A= builtin::blessed($e)B= $e->isa("OIDC::Client::Error")
| |||||||||||||||||||||||||||||||||||||||
| 726 | unless blessed($e) && $e->isa('OIDC::Client::Error'); | ||||||||||||||||||||||||||||||||||||||
| 727 | |||||||||||||||||||||||||||||||||||||||
| 728 | my $error_path = $self->{_oidc_config}{authentication_error_path} | ||||||||||||||||||||||||||||||||||||||
| 729 | 3 | ▶ | or die $e; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 730 | |||||||||||||||||||||||||||||||||||||||
| 731 | # equivalent of Mojolicious' flash: stored in 'oidc_flash_new', it becomes | ||||||||||||||||||||||||||||||||||||||
| 732 | # readable as 'oidc_flash' during the next request only (see _rotate_flash) | ||||||||||||||||||||||||||||||||||||||
| 733 | 3 | ▶ | ($env->{'psgix.session'} ||= {})->{oidc_flash_new}{error_message} = $e->message; | ||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$env->{"psgix.session"} ||= {}
MC/DC0%0/1
$env->{"psgix.session"} ||= {}
$env->{"psgix.session"}
Truth table50%1/2
A
A= $env->{"psgix.session"}
| |||||||||||||||||||||||||||||||||||||||
| 734 | |||||||||||||||||||||||||||||||||||||||
| 735 | 3 | my $req = Plack::Request->new($env); | |||||||||||||||||||||||||||||||||||||
| 736 | 3 | my $error_url = URI->new_abs($error_path, $req->base->as_string)->as_string; | |||||||||||||||||||||||||||||||||||||
| 737 | |||||||||||||||||||||||||||||||||||||||
| 738 | 3 | return $self->_psgi_redirect($error_url); | |||||||||||||||||||||||||||||||||||||
| 739 | } | ||||||||||||||||||||||||||||||||||||||
| 740 | |||||||||||||||||||||||||||||||||||||||
| 741 | |||||||||||||||||||||||||||||||||||||||
| 742 | =head1 SECURITY RECOMMENDATION | ||||||||||||||||||||||||||||||||||||||
| 743 | |||||||||||||||||||||||||||||||||||||||
| 744 | It is highly recommended to configure the framework to store session data, | ||||||||||||||||||||||||||||||||||||||
| 745 | including sensitive tokens such as access and refresh tokens, on the backend | ||||||||||||||||||||||||||||||||||||||
| 746 | rather than in client-side cookies. Although cookies can be signed and encrypted, | ||||||||||||||||||||||||||||||||||||||
| 747 | storing tokens in the client exposes them to potential security threats. | ||||||||||||||||||||||||||||||||||||||
| 748 | |||||||||||||||||||||||||||||||||||||||
| 749 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||
| 750 | |||||||||||||||||||||||||||||||||||||||
| 751 | Sébastien Mourlhou | ||||||||||||||||||||||||||||||||||||||
| 752 | |||||||||||||||||||||||||||||||||||||||
| 753 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||||||||||||
| 754 | |||||||||||||||||||||||||||||||||||||||
| 755 | Copyright (C) Sébastien Mourlhou | ||||||||||||||||||||||||||||||||||||||
| 756 | |||||||||||||||||||||||||||||||||||||||
| 757 | This program is free software, you can redistribute it and/or modify it under the terms of the Artistic License version 2.0. | ||||||||||||||||||||||||||||||||||||||
| 758 | |||||||||||||||||||||||||||||||||||||||
| 759 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||
| 760 | |||||||||||||||||||||||||||||||||||||||
| 761 | =over 2 | ||||||||||||||||||||||||||||||||||||||
| 762 | |||||||||||||||||||||||||||||||||||||||
| 763 | =item * L<OIDC::Client> | ||||||||||||||||||||||||||||||||||||||
| 764 | |||||||||||||||||||||||||||||||||||||||
| 765 | =item * L<OIDC::Client::Plugin> | ||||||||||||||||||||||||||||||||||||||
| 766 | |||||||||||||||||||||||||||||||||||||||
| 767 | =item * L<OIDC::Client::Config> | ||||||||||||||||||||||||||||||||||||||
| 768 | |||||||||||||||||||||||||||||||||||||||
| 769 | =item * L<Mojolicious::Plugin::OIDC> | ||||||||||||||||||||||||||||||||||||||
| 770 | |||||||||||||||||||||||||||||||||||||||
| 771 | =item * L<Catalyst::Plugin::OIDC> | ||||||||||||||||||||||||||||||||||||||
| 772 | |||||||||||||||||||||||||||||||||||||||
| 773 | =item * L<Dancer2::Plugin::OIDC> | ||||||||||||||||||||||||||||||||||||||
| 774 | |||||||||||||||||||||||||||||||||||||||
| 775 | =back | ||||||||||||||||||||||||||||||||||||||
| 776 | |||||||||||||||||||||||||||||||||||||||
| 777 | =cut | ||||||||||||||||||||||||||||||||||||||
| 778 | |||||||||||||||||||||||||||||||||||||||
| 779 | 9 | 1; | |||||||||||||||||||||||||||||||||||||