oauth2omniauth.rb 中的可选范围

问题描述 投票:0回答:2

为了对用户进行身份验证,我在 Rails 中使用omniauth-google-oauth2 gem。

现在我想提供额外的 google api 功能,但我不想强制执行。但我无法找到一种方法来使特定范围成为可选。

我尝试在omniauth.rb中添加另一个条目,但它似乎不允许我为同一提供程序(google_oauth2)添加多个条目。

有没有办法在 Rails 应用程序中添加任何可选范围?

ruby-on-rails ruby-on-rails-3 oauth-2.0 ruby-on-rails-4 omniauth
2个回答
4
投票

您可以使用

name
选项。这应该适用于任何 OmniAuth 提供商:

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :google_oauth2, 'GOOGLE_CLIENT_ID', 'GOOGLE_CLIENT_SECRET', {
    :name => 'google'
  }

  provider :google_oauth2, 'GOOGLE_CLIENT_ID', 'GOOGLE_CLIENT_SECRET', {
    :name => 'google_full',
    :scope => 'original_scope, extra_scope'
  }
end

使用omniauth-google-oauth2,您可以采取另一种方法:

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :google_oauth2, 'GOOGLE_CLIENT_ID', 'GOOGLE_CLIENT_SECRET', {
    :name => 'google'
  }

  provider :google_oauth2, 'GOOGLE_CLIENT_ID', 'GOOGLE_CLIENT_SECRET', {
    :name => 'google_full',
    :scope => 'extra_scope',
    :prompt => 'consent',
    :include_granted_scopes => 'true'
  }
end

查看Google 的增量授权指南了解更多详细信息。

但请注意,通过更改提供商的

name
,OmniAuth URL 将更改为
/auth/new_name
,然后
request.env['omniauth.auth']['provider']
将返回
new_name


0
投票

对于在omniauth文件中设置正确范围属性有困难的任何人,请尝试在nunosilva的该线程建议的omniauth路径/url中设置范围字符串 https://github.com/zquestz/omniauth-google-oauth2/issues/143

© www.soinside.com 2019 - 2024. All rights reserved.