Search in sources :

Example 1 with ResetTokenMessage

use of com.nexblocks.authguard.service.messaging.ResetTokenMessage in project AuthGuard by AuthGuard.

the class CredentialsServiceImpl method generateResetToken.

@Override
public PasswordResetTokenBO generateResetToken(final String identifier, final boolean returnToken, final String domain) {
    final CredentialsBO credentials = getByUsername(identifier, domain).orElseThrow(() -> new ServiceNotFoundException(ErrorCode.CREDENTIALS_DOES_NOT_EXIST, "Unknown identifier"));
    final AccountBO account = accountsService.getById(credentials.getAccountId()).orElseThrow(() -> new ServiceException(ErrorCode.ACCOUNT_DOES_NOT_EXIST, "Credentials found for the identifier but no account was associated with it. This could be the " + "result of deleting an account without deleting its credentials"));
    final OffsetDateTime now = OffsetDateTime.now();
    final AccountTokenDO accountToken = AccountTokenDO.builder().id(ID.generate()).token(cryptographicRandom.base64Url(RESET_TOKEN_SIZE)).associatedAccountId(account.getId()).additionalInformation(ImmutableMap.of("credentialsId", credentials.getId())).expiresAt(now.plus(TOKEN_LIFETIME)).build();
    accountTokensRepository.save(accountToken).join();
    messageBus.publish(CREDENTIALS_CHANNEL, Messages.resetTokenGenerated(new ResetTokenMessage(account, accountToken)));
    return PasswordResetTokenBO.builder().token(returnToken ? accountToken.getToken() : null).issuedAt(now.toEpochSecond()).expiresAt(accountToken.getExpiresAt().toEpochSecond()).build();
}
Also used : ServiceException(com.nexblocks.authguard.service.exceptions.ServiceException) OffsetDateTime(java.time.OffsetDateTime) ServiceNotFoundException(com.nexblocks.authguard.service.exceptions.ServiceNotFoundException) AccountTokenDO(com.nexblocks.authguard.dal.model.AccountTokenDO) ResetTokenMessage(com.nexblocks.authguard.service.messaging.ResetTokenMessage)

Example 2 with ResetTokenMessage

use of com.nexblocks.authguard.service.messaging.ResetTokenMessage in project AuthGuard by AuthGuard.

the class EmailResetTokenSubscriberTest method onValidMessageNoEmail.

@Test
void onValidMessageNoEmail() {
    final AccountTokenDO accountToken = AccountTokenDO.builder().token("token").build();
    final AccountBO account = AccountBO.builder().build();
    final ResetTokenMessage messageBody = new ResetTokenMessage(account, accountToken);
    final Message message = Messages.passwordlessGenerated(messageBody);
    emailResetTokenSubscriber.onMessage(message);
    Mockito.verify(emailProvider, Mockito.never()).send(Mockito.any());
}
Also used : AccountBO(com.nexblocks.authguard.service.model.AccountBO) ResetTokenMessage(com.nexblocks.authguard.service.messaging.ResetTokenMessage) Message(com.nexblocks.authguard.emb.model.Message) AccountTokenDO(com.nexblocks.authguard.dal.model.AccountTokenDO) ResetTokenMessage(com.nexblocks.authguard.service.messaging.ResetTokenMessage) Test(org.junit.jupiter.api.Test)

Example 3 with ResetTokenMessage

use of com.nexblocks.authguard.service.messaging.ResetTokenMessage in project AuthGuard by AuthGuard.

the class EmailResetTokenSubscriberTest method onValidMessage.

@Test
void onValidMessage() {
    final AccountTokenDO accountToken = AccountTokenDO.builder().token("token").build();
    final AccountBO account = AccountBO.builder().email(AccountEmailBO.builder().email("user@test.net").build()).firstName("first").lastName("second").build();
    final ResetTokenMessage messageBody = new ResetTokenMessage(account, accountToken);
    final Message message = Messages.resetTokenGenerated(messageBody);
    final ImmutableEmail expectedEmail = ImmutableEmail.builder().template("passwordReset").to(account.getEmail().getEmail()).parameters(ImmutableMap.of("token", accountToken.getToken(), "firstName", account.getFirstName(), "lastName", account.getLastName())).build();
    emailResetTokenSubscriber.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) ResetTokenMessage(com.nexblocks.authguard.service.messaging.ResetTokenMessage) Message(com.nexblocks.authguard.emb.model.Message) AccountTokenDO(com.nexblocks.authguard.dal.model.AccountTokenDO) ImmutableEmail(com.nexblocks.authguard.external.email.ImmutableEmail) ResetTokenMessage(com.nexblocks.authguard.service.messaging.ResetTokenMessage) Test(org.junit.jupiter.api.Test)

Example 4 with ResetTokenMessage

use of com.nexblocks.authguard.service.messaging.ResetTokenMessage in project AuthGuard by AuthGuard.

the class EmailResetTokenSubscriber method onMessage.

@Override
public void onMessage(final Message message) {
    if (message.getEventType() == EventType.RESET_TOKEN_GENERATED) {
        final ResetTokenMessage accountToken = (ResetTokenMessage) message.getMessageBody();
        sendEmail(accountToken.getAccount(), accountToken.getAccountToken());
    }
}
Also used : ResetTokenMessage(com.nexblocks.authguard.service.messaging.ResetTokenMessage)

Example 5 with ResetTokenMessage

use of com.nexblocks.authguard.service.messaging.ResetTokenMessage in project AuthGuard by AuthGuard.

the class EmailResetTokenSubscriberTest method onWrongMessageType.

@Test
void onWrongMessageType() {
    final AccountTokenDO accountToken = AccountTokenDO.builder().token("token").build();
    final AccountBO account = AccountBO.builder().email(AccountEmailBO.builder().email("user@test.net").build()).build();
    final ResetTokenMessage messageBody = new ResetTokenMessage(account, accountToken);
    final Message message = Messages.passwordlessGenerated(messageBody).withEventType(EventType.ADMIN);
    emailResetTokenSubscriber.onMessage(message);
    Mockito.verify(emailProvider, Mockito.never()).send(Mockito.any());
}
Also used : AccountBO(com.nexblocks.authguard.service.model.AccountBO) ResetTokenMessage(com.nexblocks.authguard.service.messaging.ResetTokenMessage) Message(com.nexblocks.authguard.emb.model.Message) AccountTokenDO(com.nexblocks.authguard.dal.model.AccountTokenDO) ResetTokenMessage(com.nexblocks.authguard.service.messaging.ResetTokenMessage) Test(org.junit.jupiter.api.Test)

Aggregations

ResetTokenMessage (com.nexblocks.authguard.service.messaging.ResetTokenMessage)5 AccountTokenDO (com.nexblocks.authguard.dal.model.AccountTokenDO)4 Message (com.nexblocks.authguard.emb.model.Message)3 AccountBO (com.nexblocks.authguard.service.model.AccountBO)3 Test (org.junit.jupiter.api.Test)3 ImmutableEmail (com.nexblocks.authguard.external.email.ImmutableEmail)1 ServiceException (com.nexblocks.authguard.service.exceptions.ServiceException)1 ServiceNotFoundException (com.nexblocks.authguard.service.exceptions.ServiceNotFoundException)1 OffsetDateTime (java.time.OffsetDateTime)1