Authlogic:如何防止重复使用以前的密码?

问题描述

如何找出用户过去的加密密码来检查他是否使用旧密码作为新密码,在以前的“Authlogic”版本中很容易检查,但在较新的版本中,我找不到密码hash,即使我知道旧盐和新密码,有没有办法确定用户是否使用旧密码作为新密码?

解决方法

你如何..检查[用户]是否使用旧密码作为新密码..我无法找出密码哈希,即使我知道旧盐和新密码..

如果您一直使用相同的 crypto_provider,它应该相当简单。

salt = current_user.password_salt
old_crypted_pw = current_user.crypted_password
new_plaintext_password = params[:new_password]
provider = User.crypto_provider
new_crypted_pw = provider.encrypt(new_plaintext_password,salt)
if new_crypted_pw == old_crypted_pw
  raise 'Cannot reuse password'
# ...

可以在 authlogic/acts_as_authentic/password.rb 中找到这些方法的文档。

如果您要从一个加密提供商转换到另一个(transition_from_crypto_providers 功能),那么您必须尝试匹配所有 User.crypto_providers