Search in sources :

Example 1 with ApiKeyBO

use of com.nexblocks.authguard.service.model.ApiKeyBO in project AuthGuard by AuthGuard.

the class ApiKeysRoute method generate.

public void generate(final Context context) {
    final ApiKeyRequestDTO request = apiKeyRequestBodyHandler.getValidated(context);
    final ApiKeyBO key = apiKeysService.generateApiKey(request.getAppId());
    context.status(201).json(restMapper.toDTO(key));
}
Also used : ApiKeyBO(com.nexblocks.authguard.service.model.ApiKeyBO) ApiKeyRequestDTO(com.nexblocks.authguard.api.dto.requests.ApiKeyRequestDTO)

Example 2 with ApiKeyBO

use of com.nexblocks.authguard.service.model.ApiKeyBO in project AuthGuard by AuthGuard.

the class ApiKeysServiceImplTest method getByAppId.

@Test
void getByAppId() {
    final String appId = "app";
    final String key = "key";
    final ApiKeyBO apiKeyBO = ApiKeyBO.builder().appId(appId).key(key).build();
    final ApiKeyDO apiKeyDO = serviceMapper.toDO(apiKeyBO);
    Mockito.when(apiKeysRepository.getByAppId(appId)).thenReturn(CompletableFuture.completedFuture(Collections.singletonList(apiKeyDO)));
    final List<ApiKeyBO> actual = apiKeysService.getByAppId(appId);
    assertThat(actual).isEqualTo(Collections.singletonList(apiKeyBO));
}
Also used : ApiKeyBO(com.nexblocks.authguard.service.model.ApiKeyBO) ApiKeyDO(com.nexblocks.authguard.dal.model.ApiKeyDO) Test(org.junit.jupiter.api.Test)

Example 3 with ApiKeyBO

use of com.nexblocks.authguard.service.model.ApiKeyBO in project AuthGuard by AuthGuard.

the class ApiKeysServiceImplTest method getById.

@Test
void getById() {
    final String id = "api_key";
    final ApiKeyBO apiKeyBO = ApiKeyBO.builder().id(id).build();
    final ApiKeyDO apiKeyDO = ApiKeyDO.builder().id(id).build();
    Mockito.when(apiKeysRepository.getById(id)).thenReturn(CompletableFuture.completedFuture(Optional.of(apiKeyDO)));
    final Optional<ApiKeyBO> actual = apiKeysService.getById(id);
    assertThat(actual).contains(apiKeyBO);
}
Also used : ApiKeyBO(com.nexblocks.authguard.service.model.ApiKeyBO) ApiKeyDO(com.nexblocks.authguard.dal.model.ApiKeyDO) Test(org.junit.jupiter.api.Test)

Example 4 with ApiKeyBO

use of com.nexblocks.authguard.service.model.ApiKeyBO in project AuthGuard by AuthGuard.

the class ApiKeysServiceImplTest method generateApiKey.

@Test
void generateApiKey() {
    final String appId = "app";
    final String key = "key";
    final AppBO app = AppBO.builder().id(appId).build();
    Mockito.when(applicationsService.getById(appId)).thenReturn(Optional.of(app));
    Mockito.when(apiKeysRepository.save(Mockito.any())).thenAnswer(invocation -> CompletableFuture.completedFuture(invocation.getArgument(0, ApiKeyDO.class)));
    Mockito.when(apiKeyExchange.generateKey(app)).thenReturn(AuthResponseBO.builder().token(key).build());
    final ApiKeyBO actual = apiKeysService.generateApiKey(appId);
    assertThat(actual.getAppId()).isEqualTo(appId);
    assertThat(actual.getKey()).isEqualTo(key);
    // verify that we persisted the hashed key and not the clear key
    final ArgumentCaptor<ApiKeyDO> argumentCaptor = ArgumentCaptor.forClass(ApiKeyDO.class);
    Mockito.verify(apiKeysRepository).save(argumentCaptor.capture());
    final ApiKeyDO persisted = argumentCaptor.getValue();
    assertThat(persisted.getKey()).isEqualTo(apiKeyHash.hash(key));
}
Also used : ApiKeyBO(com.nexblocks.authguard.service.model.ApiKeyBO) ApiKeyDO(com.nexblocks.authguard.dal.model.ApiKeyDO) AppBO(com.nexblocks.authguard.service.model.AppBO) Test(org.junit.jupiter.api.Test)

Example 5 with ApiKeyBO

use of com.nexblocks.authguard.service.model.ApiKeyBO in project AuthGuard by AuthGuard.

the class ApiKeysServiceImplTest method delete.

@Test
void delete() {
    final String id = "api_key";
    final ApiKeyBO apiKeyBO = ApiKeyBO.builder().id(id).build();
    final ApiKeyDO apiKeyDO = ApiKeyDO.builder().id(id).build();
    Mockito.when(apiKeysRepository.delete(id)).thenReturn(CompletableFuture.completedFuture(Optional.of(apiKeyDO)));
    final Optional<ApiKeyBO> actual = apiKeysService.delete(id);
    assertThat(actual).contains(apiKeyBO);
}
Also used : ApiKeyBO(com.nexblocks.authguard.service.model.ApiKeyBO) ApiKeyDO(com.nexblocks.authguard.dal.model.ApiKeyDO) Test(org.junit.jupiter.api.Test)

Aggregations

ApiKeyBO (com.nexblocks.authguard.service.model.ApiKeyBO)6 ApiKeyDO (com.nexblocks.authguard.dal.model.ApiKeyDO)4 Test (org.junit.jupiter.api.Test)4 ApiKeyRequestDTO (com.nexblocks.authguard.api.dto.requests.ApiKeyRequestDTO)1 AppBO (com.nexblocks.authguard.service.model.AppBO)1 AuthResponseBO (com.nexblocks.authguard.service.model.AuthResponseBO)1