2016-12-30 09:01:55 +01:00
|
|
|
package Lemonldap::NG::Portal::Issuer::OpenIDConnect;
|
|
|
|
|
|
|
|
use strict;
|
2016-12-31 14:08:39 +01:00
|
|
|
use JSON;
|
2016-12-30 09:01:55 +01:00
|
|
|
use Mouse;
|
2017-03-10 12:39:19 +01:00
|
|
|
use Lemonldap::NG::Common::FormEncode;
|
2016-12-30 09:01:55 +01:00
|
|
|
use Lemonldap::NG::Portal::Main::Constants qw(
|
2017-01-24 22:02:06 +01:00
|
|
|
PE_BADURL
|
2016-12-31 08:57:24 +01:00
|
|
|
PE_CONFIRM
|
2016-12-30 09:01:55 +01:00
|
|
|
PE_ERROR
|
2016-12-31 08:57:24 +01:00
|
|
|
PE_LOGOUT_OK
|
2017-01-03 14:44:40 +01:00
|
|
|
PE_REDIRECT
|
2016-12-30 09:01:55 +01:00
|
|
|
PE_OK
|
2017-03-22 21:47:07 +01:00
|
|
|
PE_UNAUTHORIZEDPARTNER
|
2016-12-30 09:01:55 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
our $VERSION = '2.0.0';
|
|
|
|
|
|
|
|
extends 'Lemonldap::NG::Portal::Main::Issuer',
|
2017-01-07 19:04:20 +01:00
|
|
|
'Lemonldap::NG::Portal::Lib::OpenIDConnect',
|
|
|
|
'Lemonldap::NG::Common::Conf::AccessLib';
|
2016-12-30 09:01:55 +01:00
|
|
|
|
2017-02-08 14:51:46 +01:00
|
|
|
# INTERFACE
|
|
|
|
|
|
|
|
sub beforeAuth { 'exportRequestParameters' }
|
|
|
|
|
2016-12-30 09:01:55 +01:00
|
|
|
# INITIALIZATION
|
|
|
|
|
2017-03-06 13:18:06 +01:00
|
|
|
has configStorage => (
|
|
|
|
is => 'ro',
|
2017-03-27 18:51:18 +02:00
|
|
|
lazy => 1,
|
2017-03-06 13:18:06 +01:00
|
|
|
default => sub {
|
|
|
|
$_[0]->{p}->HANDLER->localConfig->{configStorage};
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2016-12-31 08:57:24 +01:00
|
|
|
# OIDC has 7 endpoints managed here as PSGI endpoints or in run() [Main/Issuer.pm
|
|
|
|
# manage transparent authentication for run()]:
|
|
|
|
# - authorize : in run()
|
|
|
|
# - logout : in run()
|
|
|
|
# => endSessionDone() for unauth users
|
|
|
|
# - checksession: => checkSession() for all
|
|
|
|
# - token : => token() for unauth users (RP)
|
|
|
|
# - userinfo : => userInfo() for unauth users (RP)
|
|
|
|
# - jwks : => jwks() for unauth users (RP)
|
|
|
|
# - register : => registration() for unauth users (RP)
|
|
|
|
#
|
|
|
|
# Other paths will be handle by run() and return PE_ERROR
|
2016-12-31 14:08:39 +01:00
|
|
|
#
|
|
|
|
# .well-known/openid-configuration is handled by metadata()
|
2016-12-31 08:57:24 +01:00
|
|
|
|
2016-12-30 09:01:55 +01:00
|
|
|
sub init {
|
|
|
|
my ($self) = @_;
|
|
|
|
|
|
|
|
# Initialize RP list
|
|
|
|
return 0
|
|
|
|
unless ( $self->Lemonldap::NG::Portal::Main::Issuer::init()
|
|
|
|
and $self->loadRPs );
|
|
|
|
|
|
|
|
# Manage RP requests
|
|
|
|
$self->addRouteFromConf(
|
|
|
|
'Unauth',
|
2016-12-31 08:57:24 +01:00
|
|
|
oidcServiceMetaDataEndSessionURI => 'endSessionDone',
|
|
|
|
oidcServiceMetaDataCheckSessionURI => 'checkSession',
|
2016-12-30 09:01:55 +01:00
|
|
|
oidcServiceMetaDataTokenURI => 'token',
|
|
|
|
oidcServiceMetaDataUserInfoURI => 'userInfo',
|
|
|
|
oidcServiceMetaDataJWKSURI => 'jwks',
|
|
|
|
oidcServiceMetaDataRegistrationURI => 'registration',
|
|
|
|
);
|
|
|
|
|
|
|
|
# Manage user requests
|
|
|
|
$self->addRouteFromConf(
|
|
|
|
'Auth',
|
2016-12-31 08:57:24 +01:00
|
|
|
oidcServiceMetaDataCheckSessionURI => 'checkSession',
|
2016-12-30 09:01:55 +01:00
|
|
|
oidcServiceMetaDataTokenURI => 'badAuthRequest',
|
|
|
|
oidcServiceMetaDataUserInfoURI => 'badAuthRequest',
|
|
|
|
oidcServiceMetaDataJWKSURI => 'badAuthRequest',
|
|
|
|
oidcServiceMetaDataRegistrationURI => 'badAuthRequest',
|
|
|
|
);
|
2016-12-31 14:08:39 +01:00
|
|
|
|
|
|
|
# Metadata (.well-known/openid-configuration)
|
|
|
|
$self->addUnauthRoute(
|
|
|
|
'.well-known' => { 'openid-configuration' => 'metadata' },
|
|
|
|
['GET']
|
|
|
|
);
|
|
|
|
$self->addAuthRoute(
|
|
|
|
'.well-known' => { 'openid-configuration' => 'metadata' },
|
|
|
|
['GET']
|
|
|
|
);
|
2016-12-30 09:01:55 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
# RUNNING METHODS
|
|
|
|
|
2016-12-31 08:57:24 +01:00
|
|
|
# Main method (launched only for authenticated users, see Main/Issuer.pm)
|
|
|
|
# run() manages only "authorize" and "logout" endpoints.
|
2016-12-30 09:01:55 +01:00
|
|
|
sub run {
|
|
|
|
my ( $self, $req, $path ) = @_;
|
|
|
|
if ($path) {
|
|
|
|
|
|
|
|
# AUTHORIZE
|
|
|
|
if ( $path eq $self->conf->{oidcServiceMetaDataAuthorizeURI} ) {
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug(
|
|
|
|
"URL detected as an OpenID Connect AUTHORIZE URL");
|
2016-12-31 08:57:24 +01:00
|
|
|
|
|
|
|
# Get and save parameters
|
|
|
|
my $oidc_request = {};
|
|
|
|
foreach my $param (
|
|
|
|
qw/response_type scope client_id state redirect_uri nonce
|
|
|
|
response_mode display prompt max_age ui_locales id_token_hint
|
2017-02-08 11:10:16 +01:00
|
|
|
login_hint acr_values request request_uri/
|
2016-12-31 08:57:24 +01:00
|
|
|
)
|
|
|
|
{
|
2017-01-01 10:43:48 +01:00
|
|
|
if ( $req->param($param) ) {
|
|
|
|
$oidc_request->{$param} = $req->param($param);
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug( "OIDC request parameter $param: "
|
|
|
|
. $oidc_request->{$param} );
|
2017-03-27 18:51:18 +02:00
|
|
|
$self->p->setHiddenFormValue( $req, $param,
|
|
|
|
$oidc_request->{$param},
|
|
|
|
'', 0 );
|
2017-01-01 10:43:48 +01:00
|
|
|
}
|
2016-12-31 08:57:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# Detect requested flow
|
|
|
|
my $response_type = $oidc_request->{'response_type'};
|
|
|
|
my $flow = $self->getFlowType($response_type);
|
|
|
|
|
|
|
|
unless ($flow) {
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->error("Unknown response type: $response_type");
|
2016-12-31 08:57:24 +01:00
|
|
|
return PE_ERROR;
|
|
|
|
}
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug(
|
|
|
|
"OIDC $flow flow requested (response type: $response_type)");
|
2016-12-31 08:57:24 +01:00
|
|
|
|
|
|
|
# Extract request_uri/request parameter
|
|
|
|
if ( $oidc_request->{'request_uri'} ) {
|
|
|
|
my $request =
|
|
|
|
$self->getRequestJWT( $oidc_request->{'request_uri'} );
|
|
|
|
|
|
|
|
if ($request) {
|
|
|
|
$oidc_request->{'request'} = $request;
|
|
|
|
}
|
|
|
|
else {
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->error("Error with Request URI resolution");
|
2016-12-31 08:57:24 +01:00
|
|
|
return PE_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $oidc_request->{'request'} ) {
|
|
|
|
my $request =
|
|
|
|
$self->getJWTJSONData( $oidc_request->{'request'} );
|
|
|
|
|
|
|
|
# Override OIDC parameters by request content
|
|
|
|
foreach ( keys %$request ) {
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug(
|
|
|
|
"Override $_ OIDC param by value present in request parameter"
|
2016-12-31 08:57:24 +01:00
|
|
|
);
|
|
|
|
$oidc_request->{$_} = $request->{$_};
|
2017-01-01 18:56:46 +01:00
|
|
|
$self->p->setHiddenFormValue( $_, $request->{$_}, '' );
|
2016-12-31 08:57:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Check all required parameters
|
|
|
|
unless ( $oidc_request->{'redirect_uri'} ) {
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->error("Redirect URI is required");
|
2016-12-31 08:57:24 +01:00
|
|
|
return PE_ERROR;
|
|
|
|
}
|
|
|
|
unless ( $oidc_request->{'scope'} ) {
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->error("Scope is required");
|
2016-12-31 08:57:24 +01:00
|
|
|
$self->returnRedirectError(
|
|
|
|
$req,
|
|
|
|
$oidc_request->{'redirect_uri'},
|
2017-03-09 22:56:43 +01:00
|
|
|
'invalid_request',
|
2016-12-31 08:57:24 +01:00
|
|
|
"scope required",
|
|
|
|
undef,
|
|
|
|
$oidc_request->{'state'},
|
|
|
|
( $flow ne "authorizationcode" )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
unless ( $oidc_request->{'client_id'} ) {
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->error("Client ID is required");
|
2016-12-31 08:57:24 +01:00
|
|
|
return $self->returnRedirectError(
|
|
|
|
$req,
|
|
|
|
$oidc_request->{'redirect_uri'},
|
2017-03-09 22:56:43 +01:00
|
|
|
'invalid_request',
|
2016-12-31 08:57:24 +01:00
|
|
|
"client_id required",
|
|
|
|
undef,
|
|
|
|
$oidc_request->{'state'},
|
|
|
|
( $flow ne "authorizationcode" )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if ( $flow eq "implicit" and not defined $oidc_request->{'nonce'} )
|
|
|
|
{
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->error("Nonce is required for implicit flow");
|
2016-12-31 08:57:24 +01:00
|
|
|
return $self->returnRedirectError(
|
|
|
|
$req, $oidc_request->{'redirect_uri'},
|
2017-03-09 22:56:43 +01:00
|
|
|
'invalid_request', "nonce required",
|
2016-12-31 08:57:24 +01:00
|
|
|
undef, $oidc_request->{'state'}, 1
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
# Check if flow is allowed
|
|
|
|
if ( $flow eq "authorizationcode"
|
|
|
|
and not $self->conf->{oidcServiceAllowAuthorizationCodeFlow} )
|
|
|
|
{
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->userLogger->error(
|
|
|
|
"Authorization code flow is not allowed");
|
2016-12-31 08:57:24 +01:00
|
|
|
return $self->returnRedirectError(
|
|
|
|
$req, $oidc_request->{'redirect_uri'},
|
|
|
|
"server_error", "Authorization code flow not allowed",
|
|
|
|
undef, $oidc_request->{'state'},
|
|
|
|
0
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if ( $flow eq "implicit"
|
|
|
|
and not $self->conf->{oidcServiceAllowImplicitFlow} )
|
|
|
|
{
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->error("Implicit flow is not allowed");
|
2016-12-31 08:57:24 +01:00
|
|
|
return $self->returnRedirectError(
|
|
|
|
$req, $oidc_request->{'redirect_uri'},
|
|
|
|
"server_error", "Implicit flow not allowed",
|
|
|
|
undef, $oidc_request->{'state'},
|
|
|
|
1
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if ( $flow eq "hybrid"
|
|
|
|
and not $self->conf->{oidcServiceAllowHybridFlow} )
|
|
|
|
{
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->error("Hybrid flow is not allowed");
|
2016-12-31 08:57:24 +01:00
|
|
|
return $self->returnRedirectError(
|
|
|
|
$req, $oidc_request->{'redirect_uri'},
|
|
|
|
"server_error", "Hybrid flow not allowed",
|
|
|
|
undef, $oidc_request->{'state'},
|
|
|
|
1
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
# Check if user needs to be reauthenticated
|
2017-03-28 19:09:46 +02:00
|
|
|
my $prompt = $oidc_request->{'prompt'};
|
|
|
|
if (
|
|
|
|
$prompt
|
|
|
|
and $prompt =~ /\blogin\b/
|
|
|
|
and (
|
|
|
|
time - $req->sessionInfo->{_utime} >
|
|
|
|
$self->conf->{portalForceAuthnInterval} )
|
|
|
|
)
|
|
|
|
{
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug(
|
|
|
|
"Reauthentication requested by Relying Party in prompt parameter"
|
2016-12-31 08:57:24 +01:00
|
|
|
);
|
2017-03-28 19:09:46 +02:00
|
|
|
return $self->reAuth($req);
|
2016-12-31 08:57:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
my $max_age = $oidc_request->{'max_age'};
|
|
|
|
my $_lastAuthnUTime = $req->{sessionInfo}->{_lastAuthnUTime};
|
|
|
|
if ( $max_age && time > $_lastAuthnUTime + $max_age ) {
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug(
|
|
|
|
"Reauthentication forced cause authentication time ($_lastAuthnUTime) is too old (>$max_age s)"
|
2016-12-31 08:57:24 +01:00
|
|
|
);
|
2017-03-28 19:09:46 +02:00
|
|
|
return $self->reAuth($req);
|
2016-12-31 08:57:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# Check openid scope
|
|
|
|
unless ( $oidc_request->{'scope'} =~ /\bopenid\b/ ) {
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug("No openid scope found");
|
2016-12-31 08:57:24 +01:00
|
|
|
|
|
|
|
#TODO manage standard OAuth request
|
|
|
|
return PE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
# Check client_id
|
|
|
|
my $client_id = $oidc_request->{'client_id'};
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug("Request from client id $client_id");
|
2016-12-31 08:57:24 +01:00
|
|
|
|
|
|
|
# Verify that client_id is registered in configuration
|
|
|
|
my $rp = $self->getRP($client_id);
|
|
|
|
|
|
|
|
unless ($rp) {
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->error(
|
|
|
|
"No registered Relying Party found with client_id $client_id"
|
2016-12-31 08:57:24 +01:00
|
|
|
);
|
|
|
|
return $self->returnRedirectError(
|
|
|
|
$req,
|
|
|
|
$oidc_request->{'redirect_uri'},
|
2017-03-09 22:56:43 +01:00
|
|
|
'invalid_request',
|
2016-12-31 08:57:24 +01:00
|
|
|
"client_id $client_id unknown",
|
|
|
|
undef,
|
|
|
|
$oidc_request->{'state'},
|
|
|
|
( $flow ne "authorizationcode" )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else {
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug("Client id $client_id match RP $rp");
|
2016-12-31 08:57:24 +01:00
|
|
|
}
|
|
|
|
|
2017-03-22 21:47:07 +01:00
|
|
|
# Check if this RP is authorizated
|
|
|
|
if ( my $rule = $self->rpRules->{$rp} ) {
|
2017-03-28 23:07:49 +02:00
|
|
|
unless ( $rule->( $req, $req->sessionInfo ) ) {
|
2017-03-22 21:47:07 +01:00
|
|
|
$self->userLogger->warn( 'User '
|
|
|
|
. $req->sessionInfo->{ $self->conf->{whatToTrace} }
|
|
|
|
. "was not authorizated to access to $rp" );
|
|
|
|
return PE_UNAUTHORIZEDPARTNER;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-31 08:57:24 +01:00
|
|
|
# Check Request JWT signature
|
|
|
|
if ( $oidc_request->{'request'} ) {
|
|
|
|
unless (
|
|
|
|
$self->verifyJWTSignature(
|
|
|
|
$oidc_request->{'request'},
|
|
|
|
undef, $rp
|
|
|
|
)
|
|
|
|
)
|
|
|
|
{
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->error(
|
|
|
|
"Request JWT signature could not be verified");
|
2016-12-31 08:57:24 +01:00
|
|
|
return PE_ERROR;
|
|
|
|
}
|
|
|
|
else {
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug("Request JWT signature verified");
|
2016-12-31 08:57:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Check redirect_uri
|
|
|
|
my $redirect_uri = $oidc_request->{'redirect_uri'};
|
|
|
|
my $redirect_uris = $self->conf->{oidcRPMetaDataOptions}->{$rp}
|
|
|
|
->{oidcRPMetaDataOptionsRedirectUris};
|
|
|
|
|
|
|
|
if ($redirect_uris) {
|
|
|
|
my $redirect_uri_allowed = 0;
|
|
|
|
foreach ( split( /\s+/, $redirect_uris ) ) {
|
|
|
|
$redirect_uri_allowed = 1 if $redirect_uri eq $_;
|
|
|
|
}
|
|
|
|
unless ($redirect_uri_allowed) {
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->userLogger->error(
|
|
|
|
"Redirect URI $redirect_uri not allowed");
|
2017-01-24 17:19:18 +01:00
|
|
|
return PE_BADURL;
|
2016-12-31 08:57:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Check id_token_hint
|
|
|
|
my $id_token_hint = $oidc_request->{'id_token_hint'};
|
|
|
|
if ($id_token_hint) {
|
|
|
|
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug("Check sub of ID Token $id_token_hint");
|
2016-12-31 08:57:24 +01:00
|
|
|
|
|
|
|
# Check that id_token_hint sub match current user
|
|
|
|
my $sub = $self->getIDTokenSub($id_token_hint);
|
|
|
|
my $user_id_attribute =
|
|
|
|
$self->conf->{oidcRPMetaDataOptions}->{$rp}
|
2017-01-03 20:00:35 +01:00
|
|
|
->{oidcRPMetaDataOptionsUserIDAttr}
|
|
|
|
|| $self->conf->{whatToTrace};
|
2016-12-31 08:57:24 +01:00
|
|
|
my $user_id = $req->{sessionInfo}->{$user_id_attribute};
|
|
|
|
unless ( $sub eq $user_id ) {
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->userLogger->error(
|
|
|
|
"ID Token hint sub $sub do not match user $user_id");
|
2016-12-31 08:57:24 +01:00
|
|
|
return $self->returnRedirectError(
|
|
|
|
$req,
|
|
|
|
$oidc_request->{'redirect_uri'},
|
2017-03-09 22:56:43 +01:00
|
|
|
'invalid_request',
|
2016-12-31 08:57:24 +01:00
|
|
|
"current user do not match id_token_hint sub",
|
|
|
|
undef,
|
|
|
|
$oidc_request->{'state'},
|
|
|
|
( $flow ne "authorizationcode" )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else {
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug(
|
|
|
|
"ID Token hint sub $sub match current user");
|
2016-12-31 08:57:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Obtain consent
|
|
|
|
my $bypassConsent = $self->conf->{oidcRPMetaDataOptions}->{$rp}
|
|
|
|
->{oidcRPMetaDataOptionsBypassConsent};
|
|
|
|
if ($bypassConsent) {
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug(
|
|
|
|
"Consent is disabled for RP $rp, user will not be prompted"
|
2016-12-31 08:57:24 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
my $ask_for_consent = 1;
|
|
|
|
if ( $req->{sessionInfo}->{"_oidc_consent_time_$rp"}
|
|
|
|
and $req->{sessionInfo}->{"_oidc_consent_scope_$rp"} )
|
|
|
|
{
|
|
|
|
$ask_for_consent = 0;
|
|
|
|
my $consent_time =
|
|
|
|
$req->{sessionInfo}->{"_oidc_consent_time_$rp"};
|
|
|
|
my $consent_scope =
|
|
|
|
$req->{sessionInfo}->{"_oidc_consent_scope_$rp"};
|
|
|
|
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug(
|
|
|
|
"Consent already given for Relying Party $rp (time: $consent_time, scope: $consent_scope)"
|
2016-12-31 08:57:24 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
# Check accepted scope
|
|
|
|
foreach my $requested_scope (
|
|
|
|
split( /\s+/, $oidc_request->{'scope'} ) )
|
|
|
|
{
|
|
|
|
if ( $consent_scope =~ /\b$requested_scope\b/ ) {
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug(
|
|
|
|
"Scope $requested_scope already accepted");
|
2016-12-31 08:57:24 +01:00
|
|
|
}
|
|
|
|
else {
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug(
|
|
|
|
"Scope $requested_scope was not previously accepted"
|
2016-12-31 08:57:24 +01:00
|
|
|
);
|
|
|
|
$ask_for_consent = 1;
|
|
|
|
last;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Check prompt parameter
|
|
|
|
$ask_for_consent = 1 if ( $prompt =~ /\bconsent\b/ );
|
|
|
|
}
|
|
|
|
if ($ask_for_consent) {
|
2017-01-01 10:43:48 +01:00
|
|
|
if ( $req->param('confirm')
|
|
|
|
and $req->param('confirm') == 1 )
|
|
|
|
{
|
|
|
|
$self->p->updatePersistentSession(
|
2016-12-31 08:57:24 +01:00
|
|
|
{ "_oidc_consent_time_$rp" => time } );
|
2017-01-01 10:43:48 +01:00
|
|
|
$self->p->updatePersistentSession(
|
2016-12-31 08:57:24 +01:00
|
|
|
{
|
|
|
|
"_oidc_consent_scope_$rp" =>
|
|
|
|
$oidc_request->{'scope'}
|
|
|
|
}
|
|
|
|
);
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug(
|
|
|
|
"Consent given for Relying Party $rp");
|
2016-12-31 08:57:24 +01:00
|
|
|
}
|
2017-01-01 10:43:48 +01:00
|
|
|
elsif ( $req->param('confirm')
|
|
|
|
and $req->param('confirm') == -1 )
|
|
|
|
{
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug(
|
|
|
|
"User refused consent for Relying party $rp");
|
2016-12-31 08:57:24 +01:00
|
|
|
return $self->returnRedirectError(
|
|
|
|
$req,
|
|
|
|
$oidc_request->{'redirect_uri'},
|
|
|
|
"consent_required",
|
|
|
|
"consent not given",
|
|
|
|
undef,
|
|
|
|
$oidc_request->{'state'},
|
|
|
|
( $flow ne "authorizationcode" )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else {
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug(
|
|
|
|
"Obtain user consent for Relying Party $rp");
|
2016-12-31 08:57:24 +01:00
|
|
|
|
|
|
|
# Return error if prompt is none
|
2017-01-01 10:43:48 +01:00
|
|
|
if ( $prompt and $prompt =~ /\bnone\b/ ) {
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug(
|
|
|
|
"Consent is needed but prompt is none");
|
2016-12-31 08:57:24 +01:00
|
|
|
return $self->returnRedirectError(
|
|
|
|
$req,
|
|
|
|
$oidc_request->{'redirect_uri'},
|
|
|
|
"consent_required",
|
|
|
|
"consent required",
|
|
|
|
undef,
|
|
|
|
$oidc_request->{'state'},
|
|
|
|
( $flow ne "authorizationcode" )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
my $display_name =
|
|
|
|
$self->conf->{oidcRPMetaDataOptions}->{$rp}
|
|
|
|
->{oidcRPMetaDataOptionsDisplayName};
|
|
|
|
my $icon = $self->conf->{oidcRPMetaDataOptions}->{$rp}
|
|
|
|
->{oidcRPMetaDataOptionsIcon};
|
|
|
|
my $img_src;
|
|
|
|
|
|
|
|
if ($icon) {
|
|
|
|
$img_src =
|
|
|
|
( $icon =~ m#^https?://# )
|
|
|
|
? $icon
|
|
|
|
: $self->p->staticPrefic . "/common/" . $icon;
|
|
|
|
}
|
|
|
|
|
|
|
|
# HERE
|
2017-01-01 10:43:48 +01:00
|
|
|
$req->info('<div class="oidc_consent_message">');
|
|
|
|
$req->info( '<img src="' . $img_src . '" />' )
|
2016-12-31 08:57:24 +01:00
|
|
|
if $img_src;
|
2017-01-01 10:43:48 +01:00
|
|
|
$req->info(
|
2016-12-31 08:57:24 +01:00
|
|
|
qq'<h3 trspan="oidcConsent,$display_name">The application $display_name would like to know:</h3><ul>'
|
|
|
|
);
|
|
|
|
|
|
|
|
my $scope_messages = {
|
|
|
|
openid => 'yourIdentity',
|
|
|
|
profile => 'yourProfile',
|
|
|
|
email => 'yourEmail',
|
|
|
|
address => 'yourAddress',
|
|
|
|
phone => 'yourPhone',
|
|
|
|
};
|
|
|
|
foreach my $requested_scope (
|
|
|
|
split( /\s/, $oidc_request->{'scope'} ) )
|
|
|
|
{
|
|
|
|
my $message = $scope_messages->{$requested_scope}
|
|
|
|
|| 'anotherInformation';
|
2017-01-01 10:43:48 +01:00
|
|
|
$req->info(
|
2016-12-31 08:57:24 +01:00
|
|
|
qq'<li trspan="$message ">$message</li>');
|
|
|
|
}
|
2017-01-01 10:43:48 +01:00
|
|
|
$req->info('</ul></div>');
|
2016-12-31 08:57:24 +01:00
|
|
|
$req->datas->{activeTimer} = 0;
|
|
|
|
return PE_CONFIRM;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Create session_state
|
|
|
|
my $session_state =
|
|
|
|
$self->createSessionState( $req->id, $client_id );
|
|
|
|
|
|
|
|
# Authorization Code Flow
|
|
|
|
if ( $flow eq "authorizationcode" ) {
|
|
|
|
|
|
|
|
# Store data in session
|
2017-03-03 18:25:03 +01:00
|
|
|
my $codeSession = $self->getOpenIDConnectSession(
|
|
|
|
undef,
|
2016-12-31 08:57:24 +01:00
|
|
|
{
|
|
|
|
redirect_uri => $oidc_request->{'redirect_uri'},
|
|
|
|
scope => $oidc_request->{'scope'},
|
|
|
|
user_session_id => $req->id,
|
|
|
|
_utime => time,
|
|
|
|
nonce => $oidc_request->{'nonce'},
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2017-02-20 22:00:05 +01:00
|
|
|
# Generate code
|
2017-03-03 18:25:03 +01:00
|
|
|
my $code = $codeSession->id();
|
2017-02-20 22:00:05 +01:00
|
|
|
|
|
|
|
$self->logger->debug("Generated code: $code");
|
|
|
|
|
2016-12-31 08:57:24 +01:00
|
|
|
# Build Response
|
|
|
|
my $response_url = $self->buildAuthorizationCodeAuthnResponse(
|
|
|
|
$oidc_request->{'redirect_uri'},
|
|
|
|
$code, $oidc_request->{'state'},
|
|
|
|
$session_state
|
|
|
|
);
|
|
|
|
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug("Redirect user to $response_url");
|
2016-12-31 08:57:24 +01:00
|
|
|
$req->urldc($response_url);
|
|
|
|
|
2017-01-03 14:44:40 +01:00
|
|
|
return PE_REDIRECT;
|
2016-12-31 08:57:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# Implicit Flow
|
|
|
|
if ( $flow eq "implicit" ) {
|
|
|
|
|
|
|
|
my $access_token;
|
|
|
|
my $at_hash;
|
|
|
|
|
2017-01-03 14:44:22 +01:00
|
|
|
if ( $response_type =~ /\btoken\b/ ) {
|
2016-12-31 08:57:24 +01:00
|
|
|
|
2017-02-20 22:00:05 +01:00
|
|
|
# Store data in access token
|
2016-12-31 08:57:24 +01:00
|
|
|
# Generate access_token
|
2017-03-03 18:25:03 +01:00
|
|
|
my $accessTokenSession = $self->getOpenIDConnectSession(
|
|
|
|
undef,
|
2017-02-20 22:00:05 +01:00
|
|
|
{
|
|
|
|
scope => $oidc_request->{'scope'},
|
|
|
|
rp => $rp,
|
|
|
|
user_session_id => $req->id,
|
|
|
|
_utime => time,
|
|
|
|
}
|
|
|
|
);
|
2016-12-31 08:57:24 +01:00
|
|
|
|
|
|
|
unless ($accessTokenSession) {
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->error(
|
|
|
|
"Unable to create OIDC session for access_token");
|
2016-12-31 08:57:24 +01:00
|
|
|
$self->returnRedirectError( $req,
|
|
|
|
$oidc_request->{'redirect_uri'},
|
|
|
|
"server_error", undef, undef,
|
|
|
|
$oidc_request->{'state'}, 1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
$access_token = $accessTokenSession->id;
|
|
|
|
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug(
|
|
|
|
"Generated access token: $access_token");
|
2016-12-31 08:57:24 +01:00
|
|
|
|
|
|
|
# Compute hash to store in at_hash
|
|
|
|
my $alg = $self->conf->{oidcRPMetaDataOptions}->{$rp}
|
|
|
|
->{oidcRPMetaDataOptionsIDTokenSignAlg};
|
|
|
|
my ($hash_level) = ( $alg =~ /(?:\w{2})(\d{3})/ );
|
|
|
|
$at_hash = $self->createHash( $access_token, $hash_level );
|
|
|
|
}
|
|
|
|
|
|
|
|
# ID token payload
|
|
|
|
my $id_token_exp = $self->conf->{oidcRPMetaDataOptions}->{$rp}
|
|
|
|
->{oidcRPMetaDataOptionsIDTokenExpiration};
|
|
|
|
$id_token_exp += time;
|
|
|
|
|
|
|
|
my $authenticationLevel =
|
|
|
|
$req->{sessionInfo}->{authenticationLevel};
|
|
|
|
my $id_token_acr;
|
|
|
|
foreach (
|
|
|
|
keys %{ $self->conf->{oidcServiceMetaDataAuthnContext} } )
|
|
|
|
{
|
|
|
|
if ( $self->conf->{oidcServiceMetaDataAuthnContext}->{$_} eq
|
|
|
|
$authenticationLevel )
|
|
|
|
{
|
|
|
|
$id_token_acr = $_;
|
|
|
|
last;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
my $user_id_attribute =
|
|
|
|
$self->conf->{oidcRPMetaDataOptions}->{$rp}
|
|
|
|
->{oidcRPMetaDataOptionsUserIDAttr}
|
|
|
|
|| $self->conf->{whatToTrace};
|
|
|
|
my $user_id = $req->{sessionInfo}->{$user_id_attribute};
|
|
|
|
|
|
|
|
my $id_token_payload_hash = {
|
|
|
|
iss => $self->conf->{oidcServiceMetaDataIssuer}
|
|
|
|
, # Issuer Identifier
|
|
|
|
sub => $user_id, # Subject Identifier
|
|
|
|
aud => [$client_id], # Audience
|
|
|
|
exp => $id_token_exp, # expiration
|
|
|
|
iat => time, # Issued time
|
|
|
|
auth_time => $req->{sessionInfo}->{_lastAuthnUTime}
|
|
|
|
, # Authentication time
|
|
|
|
azp => $client_id, # Authorized party
|
|
|
|
# TODO amr
|
|
|
|
nonce => $oidc_request->{'nonce'} # Nonce
|
|
|
|
};
|
|
|
|
|
|
|
|
$id_token_payload_hash->{'at_hash'} = $at_hash if $at_hash;
|
|
|
|
$id_token_payload_hash->{'acr'} = $id_token_acr
|
|
|
|
if $id_token_acr;
|
|
|
|
|
|
|
|
# Create ID Token
|
|
|
|
my $id_token =
|
|
|
|
$self->createIDToken( $id_token_payload_hash, $rp );
|
|
|
|
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug("Generated id token: $id_token");
|
2016-12-31 08:57:24 +01:00
|
|
|
|
|
|
|
# Send token response
|
|
|
|
my $expires_in = $self->conf->{oidcRPMetaDataOptions}->{$rp}
|
|
|
|
->{oidcRPMetaDataOptionsAccessTokenExpiration};
|
|
|
|
|
|
|
|
# Build Response
|
|
|
|
my $response_url = $self->buildImplicitAuthnResponse(
|
|
|
|
$oidc_request->{'redirect_uri'},
|
|
|
|
$access_token, $id_token, $expires_in,
|
|
|
|
$oidc_request->{'state'},
|
|
|
|
$session_state
|
|
|
|
);
|
|
|
|
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug("Redirect user to $response_url");
|
2017-01-03 14:44:40 +01:00
|
|
|
$req->urldc($response_url);
|
2016-12-31 08:57:24 +01:00
|
|
|
|
2017-01-03 14:44:40 +01:00
|
|
|
return PE_REDIRECT;
|
2016-12-31 08:57:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# Hybrid Flow
|
|
|
|
if ( $flow eq "hybrid" ) {
|
|
|
|
|
|
|
|
my $access_token;
|
|
|
|
my $id_token;
|
|
|
|
my $at_hash;
|
|
|
|
my $c_hash;
|
|
|
|
|
|
|
|
# Hash level
|
|
|
|
my $alg = $self->conf->{oidcRPMetaDataOptions}->{$rp}
|
|
|
|
->{oidcRPMetaDataOptionsIDTokenSignAlg};
|
|
|
|
my ($hash_level) = ( $alg =~ /(?:\w{2})(\d{3})/ );
|
|
|
|
|
|
|
|
# Store data in session
|
2017-03-03 18:25:03 +01:00
|
|
|
my $codeSession = $self->getOpenIDConnectSession(
|
|
|
|
undef,
|
2016-12-31 08:57:24 +01:00
|
|
|
{
|
|
|
|
redirect_uri => $oidc_request->{'redirect_uri'},
|
|
|
|
scope => $oidc_request->{'scope'},
|
|
|
|
user_session_id => $req->id,
|
|
|
|
_utime => time,
|
|
|
|
nonce => $oidc_request->{'nonce'},
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2017-02-20 22:00:05 +01:00
|
|
|
# Generate code
|
2017-03-03 18:25:03 +01:00
|
|
|
my $code = $codeSession->id();
|
2017-02-20 22:00:05 +01:00
|
|
|
|
|
|
|
$self->logger->debug("Generated code: $code");
|
|
|
|
|
2016-12-31 08:57:24 +01:00
|
|
|
# Compute hash to store in c_hash
|
|
|
|
$c_hash = $self->createHash( $code, $hash_level );
|
|
|
|
|
|
|
|
if ( $response_type =~ /\btoken\b/ ) {
|
|
|
|
|
|
|
|
# Generate access_token
|
2017-03-03 18:25:03 +01:00
|
|
|
my $accessTokenSession = $self->getOpenIDConnectSession(
|
|
|
|
undef,
|
2017-02-20 22:00:05 +01:00
|
|
|
{
|
|
|
|
scope => $oidc_request->{'scope'},
|
|
|
|
rp => $rp,
|
|
|
|
user_session_id => $req->id,
|
|
|
|
_utime => time,
|
|
|
|
}
|
|
|
|
);
|
2016-12-31 08:57:24 +01:00
|
|
|
|
|
|
|
unless ($accessTokenSession) {
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->error(
|
|
|
|
"Unable to create OIDC session for access_token");
|
2016-12-31 08:57:24 +01:00
|
|
|
return $self->returnRedirectError( $req,
|
|
|
|
$oidc_request->{'redirect_uri'},
|
|
|
|
"server_error", undef, undef,
|
|
|
|
$oidc_request->{'state'}, 1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
$access_token = $accessTokenSession->id;
|
|
|
|
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug(
|
|
|
|
"Generated access token: $access_token");
|
2016-12-31 08:57:24 +01:00
|
|
|
|
|
|
|
# Compute hash to store in at_hash
|
|
|
|
$at_hash = $self->createHash( $access_token, $hash_level );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $response_type =~ /\bid_token\b/ ) {
|
|
|
|
|
|
|
|
# ID token payload
|
|
|
|
my $id_token_exp =
|
|
|
|
$self->conf->{oidcRPMetaDataOptions}->{$rp}
|
|
|
|
->{oidcRPMetaDataOptionsIDTokenExpiration};
|
|
|
|
$id_token_exp += time;
|
|
|
|
|
|
|
|
my $id_token_acr =
|
|
|
|
"loa-" . $req->{sessionInfo}->{authenticationLevel};
|
|
|
|
|
|
|
|
my $user_id_attribute =
|
|
|
|
$self->conf->{oidcRPMetaDataOptions}->{$rp}
|
|
|
|
->{oidcRPMetaDataOptionsUserIDAttr}
|
2017-01-03 20:00:35 +01:00
|
|
|
|| $self->conf->{whatToTrace};
|
2016-12-31 08:57:24 +01:00
|
|
|
my $user_id = $req->{sessionInfo}->{$user_id_attribute};
|
|
|
|
|
|
|
|
my $id_token_payload_hash = {
|
|
|
|
iss => $self->conf->{oidcServiceMetaDataIssuer}
|
|
|
|
, # Issuer Identifier
|
|
|
|
sub => $user_id, # Subject Identifier
|
|
|
|
aud => [$client_id], # Audience
|
|
|
|
exp => $id_token_exp, # expiration
|
|
|
|
iat => time, # Issued time
|
|
|
|
auth_time => $req->{sessionInfo}->{_lastAuthnUTime}
|
|
|
|
, # Authentication time
|
|
|
|
acr => $id_token_acr
|
|
|
|
, # Authentication Context Class Reference
|
|
|
|
azp => $client_id, # Authorized party
|
|
|
|
# TODO amr
|
|
|
|
nonce => $oidc_request->{'nonce'} # Nonce
|
|
|
|
};
|
|
|
|
|
|
|
|
$id_token_payload_hash->{'at_hash'} = $at_hash if $at_hash;
|
|
|
|
$id_token_payload_hash->{'c_hash'} = $c_hash if $c_hash;
|
|
|
|
|
|
|
|
# Create ID Token
|
|
|
|
$id_token =
|
|
|
|
$self->createIDToken( $id_token_payload_hash, $rp );
|
|
|
|
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug("Generated id token: $id_token");
|
2016-12-31 08:57:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
my $expires_in = $self->conf->{oidcRPMetaDataOptions}->{$rp}
|
|
|
|
->{oidcRPMetaDataOptionsAccessTokenExpiration};
|
|
|
|
|
|
|
|
# Build Response
|
|
|
|
my $response_url = $self->buildHybridAuthnResponse(
|
|
|
|
$oidc_request->{'redirect_uri'}, $code,
|
|
|
|
$access_token, $id_token,
|
|
|
|
$expires_in, $oidc_request->{'state'},
|
|
|
|
$session_state
|
|
|
|
);
|
|
|
|
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug("Redirect user to $response_url");
|
2016-12-31 08:57:24 +01:00
|
|
|
$req->urldc($response_url);
|
2017-01-03 14:44:40 +01:00
|
|
|
return PE_REDIRECT;
|
2016-12-31 08:57:24 +01:00
|
|
|
}
|
|
|
|
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug("No flow has been selected");
|
2016-12-31 08:57:24 +01:00
|
|
|
return PE_OK;
|
2016-12-30 09:01:55 +01:00
|
|
|
}
|
2016-12-31 08:57:24 +01:00
|
|
|
|
|
|
|
# LOGOUT
|
2016-12-30 09:01:55 +01:00
|
|
|
elsif ( $path eq $self->conf->{oidcServiceMetaDataEndSessionURI} ) {
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug(
|
|
|
|
"URL detected as an OpenID Connect END SESSION URL");
|
2016-12-31 08:57:24 +01:00
|
|
|
|
|
|
|
# Set hidden fields
|
|
|
|
my $oidc_request = {};
|
|
|
|
foreach my $param (qw/id_token_hint post_logout_redirect_uri state/)
|
|
|
|
{
|
2017-01-01 18:56:46 +01:00
|
|
|
if ( $oidc_request->{$param} = $req->param($param) ) {
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug( "OIDC request parameter $param: "
|
|
|
|
. $oidc_request->{$param} );
|
2017-01-01 18:56:46 +01:00
|
|
|
$self->p->setHiddenFormValue( $param,
|
|
|
|
$oidc_request->{$param}, '' );
|
|
|
|
}
|
2016-12-31 08:57:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
my $post_logout_redirect_uri =
|
|
|
|
$oidc_request->{'post_logout_redirect_uri'};
|
|
|
|
my $state = $oidc_request->{'state'};
|
|
|
|
|
|
|
|
# Ask consent for logout
|
|
|
|
if ( $req->param('confirm') ) {
|
2017-03-21 20:36:10 +01:00
|
|
|
my $err;
|
2017-01-01 10:43:48 +01:00
|
|
|
if ( $req->param('confirm') == 1 ) {
|
2017-03-08 23:49:00 +01:00
|
|
|
$req->steps(
|
|
|
|
[
|
|
|
|
@{ $self->p->beforeLogout }, 'authLogout',
|
|
|
|
'deleteSession'
|
|
|
|
]
|
|
|
|
);
|
2017-03-21 20:36:10 +01:00
|
|
|
$err = $req->error( $self->p->process($req) );
|
2017-03-08 23:49:00 +01:00
|
|
|
if ( $err > 0 ) {
|
|
|
|
$self->logger->error(
|
|
|
|
"Logout process returns error code $err");
|
|
|
|
return PE_ERROR;
|
|
|
|
}
|
2016-12-31 08:57:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($post_logout_redirect_uri) {
|
|
|
|
|
|
|
|
# Build Response
|
|
|
|
my $response_url =
|
|
|
|
$self->buildLogoutResponse( $post_logout_redirect_uri,
|
|
|
|
$state );
|
|
|
|
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug("Redirect user to $response_url");
|
2016-12-31 08:57:24 +01:00
|
|
|
$req->urldc($response_url);
|
2017-01-03 14:44:40 +01:00
|
|
|
return PE_REDIRECT;
|
2016-12-31 08:57:24 +01:00
|
|
|
}
|
2017-03-27 18:51:18 +02:00
|
|
|
return $req->param('confirm') == 1
|
|
|
|
? ( $err ? $err : PE_LOGOUT_OK )
|
|
|
|
: PE_OK;
|
2016-12-31 08:57:24 +01:00
|
|
|
}
|
|
|
|
|
2017-01-01 10:43:48 +01:00
|
|
|
$req->info(
|
2016-12-31 08:57:24 +01:00
|
|
|
'<div class="oidc_logout_message"><h3 trspan="logoutConfirm">Do you want to logout?</h3></div>'
|
|
|
|
);
|
|
|
|
$req->datas->{activeTimer} = 0;
|
|
|
|
return PE_CONFIRM;
|
2016-12-30 09:01:55 +01:00
|
|
|
}
|
|
|
|
}
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->error("Unknown OIDC endpoint $path, skipping");
|
2016-12-31 08:57:24 +01:00
|
|
|
return PE_ERROR;
|
2016-12-30 09:01:55 +01:00
|
|
|
}
|
|
|
|
|
2016-12-31 08:57:24 +01:00
|
|
|
# Handle token endpoint
|
2016-12-30 09:01:55 +01:00
|
|
|
sub token {
|
|
|
|
my ( $self, $req ) = @_;
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug("URL detected as an OpenID Connect TOKEN URL");
|
2016-12-30 09:39:13 +01:00
|
|
|
|
|
|
|
# Check authentication
|
|
|
|
my ( $client_id, $client_secret ) =
|
|
|
|
$self->getEndPointAuthenticationCredentials($req);
|
|
|
|
|
|
|
|
unless ( $client_id && $client_secret ) {
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->error(
|
|
|
|
"No authentication provided to get token, or authentication type not supported"
|
2016-12-30 09:39:13 +01:00
|
|
|
);
|
2017-03-09 22:56:43 +01:00
|
|
|
return $self->p->sendError( $req, 'invalid_request', 400 );
|
2016-12-30 09:39:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# Verify that client_id is registered in configuration
|
|
|
|
my $rp = $self->getRP($client_id);
|
|
|
|
|
|
|
|
unless ($rp) {
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->userLogger->error(
|
|
|
|
"No registered Relying Party found with client_id $client_id");
|
2017-03-09 22:56:43 +01:00
|
|
|
return $self->p->sendError( $req, 'invalid_request', 400 );
|
2016-12-30 09:39:13 +01:00
|
|
|
}
|
|
|
|
else {
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug("Client id $client_id match RP $rp");
|
2016-12-30 09:39:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# Check client_secret
|
|
|
|
unless ( $client_secret eq $self->conf->{oidcRPMetaDataOptions}->{$rp}
|
|
|
|
->{oidcRPMetaDataOptionsClientSecret} )
|
|
|
|
{
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->error("Wrong credentials for $rp");
|
2017-03-09 22:56:43 +01:00
|
|
|
return $self->p->sendError( 'invalid_request', 400 );
|
2016-12-30 09:39:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# Get code session
|
2017-01-01 10:43:48 +01:00
|
|
|
my $code = $req->param('code');
|
2016-12-30 09:39:13 +01:00
|
|
|
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug("OpenID Connect Code: $code");
|
2016-12-30 09:39:13 +01:00
|
|
|
|
|
|
|
my $codeSession = $self->getOpenIDConnectSession($code);
|
|
|
|
|
|
|
|
unless ($codeSession) {
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->error("Unable to find OIDC session $code");
|
2017-03-09 22:56:43 +01:00
|
|
|
$self->p->sendError( $req, 'invalid_request', 400 );
|
2016-12-30 09:39:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# Check we have the same redirect_uri value
|
2016-12-30 09:39:15 +01:00
|
|
|
unless ( $req->param("redirect_uri") eq $codeSession->data->{redirect_uri} )
|
2016-12-30 09:39:13 +01:00
|
|
|
{
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->userLogger->error( "Provided redirect_uri is different from "
|
|
|
|
. $codeSession->{redirect_uri} );
|
2017-03-09 22:56:43 +01:00
|
|
|
$self->p->sendError( $req, 'invalid_request', 400 );
|
2016-12-30 09:39:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# Get user identifier
|
|
|
|
my $apacheSession =
|
2017-01-24 06:10:57 +01:00
|
|
|
$self->p->getApacheSession( $codeSession->data->{user_session_id},
|
|
|
|
noInfo => 1 );
|
2016-12-30 09:39:13 +01:00
|
|
|
|
|
|
|
unless ($apacheSession) {
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->userLogger->error(
|
|
|
|
"Unable to find user session linked to OIDC session $code");
|
2016-12-30 09:39:13 +01:00
|
|
|
$codeSession->remove();
|
2017-03-09 22:56:43 +01:00
|
|
|
$self->p->sendError( $req, 'invalid_request', 400 );
|
2016-12-30 09:39:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
my $user_id_attribute =
|
2016-12-30 09:39:15 +01:00
|
|
|
$self->conf->{oidcRPMetaDataOptions}->{$rp}
|
|
|
|
->{oidcRPMetaDataOptionsUserIDAttr}
|
2016-12-30 09:39:13 +01:00
|
|
|
|| $self->conf->{whatToTrace};
|
|
|
|
my $user_id = $apacheSession->data->{$user_id_attribute};
|
|
|
|
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug("Found corresponding user: $user_id");
|
2016-12-30 09:39:13 +01:00
|
|
|
|
|
|
|
# Generate access_token
|
2017-03-03 18:25:03 +01:00
|
|
|
my $accessTokenSession = $self->getOpenIDConnectSession(
|
|
|
|
undef,
|
2016-12-30 09:39:13 +01:00
|
|
|
{
|
|
|
|
scope => $codeSession->data->{scope},
|
|
|
|
rp => $rp,
|
|
|
|
user_session_id => $apacheSession->id,
|
|
|
|
_utime => time,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2017-02-20 22:00:05 +01:00
|
|
|
unless ($accessTokenSession) {
|
|
|
|
$self->userLogger->error(
|
|
|
|
"Unable to create OIDC session for access_token");
|
|
|
|
$codeSession->remove();
|
2017-03-09 22:56:43 +01:00
|
|
|
$self->p->sendError( $req, 'invalid_request', 400 );
|
2017-02-20 22:00:05 +01:00
|
|
|
}
|
|
|
|
|
2016-12-30 09:39:13 +01:00
|
|
|
my $access_token = $accessTokenSession->id;
|
|
|
|
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug("Generated access token: $access_token");
|
2016-12-30 09:39:13 +01:00
|
|
|
|
|
|
|
# Compute hash to store in at_hash
|
|
|
|
my $alg = $self->conf->{oidcRPMetaDataOptions}->{$rp}
|
|
|
|
->{oidcRPMetaDataOptionsIDTokenSignAlg};
|
|
|
|
my ($hash_level) = ( $alg =~ /(?:\w{2})(\d{3})/ );
|
|
|
|
my $at_hash = $self->createHash( $access_token, $hash_level );
|
|
|
|
|
|
|
|
# ID token payload
|
|
|
|
my $id_token_exp = $self->conf->{oidcRPMetaDataOptions}->{$rp}
|
|
|
|
->{oidcRPMetaDataOptionsIDTokenExpiration};
|
|
|
|
$id_token_exp += time;
|
|
|
|
|
|
|
|
my $id_token_acr = "loa-" . $apacheSession->data->{authenticationLevel};
|
|
|
|
|
|
|
|
my $id_token_payload_hash = {
|
2016-12-30 09:39:15 +01:00
|
|
|
iss => $self->conf->{oidcServiceMetaDataIssuer}, # Issuer Identifier
|
|
|
|
sub => $user_id, # Subject Identifier
|
|
|
|
aud => [$client_id], # Audience
|
|
|
|
exp => $id_token_exp, # expiration
|
|
|
|
iat => time, # Issued time
|
2017-02-15 07:41:50 +01:00
|
|
|
auth_time => $apacheSession->data->{_lastAuthnUTime}
|
|
|
|
, # Authentication time
|
2016-12-30 09:39:13 +01:00
|
|
|
acr => $id_token_acr, # Authentication Context Class Reference
|
|
|
|
azp => $client_id, # Authorized party
|
|
|
|
# TODO amr
|
|
|
|
};
|
|
|
|
|
|
|
|
my $nonce = $codeSession->data->{nonce};
|
|
|
|
$id_token_payload_hash->{nonce} = $nonce if defined $nonce;
|
|
|
|
$id_token_payload_hash->{'at_hash'} = $at_hash if $at_hash;
|
|
|
|
|
|
|
|
# Create ID Token
|
|
|
|
my $id_token = $self->createIDToken( $id_token_payload_hash, $rp );
|
|
|
|
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug("Generated id token: $id_token");
|
2016-12-30 09:39:13 +01:00
|
|
|
|
|
|
|
# Send token response
|
|
|
|
my $expires_in = $self->conf->{oidcRPMetaDataOptions}->{$rp}
|
|
|
|
->{oidcRPMetaDataOptionsAccessTokenExpiration};
|
|
|
|
|
|
|
|
my $token_response = {
|
|
|
|
access_token => $access_token,
|
|
|
|
token_type => 'Bearer',
|
|
|
|
expires_in => $expires_in,
|
|
|
|
id_token => $id_token,
|
|
|
|
};
|
|
|
|
|
2017-03-09 22:56:51 +01:00
|
|
|
my $cRP = join ',', $rp, $apacheSession->data->{_oidcConnectedRP} || '';
|
|
|
|
$self->p->updateSession( $req, { _oidcConnectedRP => $cRP },
|
|
|
|
$apacheSession->id );
|
2017-03-09 22:56:47 +01:00
|
|
|
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug("Send token response");
|
2016-12-30 09:39:13 +01:00
|
|
|
|
|
|
|
$codeSession->remove();
|
2016-12-30 09:39:15 +01:00
|
|
|
return $self->p->sendJSONresponse( $req, $token_response );
|
2016-12-30 09:01:55 +01:00
|
|
|
}
|
|
|
|
|
2016-12-31 08:57:24 +01:00
|
|
|
# Handle uerinfo endpoint
|
2016-12-30 09:01:55 +01:00
|
|
|
sub userInfo {
|
|
|
|
my ( $self, $req ) = @_;
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug("URL detected as an OpenID Connect USERINFO URL");
|
2016-12-30 09:39:15 +01:00
|
|
|
|
|
|
|
my $access_token = $self->getEndPointAccessToken($req);
|
|
|
|
|
|
|
|
unless ($access_token) {
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->error("Unable to get access_token");
|
2017-03-09 22:56:43 +01:00
|
|
|
return $self->returnBearerError( 'invalid_request',
|
|
|
|
"Access token not found in request", 401 );
|
2016-12-30 09:39:15 +01:00
|
|
|
}
|
|
|
|
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug("Received Access Token $access_token");
|
2016-12-30 09:39:15 +01:00
|
|
|
|
|
|
|
my $accessTokenSession = $self->getOpenIDConnectSession($access_token);
|
|
|
|
|
|
|
|
unless ($accessTokenSession) {
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->userLogger->error(
|
|
|
|
"Unable to get access token session for id $access_token");
|
2017-03-09 22:56:43 +01:00
|
|
|
return $self->returnBearerError( 'invalid_request',
|
|
|
|
'Invalid request', 401 );
|
2016-12-30 09:39:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# Get access token session data
|
|
|
|
my $scope = $accessTokenSession->data->{scope};
|
|
|
|
my $rp = $accessTokenSession->data->{rp};
|
|
|
|
my $user_session_id = $accessTokenSession->data->{user_session_id};
|
|
|
|
|
|
|
|
my $userinfo_response =
|
|
|
|
$self->buildUserInfoResponse( $scope, $rp, $user_session_id );
|
|
|
|
unless ($userinfo_response) {
|
2017-03-09 22:56:43 +01:00
|
|
|
return $self->returnBearerError( 'invalid_request', 'Invalid request',
|
|
|
|
401 );
|
2016-12-30 09:39:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
my $userinfo_sign_alg = $self->conf->{oidcRPMetaDataOptions}->{$rp}
|
|
|
|
->{oidcRPMetaDataOptionsUserInfoSignAlg};
|
|
|
|
|
|
|
|
unless ($userinfo_sign_alg) {
|
|
|
|
return $self->p->sendJSONresponse( $req, $userinfo_response );
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
my $userinfo_jwt =
|
|
|
|
$self->createJWT( $userinfo_response, $userinfo_sign_alg, $rp );
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug("Return UserInfo as JWT: $userinfo_jwt");
|
2016-12-30 09:39:15 +01:00
|
|
|
return [
|
|
|
|
200,
|
|
|
|
[
|
|
|
|
'Content-Type' => 'application/jwt',
|
|
|
|
'Content-Length' => length($userinfo_jwt)
|
|
|
|
],
|
|
|
|
[$userinfo_jwt]
|
|
|
|
];
|
|
|
|
}
|
2016-12-30 09:01:55 +01:00
|
|
|
}
|
|
|
|
|
2016-12-31 08:57:24 +01:00
|
|
|
# Handle jwks endpoint
|
2016-12-30 09:01:55 +01:00
|
|
|
sub jwks {
|
|
|
|
my ( $self, $req ) = @_;
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug("URL detected as an OpenID Connect JWKS URL");
|
2016-12-31 08:57:24 +01:00
|
|
|
|
|
|
|
my $jwks = { keys => [] };
|
|
|
|
|
|
|
|
my $public_key_sig = $self->conf->{oidcServicePublicKeySig};
|
|
|
|
my $key_id_sig = $self->conf->{oidcServiceKeyIdSig};
|
|
|
|
if ($public_key_sig) {
|
|
|
|
my $key = $self->key2jwks($public_key_sig);
|
|
|
|
$key->{kty} = "RSA";
|
|
|
|
$key->{use} = "sig";
|
|
|
|
$key->{kid} = $key_id_sig if $key_id_sig;
|
|
|
|
push @{ $jwks->{keys} }, $key;
|
|
|
|
}
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug("Send JWKS response sent");
|
2016-12-31 08:57:24 +01:00
|
|
|
return $self->p->sendJSONresponse( $req, $jwks );
|
2016-12-30 09:01:55 +01:00
|
|
|
}
|
|
|
|
|
2016-12-31 08:57:24 +01:00
|
|
|
# Handle register endpoint
|
2016-12-30 09:01:55 +01:00
|
|
|
sub registration {
|
|
|
|
my ( $self, $req ) = @_;
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug("URL detected as an OpenID Connect REGISTRATION URL");
|
2016-12-31 08:57:24 +01:00
|
|
|
|
|
|
|
# TODO: check Initial Access Token
|
|
|
|
|
|
|
|
# Specific message to allow DOS detection
|
2017-01-04 17:36:54 +01:00
|
|
|
my $source_ip = $req->address;
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->notice(
|
|
|
|
"OpenID Connect Registration request from $source_ip");
|
2016-12-31 08:57:24 +01:00
|
|
|
|
|
|
|
# Check dynamic registration is allowed
|
|
|
|
unless ( $self->conf->{oidcServiceAllowDynamicRegistration} ) {
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->error("Dynamic registration is not allowed");
|
2016-12-31 08:57:24 +01:00
|
|
|
$self->p->sendError( $req, 'server_error' );
|
|
|
|
}
|
|
|
|
|
|
|
|
# Get client metadata
|
2017-01-07 08:45:36 +01:00
|
|
|
my $client_metadata_json = $req->content;
|
2016-12-31 08:57:24 +01:00
|
|
|
unless ($client_metadata_json) {
|
|
|
|
return $self->p->sendError( $req, 'Missing POST datas', 400 );
|
|
|
|
}
|
|
|
|
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug("Client metadata received: $client_metadata_json");
|
2016-12-31 08:57:24 +01:00
|
|
|
|
|
|
|
my $client_metadata = $self->decodeJSON($client_metadata_json);
|
|
|
|
my $registration_response = {};
|
|
|
|
|
|
|
|
# Check redirect_uris
|
|
|
|
unless ( $client_metadata->{redirect_uris} ) {
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->error("Field redirect_uris is mandatory");
|
2016-12-31 08:57:24 +01:00
|
|
|
return $self->p->sendError( $req, 'invalid_client_metadata', 400 );
|
|
|
|
}
|
|
|
|
|
|
|
|
# RP identifier
|
|
|
|
my $registration_time = time;
|
|
|
|
my $rp = "register-$registration_time";
|
|
|
|
|
|
|
|
# Generate Client ID and Client Password
|
|
|
|
my $client_id = random_string("ssssssssssssssssssssssssssssss");
|
|
|
|
my $client_secret = random_string("ssssssssssssssssssssssssssssss");
|
|
|
|
|
|
|
|
# Register known parameters
|
|
|
|
my $client_name =
|
|
|
|
$client_metadata->{client_name} || "Self registered client";
|
|
|
|
my $logo_uri = $client_metadata->{logo_uri};
|
|
|
|
my $id_token_signed_response_alg =
|
|
|
|
$client_metadata->{id_token_signed_response_alg} || "RS256";
|
|
|
|
my $userinfo_signed_response_alg =
|
|
|
|
$client_metadata->{userinfo_signed_response_alg};
|
|
|
|
my $redirect_uris = $client_metadata->{redirect_uris};
|
|
|
|
|
|
|
|
# Register RP in global configuration
|
2017-01-11 22:10:13 +01:00
|
|
|
my $conf = $self->confAcc->getConf( { raw => 1 } );
|
2016-12-31 08:57:24 +01:00
|
|
|
|
|
|
|
$conf->{cfgAuthor} = "OpenID Connect Registration ($client_name)";
|
|
|
|
$conf->{cfgAuthorIP} = $source_ip;
|
2017-03-07 18:49:46 +01:00
|
|
|
$conf->{cfgVersion} = $VERSION;
|
2016-12-31 08:57:24 +01:00
|
|
|
|
|
|
|
$conf->{oidcRPMetaDataExportedVars}->{$rp} = {};
|
|
|
|
$conf->{oidcRPMetaDataOptions}->{$rp}->{oidcRPMetaDataOptionsClientID} =
|
|
|
|
$client_id;
|
|
|
|
$conf->{oidcRPMetaDataOptions}->{$rp}->{oidcRPMetaDataOptionsClientSecret}
|
|
|
|
= $client_secret;
|
|
|
|
$conf->{oidcRPMetaDataOptions}->{$rp}->{oidcRPMetaDataOptionsDisplayName} =
|
|
|
|
$client_name;
|
|
|
|
$conf->{oidcRPMetaDataOptions}->{$rp}->{oidcRPMetaDataOptionsIcon} =
|
|
|
|
$logo_uri;
|
|
|
|
$conf->{oidcRPMetaDataOptions}->{$rp}->{oidcRPMetaDataOptionsIDTokenSignAlg}
|
|
|
|
= $id_token_signed_response_alg;
|
|
|
|
$conf->{oidcRPMetaDataOptions}->{$rp}->{oidcRPMetaDataOptionsRedirectUris}
|
|
|
|
= join( ' ', @$redirect_uris );
|
|
|
|
$conf->{oidcRPMetaDataOptions}->{$rp}
|
|
|
|
->{oidcRPMetaDataOptionsUserInfoSignAlg} = $userinfo_signed_response_alg
|
|
|
|
if defined $userinfo_signed_response_alg;
|
|
|
|
|
|
|
|
if ( $self->confAcc->saveConf($conf) ) {
|
|
|
|
|
|
|
|
# Reload RP list
|
|
|
|
$self->loadRPs();
|
|
|
|
|
|
|
|
# Send registration response
|
|
|
|
$registration_response->{'client_id'} = $client_id;
|
|
|
|
$registration_response->{'client_secret'} = $client_secret;
|
|
|
|
$registration_response->{'client_id_issued_at'} = $registration_time;
|
|
|
|
$registration_response->{'client_id_expires_at'} = 0;
|
|
|
|
$registration_response->{'client_name'} = $client_name;
|
|
|
|
$registration_response->{'logo_uri'} = $logo_uri;
|
|
|
|
$registration_response->{'id_token_signed_response_alg'} =
|
|
|
|
$id_token_signed_response_alg;
|
|
|
|
$registration_response->{'redirect_uris'} = $redirect_uris;
|
|
|
|
$registration_response->{'userinfo_signed_response_alg'} =
|
|
|
|
$userinfo_signed_response_alg
|
|
|
|
if defined $userinfo_signed_response_alg;
|
|
|
|
}
|
|
|
|
else {
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->error(
|
|
|
|
"Configuration not saved: $Lemonldap::NG::Common::Conf::msg");
|
2016-12-31 08:57:24 +01:00
|
|
|
return $self->p->sendError( $req, 'server_error', 500 );
|
|
|
|
}
|
|
|
|
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug("Registration response sent");
|
2016-12-31 08:57:24 +01:00
|
|
|
return $self->p->sendJSONresponse( $req, $registration_response,
|
|
|
|
code => 201 );
|
2016-12-30 09:01:55 +01:00
|
|
|
}
|
|
|
|
|
2016-12-31 08:57:24 +01:00
|
|
|
# Handle logout endpoint for unauthenticated users
|
2016-12-30 09:01:55 +01:00
|
|
|
sub endSessionDone {
|
|
|
|
my ( $self, $req ) = @_;
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug("URL detected as an OpenID Connect END SESSION URL");
|
|
|
|
$self->logger->debug("User is already logged out");
|
2016-12-31 08:57:24 +01:00
|
|
|
|
2017-01-01 10:43:48 +01:00
|
|
|
my $post_logout_redirect_uri = $req->param('post_logout_redirect_uri');
|
|
|
|
my $state = $req->param('state');
|
2016-12-31 08:57:24 +01:00
|
|
|
|
|
|
|
if ($post_logout_redirect_uri) {
|
|
|
|
|
|
|
|
# Build Response
|
|
|
|
my $response_url =
|
|
|
|
$self->buildLogoutResponse( $post_logout_redirect_uri, $state );
|
|
|
|
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug("Redirect user to $response_url");
|
2016-12-31 08:57:24 +01:00
|
|
|
return [ 302, [ Location => $response_url ], [] ];
|
|
|
|
}
|
|
|
|
|
|
|
|
# Else, normal login process
|
|
|
|
return $self->p->login($req);
|
2016-12-30 09:01:55 +01:00
|
|
|
}
|
|
|
|
|
2016-12-31 08:57:24 +01:00
|
|
|
# Handle checksession endpoint
|
2016-12-30 09:01:55 +01:00
|
|
|
sub checkSession {
|
|
|
|
my ( $self, $req ) = @_;
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug(
|
|
|
|
"URL detected as an OpenID Connect CHECK SESSION URL");
|
2016-12-31 08:57:24 +01:00
|
|
|
|
|
|
|
# TODO: access_control_allow_origin => '*'
|
2017-01-20 07:19:54 +01:00
|
|
|
$req->frame(1);
|
2017-01-02 21:20:42 +01:00
|
|
|
return $self->p->sendHtml(
|
|
|
|
$req,
|
|
|
|
'../common/oidc_checksession',
|
|
|
|
params => {
|
2017-01-11 22:10:13 +01:00
|
|
|
COOKIENAME => $self->conf->{cookieName},
|
2017-01-02 21:20:42 +01:00
|
|
|
}
|
2016-12-31 08:57:24 +01:00
|
|
|
);
|
2016-12-30 09:01:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub badAuthRequest {
|
|
|
|
my ( $self, $req ) = @_;
|
|
|
|
return $self->p->sendError( $req,
|
|
|
|
$req->uri . ' may not be called by an authenticated user', 400 );
|
|
|
|
}
|
|
|
|
|
2016-12-31 08:57:24 +01:00
|
|
|
# Nothing to do here
|
2016-12-30 09:01:55 +01:00
|
|
|
sub logout {
|
|
|
|
my ( $self, $req ) = @_;
|
2017-03-09 22:56:51 +01:00
|
|
|
if ( my $s = $req->userData->{_oidcConnectedRP} ) {
|
|
|
|
my @rps = grep /\w/, split( ',', $s );
|
|
|
|
foreach my $rp (@rps) {
|
|
|
|
my $rpConf = $self->conf->{oidcRPMetaDataOptions}->{$rp};
|
2017-03-10 12:39:19 +01:00
|
|
|
unless ($rpConf) {
|
|
|
|
$self->logger->error("Unknown RP $rp");
|
|
|
|
return PE_ERROR;
|
|
|
|
}
|
2017-03-09 22:56:51 +01:00
|
|
|
if ( my $url = $rpConf->{oidcRPMetaDataOptionsLogoutUrl} ) {
|
|
|
|
if ( $rpConf->{oidcRPMetaDataOptionsLogoutType} eq 'front' ) {
|
2017-03-10 12:39:19 +01:00
|
|
|
if ( $rpConf->{oidcRPMetaDataOptionsLogoutSessionRequired} )
|
|
|
|
{
|
|
|
|
my $user_id_attribute =
|
|
|
|
$self->conf->{oidcRPMetaDataOptions}->{$rp}
|
|
|
|
->{oidcRPMetaDataOptionsUserIDAttr}
|
|
|
|
|| $self->conf->{whatToTrace};
|
|
|
|
my $user_id = $req->{sessionInfo}->{$user_id_attribute};
|
2017-03-11 08:16:27 +01:00
|
|
|
$url .= ( $url =~ /\?/ ? '&' : '?' )
|
2017-03-10 12:39:19 +01:00
|
|
|
. build_urlencoded(
|
|
|
|
iss => $self->conf->{oidcServiceMetaDataIssuer},
|
|
|
|
sid => $user_id
|
|
|
|
);
|
|
|
|
}
|
2017-03-09 22:56:51 +01:00
|
|
|
$req->info( qq'<iframe src="$url" class="noborder">'
|
|
|
|
. '</iframe>' );
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
# TODO
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-12-31 08:57:24 +01:00
|
|
|
return PE_OK;
|
2016-12-30 09:01:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# Internal methods
|
|
|
|
|
2016-12-31 14:08:39 +01:00
|
|
|
sub metadata {
|
|
|
|
my ( $self, $req ) = @_;
|
|
|
|
my $issuerDBOpenIDConnectPath = $self->conf->{issuerDBOpenIDConnectPath};
|
|
|
|
my $authorize_uri = $self->conf->{oidcServiceMetaDataAuthorizeURI};
|
|
|
|
my $token_uri = $self->conf->{oidcServiceMetaDataTokenURI};
|
|
|
|
my $userinfo_uri = $self->conf->{oidcServiceMetaDataUserInfoURI};
|
|
|
|
my $jwks_uri = $self->conf->{oidcServiceMetaDataJWKSURI};
|
|
|
|
my $registration_uri = $self->conf->{oidcServiceMetaDataRegistrationURI};
|
|
|
|
my $endsession_uri = $self->conf->{oidcServiceMetaDataEndSessionURI};
|
|
|
|
my $checksession_uri = $self->conf->{oidcServiceMetaDataCheckSessionURI};
|
|
|
|
|
|
|
|
my $path = $self->path . '/';
|
|
|
|
my $issuer = $self->conf->{oidcServiceMetaDataIssuer};
|
|
|
|
$path = "/" . $path unless ( $issuer =~ /\/$/ );
|
|
|
|
my $baseUrl = $issuer . $path;
|
|
|
|
|
|
|
|
my @acr = keys %{ $self->conf->{oidcServiceMetaDataAuthnContext} };
|
|
|
|
|
|
|
|
# Add a slash to path value if issuer has no trailing slash
|
|
|
|
|
|
|
|
# Create OpenID configuration hash;
|
|
|
|
return $self->p->sendJSONresponse(
|
|
|
|
$req,
|
|
|
|
{
|
|
|
|
issuer => $issuer,
|
|
|
|
|
|
|
|
# Endpoints
|
|
|
|
token_endpoint => $baseUrl . $token_uri,
|
|
|
|
userinfo_endpoint => $baseUrl . $userinfo_uri,
|
|
|
|
jwks_uri => $baseUrl . $jwks_uri,
|
|
|
|
authorization_endpoint => $baseUrl . $authorize_uri,
|
|
|
|
end_session_endpoint => $baseUrl . $endsession_uri,
|
|
|
|
check_session_iframe => $baseUrl . $checksession_uri,
|
2017-03-09 22:56:51 +01:00
|
|
|
|
|
|
|
# Logout capabilities
|
|
|
|
backchannel_logout_supported => JSON::true,
|
|
|
|
backchannel_logout_session_supported => JSON::true,
|
|
|
|
frontchannel_logout_supported => JSON::true,
|
|
|
|
frontchannel_logout_session_supported => JSON::true,
|
2016-12-31 14:08:39 +01:00
|
|
|
(
|
|
|
|
$self->conf->{oidcServiceAllowDynamicRegistration}
|
|
|
|
? ( registration_endpoint => $baseUrl . $registration_uri )
|
|
|
|
: ()
|
|
|
|
),
|
|
|
|
|
|
|
|
# Scopes
|
|
|
|
scopes_supported => [qw/openid profile email address phone/],
|
|
|
|
response_types_supported => [
|
|
|
|
"code",
|
|
|
|
"id_token",
|
|
|
|
"id_token token",
|
|
|
|
"code id_token",
|
|
|
|
"code token",
|
|
|
|
"code id_token token"
|
|
|
|
],
|
|
|
|
grant_types_supported => [qw/authorization_code implicit hybrid/],
|
|
|
|
acr_values_supported => \@acr,
|
|
|
|
subject_types_supported => ["public"],
|
|
|
|
token_endpoint_auth_methods_supported =>
|
|
|
|
[qw/client_secret_post client_secret_basic/],
|
2017-01-24 17:53:59 +01:00
|
|
|
claims_supported => [qw/sub iss auth_time acr/],
|
2016-12-31 14:08:39 +01:00
|
|
|
request_parameter_supported => JSON::true,
|
|
|
|
request_uri_parameter_supported => JSON::true,
|
|
|
|
require_request_uri_registration => JSON::false,
|
|
|
|
|
|
|
|
# Algorithms
|
|
|
|
id_token_signing_alg_values_supported =>
|
|
|
|
[qw/none HS256 HS384 HS512 RS256 RS384 RS512/],
|
|
|
|
userinfo_signing_alg_values_supported =>
|
|
|
|
[qw/none HS256 HS384 HS512 RS256 RS384 RS512/],
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
# response_modes_supported}
|
|
|
|
|
|
|
|
# id_token_encryption_alg_values_supported
|
|
|
|
# id_token_encryption_enc_values_supported
|
|
|
|
|
|
|
|
# userinfo_encryption_alg_values_supported
|
|
|
|
# userinfo_encryption_enc_values_supported
|
|
|
|
# request_object_signing_alg_values_supported
|
|
|
|
# request_object_encryption_alg_values_supported
|
|
|
|
# request_object_encryption_enc_values_supported
|
|
|
|
|
|
|
|
# token_endpoint_auth_signing_alg_values_supported
|
|
|
|
# display_values_supported
|
|
|
|
# claim_types_supported
|
|
|
|
# RECOMMENDED # claims_supported
|
|
|
|
# service_documentation
|
|
|
|
# claims_locales_supported
|
|
|
|
# ui_locales_supported
|
|
|
|
# claims_parameter_supported
|
|
|
|
|
|
|
|
# op_policy_uri
|
|
|
|
# op_tos_uri
|
|
|
|
}
|
|
|
|
|
2017-02-08 14:51:46 +01:00
|
|
|
# Store request parameters in %ENV
|
|
|
|
sub exportRequestParameters {
|
|
|
|
my ( $self, $req ) = @_;
|
|
|
|
|
|
|
|
foreach my $param (
|
|
|
|
qw/response_type scope client_id state redirect_uri nonce
|
|
|
|
response_mode display prompt max_age ui_locales id_token_hint
|
|
|
|
login_hint acr_values request request_uri/
|
|
|
|
)
|
|
|
|
{
|
|
|
|
if ( $req->param($param) ) {
|
2017-02-09 09:05:55 +01:00
|
|
|
$req->env->{ "llng_oidc_" . $param } = $req->param($param);
|
2017-02-08 14:51:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-15 11:45:57 +01:00
|
|
|
# Extract request_uri/request parameter
|
|
|
|
my $request = $req->param('request');
|
|
|
|
if ( $req->param('request_uri') ) {
|
|
|
|
$request = $self->getRequestJWT( $req->param('request_uri') );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($request) {
|
|
|
|
my $request_data = $self->getJWTJSONData($request);
|
|
|
|
foreach ( keys %$request_data ) {
|
|
|
|
$req->env->{ "llng_oidc_" . $_ } = $request_data->{$_};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $req->param('client_id') ) {
|
|
|
|
my $rp = $self->getRP( $req->param('client_id') );
|
|
|
|
$req->env->{"llng_oidc_rp"} = $rp if $rp;
|
|
|
|
}
|
|
|
|
|
2017-02-08 14:51:46 +01:00
|
|
|
return PE_OK;
|
|
|
|
}
|
|
|
|
|
2016-12-30 09:01:55 +01:00
|
|
|
1;
|