简体   繁体   English

在运行时切换omniauth-shopify-oauth2 gem的提供程序?

[英]Switch provider for the omniauth-shopify-oauth2 gem in runtime?

The initializer for the omniauth-shopify-oauth2 gem is supposed to look like this: omn​​iauth-shopify-oauth2 gem的初始化程序应该看起来像这样:

# config/initializers/omniauth.rb
Rails.application.config.middleware.use OmniAuth::Builder do
  provider :shopify, ENV['SHOPIFY_API_KEY'], ENV['SHOPIFY_SHARED_SECRET']
end

However, in our Rails app resides a few different brands who offers the same functionality. 但是,在我们的Rails应用程序中存在着几个提供相同功能的不同品牌。 Throughout the entire app, the request.domain of a request determines which brand you are exposed to ( brand1.example.com , brand2.example.com , etc.). 在整个应用程序中,请求的request.domain决定了您接触的品牌( brand1.example.combrand2.example.com等)。

We can easily store brand specific credentials and redirect the users to the brand specific authorization path: 我们可以轻松存储品牌特定的凭据,并将用户重定向到品牌特定的授权路径:

https://example.myshopify.com/admin/oauth/authorize?client_id=brand1&scope=read_orders,read_products&redirect_uri=https://brand1.example.com/auth/shopify/callback

But I can't figure out how we can have different providers for the middleware, chosen based on the visited request.domain . 但是我不知道如何为中间件提供不同的提供程序,这些提供程序是基于访问的request.domain选择的。 Any idea how to set this up? 知道如何设置吗?

Omniauth provides documentation on Dynamic Providers , which will be helpful here. Omniauth提供了有关Dynamic Providers的文档,这将对您有所帮助。 Something like: 就像是:

# config/initializers/omniauth.rb
Rails.application.config.middleware.use OmniAuth::Builder do
  provider :shopify, setup: lambda do |env|

    # Do logic to get correct credentials for request.
    # For example, if you store the credentials on a model called Brand,
    # and have it keyed on "subdomain":
    request = ActionDispatch::Request.new(env)
    brand = Brand.find_by(subdomain: request.subdomain)

    env['omniauth.strategy'].options.merge!({
      client_id: brand.client_id,
      client_secret: brand.client_secret
    })

    # `site` needs to be set. This is part of the shopify provider setup phase, which we are overriding
    env['omniauth.strategy'].options[:client_options][:site] = "https://#{ request.GET['shop'] }"
  end
end

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

相关问题 使用omniauth-google-oauth2 gem进行设计 - Devise with omniauth-google-oauth2 gem 使用Omniauth gem初始化后更改Twitter提供程序 - Change Twitter provider after initialization with Omniauth gem Omniauth:如何在运行时设置身份验证提供程序详细信息 - Omniauth: How to set authentication provider details at runtime Linkedin Omniauth gem 'omniauth-linkedin-oauth2' 验证失败:invalid_credentials: OAuth2::Error - Linkedin Omniauth gem 'omniauth-linkedin-oauth2' giving Authentication failure! invalid_credentials: OAuth2::Error 在Rails 2.3.8和ruby 1.8.7中使用omniauth-google-oauth2 gem - Using omniauth-google-oauth2 gem in rails 2.3.8 and ruby 1.8.7 如何在Rails中使用具有增量授权的omniauth-google-oauth2 gem? - How to use omniauth-google-oauth2 gem with incremental authorization in Rails? Bundler :: GemRequireError:尝试加载gem'omniauth-oauth2'时发生错误 - Bundler::GemRequireError: There was an error while trying to load the gem 'omniauth-oauth2' omn​​iauth-wordpress-oauth2-plugin gem安装错误 - omniauth-wordpress-oauth2-plugin gem install error Oauth2提供商(门卫?)是Oauth2消费者(Devise + OmniAuth)的登录用户 - Oauth2 provider (Doorkeeper?) being an Oauth2 consumer (Devise+OmniAuth) for login Ruby on Rails gem omniauth-facebook的oauth-token的用途是什么? - What is the purpose of oauth-token of Ruby on Rails gem omniauth-facebook?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM