@Test public void bcryptAuthenticatorFailsForBadUser() throws Exception { when(userDAO.getHashedPassword("invaliduser")) .thenReturn(Optional.empty()); Optional<User> missingUser = bcryptAuthenticator.authenticate(new BasicCredentials("invaliduser", "validpass")); assertThat(missingUser).isEmpty(); }
@Override public Optional<User> authenticate(BasicCredentials credentials) throws AuthenticationException { User user = null; String username = credentials.getUsername(); if (!User.isSanitizedUsername(username)) { logger.info("Username: {} must match pattern: {}", username, User.USERNAME_PATTERN); return Optional.empty(); } String password = credentials.getPassword(); Optional<String> optionalHashedPwForUser = userDAO.getHashedPassword(username); if (checkPassword(password, optionalHashedPwForUser)) { user = User.named(username); } return Optional.ofNullable(user); }
BcryptAuthenticator implements Authenticator<BasicCredentials, User> { @Override public Optional<User> authenticate(BasicCredentials credentials) throws AuthenticationException { User user = null; String username = credentials.getUsername(); if (!User.isSanitizedUsername(username)) { logger.info("Username: {} must match pattern: {}", username, User.USERNAME_PATTERN); return Optional.empty(); } String password = credentials.getPassword(); Optional<String> optionalHashedPwForUser = userDAO.getHashedPassword(username); if (checkPassword(password, optionalHashedPwForUser)) { user = User.named(username); } return Optional.ofNullable(user); } }
BcryptAuthenticator implements Authenticator<BasicCredentials, User> { @Override public Optional<User> authenticate(BasicCredentials credentials) throws AuthenticationException { User user = null; String username = credentials.getUsername(); if (!User.isSanitizedUsername(username)) { logger.info("Username: {} must match pattern: {}", username, User.USERNAME_PATTERN); return Optional.empty(); } String password = credentials.getPassword(); Optional<String> optionalHashedPwForUser = userDAO.getHashedPassword(username); if (checkPassword(password, optionalHashedPwForUser)) { user = User.named(username); } return Optional.ofNullable(user); } BcryptAuthenticator(UserDAO userDAO); }
BcryptAuthenticator implements Authenticator<BasicCredentials, User> { @Override public Optional<User> authenticate(BasicCredentials credentials) throws AuthenticationException { User user = null; String username = credentials.getUsername(); if (!User.isSanitizedUsername(username)) { logger.info("Username: {} must match pattern: {}", username, User.USERNAME_PATTERN); return Optional.empty(); } String password = credentials.getPassword(); Optional<String> optionalHashedPwForUser = userDAO.getHashedPassword(username); if (checkPassword(password, optionalHashedPwForUser)) { user = User.named(username); } return Optional.ofNullable(user); } BcryptAuthenticator(UserDAO userDAO); @Override Optional<User> authenticate(BasicCredentials credentials); static String hashPassword(String password); }
BcryptAuthenticator implements Authenticator<BasicCredentials, User> { @Override public Optional<User> authenticate(BasicCredentials credentials) throws AuthenticationException { User user = null; String username = credentials.getUsername(); if (!User.isSanitizedUsername(username)) { logger.info("Username: {} must match pattern: {}", username, User.USERNAME_PATTERN); return Optional.empty(); } String password = credentials.getPassword(); Optional<String> optionalHashedPwForUser = userDAO.getHashedPassword(username); if (checkPassword(password, optionalHashedPwForUser)) { user = User.named(username); } return Optional.ofNullable(user); } BcryptAuthenticator(UserDAO userDAO); @Override Optional<User> authenticate(BasicCredentials credentials); static String hashPassword(String password);  }