我正在使用带有令牌身份验证的Devise,现在,我想加密数据库中的令牌。 任何人都可以给我一个提示,在那里设计从DB中存储/检索令牌吗?
我也在使用 attr_encrypted gem,一旦找到正确的位置,整个加密应该很容易。
修改
我已经实现了令牌身份验证,如下所述:http://zyphdesignco.com/blog/simple-auth-token-example-with-devise
我在用户模型中添加了以下行,该行应加密authentication_token
attr_encrypted :authentication_token, :key => 'a secret key', :attribute => 'authentication_token'
当我运行它并尝试登录时,我收到以下错误消息:
Completed 500 Internal Server Error in 364ms
SystemStackError - stack level too deep:
(gem) actionpack-3.2.13/lib/action_dispatch/middleware/reloader.rb:70:in `'
似乎与设计和attr_encrypted存在冲突,并且两者都在重新定义authentication_token方法(对于提示@sbfaulkner来说是thx)
也许有人有类似的问题并且知道解决方案?
答案 0 :(得分:0)
关于令牌可验证策略的重要部分在Devise::Models:: TokenAuthenticatable
module中 - 它使用一组简单的方法:
find_for_token_authentication
用于验证资源ensure_authentication_token
/ ensure_authentication_token!
应该用于为新资源生成令牌 - Devise不会单独调用它。如果attr_encrypted
宝石与AR模型兼容,那么我相信你对Devise没有任何问题,但最好的方法是尝试它。
答案 1 :(得分:0)
我的用户模型就是这样做的:
before_save :ensure_authentication_token
attr_encrypted :authentication_token, :key => 'my key'
def ensure_authentication_token
if authentication_token.blank?
self.authentication_token = generate_authentication_token
end
end
private
def generate_authentication_token
loop do
token = User.encrypt_authentication_token(Devise.friendly_token)
break token unless User.where(encrypted_authentication_token: token).first
end
end
秘密在于这种方法:attr_encrypted创建的encrypt_authentication_token
。