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();
}
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());
}
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);
}
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());
}
}
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());
}
Aggregations