355 String statusMessage = responseType.getStatus() == null ? Messages.IDENTITY_PROVIDER_UNEXPECTED_ERROR : responseType.getStatus().getStatusMessage();
356 return callback.error(relayState, statusMessage);
358 if (responseType.getAssertions() == null || responseType.getAssertions().isEmpty()) {
359 return callback.error(relayState, Messages.IDENTITY_PROVIDER_UNEXPECTED_ERROR);
362 boolean assertionIsEncrypted = AssertionUtil.isAssertionEncrypted(responseType);
365 logger.error(
"The assertion is not encrypted, which is required.");
366 event.event(EventType.IDENTITY_PROVIDER_RESPONSE);
367 event.error(Errors.INVALID_SAML_RESPONSE);
368 return ErrorPage.error(
session, null, Response.Status.BAD_REQUEST, Messages.INVALID_REQUESTER);
371 Element assertionElement;
373 if (assertionIsEncrypted) {
375 assertionElement = AssertionUtil.decryptAssertion(holder, responseType, keys.getPrivateKey());
379 assertionElement = DocumentUtil.getElement(holder.getSamlDocument(),
new QName(JBossSAMLConstants.ASSERTION.get()));
382 boolean signed = AssertionUtil.isSignedElement(assertionElement);
385 logger.error(
"validation failed");
386 event.event(EventType.IDENTITY_PROVIDER_RESPONSE);
387 event.error(Errors.INVALID_SIGNATURE);
388 return ErrorPage.error(
session, null, Response.Status.BAD_REQUEST, Messages.INVALID_REQUESTER);
391 AssertionType assertion = responseType.getAssertions().get(0).getAssertion();
393 SubjectType subject = assertion.getSubject();
394 SubjectType.STSubType subType = subject.getSubType();
395 NameIDType subjectNameID = (NameIDType) subType.getBaseID();
397 BrokeredIdentityContext identity =
new BrokeredIdentityContext(subjectNameID.getValue());
400 if (clientId != null && ! clientId.trim().isEmpty()) {
404 identity.setUsername(subjectNameID.getValue());
407 if (subjectNameID.getFormat() != null && subjectNameID.getFormat().toString().equals(JBossSAMLURIConstants.NAMEID_FORMAT_EMAIL.get())) {
408 identity.setEmail(subjectNameID.getValue());
412 identity.setToken(samlResponse);
415 AuthnStatementType authn = null;
416 for (Object statement : assertion.getStatements()) {
417 if (statement instanceof AuthnStatementType) {
418 authn = (AuthnStatementType)statement;
423 if (assertion.getAttributeStatements() != null ) {
424 for (AttributeStatementType attrStatement : assertion.getAttributeStatements()) {
425 for (AttributeStatementType.ASTChoiceType choice : attrStatement.getAttributes()) {
426 AttributeType attribute = choice.getAttribute();
427 if (X500SAMLProfileConstants.EMAIL.getFriendlyName().equals(attribute.getFriendlyName())
428 || X500SAMLProfileConstants.EMAIL.get().equals(attribute.getName())) {
429 if (!attribute.getAttributeValue().isEmpty()) identity.setEmail(attribute.getAttributeValue().get(0).toString());
436 String brokerUserId =
config.
getAlias() +
"." + subjectNameID.getValue();
437 identity.setBrokerUserId(brokerUserId);
438 identity.setIdpConfig(
config);
440 if (authn != null && authn.getSessionIndex() != null) {
441 identity.setBrokerSessionId(identity.getBrokerUserId() +
"." + authn.getSessionIndex());
443 identity.setCode(relayState);
446 return callback.authenticated(identity);
447 }
catch (WebApplicationException e) {
448 return e.getResponse();
449 }
catch (Exception e) {
450 throw new IdentityBrokerException(
"Could not process response from SAML identity provider.", e);
String getAlias()
Definition: IdentityProviderModel.java:100
ActiveRsaKey getActiveRsaKey(RealmModel realm)
boolean isSuccessfulSamlResponse(ResponseType responseType)
Definition: SAMLEndpoint.java:455
KeycloakSession session
Definition: SAMLEndpoint.java:120
static final Logger logger
Definition: SAMLEndpoint.java:104
static final String SAML_LOGIN_RESPONSE
Definition: SAMLEndpoint.java:108
KeyLocator getIDPKeyLocator()
Definition: SAMLEndpoint.java:219
static final String SAML_IDP_INITIATED_CLIENT_ID
Definition: SAMLEndpoint.java:110
RealmModel realm
Definition: SAMLEndpoint.java:112
boolean isWantAssertionsSigned()
Definition: SAMLIdentityProviderConfig.java:140
IdentityProvider.AuthenticationCallback callback
Definition: SAMLEndpoint.java:115
boolean isWantAssertionsEncrypted()
Definition: SAMLIdentityProviderConfig.java:148
static final String SAML_AUTHN_STATEMENT
Definition: SAMLEndpoint.java:111
static final String SAML_ASSERTION
Definition: SAMLEndpoint.java:109
SAMLIdentityProvider provider
Definition: SAMLEndpoint.java:116
SAMLIdentityProviderConfig config
Definition: SAMLEndpoint.java:114
boolean isStoreToken()
Definition: IdentityProviderModel.java:124
boolean isValidateSignature()
Definition: SAMLIdentityProviderConfig.java:71