Search in sources :

Example 1 with AccountTokenDO

use of com.nexblocks.authguard.dal.model.AccountTokenDO in project AuthGuard by AuthGuard.

the class AuthorizationCodeToOidcTest method exchangeWithRestrictions.

@Test
void exchangeWithRestrictions() {
    final AuthRequestBO authRequest = AuthRequestBO.builder().token("auth code").build();
    final AccountTokenDO accountToken = AccountTokenDO.builder().associatedAccountId("account").tokenRestrictions(TokenRestrictionsDO.builder().scopes(Collections.emptySet()).permissions(new HashSet<>(Arrays.asList("perm-1", "perm-2"))).build()).build();
    final AccountBO account = AccountBO.builder().id("account").build();
    final AuthResponseBO authResponse = AuthResponseBO.builder().token("OIDC").build();
    Mockito.when(authorizationCodeVerifier.verifyAndGetAccountToken(authRequest.getToken())).thenReturn(Either.right(accountToken));
    Mockito.when(accountsService.getById(accountToken.getAssociatedAccountId())).thenReturn(Optional.of(account));
    Mockito.when(openIdConnectTokenProvider.generateToken(account, serviceMapper.toBO(accountToken.getTokenRestrictions()))).thenReturn(authResponse);
    final Either<Exception, AuthResponseBO> actual = authorizationCodeToOidc.exchange(authRequest);
    assertThat(actual.isRight());
    assertThat(actual.get()).isEqualTo(authResponse);
}
Also used : AccountBO(com.nexblocks.authguard.service.model.AccountBO) AccountTokenDO(com.nexblocks.authguard.dal.model.AccountTokenDO) AuthResponseBO(com.nexblocks.authguard.service.model.AuthResponseBO) AuthRequestBO(com.nexblocks.authguard.service.model.AuthRequestBO) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 2 with AccountTokenDO

use of com.nexblocks.authguard.dal.model.AccountTokenDO in project AuthGuard by AuthGuard.

the class AuthorizationCodeToOidcTest method exchange.

@Test
void exchange() {
    final AuthRequestBO authRequest = AuthRequestBO.builder().token("auth code").build();
    final AccountTokenDO accountToken = AccountTokenDO.builder().associatedAccountId("account").build();
    final AccountBO account = AccountBO.builder().id("account").build();
    final AuthResponseBO authResponse = AuthResponseBO.builder().token("OIDC").build();
    Mockito.when(authorizationCodeVerifier.verifyAndGetAccountToken(authRequest.getToken())).thenReturn(Either.right(accountToken));
    Mockito.when(accountsService.getById(accountToken.getAssociatedAccountId())).thenReturn(Optional.of(account));
    Mockito.when(openIdConnectTokenProvider.generateToken(account, (TokenRestrictionsBO) null)).thenReturn(authResponse);
    final Either<Exception, AuthResponseBO> actual = authorizationCodeToOidc.exchange(authRequest);
    assertThat(actual.isRight());
    assertThat(actual.get()).isEqualTo(authResponse);
}
Also used : AccountBO(com.nexblocks.authguard.service.model.AccountBO) AccountTokenDO(com.nexblocks.authguard.dal.model.AccountTokenDO) AuthResponseBO(com.nexblocks.authguard.service.model.AuthResponseBO) AuthRequestBO(com.nexblocks.authguard.service.model.AuthRequestBO) Test(org.junit.jupiter.api.Test)

Example 3 with AccountTokenDO

use of com.nexblocks.authguard.dal.model.AccountTokenDO in project AuthGuard by AuthGuard.

the class RefreshToAccessTokenTest method exchangeExpiredToken.

@Test
void exchangeExpiredToken() {
    // data
    final String accountId = "account";
    final String refreshToken = "refresh_token";
    final AuthRequestBO authRequest = AuthRequestBO.builder().token(refreshToken).build();
    final AccountTokenDO accountToken = AccountTokenDO.builder().token(refreshToken).associatedAccountId(accountId).expiresAt(OffsetDateTime.now().minusMinutes(1)).build();
    // mock
    Mockito.when(accountTokensRepository.getByToken(authRequest.getToken())).thenReturn(CompletableFuture.completedFuture(Optional.of(accountToken)));
    // do
    final Either<Exception, AuthResponseBO> actual = refreshToAccessToken.exchange(authRequest);
    // assert
    assertThat(actual.isLeft()).isTrue();
    assertThat(actual.left().get()).isInstanceOf(ServiceAuthorizationException.class);
    Mockito.verify(accountTokensRepository).deleteToken(refreshToken);
}
Also used : AccountTokenDO(com.nexblocks.authguard.dal.model.AccountTokenDO) AuthResponseBO(com.nexblocks.authguard.service.model.AuthResponseBO) AuthRequestBO(com.nexblocks.authguard.service.model.AuthRequestBO) ServiceAuthorizationException(com.nexblocks.authguard.service.exceptions.ServiceAuthorizationException) Test(org.junit.jupiter.api.Test)

Example 4 with AccountTokenDO

use of com.nexblocks.authguard.dal.model.AccountTokenDO in project AuthGuard by AuthGuard.

the class RefreshToAccessTokenTest method exchange.

@Test
void exchange() {
    // data
    final String accountId = "account";
    final String refreshToken = "refresh_token";
    final AuthRequestBO authRequest = AuthRequestBO.builder().token(refreshToken).build();
    final AccountTokenDO accountToken = AccountTokenDO.builder().token(refreshToken).associatedAccountId(accountId).expiresAt(OffsetDateTime.now().plusMinutes(1)).build();
    final AccountBO account = AccountBO.builder().id(accountId).build();
    final AuthResponseBO newTokens = AuthResponseBO.builder().token("new_token").refreshToken("new_refresh_token").build();
    // mock
    Mockito.when(accountTokensRepository.getByToken(authRequest.getToken())).thenReturn(CompletableFuture.completedFuture(Optional.of(accountToken)));
    Mockito.when(accountsService.getById(accountId)).thenReturn(Optional.of(account));
    Mockito.when(accessTokenProvider.generateToken(account, (TokenRestrictionsBO) null)).thenReturn(newTokens);
    // do
    final Either<Exception, AuthResponseBO> actual = refreshToAccessToken.exchange(authRequest);
    // assert
    assertThat(actual.isRight()).isTrue();
    assertThat(actual.right().get()).isEqualTo(newTokens);
    Mockito.verify(accountTokensRepository).deleteToken(refreshToken);
}
Also used : AccountBO(com.nexblocks.authguard.service.model.AccountBO) AccountTokenDO(com.nexblocks.authguard.dal.model.AccountTokenDO) AuthResponseBO(com.nexblocks.authguard.service.model.AuthResponseBO) AuthRequestBO(com.nexblocks.authguard.service.model.AuthRequestBO) ServiceAuthorizationException(com.nexblocks.authguard.service.exceptions.ServiceAuthorizationException) Test(org.junit.jupiter.api.Test)

Example 5 with AccountTokenDO

use of com.nexblocks.authguard.dal.model.AccountTokenDO in project AuthGuard by AuthGuard.

the class VerificationSubscriberTest method onMessage.

@Test
void onMessage() {
    final AccountBO account = AccountBO.builder().id("account-id").email(AccountEmailBO.builder().email("unverified").verified(false).build()).build();
    final VerificationRequestBO verificationRequest = VerificationRequestBO.builder().account(account).emails(Collections.singletonList(account.getEmail())).build();
    final Message<VerificationRequestBO> message = Message.<VerificationRequestBO>builder().eventType(EventType.EMAIL_VERIFICATION).bodyType(VerificationRequestBO.class).messageBody(verificationRequest).build();
    verificationSubscriber.onMessage(message);
    final ArgumentCaptor<AccountTokenDO> accountTokenCaptor = ArgumentCaptor.forClass(AccountTokenDO.class);
    final ArgumentCaptor<ImmutableEmail> emailCaptor = ArgumentCaptor.forClass(ImmutableEmail.class);
    Mockito.verify(accountTokensRepository).save(accountTokenCaptor.capture());
    Mockito.verify(emailProvider, Mockito.times(1)).send(emailCaptor.capture());
    final AccountTokenDO accountToken = accountTokenCaptor.getValue();
    final ImmutableEmail email = emailCaptor.getValue();
    assertThat(accountToken.getAssociatedAccountId()).isEqualTo(account.getId());
    assertThat(accountToken.getAdditionalInformation().get("email")).isEqualTo("unverified");
    assertThat(accountToken.getToken()).isNotNull();
    assertThat(accountToken.getExpiresAt()).isNotNull();
    assertThat(email.getTo()).isEqualTo("unverified");
    assertThat(email.getBody()).isNull();
    assertThat(email.getParameters()).containsOnlyKeys("token");
}
Also used : AccountBO(com.nexblocks.authguard.service.model.AccountBO) AccountTokenDO(com.nexblocks.authguard.dal.model.AccountTokenDO) VerificationRequestBO(com.nexblocks.authguard.service.model.VerificationRequestBO) ImmutableEmail(com.nexblocks.authguard.external.email.ImmutableEmail) Test(org.junit.jupiter.api.Test)

Aggregations

AccountTokenDO (com.nexblocks.authguard.dal.model.AccountTokenDO)36 Test (org.junit.jupiter.api.Test)22 AccountBO (com.nexblocks.authguard.service.model.AccountBO)21 Message (com.nexblocks.authguard.emb.model.Message)9 PasswordlessMessageBody (com.nexblocks.authguard.basic.passwordless.PasswordlessMessageBody)8 ServiceException (com.nexblocks.authguard.service.exceptions.ServiceException)8 AuthResponseBO (com.nexblocks.authguard.service.model.AuthResponseBO)7 AccountTokensRepository (com.nexblocks.authguard.dal.cache.AccountTokensRepository)6 AuthRequestBO (com.nexblocks.authguard.service.model.AuthRequestBO)6 ServiceAuthorizationException (com.nexblocks.authguard.service.exceptions.ServiceAuthorizationException)5 ResetTokenMessage (com.nexblocks.authguard.service.messaging.ResetTokenMessage)5 OffsetDateTime (java.time.OffsetDateTime)5 ImmutableEmail (com.nexblocks.authguard.external.email.ImmutableEmail)4 ImmutableTextMessage (com.nexblocks.authguard.external.sms.ImmutableTextMessage)3 ServiceNotFoundException (com.nexblocks.authguard.service.exceptions.ServiceNotFoundException)3 ActionTokenBO (com.nexblocks.authguard.service.model.ActionTokenBO)3 Optional (java.util.Optional)3 Inject (com.google.inject.Inject)2 AccountsService (com.nexblocks.authguard.service.AccountsService)2 ErrorCode (com.nexblocks.authguard.service.exceptions.codes.ErrorCode)2