Search in sources :

Example 1 with SimpleHash

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"));
}
Also used : SecureHasher(org.neo4j.server.security.enterprise.auth.SecureHasher) SimpleHash(org.apache.shiro.crypto.hash.SimpleHash) Test(org.junit.Test)

Example 2 with SimpleHash

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()));
    }
}
Also used : SimpleHash(org.apache.shiro.crypto.hash.SimpleHash) CacheableAuthInfo(org.neo4j.server.security.enterprise.auth.plugin.spi.CacheableAuthInfo)

Example 3 with SimpleHash

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;
}
Also used : SimpleHash(org.apache.shiro.crypto.hash.SimpleHash) ByteSource(org.apache.shiro.util.ByteSource)

Example 4 with SimpleHash

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;
        }
    });
}
Also used : TransactionStatus(org.skife.jdbi.v2.TransactionStatus) DateTime(org.joda.time.DateTime) SecurityApiException(org.killbill.billing.security.SecurityApiException) Handle(org.skife.jdbi.v2.Handle) SimpleHash(org.apache.shiro.crypto.hash.SimpleHash) ByteSource(org.apache.shiro.util.ByteSource) SecurityApiException(org.killbill.billing.security.SecurityApiException)

Example 5 with SimpleHash

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;
        }
    });
}
Also used : TransactionStatus(org.skife.jdbi.v2.TransactionStatus) SecurityApiException(org.killbill.billing.security.SecurityApiException) DateTime(org.joda.time.DateTime) Handle(org.skife.jdbi.v2.Handle) SimpleHash(org.apache.shiro.crypto.hash.SimpleHash) ByteSource(org.apache.shiro.util.ByteSource) SecurityApiException(org.killbill.billing.security.SecurityApiException)

Aggregations

SimpleHash (org.apache.shiro.crypto.hash.SimpleHash)10 ByteSource (org.apache.shiro.util.ByteSource)4 DateTime (org.joda.time.DateTime)2 Test (org.junit.Test)2 SecurityApiException (org.killbill.billing.security.SecurityApiException)2 SecureHasher (org.neo4j.server.security.enterprise.auth.SecureHasher)2 Handle (org.skife.jdbi.v2.Handle)2 TransactionStatus (org.skife.jdbi.v2.TransactionStatus)2 List (java.util.List)1 SimpleAuthenticationInfo (org.apache.shiro.authc.SimpleAuthenticationInfo)1 Hash (org.apache.shiro.crypto.hash.Hash)1 Sha256Hash (org.apache.shiro.crypto.hash.Sha256Hash)1 Shiro1CryptFormat (org.apache.shiro.crypto.hash.format.Shiro1CryptFormat)1 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)1 Matchers.containsInAnyOrder (org.hamcrest.Matchers.containsInAnyOrder)1 EntityPersistenceException (org.killbill.billing.entity.EntityPersistenceException)1 TenantApiException (org.killbill.billing.tenant.api.TenantApiException)1 EntitySqlDaoWrapperFactory (org.killbill.billing.util.entity.dao.EntitySqlDaoWrapperFactory)1 Matchers (org.mockito.Matchers)1 Mockito.mock (org.mockito.Mockito.mock)1