private void checkEmail(User user, String newEmail) throws EmailIsAlreadyTakenException, InvalidEmailException { if (!Validator.isEmail(newEmail)) { throw new InvalidEmailException(); } Optional<User> otherUser = findUser(newEmail); if (otherUser.isPresent() && !user.equals(otherUser.get())) { throw new EmailIsAlreadyTakenException(); } }
private void checkScreenName(User user, String newScreenName) throws ScreenNameIsAlreadyTakenException { Optional<User> otherUser = findUser(newScreenName); if (otherUser.isPresent() && !user.equals(otherUser.get())) { throw new ScreenNameIsAlreadyTakenException(); } }
/** * Uses the given confirmation token if it exists and it's still valid. * * @param token token's value * @return the used token * @throws InvalidConfirmationTokenException if there was no such token or if it wasn't valid. */ public ConfirmationToken useConfirmationToken(String token) throws InvalidConfirmationTokenException { Optional<ConfirmationToken> confirmationTokenHolder = getConfirmationToken(token); if (!confirmationTokenHolder.isPresent()) { throw new InvalidConfirmationTokenException(); } ConfirmationToken confirmationToken = confirmationTokenHolder.get(); if (!confirmationToken.isValid()) { throw new InvalidConfirmationTokenException(); } return confirmationToken.use(); }
@Override public void signup(User user, String rawPassword) throws InvalidEmailException, EmailIsAlreadyTakenException, ScreenNameIsAlreadyTakenException { Objects.requireNonNull(user, "user"); Objects.requireNonNull(rawPassword, "rawPassword"); String email = user.getEmail(); if (!Validator.isEmail(email)) { throw new InvalidEmailException(); } if (isEmailTaken(email)) { throw new EmailIsAlreadyTakenException(); } if (isScreenNameTaken(user.getScreenName())) { throw new ScreenNameIsAlreadyTakenException(); } Password password = passwordSecurity.ecrypt(rawPassword); user.setPassword(password); user = store(user); userEventEmitter.emit(new UserEvent(user.getId(), SIGNUP_REQUESTED)); }
@Override public Authentication authenticate(Authentication authentication) throws AuthenticationException { String username = authentication.getName(); String password = (String) authentication.getCredentials(); try { User user = userService.login(username, password); Collection<GrantedAuthority> authorities = user.getAuthorities() .stream() .map(SimpleGrantedAuthority::new) .collect(Collectors.toSet()); return new UsernamePasswordAuthenticationToken(user.getId(), null, authorities); } catch (NoSuchUserException e) { throw new UsernameNotFoundException(e.getMessage(), e); } catch (UnconfirmedUserException e) { throw new DisabledException(e.getMessage(), e); } }
@Override public User login(String emailOrScreenName, String rawPassword) throws NoSuchUserException, UnconfirmedUserException { Objects.requireNonNull(emailOrScreenName, "emailOrScreenName"); Objects.requireNonNull(rawPassword, "rawPassword"); User user = getUser(emailOrScreenName); if (!user.isConfirmed()) { throw new UnconfirmedUserException(); } if (passwordSecurity.check(user.getPassword(), rawPassword)) { // TODO: invalid all password reset tokens. userEventEmitter.emit(new UserEvent(user.getId(), SIGNIN_SUCCEEDED)); return user; } userEventEmitter.emit(new UserEvent(user.getId(), SIGNIN_FAILED)); throw new NoSuchUserException(); }
@Override public String nextScreenName(String email) throws InvalidEmailException { Objects.requireNonNull(email); if (!Validator.isEmail(email)) { throw new InvalidEmailException(); } String screenName = email.split("@")[0]; int index = 1; String possibleScreenName = screenName; while (isScreenNameTaken(possibleScreenName) && index < NEXT_SCREEN_NAME_MAX_TRIES) { possibleScreenName = screenName + (index++); } if (index < NEXT_SCREEN_NAME_MAX_TRIES) { return possibleScreenName; } if (!isScreenNameTaken(possibleScreenName)) { return possibleScreenName; } else { return screenName + IdentityGenerator.generate(); } }