Search in sources :

Example 1 with OneTimePasswordBO

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

the class OtpVerifier method verifyAccountToken.

@Override
public Either<Exception, String> verifyAccountToken(final String token) {
    // TODO: no need to have a special format for the token, just receive the two parts in the request
    final String[] parts = token.split(":");
    if (parts.length != 2) {
        return Either.left(new ServiceAuthorizationException(ErrorCode.INVALID_AUTHORIZATION_FORMAT, "Invalid OTP token format"));
    }
    final String passwordId = parts[0];
    final String otp = parts[1];
    final Optional<OneTimePasswordBO> generatedOpt = otpRepository.getById(passwordId).thenApply(optional -> optional.map(serviceMapper::toBO)).join();
    if (generatedOpt.isPresent()) {
        final OneTimePasswordBO generated = generatedOpt.get();
        if (generated.getExpiresAt().isBefore(OffsetDateTime.now())) {
            return Either.left(new ServiceAuthorizationException(ErrorCode.EXPIRED_TOKEN, "OTP " + passwordId + " has expired", EntityType.ACCOUNT, generated.getAccountId()));
        }
        if (generated.getPassword().equals(otp)) {
            return Either.right(generated.getAccountId());
        } else {
            return Either.left(new ServiceAuthorizationException(ErrorCode.PASSWORDS_DO_NOT_MATCH, "OTP " + passwordId + " values did not match", EntityType.ACCOUNT, generated.getAccountId()));
        }
    } else {
        return Either.left(new ServiceAuthorizationException(ErrorCode.INVALID_TOKEN, "Invalid OTP ID"));
    }
}
Also used : ServiceMapper(com.nexblocks.authguard.service.mappers.ServiceMapper) ServiceAuthorizationException(com.nexblocks.authguard.service.exceptions.ServiceAuthorizationException) ErrorCode(com.nexblocks.authguard.service.exceptions.codes.ErrorCode) OffsetDateTime(java.time.OffsetDateTime) AuthVerifier(com.nexblocks.authguard.service.auth.AuthVerifier) EntityType(com.nexblocks.authguard.service.model.EntityType) Either(io.vavr.control.Either) Inject(com.google.inject.Inject) Optional(java.util.Optional) OneTimePasswordBO(com.nexblocks.authguard.service.model.OneTimePasswordBO) OtpRepository(com.nexblocks.authguard.dal.cache.OtpRepository) ServiceAuthorizationException(com.nexblocks.authguard.service.exceptions.ServiceAuthorizationException) OneTimePasswordBO(com.nexblocks.authguard.service.model.OneTimePasswordBO)

Example 2 with OneTimePasswordBO

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

the class EmailOtpSubscriber method onMessage.

@Override
public void onMessage(final Message message) {
    if (message.getEventType() == EventType.OTP_GENERATED) {
        final OtpMessageBody messageBody = (OtpMessageBody) message.getMessageBody();
        final AccountBO account = messageBody.getAccount();
        final OneTimePasswordBO otp = messageBody.getOtp();
        if (messageBody.isByEmail()) {
            sendEmail(account, otp);
        } else {
            LOG.warn("Email OTP subscriber is enabled but a OTP event was received not to be sent by email");
        }
    }
}
Also used : OtpMessageBody(com.nexblocks.authguard.basic.otp.OtpMessageBody) AccountBO(com.nexblocks.authguard.service.model.AccountBO) OneTimePasswordBO(com.nexblocks.authguard.service.model.OneTimePasswordBO)

Example 3 with OneTimePasswordBO

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

the class EmailOtpSubscriberTest method onValidMessageNoEmail.

@Test
void onValidMessageNoEmail() {
    final OneTimePasswordBO otp = OneTimePasswordBO.builder().password("password").build();
    final AccountBO account = AccountBO.builder().build();
    final OtpMessageBody messageBody = new OtpMessageBody(otp, account, true, false);
    final Message message = Messages.otpGenerated(messageBody);
    otpSubscriber.onMessage(message);
    Mockito.verify(emailProvider, Mockito.never()).send(Mockito.any());
}
Also used : AccountBO(com.nexblocks.authguard.service.model.AccountBO) OtpMessageBody(com.nexblocks.authguard.basic.otp.OtpMessageBody) Message(com.nexblocks.authguard.emb.model.Message) OneTimePasswordBO(com.nexblocks.authguard.service.model.OneTimePasswordBO) Test(org.junit.jupiter.api.Test)

Example 4 with OneTimePasswordBO

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

the class EmailOtpSubscriberTest method onValidMessage.

@Test
void onValidMessage() {
    final OneTimePasswordBO otp = OneTimePasswordBO.builder().password("password").build();
    final AccountBO account = AccountBO.builder().email(AccountEmailBO.builder().email("user@test.net").build()).firstName("first").lastName("second").build();
    final OtpMessageBody messageBody = new OtpMessageBody(otp, account, true, false);
    final Message message = Messages.otpGenerated(messageBody);
    final ImmutableEmail expectedEmail = ImmutableEmail.builder().template("otp").to(account.getEmail().getEmail()).parameters(ImmutableMap.of("password", otp.getPassword(), "firstName", account.getFirstName(), "lastName", account.getLastName())).build();
    otpSubscriber.onMessage(message);
    final ArgumentCaptor<ImmutableEmail> sentEmailCaptor = ArgumentCaptor.forClass(ImmutableEmail.class);
    Mockito.verify(emailProvider).send(sentEmailCaptor.capture());
    assertThat(sentEmailCaptor.getValue()).isEqualTo(expectedEmail);
}
Also used : AccountBO(com.nexblocks.authguard.service.model.AccountBO) OtpMessageBody(com.nexblocks.authguard.basic.otp.OtpMessageBody) Message(com.nexblocks.authguard.emb.model.Message) OneTimePasswordBO(com.nexblocks.authguard.service.model.OneTimePasswordBO) ImmutableEmail(com.nexblocks.authguard.external.email.ImmutableEmail) Test(org.junit.jupiter.api.Test)

Example 5 with OneTimePasswordBO

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

the class EmailOtpSubscriberTest method onWrongMessageType.

@Test
void onWrongMessageType() {
    final OneTimePasswordBO otp = OneTimePasswordBO.builder().password("password").build();
    final AccountBO account = AccountBO.builder().email(AccountEmailBO.builder().email("user@test.net").build()).build();
    final OtpMessageBody messageBody = new OtpMessageBody(otp, account, true, false);
    final Message message = Messages.otpGenerated(messageBody).withEventType(EventType.ADMIN);
    otpSubscriber.onMessage(message);
    Mockito.verify(emailProvider, Mockito.never()).send(Mockito.any());
}
Also used : AccountBO(com.nexblocks.authguard.service.model.AccountBO) OtpMessageBody(com.nexblocks.authguard.basic.otp.OtpMessageBody) Message(com.nexblocks.authguard.emb.model.Message) OneTimePasswordBO(com.nexblocks.authguard.service.model.OneTimePasswordBO) Test(org.junit.jupiter.api.Test)

Aggregations

OneTimePasswordBO (com.nexblocks.authguard.service.model.OneTimePasswordBO)9 OtpMessageBody (com.nexblocks.authguard.basic.otp.OtpMessageBody)8 AccountBO (com.nexblocks.authguard.service.model.AccountBO)8 Message (com.nexblocks.authguard.emb.model.Message)6 Test (org.junit.jupiter.api.Test)6 ImmutableTextMessage (com.nexblocks.authguard.external.sms.ImmutableTextMessage)3 Inject (com.google.inject.Inject)1 OtpRepository (com.nexblocks.authguard.dal.cache.OtpRepository)1 ImmutableEmail (com.nexblocks.authguard.external.email.ImmutableEmail)1 AuthVerifier (com.nexblocks.authguard.service.auth.AuthVerifier)1 ServiceAuthorizationException (com.nexblocks.authguard.service.exceptions.ServiceAuthorizationException)1 ErrorCode (com.nexblocks.authguard.service.exceptions.codes.ErrorCode)1 ServiceMapper (com.nexblocks.authguard.service.mappers.ServiceMapper)1 EntityType (com.nexblocks.authguard.service.model.EntityType)1 Either (io.vavr.control.Either)1 OffsetDateTime (java.time.OffsetDateTime)1 Optional (java.util.Optional)1