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