public static AuthenticationBuilder of(final ApplicationContext applicationContext,
                                           final Principal principal,
                                           final PrincipalFactory principalFactory,
                                           final Map<String, List<Object>> principalAttributes,
                                           final Service service,
                                           final RegisteredService registeredService,
                                           final Authentication authentication) throws Throwable {
        val usernameContext = RegisteredServiceUsernameProviderContext.builder()
            .service(service)
            .principal(principal)
            .registeredService(registeredService)
            .applicationContext(applicationContext)
            .build();
        val principalId = registeredService.getUsernameAttributeProvider().resolveUsername(usernameContext);
        val newPrincipal = principalFactory.createPrincipal(principalId, principalAttributes);
        return DefaultAuthenticationBuilder.newInstance(authentication).setPrincipal(newPrincipal);
    }
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