@Override
    public Response buildResponse(final Optional<Assertion> assertion,
                                  final SamlProfileBuilderContext context) throws Exception {
        val id = '_' + String.valueOf(RandomUtils.nextLong());

        val entityId = getConfigurationContext().getCasProperties().getAuthn().getSamlIdp().getCore().getEntityId();
        val recipient = getInResponseTo(context.getSamlRequest(), entityId, context.getRegisteredService().isSkipGeneratingResponseInResponseTo());
        val samlResponse = newResponse(id, ZonedDateTime.now(ZoneOffset.UTC), recipient, null);
        samlResponse.setVersion(SAMLVersion.VERSION_20);

        val issuerId = FunctionUtils.doIf(StringUtils.isNotBlank(context.getRegisteredService().getIssuerEntityId()),
                context.getRegisteredService()::getIssuerEntityId,
                Unchecked.supplier(() -> {
                    val criteriaSet = new CriteriaSet(
                        new EvaluableEntityRoleEntityDescriptorCriterion(IDPSSODescriptor.DEFAULT_ELEMENT_NAME),
                        new SamlIdPSamlRegisteredServiceCriterion(context.getRegisteredService()));
                    LOGGER.trace("Resolving entity id from SAML2 IdP metadata to determine issuer for [{}]", context.getRegisteredService().getName());
                    val entityDescriptor = Objects.requireNonNull(getConfigurationContext().getSamlIdPMetadataResolver().resolveSingle(criteriaSet));
                    return entityDescriptor.getEntityID();
                }))
            .get();

        samlResponse.setIssuer(buildSamlResponseIssuer(issuerId));
        val acs = SamlIdPUtils.determineEndpointForRequest(Pair.of(context.getSamlRequest(), context.getMessageContext()),
            context.getAdaptor(), context.getBinding());
        val location = StringUtils.isBlank(acs.getResponseLocation()) ? acs.getLocation() : acs.getResponseLocation();
        samlResponse.setDestination(location);

        if (getConfigurationContext().getCasProperties()
            .getAuthn().getSamlIdp().getCore().isAttributeQueryProfileEnabled()) {
            storeAttributeQueryTicketInRegistry(assertion, context);
        }

        val finalAssertion = encryptAssertion(assertion, context);
        if (finalAssertion.isPresent()) {
            val result = finalAssertion.get();
            if (result instanceof final EncryptedAssertion encrypted) {
                LOGGER.trace("Built assertion is encrypted, so the response will add it to the encrypted assertions collection");
                samlResponse.getEncryptedAssertions().add(encrypted);
            } else if (result instanceof final Assertion nonEncryptedAssertion) {
                LOGGER.trace("Built assertion is not encrypted, so the response will add it to the assertions collection");
                samlResponse.getAssertions().add(nonEncryptedAssertion);
            }
        }

        samlResponse.setStatus(determineResponseStatus(context));

        val customizers = configurationContext.getApplicationContext()
            .getBeansOfType(SamlIdPResponseCustomizer.class).values();
        customizers.stream()
            .sorted(AnnotationAwareOrderComparator.INSTANCE)
            .forEach(customizer -> customizer.customizeResponse(context, this, samlResponse));

        openSamlConfigBean.logObject(samlResponse);

        if (signSamlResponseFor(context)) {
            LOGGER.debug("SAML entity id [{}] indicates that SAML responses should be signed", context.getAdaptor().getEntityId());
            val samlResponseSigned = getConfigurationContext().getSamlObjectSigner().encode(samlResponse,
                context.getRegisteredService(), context.getAdaptor(), context.getHttpResponse(), context.getHttpRequest(),
                context.getBinding(), context.getSamlRequest(), context.getMessageContext());
            openSamlConfigBean.logObject(samlResponseSigned);
            return samlResponseSigned;
        }

        return samlResponse;
    }
This is {@link SamlProfileSaml2ResponseBuilder}.

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