Search in sources :

Example 6 with PasswordHistory

use of com.emc.storageos.db.client.model.PasswordHistory in project coprhd-controller by CoprHD.

the class ExpireRule method validate.

/**
 * validate if password expired
 *
 * @param password
 */
@Override
public void validate(Password password) {
    if (expireTime == 0) {
        return;
    }
    String username = password.getUsername();
    if (username == null || username.trim().length() == 0) {
        return;
    }
    PasswordHistory ph = password.getPasswordHistory();
    if (ph == null) {
        return;
    }
    Calendar expireDate = ph.getExpireDate();
    Calendar now = Calendar.getInstance();
    if (expireDate == null) {
        Long lastChangedTime = password.getLatestChangedTime();
        expireDate = Calendar.getInstance();
        expireDate.setTimeInMillis(lastChangedTime);
        expireDate.add(Calendar.DATE, expireTime);
    }
    _log.info("now: " + now + ", " + username + " expire date: " + expireDate);
    if (expireDate.before(now)) {
        _log.info("fail");
        throw BadRequestException.badRequests.passwordExpired(expireTime);
    } else {
        _log.info("pass");
    }
}
Also used : Calendar(java.util.Calendar) PasswordHistory(com.emc.storageos.db.client.model.PasswordHistory)

Example 7 with PasswordHistory

use of com.emc.storageos.db.client.model.PasswordHistory in project coprhd-controller by CoprHD.

the class PasswordValidationUnitTest method testExpireRule.

@Test
public void testExpireRule() {
    ExpireRule expireRule = new ExpireRule(1);
    long current = System.currentTimeMillis();
    long twoDaysAgo = current - 2 * 24 * 60 * 60 * 1000;
    Password password = new Password("svcuser", "oldpassword", "password");
    PasswordHistory passwordHistory = new PasswordHistory();
    LongMap map = new LongMap();
    map.put("hashedPassword", twoDaysAgo);
    passwordHistory.setUserPasswordHash(map);
    password.setPasswordHistory(passwordHistory);
    logger.info("current=" + current + ", 2daysAgo = " + twoDaysAgo);
    try {
        expireRule.validate(password);
        Assert.fail("password already expired, should fail");
    } catch (BadRequestException e) {
        logger.info(e.getServiceCode().toString());
        logger.info(e.getMessage());
    }
}
Also used : LongMap(com.emc.storageos.db.client.model.LongMap) BadRequestException(com.emc.storageos.svcs.errorhandling.resources.BadRequestException) ExpireRule(com.emc.storageos.security.password.rules.ExpireRule) PasswordHistory(com.emc.storageos.db.client.model.PasswordHistory) Password(com.emc.storageos.security.password.Password) Test(org.junit.Test)

Example 8 with PasswordHistory

use of com.emc.storageos.db.client.model.PasswordHistory in project coprhd-controller by CoprHD.

the class Password method getSortedPasswordByTime.

private List<Map.Entry<String, Long>> getSortedPasswordByTime() {
    PasswordHistory lph = getPasswordHistory();
    if (lph == null || lph.getUserPasswordHash() == null) {
        return null;
    }
    ArrayList<Map.Entry<String, Long>> list = new ArrayList<Map.Entry<String, Long>>(lph.getUserPasswordHash().entrySet());
    Collections.sort(list, new Comparator<Map.Entry<String, Long>>() {

        public int compare(Map.Entry<String, Long> o1, Map.Entry<String, Long> o2) {
            // for fixing CTRL-10305, it should NOT use intValue() of a long, as it may get overflow.
            if (o2.getValue() == o1.getValue()) {
                return 0;
            } else if (o2.getValue() > o1.getValue()) {
                return 1;
            } else {
                return -1;
            }
        }
    });
    return list;
}
Also used : PasswordHistory(com.emc.storageos.db.client.model.PasswordHistory)

Example 9 with PasswordHistory

use of com.emc.storageos.db.client.model.PasswordHistory in project coprhd-controller by CoprHD.

the class PasswordUtils method constructUserPassword.

/**
 * construct a Password object which only contains its password history information
 *
 * @param username
 * @return
 */
public Password constructUserPassword(String username) {
    Password password = new Password(username, null, null);
    PasswordHistory ph = getPasswordHistory(username);
    password.setPasswordHistory(ph);
    return password;
}
Also used : PasswordHistory(com.emc.storageos.db.client.model.PasswordHistory)

Example 10 with PasswordHistory

use of com.emc.storageos.db.client.model.PasswordHistory in project coprhd-controller by CoprHD.

the class PasswordValidationUnitTest method testPasswordHistorySort.

@Test
public void testPasswordHistorySort() {
    long current = System.currentTimeMillis();
    long oneDayAgo = current - 1 * 24 * 60 * 60 * 1000;
    long twoDaysAgo = current - 2 * 24 * 60 * 60 * 1000;
    long threeDaysAgo = current - 3 * 24 * 60 * 60 * 1000;
    logger.info("oneDayAgo = " + oneDayAgo);
    logger.info("twoDaysAgo = " + twoDaysAgo);
    logger.info("threeDaysAgo = " + threeDaysAgo);
    Password password = new Password("svcuser", "oldpassword", "password");
    PasswordHistory passwordHistory = new PasswordHistory();
    LongMap map = new LongMap();
    map.put("hashedPassword1", oneDayAgo);
    map.put("hashedPassword3", threeDaysAgo);
    map.put("hashedPassword2", twoDaysAgo);
    passwordHistory.setUserPasswordHash(map);
    password.setPasswordHistory(passwordHistory);
    long latestChangedTime = password.getLatestChangedTime();
    logger.info("latestChangedTime = " + latestChangedTime);
    Assert.assertEquals(latestChangedTime, oneDayAgo);
    List<String> passwords = password.getPreviousPasswords(3);
    logger.info("password sorted:");
    for (String p : passwords) {
        logger.info(p);
    }
    Assert.assertTrue(passwords.get(0).equals("hashedPassword1"));
    Assert.assertTrue(passwords.get(1).equals("hashedPassword2"));
    Assert.assertTrue(passwords.get(2).equals("hashedPassword3"));
}
Also used : LongMap(com.emc.storageos.db.client.model.LongMap) PasswordHistory(com.emc.storageos.db.client.model.PasswordHistory) Password(com.emc.storageos.security.password.Password) Test(org.junit.Test)

Aggregations

PasswordHistory (com.emc.storageos.db.client.model.PasswordHistory)10 LongMap (com.emc.storageos.db.client.model.LongMap)3 Calendar (java.util.Calendar)3 CheckPermission (com.emc.storageos.security.authorization.CheckPermission)2 Password (com.emc.storageos.security.password.Password)2 Test (org.junit.Test)2 ExpireRule (com.emc.storageos.security.password.rules.ExpireRule)1 BadRequestException (com.emc.storageos.svcs.errorhandling.resources.BadRequestException)1 ParseException (java.text.ParseException)1 Date (java.util.Date)1