371 AuthenticatedClientSessionModel clientSession = clientSessionCtx.getClientSession();
372 ClientModel client = clientSession.getClient();
373 SamlClient samlClient =
new SamlClient(client);
375 String relayState = clientSession.getNote(GeneralConstants.RELAY_STATE);
376 String redirectUri = clientSession.getRedirectUri();
379 String nameId =
getNameId(nameIdFormat, clientSession, userSession);
381 if (nameId == null) {
384 redirectUri, JBossSAMLURIConstants.STATUS_INVALID_NAMEIDPOLICY, relayState
393 SAML2LoginResponseBuilder builder =
new SAML2LoginResponseBuilder();
395 .requestIssuer(clientSession.getClient().getClientId()).nameIdentifier(nameIdFormat, nameId).authMethod(JBossSAMLURIConstants.AC_UNSPECIFIED.get());
397 String sessionIndex = SamlSessionUtils.getSessionIndex(clientSession);
398 builder.sessionIndex(sessionIndex);
400 if (!samlClient.includeAuthnStatement()) {
401 builder.disableAuthnStatement(
true);
404 builder.includeOneTimeUseCondition(samlClient.includeOneTimeUseCondition());
406 List<ProtocolMapperProcessor<SAMLAttributeStatementMapper>> attributeStatementMappers =
new LinkedList<>();
407 List<ProtocolMapperProcessor<SAMLLoginResponseMapper>> loginResponseMappers =
new LinkedList<>();
408 ProtocolMapperProcessor<SAMLRoleListMapper> roleListMapper = null;
410 Set<ProtocolMapperModel> mappings = clientSessionCtx.getProtocolMappers();
411 for (ProtocolMapperModel mapping : mappings) {
416 if (mapper instanceof SAMLAttributeStatementMapper) {
417 attributeStatementMappers.add(
new ProtocolMapperProcessor<SAMLAttributeStatementMapper>((SAMLAttributeStatementMapper) mapper, mapping));
419 if (mapper instanceof SAMLLoginResponseMapper) {
420 loginResponseMappers.add(
new ProtocolMapperProcessor<SAMLLoginResponseMapper>((SAMLLoginResponseMapper) mapper, mapping));
422 if (mapper instanceof SAMLRoleListMapper) {
423 roleListMapper =
new ProtocolMapperProcessor<SAMLRoleListMapper>((SAMLRoleListMapper) mapper, mapping);
427 Document samlDocument = null;
431 String keyName = samlClient.getXmlSigKeyInfoKeyNameTransformer().getKeyName(keys.getKid(), keys.getCertificate());
434 if ((! postBinding) && samlClient.requiresRealmSignature() && samlClient.addExtensionsElementWithKeyInfo()) {
435 builder.addExtension(
new KeycloakKeySamlExtensionGenerator(keyName));
438 ResponseType samlModel = builder.buildModel();
443 if (attributeStatement.getAttributes().size() > 0) {
444 AssertionType assertion = samlModel.getAssertions().get(0).getAssertion();
445 assertion.addStatement(attributeStatement);
449 samlDocument = builder.buildDocument(samlModel);
450 }
catch (Exception e) {
451 logger.error(
"failed", e);
452 return ErrorPage.error(
session, null, Response.Status.BAD_REQUEST, Messages.FAILED_TO_PROCESS_RESPONSE);
455 JaxrsSAML2BindingBuilder bindingBuilder =
new JaxrsSAML2BindingBuilder();
456 bindingBuilder.relayState(relayState);
458 if (samlClient.requiresRealmSignature()) {
459 String canonicalization = samlClient.getCanonicalizationMethod();
460 if (canonicalization != null) {
461 bindingBuilder.canonicalizationMethod(canonicalization);
463 bindingBuilder.signatureAlgorithm(samlClient.getSignatureAlgorithm()).signWith(keyName, keys.getPrivateKey(), keys.getPublicKey(), keys.getCertificate()).signDocument();
465 if (samlClient.requiresAssertionSignature()) {
466 String canonicalization = samlClient.getCanonicalizationMethod();
467 if (canonicalization != null) {
468 bindingBuilder.canonicalizationMethod(canonicalization);
470 bindingBuilder.signatureAlgorithm(samlClient.getSignatureAlgorithm()).signWith(keyName, keys.getPrivateKey(), keys.getPublicKey(), keys.getCertificate()).signAssertions();
472 if (samlClient.requiresEncryption()) {
473 PublicKey publicKey = null;
475 publicKey = SamlProtocolUtils.getEncryptionKey(client);
476 }
catch (Exception e) {
477 logger.error(
"failed", e);
478 return ErrorPage.error(
session, null, Response.Status.BAD_REQUEST, Messages.FAILED_TO_PROCESS_RESPONSE);
480 bindingBuilder.encrypt(publicKey);
484 }
catch (Exception e) {
485 logger.error(
"failed", e);
486 return ErrorPage.error(
session, null, Response.Status.BAD_REQUEST, Messages.FAILED_TO_PROCESS_RESPONSE);
String getNameId(String nameIdFormat, CommonClientSessionModel clientSession, UserSessionModel userSession)
Definition: SamlProtocol.java:312
AttributeStatementType populateAttributeStatements(List< ProtocolMapperProcessor< SAMLAttributeStatementMapper >> attributeStatementMappers, KeycloakSession session, UserSessionModel userSession, AuthenticatedClientSessionModel clientSession)
Definition: SamlProtocol.java:508
static final Logger logger
Definition: SamlProtocol.java:93
RealmModel realm
Definition: SamlProtocol.java:125
static final String SAML_NAME_ID_FORMAT
Definition: SamlProtocol.java:117
String getResponseIssuer(RealmModel realm)
Definition: SamlProtocol.java:253
KeycloakSessionFactory getKeycloakSessionFactory()
Response buildAuthenticatedResponse(AuthenticatedClientSessionModel clientSession, String redirectUri, Document samlDocument, JaxrsSAML2BindingBuilder bindingBuilder)
Definition: SamlProtocol.java:490
boolean isPostBinding(AuthenticationSessionModel authSession)
Definition: SamlProtocol.java:257
static final String SAML_NAME_ID
Definition: SamlProtocol.java:116
String getNameIdFormat(SamlClient samlClient, AuthenticatedClientSessionModel clientSession)
Definition: SamlProtocol.java:299
int getAccessCodeLifespan()
Response samlErrorMessage(AuthenticationSessionModel authSession, SamlClient samlClient, boolean isPostBinding, String destination, JBossSAMLURIConstants statusDetail, String relayState)
Definition: SamlProtocol.java:191
KeycloakSession session
Definition: SamlProtocol.java:123
Definition: KeyManager.java:75
< T extends Provider > ProviderFactory< T > getProviderFactory(Class< T > clazz)
ResponseType transformLoginResponse(List< ProtocolMapperProcessor< SAMLLoginResponseMapper >> mappers, ResponseType response, KeycloakSession session, UserSessionModel userSession, AuthenticatedClientSessionModel clientSession)
Definition: SamlProtocol.java:518
void populateRoles(ProtocolMapperProcessor< SAMLRoleListMapper > roleListMapper, KeycloakSession session, UserSessionModel userSession, ClientSessionContext clientSessionCtx, final AttributeStatementType existingAttributeStatement)
Definition: SamlProtocol.java:525
static final String SAML_REQUEST_ID
Definition: SamlProtocol.java:107
int getAccessTokenLifespan()