2016-09-27 21:43:38 +02:00
|
|
|
package Lemonldap::NG::Portal::Issuer::SAML;
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
use Mouse;
|
|
|
|
use Lemonldap::NG::Portal::Main::Constants qw(
|
|
|
|
PE_OK
|
|
|
|
PE_SAML_ART_ERROR
|
|
|
|
PE_SAML_DESTINATION_ERROR
|
|
|
|
PE_SAML_SESSION_ERROR
|
|
|
|
PE_SAML_SIGNATURE_ERROR
|
|
|
|
PE_SAML_SLO_ERROR
|
|
|
|
PE_SAML_SSO_ERROR
|
|
|
|
PE_SAML_UNKNOWN_ENTITY
|
|
|
|
);
|
|
|
|
|
|
|
|
our $VERSION = '2.0.0';
|
|
|
|
|
|
|
|
extends 'Lemonldap::NG::Portal::Main::Issuer',
|
|
|
|
'Lemonldap::NG::Portal::Lib::SAML';
|
|
|
|
|
2016-12-19 07:14:46 +01:00
|
|
|
has ssoUrlRe => ( is => 'rw' );
|
2016-11-16 16:27:01 +01:00
|
|
|
|
2017-02-16 09:24:02 +01:00
|
|
|
has ssoUrlArtifact => ( is => 'rw' );
|
|
|
|
|
|
|
|
# INTERFACE
|
|
|
|
|
|
|
|
# Simply store SP in $req->env
|
|
|
|
use constant beforeAuth => 'storeEnv';
|
|
|
|
|
2016-09-27 21:43:38 +02:00
|
|
|
# INITIALIZATION
|
|
|
|
|
|
|
|
sub init {
|
|
|
|
my ($self) = @_;
|
|
|
|
|
2016-12-19 17:15:31 +01:00
|
|
|
# Prepare SSO URL catching
|
2016-09-27 21:43:38 +02:00
|
|
|
my $saml_sso_get_url = $self->getMetaDataURL(
|
|
|
|
"samlIDPSSODescriptorSingleSignOnServiceHTTPRedirect", 1 );
|
|
|
|
my $saml_sso_get_url_ret = $self->getMetaDataURL(
|
|
|
|
"samlIDPSSODescriptorSingleSignOnServiceHTTPRedirect", 2 );
|
|
|
|
my $saml_sso_post_url =
|
|
|
|
$self->getMetaDataURL( "samlIDPSSODescriptorSingleSignOnServiceHTTPPost",
|
|
|
|
1 );
|
|
|
|
my $saml_sso_post_url_ret =
|
|
|
|
$self->getMetaDataURL( "samlIDPSSODescriptorSingleSignOnServiceHTTPPost",
|
|
|
|
2 );
|
|
|
|
my $saml_sso_art_url = $self->getMetaDataURL(
|
|
|
|
"samlIDPSSODescriptorSingleSignOnServiceHTTPArtifact", 1 );
|
|
|
|
my $saml_sso_art_url_ret = $self->getMetaDataURL(
|
|
|
|
"samlIDPSSODescriptorSingleSignOnServiceHTTPArtifact", 2 );
|
2016-11-16 16:27:01 +01:00
|
|
|
$self->ssoUrlRe(
|
2017-01-13 09:17:20 +01:00
|
|
|
qr/^($saml_sso_get_url|$saml_sso_get_url_ret|$saml_sso_post_url|$saml_sso_post_url_ret|$saml_sso_art_url|$saml_sso_art_url_ret)(?:\?.*)?$/i
|
2016-11-16 16:27:01 +01:00
|
|
|
);
|
2017-02-16 09:24:02 +01:00
|
|
|
$self->ssoUrlArtifact(
|
|
|
|
qr/^($saml_sso_art_url|$saml_sso_art_url_ret)(?:\?.*)?$/i);
|
2016-11-16 16:27:01 +01:00
|
|
|
|
2016-12-19 17:15:31 +01:00
|
|
|
# Launch parents initialization subroutines, then launch IdP en SP lists
|
2016-12-21 23:39:12 +01:00
|
|
|
my $res = (
|
2016-11-16 16:27:01 +01:00
|
|
|
$self->Lemonldap::NG::Portal::Main::Issuer::init()
|
|
|
|
|
|
|
|
# Load SAML service
|
|
|
|
and $self->Lemonldap::NG::Portal::Lib::SAML::init()
|
|
|
|
|
|
|
|
# Load SAML service providers
|
|
|
|
and $self->loadSPs()
|
|
|
|
|
|
|
|
# Load SAML identity providers
|
|
|
|
# Required to manage SLO in Proxy mode
|
|
|
|
and $self->loadIDPs()
|
|
|
|
);
|
2016-12-22 09:40:50 +01:00
|
|
|
|
2016-12-25 16:41:23 +01:00
|
|
|
# Single logout routes
|
|
|
|
$self->addUnauthRouteFromMetaDataURL(
|
|
|
|
"samlIDPSSODescriptorSingleLogoutServiceSOAP",
|
|
|
|
1, 'sloServer', ['POST'] );
|
|
|
|
$self->addUnauthRouteFromMetaDataURL(
|
|
|
|
"samlIDPSSODescriptorSingleLogoutServiceSOAP",
|
|
|
|
2, 'sloServer', ['POST'] );
|
|
|
|
$self->addUnauthRouteFromMetaDataURL(
|
|
|
|
"samlIDPSSODescriptorSingleLogoutServiceHTTPRedirect",
|
|
|
|
1, 'sloServer', ['GET'] );
|
|
|
|
$self->addUnauthRouteFromMetaDataURL(
|
|
|
|
"samlIDPSSODescriptorSingleLogoutServiceHTTPRedirect",
|
|
|
|
2, 'sloServer', ['GET'] );
|
|
|
|
$self->addUnauthRouteFromMetaDataURL(
|
|
|
|
"samlIDPSSODescriptorSingleLogoutServiceHTTPPost",
|
|
|
|
1, 'sloServer', ['POST'] );
|
|
|
|
$self->addUnauthRouteFromMetaDataURL(
|
|
|
|
"samlIDPSSODescriptorSingleLogoutServiceHTTPPost",
|
|
|
|
2, 'sloServer', ['POST'] );
|
|
|
|
|
|
|
|
$self->addAuthRouteFromMetaDataURL(
|
|
|
|
"samlIDPSSODescriptorSingleLogoutServiceSOAP",
|
|
|
|
1, 'authSloServer', ['POST'] );
|
|
|
|
$self->addAuthRouteFromMetaDataURL(
|
|
|
|
"samlIDPSSODescriptorSingleLogoutServiceSOAP",
|
|
|
|
2, 'authSloServer', ['POST'] );
|
|
|
|
$self->addAuthRouteFromMetaDataURL(
|
|
|
|
"samlIDPSSODescriptorSingleLogoutServiceHTTPRedirect",
|
|
|
|
1, 'authSloServer', ['GET'] );
|
|
|
|
$self->addAuthRouteFromMetaDataURL(
|
|
|
|
"samlIDPSSODescriptorSingleLogoutServiceHTTPRedirect",
|
|
|
|
2, 'authSloServer', ['GET'] );
|
|
|
|
$self->addAuthRouteFromMetaDataURL(
|
|
|
|
"samlIDPSSODescriptorSingleLogoutServiceHTTPPost",
|
|
|
|
1, 'authSloServer', ['POST'] );
|
|
|
|
$self->addAuthRouteFromMetaDataURL(
|
|
|
|
"samlIDPSSODescriptorSingleLogoutServiceHTTPPost",
|
|
|
|
2, 'authSloServer', ['POST'] );
|
|
|
|
|
2016-12-21 23:39:12 +01:00
|
|
|
# SOAP routes (access without authentication)
|
|
|
|
$self->addRouteFromMetaDataURL(
|
|
|
|
'samlIDPSSODescriptorArtifactResolutionServiceArtifact',
|
|
|
|
3, 'artifactServer', ['POST'] );
|
2016-12-26 15:35:30 +01:00
|
|
|
$self->addRouteFromMetaDataURL(
|
|
|
|
'samlAttributeAuthorityDescriptorAttributeServiceSOAP',
|
|
|
|
1, 'attributeServer', ['POST'] );
|
2016-12-21 23:39:12 +01:00
|
|
|
|
|
|
|
# TODO: @coudot, why this URL isn't managed with a conf param ?
|
2016-12-23 17:03:36 +01:00
|
|
|
$self->addUnauthRoute(
|
|
|
|
$self->path => { relaySingleLogoutSOAP => 'sloRelaySoap' },
|
|
|
|
[ 'GET', 'POST' ]
|
|
|
|
);
|
2016-12-23 11:02:11 +01:00
|
|
|
$self->addAuthRoute(
|
2016-12-25 16:41:23 +01:00
|
|
|
$self->path => { relaySingleLogoutPOST => 'sloRelayPost' },
|
|
|
|
[ 'GET', 'POST' ]
|
|
|
|
);
|
|
|
|
$self->addUnauthRoute(
|
|
|
|
$self->path => { relaySingleLogoutPOST => 'sloRelayPost' },
|
2016-12-21 23:39:12 +01:00
|
|
|
[ 'GET', 'POST' ]
|
|
|
|
);
|
|
|
|
return $res;
|
2016-11-16 16:27:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# RUNNING METHODS
|
|
|
|
|
2017-02-16 09:24:02 +01:00
|
|
|
# "beforeAuth" entry point. Store just SP and SP confKey in $req->env
|
|
|
|
sub storeEnv {
|
|
|
|
my ( $self, $req ) = @_;
|
|
|
|
return PE_OK
|
|
|
|
if ( $req->uri !~ $self->ssoUrlRe or $req->uri =~ $self->ssoUrlArtifact );
|
|
|
|
my ( $request, $response, $method, $relaystate, $artifact ) =
|
|
|
|
$self->checkMessage( $req, $req->uri, $req->method, $req->content_type );
|
|
|
|
return PE_OK if ( $artifact or !$request );
|
|
|
|
my $login = $self->createLogin( $self->lassoServer );
|
|
|
|
if ( my $sp = $login->remote_providerID() ) {
|
|
|
|
$req->env->{llng_saml_sp} = $sp;
|
|
|
|
if ( my $spConfKey = $self->spList->{$sp}->{confKey} ) {
|
|
|
|
$req->env->{llng_saml_spconfkey} = $spConfKey;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return PE_OK;
|
|
|
|
}
|
|
|
|
|
2016-12-19 17:15:31 +01:00
|
|
|
# Main method (launched only for authenticated users, see Main/Issuer)
|
2016-11-16 16:27:01 +01:00
|
|
|
sub run {
|
|
|
|
my ( $self, $req ) = @_;
|
|
|
|
my $login;
|
|
|
|
my $protocolProfile;
|
|
|
|
my $artifact_method;
|
|
|
|
my $authn_context;
|
|
|
|
|
|
|
|
# Session ID
|
|
|
|
my $session_id = $req->{sessionInfo}->{_session_id} || $req->{id};
|
|
|
|
|
|
|
|
# Session creation timestamp
|
|
|
|
my $time = $req->{sessionInfo}->{_utime} || time();
|
2016-09-27 21:43:38 +02:00
|
|
|
|
2016-12-19 21:47:44 +01:00
|
|
|
# Get HTTP request information to know
|
2016-09-27 21:43:38 +02:00
|
|
|
# if we are receving SAML request or response
|
2016-11-28 22:15:57 +01:00
|
|
|
my $url = $req->uri;
|
2016-12-05 21:53:34 +01:00
|
|
|
my $request_method = $req->param('issuerMethod') || $req->method;
|
2017-01-04 17:36:54 +01:00
|
|
|
my $content_type = $req->content_type();
|
2016-11-28 22:15:57 +01:00
|
|
|
my $idp_initiated = $req->param('IDPInitiated');
|
|
|
|
my $idp_initiated_sp = $req->param('sp');
|
|
|
|
my $idp_initiated_spConfKey = $req->param('spConfKey');
|
2016-09-27 21:43:38 +02:00
|
|
|
|
|
|
|
# 1.1. SSO (SSO URL or Proxy Mode)
|
2016-11-16 16:27:01 +01:00
|
|
|
if ( $url =~ $self->ssoUrlRe or $req->datas->{_proxiedRequest} ) {
|
2016-09-27 21:43:38 +02:00
|
|
|
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug("URL $url detected as an SSO request URL");
|
2016-09-27 21:43:38 +02:00
|
|
|
|
|
|
|
# Check message
|
2016-12-27 09:40:12 +01:00
|
|
|
my ( $request, $response, $method, $relaystate, $artifact ) =
|
|
|
|
$self->checkMessage( $req, $url, $request_method, $content_type );
|
2016-09-27 21:43:38 +02:00
|
|
|
|
|
|
|
# Create Login object
|
2016-12-25 16:41:23 +01:00
|
|
|
my $login = $self->createLogin( $self->lassoServer );
|
2016-09-27 21:43:38 +02:00
|
|
|
|
|
|
|
# Ignore signature verification
|
|
|
|
$self->disableSignatureVerification($login);
|
|
|
|
|
|
|
|
# Process the request or use IDP initiated mode
|
|
|
|
if ( $request or $idp_initiated ) {
|
|
|
|
|
|
|
|
# Load Session and Identity if they exist
|
|
|
|
my $session = $req->{sessionInfo}->{_lassoSessionDump};
|
|
|
|
my $identity = $req->{sessionInfo}->{_lassoIdentityDump};
|
|
|
|
|
|
|
|
if ($session) {
|
|
|
|
unless ( $self->setSessionFromDump( $login, $session ) ) {
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->error("Unable to load Lasso Session");
|
2016-09-27 21:43:38 +02:00
|
|
|
return PE_SAML_SSO_ERROR;
|
|
|
|
}
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug("Lasso Session loaded");
|
2016-09-27 21:43:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($identity) {
|
|
|
|
unless ( $self->setIdentityFromDump( $login, $identity ) ) {
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->error("Unable to load Lasso Identity");
|
2016-09-27 21:43:38 +02:00
|
|
|
return PE_SAML_SSO_ERROR;
|
|
|
|
}
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug("Lasso Identity loaded");
|
2016-09-27 21:43:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
my $result;
|
|
|
|
|
|
|
|
# Create fake request if IDP initiated mode
|
|
|
|
if ($idp_initiated) {
|
|
|
|
|
|
|
|
# Need sp or spConfKey parameter
|
|
|
|
unless ( $idp_initiated_sp or $idp_initiated_spConfKey ) {
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->userLogger->warn(
|
|
|
|
"sp or spConfKey parameter needed to make IDP initiated SSO"
|
2016-09-27 21:43:38 +02:00
|
|
|
);
|
|
|
|
return PE_SAML_SSO_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
unless ($idp_initiated_sp) {
|
|
|
|
|
|
|
|
# Get SP from spConfKey
|
|
|
|
foreach ( keys %{ $self->spList } ) {
|
|
|
|
if ( $self->spList->{$_}->{confKey} eq
|
|
|
|
$idp_initiated_spConfKey )
|
|
|
|
{
|
|
|
|
$idp_initiated_sp = $_;
|
|
|
|
last;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
unless ( defined $self->spList->{$idp_initiated_sp} ) {
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->userLogger->error(
|
|
|
|
"SP $idp_initiated_sp not known");
|
2016-09-27 21:43:38 +02:00
|
|
|
return PE_SAML_UNKNOWN_ENTITY;
|
|
|
|
}
|
|
|
|
$idp_initiated_spConfKey =
|
|
|
|
$self->spList->{$idp_initiated_sp}->{confKey};
|
|
|
|
}
|
|
|
|
|
|
|
|
# Check if IDP Initiated SSO is allowed
|
|
|
|
unless ( $self->conf->{samlSPMetaDataOptions}
|
|
|
|
->{$idp_initiated_spConfKey}
|
|
|
|
->{samlSPMetaDataOptionsEnableIDPInitiatedURL} )
|
|
|
|
{
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->userLogger->error(
|
|
|
|
"IDP Initiated SSO not allowed for SP $idp_initiated_spConfKey"
|
2016-09-27 21:43:38 +02:00
|
|
|
);
|
|
|
|
return PE_SAML_SSO_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
$result =
|
|
|
|
$self->initIdpInitiatedAuthnRequest( $login,
|
|
|
|
$idp_initiated_sp );
|
|
|
|
unless ($result) {
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->error(
|
|
|
|
"SSO: Fail to init IDP Initiated authentication request"
|
2016-09-27 21:43:38 +02:00
|
|
|
);
|
|
|
|
return PE_SAML_SSO_ERROR;
|
|
|
|
}
|
2016-10-27 11:15:25 +02:00
|
|
|
|
|
|
|
# Force NameID Format
|
|
|
|
my $nameIDFormatKey =
|
|
|
|
$self->conf->{samlSPMetaDataOptions}
|
|
|
|
->{$idp_initiated_spConfKey}
|
|
|
|
->{samlSPMetaDataOptionsNameIDFormat} || "email";
|
2016-11-14 11:09:28 +01:00
|
|
|
eval {
|
|
|
|
$login->request()->NameIDPolicy()
|
|
|
|
->Format( $self->getNameIDFormat($nameIDFormatKey) );
|
|
|
|
};
|
2016-10-27 11:15:25 +02:00
|
|
|
|
|
|
|
# Force AllowCreate to TRUE
|
2016-11-14 11:09:28 +01:00
|
|
|
eval { $login->request()->NameIDPolicy()->AllowCreate(1); };
|
2016-09-27 21:43:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
# Process authentication request
|
|
|
|
if ($artifact) {
|
|
|
|
$result = $self->processArtResponseMsg( $login, $request );
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$result = $self->processAuthnRequestMsg( $login, $request );
|
|
|
|
}
|
|
|
|
|
|
|
|
unless ($result) {
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->error(
|
|
|
|
"SSO: Fail to process authentication request");
|
2016-09-27 21:43:38 +02:00
|
|
|
return PE_SAML_SSO_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
# Get SP entityID
|
|
|
|
my $sp = $request ? $login->remote_providerID() : $idp_initiated_sp;
|
|
|
|
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug("Found entityID $sp in SAML message");
|
2017-02-16 09:24:02 +01:00
|
|
|
$req->env->{llng_saml_sp} = $sp;
|
2016-09-27 21:43:38 +02:00
|
|
|
|
|
|
|
# SP conf key
|
|
|
|
my $spConfKey = $self->spList->{$sp}->{confKey};
|
|
|
|
|
|
|
|
unless ($spConfKey) {
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->userLogger->error(
|
|
|
|
"$sp do not match any SP in configuration");
|
2016-09-27 21:43:38 +02:00
|
|
|
return PE_SAML_UNKNOWN_ENTITY;
|
|
|
|
}
|
|
|
|
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug("$sp match $spConfKey SP in configuration");
|
2017-02-16 09:24:02 +01:00
|
|
|
$req->env->{llng_saml_spconfkey} = $spConfKey;
|
2016-09-27 21:43:38 +02:00
|
|
|
|
|
|
|
# Do we check signature?
|
|
|
|
my $checkSSOMessageSignature =
|
|
|
|
$self->conf->{samlSPMetaDataOptions}->{$spConfKey}
|
|
|
|
->{samlSPMetaDataOptionsCheckSSOMessageSignature};
|
|
|
|
|
|
|
|
if ($checkSSOMessageSignature) {
|
|
|
|
|
|
|
|
$self->forceSignatureVerification($login);
|
|
|
|
|
|
|
|
if ($artifact) {
|
|
|
|
$result = $self->processArtResponseMsg( $login, $request );
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$result = $self->processAuthnRequestMsg( $login, $request );
|
|
|
|
}
|
|
|
|
|
|
|
|
unless ($result) {
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->error("Signature is not valid");
|
2016-09-27 21:43:38 +02:00
|
|
|
return PE_SAML_SIGNATURE_ERROR;
|
|
|
|
}
|
|
|
|
else {
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug("Signature is valid");
|
2016-09-27 21:43:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug("Message signature will not be checked");
|
2016-09-27 21:43:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
# Validate request
|
|
|
|
unless ( $self->validateRequestMsg( $login, 1, 1 ) ) {
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->error("Unable to validate SSO request message");
|
2016-09-27 21:43:38 +02:00
|
|
|
return PE_SAML_SSO_ERROR;
|
|
|
|
}
|
|
|
|
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug("SSO: authentication request is valid");
|
2016-09-27 21:43:38 +02:00
|
|
|
|
|
|
|
# Get ForceAuthn flag
|
|
|
|
my $force_authn;
|
|
|
|
|
|
|
|
eval { $force_authn = $login->request()->ForceAuthn(); };
|
|
|
|
if ($@) {
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->warn(
|
|
|
|
"Unable to get ForceAuthn flag, set it to false");
|
2016-09-27 21:43:38 +02:00
|
|
|
$force_authn = 0;
|
|
|
|
}
|
|
|
|
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug(
|
|
|
|
"Found ForceAuthn flag with value $force_authn");
|
2016-09-27 21:43:38 +02:00
|
|
|
|
|
|
|
# Get ForceAuthn sessions for this session_id
|
|
|
|
my $moduleOptions = $self->conf->{samlStorageOptions} || {};
|
|
|
|
$moduleOptions->{backend} = $self->conf->{samlStorage};
|
|
|
|
my $module = "Lemonldap::NG::Common::Apache::Session";
|
|
|
|
|
|
|
|
my $forceAuthn_sessions =
|
|
|
|
$module->searchOn( $moduleOptions, "_saml_id", $session_id );
|
|
|
|
|
|
|
|
my $forceAuthn_session;
|
|
|
|
my $forceAuthnSessionInfo;
|
|
|
|
|
|
|
|
if (
|
|
|
|
my @forceAuthn_sessions_keys =
|
|
|
|
keys %$forceAuthn_sessions
|
|
|
|
)
|
|
|
|
{
|
|
|
|
|
|
|
|
# Warning if more than one session found
|
|
|
|
if ( $#forceAuthn_sessions_keys > 0 ) {
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->warn(
|
|
|
|
"More than one ForceAuthn session found for session $session_id"
|
2016-09-27 21:43:38 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
# Take the first session
|
|
|
|
$forceAuthn_session = shift @forceAuthn_sessions_keys;
|
|
|
|
|
|
|
|
# Get session
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug(
|
|
|
|
"Retrieve ForceAuthn session $forceAuthn_session for session $session_id"
|
2016-09-27 21:43:38 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
$forceAuthnSessionInfo =
|
|
|
|
$self->getSamlSession($forceAuthn_session);
|
|
|
|
|
|
|
|
# Check forceAuthn flag for current SP
|
|
|
|
if ( $forceAuthnSessionInfo->data->{$spConfKey} ) {
|
|
|
|
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug(
|
|
|
|
"User was already forced to reauthenticate for SP $spConfKey"
|
2016-09-27 21:43:38 +02:00
|
|
|
);
|
|
|
|
$force_authn = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug(
|
|
|
|
"No ForceAuthn session found for session $session_id");
|
2016-09-27 21:43:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
# Force authentication if flag is on, or previous flag still active
|
|
|
|
if ($force_authn) {
|
2017-02-20 22:00:05 +01:00
|
|
|
my $info = { $spConfKey => 1 };
|
2016-09-27 21:43:38 +02:00
|
|
|
|
|
|
|
unless ($forceAuthn_session) {
|
|
|
|
my $forceInfos;
|
2017-02-20 22:00:05 +01:00
|
|
|
$info->{'_type'} = "forceAuthn";
|
|
|
|
$info->{'_saml_id'} = $session_id;
|
|
|
|
$info->{'_utime'} = $time;
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug(
|
|
|
|
"Create ForceAuthn session $forceAuthn_session");
|
2016-09-27 21:43:38 +02:00
|
|
|
}
|
|
|
|
|
2017-02-20 22:00:05 +01:00
|
|
|
# Store flag for further requests
|
|
|
|
$forceAuthnSessionInfo =
|
|
|
|
$self->getSamlSession( $forceAuthn_session, $info );
|
|
|
|
$forceAuthn_session = $forceAuthnSessionInfo->id
|
|
|
|
unless ($forceAuthn_session);
|
|
|
|
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug(
|
|
|
|
"Set ForceAuthn flag for SP $spConfKey in ForceAuthn session $forceAuthn_session"
|
2016-09-27 21:43:38 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
# Replay authentication process
|
|
|
|
$req->{updateSession} = 1;
|
2017-02-15 20:03:59 +01:00
|
|
|
$req->steps(
|
|
|
|
[
|
|
|
|
@{ $self->p->beforeAuth },
|
|
|
|
$self->p->authProcess,
|
|
|
|
@{ $self->p->betweenAuthAndDatas },
|
|
|
|
$self->p->sessionDatas,
|
|
|
|
@{ $self->p->afterDatas }
|
|
|
|
]
|
|
|
|
);
|
|
|
|
$req->error( $self->p->process($req) );
|
2016-09-27 21:43:38 +02:00
|
|
|
|
|
|
|
# Return error if any
|
2017-02-15 20:03:59 +01:00
|
|
|
return $req->{error} if $req->{error} > 0;
|
2016-09-27 21:43:38 +02:00
|
|
|
|
|
|
|
# Else remove flag
|
|
|
|
$forceAuthnSessionInfo =
|
2017-02-20 22:00:05 +01:00
|
|
|
$self->getSamlSession( $forceAuthn_session,
|
|
|
|
{ $spConfKey => 0 } );
|
2016-09-27 21:43:38 +02:00
|
|
|
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug(
|
|
|
|
"Unset ForceAuthn flag for SP $spConfKey in ForceAuthn session $forceAuthn_session"
|
2016-09-27 21:43:38 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
# Check Destination (only in non proxy mode)
|
|
|
|
unless ( $req->datas->{_proxiedRequest} ) {
|
|
|
|
return PE_SAML_DESTINATION_ERROR
|
|
|
|
unless ( $self->checkDestination( $login->request, $url ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
# Map authenticationLevel with SAML2 authentication context
|
|
|
|
my $authenticationLevel =
|
|
|
|
$req->{sessionInfo}->{authenticationLevel};
|
|
|
|
|
|
|
|
$authn_context =
|
|
|
|
$self->authnLevel2authnContext($authenticationLevel);
|
|
|
|
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug("Authentication context is $authn_context");
|
2016-09-27 21:43:38 +02:00
|
|
|
|
|
|
|
# Get SP options notOnOrAfterTimeout
|
|
|
|
my $notOnOrAfterTimeout =
|
|
|
|
$self->conf->{samlSPMetaDataOptions}->{$spConfKey}
|
|
|
|
->{samlSPMetaDataOptionsNotOnOrAfterTimeout};
|
|
|
|
|
|
|
|
# Build Assertion
|
|
|
|
unless (
|
|
|
|
$self->buildAssertion(
|
2016-11-28 22:15:57 +01:00
|
|
|
$req, $login, $authn_context, $notOnOrAfterTimeout
|
2016-09-27 21:43:38 +02:00
|
|
|
)
|
|
|
|
)
|
|
|
|
{
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->error("Unable to build assertion");
|
2016-09-27 21:43:38 +02:00
|
|
|
return PE_SAML_SSO_ERROR;
|
|
|
|
}
|
|
|
|
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug("SSO: assertion is built");
|
2016-09-27 21:43:38 +02:00
|
|
|
|
|
|
|
# Get default NameID Format from configuration
|
|
|
|
# Set to "email" if no value in configuration
|
|
|
|
my $nameIDFormatKey =
|
|
|
|
$self->conf->{samlSPMetaDataOptions}->{$spConfKey}
|
|
|
|
->{samlSPMetaDataOptionsNameIDFormat} || "email";
|
|
|
|
my $nameIDFormat;
|
|
|
|
|
|
|
|
# Check NameID Policy in request
|
|
|
|
if ( $login->request()->NameIDPolicy ) {
|
|
|
|
$nameIDFormat = $login->request()->NameIDPolicy->Format();
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug(
|
|
|
|
"Get NameID format $nameIDFormat from request");
|
2016-09-27 21:43:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
# NameID unspecified is forced to default NameID format
|
|
|
|
if ( !$nameIDFormat
|
|
|
|
or $nameIDFormat eq $self->getNameIDFormat("unspecified") )
|
|
|
|
{
|
|
|
|
$nameIDFormat = $self->getNameIDFormat($nameIDFormatKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
# Get session key associated with NameIDFormat
|
|
|
|
# Not for unspecified, transient, persistent, entity, encrypted
|
|
|
|
my $nameIDFormatConfiguration = {
|
|
|
|
$self->getNameIDFormat("email") => 'samlNameIDFormatMapEmail',
|
|
|
|
$self->getNameIDFormat("x509") => 'samlNameIDFormatMapX509',
|
|
|
|
$self->getNameIDFormat("windows") =>
|
|
|
|
'samlNameIDFormatMapWindows',
|
|
|
|
$self->getNameIDFormat("kerberos") =>
|
|
|
|
'samlNameIDFormatMapKerberos',
|
|
|
|
};
|
|
|
|
|
|
|
|
my $nameIDSessionKey =
|
|
|
|
$self->conf->{ $nameIDFormatConfiguration->{$nameIDFormat} };
|
|
|
|
|
|
|
|
# Override default NameID Mapping
|
|
|
|
if ( $self->conf->{samlSPMetaDataOptions}->{$spConfKey}
|
|
|
|
->{samlSPMetaDataOptionsNameIDSessionKey} )
|
|
|
|
{
|
|
|
|
$nameIDSessionKey =
|
|
|
|
$self->conf->{samlSPMetaDataOptions}->{$spConfKey}
|
|
|
|
->{samlSPMetaDataOptionsNameIDSessionKey};
|
|
|
|
}
|
|
|
|
|
|
|
|
my $nameIDContent;
|
|
|
|
if ( defined $req->{sessionInfo}->{$nameIDSessionKey} ) {
|
|
|
|
$nameIDContent =
|
2016-11-28 22:15:57 +01:00
|
|
|
$self->p->getFirstValue(
|
2016-09-27 21:43:38 +02:00
|
|
|
$req->{sessionInfo}->{$nameIDSessionKey} );
|
|
|
|
}
|
|
|
|
|
|
|
|
# Manage Entity NameID format
|
|
|
|
if ( $nameIDFormat eq $self->getNameIDFormat("entity") ) {
|
|
|
|
$nameIDContent = $self->getMetaDataURL( "samlEntityID", 0, 1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
# Manage Transient NameID format
|
|
|
|
if ( $nameIDFormat eq $self->getNameIDFormat("transient") ) {
|
|
|
|
eval {
|
|
|
|
my @assert = $login->response->Assertion;
|
|
|
|
$nameIDContent = $assert[0]->Subject->NameID->content;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $login->nameIdentifier ) {
|
|
|
|
$login->nameIdentifier->Format($nameIDFormat);
|
|
|
|
$login->nameIdentifier->content($nameIDContent)
|
|
|
|
if $nameIDContent;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
my $nameIdentifier = Lasso::Saml2NameID->new();
|
|
|
|
$nameIdentifier->Format($nameIDFormat);
|
|
|
|
$nameIdentifier->content($nameIDContent)
|
|
|
|
if $nameIDContent;
|
|
|
|
$login->nameIdentifier($nameIdentifier);
|
|
|
|
}
|
|
|
|
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug(
|
|
|
|
"NameID Format is " . $login->nameIdentifier->Format );
|
|
|
|
$self->logger->debug(
|
|
|
|
"NameID Content is " . $login->nameIdentifier->content );
|
2016-09-27 21:43:38 +02:00
|
|
|
|
|
|
|
# Push mandatory attributes
|
|
|
|
my @attributes;
|
|
|
|
|
|
|
|
foreach (
|
|
|
|
keys %{
|
|
|
|
$self->conf->{samlSPMetaDataExportedAttributes}
|
|
|
|
->{$spConfKey}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
{
|
|
|
|
|
|
|
|
# Extract fields from exportedAttr value
|
|
|
|
my ( $mandatory, $name, $format, $friendly_name ) =
|
|
|
|
split( /;/,
|
|
|
|
$self->conf->{samlSPMetaDataExportedAttributes}
|
|
|
|
->{$spConfKey}->{$_} );
|
|
|
|
|
|
|
|
# Name is required
|
|
|
|
next unless $name;
|
|
|
|
|
|
|
|
# Do not send attribute if not mandatory
|
|
|
|
unless ($mandatory) {
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug(
|
|
|
|
"SAML2 attribute $name is not mandatory");
|
2016-09-27 21:43:38 +02:00
|
|
|
next;
|
|
|
|
}
|
|
|
|
|
|
|
|
# Error if corresponding attribute is not in user session
|
|
|
|
my $value = $req->{sessionInfo}->{$_};
|
|
|
|
unless ( defined $value ) {
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->warn(
|
|
|
|
"Session key $_ is required to set SAML $name attribute"
|
2016-09-27 21:43:38 +02:00
|
|
|
);
|
|
|
|
return PE_SAML_SSO_ERROR;
|
|
|
|
}
|
|
|
|
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug(
|
|
|
|
"SAML2 attribute $name will be set with $_ session key");
|
2016-09-27 21:43:38 +02:00
|
|
|
|
|
|
|
# SAML2 attribute
|
|
|
|
my $attribute =
|
|
|
|
$self->createAttribute( $name, $format, $friendly_name );
|
|
|
|
|
|
|
|
unless ($attribute) {
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->error(
|
|
|
|
"Unable to create a new SAML attribute");
|
2016-09-27 21:43:38 +02:00
|
|
|
return PE_SAML_SSO_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
# Set attribute value(s)
|
|
|
|
my @values = split $self->conf->{multiValuesSeparator}, $value;
|
|
|
|
my @saml2values;
|
|
|
|
|
|
|
|
foreach (@values) {
|
|
|
|
|
|
|
|
# SAML2 attribute value
|
|
|
|
my $saml2value = $self->createAttributeValue( $_,
|
|
|
|
$self->conf->{samlSPMetaDataOptions}->{$spConfKey}
|
|
|
|
->{samlSPMetaDataOptionsForceUTF8} );
|
|
|
|
|
|
|
|
unless ($saml2value) {
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->error(
|
|
|
|
"Unable to create a new SAML attribute value");
|
2016-09-27 21:43:38 +02:00
|
|
|
$self->checkLassoError($@);
|
|
|
|
return PE_SAML_SSO_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
push @saml2values, $saml2value;
|
|
|
|
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug("Push $_ in SAML attribute $name");
|
2016-09-27 21:43:38 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
$attribute->AttributeValue(@saml2values);
|
|
|
|
|
|
|
|
# Push attribute in attribute list
|
|
|
|
push @attributes, $attribute;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
# Get response assertion
|
|
|
|
my @response_assertions = $login->response->Assertion;
|
|
|
|
|
|
|
|
unless ( $response_assertions[0] ) {
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->error("Unable to get response assertion");
|
2016-09-27 21:43:38 +02:00
|
|
|
return PE_SAML_SSO_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
# Set subject NameID
|
|
|
|
$response_assertions[0]
|
|
|
|
->set_subject_name_id( $login->nameIdentifier );
|
|
|
|
|
|
|
|
# Set basic conditions
|
2016-11-29 22:10:00 +01:00
|
|
|
my $oneTimeUse = $self->conf->{samlSPMetaDataOptions}->{$spConfKey}
|
|
|
|
->{samlSPMetaDataOptionsOneTimeUse} // 0;
|
2016-09-27 21:43:38 +02:00
|
|
|
|
|
|
|
my $conditionNotOnOrAfter = $notOnOrAfterTimeout || "86400";
|
|
|
|
eval {
|
|
|
|
$response_assertions[0]
|
|
|
|
->set_basic_conditions( 60, $conditionNotOnOrAfter,
|
|
|
|
$oneTimeUse );
|
|
|
|
};
|
|
|
|
if ($@) {
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug("Basic conditions not set: $@");
|
2016-09-27 21:43:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
# Create attribute statement
|
|
|
|
if ( scalar @attributes ) {
|
|
|
|
|
|
|
|
my $attribute_statement;
|
|
|
|
|
|
|
|
eval {
|
|
|
|
$attribute_statement =
|
|
|
|
Lasso::Saml2AttributeStatement->new();
|
|
|
|
};
|
|
|
|
if ($@) {
|
|
|
|
$self->checkLassoError($@);
|
|
|
|
return PE_SAML_SSO_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
# Register attributes in attribute statement
|
|
|
|
$attribute_statement->Attribute(@attributes);
|
|
|
|
|
|
|
|
# Add attribute statement in response assertion
|
|
|
|
my @attributes_statement = ($attribute_statement);
|
|
|
|
$response_assertions[0]
|
|
|
|
->AttributeStatement(@attributes_statement);
|
|
|
|
}
|
|
|
|
|
|
|
|
# Get AuthnStatement
|
|
|
|
my @authn_statements = $response_assertions[0]->AuthnStatement();
|
|
|
|
|
|
|
|
# Set sessionIndex
|
|
|
|
# sessionIndex is the encrypted session_id
|
|
|
|
my $sessionIndex = $self->conf->{cipher}->encrypt($session_id);
|
|
|
|
$authn_statements[0]->SessionIndex($sessionIndex);
|
|
|
|
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug(
|
|
|
|
"Set sessionIndex $sessionIndex (encrypted from $session_id)");
|
2016-09-27 21:43:38 +02:00
|
|
|
|
|
|
|
# Set SessionNotOnOrAfter
|
|
|
|
my $sessionNotOnOrAfterTimeout =
|
|
|
|
$self->conf->{samlSPMetaDataOptions}->{$spConfKey}
|
|
|
|
->{samlSPMetaDataOptionsSessionNotOnOrAfterTimeout};
|
|
|
|
$sessionNotOnOrAfterTimeout ||= $self->conf->{timeout};
|
|
|
|
my $timeout = $time + $sessionNotOnOrAfterTimeout;
|
|
|
|
my $sessionNotOnOrAfter = $self->timestamp2samldate($timeout);
|
|
|
|
$authn_statements[0]->SessionNotOnOrAfter($sessionNotOnOrAfter);
|
|
|
|
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug(
|
|
|
|
"Set sessionNotOnOrAfter $sessionNotOnOrAfter");
|
2016-09-27 21:43:38 +02:00
|
|
|
|
|
|
|
# Register AuthnStatement in assertion
|
|
|
|
$response_assertions[0]->AuthnStatement(@authn_statements);
|
|
|
|
|
|
|
|
# Set response assertion
|
|
|
|
$login->response->Assertion(@response_assertions);
|
|
|
|
|
|
|
|
# Signature
|
|
|
|
my $signSSOMessage =
|
|
|
|
$self->conf->{samlSPMetaDataOptions}->{$spConfKey}
|
2016-11-29 22:10:00 +01:00
|
|
|
->{samlSPMetaDataOptionsSignSSOMessage} // -1;
|
2016-09-27 21:43:38 +02:00
|
|
|
|
|
|
|
if ( $signSSOMessage == 0 ) {
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug("SSO response will not be signed");
|
2016-09-27 21:43:38 +02:00
|
|
|
$self->disableSignature($login);
|
|
|
|
}
|
|
|
|
elsif ( $signSSOMessage == 1 ) {
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug("SSO response will be signed");
|
2016-09-27 21:43:38 +02:00
|
|
|
$self->forceSignature($login);
|
|
|
|
}
|
|
|
|
else {
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug(
|
|
|
|
"SSO response signature according to metadata");
|
2016-09-27 21:43:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
# log that a SAML authn response is build
|
2016-11-29 22:10:00 +01:00
|
|
|
my $user = $req->{sessionInfo}->{ $self->conf->{whatToTrace} };
|
|
|
|
my $nameIDLog = '';
|
2016-09-27 21:43:38 +02:00
|
|
|
foreach my $format (qw(persistent transient)) {
|
|
|
|
if ( $login->nameIdentifier->Format eq
|
|
|
|
$self->getNameIDFormat($format) )
|
|
|
|
{
|
|
|
|
$nameIDLog =
|
|
|
|
" with $format NameID " . $login->nameIdentifier->content;
|
|
|
|
last;
|
|
|
|
}
|
|
|
|
}
|
2017-02-15 15:16:59 +01:00
|
|
|
$self->userLogger->notice(
|
2016-09-27 21:43:38 +02:00
|
|
|
"SAML authentication response sent to SAML SP $spConfKey for $user$nameIDLog"
|
|
|
|
);
|
|
|
|
|
|
|
|
# Build SAML response
|
|
|
|
$protocolProfile = $login->protocolProfile();
|
|
|
|
|
|
|
|
# Artifact
|
2016-12-27 09:40:12 +01:00
|
|
|
# Choose method
|
2017-03-08 23:48:57 +01:00
|
|
|
no strict 'subs';
|
2016-12-27 12:17:25 +01:00
|
|
|
if ( $artifact
|
|
|
|
or $protocolProfile ==
|
|
|
|
Lasso::Constants::LOGIN_PROTOCOL_PROFILE_BRWS_ART )
|
|
|
|
{
|
|
|
|
$artifact = 1;
|
2016-12-27 09:40:12 +01:00
|
|
|
if ( $method == $self->getHttpMethod("post")
|
|
|
|
|| $method == $self->getHttpMethod("artifact-post") )
|
|
|
|
{
|
|
|
|
$artifact_method = $self->getHttpMethod("artifact-post")
|
|
|
|
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$artifact_method = $self->getHttpMethod("artifact-get");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-27 21:43:38 +02:00
|
|
|
no strict 'subs';
|
|
|
|
if ( $protocolProfile ==
|
|
|
|
Lasso::Constants::LOGIN_PROTOCOL_PROFILE_BRWS_ART )
|
|
|
|
{
|
|
|
|
|
|
|
|
# Build artifact message
|
|
|
|
unless ( $self->buildArtifactMsg( $login, $artifact_method ) ) {
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->error(
|
|
|
|
"Unable to build SSO artifact response message");
|
2016-09-27 21:43:38 +02:00
|
|
|
return PE_SAML_ART_ERROR;
|
|
|
|
}
|
|
|
|
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug("SSO: artifact response is built");
|
2016-09-27 21:43:38 +02:00
|
|
|
|
|
|
|
# Get artifact ID and Content, and store them
|
|
|
|
my $artifact_id = $login->get_artifact;
|
|
|
|
my $artifact_message = $login->get_artifact_message;
|
|
|
|
|
|
|
|
$self->storeArtifact( $artifact_id, $artifact_message,
|
|
|
|
$session_id );
|
|
|
|
}
|
|
|
|
|
|
|
|
# No artifact
|
|
|
|
else {
|
|
|
|
|
|
|
|
unless ( $self->buildAuthnResponseMsg($login) ) {
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->error(
|
|
|
|
"Unable to build SSO response message");
|
2016-09-27 21:43:38 +02:00
|
|
|
return PE_SAML_SSO_ERROR;
|
|
|
|
}
|
|
|
|
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug("SSO: authentication response is built");
|
2016-09-27 21:43:38 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
# Save Identity and Session
|
|
|
|
if ( $login->is_identity_dirty ) {
|
|
|
|
|
|
|
|
# Update session
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug("Save Lasso identity in session");
|
2016-09-27 21:43:38 +02:00
|
|
|
$self->updatePersistentSession(
|
|
|
|
{ _lassoIdentityDump => $login->get_identity->dump },
|
|
|
|
undef, $session_id );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $login->is_session_dirty ) {
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug("Save Lasso session in session");
|
2016-12-15 06:55:09 +01:00
|
|
|
$self->p->updateSession( $req,
|
2016-09-27 21:43:38 +02:00
|
|
|
{ _lassoSessionDump => $login->get_session->dump },
|
|
|
|
$session_id );
|
|
|
|
}
|
|
|
|
|
|
|
|
# Keep SAML elements for later queries
|
|
|
|
my $nameid = $login->nameIdentifier;
|
|
|
|
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug( "Store NameID "
|
2016-09-27 21:43:38 +02:00
|
|
|
. $nameid->dump
|
2017-02-15 07:41:50 +01:00
|
|
|
. " and SessionIndex $sessionIndex for session $session_id" );
|
2016-09-27 21:43:38 +02:00
|
|
|
|
|
|
|
my $infos;
|
|
|
|
|
|
|
|
$infos->{type} = 'saml'; # Session type
|
|
|
|
$infos->{_utime} = $time; # Creation time
|
|
|
|
$infos->{_saml_id} = $session_id; # SSO session id
|
|
|
|
$infos->{_nameID} = $nameid->dump; # SAML NameID
|
|
|
|
$infos->{_sessionIndex} = $sessionIndex; # SAML SessionIndex
|
|
|
|
|
2017-02-20 22:00:05 +01:00
|
|
|
my $samlSessionInfo = $self->getSamlSession( undef, $infos );
|
|
|
|
|
|
|
|
return PE_SAML_SESSION_ERROR unless $samlSessionInfo;
|
2016-09-27 21:43:38 +02:00
|
|
|
|
|
|
|
my $saml_session_id = $samlSessionInfo->id;
|
|
|
|
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug(
|
|
|
|
"Link session $session_id to SAML session $saml_session_id");
|
2016-09-27 21:43:38 +02:00
|
|
|
|
|
|
|
# Send SSO Response
|
|
|
|
|
|
|
|
# Register IDP in Common Domain Cookie if needed
|
|
|
|
if ( $self->conf->{samlCommonDomainCookieActivation}
|
|
|
|
and $self->conf->{samlCommonDomainCookieWriter} )
|
|
|
|
{
|
|
|
|
my $cdc_idp = $self->getMetaDataURL( "samlEntityID", 0, 1 );
|
|
|
|
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug(
|
|
|
|
"Will register IDP $cdc_idp in Common Domain Cookie");
|
2016-09-27 21:43:38 +02:00
|
|
|
|
|
|
|
# Redirection to CDC Writer page in a hidden iframe
|
|
|
|
my $cdc_writer_url =
|
|
|
|
$self->conf->{samlCommonDomainCookieWriter};
|
|
|
|
$cdc_writer_url .= (
|
|
|
|
$self->conf->{samlCommonDomainCookieWriter} =~ /\?/
|
|
|
|
? '&idp=' . $cdc_idp
|
|
|
|
: '?url=' . $cdc_idp
|
|
|
|
);
|
|
|
|
|
2017-01-24 06:10:57 +01:00
|
|
|
my $cdc_iframe =
|
|
|
|
qq'<iframe src="$cdc_writer_url"'
|
2017-01-19 14:20:02 +01:00
|
|
|
. ' alt="Common Dommain Cookie" marginwidth="0"'
|
|
|
|
. ' marginheight="0" scrolling="no" class="hiddenFrame"'
|
|
|
|
. ' width="0" height="0" frameborder="0"></iframe>';
|
2016-09-27 21:43:38 +02:00
|
|
|
|
2017-02-07 18:57:19 +01:00
|
|
|
$req->info(
|
2016-12-22 09:40:50 +01:00
|
|
|
'<h3 trspan="updateCdc">Update Common Domain Cookie</h3>'
|
|
|
|
. $cdc_iframe );
|
2016-09-27 21:43:38 +02:00
|
|
|
}
|
|
|
|
|
2016-12-27 09:40:12 +01:00
|
|
|
# HTTP-POST
|
2016-12-02 17:29:04 +01:00
|
|
|
if (
|
2016-12-27 09:40:12 +01:00
|
|
|
(
|
|
|
|
!$artifact
|
|
|
|
and $protocolProfile eq
|
|
|
|
Lasso::Constants::LOGIN_PROTOCOL_PROFILE_BRWS_POST
|
|
|
|
)
|
|
|
|
or ( $artifact
|
2016-12-02 17:29:04 +01:00
|
|
|
and $artifact_method ==
|
2016-12-27 09:40:12 +01:00
|
|
|
$self->getHttpMethod("artifact-post") )
|
2016-12-02 17:29:04 +01:00
|
|
|
)
|
2016-09-27 21:43:38 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
# Use autosubmit form
|
|
|
|
my $sso_url = $login->msg_url;
|
|
|
|
my $sso_body = $login->msg_body;
|
|
|
|
|
|
|
|
$req->postUrl($sso_url);
|
|
|
|
|
2016-12-02 17:29:04 +01:00
|
|
|
if ( $artifact_method
|
|
|
|
and $artifact_method ==
|
|
|
|
$self->getHttpMethod("artifact-post") )
|
2016-09-27 21:43:38 +02:00
|
|
|
{
|
|
|
|
$req->{postFields} = { 'SAMLart' => $sso_body };
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$req->{postFields} = { 'SAMLResponse' => $sso_body };
|
|
|
|
}
|
|
|
|
|
|
|
|
# RelayState
|
|
|
|
$req->{postFields}->{'RelayState'} = $relaystate
|
|
|
|
if ($relaystate);
|
|
|
|
|
2016-11-28 22:15:57 +01:00
|
|
|
$req->steps( ['autoPost'] );
|
|
|
|
return PE_OK;
|
2016-09-27 21:43:38 +02:00
|
|
|
}
|
|
|
|
|
2016-12-27 09:40:12 +01:00
|
|
|
# HTTP-REDIRECT
|
|
|
|
if ( $protocolProfile eq
|
|
|
|
Lasso::Constants::LOGIN_PROTOCOL_PROFILE_REDIRECT or $artifact )
|
|
|
|
{
|
|
|
|
|
|
|
|
# Redirect user to response URL
|
|
|
|
my $sso_url = $login->msg_url;
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug("Redirect user to $sso_url");
|
2016-12-27 09:40:12 +01:00
|
|
|
|
|
|
|
$req->{urldc} = $sso_url;
|
|
|
|
$req->mustRedirect(1);
|
|
|
|
$req->steps( [] );
|
|
|
|
|
|
|
|
return PE_OK;
|
|
|
|
}
|
|
|
|
|
2016-09-27 21:43:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
elsif ($response) {
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug(
|
|
|
|
"Authentication responses are not managed by this module");
|
2016-09-27 21:43:38 +02:00
|
|
|
return PE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
else {
|
|
|
|
|
|
|
|
# No request or response
|
|
|
|
# This should not happen
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug("No request or response found");
|
2016-09-27 21:43:38 +02:00
|
|
|
return PE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
return PE_OK;
|
|
|
|
}
|
|
|
|
|
2016-12-07 23:30:00 +01:00
|
|
|
sub artifactServer {
|
|
|
|
my ( $self, $req ) = @_;
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug(
|
|
|
|
"URL $req->uri detected as an artifact resolution service URL");
|
2016-12-07 23:30:00 +01:00
|
|
|
|
|
|
|
# Artifact request are sent with SOAP trough POST
|
2017-01-07 08:45:36 +01:00
|
|
|
my $art_request = $req->content;
|
2016-12-07 23:30:00 +01:00
|
|
|
my $art_response;
|
|
|
|
|
|
|
|
# Create Login object
|
|
|
|
my $login = $self->createLogin( $self->lassoServer );
|
|
|
|
|
|
|
|
# Process request message
|
|
|
|
unless ( $self->processArtRequestMsg( $login, $art_request ) ) {
|
|
|
|
return $self->p->sendError( $req,
|
|
|
|
'Unable to process artifact request message', 400 );
|
|
|
|
}
|
|
|
|
|
|
|
|
# Check Destination
|
|
|
|
unless ( $self->checkDestination( $login->request, $req->uri ) ) {
|
|
|
|
return $self->p->sendError( $req, 'Bad request', 400 );
|
|
|
|
}
|
|
|
|
|
|
|
|
# Create artifact response
|
2016-12-15 06:55:09 +01:00
|
|
|
unless ( $art_response = $self->createArtifactResponse( $req, $login ) ) {
|
2016-12-07 23:30:00 +01:00
|
|
|
return $self->p->sendError( $req,
|
|
|
|
"Unable to create artifact response message", 400 );
|
|
|
|
}
|
|
|
|
|
|
|
|
$self->{SOAPMessage} = $art_response;
|
|
|
|
|
|
|
|
# Return SOAP message
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug("Send SOAP Message: $art_response");
|
2016-12-07 23:30:00 +01:00
|
|
|
return [
|
|
|
|
200,
|
|
|
|
[
|
|
|
|
'Content-Type' => 'application/xml',
|
|
|
|
'Content-Length' => length($art_response)
|
|
|
|
],
|
|
|
|
[$art_response]
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2016-12-13 20:21:36 +01:00
|
|
|
sub soapSloServer {
|
|
|
|
my ( $self, $req ) = @_;
|
|
|
|
my $url = $req->uri;
|
|
|
|
my $request_method = $req->param('issuerMethod') || $req->method;
|
2017-01-04 17:36:54 +01:00
|
|
|
my $content_type = $req->content_type();
|
2016-12-13 20:21:36 +01:00
|
|
|
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug("URL $url detected as an SLO URL");
|
2016-12-13 20:21:36 +01:00
|
|
|
|
|
|
|
# Check SAML Message
|
|
|
|
my ( $request, $response, $method, $relaystate, $artifact ) =
|
|
|
|
$self->checkMessage( $req, $url, $request_method, $content_type,
|
|
|
|
"logout" );
|
|
|
|
|
|
|
|
# Create Logout object
|
|
|
|
my $logout = $self->createLogout( $self->lassoServer );
|
|
|
|
|
|
|
|
# Ignore signature verification
|
|
|
|
$self->disableSignatureVerification($logout);
|
|
|
|
|
|
|
|
if ($request) {
|
|
|
|
|
|
|
|
# Process logout request
|
|
|
|
unless ( $self->processLogoutRequestMsg( $logout, $request ) ) {
|
|
|
|
return $self->p->sendError( $req,
|
|
|
|
"SLO: Fail to process logout request", 400 );
|
|
|
|
}
|
|
|
|
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug("SLO: Logout request is valid");
|
2016-12-13 20:21:36 +01:00
|
|
|
|
|
|
|
# We accept only SOAP here
|
|
|
|
unless ( $method eq $self->getHttpMethod('soap') ) {
|
|
|
|
return $self->p->sendError( $req,
|
|
|
|
"Only SOAP requests allowed here", 400 );
|
|
|
|
}
|
|
|
|
|
|
|
|
# Get SP entityID
|
|
|
|
my $sp = $logout->remote_providerID();
|
|
|
|
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug("Found entityID $sp in SAML message");
|
2016-12-13 20:21:36 +01:00
|
|
|
|
|
|
|
# SP conf key
|
|
|
|
my $spConfKey = $self->spList->{$sp}->{confKey};
|
|
|
|
|
|
|
|
unless ($spConfKey) {
|
2016-12-25 16:41:23 +01:00
|
|
|
return $self->p->sendError( $req,
|
2016-12-13 20:21:36 +01:00
|
|
|
"$sp do not match any SP in configuration", 400 );
|
|
|
|
}
|
|
|
|
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug("$sp match $spConfKey SP in configuration");
|
2016-12-13 20:21:36 +01:00
|
|
|
|
|
|
|
# Do we check signature?
|
|
|
|
my $checkSLOMessageSignature =
|
|
|
|
$self->conf->{samlSPMetaDataOptions}->{$spConfKey}
|
|
|
|
->{samlSPMetaDataOptionsCheckSLOMessageSignature};
|
|
|
|
|
|
|
|
if ($checkSLOMessageSignature) {
|
|
|
|
|
|
|
|
$self->forceSignatureVerification($logout);
|
|
|
|
|
|
|
|
unless ( $self->processLogoutRequestMsg( $logout, $request ) ) {
|
|
|
|
return $self->p->sendError( $req, "Signature is not valid",
|
|
|
|
400 );
|
|
|
|
}
|
|
|
|
else {
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug("Signature is valid");
|
2016-12-13 20:21:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug("Message signature will not be checked");
|
2016-12-13 20:21:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# Get SAML request
|
|
|
|
my $saml_request = $logout->request();
|
|
|
|
unless ($saml_request) {
|
|
|
|
return $self->p->sendError( $req, "No SAML request found", 400 );
|
|
|
|
}
|
|
|
|
|
|
|
|
# Check Destination
|
|
|
|
return $self->sendSLOSoapErrorResponse( $req, $logout, $method )
|
|
|
|
unless ( $self->checkDestination( $saml_request, $url ) );
|
|
|
|
|
|
|
|
# Get session index
|
|
|
|
my $session_index;
|
|
|
|
eval { $session_index = $logout->request()->SessionIndex; };
|
|
|
|
|
|
|
|
# SLO requests without session index are not accepted
|
|
|
|
unless ( defined $session_index ) {
|
|
|
|
$self->p->sendError( $req,
|
|
|
|
"No session index in SLO request from $spConfKey SP", 400 );
|
|
|
|
}
|
|
|
|
|
|
|
|
# Decrypt session index
|
|
|
|
my $local_session_id = $self->conf->{cipher}->decrypt($session_index);
|
|
|
|
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug(
|
|
|
|
"Get session id $local_session_id (decrypted from $session_index)");
|
2016-12-13 20:21:36 +01:00
|
|
|
|
|
|
|
# Open local session
|
2017-01-24 06:10:57 +01:00
|
|
|
my $local_session = $self->p->getApacheSession($local_session_id);
|
2016-12-13 20:21:36 +01:00
|
|
|
|
|
|
|
unless ($local_session) {
|
|
|
|
return $self->p->sendError( $req, "No local session found", 400 );
|
|
|
|
}
|
|
|
|
|
|
|
|
# Load Session and Identity if they exist
|
|
|
|
my $session = $local_session->data->{_lassoSessionDump};
|
|
|
|
my $identity = $local_session->data->{_lassoIdentityDump};
|
|
|
|
|
|
|
|
if ($session) {
|
|
|
|
unless ( $self->setSessionFromDump( $logout, $session ) ) {
|
2016-12-25 16:41:23 +01:00
|
|
|
return $self->p->sendError( $req,
|
|
|
|
"Unable to load Lasso Session", 400 );
|
2016-12-13 20:21:36 +01:00
|
|
|
}
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug("Lasso Session loaded");
|
2016-12-13 20:21:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($identity) {
|
|
|
|
unless ( $self->setIdentityFromDump( $logout, $identity ) ) {
|
2016-12-25 16:41:23 +01:00
|
|
|
return $self->p->sendError( $req,
|
|
|
|
"Unable to load Lasso Identity", 400 );
|
2016-12-13 20:21:36 +01:00
|
|
|
}
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug("Lasso Identity loaded");
|
2016-12-13 20:21:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# Close SAML sessions
|
|
|
|
unless ( $self->deleteSAMLSecondarySessions($local_session_id) ) {
|
2016-12-25 16:41:23 +01:00
|
|
|
return $self->p->sendError( $req, "Fail to delete SAML sessions",
|
2016-12-13 20:21:36 +01:00
|
|
|
400 );
|
|
|
|
}
|
|
|
|
|
|
|
|
# Close local session
|
|
|
|
unless ( $self->p->_deleteSession( $req, $local_session ) ) {
|
2016-12-25 16:41:23 +01:00
|
|
|
return $self->p->sendError( $req,
|
2016-12-13 20:21:36 +01:00
|
|
|
"Fail to delete session $local_session_id", 400 );
|
|
|
|
}
|
|
|
|
|
|
|
|
# Validate request if no previous error
|
|
|
|
unless ( $self->validateLogoutRequest($logout) ) {
|
|
|
|
return $self->p->sendError( $req, "SLO request is not valid", 400 );
|
|
|
|
}
|
|
|
|
|
|
|
|
# Try to send SLO request trough SOAP
|
|
|
|
$self->resetProviderIdIndex($logout);
|
|
|
|
while ( my $providerID = $self->getNextProviderId($logout) ) {
|
|
|
|
|
|
|
|
# Send logout request
|
|
|
|
my ( $rstatus, $rmethod, $rinfo ) =
|
|
|
|
$self->sendLogoutRequestToProvider( $logout, $providerID,
|
|
|
|
$self->getHttpMethod('soap'), 0 );
|
|
|
|
|
|
|
|
if ($rstatus) {
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug("SOAP SLO successful on $providerID");
|
2016-12-13 20:21:36 +01:00
|
|
|
}
|
|
|
|
else {
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug("SOAP SLO error on $providerID");
|
2016-12-13 20:21:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Set RelayState
|
|
|
|
if ($relaystate) {
|
|
|
|
$logout->msg_relayState($relaystate);
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug("Set $relaystate in RelayState");
|
2016-12-13 20:21:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# Signature
|
2017-02-15 07:41:50 +01:00
|
|
|
my $signSLOMessage = $self->{samlSPMetaDataOptions}->{$spConfKey}
|
2016-12-15 21:34:56 +01:00
|
|
|
->{samlSPMetaDataOptionsSignSLOMessage} // 0;
|
2016-12-13 20:21:36 +01:00
|
|
|
|
|
|
|
if ( $signSLOMessage == 0 ) {
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug("SLO response will not be signed");
|
2016-12-13 20:21:36 +01:00
|
|
|
$self->disableSignature($logout);
|
|
|
|
}
|
|
|
|
elsif ( $signSLOMessage == 1 ) {
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug("SLO response will be signed");
|
2016-12-13 20:21:36 +01:00
|
|
|
$self->forceSignature($logout);
|
|
|
|
}
|
|
|
|
else {
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug(
|
|
|
|
"SLO response signature according to metadata");
|
2016-12-13 20:21:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# Send logout response
|
2016-12-15 21:34:56 +01:00
|
|
|
unless ( $self->buildLogoutResponseMsg($logout) ) {
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->error("Unable to build SLO response");
|
2016-12-15 21:34:56 +01:00
|
|
|
return $self->p->sendError( $req, 'Unable to build SLO response',
|
|
|
|
400 );
|
2016-12-13 20:21:36 +01:00
|
|
|
}
|
2016-12-15 21:34:56 +01:00
|
|
|
my $slo_body = $logout->msg_body;
|
|
|
|
return [
|
|
|
|
200,
|
|
|
|
[
|
|
|
|
'Content-Type' => 'application/xml',
|
|
|
|
'Content-Length' => length($slo_body)
|
|
|
|
],
|
|
|
|
[$slo_body]
|
|
|
|
];
|
2016-12-13 20:21:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-09-27 21:43:38 +02:00
|
|
|
sub logout {
|
2016-12-14 15:13:57 +01:00
|
|
|
my ( $self, $req ) = @_;
|
2016-12-25 16:41:23 +01:00
|
|
|
return PE_OK if ( $req->datas->{samlSLOCalled} );
|
2016-12-14 09:49:30 +01:00
|
|
|
|
|
|
|
# Session ID
|
|
|
|
my $session_id = $req->{sessionInfo}->{_session_id} || $req->{id};
|
|
|
|
|
|
|
|
# Close SAML sessions
|
|
|
|
unless ( $self->deleteSAMLSecondarySessions($session_id) ) {
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->error("Fail to delete SAML sessions");
|
2016-12-14 09:49:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# Create Logout object
|
|
|
|
my $logout = $self->createLogout( $self->lassoServer );
|
|
|
|
|
|
|
|
# Load Session and Identity if they exist
|
|
|
|
my $session = $req->{sessionInfo}->{_lassoSessionDump};
|
|
|
|
my $identity = $req->{sessionInfo}->{_lassoIdentityDump};
|
|
|
|
|
|
|
|
if ($session) {
|
|
|
|
unless ( $self->setSessionFromDump( $logout, $session ) ) {
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->error("Unable to load Lasso Session");
|
2016-12-14 09:49:30 +01:00
|
|
|
return PE_SAML_SLO_ERROR;
|
|
|
|
}
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug("Lasso Session loaded");
|
2016-12-14 09:49:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# No need to initiate logout requests on SP, if no SAML session is
|
|
|
|
# available into the session.
|
|
|
|
else {
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug('No SAML session available into this session');
|
2016-12-14 09:49:30 +01:00
|
|
|
return PE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($identity) {
|
|
|
|
unless ( $self->setIdentityFromDump( $logout, $identity ) ) {
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->error("Unable to load Lasso Identity");
|
2016-12-14 09:49:30 +01:00
|
|
|
return PE_SAML_SLO_ERROR;
|
|
|
|
}
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug("Lasso Identity loaded");
|
2016-12-14 09:49:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# Proceed to logout on all others SP.
|
|
|
|
# Verify that logout response is correctly sent. If we have to wait for
|
|
|
|
# providers during HTTP-REDIRECT process, return PE_INFO to notify to wait
|
|
|
|
# for them.
|
|
|
|
# Redirect on logout page when all is done.
|
2016-12-15 22:22:15 +01:00
|
|
|
if ( $self->sendLogoutRequestToProviders( $req, $logout ) ) {
|
2017-01-04 17:36:54 +01:00
|
|
|
$self->{urldc} = $req->script_name . "?logout=1";
|
2016-12-23 17:03:36 +01:00
|
|
|
return PE_OK;
|
2016-12-14 09:49:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return PE_OK;
|
2016-09-27 21:43:38 +02:00
|
|
|
}
|
|
|
|
|
2016-12-17 08:58:53 +01:00
|
|
|
sub sloRelaySoap {
|
|
|
|
my ( $self, $req ) = @_;
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug(
|
|
|
|
"URL " . $req->uri . " detected as a SOAP relay service URL" );
|
2016-12-17 08:58:53 +01:00
|
|
|
|
|
|
|
# Check if relay parameter is present (mandatory)
|
|
|
|
my $relayID;
|
|
|
|
unless ( $relayID = $req->param('relay') ) {
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->error("No relayID detected");
|
2016-12-17 08:58:53 +01:00
|
|
|
return $self->imgnok($req);
|
|
|
|
}
|
|
|
|
|
|
|
|
# Retrieve the corresponding data from samlStorage
|
|
|
|
my $relayInfos = $self->getSamlSession($relayID);
|
|
|
|
unless ($relayInfos) {
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->error("Could not get relay session $relayID");
|
2016-12-17 08:58:53 +01:00
|
|
|
return $self->imgnok($req);
|
|
|
|
}
|
|
|
|
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug("Found relay session $relayID");
|
2016-12-17 08:58:53 +01:00
|
|
|
|
|
|
|
# Rebuild the logout object
|
|
|
|
my $logout;
|
|
|
|
unless ( $logout = $self->createLogout( $self->lassoServer ) ) {
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->error("Could not rebuild logout object");
|
2016-12-17 08:58:53 +01:00
|
|
|
return $self->imgnok($req);
|
|
|
|
}
|
|
|
|
|
|
|
|
# Load Session and Identity if they exist
|
|
|
|
my $session = $relayInfos->data->{_lassoSessionDump};
|
|
|
|
my $identity = $relayInfos->data->{_lassoIdentityDump};
|
|
|
|
my $providerID = $relayInfos->data->{_providerID};
|
2016-12-19 06:31:51 +01:00
|
|
|
my $relayState = $relayInfos->data->{_relayState} // '';
|
2016-12-17 08:58:53 +01:00
|
|
|
my $spConfKey = $self->spList->{$providerID}->{confKey};
|
|
|
|
|
|
|
|
if ($session) {
|
|
|
|
unless ( $self->setSessionFromDump( $logout, $session ) ) {
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->error("Unable to load Lasso Session");
|
2016-12-17 08:58:53 +01:00
|
|
|
return $self->imgnok($req);
|
|
|
|
}
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug("Lasso Session loaded");
|
2016-12-17 08:58:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($identity) {
|
|
|
|
unless ( $self->setIdentityFromDump( $logout, $identity ) ) {
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->error("Unable to load Lasso Identity");
|
2016-12-17 08:58:53 +01:00
|
|
|
return $self->imgnok($req);
|
|
|
|
}
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug("Lasso Identity loaded");
|
2016-12-17 08:58:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# Send the logout request
|
2017-03-08 23:48:57 +01:00
|
|
|
no strict 'subs';
|
2016-12-17 08:58:53 +01:00
|
|
|
my ( $rstatus, $rmethod, $rinfo ) =
|
|
|
|
$self->sendLogoutRequestToProvider( $req, $logout, $providerID,
|
2016-12-19 06:31:51 +01:00
|
|
|
Lasso::Constants::HTTP_METHOD_SOAP,
|
|
|
|
undef, $relayState );
|
2016-12-17 08:58:53 +01:00
|
|
|
unless ($rstatus) {
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->error(
|
|
|
|
"Fail to process SOAP logout request to $providerID");
|
2016-12-17 08:58:53 +01:00
|
|
|
return $self->imgnok($req);
|
|
|
|
}
|
|
|
|
|
|
|
|
# Store success status for this SLO request
|
2017-02-20 22:00:05 +01:00
|
|
|
my $sloStatusSessionInfos =
|
|
|
|
$self->getSamlSession( $relayState, { $spConfKey => 1 } );
|
2016-12-17 08:58:53 +01:00
|
|
|
|
|
|
|
if ($sloStatusSessionInfos) {
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug(
|
|
|
|
"Store SLO status for $spConfKey in session $relayState");
|
2016-12-17 08:58:53 +01:00
|
|
|
}
|
|
|
|
else {
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->warn(
|
|
|
|
"Unable to store SLO status for $spConfKey in session $relayState");
|
2016-12-17 08:58:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# Delete relay session
|
|
|
|
$relayInfos->remove();
|
|
|
|
|
|
|
|
# SLO response is OK
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug("Display OK status for SLO on $spConfKey");
|
2016-12-17 08:58:53 +01:00
|
|
|
return $self->imgok($req);
|
|
|
|
}
|
|
|
|
|
2016-12-25 16:41:23 +01:00
|
|
|
sub sloRelayPost {
|
|
|
|
my ( $self, $req ) = @_;
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug(
|
|
|
|
"URL " . $req->uri . " detected as a POST relay service URL" );
|
2016-12-25 16:41:23 +01:00
|
|
|
|
|
|
|
# Check if relay parameter is present (mandatory)
|
|
|
|
my $relayID;
|
|
|
|
unless ( $relayID = $req->param('relay') ) {
|
|
|
|
return $self->p->sendError( $req, 'No relayID detected' );
|
|
|
|
}
|
|
|
|
|
|
|
|
# Retrieve the corresponding data from samlStorage
|
|
|
|
my $relayInfos = $self->getSamlSession($relayID);
|
|
|
|
unless ($relayInfos) {
|
|
|
|
return $self->p->sendError( $req,
|
|
|
|
"Could not get relay session $relayID" );
|
|
|
|
}
|
|
|
|
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug("Found relay session $relayID");
|
2016-12-25 16:41:23 +01:00
|
|
|
|
|
|
|
# Get data to build POST form
|
|
|
|
$req->{postUrl} = $relayInfos->data->{url};
|
|
|
|
$req->{postFields}->{'SAMLRequest'} = $relayInfos->data->{body};
|
|
|
|
$req->{postFields}->{'RelayState'} = $relayInfos->data->{relayState};
|
|
|
|
|
|
|
|
# Delete relay session
|
|
|
|
$relayInfos->remove();
|
2017-01-20 07:19:54 +01:00
|
|
|
$req->frame(1);
|
2016-12-25 16:41:23 +01:00
|
|
|
return $self->p->do( $req, ['autoPost'] );
|
|
|
|
}
|
|
|
|
|
|
|
|
sub authSloServer {
|
|
|
|
my ( $self, $req ) = @_;
|
|
|
|
$self->p->importHandlerDatas($req);
|
|
|
|
return $self->sloServer($req);
|
|
|
|
}
|
|
|
|
|
|
|
|
sub sloServer {
|
|
|
|
my ( $self, $req ) = @_;
|
|
|
|
my $url = $req->uri;
|
|
|
|
my $request_method = $req->param('issuerMethod') || $req->method;
|
2017-01-04 17:36:54 +01:00
|
|
|
my $content_type = $req->content_type();
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug("URL $url detected as an SLO URL");
|
2016-12-25 16:41:23 +01:00
|
|
|
|
|
|
|
# Check SAML Message
|
|
|
|
my ( $request, $response, $method, $relaystate, $artifact ) =
|
|
|
|
$self->checkMessage( $req, $url, $request_method, $content_type,
|
|
|
|
"logout" );
|
|
|
|
|
|
|
|
# Create Logout object
|
|
|
|
my $logout = $self->createLogout( $self->lassoServer );
|
|
|
|
|
|
|
|
# Ignore signature verification
|
|
|
|
$self->disableSignatureVerification($logout);
|
|
|
|
|
2017-01-20 07:19:54 +01:00
|
|
|
# Disable Content-Security-Policy header since logout can be embedded in
|
|
|
|
# a frame
|
|
|
|
$req->frame(1);
|
|
|
|
|
2016-12-25 16:41:23 +01:00
|
|
|
if ($request) {
|
|
|
|
|
|
|
|
# Process logout request
|
|
|
|
unless ( $self->processLogoutRequestMsg( $logout, $request ) ) {
|
|
|
|
return $self->p->sendError( $req,
|
|
|
|
"SLO: Fail to process logout request", 400 );
|
|
|
|
}
|
|
|
|
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug("SLO: Logout request is valid");
|
2016-12-25 16:41:23 +01:00
|
|
|
|
2016-12-25 16:41:28 +01:00
|
|
|
# Get SP entityID
|
|
|
|
my $sp = $logout->remote_providerID();
|
2017-02-16 09:24:02 +01:00
|
|
|
$req->env->{llng_saml_sp} = $sp;
|
2016-12-25 16:41:28 +01:00
|
|
|
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug("Found entityID $sp in SAML message");
|
2016-12-25 16:41:28 +01:00
|
|
|
|
|
|
|
# SP conf key
|
|
|
|
my $spConfKey = $self->spList->{$sp}->{confKey};
|
|
|
|
|
|
|
|
unless ($spConfKey) {
|
|
|
|
return $self->p->sendError( $req,
|
|
|
|
"$sp do not match any SP in configuration", 400 );
|
|
|
|
}
|
|
|
|
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug("$sp match $spConfKey SP in configuration");
|
2017-02-16 09:24:02 +01:00
|
|
|
$req->env->{llng_saml_spconfkey} = $spConfKey;
|
2016-12-25 16:41:28 +01:00
|
|
|
|
2016-12-25 16:41:23 +01:00
|
|
|
# Load Session and Identity if they exist
|
2016-12-25 16:41:28 +01:00
|
|
|
my ( $session, $session_index, $identity, $local_session_id );
|
|
|
|
|
|
|
|
eval { $session_index = $logout->request()->SessionIndex; };
|
|
|
|
|
|
|
|
# SLO requests without session index are not accepted
|
|
|
|
unless ( defined $session_index ) {
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->error(
|
|
|
|
"No session index in SLO request from $spConfKey SP");
|
2016-12-25 16:41:28 +01:00
|
|
|
return $self->sendSLOErrorResponse( $logout, $method );
|
|
|
|
}
|
|
|
|
$local_session_id = $self->conf->{cipher}->decrypt($session_index);
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug(
|
|
|
|
"Get session id $local_session_id (decrypted from $session_index)");
|
2016-12-25 16:41:28 +01:00
|
|
|
|
|
|
|
if ( $req->{sessionInfo} ) {
|
|
|
|
$session = $req->{sessionInfo}->{_lassoSessionDump};
|
|
|
|
$identity = $req->{sessionInfo}->{_lassoIdentityDump};
|
|
|
|
}
|
|
|
|
unless ($session) {
|
|
|
|
|
|
|
|
# Open local session
|
2017-01-24 06:10:57 +01:00
|
|
|
my $local_session = $self->p->getApacheSession($local_session_id);
|
2016-12-25 16:41:28 +01:00
|
|
|
|
|
|
|
unless ($local_session) {
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->error("No local session found");
|
2016-12-25 16:41:28 +01:00
|
|
|
return $self->sendSLOErrorResponse( $logout, $method );
|
|
|
|
}
|
|
|
|
|
|
|
|
# Load Session and Identity if they exist
|
|
|
|
$session = $local_session->data->{_lassoSessionDump};
|
|
|
|
$identity = $local_session->data->{_lassoIdentityDump};
|
|
|
|
|
|
|
|
# Import user datas in $req (for other "logout" subs)
|
|
|
|
$req->id( $local_session->data->{_session_id} );
|
|
|
|
$req->sessionInfo( $local_session->data );
|
|
|
|
$req->user( $local_session->data->{ $self->conf->{whatToTrace} } );
|
|
|
|
}
|
2016-12-25 16:41:23 +01:00
|
|
|
|
|
|
|
if ($session) {
|
|
|
|
unless ( $self->setSessionFromDump( $logout, $session ) ) {
|
|
|
|
return $self->p->sendError( $req,
|
|
|
|
"Unable to load Lasso Session", 400 );
|
|
|
|
}
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug("Lasso Session loaded");
|
2016-12-25 16:41:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($identity) {
|
|
|
|
unless ( $self->setIdentityFromDump( $logout, $identity ) ) {
|
|
|
|
return $self->p->sendError( $req,
|
|
|
|
"Unable to load Lasso Identity", 400 );
|
|
|
|
}
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug("Lasso Identity loaded");
|
2016-12-25 16:41:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# Do we check signature?
|
|
|
|
my $checkSLOMessageSignature =
|
|
|
|
$self->conf->{samlSPMetaDataOptions}->{$spConfKey}
|
|
|
|
->{samlSPMetaDataOptionsCheckSLOMessageSignature};
|
|
|
|
|
|
|
|
if ($checkSLOMessageSignature) {
|
|
|
|
|
|
|
|
$self->forceSignatureVerification($logout);
|
|
|
|
|
|
|
|
unless ( $self->processLogoutRequestMsg( $logout, $request ) ) {
|
|
|
|
return $self->p->sendError( $req, "Signature is not valid",
|
|
|
|
400 );
|
|
|
|
}
|
|
|
|
else {
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug("Signature is valid");
|
2016-12-25 16:41:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug("Message signature will not be checked");
|
2016-12-25 16:41:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# Check Destination
|
|
|
|
return $self->sendSLOErrorResponse( $logout, $method )
|
|
|
|
unless ( $self->checkDestination( $logout->request, $url ) );
|
|
|
|
|
|
|
|
# Validate request if no previous error
|
|
|
|
unless ( $self->validateLogoutRequest($logout) ) {
|
|
|
|
return $self->p->sendError( $req, "SLO request is not valid", 400 );
|
|
|
|
}
|
|
|
|
|
|
|
|
# Set RelayState
|
|
|
|
if ($relaystate) {
|
|
|
|
$logout->msg_relayState($relaystate);
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug("Set $relaystate in RelayState");
|
2016-12-25 16:41:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
my $sloInfos;
|
|
|
|
$sloInfos->{type} = 'sloStatus';
|
|
|
|
$sloInfos->{_utime} = time;
|
|
|
|
$sloInfos->{_logout} = $logout->dump;
|
|
|
|
$sloInfos->{_session} =
|
|
|
|
$logout->get_session() ? $logout->get_session()->dump : "";
|
|
|
|
$sloInfos->{_method} = $method;
|
2017-02-20 22:00:05 +01:00
|
|
|
|
|
|
|
# Create SLO status session and get ID
|
|
|
|
my $sloStatusSessionInfo = $self->getSamlSession( undef, $sloInfos );
|
2016-12-25 16:41:23 +01:00
|
|
|
my $relayID = $sloStatusSessionInfo->id;
|
|
|
|
|
|
|
|
# Prepare logout on all others SP
|
|
|
|
my $provider_nb =
|
|
|
|
$self->sendLogoutRequestToProviders( $req, $logout, $relayID );
|
|
|
|
|
|
|
|
# Close SAML sessions
|
|
|
|
unless ( $self->deleteSAMLSecondarySessions($local_session_id) ) {
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->error("Fail to delete SAML sessions");
|
2016-12-25 16:41:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# Close local session
|
|
|
|
# This flag is for logout() to say that SAML logout is already done
|
|
|
|
$req->datas->{samlSLOCalled} = 1;
|
|
|
|
|
|
|
|
# Launch normal logout and ignore errors
|
|
|
|
$self->p->do( $req, [ @{ $self->p->beforeLogout }, 'deleteSession' ] );
|
|
|
|
|
|
|
|
# Signature
|
2017-02-15 07:41:50 +01:00
|
|
|
my $signSLOMessage = $self->conf->{samlSPMetaDataOptions}->{$spConfKey}
|
2016-12-25 16:41:23 +01:00
|
|
|
->{samlSPMetaDataOptionsSignSLOMessage};
|
|
|
|
|
|
|
|
unless ($signSLOMessage) {
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug("Do not sign this SLO response");
|
2016-12-25 16:41:23 +01:00
|
|
|
return $self->sendSLOErrorResponse( $logout, $method )
|
|
|
|
unless ( $self->disableSignature($logout) );
|
|
|
|
}
|
|
|
|
|
|
|
|
# If no waiting SP, return directly SLO response
|
|
|
|
unless ($provider_nb) {
|
2017-02-15 07:41:50 +01:00
|
|
|
return $self->sendLogoutResponseToServiceProvider( $req, $logout,
|
|
|
|
$method );
|
2016-12-25 16:41:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# Else build SLO status relay URL and display info
|
|
|
|
else {
|
|
|
|
$req->{urldc} =
|
|
|
|
$self->conf->{portal} . '/saml/relaySingleLogoutTermination';
|
|
|
|
$self->p->setHiddenFormValue( 'relay', $relayID );
|
|
|
|
return $self->do( $req, [] );
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
elsif ($response) {
|
|
|
|
|
|
|
|
# No SLO response should be here
|
|
|
|
# else it means SSO session was not closed: launching it
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug(
|
|
|
|
"SLO response found on an active SSO session, ignoring it");
|
2016-12-25 16:41:23 +01:00
|
|
|
$req->datas->{samlSLOCalled} = 1;
|
|
|
|
return $self->p->do( $req,
|
|
|
|
[ @{ $self->p->beforeLogout }, 'deleteSession' ] );
|
|
|
|
}
|
|
|
|
|
|
|
|
else {
|
|
|
|
|
|
|
|
# No request or response
|
|
|
|
# This should not happen
|
|
|
|
return $self->p->sendError( $req, "No request or response found", 400 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-26 15:35:30 +01:00
|
|
|
sub attributeServer {
|
|
|
|
my ( $self, $req, ) = @_;
|
|
|
|
my $url = $req->uri;
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug("URL $url detected as an attribute service URL");
|
2016-12-26 15:35:30 +01:00
|
|
|
|
|
|
|
# Attribute request are sent with SOAP trough POST
|
2017-01-07 08:45:36 +01:00
|
|
|
my $att_request = $req->content;
|
2016-12-26 15:35:30 +01:00
|
|
|
my $att_response;
|
|
|
|
|
|
|
|
# Process request
|
|
|
|
my $query =
|
|
|
|
$self->processAttributeRequest( $self->lassoServer, $att_request );
|
|
|
|
unless ($query) {
|
|
|
|
return $self->p->sendError( $req,
|
|
|
|
"Unable to process attribute request", 400 );
|
|
|
|
}
|
|
|
|
|
|
|
|
# Get SP entityID
|
|
|
|
my $sp = $query->remote_providerID();
|
|
|
|
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug("Found entityID $sp in SAML message");
|
2016-12-26 15:35:30 +01:00
|
|
|
|
|
|
|
# SP conf key
|
|
|
|
my $spConfKey = $self->spList->{$sp}->{confKey};
|
|
|
|
|
|
|
|
unless ($spConfKey) {
|
|
|
|
return $self->p->sendError( $req,
|
|
|
|
"$sp do not match any SP in configuration", 400 );
|
|
|
|
}
|
|
|
|
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug("$sp match $spConfKey SP in configuration");
|
2016-12-26 15:35:30 +01:00
|
|
|
|
|
|
|
# Check Destination
|
|
|
|
unless ( $self->checkDestination( $query->request, $url ) ) {
|
|
|
|
return $self->p->sendError( $req, "Bad destination $url", 400 );
|
|
|
|
}
|
|
|
|
|
|
|
|
# Validate request
|
|
|
|
unless ( $self->validateAttributeRequest($query) ) {
|
|
|
|
return $self->p->sendError( $req, "Attribute request not valid", 400 );
|
|
|
|
}
|
|
|
|
|
|
|
|
# Get NameID
|
|
|
|
my $name_id = $query->nameIdentifier();
|
|
|
|
|
|
|
|
unless ($name_id) {
|
|
|
|
$self->p->sendError( $req, "Fail to get NameID from attribute request",
|
|
|
|
400 );
|
|
|
|
}
|
|
|
|
|
|
|
|
my $user = $name_id->content();
|
|
|
|
|
|
|
|
# Get sessionInfo for the given NameID
|
|
|
|
my $sessionInfo;
|
|
|
|
my $moduleOptions = $self->conf->{samlStorageOptions} || {};
|
|
|
|
$moduleOptions->{backend} = $self->conf->{samlStorage};
|
|
|
|
my $module = "Lemonldap::NG::Common::Apache::Session";
|
|
|
|
|
|
|
|
my $saml_sessions =
|
|
|
|
$module->searchOn( $moduleOptions, "_nameID", $name_id->dump );
|
|
|
|
|
|
|
|
if ( my @saml_sessions_keys = keys %$saml_sessions ) {
|
|
|
|
|
|
|
|
# Warning if more than one session found
|
|
|
|
if ( $#saml_sessions_keys > 0 ) {
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->warn(
|
|
|
|
"More than one SAML session found for user $user");
|
2016-12-26 15:35:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# Take the first session
|
|
|
|
my $saml_session = shift @saml_sessions_keys;
|
|
|
|
|
|
|
|
# Get session
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug(
|
|
|
|
"Retrieve SAML session $saml_session for user $user");
|
2016-12-26 15:35:30 +01:00
|
|
|
|
|
|
|
my $samlSessionInfo = $self->getSamlSession($saml_session);
|
|
|
|
|
|
|
|
# Get real session
|
|
|
|
my $real_session = $samlSessionInfo->data->{_saml_id};
|
|
|
|
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug(
|
|
|
|
"Retrieve real session $real_session for user $user");
|
2016-12-26 15:35:30 +01:00
|
|
|
|
2017-01-24 06:10:57 +01:00
|
|
|
$sessionInfo = $self->p->getApacheSession($real_session);
|
2016-12-26 15:35:30 +01:00
|
|
|
|
|
|
|
unless ($sessionInfo) {
|
|
|
|
return $self->p->sendError( $req,
|
|
|
|
"Cannot get session $real_session", 500 );
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return $self->p->sendError( $req,
|
|
|
|
"No SAML session found for user $user", 400 );
|
|
|
|
}
|
|
|
|
|
|
|
|
# Get requested attributes
|
|
|
|
my @requested_attributes;
|
|
|
|
eval { @requested_attributes = $query->request()->Attribute(); };
|
|
|
|
if ($@) {
|
|
|
|
$self->checkLassoError($@);
|
|
|
|
return $self->p->sendError( $req, "Unable to get requested attributes",
|
|
|
|
400 );
|
|
|
|
}
|
|
|
|
|
|
|
|
# Returned attributes
|
|
|
|
my @returned_attributes;
|
|
|
|
|
|
|
|
# Browse SP authorized attributes
|
|
|
|
foreach (
|
|
|
|
keys %{ $self->conf->{samlSPMetaDataExportedAttributes}->{$spConfKey} }
|
|
|
|
)
|
|
|
|
{
|
|
|
|
my $sp_attr = $_;
|
|
|
|
|
|
|
|
# Extract fields from exportedAttr value
|
|
|
|
my ( $mandatory, $name, $format, $friendly_name ) =
|
|
|
|
split( /;/,
|
|
|
|
$self->conf->{samlSPMetaDataExportedAttributes}->{$spConfKey}
|
|
|
|
->{$sp_attr} );
|
|
|
|
|
|
|
|
foreach (@requested_attributes) {
|
|
|
|
my $req_attr = $_;
|
|
|
|
my $rname = $req_attr->Name();
|
|
|
|
my $rformat = $req_attr->NameFormat();
|
|
|
|
my $rfriendly_name = $req_attr->FriendlyName();
|
|
|
|
|
|
|
|
# Skip if name does not match
|
|
|
|
next unless ( $rname =~ /^$name$/ );
|
|
|
|
|
|
|
|
# Check format and friendly name
|
|
|
|
next if ( $rformat and $rformat !~ /^$format$/ );
|
|
|
|
next
|
|
|
|
if ( $rfriendly_name
|
|
|
|
and $rfriendly_name !~ /^$friendly_name$/ );
|
|
|
|
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug(
|
|
|
|
"SP $spConfKey is authorized to access attribute $rname");
|
2016-12-26 15:35:30 +01:00
|
|
|
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug(
|
|
|
|
"Attribute $rname is linked to $sp_attr session key");
|
2016-12-26 15:35:30 +01:00
|
|
|
|
|
|
|
# Check if values are given
|
|
|
|
my $rvalue =
|
|
|
|
$self->getAttributeValue( $rname, $rformat, $rfriendly_name,
|
|
|
|
[$req_attr] );
|
|
|
|
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug(
|
|
|
|
"Some values are explicitely requested: $rvalue")
|
2016-12-26 15:35:30 +01:00
|
|
|
if defined $rvalue;
|
|
|
|
|
|
|
|
# Get session value
|
|
|
|
if ( $sessionInfo->data->{$sp_attr} ) {
|
|
|
|
|
|
|
|
my @values = split $self->conf->{multiValuesSeparator},
|
|
|
|
$sessionInfo->data->{$sp_attr};
|
|
|
|
my @saml2values;
|
|
|
|
|
|
|
|
# SAML2 attribute
|
|
|
|
my $ret_attr =
|
|
|
|
$self->createAttribute( $rname, $rformat, $rfriendly_name );
|
|
|
|
|
|
|
|
unless ($ret_attr) {
|
|
|
|
return $self->p->sendError( $req,
|
|
|
|
"Unable to create a new SAML attribute", 500 );
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (@values) {
|
|
|
|
|
|
|
|
my $local_value = $_;
|
|
|
|
|
|
|
|
# Check if values were set in requested attribute
|
|
|
|
# In this case, only requested values can be returned
|
|
|
|
if (
|
|
|
|
$rvalue
|
|
|
|
and !map( /^$local_value$/,
|
|
|
|
split(
|
|
|
|
$self->conf->{multiValuesSeparator}, $rvalue
|
|
|
|
) )
|
|
|
|
)
|
|
|
|
{
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->warn(
|
|
|
|
"$local_value value is not in requested values, it will not be sent"
|
2016-12-26 15:35:30 +01:00
|
|
|
);
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
|
|
|
|
# SAML2 attribute value
|
|
|
|
my $saml2value = $self->createAttributeValue( $local_value,
|
|
|
|
$self->conf->{samlSPMetaDataOptions}->{$spConfKey}
|
|
|
|
->{samlSPMetaDataOptionsForceUTF8} );
|
|
|
|
|
|
|
|
unless ($saml2value) {
|
|
|
|
return $self->p->sendError( $req,
|
|
|
|
"Unable to create a new SAML attribute value",
|
|
|
|
400 );
|
|
|
|
}
|
|
|
|
|
|
|
|
push @saml2values, $saml2value;
|
|
|
|
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug(
|
|
|
|
"Push $local_value in SAML attribute $name");
|
2016-12-26 15:35:30 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
$ret_attr->AttributeValue(@saml2values);
|
|
|
|
|
|
|
|
# Push attribute in attribute list
|
|
|
|
push @returned_attributes, $ret_attr;
|
|
|
|
|
|
|
|
}
|
|
|
|
else {
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug("No session value for $sp_attr");
|
2016-12-26 15:35:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
# Create attribute statement
|
|
|
|
if ( scalar @returned_attributes ) {
|
|
|
|
my $attribute_statement;
|
|
|
|
|
|
|
|
eval { $attribute_statement = Lasso::Saml2AttributeStatement->new(); };
|
|
|
|
if ($@) {
|
|
|
|
$self->checkLassoError($@);
|
|
|
|
return $self->p->sendError( $req, 'An error occurs, see IdP logs',
|
|
|
|
500 );
|
|
|
|
}
|
|
|
|
|
|
|
|
# Register attributes in attribute statement
|
|
|
|
$attribute_statement->Attribute(@returned_attributes);
|
|
|
|
|
|
|
|
# Create assetion
|
|
|
|
my $assertion;
|
|
|
|
|
|
|
|
eval { $assertion = Lasso::Saml2Assertion->new(); };
|
|
|
|
if ($@) {
|
|
|
|
$self->checkLassoError($@);
|
|
|
|
return $self->p->sendError( $req, 'An error occurs, see IdP logs',
|
|
|
|
500 );
|
|
|
|
}
|
|
|
|
|
|
|
|
# Add attribute statement in response assertion
|
|
|
|
my @attributes_statement = ($attribute_statement);
|
|
|
|
$assertion->AttributeStatement(@attributes_statement);
|
|
|
|
|
|
|
|
# Set response assertion
|
|
|
|
$query->response->Assertion( ($assertion) );
|
|
|
|
}
|
|
|
|
|
|
|
|
# Build response
|
|
|
|
$att_response = $self->buildAttributeResponse($query);
|
|
|
|
|
|
|
|
unless ($att_response) {
|
|
|
|
$self->p->sendError( $req, "Unable to build attribute response", 500 );
|
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
|
|
|
200,
|
|
|
|
[
|
|
|
|
'Content-Type' => 'application/xml',
|
|
|
|
'Content-Length' => length($att_response)
|
|
|
|
],
|
|
|
|
[$att_response]
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2016-09-27 21:43:38 +02:00
|
|
|
# INTERNAL METHODS
|
|
|
|
|
2016-12-17 08:58:53 +01:00
|
|
|
sub imgok {
|
|
|
|
my ( $self, $req, ) = @_;
|
|
|
|
return $self->sendImage( $req, 'ok.png' );
|
|
|
|
}
|
|
|
|
|
|
|
|
sub imgnok {
|
|
|
|
my ( $self, $req, ) = @_;
|
|
|
|
return $self->sendImage( $req, 'warning.png' );
|
|
|
|
}
|
|
|
|
|
|
|
|
sub sendImage {
|
|
|
|
my ( $self, $req,, $img ) = @_;
|
|
|
|
return $self->p->staticFile( $req, "common/$img", 'image/png' );
|
|
|
|
}
|
|
|
|
|
2016-09-27 21:43:38 +02:00
|
|
|
1;
|