shopify_app 13.2.0 → 20.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (167) hide show
  1. checksums.yaml +4 -4
  2. data/.github/CODEOWNERS +1 -0
  3. data/.github/ISSUE_TEMPLATE/bug-report.md +63 -0
  4. data/.github/ISSUE_TEMPLATE/config.yml +1 -0
  5. data/.github/ISSUE_TEMPLATE/feature-request.md +33 -0
  6. data/.github/PULL_REQUEST_TEMPLATE.md +22 -0
  7. data/.github/workflows/build.yml +40 -0
  8. data/.github/workflows/cla.yml +22 -0
  9. data/.github/workflows/close-waiting-for-response-issues.yml +20 -0
  10. data/.github/workflows/release.yml +24 -0
  11. data/.github/workflows/remove-labels-on-activity.yml +16 -0
  12. data/.github/workflows/rubocop.yml +22 -0
  13. data/.github/workflows/stale.yml +31 -0
  14. data/.gitignore +1 -2
  15. data/.nvmrc +1 -1
  16. data/.rubocop.yml +2 -0
  17. data/.ruby-version +1 -1
  18. data/CHANGELOG.md +221 -0
  19. data/CONTRIBUTING.md +81 -0
  20. data/Gemfile +5 -2
  21. data/Gemfile.lock +248 -0
  22. data/README.md +74 -563
  23. data/Rakefile +4 -3
  24. data/SECURITY.md +59 -0
  25. data/app/assets/images/storage_access.svg +1 -2
  26. data/app/assets/javascripts/shopify_app/app_bridge_3.1.1.js +10 -0
  27. data/app/assets/javascripts/shopify_app/app_bridge_redirect.js +22 -0
  28. data/app/assets/javascripts/shopify_app/app_bridge_utils_3.1.1.js +1 -0
  29. data/app/assets/javascripts/shopify_app/post_redirect.js +9 -0
  30. data/app/assets/javascripts/shopify_app/redirect.js +10 -14
  31. data/app/assets/javascripts/shopify_app/storage_access.js +5 -10
  32. data/app/assets/javascripts/shopify_app/top_level_interaction.js +1 -1
  33. data/app/controllers/concerns/shopify_app/authenticated.rb +4 -0
  34. data/app/controllers/concerns/shopify_app/ensure_authenticated_links.rb +39 -0
  35. data/app/controllers/concerns/shopify_app/require_known_shop.rb +48 -0
  36. data/app/controllers/concerns/shopify_app/shop_access_scopes_verification.rb +40 -0
  37. data/app/controllers/shopify_app/authenticated_controller.rb +1 -0
  38. data/app/controllers/shopify_app/callback_controller.rb +56 -77
  39. data/app/controllers/shopify_app/extension_verification_controller.rb +2 -7
  40. data/app/controllers/shopify_app/sessions_controller.rb +33 -117
  41. data/app/controllers/shopify_app/webhooks_controller.rb +5 -26
  42. data/app/views/shopify_app/partials/_button_styles.html.erb +41 -36
  43. data/app/views/shopify_app/partials/_card_styles.html.erb +3 -3
  44. data/app/views/shopify_app/partials/_empty_state_styles.html.erb +28 -59
  45. data/app/views/shopify_app/partials/_form_styles.html.erb +56 -0
  46. data/app/views/shopify_app/partials/_layout_styles.html.erb +16 -1
  47. data/app/views/shopify_app/partials/_typography_styles.html.erb +6 -6
  48. data/app/views/shopify_app/sessions/enable_cookies.html.erb +2 -7
  49. data/app/views/shopify_app/sessions/new.html.erb +38 -110
  50. data/app/views/shopify_app/sessions/request_storage_access.html.erb +12 -12
  51. data/app/views/shopify_app/sessions/top_level_interaction.html.erb +21 -22
  52. data/app/views/shopify_app/shared/post_redirect_to_auth_shopify.html.erb +13 -0
  53. data/app/views/shopify_app/shared/redirect.html.erb +2 -2
  54. data/config/locales/de.yml +11 -11
  55. data/config/locales/ja.yml +4 -4
  56. data/config/locales/nl.yml +2 -2
  57. data/config/locales/th.yml +4 -4
  58. data/config/locales/vi.yml +22 -0
  59. data/config/locales/zh-CN.yml +2 -2
  60. data/config/routes.rb +20 -12
  61. data/docs/Quickstart.md +19 -83
  62. data/docs/Releasing.md +18 -15
  63. data/docs/Troubleshooting.md +140 -5
  64. data/docs/Upgrading.md +247 -0
  65. data/docs/shopify_app/authentication.md +128 -0
  66. data/docs/shopify_app/content-security-policy.md +10 -0
  67. data/docs/shopify_app/engine.md +82 -0
  68. data/docs/shopify_app/generators.md +127 -0
  69. data/docs/shopify_app/handling-access-scopes-changes.md +24 -0
  70. data/docs/shopify_app/script-tags.md +28 -0
  71. data/docs/shopify_app/session-repository.md +88 -0
  72. data/docs/shopify_app/testing.md +38 -0
  73. data/docs/shopify_app/webhooks.md +72 -0
  74. data/karma.conf.js +1 -1
  75. data/lib/generators/shopify_app/add_after_authenticate_job/add_after_authenticate_job_generator.rb +10 -9
  76. data/lib/generators/shopify_app/add_after_authenticate_job/templates/after_authenticate_job.rb +1 -0
  77. data/lib/generators/shopify_app/add_marketing_activity_extension/add_marketing_activity_extension_generator.rb +4 -3
  78. data/lib/generators/shopify_app/add_webhook/add_webhook_generator.rb +15 -14
  79. data/lib/generators/shopify_app/add_webhook/templates/webhook_job.rb.tt +9 -1
  80. data/lib/generators/shopify_app/app_proxy_controller/app_proxy_controller_generator.rb +7 -6
  81. data/lib/generators/shopify_app/app_proxy_controller/templates/app_proxy_controller.rb +2 -1
  82. data/lib/generators/shopify_app/app_proxy_controller/templates/app_proxy_route.rb +1 -1
  83. data/lib/generators/shopify_app/authenticated_controller/authenticated_controller_generator.rb +4 -4
  84. data/lib/generators/shopify_app/controllers/controllers_generator.rb +5 -4
  85. data/lib/generators/shopify_app/home_controller/home_controller_generator.rb +27 -4
  86. data/lib/generators/shopify_app/home_controller/templates/home_controller.rb +12 -2
  87. data/lib/generators/shopify_app/home_controller/templates/index.html.erb +74 -16
  88. data/lib/generators/shopify_app/home_controller/templates/unauthenticated_home_controller.rb +16 -0
  89. data/lib/generators/shopify_app/install/install_generator.rb +52 -40
  90. data/lib/generators/shopify_app/install/templates/embedded_app.html.erb +5 -2
  91. data/lib/generators/shopify_app/install/templates/flash_messages.js +0 -2
  92. data/lib/generators/shopify_app/install/templates/session_store.rb +2 -1
  93. data/lib/generators/shopify_app/install/templates/shopify_app.js +1 -1
  94. data/lib/generators/shopify_app/install/templates/shopify_app.rb.tt +43 -5
  95. data/lib/generators/shopify_app/install/templates/shopify_app_importmap.js +13 -0
  96. data/lib/generators/shopify_app/products_controller/products_controller_generator.rb +19 -0
  97. data/lib/generators/shopify_app/products_controller/templates/products_controller.rb +8 -0
  98. data/lib/generators/shopify_app/rotate_shopify_token_job/rotate_shopify_token_job_generator.rb +4 -4
  99. data/lib/generators/shopify_app/rotate_shopify_token_job/templates/rotate_shopify_token.rake +1 -0
  100. data/lib/generators/shopify_app/rotate_shopify_token_job/templates/rotate_shopify_token_job.rb +1 -1
  101. data/lib/generators/shopify_app/routes/routes_generator.rb +6 -5
  102. data/lib/generators/shopify_app/routes/templates/routes.rb +5 -5
  103. data/lib/generators/shopify_app/shop_model/shop_model_generator.rb +35 -7
  104. data/lib/generators/shopify_app/shop_model/templates/db/migrate/add_shop_access_scopes_column.erb +5 -0
  105. data/lib/generators/shopify_app/shop_model/templates/shop.rb +2 -1
  106. data/lib/generators/shopify_app/shopify_app_generator.rb +4 -3
  107. data/lib/generators/shopify_app/user_model/templates/db/migrate/add_user_access_scopes_column.erb +5 -0
  108. data/lib/generators/shopify_app/user_model/templates/user.rb +2 -1
  109. data/lib/generators/shopify_app/user_model/user_model_generator.rb +35 -7
  110. data/lib/generators/shopify_app/views/views_generator.rb +5 -4
  111. data/lib/shopify_app/access_scopes/noop_strategy.rb +13 -0
  112. data/lib/shopify_app/access_scopes/shop_strategy.rb +24 -0
  113. data/lib/shopify_app/access_scopes/user_strategy.rb +41 -0
  114. data/lib/shopify_app/configuration.rb +58 -11
  115. data/lib/shopify_app/controller_concerns/app_proxy_verification.rb +4 -4
  116. data/lib/shopify_app/controller_concerns/csrf_protection.rb +16 -0
  117. data/lib/shopify_app/controller_concerns/embedded_app.rb +6 -3
  118. data/lib/shopify_app/controller_concerns/ensure_billing.rb +243 -0
  119. data/lib/shopify_app/controller_concerns/frame_ancestors.rb +16 -0
  120. data/lib/shopify_app/controller_concerns/itp.rb +3 -3
  121. data/lib/shopify_app/controller_concerns/localization.rb +1 -0
  122. data/lib/shopify_app/controller_concerns/login_protection.rb +105 -90
  123. data/lib/shopify_app/controller_concerns/payload_verification.rb +25 -0
  124. data/lib/shopify_app/controller_concerns/redirect_for_embedded.rb +36 -0
  125. data/lib/shopify_app/controller_concerns/sanitized_params.rb +36 -0
  126. data/lib/shopify_app/controller_concerns/webhook_verification.rb +3 -18
  127. data/lib/shopify_app/engine.rb +26 -11
  128. data/lib/shopify_app/errors.rb +34 -0
  129. data/lib/shopify_app/jobs/scripttags_manager_job.rb +2 -2
  130. data/lib/shopify_app/jobs/webhooks_manager_job.rb +4 -5
  131. data/lib/shopify_app/managers/scripttags_manager.rb +12 -6
  132. data/lib/shopify_app/managers/webhooks_manager.rb +62 -42
  133. data/lib/shopify_app/middleware/jwt_middleware.rb +6 -3
  134. data/lib/shopify_app/session/in_memory_session_store.rb +2 -3
  135. data/lib/shopify_app/session/in_memory_shop_session_store.rb +10 -7
  136. data/lib/shopify_app/session/in_memory_user_session_store.rb +10 -7
  137. data/lib/shopify_app/session/jwt.rb +19 -16
  138. data/lib/shopify_app/session/null_user_session_store.rb +2 -1
  139. data/lib/shopify_app/session/session_repository.rb +40 -2
  140. data/lib/shopify_app/session/session_storage.rb +4 -6
  141. data/lib/shopify_app/session/shop_session_storage.rb +6 -6
  142. data/lib/shopify_app/session/shop_session_storage_with_scopes.rb +57 -0
  143. data/lib/shopify_app/session/user_session_storage.rb +20 -7
  144. data/lib/shopify_app/session/user_session_storage_with_scopes.rb +71 -0
  145. data/lib/shopify_app/test_helpers/all.rb +2 -1
  146. data/lib/shopify_app/test_helpers/webhook_verification_helper.rb +4 -3
  147. data/lib/shopify_app/utils.rb +14 -7
  148. data/lib/shopify_app/version.rb +2 -1
  149. data/lib/shopify_app.rb +52 -29
  150. data/package.json +7 -8
  151. data/service.yml +1 -5
  152. data/shopify_app.gemspec +22 -20
  153. data/translation.yml +1 -1
  154. data/yarn.lock +2173 -2206
  155. metadata +110 -56
  156. data/.github/ISSUE_TEMPLATE.md +0 -14
  157. data/.github/probots.yml +0 -2
  158. data/.travis.yml +0 -28
  159. data/config/locales/hi.yml +0 -23
  160. data/config/locales/ms.yml +0 -22
  161. data/docs/install-on-dev-shop.png +0 -0
  162. data/docs/test-your-app.png +0 -0
  163. data/lib/generators/shopify_app/install/templates/omniauth.rb +0 -3
  164. data/lib/generators/shopify_app/install/templates/shopify_provider.rb +0 -20
  165. data/lib/generators/shopify_app/install/templates/user_agent.rb +0 -6
  166. data/lib/shopify_app/middleware/same_site_cookie_middleware.rb +0 -34
  167. data/package-lock.json +0 -7245
@@ -1,97 +1,110 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'browser_sniffer'
3
+ require "browser_sniffer"
4
4
 
5
5
  module ShopifyApp
6
6
  module LoginProtection
7
7
  extend ActiveSupport::Concern
8
8
  include ShopifyApp::Itp
9
-
10
- class ShopifyDomainNotFound < StandardError; end
9
+ include ShopifyApp::SanitizedParams
11
10
 
12
11
  included do
13
12
  after_action :set_test_cookie
14
- rescue_from ActiveResource::UnauthorizedAccess, with: :close_session
13
+ rescue_from ShopifyAPI::Errors::HttpResponseError, with: :handle_http_error
15
14
  end
16
15
 
16
+ ACCESS_TOKEN_REQUIRED_HEADER = "X-Shopify-API-Request-Failure-Unauthorized"
17
+
17
18
  def activate_shopify_session
18
- return redirect_to_login if current_shopify_session.blank?
19
- clear_top_level_oauth_cookie
19
+ if current_shopify_session.blank?
20
+ signal_access_token_required
21
+ return redirect_to_login
22
+ end
23
+
24
+ unless current_shopify_session.scope.to_a.empty? ||
25
+ current_shopify_session.scope.covers?(ShopifyAPI::Context.scope)
26
+
27
+ clear_shopify_session
28
+ return redirect_to_login
29
+ end
20
30
 
21
31
  begin
22
- ShopifyAPI::Base.activate_session(current_shopify_session)
32
+ ShopifyAPI::Context.activate_session(current_shopify_session)
23
33
  yield
24
34
  ensure
25
- ShopifyAPI::Base.clear_session
35
+ ShopifyAPI::Context.deactivate_session
26
36
  end
27
37
  end
28
38
 
29
39
  def current_shopify_session
30
40
  @current_shopify_session ||= begin
31
- user_session || shop_session
41
+ cookie_name = ShopifyAPI::Auth::Oauth::SessionCookie::SESSION_COOKIE_NAME
42
+ ShopifyAPI::Utils::SessionUtils.load_current_session(
43
+ auth_header: request.headers["HTTP_AUTHORIZATION"],
44
+ cookies: { cookie_name => cookies.encrypted[cookie_name] },
45
+ is_online: user_session_expected?
46
+ )
47
+ rescue ShopifyAPI::Errors::CookieNotFoundError
48
+ nil
49
+ rescue ShopifyAPI::Errors::InvalidJwtTokenError
50
+ nil
32
51
  end
33
52
  end
34
53
 
35
- def user_session
36
- user_session_by_jwt || user_session_by_cookie
37
- end
38
-
39
- def user_session_by_jwt
40
- return unless ShopifyApp.configuration.allow_jwt_authentication
41
- return unless jwt_shopify_user_id
42
- ShopifyApp::SessionRepository.retrieve_user_session_by_shopify_user_id(jwt_shopify_user_id)
43
- end
54
+ def login_again_if_different_user_or_shop
55
+ return unless session_id_conflicts_with_params || session_shop_conflicts_with_params
44
56
 
45
- def user_session_by_cookie
46
- return unless session[:user_id].present?
47
- ShopifyApp::SessionRepository.retrieve_user_session(session[:user_id])
57
+ clear_shopify_session
58
+ redirect_to_login
48
59
  end
49
60
 
50
- def shop_session
51
- shop_session_by_jwt || shop_session_by_cookie
61
+ def signal_access_token_required
62
+ response.set_header(ACCESS_TOKEN_REQUIRED_HEADER, "true")
52
63
  end
53
64
 
54
- def shop_session_by_jwt
55
- return unless ShopifyApp.configuration.allow_jwt_authentication
56
- return unless jwt_shopify_domain
57
- ShopifyApp::SessionRepository.retrieve_shop_session_by_shopify_domain(jwt_shopify_domain)
58
- end
65
+ def jwt_expire_at
66
+ expire_at = request.env["jwt.expire_at"]
67
+ return unless expire_at
59
68
 
60
- def shop_session_by_cookie
61
- return unless session[:shop_id].present?
62
- ShopifyApp::SessionRepository.retrieve_shop_session(session[:shop_id])
69
+ expire_at - 5.seconds # 5s gap to start fetching new token in advance
63
70
  end
64
71
 
65
- def login_again_if_different_user_or_shop
66
- if session[:user_session].present? && params[:session].present? # session data was sent/stored correctly
67
- clear_session = session[:user_session] != params[:session] # current user is different from stored user
68
-
69
- end
72
+ def add_top_level_redirection_headers(url: nil, ignore_response_code: false)
73
+ if request.xhr? && (ignore_response_code || response.code.to_i == 401)
74
+ # Make sure the shop is set in the redirection URL
75
+ unless params[:shop]
76
+ params[:shop] = if current_shopify_session
77
+ current_shopify_session.shop
78
+ elsif (matches = request.headers["HTTP_AUTHORIZATION"]&.match(/^Bearer (.+)$/))
79
+ jwt_payload = ShopifyAPI::Auth::JwtPayload.new(T.must(matches[1]))
80
+ jwt_payload.shop
81
+ end
82
+ end
70
83
 
71
- if current_shopify_session &&
72
- params[:shop] && params[:shop].is_a?(String) &&
73
- (current_shopify_session.domain != params[:shop])
74
- clear_session = true
75
- end
84
+ url ||= login_url_with_optional_shop
76
85
 
77
- if clear_session
78
- clear_shopify_session
79
- redirect_to_login
86
+ response.set_header("X-Shopify-API-Request-Failure-Reauthorize", "1")
87
+ response.set_header("X-Shopify-API-Request-Failure-Reauthorize-Url", url)
80
88
  end
81
89
  end
82
90
 
83
91
  protected
84
92
 
85
93
  def jwt_shopify_domain
86
- request.env['jwt.shopify_domain']
94
+ request.env["jwt.shopify_domain"]
87
95
  end
88
96
 
89
97
  def jwt_shopify_user_id
90
- request.env['jwt.shopify_user_id']
98
+ request.env["jwt.shopify_user_id"]
99
+ end
100
+
101
+ def host
102
+ params[:host]
91
103
  end
92
104
 
93
105
  def redirect_to_login
94
106
  if request.xhr?
107
+ add_top_level_redirection_headers(ignore_response_code: true)
95
108
  head(:unauthorized)
96
109
  else
97
110
  if request.get?
@@ -100,7 +113,8 @@ module ShopifyApp
100
113
  else
101
114
  referer = URI(request.referer || "/")
102
115
  path = referer.path
103
- query = "#{referer.query}&#{sanitized_params.to_query}"
116
+ query = Rack::Utils.parse_nested_query(referer.query)
117
+ query = query.merge(sanitized_params).to_query
104
118
  end
105
119
  session[:return_to] = query.blank? ? path.to_s : "#{path}?#{query}"
106
120
  redirect_to(login_url_with_optional_shop)
@@ -112,12 +126,16 @@ module ShopifyApp
112
126
  redirect_to(login_url_with_optional_shop)
113
127
  end
114
128
 
129
+ def handle_http_error(error)
130
+ if error.code == 401
131
+ close_session
132
+ else
133
+ raise error
134
+ end
135
+ end
136
+
115
137
  def clear_shopify_session
116
- session[:shop_id] = nil
117
- session[:user_id] = nil
118
- session[:shopify_domain] = nil
119
- session[:shopify_user] = nil
120
- session[:user_session] = nil
138
+ cookies.encrypted[ShopifyAPI::Auth::Oauth::SessionCookie::SESSION_COOKIE_NAME] = nil
121
139
  end
122
140
 
123
141
  def login_url_with_optional_shop(top_level: false)
@@ -145,65 +163,40 @@ module ShopifyApp
145
163
  query_params[:shop] ||= referer_sanitized_shop_name
146
164
  end
147
165
 
166
+ if params[:host].present?
167
+ query_params[:host] ||= host
168
+ end
169
+
148
170
  query_params[:top_level] = true if top_level
149
171
  query_params
150
172
  end
151
173
 
152
174
  def return_to_param_required?
153
- native_params = %i[shop hmac timestamp locale protocol return_to]
154
- request.path != '/' || sanitized_params.except(*native_params).any?
175
+ native_params = [:shop, :hmac, :timestamp, :locale, :protocol, :return_to]
176
+ request.path != "/" || sanitized_params.except(*native_params).any?
155
177
  end
156
178
 
157
179
  def fullpage_redirect_to(url)
158
180
  if ShopifyApp.configuration.embedded_app?
159
- render('shopify_app/shared/redirect', layout: false,
160
- locals: { url: url, current_shopify_domain: current_shopify_domain })
181
+ render("shopify_app/shared/redirect", layout: false,
182
+ locals: { url: url, current_shopify_domain: current_shopify_domain })
161
183
  else
162
184
  redirect_to(url)
163
185
  end
164
186
  end
165
187
 
166
188
  def current_shopify_domain
167
- shopify_domain = sanitized_shop_name ||
168
- jwt_shopify_domain ||
169
- session[:shopify_domain]
189
+ shopify_domain = sanitized_shop_name || current_shopify_session&.shop
170
190
 
171
191
  return shopify_domain if shopify_domain.present?
172
192
 
173
- raise ShopifyDomainNotFound
174
- end
175
-
176
- def sanitized_shop_name
177
- @sanitized_shop_name ||= sanitize_shop_param(params)
178
- end
179
-
180
- def referer_sanitized_shop_name
181
- return unless request.referer.present?
182
-
183
- @referer_sanitized_shop_name ||= begin
184
- referer_uri = URI(request.referer)
185
- query_params = Rack::Utils.parse_query(referer_uri.query)
186
-
187
- sanitize_shop_param(query_params.with_indifferent_access)
188
- end
189
- end
190
-
191
- def sanitize_shop_param(params)
192
- return unless params[:shop].present?
193
- ShopifyApp::Utils.sanitize_shop_domain(params[:shop])
194
- end
195
-
196
- def sanitized_params
197
- request.query_parameters.clone.tap do |query_params|
198
- if params[:shop].is_a?(String)
199
- query_params[:shop] = sanitize_shop_param(params)
200
- end
201
- end
193
+ raise ::ShopifyApp::ShopifyDomainNotFound
202
194
  end
203
195
 
204
196
  def return_address
205
- return base_return_address unless ShopifyApp.configuration.allow_jwt_authentication
206
- return_address_with_params(shop: current_shopify_domain)
197
+ return_address_with_params(shop: current_shopify_domain, host: host)
198
+ rescue ::ShopifyApp::ShopifyDomainNotFound, ::ShopifyApp::ShopifyHostNotFound
199
+ base_return_address
207
200
  end
208
201
 
209
202
  def base_return_address
@@ -219,5 +212,27 @@ module ShopifyApp
219
212
  .to_query
220
213
  uri.to_s
221
214
  end
215
+
216
+ private
217
+
218
+ def session_id_conflicts_with_params
219
+ shopify_session_id = current_shopify_session&.shopify_session_id
220
+ params[:session].present? && shopify_session_id.present? && params[:session] != shopify_session_id
221
+ end
222
+
223
+ def session_shop_conflicts_with_params
224
+ current_shopify_session && params[:shop].is_a?(String) && current_shopify_session.shop != params[:shop]
225
+ end
226
+
227
+ def shop_session
228
+ ShopifyApp::SessionRepository.retrieve_shop_session_by_shopify_domain(sanitize_shop_param(params))
229
+ end
230
+
231
+ def user_session_expected?
232
+ return false if shop_session.nil?
233
+ return false if ShopifyApp.configuration.shop_access_scopes_strategy.update_access_scopes?(shop_session.shop)
234
+
235
+ !ShopifyApp.configuration.user_session_repository.blank? && ShopifyApp::SessionRepository.user_storage.present?
236
+ end
222
237
  end
223
238
  end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ShopifyApp
4
+ module PayloadVerification
5
+ extend ActiveSupport::Concern
6
+
7
+ private
8
+
9
+ def shopify_hmac
10
+ request.headers["HTTP_X_SHOPIFY_HMAC_SHA256"]
11
+ end
12
+
13
+ def hmac_valid?(data)
14
+ secrets = [ShopifyApp.configuration.secret, ShopifyApp.configuration.old_secret].reject(&:blank?)
15
+
16
+ secrets.any? do |secret|
17
+ digest = OpenSSL::Digest.new("sha256")
18
+ ActiveSupport::SecurityUtils.secure_compare(
19
+ shopify_hmac,
20
+ Base64.strict_encode64(OpenSSL::HMAC.digest(digest, secret, data))
21
+ )
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ShopifyApp
4
+ module RedirectForEmbedded
5
+ include ShopifyApp::SanitizedParams
6
+
7
+ private
8
+
9
+ def embedded_redirect_url?
10
+ ShopifyApp.configuration.embedded_redirect_url.present?
11
+ end
12
+
13
+ def embedded_param?
14
+ embedded_redirect_url? && params[:embedded].present? && params[:embedded] == "1"
15
+ end
16
+
17
+ def redirect_for_embedded
18
+ # Don't actually redirect if we're already in the redirect route - we want the request to reach the FE
19
+ redirect_to(redirect_uri_for_embedded) unless request.path == ShopifyApp.configuration.embedded_redirect_url
20
+ end
21
+
22
+ def redirect_uri_for_embedded
23
+ redirect_query_params = {}
24
+ redirect_uri = "https://#{ShopifyAPI::Context.host_name}#{ShopifyApp.configuration.login_url}"
25
+ redirect_query_params[:shop] = sanitized_shop_name
26
+ redirect_query_params[:shop] ||= referer_sanitized_shop_name if referer_sanitized_shop_name.present?
27
+ redirect_query_params[:host] ||= params[:host] if params[:host].present?
28
+ redirect_uri = "#{redirect_uri}?#{redirect_query_params.to_query}" if redirect_query_params.present?
29
+
30
+ query_params = sanitized_params.except(:redirect_uri)
31
+ query_params[:redirectUri] = redirect_uri
32
+
33
+ "#{ShopifyApp.configuration.embedded_redirect_url}?#{query_params.to_query}"
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ShopifyApp
4
+ module SanitizedParams
5
+ protected
6
+
7
+ def sanitized_shop_name
8
+ @sanitized_shop_name ||= sanitize_shop_param(params)
9
+ end
10
+
11
+ def referer_sanitized_shop_name
12
+ return unless request.referer.present?
13
+
14
+ @referer_sanitized_shop_name ||= begin
15
+ referer_uri = URI(request.referer)
16
+ query_params = Rack::Utils.parse_query(referer_uri.query)
17
+
18
+ sanitize_shop_param(query_params.with_indifferent_access)
19
+ end
20
+ end
21
+
22
+ def sanitize_shop_param(params)
23
+ return unless params[:shop].present?
24
+ ShopifyApp::Utils.sanitize_shop_domain(params[:shop])
25
+ end
26
+
27
+ def sanitized_params
28
+ parameters = request.post? ? request.request_parameters : request.query_parameters
29
+ parameters.clone.tap do |params_copy|
30
+ if params[:shop].is_a?(String)
31
+ params_copy[:shop] = sanitize_shop_param(params)
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -1,7 +1,9 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  module ShopifyApp
3
4
  module WebhookVerification
4
5
  extend ActiveSupport::Concern
6
+ include ShopifyApp::PayloadVerification
5
7
 
6
8
  included do
7
9
  skip_before_action :verify_authenticity_token, raise: false
@@ -15,25 +17,8 @@ module ShopifyApp
15
17
  return head(:unauthorized) unless hmac_valid?(data)
16
18
  end
17
19
 
18
- def hmac_valid?(data)
19
- secrets = [ShopifyApp.configuration.secret, ShopifyApp.configuration.old_secret].reject(&:blank?)
20
-
21
- secrets.any? do |secret|
22
- digest = OpenSSL::Digest.new('sha256')
23
-
24
- ActiveSupport::SecurityUtils.secure_compare(
25
- shopify_hmac,
26
- Base64.strict_encode64(OpenSSL::HMAC.digest(digest, secret, data))
27
- )
28
- end
29
- end
30
-
31
20
  def shop_domain
32
- request.headers['HTTP_X_SHOPIFY_SHOP_DOMAIN']
33
- end
34
-
35
- def shopify_hmac
36
- request.headers['HTTP_X_SHOPIFY_HMAC_SHA256']
21
+ request.headers["HTTP_X_SHOPIFY_SHOP_DOMAIN"]
37
22
  end
38
23
  end
39
24
  end
@@ -1,24 +1,39 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  module ShopifyApp
4
+ module RedactJobParams
5
+ private
6
+
7
+ def args_info(job)
8
+ log_disabled_classes = ["ShopifyApp::ScripttagsManagerJob", "ShopifyApp::WebhooksManagerJob"]
9
+ return "" if log_disabled_classes.include?(job.class.name)
10
+
11
+ super
12
+ end
13
+ end
14
+
3
15
  class Engine < Rails::Engine
4
- engine_name 'shopify_app'
16
+ engine_name "shopify_app"
5
17
  isolate_namespace ShopifyApp
6
18
 
7
19
  initializer "shopify_app.assets.precompile" do |app|
8
- app.config.assets.precompile += %w[
9
- shopify_app/redirect.js
10
- shopify_app/top_level.js
11
- shopify_app/enable_cookies.js
12
- shopify_app/request_storage_access.js
13
- storage_access.svg
14
- ]
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",]
15
23
  end
16
24
 
17
25
  initializer "shopify_app.middleware" do |app|
18
- app.config.middleware.insert_after(::Rack::Runtime, ShopifyApp::SameSiteCookieMiddleware)
26
+ app.config.middleware.insert_after(::Rack::Runtime, ShopifyApp::JWTMiddleware)
27
+ end
19
28
 
20
- if ShopifyApp.configuration.allow_jwt_authentication
21
- app.config.middleware.insert_after(ShopifyApp::SameSiteCookieMiddleware, ShopifyApp::JWTMiddleware)
29
+ initializer "shopify_app.redact_job_params" do
30
+ ActiveSupport.on_load(:active_job) do
31
+ if ActiveJob::Base.respond_to?(:log_arguments?)
32
+ WebhooksManagerJob.log_arguments = false
33
+ ScripttagsManagerJob.log_arguments = false
34
+ elsif ActiveJob::Logging::LogSubscriber.private_method_defined?(:args_info)
35
+ ActiveJob::Logging::LogSubscriber.prepend(RedactJobParams)
36
+ end
22
37
  end
23
38
  end
24
39
  end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ShopifyApp
4
+ class BillingError < StandardError
5
+ attr_accessor :message
6
+ attr_accessor :errors
7
+
8
+ def initialize(message, errors)
9
+ super(message)
10
+ @message = message
11
+ @errors = errors
12
+ end
13
+ end
14
+
15
+ class ConfigurationError < StandardError; end
16
+
17
+ class CreationFailed < StandardError; end
18
+
19
+ class EnvironmentError < StandardError; end
20
+
21
+ class InvalidAudienceError < StandardError; end
22
+
23
+ class InvalidDestinationError < StandardError; end
24
+
25
+ class InvalidInput < StandardError; end
26
+
27
+ class MismatchedHostsError < StandardError; end
28
+
29
+ class MissingWebhookJobError < StandardError; end
30
+
31
+ class ShopifyDomainNotFound < StandardError; end
32
+
33
+ class ShopifyHostNotFound < StandardError; end
34
+ end
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  module ShopifyApp
3
4
  class ScripttagsManagerJob < ActiveJob::Base
4
5
  queue_as do
@@ -6,8 +7,7 @@ module ShopifyApp
6
7
  end
7
8
 
8
9
  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
10
+ ShopifyAPI::Auth::Session.temp(shop: shop_domain, access_token: shop_token) do
11
11
  manager = ScripttagsManager.new(scripttags, shop_domain)
12
12
  manager.create_scripttags
13
13
  end
@@ -1,15 +1,14 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  module ShopifyApp
3
4
  class WebhooksManagerJob < ActiveJob::Base
4
5
  queue_as do
5
6
  ShopifyApp.configuration.webhooks_manager_queue_name
6
7
  end
7
8
 
8
- def perform(shop_domain:, shop_token:, webhooks:)
9
- api_version = ShopifyApp.configuration.api_version
10
- ShopifyAPI::Session.temp(domain: shop_domain, token: shop_token, api_version: api_version) do
11
- manager = WebhooksManager.new(webhooks)
12
- manager.create_webhooks
9
+ def perform(shop_domain:, shop_token:, **)
10
+ ShopifyAPI::Auth::Session.temp(shop: shop_domain, access_token: shop_token) do |session|
11
+ WebhooksManager.create_webhooks(session: session)
13
12
  end
14
13
  end
15
14
  end
@@ -1,8 +1,7 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  module ShopifyApp
3
4
  class ScripttagsManager
4
- class CreationFailed < StandardError; end
5
-
6
5
  def self.queue(shop_domain, shop_token, scripttags)
7
6
  ShopifyApp::ScripttagsManagerJob.perform_later(
8
7
  shop_domain: shop_domain,
@@ -15,6 +14,7 @@ module ShopifyApp
15
14
  def self.build_src(scripttags, domain)
16
15
  scripttags.map do |tag|
17
16
  next tag unless tag[:src].respond_to?(:call)
17
+
18
18
  tag = tag.dup
19
19
  tag[:src] = tag[:src].call(domain)
20
20
  tag
@@ -44,7 +44,7 @@ module ShopifyApp
44
44
  def destroy_scripttags
45
45
  scripttags = expanded_scripttags
46
46
  ShopifyAPI::ScriptTag.all.each do |tag|
47
- ShopifyAPI::ScriptTag.delete(tag.id) if required_scripttag?(scripttags, tag)
47
+ tag.delete if required_scripttag?(scripttags, tag)
48
48
  end
49
49
 
50
50
  @current_scripttags = nil
@@ -61,9 +61,15 @@ module ShopifyApp
61
61
  end
62
62
 
63
63
  def create_scripttag(attributes)
64
- attributes.reverse_merge!(format: 'json')
65
- scripttag = ShopifyAPI::ScriptTag.create(attributes)
66
- raise CreationFailed, scripttag.errors.full_messages.to_sentence unless scripttag.persisted?
64
+ scripttag = ShopifyAPI::ScriptTag.new
65
+ attributes.each { |key, value| scripttag.public_send("#{key}=", value) }
66
+
67
+ begin
68
+ scripttag.save!
69
+ rescue ShopifyAPI::Errors::HttpResponseError => e
70
+ raise ::ShopifyApp::CreationFailed, e.message
71
+ end
72
+
67
73
  scripttag
68
74
  end
69
75