如何从 Ruby on Rails 应用程序中的 OAuth::Unauthorized 异常中拯救出来?

发布于 2025-01-04 12:25:45 字数 526 浏览 1 评论 0原文

如何挽救 Ruby on Rails 应用程序中 OmniAuth 引发的 OAuth::Unauthorized 异常?

显然这个:

  rescue_from OAuth::Unauthorized, :with => :unauthorized

不会工作,因为它只能捕获 Rails 内部抛出的异常,并且该异常是在机架链中的其他地方抛出的。

在此应用程序中,管理员(而不是我们,开发人员)配置 twitter 和 facebook 的凭据,因此可能会发生错误的凭据,而且确实会发生。当这种情况发生时,我想展示一个更好的信息“出了问题”。

更新:我还在omniauth google group上提问,到目前为止还没有答案,但如果您正在阅读这个问题,您可能想查看一下。

How can I rescue from an OAuth::Unauthorized exception as raised from OmniAuth in a Ruby on Rails application?

Obviously this:

  rescue_from OAuth::Unauthorized, :with => :unauthorized

won't work as that only catches exception thrown inside Rails and this exception is thrown somewhere else in the rack chain.

In this application the administrators (and not us, the developers) configure the credentials for twitter and facebook, so having the wrong ones is something that can happen and indeed does happen. I'd like to show a better message that "Something went wrong" when that happens.

Update: I also asked on the omniauth google group, so far there are no answers, but if you are reading this question you might want to check it out.

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

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

发布评论

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

评论(1

一个人的夜不怕黑 2025-01-11 12:25:45

OmniAuth 从 Rack Middleware 运行,因此救援_from 不会影响它,因为这是通过 ActionController 高于 OmniAuth 的抽象级别。

此错误通常是由于 OAuth 设置配置错误造成的。基本上,它是说您的应用程序无权进行身份验证,而不是用户的身份验证失败。

作为开发人员,您希望减轻配置错误,因此我不确定您为什么要挽救这样的异常。

如果您绝对必须挽救此异常,您可以覆盖并使用继承自 OmniAuth 的中间件

module OmniAuth
  module Strategies
    class FacebookWithExceptionHandling < OmniAuth::Strategies::Facebook
      def call
        begin
          super
        raise OmniAuth::Unauthorized => e
          #handle appropriately in rack context here
        end
      end
    end
  end
end

Rails.application.config.middleware.use OmniAuth::Builder do
  provider OmniAuth::Strategies::FacebookWithExceptionHandling, 
    api_key, #your api key 
    secret_key, #your secret key
end

OmniAuth operates from Rack Middleware, so a rescue_from will not affect it because that is a level of abstraction above OmniAuth via ActionController.

This error is usually due to a misconfiguration of your OAuth settings. Basically it is saying that your application is not authorized to authenticate, not that the user's authentication failed.

A configuration error is something you as a developer would want to mitigate, so I'm not sure why you would want to rescue an exception like this.

If you absolutely must rescue this exception, you can override and use middleware that inherits from OmniAuth

module OmniAuth
  module Strategies
    class FacebookWithExceptionHandling < OmniAuth::Strategies::Facebook
      def call
        begin
          super
        raise OmniAuth::Unauthorized => e
          #handle appropriately in rack context here
        end
      end
    end
  end
end

Rails.application.config.middleware.use OmniAuth::Builder do
  provider OmniAuth::Strategies::FacebookWithExceptionHandling, 
    api_key, #your api key 
    secret_key, #your secret key
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文