shopify_app 7.2.0 → 17.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.babelrc +5 -0
- 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/probots.yml +2 -0
- data/.github/workflows/build.yml +38 -0
- data/.github/workflows/release.yml +24 -0
- data/.github/workflows/rubocop.yml +22 -0
- data/.gitignore +4 -1
- data/.nvmrc +1 -0
- data/.rubocop.yml +18 -0
- data/.ruby-version +1 -0
- data/CHANGELOG.md +465 -0
- data/CONTRIBUTING.md +76 -0
- data/Gemfile +7 -0
- data/Gemfile.lock +256 -0
- data/README.md +73 -288
- data/Rakefile +1 -0
- data/SECURITY.md +59 -0
- data/app/assets/images/storage_access.svg +1 -0
- data/app/assets/javascripts/shopify_app/enable_cookies.js +3 -0
- data/app/assets/javascripts/shopify_app/itp_helper.js +40 -0
- data/app/assets/javascripts/shopify_app/partition_cookies.js +8 -0
- data/app/assets/javascripts/shopify_app/redirect.js +33 -0
- data/app/assets/javascripts/shopify_app/request_storage_access.js +3 -0
- data/app/assets/javascripts/shopify_app/storage_access.js +154 -0
- data/app/assets/javascripts/shopify_app/storage_access_redirect.js +17 -0
- data/app/assets/javascripts/shopify_app/top_level.js +2 -0
- data/app/assets/javascripts/shopify_app/top_level_interaction.js +11 -0
- data/app/controllers/concerns/shopify_app/authenticated.rb +16 -0
- 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 +5 -5
- data/app/controllers/shopify_app/callback_controller.rb +196 -0
- data/app/controllers/shopify_app/extension_verification_controller.rb +15 -0
- data/app/controllers/shopify_app/sessions_controller.rb +190 -2
- data/app/controllers/shopify_app/webhooks_controller.rb +16 -7
- data/app/views/shopify_app/partials/_button_styles.html.erb +109 -0
- data/app/views/shopify_app/partials/_card_styles.html.erb +33 -0
- data/app/views/shopify_app/partials/_empty_state_styles.html.erb +98 -0
- data/app/views/shopify_app/partials/_form_styles.html.erb +56 -0
- data/app/views/shopify_app/partials/_layout_styles.html.erb +182 -0
- data/app/views/shopify_app/partials/_typography_styles.html.erb +35 -0
- data/app/views/shopify_app/sessions/enable_cookies.html.erb +70 -0
- data/app/views/shopify_app/sessions/new.html.erb +39 -83
- data/app/views/shopify_app/sessions/request_storage_access.html.erb +68 -0
- data/app/views/shopify_app/sessions/top_level_interaction.html.erb +63 -0
- data/app/views/shopify_app/shared/redirect.html.erb +23 -0
- data/config/locales/cs.yml +23 -0
- data/config/locales/da.yml +20 -0
- data/config/locales/de.yml +22 -0
- data/config/locales/en.yml +12 -1
- data/config/locales/es.yml +21 -3
- data/config/locales/fi.yml +20 -0
- data/config/locales/fr.yml +23 -0
- data/config/locales/hi.yml +23 -0
- data/config/locales/it.yml +21 -0
- data/config/locales/ja.yml +17 -0
- data/config/locales/ko.yml +19 -0
- data/config/locales/ms.yml +22 -0
- data/config/locales/nb.yml +21 -0
- data/config/locales/nl.yml +21 -0
- data/config/locales/pl.yml +21 -0
- data/config/locales/pt-BR.yml +21 -0
- data/config/locales/pt-PT.yml +22 -0
- data/config/locales/sv.yml +21 -0
- data/config/locales/th.yml +20 -0
- data/config/locales/tr.yml +22 -0
- data/config/locales/vi.yml +22 -0
- data/config/locales/zh-CN.yml +16 -0
- data/config/locales/zh-TW.yml +16 -0
- data/config/routes.rb +12 -1
- data/docs/Quickstart.md +31 -0
- data/docs/Releasing.md +21 -0
- data/docs/Troubleshooting.md +16 -0
- data/docs/Upgrading.md +110 -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 +8 -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 +44 -0
- data/lib/generators/shopify_app/add_after_authenticate_job/add_after_authenticate_job_generator.rb +47 -0
- data/lib/generators/shopify_app/add_after_authenticate_job/templates/after_authenticate_job.rb +11 -0
- data/lib/generators/shopify_app/add_marketing_activity_extension/add_marketing_activity_extension_generator.rb +40 -0
- data/lib/generators/shopify_app/add_marketing_activity_extension/templates/marketing_activities_controller.rb +62 -0
- 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/app_proxy_controller/templates/index.html.erb +2 -2
- data/lib/generators/shopify_app/authenticated_controller/authenticated_controller_generator.rb +15 -0
- data/lib/generators/shopify_app/authenticated_controller/templates/authenticated_controller.rb +5 -0
- data/lib/generators/shopify_app/controllers/controllers_generator.rb +2 -1
- data/lib/generators/shopify_app/home_controller/home_controller_generator.rb +31 -9
- data/lib/generators/shopify_app/home_controller/templates/home_controller.rb +6 -1
- data/lib/generators/shopify_app/home_controller/templates/index.html.erb +70 -6
- data/lib/generators/shopify_app/home_controller/templates/unauthenticated_home_controller.rb +11 -0
- data/lib/generators/shopify_app/install/install_generator.rb +78 -27
- data/lib/generators/shopify_app/install/templates/_flash_messages.html.erb +1 -13
- data/lib/generators/shopify_app/install/templates/embedded_app.html.erb +12 -11
- data/lib/generators/shopify_app/install/templates/flash_messages.js +24 -0
- data/lib/generators/shopify_app/install/templates/omniauth.rb +3 -1
- data/lib/generators/shopify_app/install/templates/session_store.rb +4 -0
- data/lib/generators/shopify_app/install/templates/shopify_app.js +15 -0
- data/lib/generators/shopify_app/install/templates/shopify_app.rb.tt +25 -0
- data/lib/generators/shopify_app/install/templates/shopify_app_index.js +2 -0
- data/lib/generators/shopify_app/install/templates/shopify_provider.rb.tt +8 -0
- data/lib/generators/shopify_app/install/templates/user_agent.rb +6 -0
- 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/rotate_shopify_token_job/rotate_shopify_token_job_generator.rb +16 -0
- data/lib/generators/shopify_app/rotate_shopify_token_job/templates/rotate_shopify_token.rake +17 -0
- data/lib/generators/shopify_app/rotate_shopify_token_job/templates/rotate_shopify_token_job.rb +42 -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 +42 -14
- 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/db/migrate/{create_shops.rb → create_shops.erb} +1 -1
- data/lib/generators/shopify_app/shop_model/templates/shop.rb +6 -2
- data/lib/generators/shopify_app/shopify_app_generator.rb +5 -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/db/migrate/create_users.erb +16 -0
- data/lib/generators/shopify_app/user_model/templates/user.rb +8 -0
- data/lib/generators/shopify_app/user_model/templates/users.yml +4 -0
- data/lib/generators/shopify_app/user_model/user_model_generator.rb +70 -0
- 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 +69 -5
- data/lib/shopify_app/{app_proxy_verification.rb → controller_concerns/app_proxy_verification.rb} +4 -9
- data/lib/shopify_app/controller_concerns/csrf_protection.rb +15 -0
- data/lib/shopify_app/controller_concerns/embedded_app.rb +20 -0
- data/lib/shopify_app/controller_concerns/itp.rb +45 -0
- data/lib/shopify_app/controller_concerns/localization.rb +23 -0
- data/lib/shopify_app/controller_concerns/login_protection.rb +244 -0
- data/lib/shopify_app/controller_concerns/payload_verification.rb +24 -0
- data/lib/shopify_app/controller_concerns/webhook_verification.rb +23 -0
- data/lib/shopify_app/engine.rb +40 -0
- data/lib/shopify_app/jobs/scripttags_manager_job.rb +16 -0
- data/lib/shopify_app/{webhooks_manager_job.rb → jobs/webhooks_manager_job.rb} +3 -2
- data/lib/shopify_app/{scripttags_manager.rb → managers/scripttags_manager.rb} +25 -8
- data/lib/shopify_app/{webhooks_manager.rb → managers/webhooks_manager.rb} +6 -5
- data/lib/shopify_app/middleware/jwt_middleware.rb +42 -0
- data/lib/shopify_app/middleware/same_site_cookie_middleware.rb +34 -0
- data/lib/shopify_app/omniauth/omniauth_configuration.rb +64 -0
- data/lib/shopify_app/session/in_memory_session_store.rb +31 -0
- 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 +56 -0
- data/lib/shopify_app/session/session_storage.rb +20 -0
- 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 +24 -4
- data/lib/shopify_app/version.rb +2 -1
- data/lib/shopify_app.rb +65 -24
- data/package.json +27 -0
- data/service.yml +7 -0
- data/shipit.rubygems.yml +3 -0
- data/shopify_app.gemspec +20 -9
- data/translation.yml +7 -0
- data/webpack.config.js +24 -0
- data/yarn.lock +5215 -0
- metadata +274 -43
- data/.travis.yml +0 -17
- data/Gemfile.rails50 +0 -5
- data/Gemfile.ruby22 +0 -6
- data/Gemfile.ruby22.rails50 +0 -9
- data/ISSUE_TEMPLATE.md +0 -14
- data/QUICKSTART.md +0 -72
- data/RELEASING +0 -13
- data/lib/generators/shopify_app/home_controller/templates/shopify_app_ready_script.html.erb +0 -11
- data/lib/generators/shopify_app/install/templates/shopify_app.rb +0 -9
- data/lib/generators/shopify_app/install/templates/shopify_provider.rb +0 -4
- data/lib/generators/shopify_app/install/templates/shopify_session_repository.rb +0 -23
- data/lib/generators/shopify_app/shop_model/templates/shopify_session_repository.rb +0 -7
- data/lib/shopify_app/in_memory_session_store.rb +0 -25
- data/lib/shopify_app/login_protection.rb +0 -103
- data/lib/shopify_app/scripttags_manager_job.rb +0 -15
- data/lib/shopify_app/session_storage.rb +0 -23
- data/lib/shopify_app/sessions_concern.rb +0 -101
- data/lib/shopify_app/shop.rb +0 -15
- data/lib/shopify_app/shopify_session_repository.rb +0 -34
- data/lib/shopify_app/webhook_verification.rb +0 -39
data/lib/shopify_app/{app_proxy_verification.rb → controller_concerns/app_proxy_verification.rb}
RENAMED
@@ -1,19 +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
|
-
|
7
|
-
skip_before_action :verify_authenticity_token, raise: false
|
8
|
-
else
|
9
|
-
skip_before_action :verify_authenticity_token
|
10
|
-
end
|
11
|
-
|
6
|
+
skip_before_action :verify_authenticity_token, raise: false
|
12
7
|
before_action :verify_proxy_request
|
13
8
|
end
|
14
9
|
|
15
10
|
def verify_proxy_request
|
16
|
-
return head
|
11
|
+
return head(:forbidden) unless query_string_valid?(request.query_string)
|
17
12
|
end
|
18
13
|
|
19
14
|
private
|
@@ -31,7 +26,7 @@ module ShopifyApp
|
|
31
26
|
end
|
32
27
|
|
33
28
|
def calculated_signature(query_hash_without_signature)
|
34
|
-
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
|
35
30
|
|
36
31
|
OpenSSL::HMAC.hexdigest(
|
37
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
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
module ShopifyApp
|
3
|
+
module EmbeddedApp
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
included do
|
7
|
+
if ShopifyApp.configuration.embedded_app?
|
8
|
+
after_action(:set_esdk_headers)
|
9
|
+
layout('embedded_app')
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
private
|
14
|
+
|
15
|
+
def set_esdk_headers
|
16
|
+
response.set_header('P3P', 'CP="Not used"')
|
17
|
+
response.headers.except!('X-Frame-Options')
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ShopifyApp
|
4
|
+
# Cookie management helpers required for ITP implementation
|
5
|
+
module Itp
|
6
|
+
private
|
7
|
+
|
8
|
+
def set_test_cookie
|
9
|
+
return unless ShopifyApp.configuration.embedded_app?
|
10
|
+
return unless user_agent_can_partition_cookies
|
11
|
+
|
12
|
+
session['shopify.cookies_persist'] = true
|
13
|
+
end
|
14
|
+
|
15
|
+
def set_top_level_oauth_cookie
|
16
|
+
session['shopify.top_level_oauth'] = true
|
17
|
+
end
|
18
|
+
|
19
|
+
def clear_top_level_oauth_cookie
|
20
|
+
session.delete('shopify.top_level_oauth')
|
21
|
+
end
|
22
|
+
|
23
|
+
def user_agent_is_mobile
|
24
|
+
user_agent = BrowserSniffer.new(request.user_agent).browser_info
|
25
|
+
|
26
|
+
user_agent[:name].to_s.match(/Shopify\sMobile/)
|
27
|
+
end
|
28
|
+
|
29
|
+
def user_agent_is_pos
|
30
|
+
user_agent = BrowserSniffer.new(request.user_agent).browser_info
|
31
|
+
|
32
|
+
user_agent[:name].to_s.match(/Shopify\sPOS/)
|
33
|
+
end
|
34
|
+
|
35
|
+
def user_agent_can_partition_cookies
|
36
|
+
user_agent = BrowserSniffer.new(request.user_agent).browser_info
|
37
|
+
|
38
|
+
is_safari = user_agent[:name].to_s.match(/Safari/)
|
39
|
+
|
40
|
+
return false unless is_safari
|
41
|
+
|
42
|
+
user_agent[:version].to_s.match(/12\.0/)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
module ShopifyApp
|
3
|
+
module Localization
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
included do
|
7
|
+
before_action :set_locale
|
8
|
+
end
|
9
|
+
|
10
|
+
private
|
11
|
+
|
12
|
+
def set_locale
|
13
|
+
if params[:locale]
|
14
|
+
session[:locale] = params[:locale]
|
15
|
+
else
|
16
|
+
session[:locale] ||= I18n.default_locale
|
17
|
+
end
|
18
|
+
I18n.locale = session[:locale]
|
19
|
+
rescue I18n::InvalidLocale
|
20
|
+
I18n.locale = I18n.default_locale
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,244 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'browser_sniffer'
|
4
|
+
|
5
|
+
module ShopifyApp
|
6
|
+
module LoginProtection
|
7
|
+
extend ActiveSupport::Concern
|
8
|
+
include ShopifyApp::Itp
|
9
|
+
|
10
|
+
class ShopifyDomainNotFound < StandardError; end
|
11
|
+
|
12
|
+
included do
|
13
|
+
after_action :set_test_cookie
|
14
|
+
rescue_from ActiveResource::UnauthorizedAccess, with: :close_session
|
15
|
+
end
|
16
|
+
|
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
|
+
|
27
|
+
clear_top_level_oauth_cookie
|
28
|
+
|
29
|
+
begin
|
30
|
+
ShopifyAPI::Base.activate_session(current_shopify_session)
|
31
|
+
yield
|
32
|
+
ensure
|
33
|
+
ShopifyAPI::Base.clear_session
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def current_shopify_session
|
38
|
+
@current_shopify_session ||= begin
|
39
|
+
user_session || shop_session
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def user_session
|
44
|
+
user_session_by_jwt || user_session_by_cookie
|
45
|
+
end
|
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
|
78
|
+
end
|
79
|
+
|
80
|
+
if current_shopify_session &&
|
81
|
+
params[:shop] && params[:shop].is_a?(String) &&
|
82
|
+
(current_shopify_session.domain != params[:shop])
|
83
|
+
clear_session = true
|
84
|
+
end
|
85
|
+
|
86
|
+
if clear_session
|
87
|
+
clear_shopify_session
|
88
|
+
redirect_to_login
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
def signal_access_token_required
|
93
|
+
response.set_header(ACCESS_TOKEN_REQUIRED_HEADER, "true")
|
94
|
+
end
|
95
|
+
|
96
|
+
protected
|
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
|
+
|
106
|
+
def redirect_to_login
|
107
|
+
if request.xhr?
|
108
|
+
head(:unauthorized)
|
109
|
+
else
|
110
|
+
if request.get?
|
111
|
+
path = request.path
|
112
|
+
query = sanitized_params.to_query
|
113
|
+
else
|
114
|
+
referer = URI(request.referer || "/")
|
115
|
+
path = referer.path
|
116
|
+
query = "#{referer.query}&#{sanitized_params.to_query}"
|
117
|
+
end
|
118
|
+
session[:return_to] = query.blank? ? path.to_s : "#{path}?#{query}"
|
119
|
+
redirect_to(login_url_with_optional_shop)
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
def close_session
|
124
|
+
clear_shopify_session
|
125
|
+
redirect_to(login_url_with_optional_shop)
|
126
|
+
end
|
127
|
+
|
128
|
+
def clear_shopify_session
|
129
|
+
session[:shop_id] = nil
|
130
|
+
session[:user_id] = nil
|
131
|
+
session[:shopify_domain] = nil
|
132
|
+
session[:shopify_user] = nil
|
133
|
+
session[:user_session] = nil
|
134
|
+
end
|
135
|
+
|
136
|
+
def login_url_with_optional_shop(top_level: false)
|
137
|
+
url = ShopifyApp.configuration.login_url
|
138
|
+
|
139
|
+
query_params = login_url_params(top_level: top_level)
|
140
|
+
|
141
|
+
url = "#{url}?#{query_params.to_query}" if query_params.present?
|
142
|
+
url
|
143
|
+
end
|
144
|
+
|
145
|
+
def login_url_params(top_level:)
|
146
|
+
query_params = {}
|
147
|
+
query_params[:shop] = sanitized_params[:shop] if params[:shop].present?
|
148
|
+
|
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
|
153
|
+
end
|
154
|
+
|
155
|
+
has_referer_shop_name = referer_sanitized_shop_name.present?
|
156
|
+
|
157
|
+
if has_referer_shop_name
|
158
|
+
query_params[:shop] ||= referer_sanitized_shop_name
|
159
|
+
end
|
160
|
+
|
161
|
+
query_params[:top_level] = true if top_level
|
162
|
+
query_params
|
163
|
+
end
|
164
|
+
|
165
|
+
def return_to_param_required?
|
166
|
+
native_params = %i[shop hmac timestamp locale protocol return_to]
|
167
|
+
request.path != '/' || sanitized_params.except(*native_params).any?
|
168
|
+
end
|
169
|
+
|
170
|
+
def fullpage_redirect_to(url)
|
171
|
+
if ShopifyApp.configuration.embedded_app?
|
172
|
+
render('shopify_app/shared/redirect', layout: false,
|
173
|
+
locals: { url: url, current_shopify_domain: current_shopify_domain })
|
174
|
+
else
|
175
|
+
redirect_to(url)
|
176
|
+
end
|
177
|
+
end
|
178
|
+
|
179
|
+
def current_shopify_domain
|
180
|
+
shopify_domain = sanitized_shop_name ||
|
181
|
+
jwt_shopify_domain ||
|
182
|
+
session[:shopify_domain]
|
183
|
+
|
184
|
+
return shopify_domain if shopify_domain.present?
|
185
|
+
|
186
|
+
raise ShopifyDomainNotFound
|
187
|
+
end
|
188
|
+
|
189
|
+
def sanitized_shop_name
|
190
|
+
@sanitized_shop_name ||= sanitize_shop_param(params)
|
191
|
+
end
|
192
|
+
|
193
|
+
def referer_sanitized_shop_name
|
194
|
+
return unless request.referer.present?
|
195
|
+
|
196
|
+
@referer_sanitized_shop_name ||= begin
|
197
|
+
referer_uri = URI(request.referer)
|
198
|
+
query_params = Rack::Utils.parse_query(referer_uri.query)
|
199
|
+
|
200
|
+
sanitize_shop_param(query_params.with_indifferent_access)
|
201
|
+
end
|
202
|
+
end
|
203
|
+
|
204
|
+
def sanitize_shop_param(params)
|
205
|
+
return unless params[:shop].present?
|
206
|
+
ShopifyApp::Utils.sanitize_shop_domain(params[:shop])
|
207
|
+
end
|
208
|
+
|
209
|
+
def sanitized_params
|
210
|
+
request.query_parameters.clone.tap do |query_params|
|
211
|
+
if params[:shop].is_a?(String)
|
212
|
+
query_params[:shop] = sanitize_shop_param(params)
|
213
|
+
end
|
214
|
+
end
|
215
|
+
end
|
216
|
+
|
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
|
225
|
+
session.delete(:return_to) || ShopifyApp.configuration.root_url
|
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
|
243
|
+
end
|
244
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
module ShopifyApp
|
3
|
+
module PayloadVerification
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
private
|
7
|
+
|
8
|
+
def shopify_hmac
|
9
|
+
request.headers['HTTP_X_SHOPIFY_HMAC_SHA256']
|
10
|
+
end
|
11
|
+
|
12
|
+
def hmac_valid?(data)
|
13
|
+
secrets = [ShopifyApp.configuration.secret, ShopifyApp.configuration.old_secret].reject(&:blank?)
|
14
|
+
|
15
|
+
secrets.any? do |secret|
|
16
|
+
digest = OpenSSL::Digest.new('sha256')
|
17
|
+
ActiveSupport::SecurityUtils.secure_compare(
|
18
|
+
shopify_hmac,
|
19
|
+
Base64.strict_encode64(OpenSSL::HMAC.digest(digest, secret, data))
|
20
|
+
)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
module ShopifyApp
|
3
|
+
module WebhookVerification
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
include ShopifyApp::PayloadVerification
|
6
|
+
|
7
|
+
included do
|
8
|
+
skip_before_action :verify_authenticity_token, raise: false
|
9
|
+
before_action :verify_request
|
10
|
+
end
|
11
|
+
|
12
|
+
private
|
13
|
+
|
14
|
+
def verify_request
|
15
|
+
data = request.raw_post
|
16
|
+
return head(:unauthorized) unless hmac_valid?(data)
|
17
|
+
end
|
18
|
+
|
19
|
+
def shop_domain
|
20
|
+
request.headers['HTTP_X_SHOPIFY_SHOP_DOMAIN']
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
data/lib/shopify_app/engine.rb
CHANGED
@@ -1,6 +1,46 @@
|
|
1
|
+
# frozen_string_literal: true
|
1
2
|
module ShopifyApp
|
3
|
+
module RedactJobParams
|
4
|
+
private
|
5
|
+
|
6
|
+
def args_info(job)
|
7
|
+
log_disabled_classes = %w(ShopifyApp::ScripttagsManagerJob ShopifyApp::WebhooksManagerJob)
|
8
|
+
return "" if log_disabled_classes.include?(job.class.name)
|
9
|
+
super
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
2
13
|
class Engine < Rails::Engine
|
3
14
|
engine_name 'shopify_app'
|
4
15
|
isolate_namespace ShopifyApp
|
16
|
+
|
17
|
+
initializer "shopify_app.assets.precompile" do |app|
|
18
|
+
app.config.assets.precompile += %w[
|
19
|
+
shopify_app/redirect.js
|
20
|
+
shopify_app/top_level.js
|
21
|
+
shopify_app/enable_cookies.js
|
22
|
+
shopify_app/request_storage_access.js
|
23
|
+
storage_access.svg
|
24
|
+
]
|
25
|
+
end
|
26
|
+
|
27
|
+
initializer "shopify_app.middleware" do |app|
|
28
|
+
app.config.middleware.insert_after(::Rack::Runtime, ShopifyApp::SameSiteCookieMiddleware)
|
29
|
+
|
30
|
+
if ShopifyApp.configuration.allow_jwt_authentication
|
31
|
+
app.config.middleware.insert_after(ShopifyApp::SameSiteCookieMiddleware, ShopifyApp::JWTMiddleware)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
initializer "shopify_app.redact_job_params" do
|
36
|
+
ActiveSupport.on_load(:active_job) do
|
37
|
+
if ActiveJob::Base.respond_to?(:log_arguments?)
|
38
|
+
WebhooksManagerJob.log_arguments = false
|
39
|
+
ScripttagsManagerJob.log_arguments = false
|
40
|
+
elsif ActiveJob::Logging::LogSubscriber.private_method_defined?(:args_info)
|
41
|
+
ActiveJob::Logging::LogSubscriber.prepend(RedactJobParams)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
5
45
|
end
|
6
46
|
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
module ShopifyApp
|
3
|
+
class ScripttagsManagerJob < ActiveJob::Base
|
4
|
+
queue_as do
|
5
|
+
ShopifyApp.configuration.scripttags_manager_queue_name
|
6
|
+
end
|
7
|
+
|
8
|
+
def perform(shop_domain:, shop_token:, scripttags:)
|
9
|
+
api_version = ShopifyApp.configuration.api_version
|
10
|
+
ShopifyAPI::Session.temp(domain: shop_domain, token: shop_token, api_version: api_version) do
|
11
|
+
manager = ScripttagsManager.new(scripttags, shop_domain)
|
12
|
+
manager.create_scripttags
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -1,12 +1,13 @@
|
|
1
|
+
# frozen_string_literal: true
|
1
2
|
module ShopifyApp
|
2
3
|
class WebhooksManagerJob < ActiveJob::Base
|
3
|
-
|
4
4
|
queue_as do
|
5
5
|
ShopifyApp.configuration.webhooks_manager_queue_name
|
6
6
|
end
|
7
7
|
|
8
8
|
def perform(shop_domain:, shop_token:, webhooks:)
|
9
|
-
|
9
|
+
api_version = ShopifyApp.configuration.api_version
|
10
|
+
ShopifyAPI::Session.temp(domain: shop_domain, token: shop_token, api_version: api_version) do
|
10
11
|
manager = WebhooksManager.new(webhooks)
|
11
12
|
manager.create_webhooks
|
12
13
|
end
|
@@ -1,3 +1,4 @@
|
|
1
|
+
# frozen_string_literal: true
|
1
2
|
module ShopifyApp
|
2
3
|
class ScripttagsManager
|
3
4
|
class CreationFailed < StandardError; end
|
@@ -6,14 +7,25 @@ module ShopifyApp
|
|
6
7
|
ShopifyApp::ScripttagsManagerJob.perform_later(
|
7
8
|
shop_domain: shop_domain,
|
8
9
|
shop_token: shop_token,
|
9
|
-
|
10
|
+
# Procs cannot be serialized so we interpolate now, if necessary
|
11
|
+
scripttags: build_src(scripttags, shop_domain)
|
10
12
|
)
|
11
13
|
end
|
12
14
|
|
13
|
-
|
15
|
+
def self.build_src(scripttags, domain)
|
16
|
+
scripttags.map do |tag|
|
17
|
+
next tag unless tag[:src].respond_to?(:call)
|
18
|
+
tag = tag.dup
|
19
|
+
tag[:src] = tag[:src].call(domain)
|
20
|
+
tag
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
attr_reader :required_scripttags, :shop_domain
|
14
25
|
|
15
|
-
def initialize(scripttags)
|
26
|
+
def initialize(scripttags, shop_domain)
|
16
27
|
@required_scripttags = scripttags
|
28
|
+
@shop_domain = shop_domain
|
17
29
|
end
|
18
30
|
|
19
31
|
def recreate_scripttags!
|
@@ -24,14 +36,15 @@ module ShopifyApp
|
|
24
36
|
def create_scripttags
|
25
37
|
return unless required_scripttags.present?
|
26
38
|
|
27
|
-
|
39
|
+
expanded_scripttags.each do |scripttag|
|
28
40
|
create_scripttag(scripttag) unless scripttag_exists?(scripttag[:src])
|
29
41
|
end
|
30
42
|
end
|
31
43
|
|
32
44
|
def destroy_scripttags
|
33
|
-
|
34
|
-
|
45
|
+
scripttags = expanded_scripttags
|
46
|
+
ShopifyAPI::ScriptTag.all.each do |tag|
|
47
|
+
ShopifyAPI::ScriptTag.delete(tag.id) if required_scripttag?(scripttags, tag)
|
35
48
|
end
|
36
49
|
|
37
50
|
@current_scripttags = nil
|
@@ -39,8 +52,12 @@ module ShopifyApp
|
|
39
52
|
|
40
53
|
private
|
41
54
|
|
42
|
-
def
|
43
|
-
|
55
|
+
def expanded_scripttags
|
56
|
+
self.class.build_src(required_scripttags, shop_domain)
|
57
|
+
end
|
58
|
+
|
59
|
+
def required_scripttag?(scripttags, tag)
|
60
|
+
scripttags.map { |w| w[:src] }.include?(tag.src)
|
44
61
|
end
|
45
62
|
|
46
63
|
def create_scripttag(attributes)
|
@@ -1,3 +1,4 @@
|
|
1
|
+
# frozen_string_literal: true
|
1
2
|
module ShopifyApp
|
2
3
|
class WebhooksManager
|
3
4
|
class CreationFailed < StandardError; end
|
@@ -30,8 +31,8 @@ module ShopifyApp
|
|
30
31
|
end
|
31
32
|
|
32
33
|
def destroy_webhooks
|
33
|
-
ShopifyAPI::Webhook.all.each do |webhook|
|
34
|
-
ShopifyAPI::Webhook.delete(webhook.id) if
|
34
|
+
ShopifyAPI::Webhook.all.to_a.each do |webhook|
|
35
|
+
ShopifyAPI::Webhook.delete(webhook.id) if required_webhook?(webhook)
|
35
36
|
end
|
36
37
|
|
37
38
|
@current_webhooks = nil
|
@@ -39,8 +40,8 @@ module ShopifyApp
|
|
39
40
|
|
40
41
|
private
|
41
42
|
|
42
|
-
def
|
43
|
-
required_webhooks.map{ |w| w[:address] }.include?
|
43
|
+
def required_webhook?(webhook)
|
44
|
+
required_webhooks.map { |w| w[:address] }.include?(webhook.address)
|
44
45
|
end
|
45
46
|
|
46
47
|
def create_webhook(attributes)
|
@@ -55,7 +56,7 @@ module ShopifyApp
|
|
55
56
|
end
|
56
57
|
|
57
58
|
def current_webhooks
|
58
|
-
@current_webhooks ||= ShopifyAPI::Webhook.all.index_by(&:topic)
|
59
|
+
@current_webhooks ||= ShopifyAPI::Webhook.all.to_a.index_by(&:topic)
|
59
60
|
end
|
60
61
|
end
|
61
62
|
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
module ShopifyApp
|
3
|
+
class JWTMiddleware
|
4
|
+
TOKEN_REGEX = /^Bearer\s+(.*?)$/
|
5
|
+
|
6
|
+
def initialize(app)
|
7
|
+
@app = app
|
8
|
+
end
|
9
|
+
|
10
|
+
def call(env)
|
11
|
+
return call_next(env) unless authorization_header(env)
|
12
|
+
|
13
|
+
token = extract_token(env)
|
14
|
+
return call_next(env) unless token
|
15
|
+
|
16
|
+
set_env_variables(token, env)
|
17
|
+
call_next(env)
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def call_next(env)
|
23
|
+
@app.call(env)
|
24
|
+
end
|
25
|
+
|
26
|
+
def authorization_header(env)
|
27
|
+
env['HTTP_AUTHORIZATION']
|
28
|
+
end
|
29
|
+
|
30
|
+
def extract_token(env)
|
31
|
+
match = authorization_header(env).match(TOKEN_REGEX)
|
32
|
+
match && match[1]
|
33
|
+
end
|
34
|
+
|
35
|
+
def set_env_variables(token, env)
|
36
|
+
jwt = ShopifyApp::JWT.new(token)
|
37
|
+
|
38
|
+
env['jwt.shopify_domain'] = jwt.shopify_domain
|
39
|
+
env['jwt.shopify_user_id'] = jwt.shopify_user_id
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
module ShopifyApp
|
3
|
+
class SameSiteCookieMiddleware
|
4
|
+
COOKIE_SEPARATOR = "\n"
|
5
|
+
|
6
|
+
def initialize(app)
|
7
|
+
@app = app
|
8
|
+
end
|
9
|
+
|
10
|
+
def call(env)
|
11
|
+
status, headers, body = @app.call(env)
|
12
|
+
user_agent = env['HTTP_USER_AGENT']
|
13
|
+
|
14
|
+
if headers && headers['Set-Cookie'] &&
|
15
|
+
BrowserSniffer.new(user_agent).same_site_none_compatible? &&
|
16
|
+
ShopifyApp.configuration.enable_same_site_none &&
|
17
|
+
Rack::Request.new(env).ssl?
|
18
|
+
|
19
|
+
set_cookies = headers['Set-Cookie']
|
20
|
+
.split(COOKIE_SEPARATOR)
|
21
|
+
.compact
|
22
|
+
.map do |cookie|
|
23
|
+
cookie << '; Secure' unless cookie =~ /;\s*secure/i
|
24
|
+
cookie << '; SameSite=None' unless cookie =~ /;\s*samesite=/i
|
25
|
+
cookie
|
26
|
+
end
|
27
|
+
|
28
|
+
headers['Set-Cookie'] = set_cookies.join(COOKIE_SEPARATOR)
|
29
|
+
end
|
30
|
+
|
31
|
+
[status, headers, body]
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|