构建一个应用程序,根据需要从商家的帐户中提取数据。
我已经设置了我的模型,安装了 attr_encrypted gem,并且所有内容都以加密的形式正确存储。
我创建了一个获取令牌并将其发送到 API 的方法,但我需要发送解密版本。令牌是用户特定的,所以我不知道它是什么..
如何发送令牌的解密版本?
在我的控制台中,我尝试过..
x = Merchant.find(1)
x.encrypted_token
并且返回加密版本很好..
但如果我尝试:
x.token
控制台显示 ArgumentError: must specify a :key
我已将其存储为环境变量..
所以我想我会通过这个但是如何?
我的模型中有这个:
attr_encrypted :token, :key => ENV['token_key']
来自评论的更新
这就是我的数据库的样子:
"Merchant(merchant_identifier: string, name: string, encrypted_token: text,
marketplace: string, password_digest: string, email: string)"
我试过了
attr_encrypted :token, :key => 'token_key'
它给了我一个密码错误。我正在使用 figaro 来存储环境变量.. 看起来很好..
继续来!!
我在 irb 做的是:
x = Merchant.find(2)
x.encrypted_token # yields "n4gZVJ8DKfp+p..."
但是如果我按照这个
x.token
我得到:
ArgumentError: must specify a :key