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;
}
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;
}
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);
}
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);
}
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()));
}
Aggregations