public ResponseType createResponse(SamlIdpSession session, SamlEndpoint externalSamlEndpoint) {
ResponseType response = objectFactory.createResponseType();
fillStatusResponseFields(response, SamlConstants.STATUS_SUCCESS, null);
AssertionType assertion = assertionObjectFactory.createAssertionType();
response.getAssertionOrEncryptedAssertion().add(assertion);
SubjectType subject = assertionObjectFactory.createSubjectType();
assertion.setSubject(subject);
NameIDType nameID = assertionObjectFactory.createNameIDType();
subject.getContent().add(assertionObjectFactory.createNameID(nameID));
nameID.setValue(session.getPrincipal().getNameId().getValue());
nameID.setFormat(session.getPrincipal().getNameId().getFormat());
nameID.setNameQualifier(session.getPrincipal().getNameId().getQualifier());
SubjectConfirmationType subjectConfirmation = assertionObjectFactory.createSubjectConfirmationType();
subject.getContent().add(assertionObjectFactory.createSubjectConfirmation(subjectConfirmation));
subjectConfirmation.setMethod(SamlConstants.CONFIRMATION_METHOD_BEARER);
subjectConfirmation.setNameID(nameID);
SubjectConfirmationDataType subjectConfirmationData = assertionObjectFactory.createSubjectConfirmationDataType();
subjectConfirmation.setSubjectConfirmationData(subjectConfirmationData);
subjectConfirmationData.setRecipient(externalSamlEndpoint.getLocation());
subjectConfirmationData.setNotOnOrAfter(SamlUtils.getXMLGregorianCalendarNowPlusDuration(GregorianCalendar.MINUTE, ASSERTION_VALIDITY_IN_MINUTES));
subjectConfirmationData.setInResponseTo(samlDialogue.get().getExternalProviderMessageId());
ConditionsType conditions = assertionObjectFactory.createConditionsType();
assertion.setConditions(conditions);
AudienceRestrictionType audienceRestriction = assertionObjectFactory.createAudienceRestrictionType();
conditions.getConditionOrAudienceRestrictionOrOneTimeUse().add(audienceRestriction);
audienceRestriction.getAudience().add(samlDialogue.get().getExternalProvider().getEntityId());
AuthnStatementType authnStatement = assertionObjectFactory.createAuthnStatementType();
assertion.getStatementOrAuthnStatementOrAuthzDecisionStatement().add(authnStatement);
authnStatement.setAuthnInstant(SamlUtils.getXMLGregorianCalendarNow());
authnStatement.setSessionIndex(((SamlIdpSessionImpl) session).getSessionIndex());
AuthnContextType authnContext = assertionObjectFactory.createAuthnContextType();
authnStatement.setAuthnContext(authnContext);