@Override
    @CanIgnoreReturnValue
    public Authentication build() {
        val resultingCredentials = new LinkedHashMap<String, Credential>();
        credentials.forEach(credential -> {
            val key = credential.getId() + '#' + credential.getClass().getName();
            if (resultingCredentials.containsKey(key)) {
                val current = resultingCredentials.get(key);

                if (current instanceof final MutableCredential currentMutable && credential instanceof final MutableCredential credentialMutable) {
                    FunctionUtils.doIfNull(credential.getCredentialMetadata(), __ -> credentialMutable.setCredentialMetadata(new BasicCredentialMetadata(credentialMutable)));
                    FunctionUtils.doIfNull(current.getCredentialMetadata(), __ -> currentMutable.setCredentialMetadata(new BasicCredentialMetadata(currentMutable)));
                    current.getCredentialMetadata().putProperties(credential.getCredentialMetadata().getProperties());
                }
                resultingCredentials.put(key, current);
            } else {
                resultingCredentials.put(key, credential);
            }
        });

        return new DefaultAuthentication(this.authenticationDate, this.principal,
            this.warnings, new ArrayList<>(resultingCredentials.values()),
            this.attributes, this.successes, this.failures);
    }
Factory method.

@param applicationContext  the application context
@param principal           principal.
@param principalFactory    principalFactory.
@param principalAttributes principalAttributes.
@param service             service.
@param registeredService   registeredService.
@param authentication      authentication.
@return AuthenticationBuilder new AuthenticationBuilder instance.
@throws Throwable the throwable
core/cas-server-core-authentication-api/src/main/java/org/apereo/cas/authentication/DefaultAuthenticationBuilder.java