Search in sources :

Example 6 with UserServiceUnexpectedException

use of org.summerb.microservices.users.api.exceptions.UserServiceUnexpectedException in project summerb by skarpushin.

the class AuthTokenServiceImpl method isAuthTokenValid.

@Override
@Transactional(rollbackFor = Throwable.class)
public AuthToken isAuthTokenValid(String userUuid, String authTokenUuid, String tokenValue) throws UserNotFoundException {
    Preconditions.checkArgument(userUuid != null);
    Preconditions.checkArgument(authTokenUuid != null);
    Preconditions.checkArgument(StringUtils.hasText(tokenValue), "TokenValue is mandatory");
    try {
        // First - check token itself
        AuthToken authToken = getAuthTokenByUuid(authTokenUuid);
        if (authToken.getExpiresAt() < getNow()) {
            authTokenDao.deleteAuthToken(authTokenUuid);
            return null;
        }
        if (!tokenValue.equals(authToken.getTokenValue())) {
            return null;
        }
        // Check reference to user
        User user = userService.getUserByUuid(userUuid);
        if (!authToken.getUserUuid().equals(user.getUuid())) {
            return null;
        }
        // Now we need to update time when token was checked
        authToken.setTokenValue(UUID.randomUUID().toString());
        authToken.setLastVerifiedAt(getNow());
        authTokenDao.updateToken(authTokenUuid, authToken.getLastVerifiedAt(), authToken.getTokenValue());
        return authToken;
    } catch (AuthTokenNotFoundException nfe) {
        return null;
    } catch (Throwable t) {
        Throwables.throwIfInstanceOf(t, UserNotFoundException.class);
        String msg = String.format("Failed to check auth token '%s' validity for user '%s'", authTokenUuid, userUuid);
        throw new UserServiceUnexpectedException(msg, t);
    }
}
Also used : UserNotFoundException(org.summerb.microservices.users.api.exceptions.UserNotFoundException) User(org.summerb.microservices.users.api.dto.User) UserServiceUnexpectedException(org.summerb.microservices.users.api.exceptions.UserServiceUnexpectedException) AuthToken(org.summerb.microservices.users.api.dto.AuthToken) AuthTokenNotFoundException(org.summerb.microservices.users.api.exceptions.AuthTokenNotFoundException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 7 with UserServiceUnexpectedException

use of org.summerb.microservices.users.api.exceptions.UserServiceUnexpectedException 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);
    }
}
Also used : FieldValidationException(org.summerb.approaches.validation.FieldValidationException) UserServiceUnexpectedException(org.summerb.microservices.users.api.exceptions.UserServiceUnexpectedException) FieldRequiredValidationError(org.summerb.approaches.validation.errors.FieldRequiredValidationError) Transactional(org.springframework.transaction.annotation.Transactional)

Example 8 with UserServiceUnexpectedException

use of org.summerb.microservices.users.api.exceptions.UserServiceUnexpectedException in project summerb by skarpushin.

the class PasswordServiceImpl method deleteRestorationToken.

@Override
@Transactional(rollbackFor = Throwable.class)
public void deleteRestorationToken(String userUuid) throws UserNotFoundException {
    Preconditions.checkArgument(userUuid != null);
    assertUserExists(userUuid);
    try {
        int updateResult = passwordDao.setRestorationToken(userUuid, null);
        if (updateResult != 1) {
            throw new RuntimeException("deleteRestorationToken returned unexpected result = " + updateResult);
        }
    } catch (Throwable t) {
        String msg = String.format("Failed to delete restoration token for user '%s'", userUuid);
        throw new UserServiceUnexpectedException(msg, t);
    }
}
Also used : UserServiceUnexpectedException(org.summerb.microservices.users.api.exceptions.UserServiceUnexpectedException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 9 with UserServiceUnexpectedException

use of org.summerb.microservices.users.api.exceptions.UserServiceUnexpectedException in project summerb by skarpushin.

the class PasswordServiceImpl method isRestorationTokenValid.

@Override
public boolean isRestorationTokenValid(String userUuid, String restorationTokenUuid) throws UserNotFoundException {
    Preconditions.checkArgument(userUuid != null);
    Preconditions.checkArgument(restorationTokenUuid != null);
    assertUserExists(userUuid);
    try {
        Password password = passwordDao.findPasswordByUserUuid(userUuid);
        if (password == null || !restorationTokenUuid.equals(password.getRestorationToken())) {
            return false;
        }
    } catch (Throwable t) {
        String msg = String.format("Failed to check user '%s' restoration token validity", userUuid);
        throw new UserServiceUnexpectedException(msg, t);
    }
    return true;
}
Also used : UserServiceUnexpectedException(org.summerb.microservices.users.api.exceptions.UserServiceUnexpectedException) Password(org.summerb.microservices.users.impl.dom.Password)

Example 10 with UserServiceUnexpectedException

use of org.summerb.microservices.users.api.exceptions.UserServiceUnexpectedException in project summerb by skarpushin.

the class PermissionServiceImpl method grantPermission.

@Override
@Transactional(rollbackFor = Throwable.class)
public void grantPermission(String optionalDomainName, String userUuid, String optionalSubjectId, String permissionKey) {
    Preconditions.checkArgument(StringUtils.hasText(permissionKey));
    Preconditions.checkArgument(StringUtils.hasText(userUuid));
    String domainName = getOptionalParamValue(optionalDomainName);
    String subjectId = getOptionalParamValue(optionalSubjectId);
    try {
        permissionDao.grantPermission(domainName, userUuid, subjectId, permissionKey);
    } catch (DuplicateKeyException dke) {
        // it's ok
        log.debug("Duplicate key exception sealed. Looks like same permission is already granted.", dke);
    } catch (Throwable t) {
        String msg = String.format("Failed to grant permission '%s' to user '%s' on subject '%s' in domain '%s'", permissionKey, userUuid, subjectId, domainName);
        throw new UserServiceUnexpectedException(msg, t);
    }
}
Also used : UserServiceUnexpectedException(org.summerb.microservices.users.api.exceptions.UserServiceUnexpectedException) DuplicateKeyException(org.springframework.dao.DuplicateKeyException) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

UserServiceUnexpectedException (org.summerb.microservices.users.api.exceptions.UserServiceUnexpectedException)27 Transactional (org.springframework.transaction.annotation.Transactional)18 FieldValidationException (org.summerb.approaches.validation.FieldValidationException)11 User (org.summerb.microservices.users.api.dto.User)11 UserNotFoundException (org.summerb.microservices.users.api.exceptions.UserNotFoundException)9 DuplicateKeyException (org.springframework.dao.DuplicateKeyException)3 AuthToken (org.summerb.microservices.users.api.dto.AuthToken)3 AuthTokenNotFoundException (org.summerb.microservices.users.api.exceptions.AuthTokenNotFoundException)3 InvalidPasswordException (org.summerb.microservices.users.api.exceptions.InvalidPasswordException)2 DuplicateUserValidationError (org.summerb.microservices.users.api.validation.DuplicateUserValidationError)2 Password (org.summerb.microservices.users.impl.dom.Password)2 Date (java.util.Date)1 UserStatus (org.summerb.approaches.springmvc.security.dto.UserStatus)1 RegistrationAlreadyRequestedValidationError (org.summerb.approaches.springmvc.security.ve.RegistrationAlreadyRequestedValidationError)1 ValidationError (org.summerb.approaches.validation.ValidationError)1 FieldRequiredValidationError (org.summerb.approaches.validation.errors.FieldRequiredValidationError)1