private static UserContext redecorate(DecoratedUserContext decorated,
            UserContext userContext, AuthenticatedUser authenticatedUser,
            Credentials credentials) throws GuacamoleAuthenticationProcessException {

        AuthenticationProvider authProvider = decorated.getDecoratingAuthenticationProvider();

        // Skip the AuthenticationProvider which produced the UserContext
        // being decorated
        if (authProvider != userContext.getAuthenticationProvider()) {

            // Apply next layer of wrapping around UserContext
            UserContext redecorated;
            try {
                redecorated = authProvider.redecorate(decorated.getDelegateUserContext(),
                        userContext, authenticatedUser, credentials);
            }
            catch (GuacamoleException | RuntimeException | Error e) {
                throw new GuacamoleAuthenticationProcessException("User "
                        + "authentication aborted by redecorating UserContext.",
                        authProvider, e);
            }

            // Do not allow misbehaving extensions to wipe out the
            // UserContext entirely
            if (redecorated != null)
                return redecorated;

        }

        return userContext;

    }
Redecorates an updated UserContext (as would be returned by
updateUserContext()), invoking the redecorate() function of the given
AuthenticationProvider to apply an additional layer of decoration. If the
AuthenticationProvider originated the given UserContext, this function
has no effect.

@param decorated
    The DecoratedUserContext associated with an older version of the
    given UserContext.

@param userContext
    The new version of the UserContext which should be decorated.

@param authenticatedUser
    The AuthenticatedUser identifying the user associated with the given
    UserContext.

@param credentials
    The credentials associated with the request which produced the given
    UserContext.

@return
    A UserContext instance which has been decorated (wrapped) by the
    given AuthenticationProvider, or the original UserContext if the
    given AuthenticationProvider originated the UserContext.

@throws GuacamoleAuthenticationProcessException
    If the given AuthenticationProvider fails while decorating the
    UserContext.
guacamole/src/main/java/org/apache/guacamole/rest/auth/DecoratedUserContext.java