Search in sources :

Example 1 with SessionBO

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

the class OAuthServiceTest method exchangeAuthorizationCodeAndGetAccount.

@Test
void exchangeAuthorizationCodeAndGetAccount() {
    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.getByExternalId("1")).thenReturn(Optional.of(AccountBO.builder().id("1").build()));
    final TokensResponse actual = oAuthService.exchangeAuthorizationCode("account_test", "random", "code").join();
    final TokensResponse expected = testIdentityServer.getSuccessResponse();
    expected.setAccountId("1");
    assertThat(actual).isEqualTo(expected);
}
Also used : TokensResponse(com.nexblocks.authguard.jwt.oauth.TokensResponse) SessionBO(com.nexblocks.authguard.service.model.SessionBO)

Example 2 with SessionBO

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

the class OAuthServiceTest method exchangeAuthorizationCode.

@Test
void exchangeAuthorizationCode() {
    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);
    });
    final TokensResponse actual = oAuthService.exchangeAuthorizationCode("test", "random", "code").join();
    final TokensResponse expected = testIdentityServer.getSuccessResponse();
    assertThat(actual).isEqualTo(expected);
}
Also used : TokensResponse(com.nexblocks.authguard.jwt.oauth.TokensResponse) SessionBO(com.nexblocks.authguard.service.model.SessionBO)

Example 3 with SessionBO

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

the class SessionsServiceImpl method create.

@Override
public SessionBO create(final SessionBO session) {
    final SessionDO sessionDO = serviceMapper.toDO(session);
    sessionDO.setId(ID.generate());
    sessionDO.setSessionToken(cryptographicRandom.base64Url(config.getRandomSize()));
    return sessionsRepository.save(sessionDO).thenApply(created -> {
        emb.publish(CHANNEL, Messages.created(created));
        return serviceMapper.toBO(created);
    }).join();
}
Also used : SessionsService(com.nexblocks.authguard.service.SessionsService) ServiceMapper(com.nexblocks.authguard.service.mappers.ServiceMapper) SessionsConfig(com.nexblocks.authguard.service.config.SessionsConfig) SessionsRepository(com.nexblocks.authguard.dal.cache.SessionsRepository) SessionDO(com.nexblocks.authguard.dal.model.SessionDO) MessageBus(com.nexblocks.authguard.emb.MessageBus) CryptographicRandom(com.nexblocks.authguard.service.random.CryptographicRandom) Inject(com.google.inject.Inject) Messages(com.nexblocks.authguard.emb.Messages) SessionBO(com.nexblocks.authguard.service.model.SessionBO) Optional(java.util.Optional) ConfigContext(com.nexblocks.authguard.config.ConfigContext) Named(com.google.inject.name.Named) ID(com.nexblocks.authguard.service.util.ID) SessionDO(com.nexblocks.authguard.dal.model.SessionDO)

Example 4 with SessionBO

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

the class SessionsServiceImplTest method deleteByToken.

@Test
void deleteByToken() {
    final SessionDO sessionDO = SessionDO.builder().id("session-id").accountId("account").sessionToken("token").data(Collections.singletonMap("key", "value")).build();
    Mockito.when(repository.deleteByToken(sessionDO.getSessionToken())).thenReturn(CompletableFuture.completedFuture(Optional.of(sessionDO)));
    final SessionBO expected = serviceMapper.toBO(sessionDO);
    final Optional<SessionBO> actual = service.deleteByToken(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 5 with SessionBO

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

the class OAuthServiceTest method exchangeAuthorizationCodeExpiredState.

@Test
void exchangeAuthorizationCodeExpiredState() {
    Mockito.when(sessionsService.getByToken(Mockito.any())).thenAnswer(invocation -> {
        final SessionBO session = SessionBO.builder().sessionToken(invocation.getArgument(0)).expiresAt(OffsetDateTime.now().minus(Duration.ofMinutes(2))).build();
        return Optional.of(session);
    });
    assertThatThrownBy(() -> oAuthService.exchangeAuthorizationCode("test", "random", "code").join()).hasCauseInstanceOf(ServiceAuthorizationException.class);
}
Also used : 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