use of org.apache.shiro.crypto.hash.SimpleHash in project neo4j by neo4j.
the class PluginAuthenticationInfoTest method shouldCreateCorrectAuthenticationInfoFromCacheable.
@Test
public void shouldCreateCorrectAuthenticationInfoFromCacheable() {
SecureHasher hasher = mock(SecureHasher.class);
when(hasher.hash(Matchers.any())).thenReturn(new SimpleHash("some-hash"));
PluginAuthenticationInfo internalAuthInfo = PluginAuthenticationInfo.createCacheable(CacheableAuthenticationInfo.of("thePrincipal", new byte[] { 1 }), "theRealm", hasher);
assertThat((List<String>) internalAuthInfo.getPrincipals().asList(), containsInAnyOrder("thePrincipal"));
}
use of org.apache.shiro.crypto.hash.SimpleHash in project neo4j by neo4j.
the class PluginAuthInfo method createCacheable.
public static PluginAuthInfo createCacheable(AuthInfo authInfo, String realmName, SecureHasher secureHasher) {
if (authInfo instanceof CacheableAuthInfo) {
byte[] credentials = ((CacheableAuthInfo) authInfo).credentials();
SimpleHash hashedCredentials = secureHasher.hash(credentials);
return new PluginAuthInfo(authInfo, hashedCredentials, realmName);
} else {
return new PluginAuthInfo(authInfo.principal(), realmName, authInfo.roles().stream().collect(Collectors.toSet()));
}
}
use of org.apache.shiro.crypto.hash.SimpleHash in project shiro by apache.
the class Shiro1CryptFormat method parse.
public Hash parse(String formatted) {
if (formatted == null) {
return null;
}
if (!formatted.startsWith(MCF_PREFIX)) {
// TODO create a HashFormatException class
String msg = "The argument is not a valid '" + ID + "' formatted hash.";
throw new IllegalArgumentException(msg);
}
String suffix = formatted.substring(MCF_PREFIX.length());
String[] parts = suffix.split("\\$");
// last part is always the digest/checksum, Base64-encoded:
int i = parts.length - 1;
String digestBase64 = parts[i--];
// second-to-last part is always the salt, Base64-encoded:
String saltBase64 = parts[i--];
String iterationsString = parts[i--];
String algorithmName = parts[i];
byte[] digest = Base64.decode(digestBase64);
ByteSource salt = null;
if (StringUtils.hasLength(saltBase64)) {
byte[] saltBytes = Base64.decode(saltBase64);
salt = ByteSource.Util.bytes(saltBytes);
}
int iterations;
try {
iterations = Integer.parseInt(iterationsString);
} catch (NumberFormatException e) {
String msg = "Unable to parse formatted hash string: " + formatted;
throw new IllegalArgumentException(msg, e);
}
SimpleHash hash = new SimpleHash(algorithmName);
hash.setBytes(digest);
if (salt != null) {
hash.setSalt(salt);
}
hash.setIterations(iterations);
return hash;
}
use of org.apache.shiro.crypto.hash.SimpleHash in project killbill by killbill.
the class DefaultUserDao method insertUser.
@Override
public void insertUser(final String username, final String password, final List<String> roles, final String createdBy) throws SecurityApiException {
final ByteSource salt = rng.nextBytes();
final String hashedPasswordBase64 = new SimpleHash(KillbillCredentialsMatcher.HASH_ALGORITHM_NAME, password, salt.toBase64(), securityConfig.getShiroNbHashIterations()).toBase64();
final DateTime createdDate = clock.getUTCNow();
inTransactionWithExceptionHandling(new TransactionCallback<Void>() {
@Override
public Void inTransaction(final Handle handle, final TransactionStatus status) throws Exception {
final UserRolesSqlDao userRolesSqlDao = handle.attach(UserRolesSqlDao.class);
for (final String role : roles) {
userRolesSqlDao.create(new UserRolesModelDao(username, role, createdDate, createdBy));
}
final UsersSqlDao usersSqlDao = handle.attach(UsersSqlDao.class);
final UserModelDao userModelDao = usersSqlDao.getByUsername(username);
if (userModelDao != null) {
throw new SecurityApiException(ErrorCode.SECURITY_USER_ALREADY_EXISTS, username);
}
usersSqlDao.create(new UserModelDao(username, hashedPasswordBase64, salt.toBase64(), createdDate, createdBy));
return null;
}
});
}
use of org.apache.shiro.crypto.hash.SimpleHash in project killbill by killbill.
the class DefaultUserDao method updateUserPassword.
@Override
public void updateUserPassword(final String username, final String password, final String updatedBy) throws SecurityApiException {
final ByteSource salt = rng.nextBytes();
final String hashedPasswordBase64 = new SimpleHash(KillbillCredentialsMatcher.HASH_ALGORITHM_NAME, password, salt.toBase64(), securityConfig.getShiroNbHashIterations()).toBase64();
inTransactionWithExceptionHandling(new TransactionCallback<Void>() {
@Override
public Void inTransaction(final Handle handle, final TransactionStatus status) throws Exception {
final DateTime updatedDate = clock.getUTCNow();
final UsersSqlDao usersSqlDao = handle.attach(UsersSqlDao.class);
final UserModelDao userModelDao = usersSqlDao.getByUsername(username);
if (userModelDao == null) {
throw new SecurityApiException(ErrorCode.SECURITY_INVALID_USER, username);
}
usersSqlDao.updatePassword(username, hashedPasswordBase64, salt.toBase64(), updatedDate.toDate(), updatedBy);
return null;
}
});
}
Aggregations