Skip to content

Commit

Permalink
Use ApiAccess from shopify_api to encapsulate scopes and their common…
Browse files Browse the repository at this point in the history
… operations
  • Loading branch information
rezaansyed committed Jan 28, 2021
1 parent 3d618ea commit 97ea8b0
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 12 deletions.
1 change: 1 addition & 0 deletions lib/omniauth/shopify.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
require 'omniauth/shopify/version'
require 'omniauth/strategies/shopify'
require 'shopify_api'
19 changes: 7 additions & 12 deletions lib/omniauth/strategies/shopify.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ class Shopify < OmniAuth::Strategies::OAuth2
# Available scopes: content themes products customers orders script_tags shipping
# read_* or write_*
DEFAULT_SCOPE = 'read_products'
SCOPE_DELIMITER = ','
MINUTE = 60
CODE_EXPIRES_AFTER = 10 * MINUTE

Expand Down Expand Up @@ -74,16 +73,11 @@ def valid_signature?
end

def valid_scope?(token)
params = options.authorize_params.merge(options_for("authorize"))
return false unless token && params[:scope] && token['scope']
expected_scope = normalized_scopes(params[:scope]).sort
(expected_scope == token['scope'].split(SCOPE_DELIMITER).sort)
end

def normalized_scopes(scopes)
scope_list = scopes.to_s.split(SCOPE_DELIMITER).map(&:strip).reject(&:empty?).uniq
ignore_scopes = scope_list.map { |scope| scope =~ /\A(unauthenticated_)?write_(.*)\z/ && "#{$1}read_#{$2}" }.compact
scope_list - ignore_scopes
config = options.authorize_params.merge(options_for("authorize"))
return false unless token && config[:scope] && token['scope']
expected_api_access = ::ShopifyAPI::ApiAccess.new(config[:scope])
actual_api_access = ::ShopifyAPI::ApiAccess.new(token['scope'])
actual_api_access == expected_api_access
end

def self.encoded_params_for_signature(params)
Expand Down Expand Up @@ -146,7 +140,8 @@ def build_access_token

def authorize_params
super.tap do |params|
params[:scope] = normalized_scopes(params[:scope] || DEFAULT_SCOPE).join(SCOPE_DELIMITER)
scopes = params[:scope] || DEFAULT_SCOPE
params[:scope] = ::ShopifyAPI::ApiAccess.new(scopes).to_s
params[:grant_options] = ['per-user'] if options[:per_user_permissions]
end
end
Expand Down
1 change: 1 addition & 0 deletions omniauth-shopify-oauth2.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Gem::Specification.new do |s|

s.add_runtime_dependency 'omniauth-oauth2', ['~> 1.5', '< 1.7.1']
s.add_runtime_dependency 'activesupport'
s.add_runtime_dependency('shopify_api', '~> 9.3')

s.add_development_dependency 'minitest', '~> 5.6'
s.add_development_dependency 'rspec', '~> 3.9.0'
Expand Down

0 comments on commit 97ea8b0

Please sign in to comment.