Search in sources :

Example 1 with InvalidAccessTokenException

use of com.thoughtworks.go.server.exceptions.InvalidAccessTokenException in project gocd by gocd.

the class AccessTokenBasedPluginAuthenticationProviderTest method itShouldThrowErrorWhenAccessTokenBelongingTheUserDoesNotExists.

@Test
void itShouldThrowErrorWhenAccessTokenBelongingTheUserDoesNotExists() {
    when(authorizationService.isValidUser(pluginId, credentials.getAccessToken().getUsername(), authConfig)).thenReturn(false);
    InvalidAccessTokenException exception = assertThrows(InvalidAccessTokenException.class, () -> {
        provider.authenticateWithExtension(pluginId, credentials, authConfig, null);
    });
    assertThat(exception.getMessage()).startsWith("Invalid Personal Access Token. Access Token belonging to the user has either been disabled, removed or expired.");
}
Also used : InvalidAccessTokenException(com.thoughtworks.go.server.exceptions.InvalidAccessTokenException) Test(org.junit.jupiter.api.Test)

Example 2 with InvalidAccessTokenException

use of com.thoughtworks.go.server.exceptions.InvalidAccessTokenException in project gocd by gocd.

the class AccessTokenServiceIntegrationTest method shouldFailToGetAccessTokenWhenProvidedTokenHashEqualityFails.

@Test
public void shouldFailToGetAccessTokenWhenProvidedTokenHashEqualityFails() {
    String tokenDescription = "This is my first Token";
    AccessToken.AccessTokenWithDisplayValue createdToken = accessTokenService.create(tokenDescription, "bob", authConfigId);
    String accessTokenInString = createdToken.getDisplayValue();
    // replace last 5 characters to make the current token invalid
    String invalidAccessToken = StringUtils.replace(accessTokenInString, accessTokenInString.substring(35), "abcde");
    InvalidAccessTokenException exception = assertThrows(InvalidAccessTokenException.class, () -> accessTokenService.findByAccessToken(invalidAccessToken));
    assertThat("Invalid Personal Access Token.").isEqualTo(exception.getMessage());
}
Also used : InvalidAccessTokenException(com.thoughtworks.go.server.exceptions.InvalidAccessTokenException) AccessToken(com.thoughtworks.go.domain.AccessToken) Test(org.junit.jupiter.api.Test)

Example 3 with InvalidAccessTokenException

use of com.thoughtworks.go.server.exceptions.InvalidAccessTokenException in project gocd by gocd.

the class AccessTokenService method findByAccessToken.

public AccessToken findByAccessToken(String actualToken) {
    if (actualToken.length() != 40) {
        throw new InvalidAccessTokenException();
    }
    String saltId = StringUtils.substring(actualToken, 0, 8);
    AccessToken token = accessTokenDao.findAccessTokenBySaltId(saltId);
    if (token == null) {
        throw new InvalidAccessTokenException();
    }
    boolean isValid = token.isValidToken(actualToken);
    if (!isValid) {
        throw new InvalidAccessTokenException();
    }
    if (token.isRevoked()) {
        throw new RevokedAccessTokenException(token.getRevokedAt());
    }
    return token;
}
Also used : InvalidAccessTokenException(com.thoughtworks.go.server.exceptions.InvalidAccessTokenException) AccessToken(com.thoughtworks.go.domain.AccessToken) RevokedAccessTokenException(com.thoughtworks.go.server.exceptions.RevokedAccessTokenException)

Example 4 with InvalidAccessTokenException

use of com.thoughtworks.go.server.exceptions.InvalidAccessTokenException in project gocd by gocd.

the class AccessTokenBasedPluginAuthenticationProvider method authenticateWithExtension.

@Override
protected AuthenticationResponse authenticateWithExtension(String pluginId, AccessTokenCredential credentials, SecurityAuthConfig authConfig, List<PluginRoleConfig> pluginRoleConfigs) {
    String username = credentials.getAccessToken().getUsername();
    if (authorizationExtensionCacheService.isValidUser(pluginId, username, authConfig)) {
        List<String> roles = new ArrayList<>();
        if (store.doesPluginSupportGetUserRolesCall(pluginId)) {
            roles.addAll(authorizationExtensionCacheService.getUserRoles(pluginId, username, authConfig, pluginRoleConfigs));
        }
        com.thoughtworks.go.domain.User fetched = userService.findUserByName(username);
        User user = new User(fetched.getUsername().getUsername().toString(), fetched.getDisplayName(), fetched.getEmail());
        return new AuthenticationResponse(user, roles);
    } else {
        String msg = String.format("Access Token belonging to the user has either been disabled, removed or expired. ", username, pluginId, authConfig.getId());
        throw new InvalidAccessTokenException(msg);
    }
}
Also used : InvalidAccessTokenException(com.thoughtworks.go.server.exceptions.InvalidAccessTokenException) User(com.thoughtworks.go.plugin.domain.authorization.User) ArrayList(java.util.ArrayList) AuthenticationResponse(com.thoughtworks.go.plugin.domain.authorization.AuthenticationResponse)

Example 5 with InvalidAccessTokenException

use of com.thoughtworks.go.server.exceptions.InvalidAccessTokenException 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

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