omniauth-shopify-oauth2 1.1.14 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e88a21bce303e6c6d637d58c50a2bfa7d75dfe86
4
- data.tar.gz: bced14c2fffa27c843425fe95dd088dbb5866c28
3
+ metadata.gz: 3c2fdff942824597bca290d63c4d5f2e8ad3fea3
4
+ data.tar.gz: 175291a1282389126084a7d32c2fd7c4965faa9c
5
5
  SHA512:
6
- metadata.gz: 575588b874c8c8d1774070ed66fbf55e8d70f9b82f9097c6f97e1ccb3423e792b82b400c2c3e481527eb60f5f54a69092aebd5715a5211845033bdc3a3be974c
7
- data.tar.gz: 3aa7bb0263d3c262f6c93aa39ce8729d92225fe712c9f0acc46560aed468f8c9dc125b4820a20273add6d4490b424215ba89565ac8c9fd8488d74dcc4dbe0f17
6
+ metadata.gz: f905cd84cc0df6a8b1ef9d61ae432e0aaeb2d88547b77e4ddf5ca09c991ef181d131690a5ec6022553ff31b1246875cc76fd7420911cb463d021f459a72b8b4b
7
+ data.tar.gz: e909527c818473c35ae5e9caf4f3afaffe38e9fdf835fc7bff13fe41c1be8ab2e81f71f1fca02011013d78303d1baccb222fb9e2c6cd7be9704fd9645c668e31
@@ -0,0 +1,2 @@
1
+ enabled:
2
+ - cla
data/.travis.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 2.1.0
3
+ - 2.1.9
4
4
  - 2.2.2
data/Gemfile CHANGED
@@ -1,3 +1,7 @@
1
1
  source "https://rubygems.org"
2
2
 
3
3
  gemspec
4
+
5
+ if Gem::Version.new(RUBY_VERSION) < Gem::Version.new("2.2")
6
+ gem 'rack', '~> 1.6'
7
+ end
data/README.md CHANGED
@@ -26,8 +26,20 @@ Rails.application.config.middleware.use OmniAuth::Builder do
26
26
  end
27
27
  ```
28
28
 
29
+ Authenticate the user by having them visit /auth/shopify with a `shop` query parameter of their shop's myshopify.com domain. For example, the following form could be used
30
+
31
+ ```html
32
+ <form action="/auth/shopify" method="get">
33
+ <label for="shop">Enter your store's URL:</label>
34
+ <input type="text" name="shop" placeholder="your-shop-url.myshopify.com">
35
+ <button type="submit">Log In</button>
36
+ </form>
37
+ ```
38
+
29
39
  ## Configuring
30
40
 
41
+ ### Scope
42
+
31
43
  You can configure the scope, which you pass in to the `provider` method via a `Hash`:
32
44
 
33
45
  * `scope`: A comma-separated list of permissions you want to request from the user. See [the Shopify API docs](http://docs.shopify.com/api/tutorials/oauth) for a full list of available permissions.
@@ -40,6 +52,19 @@ Rails.application.config.middleware.use OmniAuth::Builder do
40
52
  end
41
53
  ```
42
54
 
55
+ ### Online Access
56
+
57
+ Shopify offers two different types of access tokens: [online access and offline access](https://help.shopify.com/api/getting-started/authentication/oauth/api-access-modes). You can configure for online-access by passing the `per_user_permissions` option:
58
+
59
+ ```
60
+ Rails.application.config.middleware.use OmniAuth::Builder do
61
+ provider :shopify, ENV['SHOPIFY_API_KEY'],
62
+ ENV['SHOPIFY_SHARED_SECRET'],
63
+ :scope => 'read_orders',
64
+ :per_user_permissions => true
65
+ end
66
+ ```
67
+
43
68
  ## Authentication Hash
44
69
 
45
70
  Here's an example *Authentication Hash* available in `request.env['omniauth.auth']`:
@@ -1,5 +1,5 @@
1
1
  module OmniAuth
2
2
  module Shopify
3
- VERSION = "1.1.14"
3
+ VERSION = "2.0.0"
4
4
  end
5
5
  end
@@ -18,17 +18,35 @@ module OmniAuth
18
18
  option :callback_url
19
19
  option :myshopify_domain, 'myshopify.com'
20
20
 
21
- # When `true`, the authorization phase will fail if the granted scopes
22
- # mismatch the requested scopes.
23
- option :validate_granted_scopes, true
21
+ # When `true`, the user's permission level will apply (in addition to
22
+ # the requested access scope) when making API requests to Shopify.
23
+ option :per_user_permissions, false
24
24
 
25
25
  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
28
36
  }
29
37
 
30
38
  uid { URI.parse(options[:client_options][:site]).host }
31
39
 
40
+ extra do
41
+ if access_token
42
+ {
43
+ 'associated_user' => access_token['associated_user'],
44
+ 'associated_user_scope' => access_token['associated_user_scope'],
45
+ 'scope' => access_token['scope'],
46
+ }
47
+ end
48
+ end
49
+
32
50
  def valid_site?
33
51
  !!(/\A(https|http)\:\/\/[a-zA-Z0-9][a-zA-Z0-9\-]*\.#{Regexp.quote(options[:myshopify_domain])}[\/]?\z/ =~ options[:client_options][:site])
34
52
  end
@@ -71,8 +89,12 @@ module OmniAuth
71
89
  OpenSSL::HMAC.hexdigest(OpenSSL::Digest::SHA256.new, secret, encoded_params)
72
90
  end
73
91
 
92
+ def valid_permissions?(token)
93
+ token && (options[:per_user_permissions] == !token['associated_user'].nil?)
94
+ end
95
+
74
96
  def fix_https
75
- options[:client_options][:site].gsub!(/\Ahttp\:/, 'https:')
97
+ options[:client_options][:site] = options[:client_options][:site].gsub(/\Ahttp\:/, 'https:')
76
98
  end
77
99
 
78
100
  def setup_phase
@@ -96,6 +118,9 @@ module OmniAuth
96
118
  unless valid_scope?(token)
97
119
  return fail!(:invalid_scope, CallbackError.new(:invalid_scope, "Scope does not match, it may have been tampered with."))
98
120
  end
121
+ unless valid_permissions?(token)
122
+ return fail!(:invalid_permissions, CallbackError.new(:invalid_permissions, "Requested API access mode does not match."))
123
+ end
99
124
 
100
125
  super
101
126
  end
@@ -107,6 +132,7 @@ module OmniAuth
107
132
  def authorize_params
108
133
  super.tap do |params|
109
134
  params[:scope] = normalized_scopes(params[:scope] || DEFAULT_SCOPE).join(SCOPE_DELIMITER)
135
+ params[:grant_options] = ['per-user'] if options[:per_user_permissions]
110
136
  end
111
137
  end
112
138
 
@@ -6,7 +6,7 @@ Gem::Specification.new do |s|
6
6
  s.name = 'omniauth-shopify-oauth2'
7
7
  s.version = OmniAuth::Shopify::VERSION
8
8
  s.authors = ['Denis Odorcic']
9
- s.email = ['denis.odorcic@shopify.com']
9
+ s.email = ['gems@shopify.com']
10
10
  s.summary = 'Shopify strategy for OmniAuth'
11
11
  s.homepage = 'https://github.com/Shopify/omniauth-shopify-oauth2'
12
12
  s.license = 'MIT'
@@ -15,8 +15,10 @@ Gem::Specification.new do |s|
15
15
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
16
16
  s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
17
17
  s.require_paths = ['lib']
18
+ s.required_ruby_version = '>= 2.1.9'
18
19
 
19
- s.add_runtime_dependency 'omniauth-oauth2', '~> 1.2'
20
+ s.add_runtime_dependency 'omniauth-oauth2', '~> 1.5.0'
21
+ s.add_runtime_dependency 'activesupport'
20
22
 
21
23
  s.add_development_dependency 'minitest', '~> 5.6'
22
24
  s.add_development_dependency 'fakeweb', '~> 1.3'
@@ -29,6 +29,12 @@ describe OmniAuth::Strategies::Shopify do
29
29
  subject.options[:client_options][:site].should eq('https://foo.bar/')
30
30
  end
31
31
 
32
+ it 'replaces http scheme by https with an immutable string' do
33
+ @options = {:client_options => {:site => 'http://foo.bar/'.freeze}}
34
+ subject.fix_https
35
+ subject.options[:client_options][:site].should eq('https://foo.bar/')
36
+ end
37
+
32
38
  it 'does not replace https scheme' do
33
39
  @options = {:client_options => {:site => 'https://foo.bar/'}}
34
40
  subject.fix_https
@@ -13,11 +13,19 @@ class IntegrationTest < Minitest::Test
13
13
  def test_authorize
14
14
  response = authorize('snowdevil.myshopify.com')
15
15
  assert_equal 302, response.status
16
- assert_match /\A#{Regexp.quote("https://snowdevil.myshopify.com/admin/oauth/authorize?")}/, response.location
16
+ assert_match %r{\A#{Regexp.quote(shopify_authorize_url)}}, response.location
17
17
  redirect_params = Rack::Utils.parse_query(URI(response.location).query)
18
18
  assert_equal "123", redirect_params['client_id']
19
19
  assert_equal "https://app.example.com/auth/shopify/callback", redirect_params['redirect_uri']
20
20
  assert_equal "read_products", redirect_params['scope']
21
+ assert_nil redirect_params['grant_options']
22
+ end
23
+
24
+ def test_authorize_includes_auth_type_when_per_user_permissions_are_requested
25
+ build_app(per_user_permissions: true)
26
+ response = authorize('snowdevil.myshopify.com')
27
+ redirect_params = Rack::Utils.parse_query(URI(response.location).query)
28
+ assert_equal 'per-user', redirect_params['grant_options[]']
21
29
  end
22
30
 
23
31
  def test_authorize_overrides_site_with_https_scheme
@@ -26,8 +34,8 @@ class IntegrationTest < Minitest::Test
26
34
  env['omniauth.strategy'].options[:client_options][:site] = "http://#{params['shop']}"
27
35
  }
28
36
 
29
- response = authorize('snowdevil.myshopify.com')
30
- assert_match /\A#{Regexp.quote("https://snowdevil.myshopify.com/admin/oauth/authorize?")}/, response.location
37
+ response = request.get('https://app.example.com/auth/shopify?shop=snowdevil.myshopify.com')
38
+ assert_match %r{\A#{Regexp.quote(shopify_authorize_url)}}, response.location
31
39
  end
32
40
 
33
41
  def test_site_validation
@@ -40,6 +48,7 @@ class IntegrationTest < Minitest::Test
40
48
  'user@snowdevil.myshopify.com', # shop contains user
41
49
  'snowdevil.myshopify.com:22', # shop contains port
42
50
  ].each do |shop, valid|
51
+ @shop = shop
43
52
  response = authorize(shop)
44
53
  assert_auth_failure(response, 'invalid_site')
45
54
 
@@ -125,7 +134,10 @@ class IntegrationTest < Minitest::Test
125
134
 
126
135
  response = request.get("https://app.example.com/auth/shopify/callback?#{Rack::Utils.build_query(params)}",
127
136
  input: body,
128
- "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
+ })
129
141
 
130
142
  assert_auth_failure(response, 'invalid_signature')
131
143
  end
@@ -140,25 +152,33 @@ class IntegrationTest < Minitest::Test
140
152
  env['omniauth.strategy'].options[:client_options][:site] = "https://#{shop}"
141
153
  }
142
154
 
143
- response = authorize('snowdevil')
155
+ response = request.get("https://app.example.com/auth/shopify?shop=snowdevil.myshopify.dev:3000")
144
156
  assert_equal 302, response.status
145
- assert_match /\A#{Regexp.quote("https://snowdevil.myshopify.dev:3000/admin/oauth/authorize?")}/, response.location
157
+ assert_match %r{\A#{Regexp.quote("https://snowdevil.myshopify.dev:3000/admin/oauth/authorize?")}}, response.location
146
158
  redirect_params = Rack::Utils.parse_query(URI(response.location).query)
147
159
  assert_equal 'read_products,read_orders,write_content', redirect_params['scope']
148
160
  assert_equal 'https://app.example.com/admin/auth/legacy/callback', redirect_params['redirect_uri']
149
161
  end
150
162
 
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
+
151
172
  def test_unnecessary_read_scopes_are_removed
152
173
  build_app scope: 'read_content,read_products,write_products',
153
174
  callback_path: '/admin/auth/legacy/callback',
154
175
  myshopify_domain: 'myshopify.dev:3000',
155
176
  setup: lambda { |env|
156
177
  shop = Rack::Request.new(env).GET['shop']
157
- shop += ".myshopify.dev:3000" unless shop.include?(".")
158
178
  env['omniauth.strategy'].options[:client_options][:site] = "https://#{shop}"
159
179
  }
160
180
 
161
- response = authorize('snowdevil')
181
+ response = request.get("https://app.example.com/auth/shopify?shop=snowdevil.myshopify.dev:3000")
162
182
  assert_equal 302, response.status
163
183
  redirect_params = Rack::Utils.parse_query(URI(response.location).query)
164
184
  assert_equal 'read_content,write_products', redirect_params['scope']
@@ -178,7 +198,7 @@ class IntegrationTest < Minitest::Test
178
198
  def test_callback_with_mismatching_scope_fails
179
199
  access_token = SecureRandom.hex(16)
180
200
  code = SecureRandom.hex(16)
181
- expect_access_token_request(access_token, 'some_invalid_scope')
201
+ expect_access_token_request(access_token, 'some_invalid_scope', nil)
182
202
 
183
203
  response = callback(sign_params(shop: 'snowdevil.myshopify.com', code: code, state: opts["rack.session"]["omniauth.state"]))
184
204
 
@@ -247,6 +267,44 @@ class IntegrationTest < Minitest::Test
247
267
  assert_callback_success(response, access_token, code)
248
268
  end
249
269
 
270
+ def test_callback_when_per_user_permissions_are_present_but_not_requested
271
+ build_app(scope: 'scope', per_user_permissions: false)
272
+
273
+ access_token = SecureRandom.hex(16)
274
+ code = SecureRandom.hex(16)
275
+ expect_access_token_request(access_token, 'scope', { id: 1, email: 'bob@bobsen.com'})
276
+
277
+ response = callback(sign_params(shop: 'snowdevil.myshopify.com', code: code, state: opts["rack.session"]["omniauth.state"]))
278
+
279
+ assert_equal 302, response.status
280
+ assert_equal '/auth/failure?message=invalid_permissions&strategy=shopify', response.location
281
+ end
282
+
283
+ def test_callback_when_per_user_permissions_are_not_present_but_requested
284
+ build_app(scope: 'scope', per_user_permissions: true)
285
+
286
+ access_token = SecureRandom.hex(16)
287
+ code = SecureRandom.hex(16)
288
+ expect_access_token_request(access_token, 'scope', nil)
289
+
290
+ response = callback(sign_params(shop: 'snowdevil.myshopify.com', code: code, state: opts["rack.session"]["omniauth.state"]))
291
+
292
+ assert_equal 302, response.status
293
+ assert_equal '/auth/failure?message=invalid_permissions&strategy=shopify', response.location
294
+ end
295
+
296
+ def test_callback_works_when_per_user_permissions_are_present_and_requested
297
+ build_app(scope: 'scope', per_user_permissions: true)
298
+
299
+ access_token = SecureRandom.hex(16)
300
+ code = SecureRandom.hex(16)
301
+ expect_access_token_request(access_token, 'scope', { id: 1, email: 'bob@bobsen.com'})
302
+
303
+ response = callback(sign_params(shop: 'snowdevil.myshopify.com', code: code, state: opts["rack.session"]["omniauth.state"]))
304
+
305
+ assert_equal 200, response.status
306
+ end
307
+
250
308
  private
251
309
 
252
310
  def sign_params(params)
@@ -259,9 +317,9 @@ class IntegrationTest < Minitest::Test
259
317
  params
260
318
  end
261
319
 
262
- def expect_access_token_request(access_token, scope)
320
+ def expect_access_token_request(access_token, scope, associated_user=nil)
263
321
  FakeWeb.register_uri(:post, "https://snowdevil.myshopify.com/admin/oauth/access_token",
264
- body: JSON.dump(access_token: access_token, scope: scope),
322
+ body: JSON.dump(access_token: access_token, scope: scope, associated_user: associated_user),
265
323
  content_type: 'application/json')
266
324
  end
267
325
 
@@ -282,7 +340,7 @@ class IntegrationTest < Minitest::Test
282
340
  def assert_auth_failure(response, reason)
283
341
  assert_nil FakeWeb.last_request
284
342
  assert_equal 302, response.status
285
- assert_match /\A#{Regexp.quote("/auth/failure?message=#{reason}")}/, response.location
343
+ assert_match %r{\A#{Regexp.quote("/auth/failure?message=#{reason}")}}, response.location
286
344
  end
287
345
 
288
346
  def build_app(options={})
@@ -299,11 +357,17 @@ class IntegrationTest < Minitest::Test
299
357
  @app = Rack::Session::Cookie.new(app, secret: SecureRandom.hex(64))
300
358
  end
301
359
 
360
+ def shop
361
+ @shop ||= 'snowdevil.myshopify.com'
362
+ end
363
+
302
364
  def authorize(shop)
303
- 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)
304
367
  end
305
368
 
306
369
  def callback(params)
370
+ @opts['rack.session']['shopify.omniauth_params'] = { shop: shop }
307
371
  request.get("https://app.example.com/auth/shopify/callback?#{Rack::Utils.build_query(params)}", opts)
308
372
  end
309
373
 
@@ -314,4 +378,8 @@ class IntegrationTest < Minitest::Test
314
378
  def request
315
379
  Rack::MockRequest.new(@app)
316
380
  end
381
+
382
+ def shopify_authorize_url
383
+ "https://snowdevil.myshopify.com/admin/oauth/authorize?"
384
+ end
317
385
  end
data/test/test_helper.rb CHANGED
@@ -5,6 +5,7 @@ require 'omniauth-shopify-oauth2'
5
5
  require 'minitest/autorun'
6
6
  require 'fakeweb'
7
7
  require 'json'
8
+ require 'active_support/core_ext/hash'
8
9
 
9
10
  OmniAuth.config.logger = Logger.new(nil)
10
11
  FakeWeb.allow_net_connect = false
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: omniauth-shopify-oauth2
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.14
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Denis Odorcic
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-02-25 00:00:00.000000000 Z
11
+ date: 2018-11-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: omniauth-oauth2
@@ -16,14 +16,28 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '1.2'
19
+ version: 1.5.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '1.2'
26
+ version: 1.5.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: minitest
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -68,11 +82,12 @@ dependencies:
68
82
  version: '0'
69
83
  description:
70
84
  email:
71
- - denis.odorcic@shopify.com
85
+ - gems@shopify.com
72
86
  executables: []
73
87
  extensions: []
74
88
  extra_rdoc_files: []
75
89
  files:
90
+ - ".github/probots.yml"
76
91
  - ".gitignore"
77
92
  - ".travis.yml"
78
93
  - Gemfile
@@ -101,7 +116,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
101
116
  requirements:
102
117
  - - ">="
103
118
  - !ruby/object:Gem::Version
104
- version: '0'
119
+ version: 2.1.9
105
120
  required_rubygems_version: !ruby/object:Gem::Requirement
106
121
  requirements:
107
122
  - - ">="
@@ -109,7 +124,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
109
124
  version: '0'
110
125
  requirements: []
111
126
  rubyforge_project:
112
- rubygems_version: 2.2.3
127
+ rubygems_version: 2.6.14
113
128
  signing_key:
114
129
  specification_version: 4
115
130
  summary: Shopify strategy for OmniAuth