Search in sources :

Example 41 with AccountBO

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

the class JwtPermissionsMapperTest method mapNoRestrictions.

@Test
void mapNoRestrictions() {
    final AccountBO account = AccountBO.builder().permissions(Arrays.asList(PermissionBO.builder().group("test").name("read").build(), PermissionBO.builder().group("test").name("write").build(), PermissionBO.builder().group("test").name("nothing").build())).build();
    final String[] expected = new String[] { "test:read", "test:write", "test:nothing" };
    final String[] actual = JwtPermissionsMapper.map(account, null);
    assertThat(Arrays.asList(actual)).isEqualTo(Arrays.asList(expected));
}
Also used : AccountBO(com.nexblocks.authguard.service.model.AccountBO) Test(org.junit.jupiter.api.Test)

Example 42 with AccountBO

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

the class JwtPermissionsMapperTest method mapNoPermissionRestrictions.

@Test
void mapNoPermissionRestrictions() {
    final AccountBO account = AccountBO.builder().permissions(Arrays.asList(PermissionBO.builder().group("test").name("read").build(), PermissionBO.builder().group("test").name("write").build(), PermissionBO.builder().group("test").name("nothing").build())).build();
    final TokenRestrictionsBO restrictions = TokenRestrictionsBO.builder().build();
    final String[] expected = new String[] { "test:read", "test:write", "test:nothing" };
    final String[] actual = JwtPermissionsMapper.map(account, restrictions);
    assertThat(Arrays.asList(actual)).isEqualTo(Arrays.asList(expected));
}
Also used : AccountBO(com.nexblocks.authguard.service.model.AccountBO) TokenRestrictionsBO(com.nexblocks.authguard.service.model.TokenRestrictionsBO) Test(org.junit.jupiter.api.Test)

Example 43 with AccountBO

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

the class VerificationSubscriber method doSendEmails.

private void doSendEmails(final VerificationRequestBO verificationRequest) {
    final AccountBO account = verificationRequest.getAccount();
    verificationRequest.getEmails().forEach(email -> {
        if (email == null) {
            LOG.warn("Email is null. Skipping.");
        } else if (email.isVerified()) {
            LOG.warn("Email is already verified. Skipping.");
        } else {
            final String token = generateVerificationString();
            final AccountTokenDO accountToken = AccountTokenDO.builder().expiresAt(OffsetDateTime.now().plus(tokenTtl)).associatedAccountId(account.getId()).token(token).additionalInformation(Collections.singletonMap("email", email.getEmail())).build();
            accountTokensRepository.save(accountToken);
            final ImmutableEmail email1 = ImmutableEmail.builder().template(EMAIL_TEMPLATE).to(email.getEmail()).putParameters("token", token).build();
            emailProvider.send(email1);
            LOG.info("Sent a verification email");
        }
    });
}
Also used : AccountBO(com.nexblocks.authguard.service.model.AccountBO) AccountTokenDO(com.nexblocks.authguard.dal.model.AccountTokenDO) ImmutableEmail(com.nexblocks.authguard.external.email.ImmutableEmail)

Example 44 with AccountBO

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

Example 45 with AccountBO

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

the class JwtTokenVerifierTest method validate.

@Test
void validate() {
    final StrategyConfig strategyConfig = strategyConfig(false);
    final JwtConfig jwtConfig = jwtConfig();
    final JwtTokenVerifier jwtTokenVerifier = newVerifierInstance(strategyConfig);
    final AccountBO account = RANDOM.nextObject(AccountBO.class);
    final AuthResponseBO tokens = generateToken(jwtConfig, account, null);
    final Either<Exception, DecodedJWT> validatedToken = jwtTokenVerifier.verify(tokens.getToken().toString());
    assertThat(validatedToken.isRight()).isTrue();
    verifyToken(validatedToken.get(), account.getId(), null, null, null);
}
Also used : AccountBO(com.nexblocks.authguard.service.model.AccountBO) JwtConfig(com.nexblocks.authguard.service.config.JwtConfig) StrategyConfig(com.nexblocks.authguard.service.config.StrategyConfig) AuthResponseBO(com.nexblocks.authguard.service.model.AuthResponseBO) DecodedJWT(com.auth0.jwt.interfaces.DecodedJWT) ServiceAuthorizationException(com.nexblocks.authguard.service.exceptions.ServiceAuthorizationException) 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