Search in sources :

Example 11 with AccessToken

use of com.thoughtworks.go.domain.AccessToken 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 12 with AccessToken

use of com.thoughtworks.go.domain.AccessToken in project gocd by gocd.

the class AccessTokenAuthenticationFilter method extractAuthTokenCredential.

private AccessTokenCredential extractAuthTokenCredential(String authorizationHeader) {
    final Pattern BEARER_AUTH_EXTRACTOR_PATTERN = Pattern.compile("bearer (.*)", Pattern.CASE_INSENSITIVE);
    if (isBlank(authorizationHeader)) {
        return null;
    }
    final Matcher matcher = BEARER_AUTH_EXTRACTOR_PATTERN.matcher(authorizationHeader);
    if (matcher.matches()) {
        String token = matcher.group(1);
        AccessToken accessToken = accessTokenService.findByAccessToken(token);
        return new AccessTokenCredential(accessToken);
    }
    return null;
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) AccessToken(com.thoughtworks.go.domain.AccessToken) AccessTokenCredential(com.thoughtworks.go.server.newsecurity.models.AccessTokenCredential)

Example 13 with AccessToken

use of com.thoughtworks.go.domain.AccessToken in project gocd by gocd.

the class CurrentUserAccessTokenControllerV1 method createAccessToken.

public String createAccessToken(Request request, Response response) throws Exception {
    String authConfigId = currentUserAuthConfigId(request);
    SecurityAuthConfig authConfig = authConfigService.findProfile(authConfigId);
    if (!extension.supportsPluginAPICallsRequiredForAccessToken(authConfig)) {
        response.status(422);
        return MessageJson.create(String.format("Can not create Access Token. Please upgrade '%s' plugin to use Access Token Feature.", authConfig.getPluginId()));
    }
    final JsonReader reader = GsonTransformer.getInstance().jsonReaderFrom(request.body());
    String tokenDescription = reader.optString("description").orElse(null);
    AccessToken created = accessTokenService.create(tokenDescription, currentUsernameString(), currentUserAuthConfigId(request));
    if (!created.persisted()) {
        response.status(422);
    }
    return renderAccessToken(request, response, created);
}
Also used : SecurityAuthConfig(com.thoughtworks.go.config.SecurityAuthConfig) AccessToken(com.thoughtworks.go.domain.AccessToken) JsonReader(com.thoughtworks.go.api.representers.JsonReader)

Example 14 with AccessToken

use of com.thoughtworks.go.domain.AccessToken in project gocd by gocd.

the class AccessTokenSqlMapDaoIntegrationTest method shouldLoadAccessTokenBasedOnSaltId.

@Test
public void shouldLoadAccessTokenBasedOnSaltId() {
    AccessToken accessToken = randomAccessToken();
    accessTokenSqlMapDao.saveOrUpdate(accessToken);
    AccessToken savedAccessToken = accessTokenSqlMapDao.findAccessTokenBySaltId(accessToken.getSaltId());
    assertThat(savedAccessToken).isEqualTo(accessToken);
}
Also used : AccessTokenMother.randomAccessToken(com.thoughtworks.go.helper.AccessTokenMother.randomAccessToken) AccessToken(com.thoughtworks.go.domain.AccessToken) Test(org.junit.jupiter.api.Test)

Example 15 with AccessToken

use of com.thoughtworks.go.domain.AccessToken in project gocd by gocd.

the class AccessTokenSqlMapDaoIntegrationTest method shouldListAllTokens.

@Test
public void shouldListAllTokens() {
    String user1 = "will-be-deleted";
    String user2 = "will-be-revoked";
    AccessToken token1 = randomAccessTokenForUser(user1);
    AccessToken token2 = randomAccessTokenForUser(user2);
    accessTokenSqlMapDao.saveOrUpdate(token1);
    accessTokenSqlMapDao.saveOrUpdate(token2);
    accessTokenSqlMapDao.saveOrUpdate(token1.revoke("admin", "user is making too many requests", clock.currentTimestamp()));
    accessTokenSqlMapDao.revokeTokensBecauseOfUserDelete(Collections.singletonList(user2), "admin");
    assertThat(accessTokenSqlMapDao.findAllTokens(AccessTokenFilter.all)).hasSize(2).containsExactlyInAnyOrder(accessTokenSqlMapDao.loadForAdminUser(token1.getId()), accessTokenSqlMapDao.loadForAdminUser(token2.getId()));
}
Also used : AccessTokenMother.randomAccessToken(com.thoughtworks.go.helper.AccessTokenMother.randomAccessToken) AccessToken(com.thoughtworks.go.domain.AccessToken) Test(org.junit.jupiter.api.Test)

Aggregations

AccessToken (com.thoughtworks.go.domain.AccessToken)25 Test (org.junit.jupiter.api.Test)18 AccessTokenMother.randomAccessToken (com.thoughtworks.go.helper.AccessTokenMother.randomAccessToken)12 JsonReader (com.thoughtworks.go.api.representers.JsonReader)2 AccessTokenFilter (com.thoughtworks.go.server.service.AccessTokenFilter)2 Timestamp (java.sql.Timestamp)2 SecurityAuthConfig (com.thoughtworks.go.config.SecurityAuthConfig)1 ConflictException (com.thoughtworks.go.config.exceptions.ConflictException)1 RecordNotFoundException (com.thoughtworks.go.config.exceptions.RecordNotFoundException)1 InvalidAccessTokenException (com.thoughtworks.go.server.exceptions.InvalidAccessTokenException)1 RevokedAccessTokenException (com.thoughtworks.go.server.exceptions.RevokedAccessTokenException)1 AccessTokenCredential (com.thoughtworks.go.server.newsecurity.models.AccessTokenCredential)1 TransactionTemplate (com.thoughtworks.go.server.transaction.TransactionTemplate)1 Clock (com.thoughtworks.go.util.Clock)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1