Search in sources :

Example 1 with NewTokenEntity

use of io.gravitee.rest.api.model.NewTokenEntity 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 2 with NewTokenEntity

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

the class TokenServiceTest method shouldNotCreateNameExists.

@Test(expected = TokenNameAlreadyExistsException.class)
public void shouldNotCreateNameExists() throws TechnicalException {
    final NewTokenEntity newToken = new NewTokenEntity();
    newToken.setName("name");
    when(tokenRepository.findByReference(eq(USER.name()), eq(USER_ID))).thenReturn(singletonList(token));
    tokenService.create(newToken);
}
Also used : NewTokenEntity(io.gravitee.rest.api.model.NewTokenEntity) Test(org.junit.Test)

Example 3 with NewTokenEntity

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

the class TokenServiceTest method shouldCreate.

@Test
public void shouldCreate() throws TechnicalException {
    final NewTokenEntity newToken = new NewTokenEntity();
    newToken.setName("name");
    when(tokenRepository.create(any())).thenReturn(token);
    tokenService.create(newToken);
    verify(auditService).createEnvironmentAuditLog(anyMap(), eq(TOKEN_CREATED), any(Date.class), isNull(), any());
    verify(tokenRepository).create(any());
    verify(tokenRepository).findByReference(eq(USER.name()), eq(USER_ID));
}
Also used : NewTokenEntity(io.gravitee.rest.api.model.NewTokenEntity) Date(java.util.Date) Test(org.junit.Test)

Aggregations

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