use of org.summerb.approaches.validation.FieldValidationException in project summerb by skarpushin.
the class UsersServiceFacadeImpl method validateUserIsEligableForPasswordReset.
protected void validateUserIsEligableForPasswordReset(String email) throws FieldValidationException {
ValidationContext ctx = new ValidationContext();
if (!ctx.validateEmailFormat(email, User.FN_EMAIL)) {
throw new FieldValidationException(ctx.getErrors());
}
// now see if this user exists
UserStatus userStatus = getUserStatusByEmail(email);
if (userStatus == UserStatus.NotExists || userStatus == UserStatus.Provisioned) {
throw new FieldValidationException(new RegistrationRequiredValidationError());
}
// Sanity check
if (userStatus != UserStatus.NormalUser && userStatus != UserStatus.AwaitingActivation) {
throw new RuntimeException("Password Reset scenario is not supported for user whose status is: " + userStatus);
}
}
use of org.summerb.approaches.validation.FieldValidationException in project summerb by skarpushin.
the class UsersServiceFacadeImpl method getNewPasswordResetToken.
@Transactional(rollbackFor = Throwable.class)
@Override
public String getNewPasswordResetToken(String email) throws FieldValidationException {
try {
validateUserIsEligableForPasswordReset(email);
User user = userService.getUserByEmail(email);
String passwordResetToken = passwordService.getNewRestorationTokenForUser(user.getUuid());
if (passwordResetArmedHandler != null) {
passwordResetArmedHandler.onPasswordResetRequested(user, passwordResetToken);
}
return passwordResetToken;
} catch (Throwable e) {
Throwables.throwIfInstanceOf(e, FieldValidationException.class);
throw new UserServiceUnexpectedException("Failed to arrange password reset", e);
}
}
use of org.summerb.approaches.validation.FieldValidationException in project summerb by skarpushin.
the class AuthTokenServiceImpl method authenticate.
@Override
@Transactional(rollbackFor = Throwable.class)
public AuthToken authenticate(String userEmail, String passwordPlain, String clientIp) throws UserNotFoundException, FieldValidationException, InvalidPasswordException {
Preconditions.checkArgument(userEmail != null);
Preconditions.checkArgument(passwordPlain != null);
Preconditions.checkArgument(clientIp != null);
try {
User user = validateAndGetUser(userEmail, passwordPlain);
return createAuthToken(user.getEmail(), clientIp, UUID.randomUUID().toString(), UUID.randomUUID().toString());
} catch (Throwable t) {
Throwables.throwIfInstanceOf(t, UserNotFoundException.class);
Throwables.throwIfInstanceOf(t, FieldValidationException.class);
Throwables.throwIfInstanceOf(t, InvalidPasswordException.class);
String msg = String.format("Failed to create auth otken for user '%s'", userEmail);
throw new UserServiceUnexpectedException(msg, t);
}
}
use of org.summerb.approaches.validation.FieldValidationException in project summerb by skarpushin.
the class AuthTokenServiceImpl method updateToken.
@Override
@Transactional(rollbackFor = Throwable.class)
public void updateToken(String authTokenUuid, long lastVerifiedAt, String newTokenValue) throws AuthTokenNotFoundException, FieldValidationException {
Preconditions.checkArgument(authTokenUuid != null);
Preconditions.checkArgument(StringUtils.hasText(newTokenValue), "TokenValue is mandatory");
try {
// First - check token itself
AuthToken authToken = getAuthTokenByUuid(authTokenUuid);
if (newTokenValue.equals(authToken.getTokenValue())) {
throw new FieldValidationException(new ValidationError("validation.newValueExpected", "newTokenValue"));
}
// Now we need to update time when token was checked
authTokenDao.updateToken(authTokenUuid, lastVerifiedAt, newTokenValue);
} catch (Throwable t) {
Throwables.throwIfInstanceOf(t, FieldValidationException.class);
Throwables.throwIfInstanceOf(t, AuthTokenNotFoundException.class);
String msg = String.format("Failed to update token '%s'", authTokenUuid);
throw new UserServiceUnexpectedException(msg, t);
}
}
use of org.summerb.approaches.validation.FieldValidationException in project summerb by skarpushin.
the class PasswordServiceImpl method setUserPassword.
@Override
@Transactional(rollbackFor = Throwable.class)
public void setUserPassword(String userUuid, String newPasswordPlain) throws UserNotFoundException, FieldValidationException {
Preconditions.checkArgument(userUuid != null);
Preconditions.checkArgument(newPasswordPlain != null);
assertUserExists(userUuid);
if (!StringUtils.hasText(newPasswordPlain)) {
throw new FieldValidationException(new FieldRequiredValidationError(FN_PASSWORD));
}
String newPasswordHash = null;
try {
newPasswordHash = encodePassword(newPasswordPlain);
// sanity check
if (!isPasswordMatch(newPasswordPlain, newPasswordHash)) {
throw new RuntimeException("Password doesn't match just created hash");
}
// set user password
int updateResult = passwordDao.updateUserPassword(userUuid, newPasswordHash);
if (updateResult < 1) {
throw new RuntimeException("updateUserPassword returned unexpected result = " + updateResult);
}
} catch (Throwable t) {
String msg = String.format("Failed to set user '%s' passwordHash '%s'", userUuid, newPasswordHash);
throw new UserServiceUnexpectedException(msg, t);
}
}
Aggregations