public String format(Hash hash) { if (hash == null) { return null; } String algorithmName = hash.getAlgorithmName(); ByteSource salt = hash.getSalt(); int iterations = hash.getIterations(); StringBuilder sb = new StringBuilder(MCF_PREFIX).append(algorithmName).append(TOKEN_DELIMITER).append(iterations).append(TOKEN_DELIMITER); if (salt != null) { sb.append(salt.toBase64()); } sb.append(TOKEN_DELIMITER); sb.append(hash.toBase64()); return sb.toString(); }
@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; } });
@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; } }); }
@Override public Void inTransaction(final EntitySqlDaoWrapperFactory entitySqlDaoWrapperFactory) throws Exception { final TenantModelDao tenantModelDaoWithSecret = new TenantModelDao(entity.getId(), context.getCreatedDate(), context.getUpdatedDate(), entity.getExternalKey(), entity.getApiKey(), hashedPasswordBase64, salt.toBase64()); final TenantSqlDao tenantSqlDao = entitySqlDaoWrapperFactory.become(TenantSqlDao.class); createAndRefresh(tenantSqlDao, tenantModelDaoWithSecret, context); return null; } });
@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; } }); }
@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; } });
public AuthorizedUser(String name, Hash hash, String email) { this.name = name; this.password = hash.toBase64(); this.salt = hash.getSalt().toBase64(); this.hashAlgorithm = hash.getAlgorithmName(); this.hashIterations = hash.getIterations(); this.email = email; this.chatLockedUntil = null; this.active = true; this.lockedUntil = null; }
@Override protected String encodeKey(byte[] key) throws GeneralException { if (kek != null) { return saltedCipherService.encrypt(key, kek).toBase64(); } else { return Base64.encodeBase64String(key); } }
public void process(Exchange exchange) throws Exception { ByteSource bytes = encrypt(); Object token; if (isBase64()) { token = bytes.toBase64(); } else { token = bytes; } exchange.getIn().setHeader(ShiroSecurityConstants.SHIRO_SECURITY_TOKEN, token); }
public String format(Hash hash) { if (hash == null) { return null; } String algorithmName = hash.getAlgorithmName(); ByteSource salt = hash.getSalt(); int iterations = hash.getIterations(); StringBuilder sb = new StringBuilder(MCF_PREFIX).append(algorithmName).append(TOKEN_DELIMITER).append(iterations).append(TOKEN_DELIMITER); if (salt != null) { sb.append(salt.toBase64()); } sb.append(TOKEN_DELIMITER); sb.append(hash.toBase64()); return sb.toString(); }
/** * Generates a salt for use with password salting * * @return randomly generated salt in Base64 format */ public static String generateSalt() { // Generate a random salt for a password (password salt is Base64) RandomNumberGenerator rng = new SecureRandomNumberGenerator(); return rng.nextBytes().toBase64(); }
@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; } });
private static void encryptPassword(User user) throws UnsupportedEncodingException { RandomNumberGenerator rng = new SecureRandomNumberGenerator(); ByteSource byteSource = rng.nextBytes(); String salt = byteSource.toBase64(); String hashedPasswordBase64 = new Sha256Hash(user.getPassword(), salt, 1024).toBase64(); user.setPassword(hashedPasswordBase64); user.setSalt(salt); }
@Override public Void inTransaction(final EntitySqlDaoWrapperFactory entitySqlDaoWrapperFactory) throws Exception { final TenantModelDao tenantModelDaoWithSecret = new TenantModelDao(entity.getId(), context.getCreatedDate(), context.getUpdatedDate(), entity.getExternalKey(), entity.getApiKey(), hashedPasswordBase64, salt.toBase64()); final TenantSqlDao tenantSqlDao = entitySqlDaoWrapperFactory.become(TenantSqlDao.class); createAndRefresh(tenantSqlDao, tenantModelDaoWithSecret, context); return null; } });
@Override public Void inTransaction(final EntitySqlDaoWrapperFactory<EntitySqlDao> entitySqlDaoWrapperFactory) throws Exception { final TenantModelDao tenantModelDaoWithSecret = new TenantModelDao(entity.getId(), context.getCreatedDate(), context.getUpdatedDate(), entity.getExternalKey(), entity.getApiKey(), hashedPasswordBase64, salt.toBase64()); entitySqlDaoWrapperFactory.become(TenantSqlDao.class).create(tenantModelDaoWithSecret, context); return null; } });
@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; } });
public void updatePwd(Object uid, String password) { String salt = new SecureRandomNumberGenerator().nextBytes().toBase64(); dao().update(User.class, Chain.make("password", new Sha256Hash(password, salt, 1024).toBase64()).add("salt", salt), Cnd.where("id", "=", uid)); }
public User regist(User user, String addr) { user.setCreateDate(Times.now()); user.setRegisterIp(addr); user.setSystem(false); String salt = new SecureRandomNumberGenerator().nextBytes().toBase64(); user.setSalt(salt); user.setPassword(new Sha256Hash(user.getPassword(), salt, 1024).toBase64()); return dao().insert(user); }
public boolean save(String username, String password, boolean isEnabled, String addr, int[] roleIds) { User user = new User(); user.setCreateDate(Times.now()); user.setDescription("--"); user.setLocked(!isEnabled); user.setName(username); user.setRegisterIp(addr); user.setRoles(dao().query(Role.class, Cnd.where("id", "in", roleIds))); String salt = new SecureRandomNumberGenerator().nextBytes().toBase64(); user.setSalt(salt); user.setPassword(new Sha256Hash(password, salt, 1024).toBase64()); insert(user); return true; }