覆盖omniauth gem中设置的默认范围

时间:2013-06-23 03:48:38

标签: ruby-on-rails-3 oauth-2.0 omniauth

我试图覆盖omniauth gem中设置的默认范围。我已经尝试通过初始化程序设置范围,并尝试将范围留空。我希望能够做到

/auth/<provider>?scope=<scope>

作为登录的链接。

但是,无论我如何在

中设置提供程序
config/initializers/omniauth.rb

例如:

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :<provider>, <app_uid>, <app_secret>,
  :scope => "",
  :client_options => {
    :site          => "https://api.<provider_url>",
    :authorize_url => "https://api.<provider_url>/oauth/authorize",
    :token_url     => "https://api.<provider_url>/oauth/token"
  }
end

它仍然尝试使用

中的默认范围设置发送omniauth / oauth-2请求
omniauth-<provider> gem

如何通过将范围参数传递给传递给提供者的网址来覆盖gem中声明的默认范围?

1 个答案:

答案 0 :(得分:2)

我可以使用以下初始化程序完成此操作。

Rails.application.config.middleware.use OmniAuth::Builder do
   :<provider>, <app_uid>, <app_secret>,
  :setup => lambda { |env| env["omniauth.strategy"].options[:scope] = env["rack.session"]["omniauth.params"]["scope"]},
  :client_options => {
    :site          => "https://api.<provider_url>",
    :authorize_url => "https://api.<provider_url>/oauth/authorize",
    :token_url     => "https://api.<provider_url>/oauth/token"
  }

end