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);
}
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);
}
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());
}
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);
}
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());
}
Aggregations