shopify_app 21.0.0 → 22.5.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/ENHANCEMENT.md +9 -0
- data/.github/ISSUE_TEMPLATE/bug-report.md +30 -47
- data/.github/ISSUE_TEMPLATE/feature-request.md +5 -29
- data/.github/workflows/build.yml +11 -12
- data/.github/workflows/release.yml +2 -2
- data/.github/workflows/remove-labels-on-activity.yml +1 -1
- data/.github/workflows/rubocop.yml +2 -3
- data/.nvmrc +1 -1
- data/.rubocop.yml +2 -1
- data/.ruby-version +1 -1
- data/.spin/rails/prepare-application +8 -0
- data/CHANGELOG.md +173 -7
- data/CODE_OF_CONDUCT.md +46 -0
- data/CONTRIBUTING.md +16 -6
- data/Gemfile +1 -0
- data/Gemfile.lock +160 -121
- data/README.md +67 -19
- data/SECURITY.md +1 -1
- data/app/assets/javascripts/shopify_app/redirect.js +3 -10
- data/app/controllers/concerns/shopify_app/ensure_authenticated_links.rb +9 -4
- data/app/controllers/concerns/shopify_app/ensure_has_session.rb +25 -0
- data/app/controllers/concerns/shopify_app/ensure_installed.rb +84 -0
- data/app/controllers/concerns/shopify_app/shop_access_scopes_verification.rb +5 -1
- data/app/controllers/shopify_app/authenticated_controller.rb +1 -1
- data/app/controllers/shopify_app/callback_controller.rb +101 -39
- data/app/controllers/shopify_app/extension_verification_controller.rb +4 -1
- data/app/controllers/shopify_app/sessions_controller.rb +37 -7
- data/app/controllers/shopify_app/webhooks_controller.rb +1 -1
- data/app/views/shopify_app/layouts/app_bridge.html.erb +17 -0
- data/app/views/shopify_app/sessions/patch_shopify_id_token.html.erb +0 -0
- data/app/views/shopify_app/shared/redirect.html.erb +10 -1
- data/config/locales/cs.yml +0 -18
- data/config/locales/da.yml +0 -15
- data/config/locales/de.yml +0 -17
- data/config/locales/en.yml +0 -11
- data/config/locales/es.yml +0 -17
- data/config/locales/fi.yml +0 -15
- data/config/locales/fr.yml +0 -18
- data/config/locales/it.yml +0 -16
- data/config/locales/ja.yml +0 -12
- data/config/locales/ko.yml +0 -14
- data/config/locales/nb.yml +0 -16
- data/config/locales/nl.yml +0 -16
- data/config/locales/pl.yml +0 -16
- data/config/locales/pt-BR.yml +0 -16
- data/config/locales/pt-PT.yml +0 -17
- data/config/locales/sv.yml +0 -16
- data/config/locales/th.yml +0 -15
- data/config/locales/tr.yml +0 -17
- data/config/locales/vi.yml +0 -17
- data/config/locales/zh-CN.yml +0 -11
- data/config/locales/zh-TW.yml +0 -11
- data/config/routes.rb +2 -1
- data/docs/Quickstart.md +14 -5
- data/docs/Troubleshooting.md +38 -25
- data/docs/Upgrading.md +103 -32
- data/docs/shopify_app/authentication.md +179 -58
- data/docs/shopify_app/controller-concerns.md +89 -0
- data/docs/shopify_app/engine.md +2 -11
- data/docs/shopify_app/generators.md +2 -2
- data/docs/shopify_app/logging.md +21 -0
- data/docs/shopify_app/sessions.md +358 -0
- data/docs/shopify_app/testing.md +32 -10
- data/docs/shopify_app/webhooks.md +97 -7
- data/karma.conf.js +6 -4
- data/lib/generators/shopify_app/add_after_authenticate_job/add_after_authenticate_job_generator.rb +6 -3
- data/lib/generators/shopify_app/add_after_authenticate_job/templates/after_authenticate_job.rb +1 -1
- data/lib/generators/shopify_app/add_app_uninstalled_job/add_app_uninstalled_job_generator.rb +15 -0
- data/lib/generators/shopify_app/add_app_uninstalled_job/templates/app_uninstalled_job.rb.tt +22 -0
- data/lib/generators/shopify_app/add_declarative_webhook/add_declarative_webhook_generator.rb +53 -0
- data/lib/generators/shopify_app/add_declarative_webhook/templates/webhook_controller.rb.tt +13 -0
- data/lib/generators/shopify_app/add_declarative_webhook/templates/webhook_job.rb.tt +15 -0
- data/lib/generators/shopify_app/add_privacy_jobs/add_privacy_jobs_generator.rb +23 -0
- data/lib/generators/shopify_app/add_privacy_jobs/templates/customers_data_request_job.rb.tt +22 -0
- data/lib/generators/shopify_app/add_privacy_jobs/templates/customers_redact_job.rb.tt +22 -0
- data/lib/generators/shopify_app/add_privacy_jobs/templates/shop_redact_job.rb.tt +22 -0
- data/lib/generators/shopify_app/add_webhook/add_webhook_generator.rb +8 -3
- data/lib/generators/shopify_app/add_webhook/templates/webhook_job.rb.tt +4 -2
- data/lib/generators/shopify_app/app_proxy_controller/app_proxy_controller_generator.rb +1 -1
- data/lib/generators/shopify_app/authenticated_controller/templates/authenticated_controller.rb +1 -1
- data/lib/generators/shopify_app/home_controller/templates/index.html.erb +1 -1
- data/lib/generators/shopify_app/home_controller/templates/unauthenticated_home_controller.rb +1 -1
- data/lib/generators/shopify_app/install/install_generator.rb +4 -4
- data/lib/generators/shopify_app/install/templates/shopify_app.rb.tt +13 -3
- data/lib/generators/shopify_app/rotate_shopify_token_job/templates/rotate_shopify_token.rake +1 -1
- data/lib/generators/shopify_app/rotate_shopify_token_job/templates/rotate_shopify_token_job.rb +1 -1
- data/lib/generators/shopify_app/routes/routes_generator.rb +1 -1
- data/lib/generators/shopify_app/shop_model/shop_model_generator.rb +1 -1
- data/lib/generators/shopify_app/shop_model/templates/db/migrate/add_shop_access_scopes_column.erb +1 -1
- data/lib/generators/shopify_app/shopify_app_generator.rb +2 -0
- data/lib/generators/shopify_app/user_model/templates/db/migrate/add_user_access_scopes_column.erb +1 -1
- data/lib/generators/shopify_app/user_model/templates/db/migrate/add_user_expires_at_column.erb +5 -0
- data/lib/generators/shopify_app/user_model/user_model_generator.rb +21 -1
- data/lib/shopify_app/access_scopes/noop_strategy.rb +4 -0
- data/lib/shopify_app/access_scopes/user_strategy.rb +9 -2
- data/lib/shopify_app/admin_api/with_token_refetch.rb +27 -0
- data/lib/shopify_app/auth/post_authenticate_tasks.rb +48 -0
- data/lib/shopify_app/auth/token_exchange.rb +73 -0
- data/lib/shopify_app/configuration.rb +82 -1
- data/lib/shopify_app/controller_concerns/app_proxy_verification.rb +3 -3
- data/lib/shopify_app/controller_concerns/csrf_protection.rb +2 -1
- data/lib/shopify_app/controller_concerns/embedded_app.rb +42 -3
- data/lib/shopify_app/controller_concerns/ensure_billing.rb +28 -12
- data/lib/shopify_app/controller_concerns/frame_ancestors.rb +1 -1
- data/lib/shopify_app/controller_concerns/localization.rb +11 -8
- data/lib/shopify_app/controller_concerns/login_protection.rb +83 -38
- data/lib/shopify_app/controller_concerns/payload_verification.rb +1 -1
- data/lib/shopify_app/controller_concerns/redirect_for_embedded.rb +15 -3
- data/lib/shopify_app/controller_concerns/sanitized_params.rb +5 -0
- data/lib/shopify_app/controller_concerns/token_exchange.rb +111 -0
- data/lib/shopify_app/controller_concerns/webhook_verification.rb +4 -1
- data/lib/shopify_app/controller_concerns/with_shopify_id_token.rb +48 -0
- data/lib/shopify_app/engine.rb +7 -8
- data/lib/shopify_app/logger.rb +28 -0
- data/lib/shopify_app/managers/webhooks_manager.rb +20 -10
- data/lib/shopify_app/middleware/jwt_middleware.rb +13 -9
- data/lib/shopify_app/session/in_memory_user_session_store.rb +1 -1
- data/lib/shopify_app/session/jwt.rb +11 -2
- data/lib/shopify_app/session/session_repository.rb +66 -14
- data/lib/shopify_app/session/session_storage.rb +2 -2
- data/lib/shopify_app/session/shop_session_storage.rb +5 -1
- data/lib/shopify_app/session/shop_session_storage_with_scopes.rb +5 -1
- data/lib/shopify_app/session/user_session_storage.rb +6 -2
- data/lib/shopify_app/session/user_session_storage_with_scopes.rb +27 -2
- data/lib/shopify_app/test_helpers/all.rb +1 -0
- data/lib/shopify_app/test_helpers/shopify_session_helper.rb +16 -0
- data/lib/shopify_app/utils.rb +82 -20
- data/lib/shopify_app/version.rb +1 -1
- data/lib/shopify_app.rb +12 -3
- data/package.json +5 -6
- data/service.yml +0 -2
- data/shopify_app.gemspec +6 -5
- data/translation.yml +1 -0
- data/yarn.lock +2139 -3910
- metadata +78 -58
- data/.github/workflows/stale.yml +0 -31
- data/app/assets/images/storage_access.svg +0 -1
- data/app/assets/javascripts/shopify_app/app_bridge_3.1.1.js +0 -10
- data/app/assets/javascripts/shopify_app/app_bridge_redirect.js +0 -22
- data/app/assets/javascripts/shopify_app/app_bridge_utils_3.1.1.js +0 -1
- data/app/assets/javascripts/shopify_app/enable_cookies.js +0 -3
- data/app/assets/javascripts/shopify_app/itp_helper.js +0 -40
- data/app/assets/javascripts/shopify_app/partition_cookies.js +0 -8
- data/app/assets/javascripts/shopify_app/post_redirect.js +0 -9
- data/app/assets/javascripts/shopify_app/request_storage_access.js +0 -3
- data/app/assets/javascripts/shopify_app/storage_access.js +0 -148
- data/app/assets/javascripts/shopify_app/storage_access_redirect.js +0 -17
- data/app/assets/javascripts/shopify_app/top_level.js +0 -2
- data/app/assets/javascripts/shopify_app/top_level_interaction.js +0 -11
- data/app/controllers/concerns/shopify_app/authenticated.rb +0 -19
- data/app/controllers/concerns/shopify_app/require_known_shop.rb +0 -48
- data/app/views/shopify_app/sessions/enable_cookies.html.erb +0 -70
- data/app/views/shopify_app/sessions/request_storage_access.html.erb +0 -68
- data/app/views/shopify_app/sessions/top_level_interaction.html.erb +0 -63
- data/app/views/shopify_app/shared/post_redirect_to_auth_shopify.html.erb +0 -13
- data/docs/shopify_app/script-tags.md +0 -28
- data/docs/shopify_app/session-repository.md +0 -88
- data/lib/generators/shopify_app/add_marketing_activity_extension/add_marketing_activity_extension_generator.rb +0 -41
- data/lib/generators/shopify_app/add_marketing_activity_extension/templates/marketing_activities_controller.rb +0 -62
- data/lib/shopify_app/controller_concerns/itp.rb +0 -45
- data/lib/shopify_app/jobs/scripttags_manager_job.rb +0 -16
- data/lib/shopify_app/managers/scripttags_manager.rb +0 -84
@@ -4,6 +4,11 @@ module ShopifyApp
|
|
4
4
|
module RedirectForEmbedded
|
5
5
|
include ShopifyApp::SanitizedParams
|
6
6
|
|
7
|
+
def self.add_app_bridge_redirect_url_header(url, response)
|
8
|
+
response.set_header("X-Shopify-API-Request-Failure-Reauthorize", "1")
|
9
|
+
response.set_header("X-Shopify-API-Request-Failure-Reauthorize-Url", url)
|
10
|
+
end
|
11
|
+
|
7
12
|
private
|
8
13
|
|
9
14
|
def embedded_redirect_url?
|
@@ -11,17 +16,24 @@ module ShopifyApp
|
|
11
16
|
end
|
12
17
|
|
13
18
|
def embedded_param?
|
14
|
-
embedded_redirect_url? && params[:embedded].present? &&
|
19
|
+
embedded_redirect_url? && params[:embedded].present? && loaded_directly_from_admin?
|
20
|
+
end
|
21
|
+
|
22
|
+
def loaded_directly_from_admin?
|
23
|
+
ShopifyApp.configuration.embedded_app? && params[:embedded] == "1"
|
15
24
|
end
|
16
25
|
|
17
26
|
def redirect_for_embedded
|
18
27
|
# Don't actually redirect if we're already in the redirect route - we want the request to reach the FE
|
19
|
-
|
28
|
+
unless request.path == ShopifyApp.configuration.embedded_redirect_url
|
29
|
+
ShopifyApp::Logger.debug("Redirecting to #{redirect_uri_for_embedded}")
|
30
|
+
redirect_to(redirect_uri_for_embedded)
|
31
|
+
end
|
20
32
|
end
|
21
33
|
|
22
34
|
def redirect_uri_for_embedded
|
23
35
|
redirect_query_params = {}
|
24
|
-
redirect_uri = "
|
36
|
+
redirect_uri = "#{ShopifyAPI::Context.host}#{ShopifyApp.configuration.login_url}"
|
25
37
|
redirect_query_params[:shop] = sanitized_shop_name
|
26
38
|
redirect_query_params[:shop] ||= referer_sanitized_shop_name if referer_sanitized_shop_name.present?
|
27
39
|
redirect_query_params[:host] ||= params[:host] if params[:host].present?
|
@@ -21,6 +21,7 @@ module ShopifyApp
|
|
21
21
|
|
22
22
|
def sanitize_shop_param(params)
|
23
23
|
return unless params[:shop].present?
|
24
|
+
|
24
25
|
ShopifyApp::Utils.sanitize_shop_domain(params[:shop])
|
25
26
|
end
|
26
27
|
|
@@ -32,5 +33,9 @@ module ShopifyApp
|
|
32
33
|
end
|
33
34
|
end
|
34
35
|
end
|
36
|
+
|
37
|
+
def embedded?
|
38
|
+
params[:embedded] == "1" || request.env["HTTP_SEC_FETCH_DEST"] == "iframe"
|
39
|
+
end
|
35
40
|
end
|
36
41
|
end
|
@@ -0,0 +1,111 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ShopifyApp
|
4
|
+
module TokenExchange
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
include ShopifyApp::AdminAPI::WithTokenRefetch
|
7
|
+
include ShopifyApp::SanitizedParams
|
8
|
+
include ShopifyApp::EmbeddedApp
|
9
|
+
|
10
|
+
included do
|
11
|
+
include ShopifyApp::WithShopifyIdToken
|
12
|
+
end
|
13
|
+
|
14
|
+
INVALID_SHOPIFY_ID_TOKEN_ERRORS = [
|
15
|
+
ShopifyAPI::Errors::MissingJwtTokenError,
|
16
|
+
ShopifyAPI::Errors::InvalidJwtTokenError,
|
17
|
+
].freeze
|
18
|
+
|
19
|
+
def activate_shopify_session(&block)
|
20
|
+
retrieve_session_from_token_exchange if current_shopify_session.blank? || should_exchange_expired_token?
|
21
|
+
|
22
|
+
ShopifyApp::Logger.debug("Activating Shopify session")
|
23
|
+
ShopifyAPI::Context.activate_session(current_shopify_session)
|
24
|
+
with_token_refetch(current_shopify_session, shopify_id_token, &block)
|
25
|
+
rescue *INVALID_SHOPIFY_ID_TOKEN_ERRORS => e
|
26
|
+
respond_to_invalid_shopify_id_token(e)
|
27
|
+
ensure
|
28
|
+
ShopifyApp::Logger.debug("Deactivating session")
|
29
|
+
ShopifyAPI::Context.deactivate_session
|
30
|
+
end
|
31
|
+
|
32
|
+
def should_exchange_expired_token?
|
33
|
+
ShopifyApp.configuration.check_session_expiry_date && current_shopify_session.expired?
|
34
|
+
end
|
35
|
+
|
36
|
+
def current_shopify_session
|
37
|
+
return unless current_shopify_session_id
|
38
|
+
|
39
|
+
@current_shopify_session ||= ShopifyApp::SessionRepository.load_session(current_shopify_session_id)
|
40
|
+
end
|
41
|
+
|
42
|
+
def current_shopify_session_id
|
43
|
+
@current_shopify_session_id ||= ShopifyAPI::Utils::SessionUtils.session_id_from_shopify_id_token(
|
44
|
+
id_token: shopify_id_token,
|
45
|
+
online: online_token_configured?,
|
46
|
+
)
|
47
|
+
end
|
48
|
+
|
49
|
+
def current_shopify_domain
|
50
|
+
sanitized_shop_name || current_shopify_session&.shop
|
51
|
+
rescue *INVALID_SHOPIFY_ID_TOKEN_ERRORS => e
|
52
|
+
respond_to_invalid_shopify_id_token(e)
|
53
|
+
end
|
54
|
+
|
55
|
+
private
|
56
|
+
|
57
|
+
def retrieve_session_from_token_exchange
|
58
|
+
@current_shopify_session = nil
|
59
|
+
ShopifyApp::Auth::TokenExchange.perform(shopify_id_token)
|
60
|
+
end
|
61
|
+
|
62
|
+
def respond_to_invalid_shopify_id_token(error)
|
63
|
+
ShopifyApp::Logger.debug("Responding to invalid Shopify ID token: #{error.message}")
|
64
|
+
return if performed?
|
65
|
+
|
66
|
+
if request.headers["HTTP_AUTHORIZATION"].blank?
|
67
|
+
if embedded?
|
68
|
+
redirect_to_bounce_page
|
69
|
+
else
|
70
|
+
redirect_to_embed_app_in_admin
|
71
|
+
end
|
72
|
+
else
|
73
|
+
ShopifyApp::Logger.debug("Responding to invalid Shopify ID token with unauthorized response")
|
74
|
+
response.set_header("X-Shopify-Retry-Invalid-Session-Request", 1)
|
75
|
+
unauthorized_response = { message: :unauthorized }
|
76
|
+
render(json: { errors: [unauthorized_response] }, status: :unauthorized)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
def redirect_to_bounce_page
|
81
|
+
ShopifyApp::Logger.debug("Redirecting to bounce page for patching Shopify ID token")
|
82
|
+
patch_shopify_id_token_url =
|
83
|
+
"#{ShopifyAPI::Context.host}#{ShopifyApp.configuration.root_url}/patch_shopify_id_token"
|
84
|
+
patch_shopify_id_token_params = request.query_parameters.except(:id_token)
|
85
|
+
|
86
|
+
bounce_url = "#{request.path}?#{patch_shopify_id_token_params.to_query}"
|
87
|
+
|
88
|
+
# App Bridge will trigger a fetch to the URL in shopify-reload, with a new session token in headers
|
89
|
+
patch_shopify_id_token_params["shopify-reload"] = bounce_url
|
90
|
+
|
91
|
+
redirect_to(
|
92
|
+
"#{patch_shopify_id_token_url}?#{patch_shopify_id_token_params.to_query}",
|
93
|
+
allow_other_host: true,
|
94
|
+
)
|
95
|
+
end
|
96
|
+
|
97
|
+
def online_token_configured?
|
98
|
+
ShopifyApp.configuration.online_token_configured?
|
99
|
+
end
|
100
|
+
|
101
|
+
def fullpage_redirect_to(url)
|
102
|
+
raise ShopifyApp::ShopifyDomainNotFound if current_shopify_domain.nil?
|
103
|
+
|
104
|
+
render(
|
105
|
+
"shopify_app/shared/redirect",
|
106
|
+
layout: false,
|
107
|
+
locals: { url: url, current_shopify_domain: current_shopify_domain, is_iframe: embedded? },
|
108
|
+
)
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
@@ -14,7 +14,10 @@ module ShopifyApp
|
|
14
14
|
|
15
15
|
def verify_request
|
16
16
|
data = request.raw_post
|
17
|
-
|
17
|
+
unless hmac_valid?(data)
|
18
|
+
ShopifyApp::Logger.debug("Webhook verification failed - HMAC invalid")
|
19
|
+
head(:unauthorized)
|
20
|
+
end
|
18
21
|
end
|
19
22
|
|
20
23
|
def shop_domain
|
@@ -0,0 +1,48 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ShopifyApp
|
4
|
+
module WithShopifyIdToken
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
def shopify_id_token
|
8
|
+
return @shopify_id_token if defined?(@shopify_id_token)
|
9
|
+
|
10
|
+
@shopify_id_token = id_token_from_authorization_header || id_token_from_url_param
|
11
|
+
end
|
12
|
+
|
13
|
+
def jwt_payload
|
14
|
+
return @jwt_payload if defined?(@jwt_payload)
|
15
|
+
|
16
|
+
@jwt_payload = shopify_id_token.present? ? ShopifyAPI::Auth::JwtPayload.new(shopify_id_token) : nil
|
17
|
+
end
|
18
|
+
|
19
|
+
def jwt_shopify_domain
|
20
|
+
return @jwt_shopify_domain if defined?(@jwt_shopify_domain)
|
21
|
+
|
22
|
+
@jwt_shopify_domain = if jwt_payload.present?
|
23
|
+
ShopifyApp::Utils.sanitize_shop_domain(jwt_payload.shopify_domain)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def jwt_shopify_user_id
|
28
|
+
jwt_payload&.shopify_user_id
|
29
|
+
end
|
30
|
+
|
31
|
+
def jwt_expire_at
|
32
|
+
expire_at = jwt_payload&.expire_at
|
33
|
+
return unless expire_at
|
34
|
+
|
35
|
+
expire_at - 5.seconds # 5s gap to start fetching new token in advance
|
36
|
+
end
|
37
|
+
|
38
|
+
private
|
39
|
+
|
40
|
+
def id_token_from_authorization_header
|
41
|
+
request.headers["HTTP_AUTHORIZATION"]&.match(/^Bearer (.+)$/)&.[](1)
|
42
|
+
end
|
43
|
+
|
44
|
+
def id_token_from_url_param
|
45
|
+
params["id_token"]
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
data/lib/shopify_app/engine.rb
CHANGED
@@ -5,7 +5,7 @@ module ShopifyApp
|
|
5
5
|
private
|
6
6
|
|
7
7
|
def args_info(job)
|
8
|
-
log_disabled_classes = ["ShopifyApp::
|
8
|
+
log_disabled_classes = ["ShopifyApp::WebhooksManagerJob"]
|
9
9
|
return "" if log_disabled_classes.include?(job.class.name)
|
10
10
|
|
11
11
|
super
|
@@ -16,21 +16,20 @@ module ShopifyApp
|
|
16
16
|
engine_name "shopify_app"
|
17
17
|
isolate_namespace ShopifyApp
|
18
18
|
|
19
|
-
initializer "shopify_app.assets.precompile" do |app|
|
20
|
-
app.config.assets.precompile += ["shopify_app/redirect.js", "shopify_app/post_redirect.js",
|
21
|
-
"shopify_app/top_level.js", "shopify_app/enable_cookies.js",
|
22
|
-
"shopify_app/request_storage_access.js", "storage_access.svg",]
|
23
|
-
end
|
24
|
-
|
25
19
|
initializer "shopify_app.middleware" do |app|
|
26
20
|
app.config.middleware.insert_after(::Rack::Runtime, ShopifyApp::JWTMiddleware)
|
27
21
|
end
|
28
22
|
|
23
|
+
initializer "shopify_app.assets.precompile" do |app|
|
24
|
+
app.config.assets.precompile += [
|
25
|
+
"shopify_app/redirect.js",
|
26
|
+
]
|
27
|
+
end
|
28
|
+
|
29
29
|
initializer "shopify_app.redact_job_params" do
|
30
30
|
ActiveSupport.on_load(:active_job) do
|
31
31
|
if ActiveJob::Base.respond_to?(:log_arguments?)
|
32
32
|
WebhooksManagerJob.log_arguments = false
|
33
|
-
ScripttagsManagerJob.log_arguments = false
|
34
33
|
elsif ActiveJob::Logging::LogSubscriber.private_method_defined?(:args_info)
|
35
34
|
ActiveJob::Logging::LogSubscriber.prepend(RedactJobParams)
|
36
35
|
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ShopifyApp
|
4
|
+
class Logger < ShopifyAPI::Logger
|
5
|
+
class << self
|
6
|
+
def deprecated(message, version)
|
7
|
+
return unless enabled_for_log_level?(:warn)
|
8
|
+
|
9
|
+
raise ShopifyAPI::Errors::FeatureDeprecatedError unless valid_version(version)
|
10
|
+
|
11
|
+
ActiveSupport::Deprecation.warn("[#{version}] #{context(:warn)} #{message}")
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
def context(log_level)
|
17
|
+
current_shop = ShopifyAPI::Context.active_session&.shop || "Shop Not Found"
|
18
|
+
"[ ShopifyApp | #{log_level.to_s.upcase} | #{current_shop} ]"
|
19
|
+
end
|
20
|
+
|
21
|
+
def valid_version(version)
|
22
|
+
current_version = Gem::Version.create(ShopifyApp::VERSION)
|
23
|
+
deprecate_version = Gem::Version.create(version)
|
24
|
+
current_version < deprecate_version
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -8,44 +8,52 @@ module ShopifyApp
|
|
8
8
|
def queue(shop_domain, shop_token)
|
9
9
|
ShopifyApp::WebhooksManagerJob.perform_later(
|
10
10
|
shop_domain: shop_domain,
|
11
|
-
shop_token: shop_token
|
11
|
+
shop_token: shop_token,
|
12
12
|
)
|
13
13
|
end
|
14
14
|
|
15
15
|
def create_webhooks(session:)
|
16
16
|
return unless ShopifyApp.configuration.has_webhooks?
|
17
17
|
|
18
|
+
ShopifyApp::Logger.debug("Creating webhooks #{ShopifyApp.configuration.webhooks}")
|
19
|
+
|
18
20
|
ShopifyAPI::Webhooks::Registry.register_all(session: session)
|
19
21
|
end
|
20
22
|
|
21
23
|
def recreate_webhooks!(session:)
|
22
|
-
destroy_webhooks
|
24
|
+
destroy_webhooks(session: session)
|
23
25
|
return unless ShopifyApp.configuration.has_webhooks?
|
24
26
|
|
27
|
+
ShopifyApp::Logger.debug("Recreating webhooks")
|
25
28
|
add_registrations
|
26
|
-
|
29
|
+
create_webhooks(session: session)
|
27
30
|
end
|
28
31
|
|
29
|
-
def destroy_webhooks
|
32
|
+
def destroy_webhooks(session:)
|
30
33
|
return unless ShopifyApp.configuration.has_webhooks?
|
31
34
|
|
35
|
+
ShopifyApp::Logger.debug("Destroying webhooks")
|
32
36
|
ShopifyApp.configuration.webhooks.each do |attributes|
|
33
|
-
ShopifyAPI::Webhooks::Registry.unregister(topic: attributes[:topic])
|
37
|
+
ShopifyAPI::Webhooks::Registry.unregister(topic: attributes[:topic], session: session)
|
34
38
|
end
|
35
39
|
end
|
36
40
|
|
37
41
|
def add_registrations
|
38
42
|
return unless ShopifyApp.configuration.has_webhooks?
|
39
43
|
|
44
|
+
ShopifyApp::Logger.debug("Adding registrations to webhooks")
|
40
45
|
ShopifyApp.configuration.webhooks.each do |attributes|
|
41
46
|
webhook_path = path(attributes)
|
47
|
+
delivery_method = attributes[:delivery_method] || :http
|
42
48
|
|
43
49
|
ShopifyAPI::Webhooks::Registry.add_registration(
|
44
50
|
topic: attributes[:topic],
|
45
|
-
delivery_method:
|
51
|
+
delivery_method: delivery_method,
|
46
52
|
path: webhook_path,
|
47
|
-
handler: webhook_job_klass(webhook_path),
|
48
|
-
fields: attributes[:fields]
|
53
|
+
handler: delivery_method == :http ? webhook_job_klass(webhook_path) : nil,
|
54
|
+
fields: attributes[:fields],
|
55
|
+
filter: attributes[:filter],
|
56
|
+
metafield_namespaces: attributes[:metafield_namespaces],
|
49
57
|
)
|
50
58
|
end
|
51
59
|
end
|
@@ -74,8 +82,10 @@ module ShopifyApp
|
|
74
82
|
def webhook_job_klass_name(path)
|
75
83
|
job_file_name = Pathname(path.to_s).basename
|
76
84
|
|
77
|
-
[
|
78
|
-
|
85
|
+
[
|
86
|
+
ShopifyApp.configuration.webhook_jobs_namespace,
|
87
|
+
"#{job_file_name}_job",
|
88
|
+
].compact.join("/").classify
|
79
89
|
end
|
80
90
|
end
|
81
91
|
end
|
@@ -2,16 +2,17 @@
|
|
2
2
|
|
3
3
|
module ShopifyApp
|
4
4
|
class JWTMiddleware
|
5
|
-
TOKEN_REGEX = /^Bearer
|
5
|
+
TOKEN_REGEX = /^Bearer (.+)$/
|
6
|
+
ID_TOKEN_QUERY_PARAM = "id_token"
|
6
7
|
|
7
8
|
def initialize(app)
|
8
9
|
@app = app
|
9
10
|
end
|
10
11
|
|
11
12
|
def call(env)
|
12
|
-
return call_next(env) unless
|
13
|
+
return call_next(env) unless ShopifyApp.configuration.embedded_app?
|
13
14
|
|
14
|
-
token =
|
15
|
+
token = token_from_authorization_header(env) || token_from_query_string(env)
|
15
16
|
return call_next(env) unless token
|
16
17
|
|
17
18
|
set_env_variables(token, env)
|
@@ -24,21 +25,24 @@ module ShopifyApp
|
|
24
25
|
@app.call(env)
|
25
26
|
end
|
26
27
|
|
27
|
-
def
|
28
|
-
env["HTTP_AUTHORIZATION"]
|
28
|
+
def token_from_authorization_header(env)
|
29
|
+
env["HTTP_AUTHORIZATION"]&.match(TOKEN_REGEX)&.[](1)
|
29
30
|
end
|
30
31
|
|
31
|
-
def
|
32
|
-
|
33
|
-
match && match[1]
|
32
|
+
def token_from_query_string(env)
|
33
|
+
Rack::Utils.parse_nested_query(env["QUERY_STRING"])[ID_TOKEN_QUERY_PARAM]
|
34
34
|
end
|
35
35
|
|
36
36
|
def set_env_variables(token, env)
|
37
|
-
jwt =
|
37
|
+
jwt = ShopifyAPI::Auth::JwtPayload.new(token)
|
38
38
|
|
39
|
+
env["jwt.token"] = token
|
39
40
|
env["jwt.shopify_domain"] = jwt.shopify_domain
|
40
41
|
env["jwt.shopify_user_id"] = jwt.shopify_user_id
|
41
42
|
env["jwt.expire_at"] = jwt.expire_at
|
43
|
+
rescue ShopifyAPI::Errors::InvalidJwtTokenError
|
44
|
+
# ShopifyApp::JWT did not raise any exceptions, ensuring behaviour does not change
|
45
|
+
nil
|
42
46
|
end
|
43
47
|
end
|
44
48
|
end
|
@@ -13,6 +13,7 @@ module ShopifyApp
|
|
13
13
|
]
|
14
14
|
|
15
15
|
def initialize(token)
|
16
|
+
warn_deprecation
|
16
17
|
@token = token
|
17
18
|
set_payload
|
18
19
|
end
|
@@ -34,8 +35,7 @@ module ShopifyApp
|
|
34
35
|
def set_payload
|
35
36
|
payload, _ = parse_token_data(ShopifyApp.configuration&.secret, ShopifyApp.configuration&.old_secret)
|
36
37
|
@payload = validate_payload(payload)
|
37
|
-
rescue *WARN_EXCEPTIONS
|
38
|
-
Rails.logger.warn("[ShopifyApp::JWT] Failed to validate JWT: [#{error.class}] #{error}")
|
38
|
+
rescue *WARN_EXCEPTIONS
|
39
39
|
nil
|
40
40
|
end
|
41
41
|
|
@@ -55,10 +55,19 @@ module ShopifyApp
|
|
55
55
|
raise ::ShopifyApp::InvalidAudienceError,
|
56
56
|
"'aud' claim does not match api_key" unless payload["aud"] == api_key
|
57
57
|
raise ::ShopifyApp::InvalidDestinationError, "'dest' claim host not a valid shopify host" unless dest_host
|
58
|
+
|
58
59
|
raise ::ShopifyApp::MismatchedHostsError,
|
59
60
|
"'dest' claim host does not match 'iss' claim host" unless dest_host == iss_host
|
60
61
|
|
61
62
|
payload
|
62
63
|
end
|
64
|
+
|
65
|
+
def warn_deprecation
|
66
|
+
message = <<~EOS
|
67
|
+
"ShopifyApp::JWT will be deprecated, use ShopifyAPI::Auth::JwtPayload to parse JWT token instead."
|
68
|
+
EOS
|
69
|
+
|
70
|
+
ShopifyApp::Logger.deprecated(message, "23.0.0")
|
71
|
+
end
|
63
72
|
end
|
64
73
|
end
|
@@ -2,8 +2,6 @@
|
|
2
2
|
|
3
3
|
module ShopifyApp
|
4
4
|
class SessionRepository
|
5
|
-
extend ShopifyAPI::Auth::SessionStorage
|
6
|
-
|
7
5
|
class << self
|
8
6
|
attr_writer :shop_storage
|
9
7
|
|
@@ -25,6 +23,14 @@ module ShopifyApp
|
|
25
23
|
user_storage.retrieve_by_shopify_user_id(user_id)
|
26
24
|
end
|
27
25
|
|
26
|
+
def destroy_shop_session_by_domain(shopify_domain)
|
27
|
+
shop_storage.destroy_by_shopify_domain(shopify_domain)
|
28
|
+
end
|
29
|
+
|
30
|
+
def destroy_user_session_by_shopify_user_id(user_id)
|
31
|
+
user_storage.destroy_by_shopify_user_id(user_id)
|
32
|
+
end
|
33
|
+
|
28
34
|
def store_shop_session(session)
|
29
35
|
shop_storage.store(session)
|
30
36
|
end
|
@@ -34,8 +40,11 @@ module ShopifyApp
|
|
34
40
|
end
|
35
41
|
|
36
42
|
def shop_storage
|
37
|
-
load_shop_storage || raise(
|
38
|
-
|
43
|
+
load_shop_storage || raise(
|
44
|
+
::ShopifyApp::ConfigurationError,
|
45
|
+
"ShopifyApp::Configuration.shop_session_repository is not configured!\n
|
46
|
+
See docs here: https://github.com/Shopify/shopify_app/blob/main/docs/shopify_app/sessions.md#sessions",
|
47
|
+
)
|
39
48
|
end
|
40
49
|
|
41
50
|
def user_storage
|
@@ -45,8 +54,11 @@ module ShopifyApp
|
|
45
54
|
# ShopifyAPI::Auth::SessionStorage override
|
46
55
|
def store_session(session)
|
47
56
|
if session.online?
|
48
|
-
|
57
|
+
user = session.associated_user
|
58
|
+
ShopifyApp::Logger.debug("Storing online user session - session: #{session.id}")
|
59
|
+
user_storage.store(session, user)
|
49
60
|
else
|
61
|
+
ShopifyApp::Logger.debug("Storing offline store session - session: #{session.id}")
|
50
62
|
shop_storage.store(session)
|
51
63
|
end
|
52
64
|
end
|
@@ -55,9 +67,13 @@ module ShopifyApp
|
|
55
67
|
def load_session(id)
|
56
68
|
match = id.match(/^offline_(.*)/)
|
57
69
|
if match
|
58
|
-
|
70
|
+
domain = match[1]
|
71
|
+
ShopifyApp::Logger.debug("Loading session by domain - domain: #{domain}")
|
72
|
+
retrieve_shop_session_by_shopify_domain(domain)
|
59
73
|
else
|
60
|
-
|
74
|
+
user = id.split("_").last
|
75
|
+
ShopifyApp::Logger.debug("Loading session by user_id - user: #{user}")
|
76
|
+
retrieve_user_session_by_shopify_user_id(user)
|
61
77
|
end
|
62
78
|
end
|
63
79
|
|
@@ -65,14 +81,17 @@ module ShopifyApp
|
|
65
81
|
def delete_session(id)
|
66
82
|
match = id.match(/^offline_(.*)/)
|
67
83
|
|
68
|
-
|
69
|
-
|
84
|
+
if match
|
85
|
+
domain = match[1]
|
86
|
+
ShopifyApp::Logger.debug("Destroying session by domain - domain: #{domain}")
|
87
|
+
destroy_shop_session_by_domain(domain)
|
88
|
+
|
70
89
|
else
|
71
|
-
|
90
|
+
shopify_user_id = id.split("_").last
|
91
|
+
ShopifyApp::Logger.debug("Destroying session by user - user_id: #{shopify_user_id}")
|
92
|
+
destroy_user_session_by_shopify_user_id(shopify_user_id)
|
72
93
|
end
|
73
94
|
|
74
|
-
record.destroy
|
75
|
-
|
76
95
|
true
|
77
96
|
end
|
78
97
|
|
@@ -81,13 +100,46 @@ module ShopifyApp
|
|
81
100
|
def load_shop_storage
|
82
101
|
return unless @shop_storage
|
83
102
|
|
84
|
-
|
103
|
+
shop_storage_class =
|
104
|
+
@shop_storage.respond_to?(:safe_constantize) ? @shop_storage.safe_constantize : @shop_storage
|
105
|
+
|
106
|
+
[
|
107
|
+
:store,
|
108
|
+
:retrieve,
|
109
|
+
:retrieve_by_shopify_domain,
|
110
|
+
].each do |method|
|
111
|
+
raise(
|
112
|
+
::ShopifyApp::ConfigurationError,
|
113
|
+
missing_method_message("shop", method.to_s),
|
114
|
+
) unless shop_storage_class.respond_to?(method)
|
115
|
+
end
|
116
|
+
|
117
|
+
shop_storage_class
|
85
118
|
end
|
86
119
|
|
87
120
|
def load_user_storage
|
88
121
|
return NullUserSessionStore unless @user_storage
|
89
122
|
|
90
|
-
|
123
|
+
user_storage_class =
|
124
|
+
@user_storage.respond_to?(:safe_constantize) ? @user_storage.safe_constantize : @user_storage
|
125
|
+
|
126
|
+
[
|
127
|
+
:store,
|
128
|
+
:retrieve,
|
129
|
+
:retrieve_by_shopify_user_id,
|
130
|
+
].each do |method|
|
131
|
+
raise(
|
132
|
+
::ShopifyApp::ConfigurationError,
|
133
|
+
missing_method_message("user", method.to_s),
|
134
|
+
) unless user_storage_class.respond_to?(method)
|
135
|
+
end
|
136
|
+
|
137
|
+
user_storage_class
|
138
|
+
end
|
139
|
+
|
140
|
+
def missing_method_message(type, method)
|
141
|
+
"Missing method - '#{method}' implementation for #{type}_storage_repository\n
|
142
|
+
See docs here: https://github.com/Shopify/shopify_app/blob/main/docs/shopify_app/sessions.md#sessions"
|
91
143
|
end
|
92
144
|
end
|
93
145
|
end
|
@@ -10,8 +10,8 @@ module ShopifyApp
|
|
10
10
|
end
|
11
11
|
|
12
12
|
def with_shopify_session(&block)
|
13
|
-
ShopifyAPI::Auth::Session.temp(shop: shopify_domain, access_token: shopify_token) do
|
14
|
-
|
13
|
+
ShopifyAPI::Auth::Session.temp(shop: shopify_domain, access_token: shopify_token) do |session|
|
14
|
+
block.call(session)
|
15
15
|
end
|
16
16
|
end
|
17
17
|
end
|
@@ -27,6 +27,10 @@ module ShopifyApp
|
|
27
27
|
construct_session(shop)
|
28
28
|
end
|
29
29
|
|
30
|
+
def destroy_by_shopify_domain(domain)
|
31
|
+
destroy_by(shopify_domain: domain)
|
32
|
+
end
|
33
|
+
|
30
34
|
private
|
31
35
|
|
32
36
|
def construct_session(shop)
|
@@ -34,7 +38,7 @@ module ShopifyApp
|
|
34
38
|
|
35
39
|
ShopifyAPI::Auth::Session.new(
|
36
40
|
shop: shop.shopify_domain,
|
37
|
-
access_token: shop.shopify_token
|
41
|
+
access_token: shop.shopify_token,
|
38
42
|
)
|
39
43
|
end
|
40
44
|
end
|
@@ -29,6 +29,10 @@ module ShopifyApp
|
|
29
29
|
construct_session(shop)
|
30
30
|
end
|
31
31
|
|
32
|
+
def destroy_by_shopify_domain(domain)
|
33
|
+
destroy_by(shopify_domain: domain)
|
34
|
+
end
|
35
|
+
|
32
36
|
private
|
33
37
|
|
34
38
|
def construct_session(shop)
|
@@ -37,7 +41,7 @@ module ShopifyApp
|
|
37
41
|
ShopifyAPI::Auth::Session.new(
|
38
42
|
shop: shop.shopify_domain,
|
39
43
|
access_token: shop.shopify_token,
|
40
|
-
scope: shop.access_scopes
|
44
|
+
scope: shop.access_scopes,
|
41
45
|
)
|
42
46
|
end
|
43
47
|
end
|