assertTrue(uc instanceof CryptedSimpleCredentials);
assertTrue(((CryptedSimpleCredentials) uc).matches((SimpleCredentials) creds));
}
public void testChangePassword() throws RepositoryException, NotExecutableException, NoSuchAlgorithmException, UnsupportedEncodingException {
User u = (User) userMgr.getAuthorizable(uID);
String sha1Hash = "{" +SecurityConstants.DEFAULT_DIGEST+ "}" + Text.digest(SecurityConstants.DEFAULT_DIGEST, "abc".getBytes());
String md5Hash = "{md5}" + Text.digest("md5", "abc".getBytes());
// valid passwords and the corresponding match
Map<String,String> pwds = new HashMap<String, String>();
// plain text passwords
pwds.put("abc", "abc");
pwds.put("{a}password", "{a}password");
// passwords already in hashed format.
pwds.put(sha1Hash, "abc");
pwds.put(md5Hash, "abc");
for (String pw : pwds.keySet()) {
u.changePassword(pw);
String plain = pwds.get(pw);
SimpleCredentials sc = new SimpleCredentials(u.getID(), plain.toCharArray());
CryptedSimpleCredentials cc = (CryptedSimpleCredentials) u.getCredentials();
assertTrue(cc.matches(sc));
}
// valid passwords, non-matching plain text
Map<String, String>noMatch = new HashMap<String, String>();
noMatch.put("{"+SecurityConstants.DEFAULT_DIGEST+"}", "");
noMatch.put("{"+SecurityConstants.DEFAULT_DIGEST+"}", "{"+SecurityConstants.DEFAULT_DIGEST+"}");
noMatch.put("{"+SecurityConstants.DEFAULT_DIGEST+"}any", "any");
noMatch.put("{"+SecurityConstants.DEFAULT_DIGEST+"}any", "{"+SecurityConstants.DEFAULT_DIGEST+"}any");
noMatch.put(sha1Hash, sha1Hash);
noMatch.put(md5Hash, md5Hash);
for (String pw : noMatch.keySet()) {
u.changePassword(pw);
String plain = noMatch.get(pw);
SimpleCredentials sc = new SimpleCredentials(u.getID(), plain.toCharArray());
CryptedSimpleCredentials cc = (CryptedSimpleCredentials) u.getCredentials();
assertFalse(cc.matches(sc));
}
// invalid pw string
try {
u.changePassword(null);
fail("invalid pw null");
} catch (Exception e) {
// success
}
}