Search in sources :

Example 6 with AuthenticationResponse

use of com.thoughtworks.go.plugin.domain.authorization.AuthenticationResponse in project gocd by gocd.

the class AbstractPluginAuthenticationProvider method authenticateUser.

public AuthenticationToken<T> authenticateUser(T credentials, SecurityAuthConfig authConfig) {
    String pluginId = authConfig.getPluginId();
    try {
        if (!doesPluginSupportAuthentication(pluginId)) {
            return null;
        }
        final List<PluginRoleConfig> roleConfigs = goConfigService.security().getRoles().pluginRoleConfigsFor(authConfig.getId());
        LOGGER.debug("Authenticating user using the authorization plugin: `{}`", pluginId);
        AuthenticationResponse response = authenticateWithExtension(pluginId, credentials, authConfig, roleConfigs);
        User user = ensureDisplayNamePresent(response.getUser());
        if (user != null) {
            userService.addOrUpdateUser(toDomainUser(user), authConfig);
            pluginRoleService.updatePluginRoles(pluginId, user.getUsername(), CaseInsensitiveString.list(response.getRoles()));
            LOGGER.debug("Successfully authenticated user: `{}` using the authorization plugin: `{}`", user.getUsername(), pluginId);
            final GoUserPrinciple goUserPrinciple = new GoUserPrinciple(user.getUsername(), user.getDisplayName(), authorityGranter.authorities(user.getUsername()));
            return createAuthenticationToken(goUserPrinciple, credentials, pluginId, authConfig.getId());
        }
    } catch (OnlyKnownUsersAllowedException e) {
        LOGGER.info("User {} is successfully authenticated. Auto register new user is disabled. Please refer {}", e.getUsername(), CurrentGoCDVersion.docsUrl("configuration/dev_authentication.html#controlling-user-access"));
        throw e;
    } catch (InvalidAccessTokenException e) {
        LOGGER.error("Error while authenticating user using auth_config: {} with the authorization plugin: {} ", authConfig.getId(), pluginId);
        throw e;
    } catch (Exception e) {
        LOGGER.error("Error while authenticating user using auth_config: {} with the authorization plugin: {} ", authConfig.getId(), pluginId);
    }
    LOGGER.debug("Authentication failed using the authorization plugin: `{}`", pluginId);
    return null;
}
Also used : InvalidAccessTokenException(com.thoughtworks.go.server.exceptions.InvalidAccessTokenException) User(com.thoughtworks.go.plugin.domain.authorization.User) OnlyKnownUsersAllowedException(com.thoughtworks.go.server.security.OnlyKnownUsersAllowedException) CaseInsensitiveString(com.thoughtworks.go.config.CaseInsensitiveString) GoUserPrinciple(com.thoughtworks.go.server.security.userdetail.GoUserPrinciple) PluginRoleConfig(com.thoughtworks.go.config.PluginRoleConfig) AuthenticationResponse(com.thoughtworks.go.plugin.domain.authorization.AuthenticationResponse) InvalidAccessTokenException(com.thoughtworks.go.server.exceptions.InvalidAccessTokenException) OnlyKnownUsersAllowedException(com.thoughtworks.go.server.security.OnlyKnownUsersAllowedException)

Aggregations

AuthenticationResponse (com.thoughtworks.go.plugin.domain.authorization.AuthenticationResponse)6 User (com.thoughtworks.go.plugin.domain.authorization.User)4 Test (org.junit.jupiter.api.Test)4 PluginRoleConfig (com.thoughtworks.go.config.PluginRoleConfig)2 SecurityAuthConfig (com.thoughtworks.go.config.SecurityAuthConfig)2 SecurityAuthConfigs (com.thoughtworks.go.config.SecurityAuthConfigs)2 User (com.thoughtworks.go.domain.User)2 DefaultGoPluginApiResponse (com.thoughtworks.go.plugin.api.response.DefaultGoPluginApiResponse)2 InvalidAccessTokenException (com.thoughtworks.go.server.exceptions.InvalidAccessTokenException)2 CaseInsensitiveString (com.thoughtworks.go.config.CaseInsensitiveString)1 OnlyKnownUsersAllowedException (com.thoughtworks.go.server.security.OnlyKnownUsersAllowedException)1 GoUserPrinciple (com.thoughtworks.go.server.security.userdetail.GoUserPrinciple)1 ArrayList (java.util.ArrayList)1