删除 Warden 策略 - 如何确保原始 devise_authenticable 策略消失

发布于 2024-12-06 13:23:00 字数 216 浏览 3 评论 0原文

我创建了自己的 Warden 策略以与 Devise 一起使用。它与 Devise::Strategies::DatabaseAuthenticatable 非常相似,实际上它继承自它并重新实现了authenticate!

但我的问题是我想确保原来的 devise_authenticable Warden 策略已经消失。这不在 Warden 将尝试的策略列表中,因为这实际上是一个安全问题。这可能吗?

I created my own Warden strategy for using with Devise. It's very similar to Devise::Strategies::DatabaseAuthenticatable and actually it inherits from it and re-implements authenticate!

My issue though is that I want to make sure the original devise_authenticable Warden strategy is gone. That is not in the list of strategies Warden will try because it's actually a security problem. Is that possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

忘东忘西忘不掉你 2024-12-13 13:23:00

根据我的手动检查和测试, devise.rb 初始化程序中的这一点实现了目标:

config.warden do |manager|
  strategies = manager.default_strategies(:scope => :user)
  strategies[strategies.index(:database_authenticatable)] = :alternative_strategy
end

并且该策略是这样实现的(不是这个问题的一部分,但我发现那里存在冲突的信息,而这个信息对我有用使用 Rails 3.1、devise 1.4.7 和 Warden 1.0.5):

class AlternativeStrategy < Devise::Strategies::Authenticatable
  def authenticate!
  end
end
Warden::Strategies.add(:alternative_strategy, AlternativeStrategy)

According to my manual inspection and tests, this in the devise.rb initializer achieves the goal:

config.warden do |manager|
  strategies = manager.default_strategies(:scope => :user)
  strategies[strategies.index(:database_authenticatable)] = :alternative_strategy
end

And the strategy is implemented this way (not part of this question, but I found conflicting information out there and this one is the one that worked for me using Rails 3.1, devise 1.4.7 and warden 1.0.5):

class AlternativeStrategy < Devise::Strategies::Authenticatable
  def authenticate!
  end
end
Warden::Strategies.add(:alternative_strategy, AlternativeStrategy)

我也刚刚实现了这个。 Devise 将尝试其列表中的每一种策略,直到其中一种策略成功为止。

对我来说,我没有替换 :database_authenticatable 策略,而是将我的策略添加到列表的开头,并将 :database_authenticatable 从现有列表的末尾弹出。

config.warden do |manager|
  # Exiles::Devise::Strategies::BySite implemented in lib/.  It matches the stub in Pablo's answer
  manager.strategies.add( :by_site_auth, Exiles::Devise::Strategies::BySite )

  # add my strategy to the beginning of the list.
  manager.default_strategies(:scope => :user).unshift :by_site_auth

  # remove the default database_authenticatable strategy from the list
  manager.default_strategies(:scope => :user).pop
end

I just implemented this as well. Devise will try each strategy in its list until one succeeds.

For me, rather than replace the :database_authenticatable strategy in place, I just added my strategy to the beginning of the list and popped :database_authenticatable off the end of the existing list.

config.warden do |manager|
  # Exiles::Devise::Strategies::BySite implemented in lib/.  It matches the stub in Pablo's answer
  manager.strategies.add( :by_site_auth, Exiles::Devise::Strategies::BySite )

  # add my strategy to the beginning of the list.
  manager.default_strategies(:scope => :user).unshift :by_site_auth

  # remove the default database_authenticatable strategy from the list
  manager.default_strategies(:scope => :user).pop
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文