use of org.summerb.users.api.exceptions.UserNotFoundException 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.UserNotFoundException 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.UserNotFoundException 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.UserNotFoundException in project summerb by skarpushin.
the class UserDaoImplTest method testDeleteUser_expectUserWillNotBeFoundAfterDeletition.
@Test
public void testDeleteUser_expectUserWillNotBeFoundAfterDeletition() throws Exception {
User userToCreate = UserFactory.createNewUserTemplate();
userToCreate = userService.createUser(userToCreate);
userService.deleteUserByUuid(userToCreate.getUuid());
try {
userService.getUserByEmail(userToCreate.getEmail());
fail();
} catch (UserNotFoundException e) {
// it's expected
}
}
use of org.summerb.users.api.exceptions.UserNotFoundException in project summerb by skarpushin.
the class AuthenticationProviderImpl method authenticate.
@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
// Ensure that all conditions apply
Assert.isInstanceOf(UsernamePasswordAuthenticationToken.class, authentication, getMessage("AbstractUserDetailsAuthenticationProvider.onlySupports", "Only UsernamePasswordAuthenticationToken is supported"));
// check we have credentials specified
if (authentication.getCredentials() == null) {
logger.debug("Authentication failed: no credentials provided");
throw new BadCredentialsException(getMessage("AbstractUserDetailsAuthenticationProvider.badCredentials", "Bad credentials"));
}
// Determine user-name
String username = (authentication.getPrincipal() == null) ? "" : authentication.getName();
// Encode password
String presentedPlainPassword = authentication.getCredentials().toString();
try {
if (loginEligibilityVerifier != null) {
loginEligibilityVerifier.validateUserAllowedToLogin(username);
}
// Proceed with authentication
// get user
User user = userService.getUserByEmail(username);
// check password
if (!passwordService.isUserPasswordValid(user.getUuid(), presentedPlainPassword)) {
throw new InvalidPasswordException();
}
// get user permission
List<String> permissions = permissionService.findUserPermissionsForSubject(SecurityConstants.DOMAIN, user.getUuid(), null);
UserDetailsImpl userDetails = new UserDetailsImpl(user, "[PASSWORD REMOVED]", permissions, null);
UsernamePasswordAuthenticationToken ret = new UsernamePasswordAuthenticationToken(userDetails, authentication.getCredentials(), userDetails.getAuthorities());
ret.setDetails(authentication.getDetails());
return ret;
} catch (FieldValidationException e) {
throw buildBadCredentialsExc(e);
} catch (UserNotFoundException e) {
throw buildBadCredentialsExc(new FieldValidationException(new UserNotFoundValidationError()));
} catch (InvalidPasswordException e) {
throw buildBadCredentialsExc(new FieldValidationException(new PasswordInvalidValidationError()));
} catch (Throwable t) {
throw new AuthenticationServiceException(getMessage(SecurityMessageCodes.AUTH_FATAL, "Fatal authentication exception"), t);
}
}
Aggregations