Skip to content

Commit 1c4a28c

Browse files
authored
Merge pull request #71 from Shopify/get-shop-params-from-session
Get shop from session by default, fallback to shop param
2 parents 7750af0 + 10914fc commit 1c4a28c

File tree

5 files changed

+37
-9
lines changed

5 files changed

+37
-9
lines changed

lib/omniauth/shopify/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module OmniAuth
22
module Shopify
3-
VERSION = "1.2.1"
3+
VERSION = "2.0.0"
44
end
55
end

lib/omniauth/strategies/shopify.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,16 @@ class Shopify < OmniAuth::Strategies::OAuth2
2323
option :per_user_permissions, false
2424

2525
option :setup, proc { |env|
26-
request = Rack::Request.new(env)
27-
env['omniauth.strategy'].options[:client_options][:site] = "https://#{request.GET['shop']}"
26+
strategy = env['omniauth.strategy']
27+
28+
shopify_auth_params = strategy.session['shopify.omniauth_params'] && strategy.session['shopify.omniauth_params'].with_indifferent_access
29+
shop = if shopify_auth_params && shopify_auth_params['shop']
30+
"https://#{shopify_auth_params['shop']}"
31+
else
32+
''
33+
end
34+
35+
strategy.options[:client_options][:site] = shop
2836
}
2937

3038
uid { URI.parse(options[:client_options][:site]).host }

omniauth-shopify-oauth2.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Gem::Specification.new do |s|
1818
s.required_ruby_version = '>= 2.1.9'
1919

2020
s.add_runtime_dependency 'omniauth-oauth2', '~> 1.5.0'
21+
s.add_runtime_dependency 'activesupport'
2122

2223
s.add_development_dependency 'minitest', '~> 5.6'
2324
s.add_development_dependency 'fakeweb', '~> 1.3'

test/integration_test.rb

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def test_authorize_overrides_site_with_https_scheme
3434
env['omniauth.strategy'].options[:client_options][:site] = "http://#{params['shop']}"
3535
}
3636

37-
response = authorize('snowdevil.myshopify.com')
37+
response = request.get('https://app.example.com/auth/shopify?shop=snowdevil.myshopify.com')
3838
assert_match %r{\A#{Regexp.quote(shopify_authorize_url)}}, response.location
3939
end
4040

@@ -48,6 +48,7 @@ def test_site_validation
4848
'[email protected]', # shop contains user
4949
'snowdevil.myshopify.com:22', # shop contains port
5050
].each do |shop, valid|
51+
@shop = shop
5152
response = authorize(shop)
5253
assert_auth_failure(response, 'invalid_site')
5354

@@ -133,7 +134,10 @@ def test_callback_rejects_body_params
133134

134135
response = request.get("https://app.example.com/auth/shopify/callback?#{Rack::Utils.build_query(params)}",
135136
input: body,
136-
"CONTENT_TYPE" => 'application/x-www-form-urlencoded')
137+
"CONTENT_TYPE" => 'application/x-www-form-urlencoded',
138+
'rack.session' => {
139+
'shopify.omniauth_params' => { shop: 'snowdevil.myshopify.com' }
140+
})
137141

138142
assert_auth_failure(response, 'invalid_signature')
139143
end
@@ -148,25 +152,33 @@ def test_provider_options
148152
env['omniauth.strategy'].options[:client_options][:site] = "https://#{shop}"
149153
}
150154

151-
response = authorize('snowdevil')
155+
response = request.get("https://app.example.com/auth/shopify?shop=snowdevil.myshopify.dev:3000")
152156
assert_equal 302, response.status
153157
assert_match %r{\A#{Regexp.quote("https://snowdevil.myshopify.dev:3000/admin/oauth/authorize?")}}, response.location
154158
redirect_params = Rack::Utils.parse_query(URI(response.location).query)
155159
assert_equal 'read_products,read_orders,write_content', redirect_params['scope']
156160
assert_equal 'https://app.example.com/admin/auth/legacy/callback', redirect_params['redirect_uri']
157161
end
158162

163+
def test_default_setup_reads_shop_from_session
164+
build_app
165+
response = authorize('snowdevil.myshopify.com')
166+
assert_equal 302, response.status
167+
assert_match %r{\A#{Regexp.quote("https://snowdevil.myshopify.com/admin/oauth/authorize?")}}, response.location
168+
redirect_params = Rack::Utils.parse_query(URI(response.location).query)
169+
assert_equal 'https://app.example.com/auth/shopify/callback', redirect_params['redirect_uri']
170+
end
171+
159172
def test_unnecessary_read_scopes_are_removed
160173
build_app scope: 'read_content,read_products,write_products',
161174
callback_path: '/admin/auth/legacy/callback',
162175
myshopify_domain: 'myshopify.dev:3000',
163176
setup: lambda { |env|
164177
shop = Rack::Request.new(env).GET['shop']
165-
shop += ".myshopify.dev:3000" unless shop.include?(".")
166178
env['omniauth.strategy'].options[:client_options][:site] = "https://#{shop}"
167179
}
168180

169-
response = authorize('snowdevil')
181+
response = request.get("https://app.example.com/auth/shopify?shop=snowdevil.myshopify.dev:3000")
170182
assert_equal 302, response.status
171183
redirect_params = Rack::Utils.parse_query(URI(response.location).query)
172184
assert_equal 'read_content,write_products', redirect_params['scope']
@@ -345,11 +357,17 @@ def build_app(options={})
345357
@app = Rack::Session::Cookie.new(app, secret: SecureRandom.hex(64))
346358
end
347359

360+
def shop
361+
@shop ||= 'snowdevil.myshopify.com'
362+
end
363+
348364
def authorize(shop)
349-
request.get("https://app.example.com/auth/shopify?shop=#{CGI.escape(shop)}", opts)
365+
@opts['rack.session']['shopify.omniauth_params'] = { shop: shop }
366+
request.get('https://app.example.com/auth/shopify', opts)
350367
end
351368

352369
def callback(params)
370+
@opts['rack.session']['shopify.omniauth_params'] = { shop: shop }
353371
request.get("https://app.example.com/auth/shopify/callback?#{Rack::Utils.build_query(params)}", opts)
354372
end
355373

test/test_helper.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
require 'minitest/autorun'
66
require 'fakeweb'
77
require 'json'
8+
require 'active_support/core_ext/hash'
89

910
OmniAuth.config.logger = Logger.new(nil)
1011
FakeWeb.allow_net_connect = false

0 commit comments

Comments
 (0)