Search in sources :

Example 1 with AccountTokensRepository

use of com.nexblocks.authguard.dal.cache.AccountTokensRepository in project AuthGuard by AuthGuard.

the class AuthorizationCodeVerifierTest method expiredToken.

@Test
void expiredToken() {
    final AccountTokensRepository accountTokensRepository = Mockito.mock(AccountTokensRepository.class);
    final AuthorizationCodeVerifier authorizationCodeVerifier = new AuthorizationCodeVerifier(accountTokensRepository);
    final String accountId = "account-id";
    final String authorizationCode = "authorization-code";
    final AccountTokenDO accountToken = AccountTokenDO.builder().expiresAt(OffsetDateTime.now().minus(Duration.ofMinutes(5))).associatedAccountId(accountId).token(authorizationCode).build();
    Mockito.when(accountTokensRepository.getByToken(authorizationCode)).thenReturn(CompletableFuture.completedFuture(Optional.of(accountToken)));
    assertThatThrownBy(() -> authorizationCodeVerifier.verifyAccountToken(authorizationCode)).isInstanceOf(ServiceAuthorizationException.class);
}
Also used : AccountTokensRepository(com.nexblocks.authguard.dal.cache.AccountTokensRepository) AccountTokenDO(com.nexblocks.authguard.dal.model.AccountTokenDO) Test(org.junit.jupiter.api.Test)

Example 2 with AccountTokensRepository

use of com.nexblocks.authguard.dal.cache.AccountTokensRepository in project AuthGuard by AuthGuard.

the class CredentialsServiceImplTest method setup.

@BeforeEach
void setup() {
    accountsService = Mockito.mock(AccountsService.class);
    idempotencyService = Mockito.mock(IdempotencyService.class);
    credentialsRepository = Mockito.mock(CredentialsRepository.class);
    credentialsAuditRepository = Mockito.mock(CredentialsAuditRepository.class);
    accountTokensRepository = Mockito.mock(AccountTokensRepository.class);
    securePassword = Mockito.mock(SecurePassword.class);
    securePasswordProvider = Mockito.mock(SecurePasswordProvider.class);
    messageBus = Mockito.mock(MessageBus.class);
    serviceMapper = new ServiceMapperImpl();
    Mockito.when(securePasswordProvider.get()).thenReturn(securePassword);
    Mockito.when(securePasswordProvider.getCurrentVersion()).thenReturn(1);
    final PasswordValidator passwordValidator = new PasswordValidator(PasswordsConfig.builder().conditions(PasswordConditions.builder().build()).build());
    credentialsService = new CredentialsServiceImpl(accountsService, idempotencyService, credentialsRepository, credentialsAuditRepository, accountTokensRepository, securePasswordProvider, passwordValidator, messageBus, serviceMapper);
    Mockito.when(accountsService.getById(any())).thenReturn(Optional.of(RANDOM.nextObject(AccountBO.class)));
}
Also used : MessageBus(com.nexblocks.authguard.emb.MessageBus) CredentialsRepository(com.nexblocks.authguard.dal.persistence.CredentialsRepository) AccountTokensRepository(com.nexblocks.authguard.dal.cache.AccountTokensRepository) SecurePasswordProvider(com.nexblocks.authguard.basic.passwords.SecurePasswordProvider) ServiceMapperImpl(com.nexblocks.authguard.service.mappers.ServiceMapperImpl) AccountsService(com.nexblocks.authguard.service.AccountsService) SecurePassword(com.nexblocks.authguard.basic.passwords.SecurePassword) PasswordValidator(com.nexblocks.authguard.basic.passwords.PasswordValidator) IdempotencyService(com.nexblocks.authguard.service.IdempotencyService) CredentialsAuditRepository(com.nexblocks.authguard.dal.persistence.CredentialsAuditRepository) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 3 with AccountTokensRepository

use of com.nexblocks.authguard.dal.cache.AccountTokensRepository in project AuthGuard by AuthGuard.

the class AccessTokenProviderTest method newProviderInstance.

private AccessTokenProvider newProviderInstance(final JwtConfig jwtConfig, final StrategyConfig strategyConfig) {
    jtiProvider = Mockito.mock(JtiProvider.class);
    accountTokensRepository = Mockito.mock(AccountTokensRepository.class);
    tokenEncryptor = Mockito.mock(TokenEncryptorAdapter.class);
    Mockito.when(accountTokensRepository.save(Mockito.any())).thenAnswer(invocation -> {
        final AccountTokenDO arg = invocation.getArgument(0);
        return CompletableFuture.completedFuture(arg);
    });
    return new AccessTokenProvider(accountTokensRepository, jwtConfig, strategyConfig, jtiProvider, tokenEncryptor, new ServiceMapperImpl());
}
Also used : AccountTokensRepository(com.nexblocks.authguard.dal.cache.AccountTokensRepository) AccountTokenDO(com.nexblocks.authguard.dal.model.AccountTokenDO) ServiceMapperImpl(com.nexblocks.authguard.service.mappers.ServiceMapperImpl) TokenEncryptorAdapter(com.nexblocks.authguard.jwt.crypto.TokenEncryptorAdapter)

Example 4 with AccountTokensRepository

use of com.nexblocks.authguard.dal.cache.AccountTokensRepository in project AuthGuard by AuthGuard.

the class VerificationSubscriberTest method setup.

@BeforeEach
void setup() {
    emailProvider = Mockito.mock(EmailProvider.class);
    accountTokensRepository = Mockito.mock(AccountTokensRepository.class);
    final ConfigContext configContext = Mockito.mock(ConfigContext.class);
    final ImmutableVerificationConfig verificationConfig = ImmutableVerificationConfig.builder().emailVerificationLife("1d").build();
    Mockito.when(configContext.asConfigBean(ImmutableVerificationConfig.class)).thenReturn(verificationConfig);
    verificationSubscriber = new VerificationSubscriber(emailProvider, accountTokensRepository, configContext);
}
Also used : EmailProvider(com.nexblocks.authguard.external.email.EmailProvider) AccountTokensRepository(com.nexblocks.authguard.dal.cache.AccountTokensRepository) ConfigContext(com.nexblocks.authguard.config.ConfigContext) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 5 with AccountTokensRepository

use of com.nexblocks.authguard.dal.cache.AccountTokensRepository in project AuthGuard by AuthGuard.

the class AuthorizationCodeProviderTest method generateToken.

@Test
void generateToken() {
    final AccountTokensRepository accountTokensRepository = Mockito.mock(AccountTokensRepository.class);
    final AuthorizationCodeProvider authorizationCodeProvider = new AuthorizationCodeProvider(accountTokensRepository, new ServiceMapperImpl(), config());
    final AccountBO account = AccountBO.builder().id("account-id").build();
    final AuthResponseBO tokens = authorizationCodeProvider.generateToken(account);
    assertThat(tokens.getType()).isEqualTo("authorizationCode");
    assertThat(tokens.getToken()).isNotNull();
    assertThat(tokens.getRefreshToken()).isNull();
    final ArgumentCaptor<AccountTokenDO> argCaptor = ArgumentCaptor.forClass(AccountTokenDO.class);
    Mockito.verify(accountTokensRepository, Mockito.times(1)).save(argCaptor.capture());
    assertThat(argCaptor.getValue().getToken()).isEqualTo(tokens.getToken());
    assertThat(argCaptor.getValue().getAssociatedAccountId()).isEqualTo(account.getId());
    assertThat(argCaptor.getValue().getExpiresAt()).isAfter(OffsetDateTime.now()).isBefore(OffsetDateTime.now().plus(Duration.ofMinutes(6)));
}
Also used : AccountBO(com.nexblocks.authguard.service.model.AccountBO) AccountTokensRepository(com.nexblocks.authguard.dal.cache.AccountTokensRepository) ServiceMapperImpl(com.nexblocks.authguard.service.mappers.ServiceMapperImpl) AccountTokenDO(com.nexblocks.authguard.dal.model.AccountTokenDO) AuthResponseBO(com.nexblocks.authguard.service.model.AuthResponseBO) Test(org.junit.jupiter.api.Test)

Aggregations

AccountTokensRepository (com.nexblocks.authguard.dal.cache.AccountTokensRepository)7 AccountTokenDO (com.nexblocks.authguard.dal.model.AccountTokenDO)4 Test (org.junit.jupiter.api.Test)4 ServiceMapperImpl (com.nexblocks.authguard.service.mappers.ServiceMapperImpl)3 BeforeEach (org.junit.jupiter.api.BeforeEach)2 PasswordValidator (com.nexblocks.authguard.basic.passwords.PasswordValidator)1 SecurePassword (com.nexblocks.authguard.basic.passwords.SecurePassword)1 SecurePasswordProvider (com.nexblocks.authguard.basic.passwords.SecurePasswordProvider)1 ConfigContext (com.nexblocks.authguard.config.ConfigContext)1 CredentialsAuditRepository (com.nexblocks.authguard.dal.persistence.CredentialsAuditRepository)1 CredentialsRepository (com.nexblocks.authguard.dal.persistence.CredentialsRepository)1 MessageBus (com.nexblocks.authguard.emb.MessageBus)1 EmailProvider (com.nexblocks.authguard.external.email.EmailProvider)1 TokenEncryptorAdapter (com.nexblocks.authguard.jwt.crypto.TokenEncryptorAdapter)1 AccountsService (com.nexblocks.authguard.service.AccountsService)1 IdempotencyService (com.nexblocks.authguard.service.IdempotencyService)1 ServiceAuthorizationException (com.nexblocks.authguard.service.exceptions.ServiceAuthorizationException)1 AccountBO (com.nexblocks.authguard.service.model.AccountBO)1 AuthResponseBO (com.nexblocks.authguard.service.model.AuthResponseBO)1