Search in sources :

Example 1 with AccessToken

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

the class AccessTokenServiceIntegrationTest method shouldUpdateLastUsedTimeToDB.

@Test
public void shouldUpdateLastUsedTimeToDB() {
    AccessToken createdToken = accessTokenService.create("This is my first Token", "BOB", authConfigId);
    final AccessToken fetchedFromDB = accessTokenService.find(createdToken.getId(), createdToken.getUsername());
    assertThat(fetchedFromDB.getLastUsed()).isNull();
    accessTokenService.updateLastUsedCacheWith(createdToken);
    accessTokenService.onTimer();
    final AccessToken accessToken = accessTokenService.find(createdToken.getId(), createdToken.getUsername());
    assertThat(accessToken.getLastUsed()).isNotNull();
}
Also used : AccessToken(com.thoughtworks.go.domain.AccessToken) Test(org.junit.jupiter.api.Test)

Example 2 with AccessToken

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

the class AccessTokenServiceIntegrationTest method shouldGetAccessTokenProvidedTokenValue.

@Test
public void shouldGetAccessTokenProvidedTokenValue() {
    String tokenDescription = "This is my first Token";
    AccessToken.AccessTokenWithDisplayValue createdToken = accessTokenService.create(tokenDescription, "bob", authConfigId);
    String accessTokenInString = createdToken.getDisplayValue();
    AccessToken fetchedToken = accessTokenService.findByAccessToken(accessTokenInString);
    assertThat(fetchedToken).isEqualTo(createdToken);
}
Also used : AccessToken(com.thoughtworks.go.domain.AccessToken) Test(org.junit.jupiter.api.Test)

Example 3 with AccessToken

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

the class AccessTokenSqlMapDao method revokeTokensBecauseOfUserDelete.

@Override
public void revokeTokensBecauseOfUserDelete(Collection<String> usernames, String byWhom) {
    transactionTemplate.execute(status -> {
        Session currentSession = sessionFactory.getCurrentSession();
        usernames.stream().flatMap(username -> findAllTokensForUser(username, AccessTokenFilter.all).stream()).forEach(accessToken -> {
            accessToken.revokeBecauseOfUserDelete(byWhom, clock.currentTimestamp());
            currentSession.saveOrUpdate(accessToken);
        });
        return Boolean.TRUE;
    });
}
Also used : Criteria(org.hibernate.Criteria) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) Restrictions(org.hibernate.criterion.Restrictions) AccessToken(com.thoughtworks.go.domain.AccessToken) Clock(com.thoughtworks.go.util.Clock) Timestamp(java.sql.Timestamp) Collection(java.util.Collection) SessionFactory(org.hibernate.SessionFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) HibernateDaoSupport(org.springframework.orm.hibernate3.support.HibernateDaoSupport) Component(org.springframework.stereotype.Component) List(java.util.List) Session(org.hibernate.classic.Session) AccessTokenFilter(com.thoughtworks.go.server.service.AccessTokenFilter) Map(java.util.Map) TransactionStatus(org.springframework.transaction.TransactionStatus) Query(org.hibernate.Query) TransactionCallback(org.springframework.transaction.support.TransactionCallback) TransactionTemplate(com.thoughtworks.go.server.transaction.TransactionTemplate) Session(org.hibernate.classic.Session)

Example 4 with AccessToken

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

the class AbstractUserAccessTokenControllerV1 method getAllAccessTokens.

public String getAllAccessTokens(Request request, Response response) throws Exception {
    String filterValue = request.queryParams("filter");
    AccessTokenFilter filter = AccessTokenFilter.fromString(filterValue);
    if (filter == null) {
        throw HaltApiResponses.haltBecauseQueryParamIsUnknown("filter", filterValue, Arrays.stream(AccessTokenFilter.values()).map(Object::toString).toArray(String[]::new));
    }
    List<AccessToken> allTokens = allTokens(filter);
    return writerForTopLevelObject(request, response, outputWriter -> AccessTokensRepresenter.toJSON(outputWriter, urlContext(), allTokens));
}
Also used : AccessTokenFilter(com.thoughtworks.go.server.service.AccessTokenFilter) AccessToken(com.thoughtworks.go.domain.AccessToken)

Example 5 with AccessToken

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

the class AbstractUserAccessTokenControllerV1 method revokeAccessToken.

public String revokeAccessToken(Request request, Response response) throws Exception {
    long id = Long.parseLong(request.params(":id"));
    final JsonReader reader = GsonTransformer.getInstance().jsonReaderFrom(request.body());
    String revokeCause = reader.optString("revoke_cause").orElse(null);
    AccessToken revokeAccessToken = accessTokenService.revokeAccessToken(id, currentUsernameString(), revokeCause);
    return renderAccessToken(request, response, revokeAccessToken);
}
Also used : AccessToken(com.thoughtworks.go.domain.AccessToken) JsonReader(com.thoughtworks.go.api.representers.JsonReader)

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