Search in sources :

Example 6 with AccountBO

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

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

use of com.nexblocks.authguard.service.model.AccountBO 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)

Example 9 with AccountBO

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

the class IdTokenProviderTest method generate.

@Test
void generate() {
    final IdTokenProvider idTokenProvider = newProviderInstance(jwtConfig());
    final AccountBO account = RANDOM.nextObject(AccountBO.class).withActive(true);
    final AuthResponseBO tokens = idTokenProvider.generateToken(account);
    assertThat(tokens).isNotNull();
    assertThat(tokens.getToken()).isNotNull();
    assertThat(tokens.getRefreshToken()).isNotNull();
    assertThat(tokens.getToken()).isNotEqualTo(tokens.getRefreshToken());
    verifyToken(tokens.getToken().toString(), account.getId(), null, null, null);
}
Also used : AccountBO(com.nexblocks.authguard.service.model.AccountBO) AuthResponseBO(com.nexblocks.authguard.service.model.AuthResponseBO) Test(org.junit.jupiter.api.Test)

Example 10 with AccountBO

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

the class OtpProviderTest method generateToken.

@Test
void generateToken() {
    final OtpConfig otpConfig = OtpConfig.builder().mode(OtpMode.ALPHANUMERIC).length(6).lifeTime("5m").build();
    setup(otpConfig);
    final AccountBO account = random.nextObject(AccountBO.class).withActive(true);
    final AuthResponseBO expected = AuthResponseBO.builder().type("otp").entityType(EntityType.ACCOUNT).entityId(account.getId()).build();
    final AuthResponseBO generated = otpProvider.generateToken(account);
    assertThat(generated).isEqualToIgnoringGivenFields(expected, "token");
    assertThat(generated.getToken()).isNotNull();
    final ArgumentCaptor<OneTimePasswordDO> argumentCaptor = ArgumentCaptor.forClass(OneTimePasswordDO.class);
    Mockito.verify(mockOtpRepository).save(argumentCaptor.capture());
    final OneTimePasswordDO persisted = argumentCaptor.getValue();
    assertThat(persisted.getAccountId()).isEqualTo(account.getId());
    assertThat(persisted.getExpiresAt()).isAfter(OffsetDateTime.now()).isBefore(OffsetDateTime.now().plus(Duration.ofMinutes(6)));
    assertThat(persisted.getId()).isNotNull();
    assertThat(persisted.getPassword()).isNotNull();
    assertThat(persisted.getPassword()).hasSize(6);
    Mockito.verify(messageBus, Mockito.times(1)).publish(eq("otp"), any());
}
Also used : AccountBO(com.nexblocks.authguard.service.model.AccountBO) AuthResponseBO(com.nexblocks.authguard.service.model.AuthResponseBO) OtpConfig(com.nexblocks.authguard.basic.config.OtpConfig) OneTimePasswordDO(com.nexblocks.authguard.dal.model.OneTimePasswordDO) Test(org.junit.jupiter.api.Test)

Aggregations

AccountBO (com.nexblocks.authguard.service.model.AccountBO)55 Test (org.junit.jupiter.api.Test)43 AccountTokenDO (com.nexblocks.authguard.dal.model.AccountTokenDO)21 Message (com.nexblocks.authguard.emb.model.Message)15 AuthResponseBO (com.nexblocks.authguard.service.model.AuthResponseBO)15 OtpMessageBody (com.nexblocks.authguard.basic.otp.OtpMessageBody)8 PasswordlessMessageBody (com.nexblocks.authguard.basic.passwordless.PasswordlessMessageBody)8 OneTimePasswordBO (com.nexblocks.authguard.service.model.OneTimePasswordBO)8 ServiceAuthorizationException (com.nexblocks.authguard.service.exceptions.ServiceAuthorizationException)7 DecodedJWT (com.auth0.jwt.interfaces.DecodedJWT)6 ImmutableTextMessage (com.nexblocks.authguard.external.sms.ImmutableTextMessage)6 RequestContextBO (com.nexblocks.authguard.service.model.RequestContextBO)6 ImmutableEmail (com.nexblocks.authguard.external.email.ImmutableEmail)5 JwtConfig (com.nexblocks.authguard.service.config.JwtConfig)5 StrategyConfig (com.nexblocks.authguard.service.config.StrategyConfig)5 ServiceException (com.nexblocks.authguard.service.exceptions.ServiceException)5 AuthRequestBO (com.nexblocks.authguard.service.model.AuthRequestBO)5 CreateAccountRequestDTO (com.nexblocks.authguard.api.dto.requests.CreateAccountRequestDTO)4 OtpConfig (com.nexblocks.authguard.basic.config.OtpConfig)4 CreateCompleteAccountRequestDTO (com.nexblocks.authguard.api.dto.requests.CreateCompleteAccountRequestDTO)3