Search in sources :

Example 1 with TokenEntity

use of io.gravitee.rest.api.model.TokenEntity in project gravitee-management-rest-api by gravitee-io.

the class TokenServiceImpl method convert.

private TokenEntity convert(final Token token, final String decodedToken) {
    final TokenEntity tokenEntity = new TokenEntity();
    tokenEntity.setId(token.getId());
    tokenEntity.setToken(decodedToken);
    tokenEntity.setName(token.getName());
    tokenEntity.setCreatedAt(token.getCreatedAt());
    tokenEntity.setExpiresAt(token.getExpiresAt());
    tokenEntity.setLastUseAt(token.getLastUseAt());
    return tokenEntity;
}
Also used : TokenEntity(io.gravitee.rest.api.model.TokenEntity) NewTokenEntity(io.gravitee.rest.api.model.NewTokenEntity)

Example 2 with TokenEntity

use of io.gravitee.rest.api.model.TokenEntity in project gravitee-management-rest-api by gravitee-io.

the class TokenServiceImpl method create.

@Override
public TokenEntity create(NewTokenEntity newToken) {
    try {
        final String username = getAuthenticatedUsername();
        // check if name already exists
        final List<TokenEntity> tokens = findByUser(username);
        final boolean nameAlreadyExists = tokens.stream().anyMatch(token -> newToken.getName().equalsIgnoreCase(token.getName()));
        if (nameAlreadyExists) {
            throw new TokenNameAlreadyExistsException(newToken.getName());
        }
        final String decodedToken = UUID.toString(UUID.random());
        final Token token = convert(newToken, TokenReferenceType.USER, username, passwordEncoder.encode(decodedToken));
        auditService.createEnvironmentAuditLog(Collections.singletonMap(TOKEN, token.getId()), TOKEN_CREATED, token.getCreatedAt(), null, token);
        return convert(tokenRepository.create(token), decodedToken);
    } catch (TechnicalException e) {
        final String error = "An error occurs while trying to create a token " + newToken;
        LOGGER.error(error, e);
        throw new TechnicalManagementException(error, e);
    }
}
Also used : TechnicalException(io.gravitee.repository.exceptions.TechnicalException) TokenEntity(io.gravitee.rest.api.model.TokenEntity) NewTokenEntity(io.gravitee.rest.api.model.NewTokenEntity) Token(io.gravitee.repository.management.model.Token) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException) TokenNameAlreadyExistsException(io.gravitee.rest.api.service.exceptions.TokenNameAlreadyExistsException)

Example 3 with TokenEntity

use of io.gravitee.rest.api.model.TokenEntity in project gravitee-management-rest-api by gravitee-io.

the class TokenServiceTest method shouldFindByUser.

@Test
public void shouldFindByUser() throws TechnicalException {
    final Token token2 = new Token();
    token2.setId("2");
    when(tokenRepository.findByReference(eq(USER.name()), eq(USER_ID))).thenReturn(asList(token, token2));
    final List<TokenEntity> tokens = tokenService.findByUser(USER_ID);
    assertEquals(TOKEN_ID, tokens.get(0).getId());
    assertEquals("name", tokens.get(0).getName());
    assertNull("Token cannot be read after creation", tokens.get(0).getToken());
    assertEquals(new Date(1486771200000L), tokens.get(0).getCreatedAt());
    assertEquals(new Date(1486772200000L), tokens.get(0).getExpiresAt());
    assertEquals(new Date(1486773200000L), tokens.get(0).getLastUseAt());
    assertEquals("2", tokens.get(1).getId());
}
Also used : TokenEntity(io.gravitee.rest.api.model.TokenEntity) NewTokenEntity(io.gravitee.rest.api.model.NewTokenEntity) Token(io.gravitee.repository.management.model.Token) Date(java.util.Date) Test(org.junit.Test)

Aggregations

NewTokenEntity (io.gravitee.rest.api.model.NewTokenEntity)3 TokenEntity (io.gravitee.rest.api.model.TokenEntity)3 Token (io.gravitee.repository.management.model.Token)2 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)1 TechnicalManagementException (io.gravitee.rest.api.service.exceptions.TechnicalManagementException)1 TokenNameAlreadyExistsException (io.gravitee.rest.api.service.exceptions.TokenNameAlreadyExistsException)1 Date (java.util.Date)1 Test (org.junit.Test)1