| 1 | package Mojolicious::Plugin::OIDC; | ||||||||||||||||||||||||||||
| 2 | 3 | ▶ | use Mojo::Base 'Mojolicious::Plugin', -signatures; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use Clone qw(clone); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use Try::Tiny; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use OIDC::Client; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use OIDC::Client::Plugin; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 3 | ▶ | use OIDC::Client::Error; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 3 | ▶ | use OIDC::Client::Error::Authentication; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 3 | our $VERSION = '1.05'; # VERSION: generated by Dist::Zilla::Plugin::OurPkgVersion | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | =encoding utf8 | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | =head1 NAME | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | Mojolicious::Plugin::OIDC - OIDC protocol integration for Mojolicious | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | This plugin makes it easy to integrate the OpenID Connect protocol | ||||||||||||||||||||||||||||
| 24 | into a Mojolicious application. It enables applications to act as an | ||||||||||||||||||||||||||||
| 25 | OpenID Connect Relying Party (RP) for user authentication and as an | ||||||||||||||||||||||||||||
| 26 | OAuth 2.0 Resource Server by validating Access Tokens issued by an | ||||||||||||||||||||||||||||
| 27 | external Authorization Server or OpenID Provider. It does not implement | ||||||||||||||||||||||||||||
| 28 | an OpenID Provider. | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | It essentially uses the L<OIDC-Client|https://metacpan.org/dist/OIDC-Client> | ||||||||||||||||||||||||||||
| 31 | distribution. | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | =cut | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 3 | has '_oidc_config'; | |||||||||||||||||||||||||||
| 36 | 3 | has '_oidc_client_by_provider'; | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | =head1 METHODS | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | =head2 register | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | Code executed once when the application is loaded. | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | Depending on the configuration, creates and keeps in memory one or more clients | ||||||||||||||||||||||||||||
| 46 | (L<OIDC::Client> stateless objects) and automatically adds the callback routes | ||||||||||||||||||||||||||||
| 47 | to the application. | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | =cut | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 2 | ▶ | sub register ($self, $app, $config) { | ||||||||||||||||||||||||||
|
Subroutine
register: called CC=11 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | keys %$config | ||||||||||||||||||||||||||||
| 54 | 2 | ▶ | or $config = ($app->config->{oidc_client} || {}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$app->config->{'oidc_client'} || {}
MC/DC0%0/1
$app->config->{'oidc_client'} || {}
$app->config->{'oidc_client'}
Truth table0%0/2
A
A= $app->config->{'oidc_client'}
| |||||||||||||||||||||||||||||
| 55 | 2 | $self->_oidc_config($config); | |||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 2 | my %client_by_provider; | |||||||||||||||||||||||||||
| 58 | my %seen_path; | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 2 | ▶ | foreach my $provider (keys %{ $config->{provider} || {} }) { | ||||||||||||||||||||||||||
|
Condition50%1/2
$config->{'provider'} || {}
MC/DC0%0/1
$config->{'provider'} || {}
$config->{'provider'}
Truth table50%1/2
A
A= $config->{'provider'}
| |||||||||||||||||||||||||||||
| 61 | 2 | my $config_provider = clone($config->{provider}{$provider}); | |||||||||||||||||||||||||||
| 62 | 2 | $config_provider->{provider} = $provider; | |||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 2 | $client_by_provider{$provider} = OIDC::Client->new( | |||||||||||||||||||||||||||
| 65 | config => $config_provider, | ||||||||||||||||||||||||||||
| 66 | log => $app->log, | ||||||||||||||||||||||||||||
| 67 | ); | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | # dynamically add the callback routes to the application | ||||||||||||||||||||||||||||
| 70 | 2 | foreach my $action_type (qw/ login logout /) { | |||||||||||||||||||||||||||
| 71 | my $path = $action_type eq 'login' ? $config_provider->{signin_redirect_path} | ||||||||||||||||||||||||||||
| 72 | 4 | ▶ | : $config_provider->{logout_redirect_path}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | 4 | ▶ | next if !$path || $seen_path{$path}++; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not $path or $seen_path{$path}++
MC/DC50%1/2
not $path or $seen_path{$path}++
not $path
$seen_path{$path}++
Truth table66%2/3
not(A) or B
A= not $pathB= $seen_path{$path}++
| |||||||||||||||||||||||||||||
| 74 | 1 | ▶ | my $method = $action_type eq 'login' ? '_login_callback' : '_logout_callback'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 1 | ▶ | my $name = $action_type eq 'login' ? 'oidc_login_callback' : 'oidc_logout_callback'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | 1 | ▶ | $app->routes->any(['GET', 'POST'] => $path => sub { $self->$method(@_) } => $name); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | 2 | $self->_oidc_client_by_provider(\%client_by_provider); | |||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 2 | ▶ | $app->helper('oidc' => sub { $self->_helper_oidc(@_) }); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | =head1 METHODS ADDED TO THE APPLICATION | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | =head2 oidc( $provider ) | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | # with just one provider | ||||||||||||||||||||||||||||
| 90 | my $oidc = $c->oidc; | ||||||||||||||||||||||||||||
| 91 | # or | ||||||||||||||||||||||||||||
| 92 | my $oidc = $c->oidc('my_provider'); | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | # with several providers | ||||||||||||||||||||||||||||
| 95 | my $oidc = $c->oidc('my_other_provider'); | ||||||||||||||||||||||||||||
| 96 | # or | ||||||||||||||||||||||||||||
| 97 | my $oidc = $c->oidc; # here, you must define a default provider in the configuration | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | Creates and returns an instance of L<OIDC::Client::Plugin> with the data | ||||||||||||||||||||||||||||
| 100 | from the current request and session. | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | If several providers are configured, the I<$provider> parameter is mandatory | ||||||||||||||||||||||||||||
| 103 | unless the C<default_provider> configuration entry is defined (see L<OIDC::Client::Config>). | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | This is the application's entry point to the library. Please see the | ||||||||||||||||||||||||||||
| 106 | L<OIDC::Client::Plugin> documentation to find out what methods are available. | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | =cut | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 12 | ▶ | sub _helper_oidc ($self, $c, $provider = undef) { | ||||||||||||||||||||||||||
|
Subroutine
_helper_oidc: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 12 | my $client = $self->_get_client_for_provider($provider); | |||||||||||||||||||||||||||
| 113 | 12 | my $plugin = $c->stash->{oidc}{plugin}; | |||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 12 | ▶ | return $plugin | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$plugin and $plugin->client->provider eq $client->provider
MC/DC50%1/2
$plugin and $plugin->client->provider eq $client->provider
$plugin
$plugin->client->provider eq $client->provider
Truth table66%2/3
A and B
A= $pluginB= $plugin->client->provider eq $client->provider
| |||||||||||||||||||||||||||||
| 116 | if $plugin && $plugin->client->provider eq $client->provider; | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | $plugin = $c->stash->{oidc}{plugin} = OIDC::Client::Plugin->new( | ||||||||||||||||||||||||||||
| 119 | log => $c->log, | ||||||||||||||||||||||||||||
| 120 | request_params => $c->req->params->to_hash, | ||||||||||||||||||||||||||||
| 121 | request_headers => $c->req->headers->to_hash, | ||||||||||||||||||||||||||||
| 122 | session => $c->session, | ||||||||||||||||||||||||||||
| 123 | stash => $c->stash, | ||||||||||||||||||||||||||||
| 124 | 2 | ▶ | redirect => sub { $c->redirect_to($_[0]); return; }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 125 | 8 | client => $client, | |||||||||||||||||||||||||||
| 126 | base_url => $c->req->url->base->to_string, | ||||||||||||||||||||||||||||
| 127 | current_url => $c->req->url->to_string, | ||||||||||||||||||||||||||||
| 128 | ); | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 8 | return $plugin; | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | # code executed on callback after authentication attempt | ||||||||||||||||||||||||||||
| 134 | 2 | ▶ | sub _login_callback ($self, $c) { | ||||||||||||||||||||||||||
|
Subroutine
_login_callback: called CC=1 SCAR=0.4
| |||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 2 | my $auth_data = $self->_get_auth_data($c); | |||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 2 | ▶ | try { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=8.1
| |||||||||||||||||||||||||||||
| 139 | 2 | $c->oidc($auth_data->{provider})->get_token(); | |||||||||||||||||||||||||||
| 140 | 1 | ▶ | $c->redirect_to($auth_data->{target_url} || $c->url_for('/')); | ||||||||||||||||||||||||||
|
Condition33%1/3
$auth_data->{'target_url'} || $c->url_for('/')
MC/DC0%0/2
$auth_data->{'target_url'} || $c->url_for('/')
$auth_data->{'target_url'}
$c->url_for('/')
Truth table33%1/3
A || B
A= $auth_data->{'target_url'}B= $c->url_for('/')
| |||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | 1 | ▶ | catch { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=15.9
| |||||||||||||||||||||||||||||
| 143 | 1 | my $e = $_; | |||||||||||||||||||||||||||
| 144 | 1 | ▶ | die $e unless blessed($e) && $e->isa('OIDC::Client::Error'); | ||||||||||||||||||||||||||
|
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")
| |||||||||||||||||||||||||||||
| 145 | 1 | ▶ | if (my $error_path = $self->_oidc_config->{authentication_error_path}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 146 | 1 | $c->flash('error_message' => $e->message); | |||||||||||||||||||||||||||
| 147 | 1 | $c->redirect_to($c->url_for($error_path)); | |||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | else { | ||||||||||||||||||||||||||||
| 150 | 0 | OIDC::Client::Error::Authentication->throw($e->message); | |||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||
| 152 | 2 | }; | |||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | 2 | ▶ | sub _get_auth_data ($self, $c) { | ||||||||||||||||||||||||||
|
Subroutine
_get_auth_data: called CC=3 SCAR=11.7
| |||||||||||||||||||||||||||||
| 156 | 2 | ▶ | my $state = $c->req->param('state') | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 157 | or OIDC::Client::Error::Authentication->throw("OIDC: no state parameter in login callback request"); | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 2 | ▶ | my $auth_data = $c->session->{oidc_auth}{$state} | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 160 | or OIDC::Client::Error::Authentication->throw("OIDC: no authorisation data"); | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 2 | return $auth_data; | |||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | # code executed on callback after user logout | ||||||||||||||||||||||||||||
| 166 | 0 | ▶ | sub _logout_callback ($self, $c) { | ||||||||||||||||||||||||||
|
Subroutine
_logout_callback: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | 0 | $c->log->debug('Logging out'); | |||||||||||||||||||||||||||
| 169 | 0 | my $logout_data = $self->_extract_logout_data($c); | |||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | 0 | $c->oidc($logout_data->{provider})->delete_stored_data(); | |||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | 0 | ▶ | $c->redirect_to($logout_data->{target_url} || $c->url_for('/')); | ||||||||||||||||||||||||||
|
Condition0%0/3
$logout_data->{'target_url'} || $c->url_for('/')
MC/DC0%0/2
$logout_data->{'target_url'} || $c->url_for('/')
$logout_data->{'target_url'}
$c->url_for('/')
Truth table0%0/3
A || B
A= $logout_data->{'target_url'}B= $c->url_for('/')
| |||||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | 0 | ▶ | sub _extract_logout_data ($self, $c) { | ||||||||||||||||||||||||||
|
Subroutine
_extract_logout_data: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 177 | 0 | ▶ | my $state = $c->req->param('state') | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 178 | or OIDC::Client::Error->throw("OIDC: no state parameter in logout callback request"); | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | 0 | ▶ | my $logout_data = delete $c->session->{oidc_logout}{$state} | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 181 | or OIDC::Client::Error->throw("OIDC: no logout data"); | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | 0 | return $logout_data; | |||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | 12 | ▶ | sub _get_client_for_provider ($self, $provider) { | ||||||||||||||||||||||||||
|
Subroutine
_get_client_for_provider: called CC=6 SCAR=19.9
| |||||||||||||||||||||||||||||
| 187 | 12 | ▶ | $provider //= $self->_oidc_config->{default_provider}; | ||||||||||||||||||||||||||
|
Condition66%2/3
$provider //= $self->_oidc_config->{"default_provider"}
MC/DC50%1/2
$provider //= $self->_oidc_config->{"default_provider"}
$provider
$self->_oidc_config->{"default_provider"}
Truth table66%2/3
A //= B
A= $providerB= $self->_oidc_config->{"default_provider"}
| |||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | 12 | ▶ | unless (defined $provider) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 190 | 10 | my @providers = keys %{ $self->_oidc_client_by_provider }; | |||||||||||||||||||||||||||
| 191 | 10 | ▶ | if (@providers == 1) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 192 | 10 | $provider = $providers[0]; | |||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||
| 194 | elsif (@providers > 1) { | ||||||||||||||||||||||||||||
| 195 | 0 | croak(q{OIDC: more than one provider are configured, the provider is mandatory : $c->oidc('my_provider')}); | |||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||
| 197 | else { | ||||||||||||||||||||||||||||
| 198 | 0 | croak("OIDC: no provider configured"); | |||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | 12 | ▶ | my $client = $self->_oidc_client_by_provider->{$provider} | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 203 | or croak("OIDC: no client for provider $provider"); | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | 12 | return $client; | |||||||||||||||||||||||||||
| 206 | } | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | =head1 CONFIGURATION | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | Section to be added to your configuration file : | ||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | oidc_client => { | ||||||||||||||||||||||||||||
| 213 | provider => { | ||||||||||||||||||||||||||||
| 214 | provider_name => { | ||||||||||||||||||||||||||||
| 215 | id => 'my-app-id', | ||||||||||||||||||||||||||||
| 216 | secret => 'xxxxxxxxx', | ||||||||||||||||||||||||||||
| 217 | well_known_url => 'https://yourprovider.com/oauth2/.well-known/openid-configuration', | ||||||||||||||||||||||||||||
| 218 | signin_redirect_path => '/oidc/login/callback', | ||||||||||||||||||||||||||||
| 219 | scope => 'openid profile roles email', | ||||||||||||||||||||||||||||
| 220 | expiration_leeway => 20, | ||||||||||||||||||||||||||||
| 221 | claim_mapping => { | ||||||||||||||||||||||||||||
| 222 | login => 'sub', | ||||||||||||||||||||||||||||
| 223 | lastname => 'lastName', | ||||||||||||||||||||||||||||
| 224 | firstname => 'firstName', | ||||||||||||||||||||||||||||
| 225 | email => 'email', | ||||||||||||||||||||||||||||
| 226 | roles => 'roles', | ||||||||||||||||||||||||||||
| 227 | }, | ||||||||||||||||||||||||||||
| 228 | audience_alias => { | ||||||||||||||||||||||||||||
| 229 | other_app_name => { | ||||||||||||||||||||||||||||
| 230 | audience => 'other-app-audience', | ||||||||||||||||||||||||||||
| 231 | } | ||||||||||||||||||||||||||||
| 232 | } | ||||||||||||||||||||||||||||
| 233 | } | ||||||||||||||||||||||||||||
| 234 | } | ||||||||||||||||||||||||||||
| 235 | } | ||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||
| 237 | This is an example, see the detailed possibilities in L<OIDC::Client::Config>. | ||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||
| 239 | =head1 SAMPLES | ||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||
| 241 | Here are some samples by category. Although you will have to adapt them to your needs, | ||||||||||||||||||||||||||||
| 242 | they should be a good starting point. | ||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||
| 244 | =head2 Setup | ||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||
| 246 | To setup the plugin when the application is launched : | ||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | $app->plugin('OIDC'); | ||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||
| 250 | =head2 Authentication | ||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||
| 252 | To authenticate the end-user : | ||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||
| 254 | $app->hook(before_dispatch => sub { | ||||||||||||||||||||||||||||
| 255 | my $c = shift; | ||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||
| 257 | my $path = $c->req->url->path; | ||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | # Public routes | ||||||||||||||||||||||||||||
| 260 | return if $path =~ m[^/oidc/] | ||||||||||||||||||||||||||||
| 261 | || $path =~ m[^/error/]; | ||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||
| 263 | # Authentication | ||||||||||||||||||||||||||||
| 264 | if (my $identity = $c->oidc->get_valid_identity()) { | ||||||||||||||||||||||||||||
| 265 | $c->remote_user($identity->subject); | ||||||||||||||||||||||||||||
| 266 | } | ||||||||||||||||||||||||||||
| 267 | elsif (uc($c->req->method) eq 'GET' && !$c->is_ajax_request()) { | ||||||||||||||||||||||||||||
| 268 | $c->oidc->redirect_to_authorize(); | ||||||||||||||||||||||||||||
| 269 | } | ||||||||||||||||||||||||||||
| 270 | else { | ||||||||||||||||||||||||||||
| 271 | $c->render(template => 'error', | ||||||||||||||||||||||||||||
| 272 | message => "You have been logged out. Please try again after refreshing the page.", | ||||||||||||||||||||||||||||
| 273 | status => 401); | ||||||||||||||||||||||||||||
| 274 | } | ||||||||||||||||||||||||||||
| 275 | }); | ||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||
| 277 | =head2 API call | ||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||
| 279 | To make an API call with propagation of the security context (token exchange) : | ||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||
| 281 | # Retrieving a web client (Mojo::UserAgent object) | ||||||||||||||||||||||||||||
| 282 | my $ua = try { | ||||||||||||||||||||||||||||
| 283 | $c->oidc->build_api_useragent('other_app_name') | ||||||||||||||||||||||||||||
| 284 | } | ||||||||||||||||||||||||||||
| 285 | catch { | ||||||||||||||||||||||||||||
| 286 | $c->log->warn("Unable to exchange token : $_"); | ||||||||||||||||||||||||||||
| 287 | $c->render(template => 'error', | ||||||||||||||||||||||||||||
| 288 | message => "Authorization problem. Please try again after refreshing the page.", | ||||||||||||||||||||||||||||
| 289 | status => 403); | ||||||||||||||||||||||||||||
| 290 | return; | ||||||||||||||||||||||||||||
| 291 | } or return; | ||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||
| 293 | # Usual call to the API | ||||||||||||||||||||||||||||
| 294 | my $res = $ua->get($url)->result; | ||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||
| 296 | =head2 Resource Server | ||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||
| 298 | To check an access token from a Resource Server, assuming it's a JWT token. | ||||||||||||||||||||||||||||
| 299 | For example, with an application using L<Mojolicious::Plugin::OpenAPI>, you can | ||||||||||||||||||||||||||||
| 300 | define a security definition that checks that the access token is intended for all | ||||||||||||||||||||||||||||
| 301 | the expected scopes : | ||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||
| 303 | $app->plugin(OpenAPI => { | ||||||||||||||||||||||||||||
| 304 | url => "data:///swagger.yaml", | ||||||||||||||||||||||||||||
| 305 | security => { | ||||||||||||||||||||||||||||
| 306 | oidc => sub { | ||||||||||||||||||||||||||||
| 307 | my ($c, $definition, $scopes, $cb) = @_; | ||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||
| 309 | my $access_token = try { | ||||||||||||||||||||||||||||
| 310 | return $c->oidc->verify_token(); | ||||||||||||||||||||||||||||
| 311 | } | ||||||||||||||||||||||||||||
| 312 | catch { | ||||||||||||||||||||||||||||
| 313 | $c->log->warn("Token validation : $_"); | ||||||||||||||||||||||||||||
| 314 | return; | ||||||||||||||||||||||||||||
| 315 | } or return $c->$cb("Invalid or incomplete token"); | ||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||
| 317 | foreach my $expected_scope (@$scopes) { | ||||||||||||||||||||||||||||
| 318 | unless ($access_token->has_scope($expected_scope)) { | ||||||||||||||||||||||||||||
| 319 | return $c->$cb("Insufficient scopes"); | ||||||||||||||||||||||||||||
| 320 | } | ||||||||||||||||||||||||||||
| 321 | } | ||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||
| 323 | return $c->$cb(); | ||||||||||||||||||||||||||||
| 324 | }, | ||||||||||||||||||||||||||||
| 325 | } | ||||||||||||||||||||||||||||
| 326 | }); | ||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||
| 328 | Another security definition that checks that the user has at least | ||||||||||||||||||||||||||||
| 329 | one expected role : | ||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||
| 331 | $app->plugin(OpenAPI => { | ||||||||||||||||||||||||||||
| 332 | url => "data:///swagger.yaml", | ||||||||||||||||||||||||||||
| 333 | security => { | ||||||||||||||||||||||||||||
| 334 | oidc => sub { | ||||||||||||||||||||||||||||
| 335 | my ($c, $definition, $roles_to_check, $cb) = @_; | ||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||
| 337 | my $user = try { | ||||||||||||||||||||||||||||
| 338 | my $access_token = $c->oidc->verify_token(); | ||||||||||||||||||||||||||||
| 339 | return $c->oidc->build_user_from_claims($access_token->claims); | ||||||||||||||||||||||||||||
| 340 | } | ||||||||||||||||||||||||||||
| 341 | catch { | ||||||||||||||||||||||||||||
| 342 | $c->log->warn("Token/User validation : $_"); | ||||||||||||||||||||||||||||
| 343 | return; | ||||||||||||||||||||||||||||
| 344 | } or return $c->$cb('Unauthorized'); | ||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||
| 346 | foreach my $role_to_check (@$roles_to_check) { | ||||||||||||||||||||||||||||
| 347 | if ($user->has_role($role_to_check)) { | ||||||||||||||||||||||||||||
| 348 | return $c->$cb(); | ||||||||||||||||||||||||||||
| 349 | } | ||||||||||||||||||||||||||||
| 350 | } | ||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||
| 352 | return $c->$cb("Insufficient roles"); | ||||||||||||||||||||||||||||
| 353 | }, | ||||||||||||||||||||||||||||
| 354 | } | ||||||||||||||||||||||||||||
| 355 | }); | ||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||
| 357 | =head1 SECURITY RECOMMENDATION | ||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||
| 359 | It is highly recommended to configure the framework to store session data, | ||||||||||||||||||||||||||||
| 360 | including sensitive tokens such as access and refresh tokens, on the backend | ||||||||||||||||||||||||||||
| 361 | rather than in client-side cookies. Although cookies can be signed and encrypted, | ||||||||||||||||||||||||||||
| 362 | storing tokens in the client exposes them to potential security threats. | ||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||
| 364 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||
| 366 | Sébastien Mourlhou | ||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||
| 368 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||
| 370 | Copyright (C) Sébastien Mourlhou | ||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||
| 372 | This program is free software, you can redistribute it and/or modify it under the terms of the Artistic License version 2.0. | ||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||
| 374 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||
| 376 | =over 2 | ||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||
| 378 | =item * L<Mojolicious::Plugin::OAuth2> | ||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||
| 380 | =back | ||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||
| 382 | =cut | ||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||
| 384 | 3 | 1; | |||||||||||||||||||||||||||