Search in sources :

Example 6 with SessionBO

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

the class OAuthServiceTest method exchangeAuthorizationCodeAndCreateAccount.

@Test
void exchangeAuthorizationCodeAndCreateAccount() {
    final RequestContextBO expectedContext = RequestContextBO.builder().idempotentKey("code").source("account_test").build();
    Mockito.when(sessionsService.getByToken(Mockito.any())).thenAnswer(invocation -> {
        final SessionBO session = SessionBO.builder().sessionToken(invocation.getArgument(0)).expiresAt(OffsetDateTime.now().plus(Duration.ofMinutes(2))).build();
        return Optional.of(session);
    });
    Mockito.when(accountsService.create(Mockito.any(), Mockito.eq(expectedContext))).thenAnswer(invocation -> invocation.getArgument(0, AccountBO.class).withId("1"));
    final TokensResponse actual = oAuthService.exchangeAuthorizationCode("account_test", "random", "code").join();
    final TokensResponse expected = testIdentityServer.getSuccessResponse();
    expected.setAccountId("1");
    assertThat(actual).isEqualTo(expected);
}
Also used : RequestContextBO(com.nexblocks.authguard.service.model.RequestContextBO) TokensResponse(com.nexblocks.authguard.jwt.oauth.TokensResponse) SessionBO(com.nexblocks.authguard.service.model.SessionBO)

Example 7 with SessionBO

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

the class SessionsServiceImplTest method getByToken.

@Test
void getByToken() {
    final SessionDO sessionDO = SessionDO.builder().id("session-id").accountId("account").sessionToken("token").data(Collections.singletonMap("key", "value")).build();
    Mockito.when(repository.getByToken(sessionDO.getSessionToken())).thenReturn(CompletableFuture.completedFuture(Optional.of(sessionDO)));
    final SessionBO expected = serviceMapper.toBO(sessionDO);
    final Optional<SessionBO> actual = service.getByToken(sessionDO.getSessionToken());
    assertThat(actual).contains(expected);
}
Also used : SessionDO(com.nexblocks.authguard.dal.model.SessionDO) SessionBO(com.nexblocks.authguard.service.model.SessionBO) Test(org.junit.jupiter.api.Test)

Example 8 with SessionBO

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

the class SessionsServiceImplTest method create.

@Test
void create() {
    final SessionBO sessionBO = SessionBO.builder().accountId("account").data(Collections.singletonMap("key", "value")).build();
    Mockito.when(repository.save(Mockito.any())).thenAnswer(invocation -> CompletableFuture.completedFuture(invocation.getArgument(0, SessionDO.class)));
    final SessionBO created = service.create(sessionBO);
    assertThat(created).isEqualToIgnoringGivenFields(sessionBO, "id", "sessionToken");
    assertThat(created.getId()).isNotNull();
    Mockito.verify(repository).save(Mockito.any());
    Mockito.verify(emb).publish(Mockito.eq("sessions"), Mockito.any());
}
Also used : SessionBO(com.nexblocks.authguard.service.model.SessionBO) Test(org.junit.jupiter.api.Test)

Example 9 with SessionBO

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

the class SessionsServiceImplTest method getById.

@Test
void getById() {
    final SessionDO sessionDO = SessionDO.builder().id("session-id").accountId("account").data(Collections.singletonMap("key", "value")).build();
    Mockito.when(repository.getById(sessionDO.getId())).thenReturn(CompletableFuture.completedFuture(Optional.of(sessionDO)));
    final SessionBO expected = serviceMapper.toBO(sessionDO);
    final Optional<SessionBO> actual = service.getById(sessionDO.getId());
    assertThat(actual).contains(expected);
}
Also used : SessionDO(com.nexblocks.authguard.dal.model.SessionDO) SessionBO(com.nexblocks.authguard.service.model.SessionBO) Test(org.junit.jupiter.api.Test)

Example 10 with SessionBO

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

the class SessionTokenToSession method exchange.

@Override
public Either<Exception, AuthResponseBO> exchange(final AuthRequestBO request) {
    final Optional<SessionBO> sessionOpt = sessionsService.getByToken(request.getToken());
    if (sessionOpt.isEmpty()) {
        return Either.left(new ServiceAuthorizationException(ErrorCode.INVALID_TOKEN, "Session token does not exist"));
    }
    final SessionBO session = sessionOpt.get();
    if (session.getExpiresAt().isBefore(OffsetDateTime.now())) {
        return Either.left(new ServiceAuthorizationException(ErrorCode.EXPIRED_TOKEN, "Session token has expired"));
    }
    return Either.right(AuthResponseBO.builder().type(TOKEN_TYPE).token(session).entityType(EntityType.ACCOUNT).entityId(session.getAccountId()).build());
}
Also used : ServiceAuthorizationException(com.nexblocks.authguard.service.exceptions.ServiceAuthorizationException) SessionBO(com.nexblocks.authguard.service.model.SessionBO)

Aggregations

SessionBO (com.nexblocks.authguard.service.model.SessionBO)12 Test (org.junit.jupiter.api.Test)6 SessionDO (com.nexblocks.authguard.dal.model.SessionDO)4 TokensResponse (com.nexblocks.authguard.jwt.oauth.TokensResponse)3 SessionsService (com.nexblocks.authguard.service.SessionsService)3 ServiceAuthorizationException (com.nexblocks.authguard.service.exceptions.ServiceAuthorizationException)3 Inject (com.google.inject.Inject)1 Named (com.google.inject.name.Named)1 ConfigContext (com.nexblocks.authguard.config.ConfigContext)1 SessionsRepository (com.nexblocks.authguard.dal.cache.SessionsRepository)1 MessageBus (com.nexblocks.authguard.emb.MessageBus)1 Messages (com.nexblocks.authguard.emb.Messages)1 SessionsConfig (com.nexblocks.authguard.service.config.SessionsConfig)1 ServiceMapper (com.nexblocks.authguard.service.mappers.ServiceMapper)1 RequestContextBO (com.nexblocks.authguard.service.model.RequestContextBO)1 CryptographicRandom (com.nexblocks.authguard.service.random.CryptographicRandom)1 ID (com.nexblocks.authguard.service.util.ID)1 Optional (java.util.Optional)1