@Test public final void canNotAuthenticateIfAccountCredentialCanNotMatchPassword() { Account account = new Account(); account.setUsername("user"); account.setPasswordHash("$31$16$ErL2RQyoK4C3N_0woVfE5De37d6t-XI1sIfEpldJl9I"); given(accountDAO.findAccountByUsername("user")).willReturn(Optional.of(account)); Optional<Account> result = service.authenticate("user", "notMatch"); assertThat(result).isEmpty(); }
public Optional<Account> authenticate(String username, String password) { Optional<Account> account = accountDAO.findAccountByUsername(username); return account.flatMap(acc -> { if (passwordUtil.authenticate(password.toCharArray(), acc.getPasswordHash())) { return account; } else { return Optional.empty(); } }); }
AccountService implements Serializable { public Optional<Account> authenticate(String username, String password) { Optional<Account> account = accountDAO.findAccountByUsername(username); return account.flatMap(acc -> { if (passwordUtil.authenticate(password.toCharArray(), acc.getPasswordHash())) { return account; } else { return Optional.empty(); } }); } }
AccountService implements Serializable { public Optional<Account> authenticate(String username, String password) { Optional<Account> account = accountDAO.findAccountByUsername(username); return account.flatMap(acc -> { if (passwordUtil.authenticate(password.toCharArray(), acc.getPasswordHash())) { return account; } else { return Optional.empty(); } }); } @Inject  AccountService(AccountDAO accountDAO); @SuppressWarnings("unused")  AccountService(); }
AccountService implements Serializable { public Optional<Account> authenticate(String username, String password) { Optional<Account> account = accountDAO.findAccountByUsername(username); return account.flatMap(acc -> { if (passwordUtil.authenticate(password.toCharArray(), acc.getPasswordHash())) { return account; } else { return Optional.empty(); } }); } @Inject  AccountService(AccountDAO accountDAO); @SuppressWarnings("unused")  AccountService(); @Transactional AccountDto registerNewAccount(AccountDto accountDto, String username,
            char[] password); Optional<Account> authenticate(String username, String password); List<AccountDto> getAllAccounts(boolean showDisabled); @Transactional boolean updateAccount(AccountDto accountDto); }
AccountService implements Serializable { public Optional<Account> authenticate(String username, String password) { Optional<Account> account = accountDAO.findAccountByUsername(username); return account.flatMap(acc -> { if (passwordUtil.authenticate(password.toCharArray(), acc.getPasswordHash())) { return account; } else { return Optional.empty(); } }); } @Inject  AccountService(AccountDAO accountDAO); @SuppressWarnings("unused")  AccountService(); @Transactional AccountDto registerNewAccount(AccountDto accountDto, String username,
            char[] password); Optional<Account> authenticate(String username, String password); List<AccountDto> getAllAccounts(boolean showDisabled); @Transactional boolean updateAccount(AccountDto accountDto);  }