throw new AuthenticationException(authenticationErrorMessage(mode, username)); return new AuthenticatedUser(username);
// returns AuthenticatedUser object if authentication is successful, otherwise null public AuthenticatedUser authenticate(String username, String password) { PreparedStatement st = ...; st.setString(1, username); st.setString(2, password); ResultSet rs = st.executeQuery(); AuthenticatedUser user = null; //login valid because there is something from the result set, then create user object if (rs.next() ) { // set all the useful user information in this POJO user = new AuthenticatedUser(username, rs.getString("name"), rs.getString("whatever_important_info")); } ... // close resultset, preparedStatement, connection, clean up, etc. return user; }
private AuthenticatedUser authenticate(String username, String password) throws AuthenticationException { try { String hash = cache.get(username); if (!checkpw(password, hash)) throw new AuthenticationException(String.format("Provided username %s and/or password are incorrect", username)); return new AuthenticatedUser(username); } catch (ExecutionException | UncheckedExecutionException e) { // the credentials were somehow invalid - either a non-existent role, or one without a defined password if (e.getCause() instanceof NoSuchCredentialsException) throw new AuthenticationException(String.format("Provided username %s and/or password are incorrect", username)); // an unanticipated exception occured whilst querying the credentials table if (e.getCause() instanceof RequestExecutionException) { logger.trace("Error performing internal authentication", e); throw new AuthenticationException(String.format("Error during authentication of user %s : %s", username, e.getMessage())); } throw new RuntimeException(e); } }
private AuthenticatedUser authenticate(String username, String password) throws AuthenticationException { try { String hash = cache.get(username); if (!checkpw(password, hash)) throw new AuthenticationException(String.format("Provided username %s and/or password are incorrect", username)); return new AuthenticatedUser(username); } catch (ExecutionException | UncheckedExecutionException e) { // the credentials were somehow invalid - either a non-existent role, or one without a defined password if (e.getCause() instanceof NoSuchCredentialsException) throw new AuthenticationException(String.format("Provided username %s and/or password are incorrect", username)); // an unanticipated exception occured whilst querying the credentials table if (e.getCause() instanceof RequestExecutionException) { logger.trace("Error performing internal authentication", e); throw new AuthenticationException(String.format("Error during authentication of user %s : %s", username, e.getMessage())); } throw new RuntimeException(e); } }
private AuthenticatedUser authenticate(String username, String password) throws AuthenticationException { try { String hash = cache.get(username); if (!checkpw(password, hash)) throw new AuthenticationException(String.format("Provided username %s and/or password are incorrect", username)); return new AuthenticatedUser(username); } catch (ExecutionException | UncheckedExecutionException e) { // the credentials were somehow invalid - either a non-existent role, or one without a defined password if (e.getCause() instanceof NoSuchCredentialsException) throw new AuthenticationException(String.format("Provided username %s and/or password are incorrect", username)); // an unanticipated exception occured whilst querying the credentials table if (e.getCause() instanceof RequestExecutionException) { logger.trace("Error performing internal authentication", e); throw new AuthenticationException(String.format("Error during authentication of user %s : %s", username, e.getMessage())); } throw new RuntimeException(e); } }
public AuthenticatedUser authenticate(Map<String, String> credentials) throws AuthenticationException { String username = credentials.get(USERNAME_KEY); if (username == null) throw new AuthenticationException(String.format("Required key '%s' is missing", USERNAME_KEY)); String password = credentials.get(PASSWORD_KEY); if (password == null) throw new AuthenticationException(String.format("Required key '%s' is missing", PASSWORD_KEY)); UntypedResultSet result; try { ResultMessage.Rows rows = authenticateStatement.execute(QueryState.forInternalCalls(), QueryOptions.forInternalCalls(consistencyForUser(username), Lists.newArrayList(ByteBufferUtil.bytes(username)))); result = UntypedResultSet.create(rows.result); } catch (RequestValidationException e) { throw new AssertionError(e); // not supposed to happen } catch (RequestExecutionException e) { throw new AuthenticationException(e.toString()); } if (result.isEmpty() || !BCrypt.checkpw(password, result.one().getString(SALTED_HASH))) throw new AuthenticationException("Username and/or password are incorrect"); return new AuthenticatedUser(username); }