查找OmniAuth的已加载提供程序 [英] Find loaded providers for OmniAuth

查看:67
本文介绍了查找OmniAuth的已加载提供程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道 OmniAuth gem 已加载了哪些提供程序.我已经尝试过了:

I'd like to know which providers have been loaded for use by the OmniAuth gem. I've tried this:

OmniAuth::Strategies.constants  # a method provided by the standard lib Module class
# => [:Developer, :OAuth, :Twitter]

和这个:

OmniAuth.strategies.inspect # a method provided by the OmniAuth class, but which has no documentation or comments around it.
# => [OmniAuth::Strategies::OAuth]

我期望(或想要)的答案是[:Developer, :Twitter],因为在我的测试代码中,我仅显式加载了twitter,默认情况下加载了提供的开发人员.

The answer I'd expect (or want) is [:Developer, :Twitter] as in my test code I've only loaded twitter explicitly, and the developer provided is loaded by default.

(这就是所有不同的库可以根据运行OmniAuth加载正确的东西以使其正常工作的原因.)

(This is all so a different library can load the correct things for it to work, dependent on what OmniAuth is running.)

如果有办法,并且您知道,请告诉我.否则,我会务实地将OAuth从第一个示例中删除.

If there is a way and you know of it, please let me know. Otherwise I'll be pragmatic and knock OAuth out of the list from the first example.

Ruby是1.9.3,OmniAuth是v1.1.1

Ruby is 1.9.3 and OmniAuth is v1.1.1

推荐答案

OmniAuth :: Strategies列出了可用和已注册的策略.不是那些正在使用中的.如果您浏览 OmniAuth构建器的代码,您会发现各种策略在提供程序块中使用use作为中间件将它们传递到Rack,这使得对策略的跟踪变得更加困难.另一种实用"的方法是猴子修补OmniAuth Builder并跟踪提供程序.

OmniAuth::Strategies lists strategies available and registered. Not those that are in 'use'. If you dig through the code of OmniAuth builder, you will see that various strategies are passed onto Rack using use as middleware in the provider block, which makes tracking the strategies harder. Another "pragmatic" approach is to monkey patch OmniAuth Builder and track the providers.

module OmniAuth
  class Builder < ::Rack::Builder
    def provider_patch(klass, *args, &block)
      @@providers ||= []
      @@providers << klass
      old_provider(klass, *args, &block)
    end
    alias old_provider provider
    alias provider provider_patch
    class << self
      def providers
        @@providers
      end
    end
  end
end

在配置提供程序之前,请包括此修补程序.加载所有提供程序后,OmniAuth::Builder.providers将为您提供所需的阵列.

Include this patch before configuring your providers. Once all the providers have been loaded, OmniAuth::Builder.providers will give you the array you want.

即使开发人员策略可用,也不会加载.仅在您指定时加载

Even though Developer strategy is available, it is not loaded. It is only loaded if you specify

provider :developer

这篇关于查找OmniAuth的已加载提供程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆