shopify_app 12.0.0 → 17.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/CODEOWNERS +1 -0
- data/.github/ISSUE_TEMPLATE/bug-report.md +63 -0
- data/.github/ISSUE_TEMPLATE/config.yml +1 -0
- data/.github/ISSUE_TEMPLATE/feature-request.md +33 -0
- data/.github/PULL_REQUEST_TEMPLATE.md +22 -0
- data/.github/workflows/build.yml +41 -0
- data/.github/workflows/release.yml +24 -0
- data/.github/workflows/rubocop.yml +22 -0
- data/.gitignore +0 -2
- data/.rubocop.yml +14 -6
- data/CHANGELOG.md +180 -0
- data/CONTRIBUTING.md +76 -0
- data/Gemfile +5 -0
- data/Gemfile.lock +257 -0
- data/README.md +72 -487
- data/Rakefile +1 -0
- data/SECURITY.md +59 -0
- data/app/assets/images/storage_access.svg +1 -2
- data/app/assets/javascripts/shopify_app/itp_helper.js +6 -6
- data/app/assets/javascripts/shopify_app/storage_access.js +37 -7
- data/app/assets/javascripts/shopify_app/top_level_interaction.js +1 -1
- data/app/controllers/concerns/shopify_app/authenticated.rb +2 -1
- data/app/controllers/concerns/shopify_app/ensure_authenticated_links.rb +26 -0
- data/app/controllers/concerns/shopify_app/require_known_shop.rb +39 -0
- data/app/controllers/concerns/shopify_app/shop_access_scopes_verification.rb +32 -0
- data/app/controllers/shopify_app/authenticated_controller.rb +1 -0
- data/app/controllers/shopify_app/callback_controller.rb +113 -23
- data/app/controllers/shopify_app/extension_verification_controller.rb +2 -7
- data/app/controllers/shopify_app/sessions_controller.rb +62 -14
- data/app/controllers/shopify_app/webhooks_controller.rb +6 -5
- data/app/views/shopify_app/partials/_button_styles.html.erb +41 -36
- data/app/views/shopify_app/partials/_card_styles.html.erb +3 -3
- data/app/views/shopify_app/partials/_empty_state_styles.html.erb +28 -59
- data/app/views/shopify_app/partials/_form_styles.html.erb +56 -0
- data/app/views/shopify_app/partials/_layout_styles.html.erb +16 -1
- data/app/views/shopify_app/partials/_typography_styles.html.erb +6 -6
- data/app/views/shopify_app/sessions/enable_cookies.html.erb +3 -8
- data/app/views/shopify_app/sessions/new.html.erb +38 -110
- data/app/views/shopify_app/sessions/request_storage_access.html.erb +2 -2
- data/app/views/shopify_app/sessions/top_level_interaction.html.erb +21 -22
- data/config/locales/de.yml +11 -11
- data/config/locales/fi.yml +1 -1
- data/config/locales/nl.yml +8 -8
- data/config/locales/pt-BR.yml +1 -1
- data/config/locales/th.yml +4 -4
- data/config/locales/vi.yml +22 -0
- data/config/locales/zh-CN.yml +1 -1
- data/config/routes.rb +1 -0
- data/docs/Quickstart.md +15 -87
- data/docs/Releasing.md +18 -14
- data/docs/Troubleshooting.md +129 -4
- data/docs/Upgrading.md +126 -0
- data/docs/shopify_app/authentication.md +124 -0
- data/docs/shopify_app/engine.md +82 -0
- data/docs/shopify_app/generators.md +127 -0
- data/docs/shopify_app/handling-access-scopes-changes.md +14 -0
- data/docs/shopify_app/script-tags.md +28 -0
- data/docs/shopify_app/session-repository.md +88 -0
- data/docs/shopify_app/testing.md +38 -0
- data/docs/shopify_app/webhooks.md +72 -0
- data/karma.conf.js +1 -1
- data/lib/generators/shopify_app/add_after_authenticate_job/add_after_authenticate_job_generator.rb +5 -3
- data/lib/generators/shopify_app/add_after_authenticate_job/templates/after_authenticate_job.rb +1 -0
- data/lib/generators/shopify_app/add_marketing_activity_extension/add_marketing_activity_extension_generator.rb +2 -1
- data/lib/generators/shopify_app/add_marketing_activity_extension/templates/marketing_activities_controller.rb +4 -8
- data/lib/generators/shopify_app/add_webhook/add_webhook_generator.rb +5 -4
- data/lib/generators/shopify_app/add_webhook/templates/{webhook_job.rb → webhook_job.rb.tt} +5 -0
- data/lib/generators/shopify_app/app_proxy_controller/app_proxy_controller_generator.rb +4 -3
- data/lib/generators/shopify_app/app_proxy_controller/templates/app_proxy_controller.rb +3 -3
- data/lib/generators/shopify_app/app_proxy_controller/templates/app_proxy_route.rb +10 -9
- data/lib/generators/shopify_app/authenticated_controller/authenticated_controller_generator.rb +1 -1
- data/lib/generators/shopify_app/controllers/controllers_generator.rb +2 -1
- data/lib/generators/shopify_app/home_controller/home_controller_generator.rb +31 -3
- data/lib/generators/shopify_app/home_controller/templates/home_controller.rb +2 -0
- data/lib/generators/shopify_app/home_controller/templates/index.html.erb +66 -16
- data/lib/generators/shopify_app/home_controller/templates/unauthenticated_home_controller.rb +11 -0
- data/lib/generators/shopify_app/install/install_generator.rb +46 -11
- data/lib/generators/shopify_app/install/templates/embedded_app.html.erb +1 -1
- data/lib/generators/shopify_app/install/templates/flash_messages.js +0 -2
- data/lib/generators/shopify_app/install/templates/omniauth.rb +3 -1
- data/lib/generators/shopify_app/install/templates/shopify_app.rb.tt +25 -0
- data/lib/generators/shopify_app/install/templates/shopify_provider.rb.tt +8 -0
- data/lib/generators/shopify_app/install/templates/user_agent.rb +2 -1
- data/lib/generators/shopify_app/products_controller/products_controller_generator.rb +19 -0
- data/lib/generators/shopify_app/products_controller/templates/products_controller.rb +8 -0
- data/lib/generators/shopify_app/routes/routes_generator.rb +1 -0
- data/lib/generators/shopify_app/routes/templates/routes.rb +10 -9
- data/lib/generators/shopify_app/shop_model/shop_model_generator.rb +39 -7
- data/lib/generators/shopify_app/shop_model/templates/db/migrate/add_shop_access_scopes_column.erb +5 -0
- data/lib/generators/shopify_app/shop_model/templates/shop.rb +2 -1
- data/lib/generators/shopify_app/shopify_app_generator.rb +4 -3
- data/lib/generators/shopify_app/user_model/templates/db/migrate/add_user_access_scopes_column.erb +5 -0
- data/lib/generators/shopify_app/user_model/templates/user.rb +2 -1
- data/lib/generators/shopify_app/user_model/user_model_generator.rb +39 -7
- data/lib/generators/shopify_app/views/views_generator.rb +2 -1
- data/lib/shopify_app/access_scopes/noop_strategy.rb +13 -0
- data/lib/shopify_app/access_scopes/shop_strategy.rb +24 -0
- data/lib/shopify_app/access_scopes/user_strategy.rb +41 -0
- data/lib/shopify_app/configuration.rb +46 -11
- data/lib/shopify_app/controller_concerns/app_proxy_verification.rb +3 -3
- data/lib/shopify_app/controller_concerns/csrf_protection.rb +15 -0
- data/lib/shopify_app/controller_concerns/embedded_app.rb +3 -2
- data/lib/shopify_app/controller_concerns/localization.rb +1 -0
- data/lib/shopify_app/controller_concerns/login_protection.rb +105 -30
- data/lib/shopify_app/controller_concerns/payload_verification.rb +24 -0
- data/lib/shopify_app/controller_concerns/webhook_verification.rb +3 -18
- data/lib/shopify_app/engine.rb +27 -1
- data/lib/shopify_app/jobs/scripttags_manager_job.rb +1 -1
- data/lib/shopify_app/jobs/webhooks_manager_job.rb +1 -1
- data/lib/shopify_app/managers/scripttags_manager.rb +4 -3
- data/lib/shopify_app/managers/webhooks_manager.rb +4 -3
- data/lib/shopify_app/middleware/jwt_middleware.rb +42 -0
- data/lib/shopify_app/middleware/same_site_cookie_middleware.rb +19 -45
- data/lib/shopify_app/omniauth/omniauth_configuration.rb +64 -0
- data/lib/shopify_app/session/in_memory_session_store.rb +7 -3
- data/lib/shopify_app/session/in_memory_shop_session_store.rb +16 -0
- data/lib/shopify_app/session/in_memory_user_session_store.rb +16 -0
- data/lib/shopify_app/session/jwt.rb +63 -0
- data/lib/shopify_app/session/null_user_session_store.rb +22 -0
- data/lib/shopify_app/session/session_repository.rb +36 -14
- data/lib/shopify_app/session/session_storage.rb +1 -10
- data/lib/shopify_app/session/shop_session_storage.rb +42 -0
- data/lib/shopify_app/session/shop_session_storage_with_scopes.rb +58 -0
- data/lib/shopify_app/session/user_session_storage.rb +42 -0
- data/lib/shopify_app/session/user_session_storage_with_scopes.rb +58 -0
- data/lib/shopify_app/test_helpers/all.rb +2 -0
- data/lib/shopify_app/test_helpers/webhook_verification_helper.rb +17 -0
- data/lib/shopify_app/utils.rb +18 -5
- data/lib/shopify_app/version.rb +2 -1
- data/lib/shopify_app.rb +24 -5
- data/package.json +8 -9
- data/shopify_app.gemspec +15 -10
- data/translation.yml +1 -1
- data/yarn.lock +2120 -2168
- metadata +94 -20
- data/.github/ISSUE_TEMPLATE.md +0 -14
- data/.travis.yml +0 -27
- data/docs/install-on-dev-shop.png +0 -0
- data/docs/test-your-app.png +0 -0
- data/lib/generators/shopify_app/install/templates/shopify_app.rb +0 -15
- data/lib/generators/shopify_app/install/templates/shopify_provider.rb +0 -20
- data/lib/shopify_app/session/storage_strategies/shop_storage_strategy.rb +0 -23
- data/lib/shopify_app/session/storage_strategies/user_storage_strategy.rb +0 -24
- data/package-lock.json +0 -7224
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ShopifyApp
|
4
|
+
module AccessScopes
|
5
|
+
class ShopStrategy
|
6
|
+
class << self
|
7
|
+
def update_access_scopes?(shop_domain)
|
8
|
+
shop_access_scopes = shop_access_scopes(shop_domain)
|
9
|
+
configuration_access_scopes != shop_access_scopes
|
10
|
+
end
|
11
|
+
|
12
|
+
private
|
13
|
+
|
14
|
+
def shop_access_scopes(shop_domain)
|
15
|
+
ShopifyApp::SessionRepository.retrieve_shop_session_by_shopify_domain(shop_domain)&.access_scopes
|
16
|
+
end
|
17
|
+
|
18
|
+
def configuration_access_scopes
|
19
|
+
ShopifyAPI::ApiAccess.new(ShopifyApp.configuration.shop_access_scopes)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ShopifyApp
|
4
|
+
module AccessScopes
|
5
|
+
class UserStrategy
|
6
|
+
class InvalidInput < StandardError; end
|
7
|
+
|
8
|
+
class << self
|
9
|
+
def update_access_scopes?(user_id: nil, shopify_user_id: nil)
|
10
|
+
return update_access_scopes_for_user_id?(user_id) if user_id
|
11
|
+
return update_access_scopes_for_shopify_user_id?(shopify_user_id) if shopify_user_id
|
12
|
+
raise(InvalidInput, '#update_access_scopes? requires user_id or shopify_user_id parameter inputs')
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def update_access_scopes_for_user_id?(user_id)
|
18
|
+
user_access_scopes = user_access_scopes_by_user_id(user_id)
|
19
|
+
configuration_access_scopes != user_access_scopes
|
20
|
+
end
|
21
|
+
|
22
|
+
def update_access_scopes_for_shopify_user_id?(shopify_user_id)
|
23
|
+
user_access_scopes = user_access_scopes_by_shopify_user_id(shopify_user_id)
|
24
|
+
configuration_access_scopes != user_access_scopes
|
25
|
+
end
|
26
|
+
|
27
|
+
def user_access_scopes_by_user_id(user_id)
|
28
|
+
ShopifyApp::SessionRepository.retrieve_user_session(user_id)&.access_scopes
|
29
|
+
end
|
30
|
+
|
31
|
+
def user_access_scopes_by_shopify_user_id(shopify_user_id)
|
32
|
+
ShopifyApp::SessionRepository.retrieve_user_session_by_shopify_user_id(shopify_user_id)&.access_scopes
|
33
|
+
end
|
34
|
+
|
35
|
+
def configuration_access_scopes
|
36
|
+
ShopifyAPI::ApiAccess.new(ShopifyApp.configuration.user_access_scopes)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -1,6 +1,6 @@
|
|
1
|
+
# frozen_string_literal: true
|
1
2
|
module ShopifyApp
|
2
3
|
class Configuration
|
3
|
-
|
4
4
|
# Shopify App settings. These values should match the configuration
|
5
5
|
# for the app in your Shopify Partners page. Change your settings in
|
6
6
|
# `config/initializers/shopify_app.rb`
|
@@ -9,19 +9,20 @@ module ShopifyApp
|
|
9
9
|
attr_accessor :secret
|
10
10
|
attr_accessor :old_secret
|
11
11
|
attr_accessor :scope
|
12
|
+
attr_writer :shop_access_scopes
|
13
|
+
attr_writer :user_access_scopes
|
12
14
|
attr_accessor :embedded_app
|
13
15
|
alias_method :embedded_app?, :embedded_app
|
14
16
|
attr_accessor :webhooks
|
15
17
|
attr_accessor :scripttags
|
16
18
|
attr_accessor :after_authenticate_job
|
17
|
-
attr_reader :session_repository
|
18
|
-
attr_accessor :per_user_tokens
|
19
|
-
alias_method :per_user_tokens?, :per_user_tokens
|
20
19
|
attr_accessor :api_version
|
21
20
|
|
21
|
+
attr_accessor :reauth_on_access_scope_changes
|
22
|
+
|
22
23
|
# customise urls
|
23
24
|
attr_accessor :root_url
|
24
|
-
|
25
|
+
attr_writer :login_url
|
25
26
|
|
26
27
|
# customise ActiveJob queue names
|
27
28
|
attr_accessor :scripttags_manager_queue_name
|
@@ -37,24 +38,50 @@ module ShopifyApp
|
|
37
38
|
attr_accessor :webhook_jobs_namespace
|
38
39
|
|
39
40
|
# allow enabling of same site none on cookies
|
40
|
-
|
41
|
+
attr_writer :enable_same_site_none
|
42
|
+
|
43
|
+
# allow enabling jwt headers for authentication
|
44
|
+
attr_accessor :allow_jwt_authentication
|
45
|
+
|
46
|
+
attr_accessor :allow_cookie_authentication
|
41
47
|
|
42
48
|
def initialize
|
43
49
|
@root_url = '/'
|
44
50
|
@myshopify_domain = 'myshopify.com'
|
45
51
|
@scripttags_manager_queue_name = Rails.application.config.active_job.queue_name
|
46
52
|
@webhooks_manager_queue_name = Rails.application.config.active_job.queue_name
|
47
|
-
@per_user_tokens = false
|
48
53
|
@disable_webpacker = ENV['SHOPIFY_APP_DISABLE_WEBPACKER'].present?
|
54
|
+
@allow_cookie_authentication = true
|
49
55
|
end
|
50
56
|
|
51
57
|
def login_url
|
52
58
|
@login_url || File.join(@root_url, 'login')
|
53
59
|
end
|
54
60
|
|
55
|
-
def
|
56
|
-
|
57
|
-
|
61
|
+
def user_session_repository=(klass)
|
62
|
+
ShopifyApp::SessionRepository.user_storage = klass
|
63
|
+
end
|
64
|
+
|
65
|
+
def user_session_repository
|
66
|
+
ShopifyApp::SessionRepository.user_storage
|
67
|
+
end
|
68
|
+
|
69
|
+
def shop_session_repository=(klass)
|
70
|
+
ShopifyApp::SessionRepository.shop_storage = klass
|
71
|
+
end
|
72
|
+
|
73
|
+
def shop_session_repository
|
74
|
+
ShopifyApp::SessionRepository.shop_storage
|
75
|
+
end
|
76
|
+
|
77
|
+
def shop_access_scopes_strategy
|
78
|
+
return ShopifyApp::AccessScopes::NoopStrategy unless reauth_on_access_scope_changes
|
79
|
+
ShopifyApp::AccessScopes::ShopStrategy
|
80
|
+
end
|
81
|
+
|
82
|
+
def user_access_scopes_strategy
|
83
|
+
return ShopifyApp::AccessScopes::NoopStrategy unless reauth_on_access_scope_changes
|
84
|
+
ShopifyApp::AccessScopes::UserStrategy
|
58
85
|
end
|
59
86
|
|
60
87
|
def has_webhooks?
|
@@ -66,7 +93,15 @@ module ShopifyApp
|
|
66
93
|
end
|
67
94
|
|
68
95
|
def enable_same_site_none
|
69
|
-
@enable_same_site_none.nil? ? embedded_app? : @enable_same_site_none
|
96
|
+
!Rails.env.test? && (@enable_same_site_none.nil? ? embedded_app? : @enable_same_site_none)
|
97
|
+
end
|
98
|
+
|
99
|
+
def shop_access_scopes
|
100
|
+
@shop_access_scopes || scope
|
101
|
+
end
|
102
|
+
|
103
|
+
def user_access_scopes
|
104
|
+
@user_access_scopes || scope
|
70
105
|
end
|
71
106
|
end
|
72
107
|
|
@@ -1,14 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
1
2
|
module ShopifyApp
|
2
3
|
module AppProxyVerification
|
3
4
|
extend ActiveSupport::Concern
|
4
|
-
|
5
5
|
included do
|
6
6
|
skip_before_action :verify_authenticity_token, raise: false
|
7
7
|
before_action :verify_proxy_request
|
8
8
|
end
|
9
9
|
|
10
10
|
def verify_proxy_request
|
11
|
-
return head
|
11
|
+
return head(:forbidden) unless query_string_valid?(request.query_string)
|
12
12
|
end
|
13
13
|
|
14
14
|
private
|
@@ -26,7 +26,7 @@ module ShopifyApp
|
|
26
26
|
end
|
27
27
|
|
28
28
|
def calculated_signature(query_hash_without_signature)
|
29
|
-
sorted_params = query_hash_without_signature.collect{|k,v| "#{k}=#{Array(v).join(',')}"}.sort.join
|
29
|
+
sorted_params = query_hash_without_signature.collect { |k, v| "#{k}=#{Array(v).join(',')}" }.sort.join
|
30
30
|
|
31
31
|
OpenSSL::HMAC.hexdigest(
|
32
32
|
OpenSSL::Digest.new('sha256'),
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
module ShopifyApp
|
3
|
+
module CsrfProtection
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
included do
|
6
|
+
protect_from_forgery with: :exception, unless: :valid_session_token?
|
7
|
+
end
|
8
|
+
|
9
|
+
private
|
10
|
+
|
11
|
+
def valid_session_token?
|
12
|
+
request.env['jwt.shopify_domain']
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -1,11 +1,12 @@
|
|
1
|
+
# frozen_string_literal: true
|
1
2
|
module ShopifyApp
|
2
3
|
module EmbeddedApp
|
3
4
|
extend ActiveSupport::Concern
|
4
5
|
|
5
6
|
included do
|
6
7
|
if ShopifyApp.configuration.embedded_app?
|
7
|
-
after_action
|
8
|
-
layout
|
8
|
+
after_action(:set_esdk_headers)
|
9
|
+
layout('embedded_app')
|
9
10
|
end
|
10
11
|
end
|
11
12
|
|
@@ -11,56 +11,101 @@ module ShopifyApp
|
|
11
11
|
|
12
12
|
included do
|
13
13
|
after_action :set_test_cookie
|
14
|
-
rescue_from ActiveResource::UnauthorizedAccess, :
|
14
|
+
rescue_from ActiveResource::UnauthorizedAccess, with: :close_session
|
15
15
|
end
|
16
16
|
|
17
|
-
|
18
|
-
|
17
|
+
ACCESS_TOKEN_REQUIRED_HEADER = 'X-Shopify-API-Request-Failure-Unauthorized'
|
18
|
+
|
19
|
+
def activate_shopify_session
|
20
|
+
if user_session_expected? && user_session.blank?
|
21
|
+
signal_access_token_required
|
22
|
+
return redirect_to_login
|
23
|
+
end
|
24
|
+
|
25
|
+
return redirect_to_login if current_shopify_session.blank?
|
26
|
+
|
19
27
|
clear_top_level_oauth_cookie
|
20
28
|
|
21
29
|
begin
|
22
|
-
ShopifyAPI::Base.activate_session(
|
30
|
+
ShopifyAPI::Base.activate_session(current_shopify_session)
|
23
31
|
yield
|
24
32
|
ensure
|
25
33
|
ShopifyAPI::Base.clear_session
|
26
34
|
end
|
27
35
|
end
|
28
36
|
|
29
|
-
def
|
30
|
-
|
31
|
-
|
32
|
-
@shop_session ||= ShopifyApp::SessionRepository.retrieve(session[:shopify_user]['id'])
|
33
|
-
else
|
34
|
-
return unless session[:shopify]
|
35
|
-
@shop_session ||= ShopifyApp::SessionRepository.retrieve(session[:shopify])
|
37
|
+
def current_shopify_session
|
38
|
+
@current_shopify_session ||= begin
|
39
|
+
user_session || shop_session
|
36
40
|
end
|
37
41
|
end
|
38
42
|
|
39
|
-
def
|
40
|
-
|
41
|
-
|
42
|
-
sessions_do_not_match = session[:user_session] != params[:session] # current user is different from stored user
|
43
|
+
def user_session
|
44
|
+
user_session_by_jwt || user_session_by_cookie
|
45
|
+
end
|
43
46
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
+
def user_session_by_jwt
|
48
|
+
return unless ShopifyApp.configuration.allow_jwt_authentication
|
49
|
+
return unless jwt_shopify_user_id
|
50
|
+
ShopifyApp::SessionRepository.retrieve_user_session_by_shopify_user_id(jwt_shopify_user_id)
|
51
|
+
end
|
52
|
+
|
53
|
+
def user_session_by_cookie
|
54
|
+
return unless ShopifyApp.configuration.allow_cookie_authentication
|
55
|
+
return unless session[:user_id].present?
|
56
|
+
ShopifyApp::SessionRepository.retrieve_user_session(session[:user_id])
|
57
|
+
end
|
58
|
+
|
59
|
+
def shop_session
|
60
|
+
shop_session_by_jwt || shop_session_by_cookie
|
61
|
+
end
|
62
|
+
|
63
|
+
def shop_session_by_jwt
|
64
|
+
return unless ShopifyApp.configuration.allow_jwt_authentication
|
65
|
+
return unless jwt_shopify_domain
|
66
|
+
ShopifyApp::SessionRepository.retrieve_shop_session_by_shopify_domain(jwt_shopify_domain)
|
67
|
+
end
|
68
|
+
|
69
|
+
def shop_session_by_cookie
|
70
|
+
return unless ShopifyApp.configuration.allow_cookie_authentication
|
71
|
+
return unless session[:shop_id].present?
|
72
|
+
ShopifyApp::SessionRepository.retrieve_shop_session(session[:shop_id])
|
73
|
+
end
|
74
|
+
|
75
|
+
def login_again_if_different_user_or_shop
|
76
|
+
if session[:user_session].present? && params[:session].present? # session data was sent/stored correctly
|
77
|
+
clear_session = session[:user_session] != params[:session] # current user is different from stored user
|
47
78
|
end
|
48
79
|
|
49
|
-
if
|
80
|
+
if current_shopify_session &&
|
81
|
+
params[:shop] && params[:shop].is_a?(String) &&
|
82
|
+
(current_shopify_session.domain != params[:shop])
|
50
83
|
clear_session = true
|
51
84
|
end
|
52
85
|
|
53
86
|
if clear_session
|
54
|
-
|
87
|
+
clear_shopify_session
|
55
88
|
redirect_to_login
|
56
89
|
end
|
57
90
|
end
|
58
91
|
|
92
|
+
def signal_access_token_required
|
93
|
+
response.set_header(ACCESS_TOKEN_REQUIRED_HEADER, "true")
|
94
|
+
end
|
95
|
+
|
59
96
|
protected
|
60
97
|
|
98
|
+
def jwt_shopify_domain
|
99
|
+
request.env['jwt.shopify_domain']
|
100
|
+
end
|
101
|
+
|
102
|
+
def jwt_shopify_user_id
|
103
|
+
request.env['jwt.shopify_user_id']
|
104
|
+
end
|
105
|
+
|
61
106
|
def redirect_to_login
|
62
107
|
if request.xhr?
|
63
|
-
head
|
108
|
+
head(:unauthorized)
|
64
109
|
else
|
65
110
|
if request.get?
|
66
111
|
path = request.path
|
@@ -70,18 +115,19 @@ module ShopifyApp
|
|
70
115
|
path = referer.path
|
71
116
|
query = "#{referer.query}&#{sanitized_params.to_query}"
|
72
117
|
end
|
73
|
-
session[:return_to] = "#{path}?#{query}"
|
118
|
+
session[:return_to] = query.blank? ? path.to_s : "#{path}?#{query}"
|
74
119
|
redirect_to(login_url_with_optional_shop)
|
75
120
|
end
|
76
121
|
end
|
77
122
|
|
78
123
|
def close_session
|
79
|
-
|
124
|
+
clear_shopify_session
|
80
125
|
redirect_to(login_url_with_optional_shop)
|
81
126
|
end
|
82
127
|
|
83
|
-
def
|
84
|
-
session[:
|
128
|
+
def clear_shopify_session
|
129
|
+
session[:shop_id] = nil
|
130
|
+
session[:user_id] = nil
|
85
131
|
session[:shopify_domain] = nil
|
86
132
|
session[:shopify_user] = nil
|
87
133
|
session[:user_session] = nil
|
@@ -100,8 +146,10 @@ module ShopifyApp
|
|
100
146
|
query_params = {}
|
101
147
|
query_params[:shop] = sanitized_params[:shop] if params[:shop].present?
|
102
148
|
|
103
|
-
|
104
|
-
|
149
|
+
return_to = RedirectSafely.make_safe(session[:return_to] || params[:return_to], nil)
|
150
|
+
|
151
|
+
if return_to.present? && return_to_param_required?
|
152
|
+
query_params[:return_to] = return_to
|
105
153
|
end
|
106
154
|
|
107
155
|
has_referer_shop_name = referer_sanitized_shop_name.present?
|
@@ -121,14 +169,18 @@ module ShopifyApp
|
|
121
169
|
|
122
170
|
def fullpage_redirect_to(url)
|
123
171
|
if ShopifyApp.configuration.embedded_app?
|
124
|
-
render
|
172
|
+
render('shopify_app/shared/redirect', layout: false,
|
173
|
+
locals: { url: url, current_shopify_domain: current_shopify_domain })
|
125
174
|
else
|
126
|
-
redirect_to
|
175
|
+
redirect_to(url)
|
127
176
|
end
|
128
177
|
end
|
129
178
|
|
130
179
|
def current_shopify_domain
|
131
|
-
shopify_domain = sanitized_shop_name ||
|
180
|
+
shopify_domain = sanitized_shop_name ||
|
181
|
+
jwt_shopify_domain ||
|
182
|
+
session[:shopify_domain]
|
183
|
+
|
132
184
|
return shopify_domain if shopify_domain.present?
|
133
185
|
|
134
186
|
raise ShopifyDomainNotFound
|
@@ -163,7 +215,30 @@ module ShopifyApp
|
|
163
215
|
end
|
164
216
|
|
165
217
|
def return_address
|
218
|
+
return base_return_address unless ShopifyApp.configuration.allow_jwt_authentication
|
219
|
+
return_address_with_params(shop: current_shopify_domain)
|
220
|
+
rescue ShopifyDomainNotFound
|
221
|
+
base_return_address
|
222
|
+
end
|
223
|
+
|
224
|
+
def base_return_address
|
166
225
|
session.delete(:return_to) || ShopifyApp.configuration.root_url
|
167
226
|
end
|
227
|
+
|
228
|
+
def return_address_with_params(params)
|
229
|
+
uri = URI(base_return_address)
|
230
|
+
uri.query = CGI.parse(uri.query.to_s)
|
231
|
+
.symbolize_keys
|
232
|
+
.transform_values { |v| v.one? ? v.first : v }
|
233
|
+
.merge(params)
|
234
|
+
.to_query
|
235
|
+
uri.to_s
|
236
|
+
end
|
237
|
+
|
238
|
+
private
|
239
|
+
|
240
|
+
def user_session_expected?
|
241
|
+
!ShopifyApp.configuration.user_session_repository.blank? && ShopifyApp::SessionRepository.user_storage.present?
|
242
|
+
end
|
168
243
|
end
|
169
244
|
end
|