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