private EntityDescriptor buildMetadataEntityDescriptorElement(
            String defaultHostname, SamlPortConfig portConfig) {
        final EntityDescriptor entityDescriptor = build(EntityDescriptor.DEFAULT_ELEMENT_NAME);
        entityDescriptor.setEntityID(entityId);

        final SPSSODescriptor spSsoDescriptor = build(SPSSODescriptor.DEFAULT_ELEMENT_NAME);
        spSsoDescriptor.setAuthnRequestsSigned(true);
        spSsoDescriptor.setWantAssertionsSigned(true);
        spSsoDescriptor.addSupportedProtocol(SAMLConstants.SAML20P_NS);

        final List<String> nameIdFormats = idpConfigs.values().stream()
                                                     .map(p -> p.nameIdPolicy().format())
                                                     .distinct()
                                                     .map(SamlNameIdFormat::urn)
                                                     .collect(Collectors.toList());
        spSsoDescriptor.getNameIDFormats().addAll(buildNameIdFormatElements(nameIdFormats));

        final List<SingleLogoutService> sloList = spSsoDescriptor.getSingleLogoutServices();
        singleLogoutEndpoints.forEach(endpoint -> {
            final SingleLogoutService slo = build(SingleLogoutService.DEFAULT_ELEMENT_NAME);
            slo.setBinding(endpoint.bindingProtocol().urn());
            slo.setLocation(endpoint.toUriString(portConfig.scheme().uriText(),
                                                 defaultHostname,
                                                 portConfig.port()));
            sloList.add(slo);
        });

        int acsIndex = 0;
        final List<AssertionConsumerService> services = spSsoDescriptor.getAssertionConsumerServices();
        for (final SamlAssertionConsumerConfig acs : assertionConsumerConfigs) {
            services.add(buildAssertionConsumerServiceElement(acs, portConfig, defaultHostname, acsIndex++));
        }

        final X509KeyInfoGeneratorFactory keyInfoGeneratorFactory = new X509KeyInfoGeneratorFactory();
        keyInfoGeneratorFactory.setEmitEntityCertificate(true);
        keyInfoGeneratorFactory.setEmitEntityCertificateChain(true);
        final KeyInfoGenerator keyInfoGenerator = keyInfoGeneratorFactory.newInstance();

        try {
            spSsoDescriptor.getKeyDescriptors().add(
                    buildKeyDescriptorElement(UsageType.SIGNING,
                                              keyInfoGenerator.generate(signingCredential)));
            spSsoDescriptor.getKeyDescriptors().add(
                    buildKeyDescriptorElement(UsageType.ENCRYPTION,
                                              keyInfoGenerator.generate(encryptionCredential)));
        } catch (SecurityException e) {
            throw new SamlException("failed to generate KeyInfo element", e);
        }

        entityDescriptor.getRoleDescriptors().add(spSsoDescriptor);
        return entityDescriptor;
    }
A service which returns the SAML metadata in response to a user request.
saml/src/main/java/com/linecorp/armeria/server/saml/SamlMetadataServiceFunction.java