简体   繁体   English

oauth2 omniauth.rb中的可选范围

[英]Optional scopes in oauth2 omniauth.rb

To authenticate users, I am using omniauth-google-oauth2 gem in rails. 要对用户进行身份验证,我在rails中使用omniauth-google-oauth2 gem。

Now I want to provide additional google api functionality, but I don't want to make it mandatory. 现在我想提供额外的谷歌api功能,但我不想强制要求。 But I can't figure out a way to make a specific scope optional. 但我无法想出一种方法来使特定范围可选。

I tried adding another entry in omniauth.rb, but it seems that it doesn't allow me to add multiple entries for the same provider (google_oauth2). 我尝试在omniauth.rb中添加另一个条目,但似乎它不允许我为同一个提供程序添加多个条目(google_oauth2)。

Is there a way to add any optional scopes in the rails application? 有没有办法在rails应用程序中添加任何可选范围?

You can use the name option for that. 您可以使用name选项。 This should work with any OmniAuth provider: 这适用于任何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

With omniauth-google-oauth2 though, you can take another approach: 有了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

Check out Google's Incremental Authorization guide for additional details. 有关其他详细信息,请查看Google的增量授权指南

Be aware though, that by changing the name of the provider, the OmniAuth URL will change to /auth/new_name and request.env['omniauth.auth']['provider'] will then return new_name . 但请注意,通过更改提供程序的name ,OmniAuth URL将更改为/auth/new_namerequest.env['omniauth.auth']['provider']将返回new_name

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM