/** * @param nick name. * @param pass hash * @return the user * @throws BLException if invalid user/pass * @throws HibernateException on low level hibernate related exception */ public User getUserByNick (String nick, String pass) throws Exception { User u = getUserByNick(nick); assertNotNull (u, "User does not exist"); assertTrue(checkPassword(u, pass), "Invalid password"); return u; }
public Validator getCurrentPasswordMatchValidator() { return (Validator<String>) (value, context) -> { try { boolean passwordOk = (boolean) DB.exec((db) -> { UserManager mgr = new UserManager(db); try { return mgr.checkPassword((User) getOriginalEntity(), (String) value); } catch (BLException e) { return false; } }); return passwordOk ? ValidationResult.ok() : ValidationResult.error(getApp().getMessage("error.invalidPassword")); } catch (Exception e) { getApp().getLog().error(e); return ValidationResult.error(e.getMessage()); } }; } public Validator getNewPasswordNotUsedValidator() {
/** * @param u the user * @param clearpass new password in clear * @return true if password is in PasswordHistory */ public boolean checkNewPassword (User u, String clearpass) throws Exception { if (checkPassword (u, clearpass)) { return false; // same password not allowed } for (PasswordHistory p : u.getPasswordhistory()) { HashVersion v = HashVersion.getVersion(p.getValue()); switch (v) { case ZERO: if (checkV0Password(p.getValue(), u.getId(), clearpass)) return false; case ONE: if (checkV1Password (p.getValue(), clearpass)) return false; } } return true; }
boolean newPasswordOK = false; passwordOK = mgr.checkPassword(user, currentPass); newPasswordOK = mgr.checkNewPassword(user, newClearPass); if (passwordOK && newPasswordOK) {
assertFalse("User don't have 'superuser' permission", u.hasPermission("superuser")); assertTrue("User password is 'test'", mgr.checkPassword(u, "test")); assertEquals("User hash is correct", "ee89026a6c5603c51b4504d218ac60f6874b7750", u.getPasswordHash()); assertFalse("Password has to be in history", mgr.checkNewPassword(u, "test")); mgr.upgradePassword(u, "test"); assertNotEquals("User hash has changed", "ee89026a6c5603c51b4504d218ac60f6874b7750", u.getPasswordHash()); assertTrue("User password is still 'test'", mgr.checkPassword(u, "test")); assertNotEquals("User hash has changed", "ee89026a6c5603c51b4504d218ac60f6874b7750", u.getPasswordHash()); assertFalse("Password has to be in history", mgr.checkNewPassword(u, "test")); assertFalse("Password 2 has to be in history", mgr.checkNewPassword(u, "test2")); assertFalse("Password 3 has to be in history", mgr.checkNewPassword(u, "test3")); assertTrue("User password is now 'test3'", mgr.checkPassword(u, "test3")); mgr.setPassword(u, "test"); assertTrue("User password is back to 'test'", mgr.checkPassword(u, "test")); assertEquals ("History size is ", 5, u.getPasswordhistory().size()); db.commit();