use of org.summerb.users.api.exceptions.AuthTokenNotFoundException 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.users.api.exceptions.AuthTokenNotFoundException in project summerb by skarpushin.
the class AuthTokenServiceImpl method deleteAuthToken.
@Override
@Transactional(rollbackFor = Throwable.class)
public void deleteAuthToken(String authTokenUuid) throws AuthTokenNotFoundException {
Preconditions.checkArgument(authTokenUuid != null);
try {
getAuthTokenByUuid(authTokenUuid);
authTokenDao.deleteAuthToken(authTokenUuid);
} catch (AuthTokenNotFoundException nfe) {
// it's ok
return;
} catch (Throwable t) {
String msg = String.format("Failed to delete auth token '%s'", authTokenUuid);
throw new UserServiceUnexpectedException(msg, t);
}
}
use of org.summerb.users.api.exceptions.AuthTokenNotFoundException 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);
}
}
Aggregations