相同邀请令牌的不同散列值

时间:2014-10-03 18:34:53

标签: ruby-on-rails ruby devise devise-invitable

我在我的Rails中使用Devise进行身份验证,并且devise_invitable向用户发送邀请。

我在登台和制作环境中遇到了一个奇怪的问题。这是我们当地环境或开发环境中没有出现的问题。环境之间的一个显着差异是,临时和生产环境在应用服务器前面有负载均衡器,但其余设置类似。

当用户点击邀请链接(其中包含原始令牌作为参数)时,看似随机,原始令牌将在查找之前被散列为完全不同的值,导致其失败。以下是一些示例日志。

DB中的令牌:

5d44e5c9175eebbd93737ad9db2bc83fe252c89218e6767a42c1ff8b85dd8029

请求1(失败)

Started GET "/organizations/7/invitation/accept?invitation_token=FopqamFqA7zhXgXkQXQ7" for 50.156.8.77 at 2014-10-03 17:40:28 +0000
Processing by InvitationsController#edit as HTML
Parameters: {"invitation_token"=>"FopqamFqA7zhXgXkQXQ7", "id"=>"7"}
User Load (1.6ms)  SELECT  `users`.* FROM `users`  WHERE `users`.`invitation_token` = 'cf7f5029d134035c196739e8c5be9a9cdc54ad3fb9ae349f6567d29aea8b7165'  ORDER BY `users`.`id` ASC LIMIT 1
Filter chain halted as :resource_from_invitation_token rendered or redirected

请求2(成功)

Started GET "/organizations/7/invitation/accept?invitation_token=FopqamFqA7zhXgXkQXQ7" for 50.156.8.77 at 2014-10-03 17:49:41 +0000
Processing by InvitationsController#edit as HTML
Parameters: {"invitation_token"=>"FopqamFqA7zhXgXkQXQ7", "id"=>"7"}
User Load (1.5ms)  SELECT  `users`.* FROM `users`  WHERE `users`.`invitation_token` = '5d44e5c9175eebbd93737ad9db2bc83fe252c89218e6767a42c1ff8b85dd8029'  ORDER BY `users`.`id` ASC LIMIT 1

请求3(失败)

Started GET "/organizations/7/invitation/accept?invitation_token=FopqamFqA7zhXgXkQXQ7" for 50.156.8.77 at 2014-10-03 17:54:58 +0000
Processing by InvitationsController#edit as HTML
Parameters: {"invitation_token"=>"FopqamFqA7zhXgXkQXQ7", "id"=>"7"}
User Load (1.2ms)  SELECT  `users`.* FROM `users`  WHERE `users`.`invitation_token` = '69e87f81483b22c5be1a1b93dcb6fcebcd8396b172b7a85cbf17cb0ba5784cc8'  ORDER BY `users`.`id` ASC LIMIT 1
Filter chain halted as :resource_from_invitation_token rendered or redirected

以下是我的邀请控制器的编辑方法:

def edit
    if resource.present? && resource.organization.present?
      @guest_organization = resource.organization
    end
    super
end

相关版本: ruby 2.1.2,rails 4.1.0,devise 3.2.4,devise_invitable 1.3.4

在日志中可以看到,参数中的原始令牌是相同的,但每次散列值都不同。我(遗憾的是)无法在dev或本地复制此内容。

有人碰到这样的事吗?

1 个答案:

答案 0 :(得分:1)

Devise uses the app's secret_key_base to to generate the digest

我的设定是这样的:

Ginseng::Application.config.secret_token = "#{SecureRandom.hex(64)}"
Ginseng::Application.config.secret_key_base = "#{SecureRandom.hex(64)}"

所以secret_key_base随着每次部署/重启而改变,并且在每个应用服务器上都不同 - 导致散列令牌不匹配。

将secret_token.rb更改为:

Ginseng::Application.config.secret_token = ENV['secret_token']
Ginseng::Application.config.secret_key_base = ENV['secret_key_base']

解决了这个问题。