« blib/lib/Lemonldap/NG/Portal/Auth/SAML.pm
↑ Summary
blib/lib/Lemonldap/NG/Portal/UserDB/Combination.pm »
| 1 | package Lemonldap::NG::Portal::Issuer::SAML; | ||
| 2 | |||
| 3 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use Mouse; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 1 | ▶ | use URI; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 1 | ▶ | use URI::QueryParam; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 1 | ▶ | use HTML::Entities qw(encode_entities); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 1 | ▶ | use Lemonldap::NG::Portal::Lib::SAML; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 9 | use Lemonldap::NG::Portal::Main::Constants qw( | ||
| 10 | portalConsts | ||
| 11 | PE_OK | ||
| 12 | PE_REDIRECT | ||
| 13 | PE_SESSIONEXPIRED | ||
| 14 | PE_SAML_ART_ERROR | ||
| 15 | PE_SAML_DESTINATION_ERROR | ||
| 16 | PE_SAML_SESSION_ERROR | ||
| 17 | PE_SAML_SIGNATURE_ERROR | ||
| 18 | PE_SLO_ERROR | ||
| 19 | PE_SAML_SSO_ERROR | ||
| 20 | PE_ISSUERMISSINGREQATTR | ||
| 21 | PE_UNKNOWNPARTNER | ||
| 22 | PE_SAML_SERVICE_NOT_ALLOWED | ||
| 23 | PE_UNAUTHORIZEDPARTNER | ||
| 24 | ); | ||
| 25 | |||
| 26 | our $VERSION = '2.22.2'; | ||
| 27 | |||
| 28 | extends 'Lemonldap::NG::Portal::Main::Issuer', | ||
| 29 | 'Lemonldap::NG::Portal::Lib::SAML'; | ||
| 30 | |||
| 31 | with 'Lemonldap::NG::Portal::Lib::Key'; | ||
| 32 | |||
| 33 | has rule => ( is => 'rw' ); | ||
| 34 | has ssoUrlRe => ( is => 'rw' ); | ||
| 35 | has ssoUrlArtifact => ( is => 'rw' ); | ||
| 36 | has ssoGetUrl => ( is => 'rw' ); | ||
| 37 | |||
| 38 | use constant sessionKind => 'ISAML'; | ||
| 39 | use constant lsDump => '_lassoSessionDumpI'; | ||
| 40 | use constant liDump => '_lassoIdentityDumpI'; | ||
| 41 | |||
| 42 | # INTERFACE | ||
| 43 | |||
| 44 | # Simply store SP in $req->env | ||
| 45 | use constant beforeAuth => 'storeEnv'; | ||
| 46 | use constant hook => { updateSessionId => 'updateSAMLSecondarySessions', }; | ||
| 47 | |||
| 48 | # INITIALIZATION | ||
| 49 | |||
| 50 | sub init { | ||
| 51 | my ($self) = @_; | ||
| 52 | |||
| 53 | # Parse activation rule | ||
| 54 | my $hd = $self->p->HANDLER; | ||
| 55 | $self->logger->debug( "SAML rule -> " . $self->conf->{issuerDBSAMLRule} ); | ||
| 56 | my $rule = | ||
| 57 | $hd->buildSub( $hd->substitute( $self->conf->{issuerDBSAMLRule} ) ); | ||
| 58 | unless ($rule) { | ||
| 59 | my $error = $hd->tsv->{jail}->error || '???'; | ||
| 60 | $self->error("Bad SAML activation rule -> $error"); | ||
| 61 | return 0; | ||
| 62 | } | ||
| 63 | $self->{rule} = $rule; | ||
| 64 | |||
| 65 | # Prepare SSO URL catching | ||
| 66 | my $saml_sso_get_url = $self->ssoGetUrl( | ||
| 67 | $self->getMetaDataURL( | ||
| 68 | "samlIDPSSODescriptorSingleSignOnServiceHTTPRedirect", 1 | ||
| 69 | ) | ||
| 70 | ); | ||
| 71 | my $saml_sso_get_url_ret = $self->getMetaDataURL( | ||
| 72 | "samlIDPSSODescriptorSingleSignOnServiceHTTPRedirect", 2 ); | ||
| 73 | my $saml_sso_post_url = | ||
| 74 | $self->getMetaDataURL( "samlIDPSSODescriptorSingleSignOnServiceHTTPPost", | ||
| 75 | 1 ); | ||
| 76 | my $saml_sso_post_url_ret = | ||
| 77 | $self->getMetaDataURL( "samlIDPSSODescriptorSingleSignOnServiceHTTPPost", | ||
| 78 | 2 ); | ||
| 79 | my $saml_sso_art_url = $self->getMetaDataURL( | ||
| 80 | "samlIDPSSODescriptorSingleSignOnServiceHTTPArtifact", 1 ); | ||
| 81 | my $saml_sso_art_url_ret = $self->getMetaDataURL( | ||
| 82 | "samlIDPSSODescriptorSingleSignOnServiceHTTPArtifact", 2 ); | ||
| 83 | $self->ssoUrlRe( | ||
| 84 | 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 | ||
| 85 | ); | ||
| 86 | $self->ssoUrlArtifact( | ||
| 87 | qr/^($saml_sso_art_url|$saml_sso_art_url_ret)(?:\?.*)?$/i); | ||
| 88 | |||
| 89 | # Launch parents initialization subroutines, then launch IdP en SP lists | ||
| 90 | my $res = ( | ||
| 91 | $self->Lemonldap::NG::Portal::Main::Issuer::init() | ||
| 92 | |||
| 93 | # Load SAML service | ||
| 94 | and $self->Lemonldap::NG::Portal::Lib::SAML::init() | ||
| 95 | |||
| 96 | # Load SAML service providers | ||
| 97 | and $self->loadSPs() | ||
| 98 | |||
| 99 | # Load SAML identity providers | ||
| 100 | # Required to manage SLO in Proxy mode | ||
| 101 | and $self->loadIDPs() | ||
| 102 | ); | ||
| 103 | return 0 unless ($res); | ||
| 104 | |||
| 105 | if ( $self->conf->{samlOverrideIDPEntityID} ) { | ||
| 106 | $self->lassoServer->ProviderID( | ||
| 107 | $self->conf->{samlOverrideIDPEntityID} ); | ||
| 108 | } | ||
| 109 | |||
| 110 | # Single logout routes | ||
| 111 | $self->addUnauthRouteFromMetaDataURL( | ||
| 112 | "samlIDPSSODescriptorSingleLogoutServiceSOAP", | ||
| 113 | 1, 'sloServer', ['POST'] ); | ||
| 114 | $self->addUnauthRouteFromMetaDataURL( | ||
| 115 | "samlIDPSSODescriptorSingleLogoutServiceSOAP", | ||
| 116 | 2, 'sloServer', ['POST'] ); | ||
| 117 | $self->addUnauthRouteFromMetaDataURL( | ||
| 118 | "samlIDPSSODescriptorSingleLogoutServiceHTTPRedirect", | ||
| 119 | 1, 'sloServer', ['GET'] ); | ||
| 120 | $self->addUnauthRouteFromMetaDataURL( | ||
| 121 | "samlIDPSSODescriptorSingleLogoutServiceHTTPRedirect", | ||
| 122 | 2, 'sloServer', ['GET'] ); | ||
| 123 | $self->addUnauthRouteFromMetaDataURL( | ||
| 124 | "samlIDPSSODescriptorSingleLogoutServiceHTTPPost", | ||
| 125 | 1, 'sloServer', ['POST'] ); | ||
| 126 | $self->addUnauthRouteFromMetaDataURL( | ||
| 127 | "samlIDPSSODescriptorSingleLogoutServiceHTTPPost", | ||
| 128 | 2, 'sloServer', ['POST'] ); | ||
| 129 | |||
| 130 | $self->addAuthRouteFromMetaDataURL( | ||
| 131 | "samlIDPSSODescriptorSingleLogoutServiceSOAP", | ||
| 132 | 1, 'authSloServer', ['POST'] ); | ||
| 133 | $self->addAuthRouteFromMetaDataURL( | ||
| 134 | "samlIDPSSODescriptorSingleLogoutServiceSOAP", | ||
| 135 | 2, 'authSloServer', ['POST'] ); | ||
| 136 | $self->addAuthRouteFromMetaDataURL( | ||
| 137 | "samlIDPSSODescriptorSingleLogoutServiceHTTPRedirect", | ||
| 138 | 1, 'authSloServer', ['GET'] ); | ||
| 139 | $self->addAuthRouteFromMetaDataURL( | ||
| 140 | "samlIDPSSODescriptorSingleLogoutServiceHTTPRedirect", | ||
| 141 | 2, 'authSloServer', ['GET'] ); | ||
| 142 | $self->addAuthRouteFromMetaDataURL( | ||
| 143 | "samlIDPSSODescriptorSingleLogoutServiceHTTPPost", | ||
| 144 | 1, 'authSloServer', ['POST'] ); | ||
| 145 | $self->addAuthRouteFromMetaDataURL( | ||
| 146 | "samlIDPSSODescriptorSingleLogoutServiceHTTPPost", | ||
| 147 | 2, 'authSloServer', ['POST'] ); | ||
| 148 | |||
| 149 | # SOAP routes (access without authentication) | ||
| 150 | $self->addRouteFromMetaDataURL( | ||
| 151 | 'samlIDPSSODescriptorArtifactResolutionServiceArtifact', | ||
| 152 | 3, 'artifactServer', ['POST'] ); | ||
| 153 | $self->addRouteFromMetaDataURL( | ||
| 154 | 'samlAttributeAuthorityDescriptorAttributeServiceSOAP', | ||
| 155 | 1, 'attributeServer', ['POST'] ); | ||
| 156 | |||
| 157 | $self->addUnauthRoute( | ||
| 158 | $self->path => { relaySingleLogoutSOAP => 'sloRelaySoap' }, | ||
| 159 | [ 'GET', 'POST' ] | ||
| 160 | ); | ||
| 161 | $self->addAuthRoute( | ||
| 162 | $self->path => { relaySingleLogoutPOST => 'sloRelayPost' }, | ||
| 163 | [ 'GET', 'POST' ] | ||
| 164 | ); | ||
| 165 | $self->addUnauthRoute( | ||
| 166 | $self->path => { relaySingleLogoutPOST => 'sloRelayPost' }, | ||
| 167 | [ 'GET', 'POST' ] | ||
| 168 | ); | ||
| 169 | $self->addUnauthRoute( | ||
| 170 | $self->path => { singleLogoutResume => 'sloResume' }, | ||
| 171 | [ 'GET', 'POST' ] | ||
| 172 | ); | ||
| 173 | $self->addUnauthRoute( | ||
| 174 | $self->path => { relaySingleLogoutTermination => 'sloRelayTerm' }, | ||
| 175 | [ 'GET', 'POST' ] | ||
| 176 | ); | ||
| 177 | return $res; | ||
| 178 | } | ||
| 179 | |||
| 180 | # RUNNING METHODS | ||
| 181 | |||
| 182 | # "beforeAuth" entry point. Store just SP and SP confKey in $req->env | ||
| 183 | sub storeEnv { | ||
| 184 | my ( $self, $req ) = @_; | ||
| 185 | return PE_OK | ||
| 186 | if ( $req->uri !~ $self->ssoUrlRe or $req->uri =~ $self->ssoUrlArtifact ); | ||
| 187 | |||
| 188 | # This doesn't always work, especially when coming from a HTTP-Redirect flow and | ||
| 189 | # POST-ing the login form | ||
| 190 | my ( $request, $response, $method, $relaystate, $artifact ) = | ||
| 191 | $self->checkMessage( $req, $req->uri, $req->method, $req->content_type ); | ||
| 192 | return PE_OK if ( $artifact or !$request ); | ||
| 193 | my $login = $self->createLogin( $self->lassoServer ); | ||
| 194 | $self->disableSignatureVerification($login); | ||
| 195 | $self->processAuthnRequestMsgWithError( $login, $request, 'debug' ); | ||
| 196 | if ( my $sp = $login->remote_providerID() ) { | ||
| 197 | $req->env->{llng_saml_sp} = $sp; | ||
| 198 | if ( my $spConfKey = $self->spList->{$sp}->{confKey} ) { | ||
| 199 | $req->env->{llng_saml_spconfkey} = $spConfKey; | ||
| 200 | |||
| 201 | # Store target authentication level in pdata | ||
| 202 | my $targetAuthnLevel = | ||
| 203 | $self->spLevelRules->{$spConfKey}->( $req, {} ); | ||
| 204 | |||
| 205 | $req->pdata->{targetAuthnLevel} = $targetAuthnLevel | ||
| 206 | if $targetAuthnLevel; | ||
| 207 | } | ||
| 208 | if ( $login->request ) { | ||
| 209 | my $acs = $login->request->AssertionConsumerServiceURL; | ||
| 210 | $req->env->{llng_saml_acs} = $acs if $acs; | ||
| 211 | } | ||
| 212 | } | ||
| 213 | return PE_OK; | ||
| 214 | } | ||
| 215 | |||
| 216 | sub ssoMatch { | ||
| 217 | my ( $self, $req ) = @_; | ||
| 218 | my $url = $self->normalize_url( $req->uri, $self->conf->{issuerDBSAMLPath}, | ||
| 219 | $self->ssoGetUrl ); | ||
| 220 | return ( | ||
| 221 | $url =~ $self->ssoUrlRe or $req->data->{_proxiedRequest} | ||
| 222 | ? 1 | ||
| 223 | : 0 | ||
| 224 | ); | ||
| 225 | } | ||
| 226 | |||
| 227 | # Main method (launched only for authenticated users, see Main/Issuer) | ||
| 228 | sub run { | ||
| 229 | my ( $self, $req ) = @_; | ||
| 230 | my $login; | ||
| 231 | my $protocolProfile; | ||
| 232 | my $artifact_method; | ||
| 233 | my $authn_context; | ||
| 234 | |||
| 235 | # Check activation rule | ||
| 236 | unless ( $self->rule->( $req, $req->sessionInfo ) ) { | ||
| 237 | $self->userLogger->error('SAML service not authorized'); | ||
| 238 | return PE_SAML_SERVICE_NOT_ALLOWED; | ||
| 239 | } | ||
| 240 | |||
| 241 | # Session ID | ||
| 242 | my $session_id = $req->{sessionInfo}->{_session_id} || $req->id; | ||
| 243 | |||
| 244 | # Session creation timestamp | ||
| 245 | my $time = $req->{sessionInfo}->{_utime} || time(); | ||
| 246 | |||
| 247 | # Get HTTP request information to know | ||
| 248 | # if we are receving SAML request or response | ||
| 249 | my $url = $req->uri; | ||
| 250 | my $request_method = $req->param('issuerMethod') || $req->method; | ||
| 251 | my $content_type = $req->content_type(); | ||
| 252 | my $idp_initiated = $req->param('IDPInitiated'); | ||
| 253 | my $idp_initiated_sp = $req->param('sp'); | ||
| 254 | my $idp_initiated_spConfKey = $req->param('spConfKey'); | ||
| 255 | my $idp_initiated_spDest = $req->param('spDest'); | ||
| 256 | |||
| 257 | # Normalize URL to be tolerant to SAML Path | ||
| 258 | $url = $self->normalize_url( $url, $self->conf->{issuerDBSAMLPath}, | ||
| 259 | $self->ssoGetUrl ); | ||
| 260 | |||
| 261 | # Get domain GET attribute | ||
| 262 | my $domain = $req->param('domain'); | ||
| 263 | |||
| 264 | if ($domain) { | ||
| 265 | $self->logger->debug("Found domain $domain in SAML GET parameter"); | ||
| 266 | } | ||
| 267 | |||
| 268 | # 1.1. SSO (SSO URL or Proxy Mode) | ||
| 269 | if ( $url =~ $self->ssoUrlRe or $req->data->{_proxiedRequest} ) { | ||
| 270 | |||
| 271 | $self->logger->debug("URL $url detected as an SSO request URL"); | ||
| 272 | |||
| 273 | # Check message | ||
| 274 | my ( $request, $response, $method, $relaystate, $artifact ) = | ||
| 275 | $self->checkMessage( $req, $url, $request_method, $content_type ); | ||
| 276 | |||
| 277 | # Create Login object | ||
| 278 | my $login = $self->createLogin( $self->lassoServer ); | ||
| 279 | |||
| 280 | # Ignore signature verification | ||
| 281 | $self->disableSignatureVerification($login); | ||
| 282 | |||
| 283 | if ($request) { | ||
| 284 | $req->data->{_proxiedSamlRequest} = $login->request(); | ||
| 285 | $req->data->{_proxiedRequest} = $request; | ||
| 286 | $req->data->{_proxiedMethod} = $method; | ||
| 287 | $req->data->{_proxiedRelayState} = $relaystate, | ||
| 288 | $req->data->{_proxiedArtifact} = $artifact; | ||
| 289 | } | ||
| 290 | |||
| 291 | # Process the request or use IDP initiated mode | ||
| 292 | if ( $request or $idp_initiated ) { | ||
| 293 | |||
| 294 | # Load Session and Identity if they exist | ||
| 295 | my $session = $req->{sessionInfo}->{ $self->lsDump }; | ||
| 296 | my $identity = $req->{sessionInfo}->{ $self->liDump }; | ||
| 297 | |||
| 298 | if ($session) { | ||
| 299 | unless ( $self->setSessionFromDump( $login, $session ) ) { | ||
| 300 | return $self->_failAuthnRequest( $req, | ||
| 301 | msg => "Unable to load Lasso Session" ); | ||
| 302 | } | ||
| 303 | $self->logger->debug("Lasso Session loaded"); | ||
| 304 | } | ||
| 305 | |||
| 306 | if ($identity) { | ||
| 307 | unless ( $self->setIdentityFromDump( $login, $identity ) ) { | ||
| 308 | return $self->_failAuthnRequest( $req, | ||
| 309 | msg => "Unable to load Lasso Identity" ); | ||
| 310 | } | ||
| 311 | $self->logger->debug("Lasso Identity loaded"); | ||
| 312 | } | ||
| 313 | |||
| 314 | my $result; | ||
| 315 | |||
| 316 | # Create fake request if IDP initiated mode | ||
| 317 | if ($idp_initiated) { | ||
| 318 | |||
| 319 | # Need sp or spConfKey parameter | ||
| 320 | unless ( $idp_initiated_sp or $idp_initiated_spConfKey ) { | ||
| 321 | return $self->_failAuthnRequest( | ||
| 322 | $req, | ||
| 323 | msg => ( | ||
| 324 | "sp or spConfKey parameter needed" | ||
| 325 | . " to make IDP initiated SSO" | ||
| 326 | ), | ||
| 327 | userLogger => 1, | ||
| 328 | ); | ||
| 329 | } | ||
| 330 | |||
| 331 | unless ($idp_initiated_sp) { | ||
| 332 | |||
| 333 | # Get SP from spConfKey | ||
| 334 | foreach ( keys %{ $self->spList } ) { | ||
| 335 | if ( $self->spList->{$_}->{confKey} eq | ||
| 336 | $idp_initiated_spConfKey ) | ||
| 337 | { | ||
| 338 | $idp_initiated_sp = $_; | ||
| 339 | last; | ||
| 340 | } | ||
| 341 | } | ||
| 342 | } | ||
| 343 | else { | ||
| 344 | $self->lazy_load_entityid($idp_initiated_sp); | ||
| 345 | |||
| 346 | unless ( defined $self->spList->{$idp_initiated_sp} ) { | ||
| 347 | return $self->_failAuthnRequest( | ||
| 348 | $req, | ||
| 349 | msg => "SP $idp_initiated_sp not known", | ||
| 350 | res => PE_UNKNOWNPARTNER, | ||
| 351 | userLogger => 1, | ||
| 352 | ); | ||
| 353 | } | ||
| 354 | $idp_initiated_spConfKey = | ||
| 355 | $self->spList->{$idp_initiated_sp}->{confKey}; | ||
| 356 | } | ||
| 357 | |||
| 358 | # Check if IDP Initiated SSO is allowed | ||
| 359 | unless ( $self->spOptions->{$idp_initiated_sp} | ||
| 360 | ->{samlSPMetaDataOptionsEnableIDPInitiatedURL} ) | ||
| 361 | { | ||
| 362 | return $self->_failAuthnRequest( | ||
| 363 | $req, | ||
| 364 | msg => ( | ||
| 365 | "IDP Initiated SSO not allowed" | ||
| 366 | . " for SP $idp_initiated_spConfKey" | ||
| 367 | ), | ||
| 368 | userLogger => 1, | ||
| 369 | ); | ||
| 370 | } | ||
| 371 | |||
| 372 | $result = | ||
| 373 | $self->initIdpInitiatedAuthnRequest( $login, | ||
| 374 | $idp_initiated_sp ); | ||
| 375 | unless ($result) { | ||
| 376 | return $self->_failAuthnRequest( | ||
| 377 | $req, | ||
| 378 | msg => ( | ||
| 379 | "SSO: Fail to init IDP Initiated" | ||
| 380 | . " authentication request" | ||
| 381 | ) | ||
| 382 | ); | ||
| 383 | } | ||
| 384 | |||
| 385 | # Force NameID Format | ||
| 386 | my $nameIDFormatKey = | ||
| 387 | $self->spOptions->{$idp_initiated_sp} | ||
| 388 | ->{samlSPMetaDataOptionsNameIDFormat} || "email"; | ||
| 389 | eval { | ||
| 390 | |||
| 391 | $login->request()->NameIDPolicy() | ||
| 392 | ->Format( $self->getNameIDFormat($nameIDFormatKey) ); | ||
| 393 | }; | ||
| 394 | |||
| 395 | # Force AllowCreate to TRUE | ||
| 396 | eval { $login->request()->NameIDPolicy()->AllowCreate(1); }; | ||
| 397 | |||
| 398 | # Allow selection the AssertionConsumerServiceURL by the user | ||
| 399 | if ($idp_initiated_spDest) { | ||
| 400 | $login->request->AssertionConsumerServiceURL( | ||
| 401 | $idp_initiated_spDest); | ||
| 402 | } | ||
| 403 | } | ||
| 404 | |||
| 405 | my $lasso_error; | ||
| 406 | |||
| 407 | # Process authentication request | ||
| 408 | if ($artifact) { | ||
| 409 | $result = $self->processArtResponseMsg( $login, $request ); | ||
| 410 | } | ||
| 411 | else { | ||
| 412 | ( $result, $lasso_error ) = | ||
| 413 | $self->processAuthnRequestMsgWithError( $login, $request ); | ||
| 414 | } | ||
| 415 | |||
| 416 | unless ($result) { | ||
| 417 | if ( $lasso_error == | ||
| 418 | Lasso::Constants::SERVER_ERROR_PROVIDER_NOT_FOUND ) | ||
| 419 | { | ||
| 420 | my $sp = eval { $login->remote_providerID() }; | ||
| 421 | return $self->_failAuthnRequest( | ||
| 422 | $req, | ||
| 423 | msg => ( | ||
| 424 | "SSO: Incoming entity ID $sp was not found" | ||
| 425 | . " in registered Service Providers metadata." | ||
| 426 | ), | ||
| 427 | res => PE_UNKNOWNPARTNER, | ||
| 428 | ); | ||
| 429 | } | ||
| 430 | elsif ( $lasso_error == | ||
| 431 | Lasso::Constants::PROFILE_ERROR_INVALID_PROTOCOLPROFILE ) | ||
| 432 | { | ||
| 433 | if ( my $ACS = | ||
| 434 | eval { $login->request->AssertionConsumerServiceURL } ) | ||
| 435 | { | ||
| 436 | $self->logger->error( | ||
| 437 | "Requested AssertionConsumerServiceURL $ACS" | ||
| 438 | . " might be missing from registered metadata" ); | ||
| 439 | } | ||
| 440 | else { | ||
| 441 | $self->logger->error( | ||
| 442 | "Make sure AssertionConsumerServiceURL from the " | ||
| 443 | . " SAML requests is defined in registered metadata" | ||
| 444 | ); | ||
| 445 | } | ||
| 446 | |||
| 447 | return $self->_failAuthnRequest( $req, | ||
| 448 | msg => "SSO: Fail to process authentication request" ); | ||
| 449 | } | ||
| 450 | else { | ||
| 451 | return $self->_failAuthnRequest( $req, | ||
| 452 | msg => "SSO: Fail to process authentication request" ); | ||
| 453 | } | ||
| 454 | } | ||
| 455 | |||
| 456 | # Get SP entityID | ||
| 457 | my $sp = $request ? $login->remote_providerID() : $idp_initiated_sp; | ||
| 458 | $self->logger->debug("Found entityID $sp in SAML message"); | ||
| 459 | |||
| 460 | # SP conf key | ||
| 461 | my $spConfKey = $self->spList->{$sp}->{confKey}; | ||
| 462 | unless ($spConfKey) { | ||
| 463 | return $self->_failAuthnRequest( | ||
| 464 | $req, | ||
| 465 | msg => "$sp do not match any SP in configuration", | ||
| 466 | res => PE_UNKNOWNPARTNER, | ||
| 467 | logInfo => { | ||
| 468 | sp => $spConfKey, | ||
| 469 | entity_id => $sp, | ||
| 470 | }, | ||
| 471 | userLogger => 1, | ||
| 472 | ); | ||
| 473 | } | ||
| 474 | $self->logger->debug("$sp match $spConfKey SP in configuration"); | ||
| 475 | |||
| 476 | # Do we check signature? | ||
| 477 | my $checkSSOMessageSignature = | ||
| 478 | $self->spOptions->{$sp} | ||
| 479 | ->{samlSPMetaDataOptionsCheckSSOMessageSignature}; | ||
| 480 | |||
| 481 | if ($checkSSOMessageSignature) { | ||
| 482 | |||
| 483 | $self->forceSignatureVerification($login); | ||
| 484 | |||
| 485 | my $lasso_error; | ||
| 486 | if ($artifact) { | ||
| 487 | $result = $self->processArtResponseMsg( $login, $request ); | ||
| 488 | } | ||
| 489 | else { | ||
| 490 | ( $result, $lasso_error ) = | ||
| 491 | $self->processAuthnRequestMsgWithError( $login, | ||
| 492 | $request ); | ||
| 493 | } | ||
| 494 | |||
| 495 | unless ($result) { | ||
| 496 | return $self->_failAuthnRequest( | ||
| 497 | $req, | ||
| 498 | msg => ( | ||
| 499 | "Could not verify signature of " | ||
| 500 | . "incoming SSO request from $spConfKey" | ||
| 501 | ), | ||
| 502 | res => PE_SAML_SIGNATURE_ERROR, | ||
| 503 | logInfo => { | ||
| 504 | sp => $spConfKey, | ||
| 505 | entity_id => $sp, | ||
| 506 | }, | ||
| 507 | ); | ||
| 508 | } | ||
| 509 | else { | ||
| 510 | $self->logger->debug("Signature is valid"); | ||
| 511 | } | ||
| 512 | } | ||
| 513 | else { | ||
| 514 | $self->logger->debug("Message signature will not be checked"); | ||
| 515 | } | ||
| 516 | |||
| 517 | # Hook must be run after processAuthnRequestMsg | ||
| 518 | my $h = | ||
| 519 | $self->p->processHook( $req, 'samlGotAuthnRequest', $login ); | ||
| 520 | return $self->_failAuthnRequest( | ||
| 521 | $req, | ||
| 522 | msg => 'samlGotAuthnRequest hook failed', | ||
| 523 | res => $h, | ||
| 524 | logInfo => { | ||
| 525 | sp => $spConfKey, | ||
| 526 | entity_id => $sp, | ||
| 527 | }, | ||
| 528 | ) if ( $h != PE_OK ); | ||
| 529 | |||
| 530 | # Set environment for rule/macro evaluation | ||
| 531 | $req->env->{llng_saml_sp} = $sp; | ||
| 532 | $req->env->{llng_saml_spconfkey} = $spConfKey; | ||
| 533 | if ( $login->request ) { | ||
| 534 | my $acs = $login->request->AssertionConsumerServiceURL; | ||
| 535 | if ($acs) { | ||
| 536 | $req->env->{llng_saml_acs} = $acs; | ||
| 537 | $self->logger->debug( | ||
| 538 | "Using AssertionConsumerServiceURL $acs"); | ||
| 539 | } | ||
| 540 | } | ||
| 541 | |||
| 542 | # Check access rule | ||
| 543 | if ( my $rule = $self->spRules->{$spConfKey} ) { | ||
| 544 | unless ( $rule->( $req, $req->sessionInfo ) ) { | ||
| 545 | return $self->_failAuthnRequest( | ||
| 546 | $req, | ||
| 547 | msg => ( | ||
| 548 | 'User ' | ||
| 549 | . $req->sessionInfo->{ $self->conf->{whatToTrace} | ||
| 550 | } | ||
| 551 | . " is not authorized to access to $spConfKey" | ||
| 552 | ), | ||
| 553 | res => PE_UNAUTHORIZEDPARTNER, | ||
| 554 | userLogger => 1, | ||
| 555 | logInfo => { | ||
| 556 | sp => $spConfKey, | ||
| 557 | entity_id => $sp, | ||
| 558 | }, | ||
| 559 | ); | ||
| 560 | } | ||
| 561 | } | ||
| 562 | |||
| 563 | my $nameIDFormat; | ||
| 564 | |||
| 565 | # Check NameID Policy in request | ||
| 566 | if ( $login->request()->NameIDPolicy ) { | ||
| 567 | $nameIDFormat = $login->request()->NameIDPolicy->Format(); | ||
| 568 | $self->logger->debug( | ||
| 569 | "Get NameID format $nameIDFormat from request") | ||
| 570 | if defined($nameIDFormat); | ||
| 571 | } | ||
| 572 | |||
| 573 | # Get default NameID Format from configuration | ||
| 574 | # Set to "email" if no value in configuration | ||
| 575 | my $nameIDFormatKey = | ||
| 576 | $self->spOptions->{$sp}->{samlSPMetaDataOptionsNameIDFormat} | ||
| 577 | || "email"; | ||
| 578 | |||
| 579 | # NameID unspecified is forced to default NameID format | ||
| 580 | if ( !$nameIDFormat | ||
| 581 | or $nameIDFormat eq $self->getNameIDFormat("unspecified") ) | ||
| 582 | { | ||
| 583 | $nameIDFormat = $self->getNameIDFormat($nameIDFormatKey); | ||
| 584 | |||
| 585 | # Update NameIDFormat in request | ||
| 586 | eval { | ||
| 587 | if ( !$login->request()->NameIDPolicy ) { | ||
| 588 | |||
| 589 | # Create NameIDFormat object if it doesnt exist | ||
| 590 | $login->request() | ||
| 591 | ->NameIDPolicy( Lasso::Samlp2NameIDPolicy->new() ); | ||
| 592 | } | ||
| 593 | $login->request()->NameIDPolicy()->Format($nameIDFormat); | ||
| 594 | }; | ||
| 595 | if ($@) { | ||
| 596 | $self->logger->warn( | ||
| 597 | "Could not update NameIDPolicy in request: $@"); | ||
| 598 | } | ||
| 599 | } | ||
| 600 | |||
| 601 | # Force AllowCreate to TRUE for transient/persistent NameIDPolicy | ||
| 602 | if ( $login->request()->NameIDPolicy ) { | ||
| 603 | if ( $nameIDFormat eq $self->getNameIDFormat("transient") | ||
| 604 | or $nameIDFormat eq $self->getNameIDFormat("persistent") ) | ||
| 605 | { | ||
| 606 | $self->logger->debug( | ||
| 607 | "Force AllowCreate flag in NameIDPolicy"); | ||
| 608 | eval { $login->request()->NameIDPolicy()->AllowCreate(1); }; | ||
| 609 | } | ||
| 610 | } | ||
| 611 | |||
| 612 | # Validate request | ||
| 613 | unless ( $self->validateRequestMsg( $login, 1, 1 ) ) { | ||
| 614 | return $self->_failAuthnRequest( | ||
| 615 | $req, | ||
| 616 | msg => "Unable to validate SSO request message", | ||
| 617 | logInfo => { | ||
| 618 | sp => $spConfKey, | ||
| 619 | entity_id => $sp, | ||
| 620 | }, | ||
| 621 | ); | ||
| 622 | } | ||
| 623 | |||
| 624 | $self->logger->debug("SSO: authentication request is valid"); | ||
| 625 | |||
| 626 | my $spAuthnLevel = | ||
| 627 | $self->spLevelRules->{$spConfKey}->( $req, $req->sessionInfo ); | ||
| 628 | |||
| 629 | # Get ForceAuthn flag | ||
| 630 | my $force_authn; | ||
| 631 | |||
| 632 | eval { $force_authn = $login->request()->ForceAuthn(); }; | ||
| 633 | if ($@) { | ||
| 634 | $self->logger->warn( | ||
| 635 | "Unable to get ForceAuthn flag, set it to false"); | ||
| 636 | $force_authn = 0; | ||
| 637 | } | ||
| 638 | |||
| 639 | $self->logger->debug( | ||
| 640 | "Found ForceAuthn flag with value $force_authn"); | ||
| 641 | |||
| 642 | # Force authentication if flag is on, or previous flag still active | ||
| 643 | if ( | ||
| 644 | $force_authn | ||
| 645 | and ( | ||
| 646 | time - $req->sessionInfo->{_lastAuthnUTime} > | ||
| 647 | $self->conf->{portalForceAuthnInterval} ) | ||
| 648 | ) | ||
| 649 | { | ||
| 650 | |||
| 651 | $self->userLogger->info( | ||
| 652 | "SAML SP $spConfKey ask to refresh session of " | ||
| 653 | . $req->sessionInfo->{ $self->conf->{whatToTrace} } ); | ||
| 654 | |||
| 655 | # Replay authentication process | ||
| 656 | $req->pdata->{targetAuthnLevel} = $spAuthnLevel; | ||
| 657 | return $self->reAuth($req); | ||
| 658 | } | ||
| 659 | |||
| 660 | # Check Destination (only in non proxy mode) | ||
| 661 | unless ( $req->data->{_proxiedRequest} ) { | ||
| 662 | if ( !$self->checkDestination( $login->request, $url ) ) { | ||
| 663 | return $self->_failAuthnRequest( | ||
| 664 | $req, | ||
| 665 | msg => "Invalid destination", | ||
| 666 | res => PE_SAML_DESTINATION_ERROR, | ||
| 667 | logInfo => { | ||
| 668 | sp => $spConfKey, | ||
| 669 | entity_id => $sp, | ||
| 670 | }, | ||
| 671 | ); | ||
| 672 | } | ||
| 673 | } | ||
| 674 | |||
| 675 | # Check if we have sufficient auth level | ||
| 676 | my $authenticationLevel = | ||
| 677 | $req->{sessionInfo}->{authenticationLevel} || 0; | ||
| 678 | if ( $authenticationLevel < $spAuthnLevel ) { | ||
| 679 | $self->logger->debug( | ||
| 680 | "Insufficient authentication level for service $spConfKey" | ||
| 681 | . " (has: $authenticationLevel, want: $spAuthnLevel)" ); | ||
| 682 | |||
| 683 | # Reauth with sp auth level as target | ||
| 684 | $req->pdata->{targetAuthnLevel} = $spAuthnLevel; | ||
| 685 | return $self->upgradeAuth($req); | ||
| 686 | } | ||
| 687 | |||
| 688 | $authn_context = | ||
| 689 | $self->authnLevel2authnContext($authenticationLevel); | ||
| 690 | |||
| 691 | $self->logger->debug("Authentication context is $authn_context"); | ||
| 692 | |||
| 693 | # Get SP options notOnOrAfterTimeout | ||
| 694 | my $notOnOrAfterTimeout = | ||
| 695 | $self->spOptions->{$sp} | ||
| 696 | ->{samlSPMetaDataOptionsNotOnOrAfterTimeout}; | ||
| 697 | |||
| 698 | # Build Assertion | ||
| 699 | unless ( | ||
| 700 | $self->buildAssertion( | ||
| 701 | $req, $login, $authn_context, $notOnOrAfterTimeout | ||
| 702 | ) | ||
| 703 | ) | ||
| 704 | { | ||
| 705 | return $self->_failAuthnRequest( | ||
| 706 | $req, | ||
| 707 | msg => "Unable to build assertion", | ||
| 708 | logInfo => { | ||
| 709 | sp => $spConfKey, | ||
| 710 | entity_id => $sp, | ||
| 711 | }, | ||
| 712 | ); | ||
| 713 | } | ||
| 714 | |||
| 715 | $self->logger->debug("SSO: assertion is built"); | ||
| 716 | |||
| 717 | # Get session key associated with NameIDFormat | ||
| 718 | # Not for unspecified, transient, persistent, entity, encrypted | ||
| 719 | my $nameIDFormatConfiguration = { | ||
| 720 | $self->getNameIDFormat("email") => 'samlNameIDFormatMapEmail', | ||
| 721 | $self->getNameIDFormat("x509") => 'samlNameIDFormatMapX509', | ||
| 722 | $self->getNameIDFormat("windows") => | ||
| 723 | 'samlNameIDFormatMapWindows', | ||
| 724 | $self->getNameIDFormat("kerberos") => | ||
| 725 | 'samlNameIDFormatMapKerberos', | ||
| 726 | }; | ||
| 727 | |||
| 728 | my $nameIDSessionKey = | ||
| 729 | $nameIDFormatConfiguration->{$nameIDFormat} | ||
| 730 | ? $self->conf->{ $nameIDFormatConfiguration->{$nameIDFormat} } | ||
| 731 | : ''; | ||
| 732 | |||
| 733 | # Override default NameID Mapping | ||
| 734 | if ( | ||
| 735 | $self->spOptions->{$sp}->{samlSPMetaDataOptionsNameIDSessionKey} | ||
| 736 | ) | ||
| 737 | { | ||
| 738 | $nameIDSessionKey = | ||
| 739 | $self->spOptions->{$sp} | ||
| 740 | ->{samlSPMetaDataOptionsNameIDSessionKey}; | ||
| 741 | } | ||
| 742 | |||
| 743 | my $nameIDContent; | ||
| 744 | if ( $nameIDSessionKey | ||
| 745 | and $self->spMacros->{$spConfKey}->{$nameIDSessionKey} ) | ||
| 746 | { | ||
| 747 | $nameIDContent = | ||
| 748 | $self->spMacros->{$spConfKey}->{$nameIDSessionKey} | ||
| 749 | ->( $req, $req->{sessionInfo} ); | ||
| 750 | } | ||
| 751 | elsif ( $nameIDSessionKey | ||
| 752 | and ( defined $req->{sessionInfo}->{$nameIDSessionKey} ) ) | ||
| 753 | { | ||
| 754 | $nameIDContent = | ||
| 755 | $self->p->getFirstValue( | ||
| 756 | $req->{sessionInfo}->{$nameIDSessionKey} ); | ||
| 757 | } | ||
| 758 | |||
| 759 | # Manage Entity NameID format | ||
| 760 | if ( $nameIDFormat eq $self->getNameIDFormat("entity") ) { | ||
| 761 | $nameIDContent = $self->getMetaDataURL( "samlEntityID", 0, 1 ); | ||
| 762 | } | ||
| 763 | |||
| 764 | # Manage Transient NameID format | ||
| 765 | if ( $nameIDFormat eq $self->getNameIDFormat("transient") ) { | ||
| 766 | eval { | ||
| 767 | my @assert = $login->response->Assertion; | ||
| 768 | $nameIDContent = $assert[0]->Subject->NameID->content; | ||
| 769 | }; | ||
| 770 | } | ||
| 771 | |||
| 772 | if ( $login->nameIdentifier ) { | ||
| 773 | $login->nameIdentifier->Format($nameIDFormat); | ||
| 774 | $login->nameIdentifier->content($nameIDContent) | ||
| 775 | if $nameIDContent; | ||
| 776 | } | ||
| 777 | else { | ||
| 778 | my $nameIdentifier = Lasso::Saml2NameID->new(); | ||
| 779 | $nameIdentifier->Format($nameIDFormat); | ||
| 780 | $nameIdentifier->content($nameIDContent) | ||
| 781 | if $nameIDContent; | ||
| 782 | $login->nameIdentifier($nameIdentifier); | ||
| 783 | } | ||
| 784 | |||
| 785 | $self->logger->debug( | ||
| 786 | "NameID Format is " . $login->nameIdentifier->Format ); | ||
| 787 | $self->logger->debug( "NameID Content is " | ||
| 788 | . ( $login->nameIdentifier->content || "" ) ); | ||
| 789 | |||
| 790 | # Push attributes | ||
| 791 | my @attributes; | ||
| 792 | my %log_attributes; | ||
| 793 | |||
| 794 | foreach ( keys %{ $self->spAttributes->{$sp} } ) { | ||
| 795 | |||
| 796 | # Extract fields from exportedAttr value | ||
| 797 | my ( $mandatory, $name, $format, $friendly_name ) = | ||
| 798 | split( /;/, $self->spAttributes->{$sp}->{$_} ); | ||
| 799 | |||
| 800 | # Name is required | ||
| 801 | next unless $name; | ||
| 802 | |||
| 803 | # Lookup attribute value in SP macros or session | ||
| 804 | my $value; | ||
| 805 | if ( $self->spMacros->{$spConfKey}->{$_} ) { | ||
| 806 | $value = $self->spMacros->{$spConfKey}->{$_} | ||
| 807 | ->( $req, $req->{sessionInfo} ); | ||
| 808 | } | ||
| 809 | else { | ||
| 810 | $value = $req->{sessionInfo}->{$_}; | ||
| 811 | } | ||
| 812 | |||
| 813 | # Check whether the value is required or not | ||
| 814 | unless ( defined $value ) { | ||
| 815 | if ($mandatory) { | ||
| 816 | return $self->_failAuthnRequest( | ||
| 817 | $req, | ||
| 818 | msg => ( | ||
| 819 | "Session key $_ is required to set" | ||
| 820 | . " SAML $name attribute ($sp)" | ||
| 821 | ), | ||
| 822 | res => PE_ISSUERMISSINGREQATTR, | ||
| 823 | logInfo => { | ||
| 824 | sp => $spConfKey, | ||
| 825 | entity_id => $sp, | ||
| 826 | }, | ||
| 827 | ); | ||
| 828 | } | ||
| 829 | else { | ||
| 830 | $self->logger->debug( | ||
| 831 | "SAML2 attribute $name has no value" | ||
| 832 | . " but is not mandatory ($sp), skip it" ); | ||
| 833 | next; | ||
| 834 | } | ||
| 835 | } | ||
| 836 | |||
| 837 | $self->logger->debug( "SAML2 attribute $name will be set" | ||
| 838 | . " with $_ session key ($sp)" ); | ||
| 839 | |||
| 840 | # SAML2 attribute | ||
| 841 | my $attribute = | ||
| 842 | $self->createAttribute( $name, $format, $friendly_name ); | ||
| 843 | |||
| 844 | unless ($attribute) { | ||
| 845 | return $self->_failAuthnRequest( | ||
| 846 | $req, | ||
| 847 | msg => "Unable to create a new SAML attribute", | ||
| 848 | logInfo => { | ||
| 849 | sp => $spConfKey, | ||
| 850 | entity_id => $sp, | ||
| 851 | }, | ||
| 852 | ); | ||
| 853 | } | ||
| 854 | |||
| 855 | # Set attribute value(s) | ||
| 856 | my @values = split $self->conf->{multiValuesSeparator}, $value; | ||
| 857 | my @saml2values; | ||
| 858 | |||
| 859 | foreach (@values) { | ||
| 860 | |||
| 861 | # SAML2 attribute value | ||
| 862 | my $saml2value = $self->createAttributeValue( $_, | ||
| 863 | $self->spOptions->{$sp} | ||
| 864 | ->{samlSPMetaDataOptionsForceUTF8} ); | ||
| 865 | |||
| 866 | unless ($saml2value) { | ||
| 867 | $self->checkLassoError($@); | ||
| 868 | return $self->_failAuthnRequest( | ||
| 869 | $req, | ||
| 870 | msg => | ||
| 871 | "Unable to create a new SAML attribute value", | ||
| 872 | logInfo => { | ||
| 873 | sp => $spConfKey, | ||
| 874 | entity_id => $sp, | ||
| 875 | }, | ||
| 876 | ); | ||
| 877 | } | ||
| 878 | |||
| 879 | push @saml2values, $saml2value; | ||
| 880 | |||
| 881 | $self->logger->debug("Push $_ in SAML attribute $name"); | ||
| 882 | |||
| 883 | } | ||
| 884 | |||
| 885 | $attribute->AttributeValue(@saml2values); | ||
| 886 | |||
| 887 | # Push attribute in attribute list | ||
| 888 | push @attributes, $attribute; | ||
| 889 | |||
| 890 | # For logging | ||
| 891 | $log_attributes{ $friendly_name || $name } = [@values]; | ||
| 892 | } | ||
| 893 | |||
| 894 | # Get response assertion | ||
| 895 | my @response_assertions = $login->response->Assertion; | ||
| 896 | |||
| 897 | unless ( $response_assertions[0] ) { | ||
| 898 | return $self->_failAuthnRequest( | ||
| 899 | $req, | ||
| 900 | msg => "Unable to get response assertion", | ||
| 901 | logInfo => { | ||
| 902 | sp => $spConfKey, | ||
| 903 | entity_id => $sp, | ||
| 904 | }, | ||
| 905 | ); | ||
| 906 | } | ||
| 907 | |||
| 908 | # Rewrite Issuer with domain | ||
| 909 | if ($domain) { | ||
| 910 | my $original_issuer = $login->response->Issuer->content; | ||
| 911 | $self->logger->debug( | ||
| 912 | "Add domain $domain to Issuer $original_issuer"); | ||
| 913 | my $new_issuer = $original_issuer . "?domain=$domain"; | ||
| 914 | $login->response->Issuer->content($new_issuer); | ||
| 915 | $login->response->Assertion->Issuer->content($new_issuer); | ||
| 916 | } | ||
| 917 | |||
| 918 | # Set subject NameID | ||
| 919 | $response_assertions[0] | ||
| 920 | ->set_subject_name_id( $login->nameIdentifier ); | ||
| 921 | |||
| 922 | # Set basic conditions | ||
| 923 | my $oneTimeUse = | ||
| 924 | $self->spOptions->{$sp}->{samlSPMetaDataOptionsOneTimeUse} // 0; | ||
| 925 | |||
| 926 | my $conditionNotOnOrAfter = $notOnOrAfterTimeout || "86400"; | ||
| 927 | eval { | ||
| 928 | $response_assertions[0] | ||
| 929 | ->set_basic_conditions( 60, $conditionNotOnOrAfter, | ||
| 930 | $oneTimeUse ); | ||
| 931 | }; | ||
| 932 | if ($@) { | ||
| 933 | $self->logger->debug("Basic conditions not set: $@"); | ||
| 934 | } | ||
| 935 | |||
| 936 | # Create attribute statement | ||
| 937 | if ( scalar @attributes ) { | ||
| 938 | |||
| 939 | my $attribute_statement; | ||
| 940 | |||
| 941 | eval { | ||
| 942 | $attribute_statement = | ||
| 943 | Lasso::Saml2AttributeStatement->new(); | ||
| 944 | }; | ||
| 945 | if ($@) { | ||
| 946 | $self->checkLassoError($@); | ||
| 947 | return $self->_failAuthnRequest( | ||
| 948 | $req, | ||
| 949 | msg => "Unable to create attribute statement", | ||
| 950 | logInfo => { | ||
| 951 | sp => $spConfKey, | ||
| 952 | entity_id => $sp, | ||
| 953 | }, | ||
| 954 | ); | ||
| 955 | } | ||
| 956 | |||
| 957 | # Register attributes in attribute statement | ||
| 958 | $attribute_statement->Attribute(@attributes); | ||
| 959 | |||
| 960 | # Add attribute statement in response assertion | ||
| 961 | my @attributes_statement = ($attribute_statement); | ||
| 962 | $response_assertions[0] | ||
| 963 | ->AttributeStatement(@attributes_statement); | ||
| 964 | } | ||
| 965 | |||
| 966 | # Get AuthnStatement | ||
| 967 | my @authn_statements = $response_assertions[0]->AuthnStatement(); | ||
| 968 | |||
| 969 | # Set sessionIndex | ||
| 970 | my $sessionIndexSession = $self->getSamlSession(); | ||
| 971 | if ( !$sessionIndexSession ) { | ||
| 972 | return $self->_failAuthnRequest( | ||
| 973 | $req, | ||
| 974 | msg => "Unable to create SAML Session", | ||
| 975 | res => PE_SAML_SESSION_ERROR, | ||
| 976 | logInfo => { | ||
| 977 | sp => $spConfKey, | ||
| 978 | entity_id => $sp, | ||
| 979 | }, | ||
| 980 | ); | ||
| 981 | } | ||
| 982 | |||
| 983 | $sessionIndexSession->update( | ||
| 984 | { '_utime' => time, '_saml_id' => $session_id } ); | ||
| 985 | my $sessionIndex = $sessionIndexSession->id; | ||
| 986 | |||
| 987 | $authn_statements[0]->SessionIndex($sessionIndex); | ||
| 988 | |||
| 989 | $self->logger->debug( | ||
| 990 | "Set sessionIndex $sessionIndex (linked to session $session_id)" | ||
| 991 | ); | ||
| 992 | |||
| 993 | # Set SessionNotOnOrAfter | ||
| 994 | my $end_session_time = $time + $self->conf->{timeout}; | ||
| 995 | |||
| 996 | # If a timeout for the SP's session has been set, use it unless the | ||
| 997 | # SSO session is already about to expire | ||
| 998 | my $sessionNotOnOrAfterTimeout = | ||
| 999 | $self->spOptions->{$sp} | ||
| 1000 | ->{samlSPMetaDataOptionsSessionNotOnOrAfterTimeout}; | ||
| 1001 | if ( $sessionNotOnOrAfterTimeout | ||
| 1002 | and ( time + $sessionNotOnOrAfterTimeout < $end_session_time ) ) | ||
| 1003 | { | ||
| 1004 | |||
| 1005 | $end_session_time = time + $sessionNotOnOrAfterTimeout; | ||
| 1006 | } | ||
| 1007 | |||
| 1008 | my $sessionNotOnOrAfter = | ||
| 1009 | $self->timestamp2samldate($end_session_time); | ||
| 1010 | $authn_statements[0]->SessionNotOnOrAfter($sessionNotOnOrAfter); | ||
| 1011 | |||
| 1012 | $self->logger->debug( | ||
| 1013 | "Set sessionNotOnOrAfter $sessionNotOnOrAfter"); | ||
| 1014 | |||
| 1015 | # Register AuthnStatement in assertion | ||
| 1016 | $response_assertions[0]->AuthnStatement(@authn_statements); | ||
| 1017 | |||
| 1018 | # Set response assertion | ||
| 1019 | $login->response->Assertion(@response_assertions); | ||
| 1020 | |||
| 1021 | # Signature | ||
| 1022 | my $signSSOMessage = | ||
| 1023 | $self->spOptions->{$sp}->{samlSPMetaDataOptionsSignSSOMessage} | ||
| 1024 | // -1; | ||
| 1025 | |||
| 1026 | if ( $signSSOMessage == 0 ) { | ||
| 1027 | $self->logger->debug("SSO response will not be signed"); | ||
| 1028 | $self->disableSignature($login); | ||
| 1029 | } | ||
| 1030 | elsif ( $signSSOMessage == 1 ) { | ||
| 1031 | $self->logger->debug("SSO response will be signed"); | ||
| 1032 | $self->forceSignature($login); | ||
| 1033 | } | ||
| 1034 | else { | ||
| 1035 | $self->logger->debug( | ||
| 1036 | "SSO response signature according to metadata"); | ||
| 1037 | } | ||
| 1038 | |||
| 1039 | $h = | ||
| 1040 | $self->p->processHook( $req, 'samlBuildAuthnResponse', $login ); | ||
| 1041 | return $self->_failAuthnRequest( | ||
| 1042 | $req, | ||
| 1043 | msg => 'samlBuildAuthnResponse hook failed', | ||
| 1044 | res => $h, | ||
| 1045 | logInfo => { | ||
| 1046 | sp => $spConfKey, | ||
| 1047 | entity_id => $sp, | ||
| 1048 | }, | ||
| 1049 | ) if ( $h != PE_OK ); | ||
| 1050 | |||
| 1051 | # Build SAML response | ||
| 1052 | $protocolProfile = $login->protocolProfile(); | ||
| 1053 | |||
| 1054 | # Artifact | ||
| 1055 | # Choose method | ||
| 1056 | if ( $artifact | ||
| 1057 | or $protocolProfile == | ||
| 1058 | Lasso::Constants::LOGIN_PROTOCOL_PROFILE_BRWS_ART ) | ||
| 1059 | { | ||
| 1060 | $artifact = 1; | ||
| 1061 | if ( $method == $self->getHttpMethod("post") | ||
| 1062 | || $method == $self->getHttpMethod("artifact-post") ) | ||
| 1063 | { | ||
| 1064 | $artifact_method = $self->getHttpMethod("artifact-post"); | ||
| 1065 | |||
| 1066 | } | ||
| 1067 | else { | ||
| 1068 | $artifact_method = $self->getHttpMethod("artifact-get"); | ||
| 1069 | } | ||
| 1070 | } | ||
| 1071 | |||
| 1072 | if ( $protocolProfile == | ||
| 1073 | Lasso::Constants::LOGIN_PROTOCOL_PROFILE_BRWS_ART ) | ||
| 1074 | { | ||
| 1075 | |||
| 1076 | # Build artifact message | ||
| 1077 | unless ( $self->buildArtifactMsg( $login, $artifact_method ) ) { | ||
| 1078 | return $self->_failAuthnRequest( | ||
| 1079 | $req, | ||
| 1080 | msg => "Unable to build SSO artifact response message", | ||
| 1081 | res => PE_SAML_ART_ERROR, | ||
| 1082 | logInfo => { | ||
| 1083 | sp => $spConfKey, | ||
| 1084 | entity_id => $sp, | ||
| 1085 | }, | ||
| 1086 | ); | ||
| 1087 | } | ||
| 1088 | |||
| 1089 | $self->logger->debug("SSO: artifact response is built"); | ||
| 1090 | |||
| 1091 | # Get artifact ID and Content, and store them | ||
| 1092 | my $artifact_id = $login->get_artifact; | ||
| 1093 | my $artifact_message = $login->get_artifact_message; | ||
| 1094 | |||
| 1095 | $self->storeArtifact( $artifact_id, $artifact_message, | ||
| 1096 | $session_id ); | ||
| 1097 | } | ||
| 1098 | |||
| 1099 | # No artifact | ||
| 1100 | else { | ||
| 1101 | |||
| 1102 | unless ( $self->buildAuthnResponseMsg($login) ) { | ||
| 1103 | return $self->_failAuthnRequest( | ||
| 1104 | $req, | ||
| 1105 | msg => "Unable to build SSO response message", | ||
| 1106 | logInfo => { | ||
| 1107 | sp => $spConfKey, | ||
| 1108 | entity_id => $sp, | ||
| 1109 | }, | ||
| 1110 | ); | ||
| 1111 | } | ||
| 1112 | |||
| 1113 | $self->logger->debug("SSO: authentication response is built"); | ||
| 1114 | |||
| 1115 | } | ||
| 1116 | |||
| 1117 | # Save Identity and Session | ||
| 1118 | if ( $login->is_identity_dirty ) { | ||
| 1119 | |||
| 1120 | # Update session | ||
| 1121 | $self->logger->debug("Save Lasso identity in session"); | ||
| 1122 | $self->p->updatePersistentSession( $req, | ||
| 1123 | { $self->liDump => $login->get_identity->dump }, | ||
| 1124 | undef, $session_id ); | ||
| 1125 | } | ||
| 1126 | |||
| 1127 | if ( $login->is_session_dirty ) { | ||
| 1128 | $self->logger->debug("Save Lasso session in session"); | ||
| 1129 | $self->p->updateSession( $req, | ||
| 1130 | { $self->lsDump => $login->get_session->dump }, | ||
| 1131 | $session_id ); | ||
| 1132 | } | ||
| 1133 | |||
| 1134 | # Keep SAML elements for later queries | ||
| 1135 | my $nameid = $login->nameIdentifier; | ||
| 1136 | |||
| 1137 | $self->logger->debug( "Store NameID " | ||
| 1138 | . $nameid->dump | ||
| 1139 | . " and SessionIndex $sessionIndex for session $session_id" ); | ||
| 1140 | |||
| 1141 | my $infos; | ||
| 1142 | |||
| 1143 | $infos->{type} = 'saml'; # Session type | ||
| 1144 | $infos->{_utime} = $time; # Creation time | ||
| 1145 | $infos->{_saml_id} = $session_id; # SSO session id | ||
| 1146 | $infos->{_nameID} = $nameid->dump; # SAML NameID | ||
| 1147 | $infos->{_sessionIndex} = $sessionIndex; # SAML SessionIndex | ||
| 1148 | |||
| 1149 | my $samlSessionInfo = $self->getSamlSession( undef, $infos ); | ||
| 1150 | |||
| 1151 | if ( !$samlSessionInfo ) { | ||
| 1152 | return $self->_failAuthnRequest( | ||
| 1153 | $req, | ||
| 1154 | msg => "Could not get SAML session", | ||
| 1155 | res => PE_SAML_SESSION_ERROR, | ||
| 1156 | logInfo => { | ||
| 1157 | sp => $spConfKey, | ||
| 1158 | entity_id => $sp, | ||
| 1159 | }, | ||
| 1160 | ); | ||
| 1161 | } | ||
| 1162 | |||
| 1163 | my $saml_session_id = $samlSessionInfo->id; | ||
| 1164 | |||
| 1165 | $self->logger->debug( | ||
| 1166 | "Link session $session_id to SAML session $saml_session_id"); | ||
| 1167 | |||
| 1168 | $self->p->registerProtectedAppAccess( $req, | ||
| 1169 | $req->{sessionInfo}->{ $self->conf->{whatToTrace} }, | ||
| 1170 | "saml:$spConfKey" ); | ||
| 1171 | |||
| 1172 | # Send SSO Response | ||
| 1173 | |||
| 1174 | # Register IDP in Common Domain Cookie if needed | ||
| 1175 | if ( $self->conf->{samlCommonDomainCookieActivation} | ||
| 1176 | and $self->conf->{samlCommonDomainCookieWriter} ) | ||
| 1177 | { | ||
| 1178 | my $cdc_idp = $self->getMetaDataURL( "samlEntityID", 0, 1 ); | ||
| 1179 | |||
| 1180 | $self->logger->debug( | ||
| 1181 | "Will register IDP $cdc_idp in Common Domain Cookie"); | ||
| 1182 | |||
| 1183 | # Redirection to CDC Writer page in a hidden iframe | ||
| 1184 | my $cdc_writer_url = | ||
| 1185 | $self->conf->{samlCommonDomainCookieWriter}; | ||
| 1186 | $cdc_writer_url .= ( | ||
| 1187 | $self->conf->{samlCommonDomainCookieWriter} =~ /\?/ | ||
| 1188 | ? '&idp=' . $cdc_idp | ||
| 1189 | : '?url=' . $cdc_idp | ||
| 1190 | ); | ||
| 1191 | |||
| 1192 | my $cdc_iframe = | ||
| 1193 | qq'<iframe src="$cdc_writer_url"' | ||
| 1194 | . ' alt="Common Dommain Cookie" marginwidth="0"' | ||
| 1195 | . ' marginheight="0" scrolling="no" class="hiddenFrame"' | ||
| 1196 | . ' width="0" height="0" frameborder="0"></iframe>'; | ||
| 1197 | |||
| 1198 | $req->info( | ||
| 1199 | $self->loadTemplate( | ||
| 1200 | $req, 'simpleInfo', | ||
| 1201 | params => { trspan => 'updateCdc' } | ||
| 1202 | ) | ||
| 1203 | . $cdc_iframe | ||
| 1204 | ); | ||
| 1205 | } | ||
| 1206 | |||
| 1207 | # log that a SAML authn response is build | ||
| 1208 | my $user = $req->{sessionInfo}->{ $self->conf->{whatToTrace} }; | ||
| 1209 | my $nameIDLog = ''; | ||
| 1210 | foreach my $format (qw(persistent transient)) { | ||
| 1211 | if ( $login->nameIdentifier->Format eq | ||
| 1212 | $self->getNameIDFormat($format) ) | ||
| 1213 | { | ||
| 1214 | $nameIDLog = | ||
| 1215 | " with $format NameID " . $login->nameIdentifier->content; | ||
| 1216 | last; | ||
| 1217 | } | ||
| 1218 | } | ||
| 1219 | my $name_id_content = $login->nameIdentifier->content; | ||
| 1220 | my $attr_str = ( | ||
| 1221 | %log_attributes | ||
| 1222 | ? ( " with attributes " | ||
| 1223 | . join( ',', sort( keys(%log_attributes) ) ) ) | ||
| 1224 | : "" | ||
| 1225 | ); | ||
| 1226 | |||
| 1227 | # HTTP-POST | ||
| 1228 | if ( ( | ||
| 1229 | !$artifact | ||
| 1230 | and $protocolProfile eq | ||
| 1231 | Lasso::Constants::LOGIN_PROTOCOL_PROFILE_BRWS_POST | ||
| 1232 | ) | ||
| 1233 | or ( $artifact | ||
| 1234 | and $artifact_method == | ||
| 1235 | $self->getHttpMethod("artifact-post") ) | ||
| 1236 | ) | ||
| 1237 | { | ||
| 1238 | |||
| 1239 | $self->auditLog( | ||
| 1240 | $req, | ||
| 1241 | code => "ISSUER_SAML_LOGIN_SUCCESS", | ||
| 1242 | entity_id => $sp, | ||
| 1243 | sp => $spConfKey, | ||
| 1244 | message => ( | ||
| 1245 | 'User ' | ||
| 1246 | . $req->sessionInfo->{ $self->conf->{whatToTrace} } | ||
| 1247 | . " is authorized to access to $sp." | ||
| 1248 | . " SAML authentication response sent to" | ||
| 1249 | . " SAML SP $spConfKey for $user$nameIDLog$attr_str" | ||
| 1250 | ), | ||
| 1251 | user => $user, | ||
| 1252 | saml_name_id => $name_id_content, | ||
| 1253 | attributes => \%log_attributes, | ||
| 1254 | ); | ||
| 1255 | |||
| 1256 | # Use autosubmit form | ||
| 1257 | my $sso_url = $login->msg_url; | ||
| 1258 | my $sso_body = $login->msg_body; | ||
| 1259 | |||
| 1260 | $req->postUrl($sso_url); | ||
| 1261 | |||
| 1262 | if ( $artifact_method | ||
| 1263 | and $artifact_method == | ||
| 1264 | $self->getHttpMethod("artifact-post") ) | ||
| 1265 | { | ||
| 1266 | $req->{postFields} = { 'SAMLart' => $sso_body }; | ||
| 1267 | } | ||
| 1268 | else { | ||
| 1269 | $req->{postFields} = { 'SAMLResponse' => $sso_body }; | ||
| 1270 | } | ||
| 1271 | |||
| 1272 | # RelayState | ||
| 1273 | if ($relaystate) { | ||
| 1274 | $req->{postFields}->{'RelayState'} = | ||
| 1275 | encode_entities($relaystate); | ||
| 1276 | $req->data->{safeHiddenFormValues}->{RelayState} = 1; | ||
| 1277 | } | ||
| 1278 | |||
| 1279 | $req->steps( ['autoPost'] ); | ||
| 1280 | return PE_OK; | ||
| 1281 | } | ||
| 1282 | |||
| 1283 | # HTTP-REDIRECT | ||
| 1284 | if ( $protocolProfile eq | ||
| 1285 | Lasso::Constants::LOGIN_PROTOCOL_PROFILE_REDIRECT or $artifact ) | ||
| 1286 | { | ||
| 1287 | |||
| 1288 | $self->auditLog( | ||
| 1289 | $req, | ||
| 1290 | code => "ISSUER_SAML_LOGIN_SUCCESS", | ||
| 1291 | entity_id => $sp, | ||
| 1292 | sp => $spConfKey, | ||
| 1293 | message => ( | ||
| 1294 | 'User ' | ||
| 1295 | . $req->sessionInfo->{ $self->conf->{whatToTrace} } | ||
| 1296 | . " is authorized to access to $sp." | ||
| 1297 | . " SAML authentication response sent to" | ||
| 1298 | . " SAML SP $spConfKey for $user$nameIDLog$attr_str" | ||
| 1299 | ), | ||
| 1300 | user => $user, | ||
| 1301 | saml_name_id => $name_id_content, | ||
| 1302 | attributes => \%log_attributes, | ||
| 1303 | ); | ||
| 1304 | |||
| 1305 | # Redirect user to response URL | ||
| 1306 | my $sso_url = $login->msg_url; | ||
| 1307 | $self->logger->debug("Redirect user to $sso_url"); | ||
| 1308 | |||
| 1309 | $req->{urldc} = $sso_url; | ||
| 1310 | $req->mustRedirect(1); | ||
| 1311 | $req->steps( [] ); | ||
| 1312 | |||
| 1313 | return PE_OK; | ||
| 1314 | } | ||
| 1315 | |||
| 1316 | } | ||
| 1317 | |||
| 1318 | elsif ($response) { | ||
| 1319 | $self->logger->debug( | ||
| 1320 | "Authentication responses are not managed by this module"); | ||
| 1321 | return PE_OK; | ||
| 1322 | } | ||
| 1323 | |||
| 1324 | else { | ||
| 1325 | |||
| 1326 | # No request or response | ||
| 1327 | # This should not happen | ||
| 1328 | $self->logger->debug("No request or response found"); | ||
| 1329 | return PE_OK; | ||
| 1330 | } | ||
| 1331 | |||
| 1332 | } | ||
| 1333 | $self->logger->debug("Not an issuer request $url"); | ||
| 1334 | return PE_OK; | ||
| 1335 | } | ||
| 1336 | |||
| 1337 | sub _failAuthnRequest { | ||
| 1338 | my ( $self, $req, %params ) = @_; | ||
| 1339 | |||
| 1340 | my $reason = $params{'msg'} ? ": $params{'msg'}" : ""; | ||
| 1341 | my $res = $params{res} || PE_SAML_SSO_ERROR; | ||
| 1342 | my $user = $req->sessionInfo->{ $self->conf->{whatToTrace} }; | ||
| 1343 | my %logInfo = %{ $params{logInfo} || {} }; | ||
| 1344 | |||
| 1345 | $self->auditLog( | ||
| 1346 | $req, | ||
| 1347 | code => "ISSUER_SAML_LOGIN_FAILED", | ||
| 1348 | message => ( "SAML login failed" . $reason ), | ||
| 1349 | ( $params{'msg'} ? ( reason => $params{'msg'} ) : () ), | ||
| 1350 | portal_error => portalConsts->{$res}, | ||
| 1351 | user => $user, | ||
| 1352 | %logInfo, | ||
| 1353 | ); | ||
| 1354 | |||
| 1355 | return $res; | ||
| 1356 | } | ||
| 1357 | |||
| 1358 | sub artifactServer { | ||
| 1359 | my ( $self, $req ) = @_; | ||
| 1360 | $self->logger->debug( "URL " | ||
| 1361 | . $req->uri | ||
| 1362 | . " detected as an artifact resolution service URL" ); | ||
| 1363 | |||
| 1364 | # Artifact request are sent with SOAP trough POST | ||
| 1365 | my $art_request = $req->content; | ||
| 1366 | my $art_response; | ||
| 1367 | |||
| 1368 | # Create Login object | ||
| 1369 | my $login = $self->createLogin( $self->lassoServer ); | ||
| 1370 | |||
| 1371 | # Process request message | ||
| 1372 | unless ( $self->processArtRequestMsg( $login, $art_request ) ) { | ||
| 1373 | return $self->p->sendError( $req, | ||
| 1374 | 'Unable to process artifact request message', 400 ); | ||
| 1375 | } | ||
| 1376 | |||
| 1377 | # Check Destination | ||
| 1378 | unless ( $self->checkDestination( $login->request, $req->uri ) ) { | ||
| 1379 | return $self->p->sendError( $req, 'Bad request', 400 ); | ||
| 1380 | } | ||
| 1381 | |||
| 1382 | # Create artifact response | ||
| 1383 | unless ( $art_response = $self->createArtifactResponse( $req, $login ) ) { | ||
| 1384 | return $self->p->sendError( $req, | ||
| 1385 | "Unable to create artifact response message", 400 ); | ||
| 1386 | } | ||
| 1387 | |||
| 1388 | $self->{SOAPMessage} = $art_response; | ||
| 1389 | |||
| 1390 | # Return SOAP message | ||
| 1391 | $self->logger->debug("Send SOAP Message: $art_response"); | ||
| 1392 | return [ | ||
| 1393 | 200, | ||
| 1394 | [ | ||
| 1395 | 'Content-Type' => 'text/xml', | ||
| 1396 | 'Content-Length' => length($art_response) | ||
| 1397 | ], | ||
| 1398 | [$art_response] | ||
| 1399 | ]; | ||
| 1400 | } | ||
| 1401 | |||
| 1402 | sub soapSloServer { | ||
| 1403 | my ( $self, $req ) = @_; | ||
| 1404 | my $url = $req->uri; | ||
| 1405 | my $request_method = $req->param('issuerMethod') || $req->method; | ||
| 1406 | my $content_type = $req->content_type(); | ||
| 1407 | |||
| 1408 | $self->logger->debug("URL $url detected as an SLO URL"); | ||
| 1409 | |||
| 1410 | # Check SAML Message | ||
| 1411 | my ( $request, $response, $method, $relaystate, $artifact ) = | ||
| 1412 | $self->checkMessage( $req, $url, $request_method, $content_type, | ||
| 1413 | "logout" ); | ||
| 1414 | |||
| 1415 | # Create Logout object | ||
| 1416 | my $logout = $self->createLogout( $self->lassoServer ); | ||
| 1417 | |||
| 1418 | # Ignore signature verification | ||
| 1419 | $self->disableSignatureVerification($logout); | ||
| 1420 | |||
| 1421 | if ($request) { | ||
| 1422 | |||
| 1423 | # Process logout request | ||
| 1424 | unless ( $self->processLogoutRequestMsg( $logout, $request ) ) { | ||
| 1425 | return $self->p->sendError( $req, | ||
| 1426 | "SLO: Fail to process logout request", 400 ); | ||
| 1427 | } | ||
| 1428 | |||
| 1429 | $self->logger->debug("SLO: Logout request is valid"); | ||
| 1430 | |||
| 1431 | my $h = $self->p->processHook( $req, 'samlGotLogoutRequest', $logout ); | ||
| 1432 | if ( $h != PE_OK ) { | ||
| 1433 | return $self->p->sendError( $req, | ||
| 1434 | "SLO: samlGotLogoutRequest hook returned error", 400 ); | ||
| 1435 | } | ||
| 1436 | |||
| 1437 | # We accept only SOAP here | ||
| 1438 | unless ( $method eq $self->getHttpMethod('soap') ) { | ||
| 1439 | return $self->p->sendError( $req, | ||
| 1440 | "Only SOAP requests allowed here", 400 ); | ||
| 1441 | } | ||
| 1442 | |||
| 1443 | # Get SP entityID | ||
| 1444 | my $sp = $logout->remote_providerID(); | ||
| 1445 | |||
| 1446 | $self->logger->debug("Found entityID $sp in SAML message"); | ||
| 1447 | |||
| 1448 | # SP conf key | ||
| 1449 | my $spConfKey = $self->spList->{$sp}->{confKey}; | ||
| 1450 | |||
| 1451 | unless ($spConfKey) { | ||
| 1452 | return $self->p->sendError( $req, | ||
| 1453 | "$sp do not match any SP in configuration", 400 ); | ||
| 1454 | } | ||
| 1455 | |||
| 1456 | $self->logger->debug("$sp match $spConfKey SP in configuration"); | ||
| 1457 | |||
| 1458 | # Do we check signature? | ||
| 1459 | my $checkSLOMessageSignature = | ||
| 1460 | $self->spOptions->{$sp} | ||
| 1461 | ->{samlSPMetaDataOptionsCheckSLOMessageSignature}; | ||
| 1462 | |||
| 1463 | if ($checkSLOMessageSignature) { | ||
| 1464 | |||
| 1465 | $self->forceSignatureVerification($logout); | ||
| 1466 | |||
| 1467 | unless ( $self->processLogoutRequestMsg( $logout, $request ) ) { | ||
| 1468 | return $self->p->sendError( $req, "Signature is not valid", | ||
| 1469 | 400 ); | ||
| 1470 | } | ||
| 1471 | else { | ||
| 1472 | $self->logger->debug("Signature is valid"); | ||
| 1473 | } | ||
| 1474 | } | ||
| 1475 | else { | ||
| 1476 | $self->logger->debug("Message signature will not be checked"); | ||
| 1477 | } | ||
| 1478 | |||
| 1479 | # Get SAML request | ||
| 1480 | my $saml_request = $logout->request(); | ||
| 1481 | unless ($saml_request) { | ||
| 1482 | return $self->p->sendError( $req, "No SAML request found", 400 ); | ||
| 1483 | } | ||
| 1484 | |||
| 1485 | # Check Destination | ||
| 1486 | return $self->sendSLOSoapErrorResponse( $req, $logout, $method ) | ||
| 1487 | unless ( $self->checkDestination( $saml_request, $url ) ); | ||
| 1488 | |||
| 1489 | # Get session index | ||
| 1490 | my $session_index; | ||
| 1491 | eval { $session_index = $logout->request()->SessionIndex; }; | ||
| 1492 | |||
| 1493 | # SLO requests without session index are not accepted in SOAP mode | ||
| 1494 | unless ( defined $session_index ) { | ||
| 1495 | $self->p->sendError( $req, | ||
| 1496 | "No session index in SLO request from $spConfKey SP", 400 ); | ||
| 1497 | } | ||
| 1498 | |||
| 1499 | # Get session index | ||
| 1500 | my $sessionIndexSession = $self->getSamlSession($session_index); | ||
| 1501 | return $self->p->sendError( $req, 'SAML session not found', 400 ) | ||
| 1502 | unless $sessionIndexSession; | ||
| 1503 | |||
| 1504 | my $local_session_id = $sessionIndexSession->data->{_saml_id}; | ||
| 1505 | |||
| 1506 | $sessionIndexSession->remove; | ||
| 1507 | |||
| 1508 | $self->logger->debug( "Get session id $local_session_id" | ||
| 1509 | . " (from session index $session_index)" ); | ||
| 1510 | |||
| 1511 | # Open local session | ||
| 1512 | my $local_session = $self->p->getApacheSession($local_session_id); | ||
| 1513 | |||
| 1514 | unless ($local_session) { | ||
| 1515 | return $self->p->sendError( $req, "No local session found", 400 ); | ||
| 1516 | } | ||
| 1517 | |||
| 1518 | # Load Session and Identity if they exist | ||
| 1519 | my $session = $local_session->data->{ $self->lsDump }; | ||
| 1520 | my $identity = $local_session->data->{ $self->liDump }; | ||
| 1521 | |||
| 1522 | if ($session) { | ||
| 1523 | unless ( $self->setSessionFromDump( $logout, $session ) ) { | ||
| 1524 | return $self->p->sendError( $req, | ||
| 1525 | "Unable to load Lasso Session", 400 ); | ||
| 1526 | } | ||
| 1527 | $self->logger->debug("Lasso Session loaded"); | ||
| 1528 | } | ||
| 1529 | |||
| 1530 | if ($identity) { | ||
| 1531 | unless ( $self->setIdentityFromDump( $logout, $identity ) ) { | ||
| 1532 | return $self->p->sendError( $req, | ||
| 1533 | "Unable to load Lasso Identity", 400 ); | ||
| 1534 | } | ||
| 1535 | $self->logger->debug("Lasso Identity loaded"); | ||
| 1536 | } | ||
| 1537 | |||
| 1538 | # Close SAML sessions | ||
| 1539 | unless ( $self->deleteSAMLSecondarySessions($local_session_id) ) { | ||
| 1540 | return $self->p->sendError( $req, "Fail to delete SAML sessions", | ||
| 1541 | 400 ); | ||
| 1542 | } | ||
| 1543 | |||
| 1544 | # Close local session | ||
| 1545 | unless ( $self->p->_deleteSession( $req, $local_session ) ) { | ||
| 1546 | return $self->p->sendError( $req, | ||
| 1547 | "Fail to delete session $local_session_id", 400 ); | ||
| 1548 | } | ||
| 1549 | |||
| 1550 | # Validate request if no previous error | ||
| 1551 | unless ( $self->validateLogoutRequest($logout) ) { | ||
| 1552 | return $self->p->sendError( $req, "SLO request is not valid", 400 ); | ||
| 1553 | } | ||
| 1554 | |||
| 1555 | # Try to send SLO request trough SOAP | ||
| 1556 | $self->resetProviderIdIndex($logout); | ||
| 1557 | while ( my $providerID = $self->getNextProviderId($logout) ) { | ||
| 1558 | |||
| 1559 | # Send logout request | ||
| 1560 | my ( $rstatus, $rmethod, $rinfo ) = | ||
| 1561 | $self->sendLogoutRequestToProvider( $logout, $providerID, | ||
| 1562 | $self->getHttpMethod('soap'), 0 ); | ||
| 1563 | |||
| 1564 | if ($rstatus) { | ||
| 1565 | $self->logger->debug("SOAP SLO successful on $providerID"); | ||
| 1566 | } | ||
| 1567 | else { | ||
| 1568 | $self->logger->debug("SOAP SLO error on $providerID"); | ||
| 1569 | } | ||
| 1570 | } | ||
| 1571 | |||
| 1572 | # Set RelayState | ||
| 1573 | if ($relaystate) { | ||
| 1574 | $logout->msg_relayState($relaystate); | ||
| 1575 | $self->logger->debug("Set $relaystate in RelayState"); | ||
| 1576 | } | ||
| 1577 | |||
| 1578 | # Signature | ||
| 1579 | my $signSLOMessage = | ||
| 1580 | $self->spOptions->{$sp}->{samlSPMetaDataOptionsSignSLOMessage} // 0; | ||
| 1581 | |||
| 1582 | if ( $signSLOMessage == 0 ) { | ||
| 1583 | $self->logger->debug("SLO response will not be signed"); | ||
| 1584 | $self->disableSignature($logout); | ||
| 1585 | } | ||
| 1586 | elsif ( $signSLOMessage == 1 ) { | ||
| 1587 | $self->logger->debug("SLO response will be signed"); | ||
| 1588 | $self->forceSignature($logout); | ||
| 1589 | } | ||
| 1590 | else { | ||
| 1591 | $self->logger->debug( | ||
| 1592 | "SLO response signature according to metadata"); | ||
| 1593 | } | ||
| 1594 | |||
| 1595 | $h = $self->p->processHook( $req, 'samlBuildLogoutResponse', $logout ); | ||
| 1596 | if ( $h != PE_OK ) { | ||
| 1597 | return $self->p->sendError( $req, | ||
| 1598 | "SLO: samlBuildLogoutResponse hook returned error", 400 ); | ||
| 1599 | } | ||
| 1600 | |||
| 1601 | # Send logout response | ||
| 1602 | unless ( $self->buildLogoutResponseMsg($logout) ) { | ||
| 1603 | $self->logger->error("Unable to build SLO response for $spConfKey"); | ||
| 1604 | return $self->p->sendError( $req, 'Unable to build SLO response', | ||
| 1605 | 400 ); | ||
| 1606 | } | ||
| 1607 | my $slo_body = $logout->msg_body; | ||
| 1608 | return [ | ||
| 1609 | 200, | ||
| 1610 | [ | ||
| 1611 | 'Content-Type' => 'text/xml', | ||
| 1612 | 'Content-Length' => length($slo_body) | ||
| 1613 | ], | ||
| 1614 | [$slo_body] | ||
| 1615 | ]; | ||
| 1616 | } | ||
| 1617 | |||
| 1618 | } | ||
| 1619 | |||
| 1620 | sub logout { | ||
| 1621 | my ( $self, $req ) = @_; | ||
| 1622 | return PE_OK if ( $req->data->{samlSLOCalled} ); | ||
| 1623 | |||
| 1624 | # Session ID | ||
| 1625 | my $session_id = $req->{sessionInfo}->{_session_id} || $req->id; | ||
| 1626 | |||
| 1627 | # Close SAML sessions | ||
| 1628 | unless ( $self->deleteSAMLSecondarySessions($session_id) ) { | ||
| 1629 | $self->logger->error("Fail to delete SAML sessions"); | ||
| 1630 | } | ||
| 1631 | |||
| 1632 | # Create Logout object | ||
| 1633 | my $logout = $self->createLogout( $self->lassoServer ); | ||
| 1634 | |||
| 1635 | # Load Session and Identity if they exist | ||
| 1636 | my $session = $req->{sessionInfo}->{ $self->lsDump }; | ||
| 1637 | my $identity = $req->{sessionInfo}->{ $self->liDump }; | ||
| 1638 | |||
| 1639 | if ($session) { | ||
| 1640 | unless ( $self->setSessionFromDump( $logout, $session ) ) { | ||
| 1641 | $self->logger->error("Unable to load Lasso Session"); | ||
| 1642 | return PE_SLO_ERROR; | ||
| 1643 | } | ||
| 1644 | $self->logger->debug("Lasso Session loaded"); | ||
| 1645 | } | ||
| 1646 | |||
| 1647 | # No need to initiate logout requests on SP, if no SAML session is | ||
| 1648 | # available into the session. | ||
| 1649 | else { | ||
| 1650 | $self->logger->debug('No SAML session available into this session'); | ||
| 1651 | return PE_OK; | ||
| 1652 | } | ||
| 1653 | |||
| 1654 | if ($identity) { | ||
| 1655 | unless ( $self->setIdentityFromDump( $logout, $identity ) ) { | ||
| 1656 | $self->logger->error("Unable to load Lasso Identity"); | ||
| 1657 | return PE_SLO_ERROR; | ||
| 1658 | } | ||
| 1659 | $self->logger->debug("Lasso Identity loaded"); | ||
| 1660 | } | ||
| 1661 | |||
| 1662 | # Proceed to logout on all others SP. | ||
| 1663 | # Verify that logout response is correctly sent. If we have to wait for | ||
| 1664 | # providers during HTTP-REDIRECT process, return PE_INFO to notify to wait | ||
| 1665 | # for them. | ||
| 1666 | # Redirect on logout page when all is done. | ||
| 1667 | if ( $self->sendLogoutRequestToProviders( $req, $logout ) ) { | ||
| 1668 | $req->urldc( $self->p->buildUrl( $req->portal, { logout => 1 } ) ); | ||
| 1669 | return PE_OK; | ||
| 1670 | } | ||
| 1671 | |||
| 1672 | return PE_OK; | ||
| 1673 | } | ||
| 1674 | |||
| 1675 | sub sloRelaySoap { | ||
| 1676 | my ( $self, $req ) = @_; | ||
| 1677 | $self->logger->debug( | ||
| 1678 | "URL " . $req->uri . " detected as a SOAP relay service URL" ); | ||
| 1679 | |||
| 1680 | # Check if relay parameter is present (mandatory) | ||
| 1681 | my $relayID; | ||
| 1682 | unless ( $relayID = $req->param('relay') ) { | ||
| 1683 | $self->logger->error("No relayID detected"); | ||
| 1684 | return $self->imgnok($req); | ||
| 1685 | } | ||
| 1686 | |||
| 1687 | # Retrieve the corresponding data from samlStorage | ||
| 1688 | my $relayInfos = $self->getSamlSession($relayID); | ||
| 1689 | unless ($relayInfos) { | ||
| 1690 | $self->logger->warn("Could not get relay session $relayID"); | ||
| 1691 | return $self->imgnok($req); | ||
| 1692 | } | ||
| 1693 | |||
| 1694 | $self->logger->debug("Found relay session $relayID"); | ||
| 1695 | |||
| 1696 | # Rebuild the logout object | ||
| 1697 | my $logout; | ||
| 1698 | unless ( $logout = $self->createLogout( $self->lassoServer ) ) { | ||
| 1699 | $self->logger->error("Could not rebuild logout object"); | ||
| 1700 | return $self->imgnok($req); | ||
| 1701 | } | ||
| 1702 | |||
| 1703 | # Load Session and Identity if they exist | ||
| 1704 | my $session = $relayInfos->data->{ $self->lsDump }; | ||
| 1705 | my $identity = $relayInfos->data->{ $self->liDump }; | ||
| 1706 | my $providerID = $relayInfos->data->{_providerID}; | ||
| 1707 | my $relayState = $relayInfos->data->{_relayState} // ''; | ||
| 1708 | my $spConfKey = $self->spList->{$providerID}->{confKey}; | ||
| 1709 | |||
| 1710 | if ($session) { | ||
| 1711 | unless ( $self->setSessionFromDump( $logout, $session ) ) { | ||
| 1712 | $self->logger->error("Unable to load Lasso Session"); | ||
| 1713 | return $self->imgnok($req); | ||
| 1714 | } | ||
| 1715 | $self->logger->debug("Lasso Session loaded"); | ||
| 1716 | } | ||
| 1717 | |||
| 1718 | if ($identity) { | ||
| 1719 | unless ( $self->setIdentityFromDump( $logout, $identity ) ) { | ||
| 1720 | $self->logger->error("Unable to load Lasso Identity"); | ||
| 1721 | return $self->imgnok($req); | ||
| 1722 | } | ||
| 1723 | $self->logger->debug("Lasso Identity loaded"); | ||
| 1724 | } | ||
| 1725 | |||
| 1726 | # Send the logout request | ||
| 1727 | my ( $rstatus, $rmethod, $rinfo ) = | ||
| 1728 | $self->sendLogoutRequestToProvider( $req, $logout, $providerID, | ||
| 1729 | Lasso::Constants::HTTP_METHOD_SOAP, | ||
| 1730 | undef, $relayState ); | ||
| 1731 | unless ($rstatus) { | ||
| 1732 | $self->logger->error( | ||
| 1733 | "Fail to process SOAP logout request to $providerID"); | ||
| 1734 | return $self->imgnok($req); | ||
| 1735 | } | ||
| 1736 | |||
| 1737 | # Store success status for this SLO request | ||
| 1738 | my $sloStatusSessionInfos = | ||
| 1739 | $self->getSamlSession( $relayState, | ||
| 1740 | { $spConfKey => 1, _utime => time() } ); | ||
| 1741 | |||
| 1742 | if ($sloStatusSessionInfos) { | ||
| 1743 | $self->logger->debug( | ||
| 1744 | "Store SLO status for $spConfKey in session $relayState"); | ||
| 1745 | } | ||
| 1746 | else { | ||
| 1747 | $self->logger->warn( | ||
| 1748 | "Unable to store SLO status for $spConfKey in session $relayState"); | ||
| 1749 | } | ||
| 1750 | |||
| 1751 | # Delete relay session | ||
| 1752 | $relayInfos->remove(); | ||
| 1753 | |||
| 1754 | # SLO response is OK | ||
| 1755 | $self->logger->debug("Display OK status for SLO on $spConfKey"); | ||
| 1756 | return $self->imgok($req); | ||
| 1757 | } | ||
| 1758 | |||
| 1759 | sub sloRelayPost { | ||
| 1760 | my ( $self, $req ) = @_; | ||
| 1761 | $self->logger->debug( | ||
| 1762 | "URL " . $req->uri . " detected as a POST relay service URL" ); | ||
| 1763 | |||
| 1764 | # Check if relay parameter is present (mandatory) | ||
| 1765 | my $relayID; | ||
| 1766 | unless ( $relayID = $req->param('relay') ) { | ||
| 1767 | return $self->p->sendError( $req, 'No relayID detected' ); | ||
| 1768 | } | ||
| 1769 | |||
| 1770 | # Retrieve the corresponding data from samlStorage | ||
| 1771 | my $relayInfos = $self->getSamlSession($relayID); | ||
| 1772 | unless ($relayInfos) { | ||
| 1773 | return $self->p->sendError( $req, | ||
| 1774 | "Could not get relay session $relayID" ); | ||
| 1775 | } | ||
| 1776 | |||
| 1777 | $self->logger->debug("Found relay session $relayID"); | ||
| 1778 | |||
| 1779 | # Get data to build POST form | ||
| 1780 | $req->{postUrl} = $relayInfos->data->{url}; | ||
| 1781 | $req->{postFields}->{'SAMLRequest'} = $relayInfos->data->{body}; | ||
| 1782 | |||
| 1783 | # RelayState | ||
| 1784 | if ( $relayInfos->data->{relayState} ) { | ||
| 1785 | $req->{postFields}->{'RelayState'} = | ||
| 1786 | encode_entities( $relayInfos->data->{relayState} ); | ||
| 1787 | $req->data->{safeHiddenFormValues}->{RelayState} = 1; | ||
| 1788 | } | ||
| 1789 | |||
| 1790 | # Delete relay session | ||
| 1791 | $relayInfos->remove(); | ||
| 1792 | $req->frame(1); | ||
| 1793 | return $self->p->do( $req, ['autoPost'] ); | ||
| 1794 | } | ||
| 1795 | |||
| 1796 | sub sloRelayTerm { | ||
| 1797 | my ( $self, $req ) = @_; | ||
| 1798 | $self->logger->debug( "URL " | ||
| 1799 | . $req->uri | ||
| 1800 | . " detected as a SLO Termination relay service URL" ); | ||
| 1801 | |||
| 1802 | # Check if relay parameter is present (mandatory) | ||
| 1803 | my $relayID = $self->p->getHiddenFormValue( $req, 'relay', '', 0 ) | ||
| 1804 | || $req->param('relay'); | ||
| 1805 | unless ($relayID) { | ||
| 1806 | return $self->p->sendError( $req, 'No relayID detected' ); | ||
| 1807 | } | ||
| 1808 | |||
| 1809 | # Retrieve the corresponding data from samlStorage | ||
| 1810 | my $relayInfos = $self->getSamlSession($relayID); | ||
| 1811 | unless ($relayInfos) { | ||
| 1812 | return $self->p->sendError( $req, | ||
| 1813 | "Could not get relay session $relayID" ); | ||
| 1814 | } | ||
| 1815 | |||
| 1816 | $self->logger->debug("Found relay session $relayID"); | ||
| 1817 | |||
| 1818 | # Get data from relay session | ||
| 1819 | my $logout_dump = $relayInfos->data->{_logout}; | ||
| 1820 | my $session_dump = $relayInfos->data->{_session}; | ||
| 1821 | my $method = $relayInfos->data->{_method}; | ||
| 1822 | |||
| 1823 | unless ($logout_dump) { | ||
| 1824 | $self->logger->error("Could not get logout dump"); | ||
| 1825 | return PE_SLO_ERROR; | ||
| 1826 | } | ||
| 1827 | |||
| 1828 | # Rebuild Lasso::Logout object | ||
| 1829 | my $logout = $self->createLogout( $self->lassoServer, $logout_dump ); | ||
| 1830 | |||
| 1831 | unless ($logout) { | ||
| 1832 | $self->logger->error("Could not build Lasso::Logout"); | ||
| 1833 | return PE_SLO_ERROR; | ||
| 1834 | } | ||
| 1835 | |||
| 1836 | # Inject session | ||
| 1837 | unless ($session_dump) { | ||
| 1838 | $self->logger->error("Could not get session dump"); | ||
| 1839 | return PE_SLO_ERROR; | ||
| 1840 | } | ||
| 1841 | |||
| 1842 | unless ( $self->setSessionFromDump( $logout, $session_dump ) ) { | ||
| 1843 | $self->logger->error("Could not set session from dump"); | ||
| 1844 | return PE_SLO_ERROR; | ||
| 1845 | } | ||
| 1846 | |||
| 1847 | # Get Lasso::Session | ||
| 1848 | my $session = $logout->get_session(); | ||
| 1849 | |||
| 1850 | unless ($session) { | ||
| 1851 | $self->logger->error("Could not get session from logout"); | ||
| 1852 | return PE_SLO_ERROR; | ||
| 1853 | } | ||
| 1854 | |||
| 1855 | # Loop on assertions and remove them if SLO status is OK | ||
| 1856 | $self->resetProviderIdIndex($logout); | ||
| 1857 | |||
| 1858 | while ( my $sp = $self->getNextProviderId($logout) ) { | ||
| 1859 | |||
| 1860 | # Try to get SLO status from SLO session | ||
| 1861 | my $spConfKey = $self->spList->{$sp}->{confKey}; | ||
| 1862 | my $status = $relayInfos->data->{$spConfKey}; | ||
| 1863 | |||
| 1864 | # Remove assertion if status is OK | ||
| 1865 | if ($status) { | ||
| 1866 | eval { $session->remove_assertion($sp); }; | ||
| 1867 | |||
| 1868 | if ($@) { | ||
| 1869 | $self->logger->warn("Unable to remove assertion for $sp"); | ||
| 1870 | } | ||
| 1871 | else { | ||
| 1872 | $self->logger->debug("Assertion removed for $sp"); | ||
| 1873 | } | ||
| 1874 | } | ||
| 1875 | else { | ||
| 1876 | $self->logger->debug( | ||
| 1877 | "SLO status was not ok for $sp, assertion not removed"); | ||
| 1878 | } | ||
| 1879 | } | ||
| 1880 | |||
| 1881 | # Reinject session | ||
| 1882 | unless ( $session->is_empty() ) { | ||
| 1883 | $self->setSessionFromDump( $logout, $session->dump ); | ||
| 1884 | } | ||
| 1885 | |||
| 1886 | # Delete relay session | ||
| 1887 | $relayInfos->remove(); | ||
| 1888 | |||
| 1889 | # Send SLO response | ||
| 1890 | if ( my $tmp = | ||
| 1891 | $self->sendLogoutResponseToServiceProvider( $req, $logout, $method ) ) | ||
| 1892 | { | ||
| 1893 | return $tmp; | ||
| 1894 | } | ||
| 1895 | else { | ||
| 1896 | $self->logger->error("Fail to send SLO response"); | ||
| 1897 | return PE_SLO_ERROR; | ||
| 1898 | } | ||
| 1899 | |||
| 1900 | } | ||
| 1901 | |||
| 1902 | sub authSloServer { | ||
| 1903 | my ( $self, $req ) = @_; | ||
| 1904 | $self->p->importHandlerData($req); | ||
| 1905 | return $self->sloServer($req); | ||
| 1906 | } | ||
| 1907 | |||
| 1908 | sub sloResume { | ||
| 1909 | my ( $self, $req ) = @_; | ||
| 1910 | |||
| 1911 | my $ResumeParams = $req->params('ResumeParams'); | ||
| 1912 | |||
| 1913 | unless ($ResumeParams) { | ||
| 1914 | $self->logger->error("Could not find resumption info"); | ||
| 1915 | return PE_SLO_ERROR; | ||
| 1916 | } | ||
| 1917 | |||
| 1918 | my $logoutContextSession = $self->getSamlSession($ResumeParams); | ||
| 1919 | |||
| 1920 | unless ($logoutContextSession) { | ||
| 1921 | $self->logger->warn("Could not find logout context session"); | ||
| 1922 | return PE_SLO_ERROR; | ||
| 1923 | } | ||
| 1924 | |||
| 1925 | my $spConfKey = $logoutContextSession->data->{spConfKey}; | ||
| 1926 | my $method = $logoutContextSession->data->{method}; | ||
| 1927 | my $provider_nb = $logoutContextSession->data->{provider_nb}; | ||
| 1928 | my $relayID = $logoutContextSession->data->{relayID}; | ||
| 1929 | |||
| 1930 | # Restore Lasso logout object from XML dump | ||
| 1931 | my $logout = $self->createLogout( $self->lassoServer, | ||
| 1932 | $logoutContextSession->data->{logout} ); | ||
| 1933 | |||
| 1934 | # Restore session info (for logout of other SPs) | ||
| 1935 | $req->setInfo( $logoutContextSession->data->{info} ) | ||
| 1936 | if $logoutContextSession->data->{info}; | ||
| 1937 | |||
| 1938 | return $self->_finishSlo( $req, $logout, $method, $spConfKey, | ||
| 1939 | $provider_nb, $relayID ); | ||
| 1940 | } | ||
| 1941 | |||
| 1942 | sub _finishSlo { | ||
| 1943 | my ( $self, $req, $logout, $method, $spConfKey, $provider_nb, $relayID ) = | ||
| 1944 | @_; | ||
| 1945 | |||
| 1946 | # Signature | ||
| 1947 | my $sp = $logout->remote_providerID; | ||
| 1948 | my $signSLOMessage = ''; | ||
| 1949 | $signSLOMessage = | ||
| 1950 | $self->spOptions->{$sp}->{samlSPMetaDataOptionsSignSLOMessage} | ||
| 1951 | if $sp; | ||
| 1952 | |||
| 1953 | unless ($signSLOMessage) { | ||
| 1954 | $self->logger->debug("Do not sign this SLO response"); | ||
| 1955 | return $self->sendSLOErrorResponse( $req, $logout, $method ) | ||
| 1956 | unless ( $self->disableSignature($logout) ); | ||
| 1957 | } | ||
| 1958 | |||
| 1959 | # If no waiting SP, return directly SLO response | ||
| 1960 | unless ($provider_nb) { | ||
| 1961 | return $self->sendLogoutResponseToServiceProvider( $req, $logout, | ||
| 1962 | $method ); | ||
| 1963 | } | ||
| 1964 | |||
| 1965 | # Else build SLO status relay URL and display info | ||
| 1966 | else { | ||
| 1967 | $req->{urldc} = | ||
| 1968 | $self->p->buildUrl( $req->portal, 'saml', | ||
| 1969 | 'relaySingleLogoutTermination' ); | ||
| 1970 | $self->p->setHiddenFormValue( $req, 'relay', $relayID, '', 0 ); | ||
| 1971 | return $self->p->do( $req, [] ); | ||
| 1972 | } | ||
| 1973 | } | ||
| 1974 | |||
| 1975 | sub sloServer { | ||
| 1976 | my ( $self, $req ) = @_; | ||
| 1977 | my $url = $req->uri; | ||
| 1978 | my $request_method = $req->param('issuerMethod') || $req->method; | ||
| 1979 | my $content_type = $req->content_type(); | ||
| 1980 | $self->logger->debug("URL $url detected as an SLO URL"); | ||
| 1981 | |||
| 1982 | # Check SAML Message | ||
| 1983 | my ( $request, $response, $method, $relaystate, $artifact ) = | ||
| 1984 | $self->checkMessage( $req, $url, $request_method, $content_type, | ||
| 1985 | "logout" ); | ||
| 1986 | |||
| 1987 | # Create Logout object | ||
| 1988 | my $logout = $self->createLogout( $self->lassoServer ); | ||
| 1989 | |||
| 1990 | # Ignore signature verification | ||
| 1991 | $self->disableSignatureVerification($logout); | ||
| 1992 | |||
| 1993 | # Disable Content-Security-Policy header since logout can be embedded in | ||
| 1994 | # a frame | ||
| 1995 | $req->frame(1); | ||
| 1996 | |||
| 1997 | if ($request) { | ||
| 1998 | |||
| 1999 | # Process logout request | ||
| 2000 | unless ( $self->processLogoutRequestMsg( $logout, $request ) ) { | ||
| 2001 | return $self->p->sendError( $req, | ||
| 2002 | "SLO: Fail to process logout request", 400 ); | ||
| 2003 | } | ||
| 2004 | |||
| 2005 | $self->logger->debug("SLO: Logout request is valid"); | ||
| 2006 | |||
| 2007 | my $h = $self->p->processHook( $req, 'samlGotLogoutRequest', $logout ); | ||
| 2008 | if ( $h != PE_OK ) { | ||
| 2009 | return $self->p->sendError( $req, | ||
| 2010 | "SLO: samlGotLogoutRequest hook returned error", 400 ); | ||
| 2011 | } | ||
| 2012 | |||
| 2013 | # Get SP entityID | ||
| 2014 | my $sp = $logout->remote_providerID(); | ||
| 2015 | $req->env->{llng_saml_sp} = $sp; | ||
| 2016 | |||
| 2017 | $self->logger->debug("Found entityID $sp in SAML message"); | ||
| 2018 | |||
| 2019 | # SP conf key | ||
| 2020 | my $spConfKey = $self->spList->{$sp}->{confKey}; | ||
| 2021 | |||
| 2022 | unless ($spConfKey) { | ||
| 2023 | return $self->p->sendError( $req, | ||
| 2024 | "$sp do not match any SP in configuration", 400 ); | ||
| 2025 | } | ||
| 2026 | |||
| 2027 | $self->logger->debug("$sp match $spConfKey SP in configuration"); | ||
| 2028 | $req->env->{llng_saml_spconfkey} = $spConfKey; | ||
| 2029 | |||
| 2030 | # Load Session and Identity if they exist | ||
| 2031 | my ( $session, $session_index, $identity, $local_session_id ); | ||
| 2032 | |||
| 2033 | eval { $session_index = $logout->request()->SessionIndex; }; | ||
| 2034 | |||
| 2035 | # SLO requests without session index can be accepted | ||
| 2036 | unless ( defined $session_index ) { | ||
| 2037 | $self->logger->warn( | ||
| 2038 | "No session index in SLO request from $spConfKey SP"); | ||
| 2039 | } | ||
| 2040 | |||
| 2041 | if ($session_index) { | ||
| 2042 | my $sessionIndexSession = $self->getSamlSession($session_index); | ||
| 2043 | return $self->p->doPE( $req, PE_SESSIONEXPIRED ) | ||
| 2044 | unless $sessionIndexSession; | ||
| 2045 | |||
| 2046 | $local_session_id = $sessionIndexSession->data->{_saml_id}; | ||
| 2047 | |||
| 2048 | $sessionIndexSession->remove; | ||
| 2049 | |||
| 2050 | $self->logger->debug( "Get session id $local_session_id" | ||
| 2051 | . " (from session index $session_index)" ); | ||
| 2052 | } | ||
| 2053 | else { | ||
| 2054 | $local_session_id = $req->id; | ||
| 2055 | $self->logger->debug( | ||
| 2056 | "Get session id $local_session_id (from cookie)"); | ||
| 2057 | } | ||
| 2058 | if ( $req->{sessionInfo} ) { | ||
| 2059 | $session = $req->{sessionInfo}->{ $self->lsDump }; | ||
| 2060 | $identity = $req->{sessionInfo}->{ $self->liDump }; | ||
| 2061 | } | ||
| 2062 | unless ($session) { | ||
| 2063 | |||
| 2064 | # Open local session | ||
| 2065 | my $local_session = $self->p->getApacheSession($local_session_id); | ||
| 2066 | |||
| 2067 | unless ($local_session) { | ||
| 2068 | $self->logger->warn("No local session found"); | ||
| 2069 | return $self->sendSLOErrorResponse( $req, $logout, $method ); | ||
| 2070 | } | ||
| 2071 | |||
| 2072 | # Load Session and Identity if they exist | ||
| 2073 | $session = $local_session->data->{ $self->lsDump }; | ||
| 2074 | $identity = $local_session->data->{ $self->liDump }; | ||
| 2075 | |||
| 2076 | # Import user data in $req (for other "logout" subs) | ||
| 2077 | $req->id( $local_session->data->{_session_id} ); | ||
| 2078 | $req->sessionInfo( $local_session->data ); | ||
| 2079 | $req->user( $local_session->data->{ $self->conf->{whatToTrace} } ); | ||
| 2080 | } | ||
| 2081 | |||
| 2082 | if ($session) { | ||
| 2083 | unless ( $self->setSessionFromDump( $logout, $session ) ) { | ||
| 2084 | return $self->p->sendError( $req, | ||
| 2085 | "Unable to load Lasso Session", 400 ); | ||
| 2086 | } | ||
| 2087 | $self->logger->debug("Lasso Session loaded"); | ||
| 2088 | } | ||
| 2089 | |||
| 2090 | if ($identity) { | ||
| 2091 | unless ( $self->setIdentityFromDump( $logout, $identity ) ) { | ||
| 2092 | return $self->p->sendError( $req, | ||
| 2093 | "Unable to load Lasso Identity", 400 ); | ||
| 2094 | } | ||
| 2095 | $self->logger->debug("Lasso Identity loaded"); | ||
| 2096 | } | ||
| 2097 | |||
| 2098 | # Do we check signature? | ||
| 2099 | my $checkSLOMessageSignature = | ||
| 2100 | $self->spOptions->{$sp} | ||
| 2101 | ->{samlSPMetaDataOptionsCheckSLOMessageSignature}; | ||
| 2102 | |||
| 2103 | if ($checkSLOMessageSignature) { | ||
| 2104 | |||
| 2105 | $self->forceSignatureVerification($logout); | ||
| 2106 | |||
| 2107 | unless ( $self->processLogoutRequestMsg( $logout, $request ) ) { | ||
| 2108 | return $self->p->sendError( $req, "Signature is not valid", | ||
| 2109 | 400 ); | ||
| 2110 | } | ||
| 2111 | else { | ||
| 2112 | $self->logger->debug("Signature is valid"); | ||
| 2113 | } | ||
| 2114 | } | ||
| 2115 | else { | ||
| 2116 | $self->logger->debug("Message signature will not be checked"); | ||
| 2117 | } | ||
| 2118 | |||
| 2119 | # Check Destination | ||
| 2120 | return $self->sendSLOErrorResponse( $req, $logout, $method ) | ||
| 2121 | unless ( $self->checkDestination( $logout->request, $url ) ); | ||
| 2122 | |||
| 2123 | # Validate request if no previous error | ||
| 2124 | unless ( $self->validateLogoutRequest($logout) ) { | ||
| 2125 | return $self->p->sendError( $req, "SLO request is not valid", 400 ); | ||
| 2126 | } | ||
| 2127 | |||
| 2128 | # Set RelayState | ||
| 2129 | if ($relaystate) { | ||
| 2130 | $logout->msg_relayState($relaystate); | ||
| 2131 | $self->logger->debug("Set $relaystate in RelayState"); | ||
| 2132 | } | ||
| 2133 | |||
| 2134 | my $sloInfos; | ||
| 2135 | $sloInfos->{type} = 'sloStatus'; | ||
| 2136 | $sloInfos->{_utime} = time; | ||
| 2137 | $sloInfos->{_logout} = $logout->dump; | ||
| 2138 | $sloInfos->{_session} = | ||
| 2139 | $logout->get_session() ? $logout->get_session()->dump : ""; | ||
| 2140 | $sloInfos->{_method} = $method; | ||
| 2141 | |||
| 2142 | # Create SLO status session and get ID | ||
| 2143 | my $sloStatusSessionInfo = $self->getSamlSession( undef, $sloInfos ); | ||
| 2144 | my $relayID = $sloStatusSessionInfo->id; | ||
| 2145 | |||
| 2146 | $self->logger->debug("Create relay session $relayID"); | ||
| 2147 | |||
| 2148 | # Prepare logout on all others SP | ||
| 2149 | my $provider_nb = | ||
| 2150 | $self->sendLogoutRequestToProviders( $req, $logout, $relayID ); | ||
| 2151 | |||
| 2152 | # Close SAML sessions | ||
| 2153 | unless ( $self->deleteSAMLSecondarySessions($local_session_id) ) { | ||
| 2154 | $self->logger->error("Fail to delete SAML sessions"); | ||
| 2155 | } | ||
| 2156 | |||
| 2157 | # Close local session | ||
| 2158 | # This flag is for logout() to say that SAML logout is already done | ||
| 2159 | $req->data->{samlSLOCalled} = 1; | ||
| 2160 | |||
| 2161 | # This variable decides if we call the authLogout step | ||
| 2162 | # (which can redirect away) | ||
| 2163 | my $doAuthLogout = 0; | ||
| 2164 | my $logoutContextSession; | ||
| 2165 | |||
| 2166 | # TODO: only propagation to SAML or OIDC issuers is supported | ||
| 2167 | # at the moment | ||
| 2168 | if ( $req->sessionInfo->{_lassoSessionDump} | ||
| 2169 | or $req->sessionInfo->{_oidc_OP} ) | ||
| 2170 | { | ||
| 2171 | |||
| 2172 | $doAuthLogout = 1; | ||
| 2173 | |||
| 2174 | # In case we have to redirect to the IDP, save current state | ||
| 2175 | # to allow resumption. This needs to be done here because | ||
| 2176 | # issuerNextUrl will be put in the IdP logout's RelayState | ||
| 2177 | my $logoutInfos = { | ||
| 2178 | logout => $logout->dump, | ||
| 2179 | spConfKey => $spConfKey, | ||
| 2180 | method => $method, | ||
| 2181 | provider_nb => $provider_nb, | ||
| 2182 | relayID => $relayID, | ||
| 2183 | _utime => time(), | ||
| 2184 | }; | ||
| 2185 | $logoutContextSession = | ||
| 2186 | $self->getSamlSession( undef, $logoutInfos ); | ||
| 2187 | my $uri = URI->new( | ||
| 2188 | $self->p->buildUrl( | ||
| 2189 | $req->portal, 'saml', 'singleLogoutResume' | ||
| 2190 | ) | ||
| 2191 | ); | ||
| 2192 | $uri->query_param( ResumeParams => $logoutContextSession->id ); | ||
| 2193 | $req->{issuerNextUrl} = $uri->as_string; | ||
| 2194 | } | ||
| 2195 | |||
| 2196 | # We don't want info to interfere with the auth logout process | ||
| 2197 | my $savedInfo = $req->info; | ||
| 2198 | $req->setInfo(''); | ||
| 2199 | |||
| 2200 | # Launch normal logout and ignore errors | ||
| 2201 | my $tmp = $req->data->{nofail}; | ||
| 2202 | $req->data->{nofail} = 1; | ||
| 2203 | $req->steps( [ | ||
| 2204 | @{ $self->p->beforeLogout }, | ||
| 2205 | ( $doAuthLogout ? 'authLogout' : () ), | ||
| 2206 | 'deleteSession' | ||
| 2207 | ] | ||
| 2208 | ); | ||
| 2209 | my $res = $self->p->process($req); | ||
| 2210 | $req->data->{nofail} = $tmp; | ||
| 2211 | |||
| 2212 | if ( $res eq PE_REDIRECT ) { | ||
| 2213 | |||
| 2214 | # Save session info (for logout of other SP) | ||
| 2215 | if ($savedInfo) { | ||
| 2216 | $logoutContextSession->update( { info => $savedInfo } ); | ||
| 2217 | } | ||
| 2218 | return $self->p->doPE( $req, PE_REDIRECT ); | ||
| 2219 | } | ||
| 2220 | |||
| 2221 | $req->info($savedInfo); | ||
| 2222 | |||
| 2223 | return $self->_finishSlo( $req, $logout, $method, $spConfKey, | ||
| 2224 | $provider_nb, $relayID ); | ||
| 2225 | } | ||
| 2226 | |||
| 2227 | elsif ($response) { | ||
| 2228 | |||
| 2229 | # Process logout response | ||
| 2230 | my $result = $self->processLogoutResponseMsg( $logout, $response ); | ||
| 2231 | |||
| 2232 | unless ($result) { | ||
| 2233 | $self->logger->error("Fail to process logout response"); | ||
| 2234 | $self->imgnok($req); | ||
| 2235 | } | ||
| 2236 | |||
| 2237 | $self->logger->debug("Logout response is valid"); | ||
| 2238 | |||
| 2239 | my $h = $self->p->processHook( $req, 'samlGotLogoutResponse', $logout ); | ||
| 2240 | $self->imgnok($req) if ( $h != PE_OK ); | ||
| 2241 | |||
| 2242 | # Check Destination | ||
| 2243 | $self->imgnok($req) | ||
| 2244 | unless ( $self->checkDestination( $logout->response, $url ) ); | ||
| 2245 | |||
| 2246 | # Get SP entityID | ||
| 2247 | my $sp = $logout->remote_providerID(); | ||
| 2248 | |||
| 2249 | $self->logger->debug("Found entityID $sp in SAML message"); | ||
| 2250 | |||
| 2251 | # SP conf key | ||
| 2252 | my $spConfKey = $self->spList->{$sp}->{confKey}; | ||
| 2253 | |||
| 2254 | unless ($spConfKey) { | ||
| 2255 | $self->logger->error("$sp do not match any SP in configuration"); | ||
| 2256 | $self->imgnok($req); | ||
| 2257 | } | ||
| 2258 | |||
| 2259 | $self->logger->debug("$sp match $spConfKey SP in configuration"); | ||
| 2260 | |||
| 2261 | # Do we check signature? | ||
| 2262 | my $checkSLOMessageSignature = | ||
| 2263 | $self->spOptions->{$sp} | ||
| 2264 | ->{samlSPMetaDataOptionsCheckSLOMessageSignature}; | ||
| 2265 | |||
| 2266 | if ($checkSLOMessageSignature) { | ||
| 2267 | unless ( $self->checkSignatureStatus($logout) ) { | ||
| 2268 | $self->logger->error( "Could not verify signature of" | ||
| 2269 | . " incoming SLO request from $spConfKey" ); | ||
| 2270 | $self->imgnok($req); | ||
| 2271 | } | ||
| 2272 | else { | ||
| 2273 | $self->logger->debug("Signature is valid"); | ||
| 2274 | } | ||
| 2275 | } | ||
| 2276 | else { | ||
| 2277 | $self->logger->debug("Message signature will not be checked"); | ||
| 2278 | } | ||
| 2279 | |||
| 2280 | # Store success status for this SLO request | ||
| 2281 | if ($relaystate) { | ||
| 2282 | my $sloStatusSessionInfos = $self->getSamlSession($relaystate); | ||
| 2283 | |||
| 2284 | if ($sloStatusSessionInfos) { | ||
| 2285 | $sloStatusSessionInfos->update( { $spConfKey => 1 } ); | ||
| 2286 | $self->logger->debug( | ||
| 2287 | "Store SLO status for $spConfKey in session $relaystate"); | ||
| 2288 | } | ||
| 2289 | else { | ||
| 2290 | $self->logger->warn( "Unable to store SLO status for" | ||
| 2291 | . " $spConfKey in session $relaystate" ); | ||
| 2292 | } | ||
| 2293 | } | ||
| 2294 | else { | ||
| 2295 | $self->logger->warn( "Unable to store SLO status for" | ||
| 2296 | . " $spConfKey because there is no RelayState" ); | ||
| 2297 | } | ||
| 2298 | |||
| 2299 | # SLO response is OK | ||
| 2300 | $self->logger->debug("Display OK status for SLO on $spConfKey"); | ||
| 2301 | $self->imgok($req); | ||
| 2302 | } | ||
| 2303 | |||
| 2304 | else { | ||
| 2305 | |||
| 2306 | # No request or response | ||
| 2307 | # This should not happen | ||
| 2308 | return $self->p->sendError( $req, "No request or response found", 400 ); | ||
| 2309 | } | ||
| 2310 | } | ||
| 2311 | |||
| 2312 | sub attributeServer { | ||
| 2313 | my ( $self, $req, ) = @_; | ||
| 2314 | my $url = $req->uri; | ||
| 2315 | $self->logger->debug("URL $url detected as an attribute service URL"); | ||
| 2316 | |||
| 2317 | # Attribute request are sent with SOAP trough POST | ||
| 2318 | my $att_request = $req->content; | ||
| 2319 | my $att_response; | ||
| 2320 | |||
| 2321 | # Process request | ||
| 2322 | my $query = | ||
| 2323 | $self->processAttributeRequest( $self->lassoServer, $att_request ); | ||
| 2324 | unless ($query) { | ||
| 2325 | return $self->p->sendError( $req, | ||
| 2326 | "Unable to process attribute request", 400 ); | ||
| 2327 | } | ||
| 2328 | |||
| 2329 | # Get SP entityID | ||
| 2330 | my $sp = $query->remote_providerID(); | ||
| 2331 | |||
| 2332 | $self->logger->debug("Found entityID $sp in SAML message"); | ||
| 2333 | |||
| 2334 | # SP conf key | ||
| 2335 | my $spConfKey = $self->spList->{$sp}->{confKey}; | ||
| 2336 | |||
| 2337 | unless ($spConfKey) { | ||
| 2338 | return $self->p->sendError( $req, | ||
| 2339 | "$sp do not match any SP in configuration", 400 ); | ||
| 2340 | } | ||
| 2341 | |||
| 2342 | $self->logger->debug("$sp match $spConfKey SP in configuration"); | ||
| 2343 | |||
| 2344 | # Check Destination | ||
| 2345 | unless ( $self->checkDestination( $query->request, $url ) ) { | ||
| 2346 | return $self->p->sendError( $req, "Bad destination $url", 400 ); | ||
| 2347 | } | ||
| 2348 | |||
| 2349 | # Validate request | ||
| 2350 | unless ( $self->validateAttributeRequest($query) ) { | ||
| 2351 | return $self->p->sendError( $req, "Attribute request not valid", 400 ); | ||
| 2352 | } | ||
| 2353 | |||
| 2354 | # Get NameID | ||
| 2355 | my $name_id = $query->nameIdentifier(); | ||
| 2356 | |||
| 2357 | unless ($name_id) { | ||
| 2358 | $self->p->sendError( $req, | ||
| 2359 | "Fail to get NameID from attribute request", 400 ); | ||
| 2360 | } | ||
| 2361 | |||
| 2362 | my $user = $name_id->content(); | ||
| 2363 | |||
| 2364 | # Get sessionInfo for the given NameID | ||
| 2365 | my $sessionInfo; | ||
| 2366 | |||
| 2367 | my $saml_sessions = | ||
| 2368 | Lemonldap::NG::Common::Apache::Session->searchOn( $self->amOpts, | ||
| 2369 | "_nameID", $name_id->dump ); | ||
| 2370 | |||
| 2371 | if ( | ||
| 2372 | my @saml_sessions_keys = | ||
| 2373 | grep { $saml_sessions->{$_}->{_session_kind} eq $self->sessionKind } | ||
| 2374 | keys %$saml_sessions | ||
| 2375 | ) | ||
| 2376 | { | ||
| 2377 | |||
| 2378 | # Warning if more than one session found | ||
| 2379 | if ( $#saml_sessions_keys > 0 ) { | ||
| 2380 | $self->logger->warn( | ||
| 2381 | "More than one SAML session found for user $user"); | ||
| 2382 | } | ||
| 2383 | |||
| 2384 | # Take the first session | ||
| 2385 | my $saml_session = shift @saml_sessions_keys; | ||
| 2386 | |||
| 2387 | # Get session | ||
| 2388 | $self->logger->debug( | ||
| 2389 | "Retrieve SAML session $saml_session for user $user"); | ||
| 2390 | |||
| 2391 | my $samlSessionInfo = $self->getSamlSession($saml_session); | ||
| 2392 | |||
| 2393 | # Get real session | ||
| 2394 | my $real_session = $samlSessionInfo->data->{_saml_id}; | ||
| 2395 | |||
| 2396 | $self->logger->debug( | ||
| 2397 | "Retrieve real session $real_session for user $user"); | ||
| 2398 | |||
| 2399 | $sessionInfo = $self->p->getApacheSession($real_session); | ||
| 2400 | |||
| 2401 | unless ($sessionInfo) { | ||
| 2402 | return $self->p->sendError( $req, | ||
| 2403 | "Cannot get session $real_session", 500 ); | ||
| 2404 | } | ||
| 2405 | |||
| 2406 | } | ||
| 2407 | else { | ||
| 2408 | return $self->p->sendError( $req, | ||
| 2409 | "No SAML session found for user $user", 400 ); | ||
| 2410 | } | ||
| 2411 | |||
| 2412 | # Get requested attributes | ||
| 2413 | my @requested_attributes; | ||
| 2414 | eval { @requested_attributes = $query->request()->Attribute(); }; | ||
| 2415 | if ($@) { | ||
| 2416 | $self->checkLassoError($@); | ||
| 2417 | return $self->p->sendError( $req, | ||
| 2418 | "Unable to get requested attributes", 400 ); | ||
| 2419 | } | ||
| 2420 | |||
| 2421 | # Returned attributes | ||
| 2422 | my @returned_attributes; | ||
| 2423 | |||
| 2424 | # Browse SP authorized attributes | ||
| 2425 | foreach ( keys %{ $self->spAttributes->{$sp} } ) { | ||
| 2426 | my $sp_attr = $_; | ||
| 2427 | |||
| 2428 | # Extract fields from exportedAttr value | ||
| 2429 | my ( $mandatory, $name, $format, $friendly_name ) = | ||
| 2430 | split( /;/, $self->spAttributes->{$sp}->{$sp_attr} ); | ||
| 2431 | |||
| 2432 | foreach (@requested_attributes) { | ||
| 2433 | my $req_attr = $_; | ||
| 2434 | my $rname = $req_attr->Name(); | ||
| 2435 | my $rformat = $req_attr->NameFormat(); | ||
| 2436 | my $rfriendly_name = $req_attr->FriendlyName(); | ||
| 2437 | |||
| 2438 | # Skip if name does not match | ||
| 2439 | next unless ( $rname =~ /^$name$/ ); | ||
| 2440 | |||
| 2441 | # Check format and friendly name | ||
| 2442 | next if ( $rformat and $rformat !~ /^$format$/ ); | ||
| 2443 | next | ||
| 2444 | if ( $rfriendly_name | ||
| 2445 | and $rfriendly_name !~ /^$friendly_name$/ ); | ||
| 2446 | |||
| 2447 | $self->logger->debug( | ||
| 2448 | "SP $spConfKey is authorized to access attribute $rname"); | ||
| 2449 | |||
| 2450 | $self->logger->debug( | ||
| 2451 | "Attribute $rname is linked to $sp_attr session key"); | ||
| 2452 | |||
| 2453 | # Check if values are given | ||
| 2454 | my $rvalue = | ||
| 2455 | $self->getAttributeValue( $rname, $rformat, $rfriendly_name, | ||
| 2456 | [$req_attr] ); | ||
| 2457 | |||
| 2458 | $self->logger->debug( | ||
| 2459 | "Some values are explicitly requested: $rvalue") | ||
| 2460 | if defined $rvalue; | ||
| 2461 | |||
| 2462 | # Get session value | ||
| 2463 | if ( $sessionInfo->data->{$sp_attr} ) { | ||
| 2464 | |||
| 2465 | my @values = split $self->conf->{multiValuesSeparator}, | ||
| 2466 | $sessionInfo->data->{$sp_attr}; | ||
| 2467 | my @saml2values; | ||
| 2468 | |||
| 2469 | # SAML2 attribute | ||
| 2470 | my $ret_attr = | ||
| 2471 | $self->createAttribute( $rname, $rformat, $rfriendly_name ); | ||
| 2472 | |||
| 2473 | unless ($ret_attr) { | ||
| 2474 | return $self->p->sendError( $req, | ||
| 2475 | "Unable to create a new SAML attribute", 500 ); | ||
| 2476 | } | ||
| 2477 | |||
| 2478 | foreach (@values) { | ||
| 2479 | |||
| 2480 | my $local_value = $_; | ||
| 2481 | |||
| 2482 | # Check if values were set in requested attribute | ||
| 2483 | # In this case, only requested values can be returned | ||
| 2484 | if ( | ||
| 2485 | $rvalue | ||
| 2486 | and !map( /^$local_value$/, | ||
| 2487 | split( | ||
| 2488 | $self->conf->{multiValuesSeparator}, $rvalue | ||
| 2489 | ) ) | ||
| 2490 | ) | ||
| 2491 | { | ||
| 2492 | $self->logger->warn( "$local_value value is not in" | ||
| 2493 | . " requested values, it will not be sent" ); | ||
| 2494 | next; | ||
| 2495 | } | ||
| 2496 | |||
| 2497 | # SAML2 attribute value | ||
| 2498 | my $saml2value = $self->createAttributeValue( $local_value, | ||
| 2499 | $self->spOptions->{$sp} | ||
| 2500 | ->{samlSPMetaDataOptionsForceUTF8} ); | ||
| 2501 | |||
| 2502 | unless ($saml2value) { | ||
| 2503 | return $self->p->sendError( $req, | ||
| 2504 | "Unable to create a new SAML attribute value", | ||
| 2505 | 400 ); | ||
| 2506 | } | ||
| 2507 | |||
| 2508 | push @saml2values, $saml2value; | ||
| 2509 | |||
| 2510 | $self->logger->debug( | ||
| 2511 | "Push $local_value in SAML attribute $name"); | ||
| 2512 | |||
| 2513 | } | ||
| 2514 | |||
| 2515 | $ret_attr->AttributeValue(@saml2values); | ||
| 2516 | |||
| 2517 | # Push attribute in attribute list | ||
| 2518 | push @returned_attributes, $ret_attr; | ||
| 2519 | |||
| 2520 | } | ||
| 2521 | else { | ||
| 2522 | $self->logger->debug("No session value for $sp_attr"); | ||
| 2523 | } | ||
| 2524 | |||
| 2525 | } | ||
| 2526 | |||
| 2527 | } | ||
| 2528 | |||
| 2529 | # Create attribute statement | ||
| 2530 | if ( scalar @returned_attributes ) { | ||
| 2531 | my $attribute_statement; | ||
| 2532 | |||
| 2533 | eval { $attribute_statement = Lasso::Saml2AttributeStatement->new(); }; | ||
| 2534 | if ($@) { | ||
| 2535 | $self->checkLassoError($@); | ||
| 2536 | return $self->p->sendError( $req, 'An error occurs, see IdP logs', | ||
| 2537 | 500 ); | ||
| 2538 | } | ||
| 2539 | |||
| 2540 | # Register attributes in attribute statement | ||
| 2541 | $attribute_statement->Attribute(@returned_attributes); | ||
| 2542 | |||
| 2543 | # Create assetion | ||
| 2544 | my $assertion; | ||
| 2545 | |||
| 2546 | eval { $assertion = Lasso::Saml2Assertion->new(); }; | ||
| 2547 | if ($@) { | ||
| 2548 | $self->checkLassoError($@); | ||
| 2549 | return $self->p->sendError( $req, 'An error occurs, see IdP logs', | ||
| 2550 | 500 ); | ||
| 2551 | } | ||
| 2552 | |||
| 2553 | # Add attribute statement in response assertion | ||
| 2554 | my @attributes_statement = ($attribute_statement); | ||
| 2555 | $assertion->AttributeStatement(@attributes_statement); | ||
| 2556 | |||
| 2557 | # Set response assertion | ||
| 2558 | $query->response->Assertion( ($assertion) ); | ||
| 2559 | } | ||
| 2560 | |||
| 2561 | # Build response | ||
| 2562 | $att_response = $self->buildAttributeResponse($query); | ||
| 2563 | |||
| 2564 | unless ($att_response) { | ||
| 2565 | $self->p->sendError( $req, "Unable to build attribute response", 500 ); | ||
| 2566 | } | ||
| 2567 | |||
| 2568 | return [ | ||
| 2569 | 200, | ||
| 2570 | [ | ||
| 2571 | 'Content-Type' => 'text/xml', | ||
| 2572 | 'Content-Length' => length($att_response) | ||
| 2573 | ], | ||
| 2574 | [$att_response] | ||
| 2575 | ]; | ||
| 2576 | } | ||
| 2577 | |||
| 2578 | # LEGACY METHODS | ||
| 2579 | # Remove in 3.0 | ||
| 2580 | |||
| 2581 | sub imgok { | ||
| 2582 | my $self = shift; | ||
| 2583 | return $self->p->imgok(@_); | ||
| 2584 | } | ||
| 2585 | |||
| 2586 | sub imgnok { | ||
| 2587 | my $self = shift; | ||
| 2588 | return $self->p->imgnok(@_); | ||
| 2589 | } | ||
| 2590 | |||
| 2591 | sub sendImage { | ||
| 2592 | my $self = shift; | ||
| 2593 | return $self->p->sendImage(@_); | ||
| 2594 | } | ||
| 2595 | |||
| 2596 | # INTERNAL METHODS | ||
| 2597 | # Normalize url to be tolerant to SAML Path | ||
| 2598 | # Usefull if SAML Path is a regex | ||
| 2599 | # @return normalized url | ||
| 2600 | sub normalize_url { | ||
| 2601 | my ( $self, $url, $samlPath, $metadataUrl ) = @_; | ||
| 2602 | |||
| 2603 | my $initialPath = ""; | ||
| 2604 | my $finalPath = ""; | ||
| 2605 | |||
| 2606 | # Get current (bad) path | ||
| 2607 | if ( $url =~ m/($samlPath)/ ) { | ||
| 2608 | $initialPath = $1; | ||
| 2609 | } | ||
| 2610 | |||
| 2611 | # Get destination (good) path | ||
| 2612 | if ( $metadataUrl =~ m/($samlPath)/ ) { | ||
| 2613 | $finalPath = $1; | ||
| 2614 | } | ||
| 2615 | |||
| 2616 | if ( $initialPath ne "" | ||
| 2617 | and $finalPath ne "" | ||
| 2618 | and $initialPath ne $finalPath ) | ||
| 2619 | { | ||
| 2620 | $self->logger->debug( | ||
| 2621 | "Normalizing url path form $initialPath to $finalPath"); | ||
| 2622 | $url =~ s/$initialPath/$finalPath/; | ||
| 2623 | } | ||
| 2624 | |||
| 2625 | return $url; | ||
| 2626 | } | ||
| 2627 | |||
| 2628 | 1; | ||