shopify_app 21.6.0 → 22.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (114) hide show
  1. checksums.yaml +4 -4
  2. data/.github/CODEOWNERS +1 -0
  3. data/.github/ISSUE_TEMPLATE/bug-report.md +23 -18
  4. data/.github/workflows/build.yml +2 -2
  5. data/.github/workflows/release.yml +1 -1
  6. data/.github/workflows/rubocop.yml +1 -2
  7. data/.nvmrc +1 -1
  8. data/.rubocop.yml +0 -1
  9. data/CHANGELOG.md +115 -0
  10. data/CODE_OF_CONDUCT.md +46 -0
  11. data/CONTRIBUTING.md +1 -6
  12. data/Gemfile.lock +99 -96
  13. data/README.md +47 -2
  14. data/app/assets/javascripts/shopify_app/redirect.js +3 -10
  15. data/app/controllers/concerns/shopify_app/ensure_authenticated_links.rb +5 -1
  16. data/app/controllers/concerns/shopify_app/ensure_has_session.rb +11 -5
  17. data/app/controllers/concerns/shopify_app/ensure_installed.rb +10 -4
  18. data/app/controllers/concerns/shopify_app/shop_access_scopes_verification.rb +5 -1
  19. data/app/controllers/shopify_app/callback_controller.rb +39 -18
  20. data/app/controllers/shopify_app/sessions_controller.rb +25 -4
  21. data/app/views/shopify_app/layouts/app_bridge.html.erb +17 -0
  22. data/app/views/shopify_app/sessions/patch_shopify_id_token.html.erb +0 -0
  23. data/app/views/shopify_app/shared/redirect.html.erb +10 -1
  24. data/config/locales/cs.yml +0 -18
  25. data/config/locales/da.yml +0 -15
  26. data/config/locales/de.yml +0 -17
  27. data/config/locales/en.yml +0 -11
  28. data/config/locales/es.yml +0 -17
  29. data/config/locales/fi.yml +0 -15
  30. data/config/locales/fr.yml +0 -18
  31. data/config/locales/it.yml +0 -16
  32. data/config/locales/ja.yml +0 -12
  33. data/config/locales/ko.yml +0 -14
  34. data/config/locales/nb.yml +0 -16
  35. data/config/locales/nl.yml +0 -16
  36. data/config/locales/pl.yml +0 -16
  37. data/config/locales/pt-BR.yml +0 -16
  38. data/config/locales/pt-PT.yml +0 -17
  39. data/config/locales/sv.yml +0 -16
  40. data/config/locales/th.yml +0 -15
  41. data/config/locales/tr.yml +0 -17
  42. data/config/locales/vi.yml +0 -17
  43. data/config/locales/zh-CN.yml +0 -11
  44. data/config/locales/zh-TW.yml +0 -11
  45. data/config/routes.rb +2 -1
  46. data/docs/Quickstart.md +9 -2
  47. data/docs/Troubleshooting.md +0 -23
  48. data/docs/Upgrading.md +64 -1
  49. data/docs/shopify_app/authentication.md +179 -58
  50. data/docs/shopify_app/controller-concerns.md +53 -12
  51. data/docs/shopify_app/generators.md +2 -2
  52. data/docs/shopify_app/sessions.md +358 -0
  53. data/docs/shopify_app/webhooks.md +88 -11
  54. data/karma.conf.js +6 -4
  55. data/lib/generators/shopify_app/add_declarative_webhook/add_declarative_webhook_generator.rb +53 -0
  56. data/lib/generators/shopify_app/add_declarative_webhook/templates/webhook_controller.rb.tt +13 -0
  57. data/lib/generators/shopify_app/add_declarative_webhook/templates/webhook_job.rb.tt +15 -0
  58. data/lib/generators/shopify_app/{add_gdpr_jobs/add_gdpr_jobs_generator.rb → add_privacy_jobs/add_privacy_jobs_generator.rb} +1 -1
  59. data/lib/generators/shopify_app/add_webhook/add_webhook_generator.rb +6 -1
  60. data/lib/generators/shopify_app/add_webhook/templates/webhook_job.rb.tt +1 -0
  61. data/lib/generators/shopify_app/install/templates/shopify_app.rb.tt +5 -2
  62. data/lib/generators/shopify_app/shopify_app_generator.rb +1 -1
  63. data/lib/generators/shopify_app/user_model/templates/db/migrate/add_user_expires_at_column.erb +5 -0
  64. data/lib/generators/shopify_app/user_model/user_model_generator.rb +20 -0
  65. data/lib/shopify_app/admin_api/with_token_refetch.rb +27 -0
  66. data/lib/shopify_app/auth/post_authenticate_tasks.rb +48 -0
  67. data/lib/shopify_app/auth/token_exchange.rb +73 -0
  68. data/lib/shopify_app/configuration.rb +69 -1
  69. data/lib/shopify_app/controller_concerns/app_proxy_verification.rb +1 -1
  70. data/lib/shopify_app/controller_concerns/csrf_protection.rb +2 -1
  71. data/lib/shopify_app/controller_concerns/embedded_app.rb +42 -3
  72. data/lib/shopify_app/controller_concerns/ensure_billing.rb +14 -3
  73. data/lib/shopify_app/controller_concerns/frame_ancestors.rb +1 -1
  74. data/lib/shopify_app/controller_concerns/localization.rb +11 -8
  75. data/lib/shopify_app/controller_concerns/login_protection.rb +34 -38
  76. data/lib/shopify_app/controller_concerns/redirect_for_embedded.rb +5 -0
  77. data/lib/shopify_app/controller_concerns/sanitized_params.rb +4 -0
  78. data/lib/shopify_app/controller_concerns/token_exchange.rb +111 -0
  79. data/lib/shopify_app/controller_concerns/with_shopify_id_token.rb +48 -0
  80. data/lib/shopify_app/engine.rb +5 -11
  81. data/lib/shopify_app/managers/webhooks_manager.rb +6 -2
  82. data/lib/shopify_app/middleware/jwt_middleware.rb +13 -9
  83. data/lib/shopify_app/session/in_memory_user_session_store.rb +1 -1
  84. data/lib/shopify_app/session/jwt.rb +9 -0
  85. data/lib/shopify_app/session/session_repository.rb +49 -8
  86. data/lib/shopify_app/session/shop_session_storage.rb +4 -0
  87. data/lib/shopify_app/session/shop_session_storage_with_scopes.rb +4 -0
  88. data/lib/shopify_app/session/user_session_storage.rb +4 -0
  89. data/lib/shopify_app/session/user_session_storage_with_scopes.rb +25 -0
  90. data/lib/shopify_app/test_helpers/shopify_session_helper.rb +1 -0
  91. data/lib/shopify_app/utils.rb +14 -1
  92. data/lib/shopify_app/version.rb +1 -1
  93. data/lib/shopify_app.rb +9 -3
  94. data/package.json +5 -6
  95. data/shopify_app.gemspec +4 -4
  96. data/yarn.lock +2134 -3905
  97. metadata +51 -60
  98. data/.github/workflows/stale.yml +0 -43
  99. data/app/assets/images/storage_access.svg +0 -1
  100. data/app/assets/javascripts/shopify_app/app_bridge_3.1.1.js +0 -10
  101. data/app/assets/javascripts/shopify_app/app_bridge_redirect.js +0 -22
  102. data/app/assets/javascripts/shopify_app/app_bridge_utils_3.1.1.js +0 -1
  103. data/app/controllers/concerns/shopify_app/authenticated.rb +0 -17
  104. data/app/controllers/concerns/shopify_app/require_known_shop.rb +0 -16
  105. data/docs/shopify_app/script-tags.md +0 -28
  106. data/docs/shopify_app/session-repository.md +0 -79
  107. data/lib/generators/shopify_app/add_marketing_activity_extension/add_marketing_activity_extension_generator.rb +0 -42
  108. data/lib/generators/shopify_app/add_marketing_activity_extension/templates/marketing_activities_controller.rb +0 -63
  109. data/lib/shopify_app/controller_concerns/itp.rb +0 -50
  110. data/lib/shopify_app/jobs/scripttags_manager_job.rb +0 -16
  111. data/lib/shopify_app/managers/scripttags_manager.rb +0 -85
  112. /data/lib/generators/shopify_app/{add_gdpr_jobs → add_privacy_jobs}/templates/customers_data_request_job.rb.tt +0 -0
  113. /data/lib/generators/shopify_app/{add_gdpr_jobs → add_privacy_jobs}/templates/customers_redact_job.rb.tt +0 -0
  114. /data/lib/generators/shopify_app/{add_gdpr_jobs → add_privacy_jobs}/templates/shop_redact_job.rb.tt +0 -0
@@ -1,24 +1,22 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "browser_sniffer"
4
-
5
3
  module ShopifyApp
6
4
  module LoginProtection
7
5
  extend ActiveSupport::Concern
8
6
  include ShopifyApp::SanitizedParams
9
7
 
10
8
  included do
11
- if defined?(ShopifyApp::RequireKnownShop) &&
12
- defined?(ShopifyApp::EnsureInstalled) &&
13
- ancestors.include?(ShopifyApp::RequireKnownShop || ShopifyApp::EnsureInstalled)
9
+ if defined?(ShopifyApp::EnsureInstalled) &&
10
+ ancestors.include?(ShopifyApp::EnsureInstalled)
14
11
  message = <<~EOS
15
- We detected the use of incompatible concerns (RequireKnownShop/EnsureInstalled and LoginProtection) in #{name},
16
- which may lead to unpredictable behavior. In a future release of this library this will raise an error.
12
+ We detected the use of incompatible concerns (EnsureInstalled and LoginProtection) in #{name},
13
+ which leads to unpredictable behavior. You cannot include both concerns in the same controller.
17
14
  EOS
18
- ShopifyApp::Logger.deprecated(message, "22.0.0")
15
+ raise message
19
16
  end
20
17
 
21
18
  rescue_from ShopifyAPI::Errors::HttpResponseError, with: :handle_http_error
19
+ include ShopifyApp::WithShopifyIdToken
22
20
  end
23
21
 
24
22
  ACCESS_TOKEN_REQUIRED_HEADER = "X-Shopify-API-Request-Failure-Unauthorized"
@@ -30,6 +28,12 @@ module ShopifyApp
30
28
  return redirect_to_login
31
29
  end
32
30
 
31
+ if ShopifyApp.configuration.check_session_expiry_date && current_shopify_session.expired?
32
+ ShopifyApp::Logger.debug("Session expired, redirecting to login")
33
+ clear_shopify_session
34
+ return redirect_to_login
35
+ end
36
+
33
37
  if ShopifyApp.configuration.reauth_on_access_scope_changes &&
34
38
  !ShopifyApp.configuration.user_access_scopes_strategy.covers_scopes?(current_shopify_session)
35
39
  clear_shopify_session
@@ -50,7 +54,7 @@ module ShopifyApp
50
54
  @current_shopify_session ||= begin
51
55
  cookie_name = ShopifyAPI::Auth::Oauth::SessionCookie::SESSION_COOKIE_NAME
52
56
  load_current_session(
53
- auth_header: request.headers["HTTP_AUTHORIZATION"],
57
+ shopify_id_token: shopify_id_token,
54
58
  cookies: { cookie_name => cookies.encrypted[cookie_name] },
55
59
  is_online: online_token_configured?,
56
60
  )
@@ -75,13 +79,6 @@ module ShopifyApp
75
79
  response.set_header(ACCESS_TOKEN_REQUIRED_HEADER, "true")
76
80
  end
77
81
 
78
- def jwt_expire_at
79
- expire_at = request.env["jwt.expire_at"]
80
- return unless expire_at
81
-
82
- expire_at - 5.seconds # 5s gap to start fetching new token in advance
83
- end
84
-
85
82
  def add_top_level_redirection_headers(url: nil, ignore_response_code: false)
86
83
  if request.xhr? && (ignore_response_code || response.code.to_i == 401)
87
84
  ShopifyApp::Logger.debug("Adding top level redirection headers")
@@ -91,8 +88,8 @@ module ShopifyApp
91
88
  params[:shop] = if current_shopify_session
92
89
  current_shopify_session.shop
93
90
 
94
- elsif (matches = request.headers["HTTP_AUTHORIZATION"]&.match(/^Bearer (.+)$/))
95
- jwt_payload = ShopifyAPI::Auth::JwtPayload.new(T.must(matches[1]))
91
+ elsif shopify_id_token
92
+ jwt_payload = ShopifyAPI::Auth::JwtPayload.new(shopify_id_token)
96
93
  jwt_payload.shop
97
94
  end
98
95
  end
@@ -100,21 +97,12 @@ module ShopifyApp
100
97
  url ||= login_url_with_optional_shop
101
98
 
102
99
  ShopifyApp::Logger.debug("Setting Reauthorize-Url to #{url}")
103
- response.set_header("X-Shopify-API-Request-Failure-Reauthorize", "1")
104
- response.set_header("X-Shopify-API-Request-Failure-Reauthorize-Url", url)
100
+ RedirectForEmbedded.add_app_bridge_redirect_url_header(url, response)
105
101
  end
106
102
  end
107
103
 
108
104
  protected
109
105
 
110
- def jwt_shopify_domain
111
- request.env["jwt.shopify_domain"]
112
- end
113
-
114
- def jwt_shopify_user_id
115
- request.env["jwt.shopify_user_id"]
116
- end
117
-
118
106
  def host
119
107
  params[:host]
120
108
  end
@@ -134,17 +122,25 @@ module ShopifyApp
134
122
  query = Rack::Utils.parse_nested_query(referer.query)
135
123
  query = query.merge(sanitized_params).to_query
136
124
  end
137
- session[:return_to] = query.blank? ? path.to_s : "#{path}?#{query}"
125
+ session[:return_to] = return_to_url(path, query)
138
126
  ShopifyApp::Logger.debug("Redirecting to #{login_url_with_optional_shop}")
139
127
  redirect_to(login_url_with_optional_shop)
140
128
  end
141
129
  end
142
130
 
131
+ # Apps which use cookies for session storage, and thus have a 4kB session data
132
+ # limit, may choose to override this method to prevent excessively-long `query`
133
+ # strings from causing a CookieOverflow error.
134
+ def return_to_url(path, query)
135
+ query.blank? ? path.to_s : "#{path}?#{query}"
136
+ end
137
+
143
138
  def close_session
144
- clear_shopify_session
145
139
  ShopifyApp::Logger.debug("Closing session")
146
- ShopifyApp::Logger.debug("Redirecting to #{login_url_with_optional_shop}")
147
- redirect_to(login_url_with_optional_shop)
140
+ clear_shopify_session
141
+
142
+ ShopifyApp::Logger.debug("Redirecting to login")
143
+ redirect_to_login
148
144
  end
149
145
 
150
146
  def handle_http_error(error)
@@ -208,7 +204,7 @@ module ShopifyApp
208
204
  render(
209
205
  "shopify_app/shared/redirect",
210
206
  layout: false,
211
- locals: { url: url, current_shopify_domain: current_shopify_domain },
207
+ locals: { url: url, current_shopify_domain: current_shopify_domain, is_iframe: embedded? },
212
208
  )
213
209
  else
214
210
  redirect_to(url)
@@ -259,7 +255,7 @@ module ShopifyApp
259
255
  end
260
256
 
261
257
  def online_token_configured?
262
- !ShopifyApp.configuration.user_session_repository.blank? && ShopifyApp::SessionRepository.user_storage.present?
258
+ ShopifyApp.configuration.online_token_configured?
263
259
  end
264
260
 
265
261
  def user_session_expected?
@@ -269,10 +265,10 @@ module ShopifyApp
269
265
  online_token_configured?
270
266
  end
271
267
 
272
- def load_current_session(auth_header: nil, cookies: nil, is_online: false)
268
+ def load_current_session(shopify_id_token: nil, cookies: nil, is_online: false)
273
269
  return ShopifyAPI::Context.load_private_session if ShopifyAPI::Context.private?
274
270
 
275
- session_id = ShopifyAPI::Utils::SessionUtils.current_session_id(auth_header, cookies, is_online)
271
+ session_id = ShopifyAPI::Utils::SessionUtils.current_session_id(shopify_id_token, cookies, is_online)
276
272
  return nil unless session_id
277
273
 
278
274
  ShopifyApp::SessionRepository.load_session(session_id)
@@ -280,8 +276,8 @@ module ShopifyApp
280
276
 
281
277
  def requested_by_javascript?
282
278
  request.xhr? ||
283
- request.content_type == "text/javascript" ||
284
- request.content_type == "application/javascript"
279
+ request.media_type == "text/javascript" ||
280
+ request.media_type == "application/javascript"
285
281
  end
286
282
  end
287
283
  end
@@ -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?
@@ -33,5 +33,9 @@ module ShopifyApp
33
33
  end
34
34
  end
35
35
  end
36
+
37
+ def embedded?
38
+ params[:embedded] == "1" || request.env["HTTP_SEC_FETCH_DEST"] == "iframe"
39
+ end
36
40
  end
37
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
@@ -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
@@ -5,7 +5,7 @@ module ShopifyApp
5
5
  private
6
6
 
7
7
  def args_info(job)
8
- log_disabled_classes = ["ShopifyApp::ScripttagsManagerJob", "ShopifyApp::WebhooksManagerJob"]
8
+ log_disabled_classes = ["ShopifyApp::WebhooksManagerJob"]
9
9
  return "" if log_disabled_classes.include?(job.class.name)
10
10
 
11
11
  super
@@ -16,26 +16,20 @@ module ShopifyApp
16
16
  engine_name "shopify_app"
17
17
  isolate_namespace ShopifyApp
18
18
 
19
+ initializer "shopify_app.middleware" do |app|
20
+ app.config.middleware.insert_after(::Rack::Runtime, ShopifyApp::JWTMiddleware)
21
+ end
22
+
19
23
  initializer "shopify_app.assets.precompile" do |app|
20
24
  app.config.assets.precompile += [
21
25
  "shopify_app/redirect.js",
22
- "shopify_app/post_redirect.js",
23
- "shopify_app/top_level.js",
24
- "shopify_app/enable_cookies.js",
25
- "shopify_app/request_storage_access.js",
26
- "storage_access.svg",
27
26
  ]
28
27
  end
29
28
 
30
- initializer "shopify_app.middleware" do |app|
31
- app.config.middleware.insert_after(::Rack::Runtime, ShopifyApp::JWTMiddleware)
32
- end
33
-
34
29
  initializer "shopify_app.redact_job_params" do
35
30
  ActiveSupport.on_load(:active_job) do
36
31
  if ActiveJob::Base.respond_to?(:log_arguments?)
37
32
  WebhooksManagerJob.log_arguments = false
38
- ScripttagsManagerJob.log_arguments = false
39
33
  elsif ActiveJob::Logging::LogSubscriber.private_method_defined?(:args_info)
40
34
  ActiveJob::Logging::LogSubscriber.prepend(RedactJobParams)
41
35
  end
@@ -26,6 +26,7 @@ module ShopifyApp
26
26
 
27
27
  ShopifyApp::Logger.debug("Recreating webhooks")
28
28
  add_registrations
29
+ create_webhooks(session: session)
29
30
  end
30
31
 
31
32
  def destroy_webhooks(session:)
@@ -43,13 +44,16 @@ module ShopifyApp
43
44
  ShopifyApp::Logger.debug("Adding registrations to webhooks")
44
45
  ShopifyApp.configuration.webhooks.each do |attributes|
45
46
  webhook_path = path(attributes)
47
+ delivery_method = attributes[:delivery_method] || :http
46
48
 
47
49
  ShopifyAPI::Webhooks::Registry.add_registration(
48
50
  topic: attributes[:topic],
49
- delivery_method: attributes[:delivery_method] || :http,
51
+ delivery_method: delivery_method,
50
52
  path: webhook_path,
51
- handler: webhook_job_klass(webhook_path),
53
+ handler: delivery_method == :http ? webhook_job_klass(webhook_path) : nil,
52
54
  fields: attributes[:fields],
55
+ filter: attributes[:filter],
56
+ metafield_namespaces: attributes[:metafield_namespaces],
53
57
  )
54
58
  end
55
59
  end
@@ -2,16 +2,17 @@
2
2
 
3
3
  module ShopifyApp
4
4
  class JWTMiddleware
5
- TOKEN_REGEX = /^Bearer\s+(.*?)$/
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 authorization_header(env)
13
+ return call_next(env) unless ShopifyApp.configuration.embedded_app?
13
14
 
14
- token = extract_token(env)
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 authorization_header(env)
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 extract_token(env)
32
- match = authorization_header(env).match(TOKEN_REGEX)
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 = ShopifyApp::JWT.new(token)
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
@@ -5,7 +5,7 @@ module ShopifyApp
5
5
  class << self
6
6
  def store(session, user)
7
7
  id = super
8
- repo[user.shopify_user_id] = session
8
+ repo[user.id.to_s] = session
9
9
  id
10
10
  end
11
11
 
@@ -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
@@ -60,5 +61,13 @@ module ShopifyApp
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
@@ -23,6 +23,14 @@ module ShopifyApp
23
23
  user_storage.retrieve_by_shopify_user_id(user_id)
24
24
  end
25
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
+
26
34
  def store_shop_session(session)
27
35
  shop_storage.store(session)
28
36
  end
@@ -34,7 +42,8 @@ module ShopifyApp
34
42
  def shop_storage
35
43
  load_shop_storage || raise(
36
44
  ::ShopifyApp::ConfigurationError,
37
- "ShopifySessionRepository.shop_storage is not configured!",
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",
38
47
  )
39
48
  end
40
49
 
@@ -72,18 +81,17 @@ module ShopifyApp
72
81
  def delete_session(id)
73
82
  match = id.match(/^offline_(.*)/)
74
83
 
75
- record = if match
84
+ if match
76
85
  domain = match[1]
77
86
  ShopifyApp::Logger.debug("Destroying session by domain - domain: #{domain}")
78
- Shop.find_by(shopify_domain: match[1])
87
+ destroy_shop_session_by_domain(domain)
88
+
79
89
  else
80
90
  shopify_user_id = id.split("_").last
81
91
  ShopifyApp::Logger.debug("Destroying session by user - user_id: #{shopify_user_id}")
82
- User.find_by(shopify_user_id: shopify_user_id)
92
+ destroy_user_session_by_shopify_user_id(shopify_user_id)
83
93
  end
84
94
 
85
- record.destroy
86
-
87
95
  true
88
96
  end
89
97
 
@@ -92,13 +100,46 @@ module ShopifyApp
92
100
  def load_shop_storage
93
101
  return unless @shop_storage
94
102
 
95
- @shop_storage.respond_to?(:safe_constantize) ? @shop_storage.safe_constantize : @shop_storage
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
96
118
  end
97
119
 
98
120
  def load_user_storage
99
121
  return NullUserSessionStore unless @user_storage
100
122
 
101
- @user_storage.respond_to?(:safe_constantize) ? @user_storage.safe_constantize : @user_storage
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"
102
143
  end
103
144
  end
104
145
  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)
@@ -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)
@@ -28,6 +28,10 @@ module ShopifyApp
28
28
  construct_session(user)
29
29
  end
30
30
 
31
+ def destroy_by_shopify_user_id(user_id)
32
+ destroy_by(shopify_user_id: user_id)
33
+ end
34
+
31
35
  private
32
36
 
33
37
  def construct_session(user)