shopify应用程序授权陷入无限重定向

时间:2015-04-07 20:45:34

标签: ruby-on-rails oauth shopify

我已将嵌入式Shopify应用程序部署到生产服务器。在此项目中,shopify_app gem用于处理常见方案,例如使用omniauth-shopify-oauth2 gem进行身份验证。
虽然应用程序安装在商店中,但应用程序无法正确进行身份验证,因此不会存储会话。似乎在将应用程序的控制权交给omniauth-oauth2 gem之后,它将应用程序重定向到预期的根路径,但由于会话未保存且控制器具有arround_filter :shopify_session,因此无限循环是在/auth/shopify?shop=foobar.myshopify.com/login?shop=foobar.myshopify.com之间创建。最后,shopify管理面板,引发shopify the application cannot be loaded, please check that your browser allows third party cookies错误,我们无法打开该应用程序。

当应用程序服务器在localhost上但在生产服务器上不起作用时,此应用程序运行良好

以下是我认为可能有用的代码的一些部分:

服务器日志:

Started GET "/?hmac=HMAC&shop=SHOP&signature=SIGNATURE&timestamp=TIMESTAMP" for 127.0.0.1 at 2015-04-07 20:33:11 +0000
Processing by MainController#index as HTML
  Parameters: {"hmac"=>"HMAC", "shop"=>"SHOP", "signature"=>"SIGNATURE", "timestamp"=>"TIMESTAMP"}
shop_session: 
Redirected to http://PRODUCTION_SERVER_IP/login?shop=SHOP
Completed 302 Found in 1ms (ActiveRecord: 0.0ms)
Started GET "/login?shop=SHOP" for 127.0.0.1 at 2015-04-07 20:33:11 +0000
Processing by SessionsController#new as HTML
  Parameters: {"shop"=>"SHOP"}
  Rendered common/iframe_redirect.html.erb (0.0ms)
Completed 200 OK in 1ms (Views: 0.3ms | ActiveRecord: 0.0ms)
Started GET "/auth/shopify?shop=SHOP" for 127.0.0.1 at 2015-04-07 20:33:12 +0000
Started GET "/?code=CODE&hmac=HMAC&shop=SHOP&signature=SIGNATURE&timestamp=1428438796" for 127.0.0.1 at 2015-04-07 20:33:12 +0000
Processing by MainController#index as HTML
  Parameters: {"code"=>"CODE", "hmac"=>"HMAC", "shop"=>"SHOP", "signature"=>"SIGNATURE", "timestamp"=>"1428438796"}
Redirected to http://PRODUCTION_SERVER_IP/login?shop=Shop
Completed 302 Found in 1ms (ActiveRecord: 0.0ms)
Started GET "/login?shop=SHOP" for 127.0.0.1 at 2015-04-07 20:33:13 +0000
Processing by SessionsController#new as HTML
  Parameters: {"shop"=>"SHOP"}
  Rendered common/iframe_redirect.html.erb (0.0ms)
Completed 200 OK in 1ms (Views: 0.4ms | ActiveRecord: 0.0ms)
Started GET "/auth/shopify?shop=SHOP" for 127.0.0.1 at 2015-04-07 20:33:13 +0000
Started GET "/?
// And this pattern goes on...

nginx配置

upstream app_name {
  server 127.0.0.1:3000;
  server 127.0.0.1:3001;
  server 127.0.0.1:3002;
}
server {
  listen   80;
  server_name PRODUCTION_SERVER_IP;

  access_log /var/www/app_name/log/access.log;
  error_log  /var/www/app_name/log/error.log;
  root     /var/www/app_name/current;
  index index.html;

  location / {
    proxy_set_header  X-Real-IP  $remote_addr;
    proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header  Host $http_host;
    proxy_redirect  off;
    try_files /system/maintenance.html $uri $uri/index.html $uri.html @ruby;
  }

  location @ruby {
    proxy_pass http://app_name;
    proxy_set_header Host $host;
  }
}

配置/初始化/ omniauth.rb

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :shopify,
    ShopifyApp.configuration.api_key,
    ShopifyApp.configuration.secret,

    # Example permission scopes - see http://docs.shopify.com/api/tutorials/oauth for full listing
    scope: 'read_products,read_themes, write_themes, read_customers',
    myshopify_domain: ShopifyApp.configuration.myshopify_domain.presence || "myshopify.com",
    callback_url: 'http://PRODUCTION_SERVER_IP',
    setup: lambda {|env|
             params = Rack::Utils.parse_query(env['QUERY_STRING'])
             site_url = "https://#{params['shop']}"
             env['omniauth.strategy'].options[:client_options][:site] = site_url
           }
end

config / initializers / shopify_session_repository.rb

ShopifySessionRepository.storage = "Shop"

app / controllers / sessions_controller.rb

class SessionsController < ApplicationController
  layout :false

  def new
    authenticate if params[:shop]
  end

  def show
    if response = request.env['omniauth.auth']
      sess = ShopifyAPI::Session.new(params[:shop],response['credentials']['token'])    
      session[:shopify] = ShopifySessionRepository.store(sess)
      flash[:notice] = "Logged in"
      redirect_to return_address
    else
      flash[:error] = "Could not log in to Shopify store."
      redirect_to :action => 'new'
    end
  end

  protected

  def authenticate
    #
    # Instead of doing a backend redirect we need to do a javascript redirect
    # here. Open the app/views/commom/iframe_redirect.html.erb file to understand why.
    #
    if shop_name = sanitize_shop_param(params)
      @redirect_url = "/auth/shopify?shop=#{shop_name}"
      render "/common/iframe_redirect", :format => [:html], layout: false
    else
      redirect_to return_address
    end
  end

  def return_address
    session[:return_to] || root_url
  end

  def sanitize_shop_param(params)
    return unless params[:shop].present?
    return unless domain = ShopifyApp.configuration.myshopify_domain.presence || "myshopify.com"

    name = params[:shop].to_s.strip
    name += ".#{domain}" if !name.include?(domain) && !name.include?(".")
    name.sub!(%r|https?://|, '')

    u = URI("http://#{name}")
    u.host.ends_with?(".#{domain}") ? u.host : nil
  end
end

知道为什么在授权后没有存储会话?

4 个答案:

答案 0 :(得分:0)

如果回调网址与您用于注册的网址相同,则可能会发生这种情况。尝试调查请求参数,或者给另一个回调url返回。您可能会看到第一个请求与后续请求不同。

答案 1 :(得分:0)

似乎更改shopify_app.rb中的以下代码行可以解决此问题。 config.embedded_app = false 我认为这会引起新的问题。

答案 2 :(得分:0)

以及适当的解决方案。复制并粘贴来自官方Shopify App git存储库的代码,包括会话和回调控制器。然后在您的gemfile中更新Shopify API和App gem。 我认为它最近已修复。

答案 3 :(得分:0)

在我的浏览器中启用第三方cookie可以为我解决此问题。