Search in sources :

Example 6 with AppBO

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

the class ApplicationsServiceImplTest method activate.

@Test
void activate() {
    final AppDO app = random.nextObject(AppDO.class);
    app.setActive(false);
    Mockito.when(applicationsRepository.getById(app.getId())).thenReturn(CompletableFuture.completedFuture(Optional.of(app)));
    Mockito.when(applicationsRepository.update(any())).thenAnswer(invocation -> CompletableFuture.completedFuture(Optional.of(invocation.getArgument(0, AppDO.class))));
    final AppBO updated = applicationsService.activate(app.getId()).orElse(null);
    assertThat(updated).isNotNull();
    assertThat(updated.isActive()).isTrue();
}
Also used : AppDO(com.nexblocks.authguard.dal.model.AppDO) AppBO(com.nexblocks.authguard.service.model.AppBO) Test(org.junit.jupiter.api.Test)

Example 7 with AppBO

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

the class TestAccessManager method manage.

@Override
public void manage(@NotNull final Handler handler, @NotNull final Context context, @NotNull final Set<Role> set) throws Exception {
    final AppBO app = AppBO.builder().addRoles("authguard_admin").build();
    context.attribute("actor", app);
    handler.handle(context);
}
Also used : AppBO(com.nexblocks.authguard.service.model.AppBO)

Example 8 with AppBO

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

the class AuthorizationHandler method populateBearerActor.

private void populateBearerActor(final Context context, final String apiKey) {
    final Optional<AppBO> actorApp = apiKeysService.validateApiKey(apiKey);
    if (actorApp.isPresent()) {
        LOG.info("Authenticated actor {} with bearer token", actorApp.get().getId());
        context.attribute("actor", actorApp.get());
    } else {
        LOG.info("Failed to authenticate actor with bearer token");
        context.status(401).json(new Error("401", "Failed to authenticate with bearer scheme"));
    }
}
Also used : AppBO(com.nexblocks.authguard.service.model.AppBO) Error(com.nexblocks.authguard.api.dto.entities.Error)

Example 9 with AppBO

use of com.nexblocks.authguard.service.model.AppBO 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 10 with AppBO

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

the class ApplicationsServiceImplTest method create.

@Test
void create() {
    final AppBO app = random.nextObject(AppBO.class);
    final String idempotentKey = "idempotent-key";
    final RequestContextBO requestContext = RequestContextBO.builder().idempotentKey(idempotentKey).build();
    Mockito.when(accountsService.getById(app.getParentAccountId())).thenReturn(Optional.of(random.nextObject(AccountBO.class)));
    Mockito.when(applicationsRepository.save(any())).thenAnswer(invocation -> CompletableFuture.completedFuture(invocation.getArgument(0, AppDO.class)));
    Mockito.when(idempotencyService.performOperation(Mockito.any(), Mockito.eq(idempotentKey), Mockito.eq(app.getEntityType()))).thenAnswer(invocation -> {
        return CompletableFuture.completedFuture(invocation.getArgument(0, Supplier.class).get());
    });
    final AppBO created = applicationsService.create(app, requestContext);
    final List<PermissionBO> expectedPermissions = app.getPermissions().stream().map(permission -> permission.withEntityType(null)).collect(Collectors.toList());
    assertThat(created).isEqualToIgnoringGivenFields(app.withPermissions(expectedPermissions), "id", "createdAt", "lastModified", "entityType");
    Mockito.verify(messageBus, Mockito.times(1)).publish(eq("apps"), any());
}
Also used : ServiceMapper(com.nexblocks.authguard.service.mappers.ServiceMapper) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BeforeEach(org.junit.jupiter.api.BeforeEach) MessageBus(com.nexblocks.authguard.emb.MessageBus) RequestContextBO(com.nexblocks.authguard.service.model.RequestContextBO) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ServiceMapperImpl(com.nexblocks.authguard.service.mappers.ServiceMapperImpl) CompletableFuture(java.util.concurrent.CompletableFuture) Supplier(java.util.function.Supplier) AccountsService(com.nexblocks.authguard.service.AccountsService) AppBO(com.nexblocks.authguard.service.model.AppBO) ApplicationsService(com.nexblocks.authguard.service.ApplicationsService) IdempotencyService(com.nexblocks.authguard.service.IdempotencyService) EasyRandomParameters(org.jeasy.random.EasyRandomParameters) AppDO(com.nexblocks.authguard.dal.model.AppDO) ApplicationsRepository(com.nexblocks.authguard.dal.persistence.ApplicationsRepository) EasyRandom(org.jeasy.random.EasyRandom) AccountBO(com.nexblocks.authguard.service.model.AccountBO) PermissionBO(com.nexblocks.authguard.service.model.PermissionBO) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) Mockito(org.mockito.Mockito) List(java.util.List) Optional(java.util.Optional) RequestContextBO(com.nexblocks.authguard.service.model.RequestContextBO) AppBO(com.nexblocks.authguard.service.model.AppBO) PermissionBO(com.nexblocks.authguard.service.model.PermissionBO) Test(org.junit.jupiter.api.Test)

Aggregations

AppBO (com.nexblocks.authguard.service.model.AppBO)12 Test (org.junit.jupiter.api.Test)8 AppDO (com.nexblocks.authguard.dal.model.AppDO)4 Error (com.nexblocks.authguard.api.dto.entities.Error)3 ApplicationsRepository (com.nexblocks.authguard.dal.persistence.ApplicationsRepository)2 MessageBus (com.nexblocks.authguard.emb.MessageBus)2 AccountsService (com.nexblocks.authguard.service.AccountsService)2 ApplicationsService (com.nexblocks.authguard.service.ApplicationsService)2 IdempotencyService (com.nexblocks.authguard.service.IdempotencyService)2 ServiceMapper (com.nexblocks.authguard.service.mappers.ServiceMapper)2 ServiceMapperImpl (com.nexblocks.authguard.service.mappers.ServiceMapperImpl)2 AccountBO (com.nexblocks.authguard.service.model.AccountBO)2 AuthResponseBO (com.nexblocks.authguard.service.model.AuthResponseBO)2 PermissionBO (com.nexblocks.authguard.service.model.PermissionBO)2 RequestContextBO (com.nexblocks.authguard.service.model.RequestContextBO)2 List (java.util.List)2 Optional (java.util.Optional)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 Supplier (java.util.function.Supplier)2 Collectors (java.util.stream.Collectors)2