添加设计辣椒后,现有用户无法登录生产

时间:2018-03-12 08:00:10

标签: ruby-on-rails authentication devise

我们有一个拥有数千名用户的生产数据库,在上一版本中,我们添加了设计胡椒以提高安全性。

现在,现有用户无法登录到生产环境。但是在发布后创建的新用户可以登录。

我认为问题是用于在发布之前和之后加密密码的盐是不同的。

是否有任何方法可以让两个用户(胡椒集成前创建的用户和胡椒集成后创建的用户)登录?

2 个答案:

答案 0 :(得分:3)

检查您的DEVISE_PEPPER之前的令牌值,如果您现在更改了它,那么它对现有用户来说是失败的。您必须将DEVISE_PEPPER令牌值重置为上一个,或者您通过rails控制台使用相同的密码更新用户的密码。

答案 1 :(得分:1)

您需要使用以下脚本执行旧用户记录的更新:

begin
  c_pool = ActiveRecord::Base.establish_connection # initialize connection pool
  conn = c_pool.connection # create connection object

  # Fetch number of users that were old
  result = conn.execute("SELECT count(*) from users WHERE created_at < '2018-03-12 08:37:46'", )

  count = result.try(:[], 0).to_i

  batch_size = 100
  my_offset = 0

  while (count > 0) do
    users = User.where("created_at < ?", '2018-03-12 08:37:46').limit(batch_size).offset(my_offset)

    break unless users.present? # Exit from loop if no users.

    users.each do |u|
      u.password = u.old_password_field
      u.save # This will update password_digest column of the user.
      count -= 1
    end

    my_offset += batch_size
  end
rescue => e
  Rails.logger.error "#{e.message}"
ensure
  ActiveRecord::Base.clear_active_connections!
end

您可以根据需要修改批量大小。请查看我的另一个answer,它解释了为什么批处理查询更好地进行内存管理。

您可以使用rails runner运行上述脚本。