protected Optional<SAMLObject> encryptAssertion(final Optional<Assertion> assertion, final SamlProfileBuilderContext context) {
        return assertion.map(result -> {
            if (encryptAssertionFor(context)) {
                LOGGER.debug("SAML service [{}] requires assertions to be encrypted", context.getAdaptor().getEntityId());
                val encrypted = configurationContext.getSamlObjectEncrypter().encode(assertion.get(),
                    context.getRegisteredService(), context.getAdaptor());
                if (encrypted == null) {
                    LOGGER.debug("SAML registered service [{}] is unable to encrypt assertions",
                        context.getAdaptor().getEntityId());
                    return assertion.get();
                }
                return encrypted;
            }
            LOGGER.debug("SAML registered service [{}] does not require assertions to be encrypted",
                context.getAdaptor().getEntityId());
            return assertion.get();
        });

    }
The {@link BaseSamlProfileSamlResponseBuilder} is responsible for
building the final SAML assertion for the relying party.

@author Misagh Moayyed
@since 4.2
support/cas-server-support-saml-idp-web/src/main/java/org/apereo/cas/support/saml/web/idp/profile/builders/response/BaseSamlProfileSamlResponseBuilder.java