use of org.summerb.users.api.exceptions.UserServiceUnexpectedException in project summerb by skarpushin.
the class UserServiceImpl method createUser.
@Override
@Transactional(rollbackFor = Throwable.class)
public User createUser(User userTemplate) throws FieldValidationException {
Preconditions.checkArgument(userTemplate != null, "userTemplate is required");
validateUser(userTemplate);
User userToCreate = new User();
userToCreate.setDisplayName(userTemplate.getDisplayName());
userToCreate.setEmail(userTemplate.getEmail());
userToCreate.setIntegrationData(userTemplate.getIntegrationData());
userToCreate.setIsBlocked(userTemplate.getIsBlocked());
userToCreate.setLocale(userTemplate.getLocale());
userToCreate.setRegisteredAt(userTemplate.getRegisteredAt());
userToCreate.setTimeZone(userTemplate.getTimeZone());
// Patch data
if (userTemplate.getUuid() != null) {
Preconditions.checkArgument(stringIdGenerator.isValidId(userTemplate.getUuid()), "User id is invalid");
userToCreate.setUuid(userTemplate.getUuid());
} else {
userToCreate.setUuid(stringIdGenerator.generateNewId(userTemplate));
}
if (userToCreate.getRegisteredAt() == 0) {
userToCreate.setRegisteredAt(new Date().getTime());
}
if (userToCreate.getLocale() == null) {
userToCreate.setLocale(LocaleContextHolder.getLocale().toString());
}
if (userToCreate.getTimeZone() == null) {
userToCreate.setTimeZone(Calendar.getInstance().getTimeZone().getID());
}
// Create
try {
userDao.createUser(userToCreate);
eventBus.post(EntityChangedEvent.added(userToCreate));
} catch (DuplicateKeyException dke) {
throw new FieldValidationException(new DuplicateUserValidationError(User.FN_EMAIL));
} catch (Throwable t) {
String msg = String.format("Failed to create user with email '%s'", userToCreate.getEmail());
throw new UserServiceUnexpectedException(msg, t);
}
return userToCreate;
}
use of org.summerb.users.api.exceptions.UserServiceUnexpectedException 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.users.api.exceptions.UserServiceUnexpectedException in project summerb by skarpushin.
the class AuthTokenServiceImpl method createAuthToken.
@Override
@Transactional(rollbackFor = Throwable.class)
public AuthToken createAuthToken(String userEmail, String clientIp, String tokenUuid, String tokenValueUuid) throws UserNotFoundException, FieldValidationException {
Preconditions.checkArgument(userEmail != null);
Preconditions.checkArgument(clientIp != null);
Preconditions.checkArgument(StringUtils.hasText(tokenUuid));
Preconditions.checkArgument(StringUtils.hasText(tokenValueUuid));
try {
User user = userService.getUserByEmail(userEmail);
AuthToken authToken = buildNewAuthToken(user, clientIp, tokenUuid, tokenValueUuid);
authTokenDao.createAuthToken(authToken);
return authToken;
} catch (Throwable t) {
Throwables.throwIfInstanceOf(t, UserNotFoundException.class);
Throwables.throwIfInstanceOf(t, FieldValidationException.class);
String msg = String.format("Failed to create auth otken for user '%s'", userEmail);
throw new UserServiceUnexpectedException(msg, t);
}
}
use of org.summerb.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.users.api.exceptions.UserServiceUnexpectedException 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);
}
}
Aggregations