use of com.nexblocks.authguard.dal.model.AccountTokenDO in project AuthGuard by AuthGuard.
the class ActionTokenServiceImpl method generateFromOtp.
@Override
public Try<ActionTokenBO> generateFromOtp(final String passwordId, final String otp, final String action) {
final String otpToken = passwordId + ":" + otp;
final Either<Exception, Optional<AccountBO>> otpResult = otpVerifier.verifyAccountToken(otpToken).map(accountsService::getById);
if (otpResult.isLeft()) {
return Try.failure(otpResult.getLeft());
}
final AccountBO account = otpResult.get().orElse(null);
if (account == null) {
return Try.failure(new ServiceException(ErrorCode.ACCOUNT_DOES_NOT_EXIST, "The account associated with that OTP no longer exists"));
}
final AccountTokenDO token = generateToken(account, action);
return Try.success(ActionTokenBO.builder().accountId(account.getId()).token(token.getToken()).validFor(TOKEN_LIFETIME.toSeconds()).build());
}
use of com.nexblocks.authguard.dal.model.AccountTokenDO in project AuthGuard by AuthGuard.
the class CredentialsServiceImpl method resetPasswordByToken.
@Override
public Optional<CredentialsBO> resetPasswordByToken(final String token, final String plainPassword) {
final AccountTokenDO accountToken = accountTokensRepository.getByToken(token).join().orElseThrow(() -> new ServiceNotFoundException(ErrorCode.TOKEN_EXPIRED_OR_DOES_NOT_EXIST, "AccountDO token " + token + " does not exist"));
if (accountToken.getExpiresAt().isBefore(OffsetDateTime.now())) {
throw new ServiceException(ErrorCode.EXPIRED_TOKEN, "Token " + token + " has expired");
}
final String credentialsId = Optional.ofNullable(accountToken.getAdditionalInformation()).map(m -> m.get("credentialsId")).orElseThrow(() -> new ServiceException(ErrorCode.INVALID_TOKEN, "Reset token was not mapped to any credentials"));
return updatePassword(credentialsId, plainPassword);
}
use of com.nexblocks.authguard.dal.model.AccountTokenDO 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.dal.model.AccountTokenDO in project AuthGuard by AuthGuard.
the class VerificationServiceImpl method verifyEmail.
@Override
public void verifyEmail(final String verificationToken) {
final AccountTokenDO accountToken = accountTokensRepository.getByToken(verificationToken).join().orElseThrow(() -> new ServiceNotFoundException(ErrorCode.TOKEN_EXPIRED_OR_DOES_NOT_EXIST, "AccountDO token " + verificationToken + " does not exist"));
if (accountToken.getExpiresAt().isBefore(OffsetDateTime.now())) {
throw new ServiceException(ErrorCode.EXPIRED_TOKEN, "Token " + verificationToken + " has expired");
}
final String verifiedEmail = Optional.ofNullable(accountToken.getAdditionalInformation()).map(additional -> additional.get(TARGET_EMAIL_PROPERTY)).orElseThrow(() -> new ServiceException(ErrorCode.INVALID_TOKEN, "Invalid account token: no valid additional information"));
final AccountBO account = accountsService.getById(accountToken.getAssociatedAccountId()).orElseThrow(() -> new ServiceNotFoundException(ErrorCode.ACCOUNT_DOES_NOT_EXIST, "AccountDO " + accountToken.getAssociatedAccountId() + " does not exist"));
final AccountBO updated;
if (verifiedEmail.equals(account.getEmail().getEmail())) {
updated = account.withEmail(account.getEmail().withVerified(true));
} else if (verifiedEmail.equals(account.getBackupEmail().getEmail())) {
updated = account.withBackupEmail(account.getBackupEmail().withVerified(true));
} else {
throw new ServiceException(ErrorCode.INVALID_TOKEN, "Account " + account.getId() + " does not contain the " + "email associated with the verification token");
}
accountsService.update(updated);
}
use of com.nexblocks.authguard.dal.model.AccountTokenDO in project AuthGuard by AuthGuard.
the class EmailPasswordlessSubscriberTest 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 PasswordlessMessageBody messageBody = new PasswordlessMessageBody(accountToken, account);
final Message message = Messages.passwordlessGenerated(messageBody);
final ImmutableEmail expectedEmail = ImmutableEmail.builder().template("passwordless").to(account.getEmail().getEmail()).parameters(ImmutableMap.of("token", accountToken.getToken(), "firstName", account.getFirstName(), "lastName", account.getLastName())).build();
emailPasswordlessSubscriber.onMessage(message);
final ArgumentCaptor<ImmutableEmail> sentEmailCaptor = ArgumentCaptor.forClass(ImmutableEmail.class);
Mockito.verify(emailProvider).send(sentEmailCaptor.capture());
assertThat(sentEmailCaptor.getValue()).isEqualTo(expectedEmail);
}
Aggregations