370 SamlClient samlClient =
new SamlClient(client);
372 if (
logoutRequest.getDestination() == null && samlClient.requiresClientSignature()) {
373 event.detail(Details.REASON,
"invalid_destination");
374 event.error(Errors.INVALID_SAML_LOGOUT_REQUEST);
375 return ErrorPage.error(
session, null, Response.Status.BAD_REQUEST, Messages.INVALID_REQUEST);
378 event.detail(Details.REASON,
"invalid_destination");
379 event.error(Errors.INVALID_SAML_LOGOUT_REQUEST);
380 return ErrorPage.error(
session, null, Response.Status.BAD_REQUEST, Messages.INVALID_REQUEST);
385 if (authResult != null) {
387 String postBindingUri = SamlProtocol.getLogoutServiceUrl(
session.
getContext().
getUri(), client, SamlProtocol.SAML_POST_BINDING);
388 if (samlClient.forcePostBinding() && postBindingUri != null && ! postBindingUri.trim().isEmpty())
389 logoutBinding = SamlProtocol.SAML_POST_BINDING;
390 boolean postBinding = Objects.equals(SamlProtocol.SAML_POST_BINDING, logoutBinding);
393 UserSessionModel userSession = authResult.getSession();
394 userSession.setNote(SamlProtocol.SAML_LOGOUT_BINDING_URI, bindingUri);
395 if (samlClient.requiresRealmSignature()) {
396 userSession.setNote(SamlProtocol.SAML_LOGOUT_SIGNATURE_ALGORITHM, samlClient.getSignatureAlgorithm().toString());
399 if (relayState != null)
400 userSession.setNote(SamlProtocol.SAML_LOGOUT_RELAY_STATE, relayState);
401 userSession.setNote(SamlProtocol.SAML_LOGOUT_REQUEST_ID,
logoutRequest.getID());
402 userSession.setNote(SamlProtocol.SAML_LOGOUT_BINDING, logoutBinding);
403 userSession.setNote(SamlProtocol.SAML_LOGOUT_ADD_EXTENSIONS_ELEMENT_WITH_KEY_INFO, Boolean.toString((!
postBinding) && samlClient.addExtensionsElementWithKeyInfo()));
404 userSession.setNote(SamlProtocol.SAML_SERVER_SIGNATURE_KEYINFO_KEY_NAME_TRANSFORMER, samlClient.getXmlSigKeyInfoKeyNameTransformer().name());
405 userSession.setNote(SamlProtocol.SAML_LOGOUT_CANONICALIZATION, samlClient.getCanonicalizationMethod());
406 userSession.setNote(AuthenticationManager.KEYCLOAK_LOGOUT_PROTOCOL, SamlProtocol.LOGIN_PROTOCOL);
408 AuthenticatedClientSessionModel clientSession = userSession.getAuthenticatedClientSessionByClient(client.getId());
409 if (clientSession != null) {
410 clientSession.setAction(AuthenticationSessionModel.Action.LOGGED_OUT.name());
412 logger.debug(
"browser Logout");
415 for (String sessionIndex :
logoutRequest.getSessionIndex()) {
417 AuthenticatedClientSessionModel clientSession = SamlSessionUtils.getClientSession(
session,
realm, sessionIndex);
418 if (clientSession == null)
420 UserSessionModel userSession = clientSession.getUserSession();
421 if (clientSession.getClient().getClientId().equals(client.getClientId())) {
423 clientSession.setAction(AuthenticationSessionModel.Action.LOGGED_OUT.name());
428 }
catch (Exception e) {
429 logger.warn(
"Failure with backchannel logout", e);
439 String logoutBindingUri = SamlProtocol.getLogoutServiceUrl(
session.
getContext().
getUri(), client, logoutBinding);
440 String logoutRelayState = relayState;
441 SAML2LogoutResponseBuilder builder =
new SAML2LogoutResponseBuilder();
443 builder.destination(logoutBindingUri);
445 JaxrsSAML2BindingBuilder binding =
new JaxrsSAML2BindingBuilder().relayState(logoutRelayState);
446 boolean postBinding = SamlProtocol.SAML_POST_BINDING.equals(logoutBinding);
447 if (samlClient.requiresRealmSignature()) {
448 SignatureAlgorithm algorithm = samlClient.getSignatureAlgorithm();
450 binding.signatureAlgorithm(algorithm).signWith(keys.getKid(), keys.getPrivateKey(), keys.getPublicKey(), keys.getCertificate()).signDocument();
451 if (! postBinding && samlClient.addExtensionsElementWithKeyInfo()) {
452 builder.addExtension(
new KeycloakKeySamlExtensionGenerator(keys.getKid()));
457 return binding.postBinding(builder.buildDocument()).response(logoutBindingUri);
459 return binding.redirectBinding(builder.buildDocument()).response(logoutBindingUri);
461 }
catch (Exception e) {
462 throw new RuntimeException(e);
AuthResult authenticateIdentityCookie(KeycloakSession session, RealmModel realm)
Definition: AuthenticationManager.java:695
ActiveRsaKey getActiveRsaKey(RealmModel realm)
KeycloakContext getContext()
Response postBinding(@FormParam(GeneralConstants.SAML_REQUEST_KEY) String samlRequest, @FormParam(GeneralConstants.SAML_RESPONSE_KEY) String samlResponse, @FormParam(GeneralConstants.RELAY_STATE) String relayState)
Definition: SamlService.java:562
static Response browserLogout(KeycloakSession session, RealmModel realm, UserSessionModel userSession, UriInfo uriInfo, ClientConnection connection, HttpHeaders headers)
Definition: AuthenticationManager.java:492
static final Logger logger
Definition: SamlService.java:99
final DestinationValidator destinationValidator
Definition: SamlService.java:101
abstract String getBindingType()
Response logoutRequest(LogoutRequestType logoutRequest, ClientModel client, String relayState)
Definition: SamlService.java:369
static void backchannelLogout(KeycloakSession session, UserSessionModel userSession, boolean logoutBroker)
Definition: AuthenticationManager.java:186
RealmModel realm
Definition: AuthorizationEndpointBase.java:60
URI getAbsolutePath()
Definition: KeycloakUriInfo.java:66
HttpHeaders headers
Definition: AuthorizationEndpointBase.java:65
ClientConnection clientConnection
Definition: AuthorizationEndpointBase.java:71
AuthenticationManager authManager
Definition: AuthorizationEndpointBase.java:62
KeycloakSession session
Definition: AuthorizationEndpointBase.java:69