Wordpress 用户迁移到 rails 设计

Wordpress users migration to rails devise

我正在将 wordpress 迁移到 rails。在 rails 我将使用设计,唯一的问题是现有的 wordpress 用户哈希密码。

如果我将所有用户迁移到 rails,我该如何使用旧密码进行身份验证。 rails 是否可以生成与 wordpress 相同的哈希值?

是的。 Devise 需要 create a custom encryptor

Wordpress 可以通过多种方式散列密码,但是 by default uses phpass. There is a ruby implementation as a gem called phpass-ruby, that you could use as a basis for your encryptor. You may need to modify this to use your WP salt. Alternatively, check out this gist.

但是...

如果可能,我建议导入用户,然后 sending out an email to each 要求他们创建新密码。如果您的旧密码经过 MD5 哈希处理,这将更加安全,并且可以说它可能更加用户友好,因为用户(或软件)可能不会将旧密码与新站点相关联。

这将允许使用旧密码进行身份验证。如果他们更改密码,它将改用默认的 Devise BCrypt 哈希。

Gemfile
gem 'phpass-ruby' # check WordPress passwords
User.rb
require "phpass"
class User < ApplicationRecord

  # For Devise to use WordPress passwords. WordPress uses a portable PHPass of MD5 plus a salt.
  def valid_password?(password)
    return false if encrypted_password.blank?
    begin
      return true if super
    rescue BCrypt::Errors::InvalidHash => e
      logger.info "Invalid BCrypt password for #{email}. Fallback to PHPass."
    end
    # Fallback to PHPass
    phpass = Phpass.new(8)
    return phpass.check(password, encrypted_password)
  end