shopify_app 13.0.0 → 18.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (145) 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 +41 -0
  8. data/.github/workflows/release.yml +24 -0
  9. data/.github/workflows/rubocop.yml +22 -0
  10. data/.gitignore +0 -2
  11. data/.rubocop.yml +14 -6
  12. data/CHANGELOG.md +158 -0
  13. data/CONTRIBUTING.md +76 -0
  14. data/Gemfile +5 -0
  15. data/Gemfile.lock +268 -0
  16. data/README.md +73 -534
  17. data/Rakefile +1 -0
  18. data/SECURITY.md +59 -0
  19. data/app/assets/images/storage_access.svg +1 -2
  20. data/app/assets/javascripts/shopify_app/post_redirect.js +9 -0
  21. data/app/assets/javascripts/shopify_app/storage_access.js +2 -1
  22. data/app/assets/javascripts/shopify_app/top_level_interaction.js +1 -1
  23. data/app/controllers/concerns/shopify_app/authenticated.rb +1 -0
  24. data/app/controllers/concerns/shopify_app/ensure_authenticated_links.rb +26 -0
  25. data/app/controllers/concerns/shopify_app/require_known_shop.rb +39 -0
  26. data/app/controllers/concerns/shopify_app/shop_access_scopes_verification.rb +32 -0
  27. data/app/controllers/shopify_app/authenticated_controller.rb +1 -0
  28. data/app/controllers/shopify_app/callback_controller.rb +104 -18
  29. data/app/controllers/shopify_app/extension_verification_controller.rb +2 -7
  30. data/app/controllers/shopify_app/sessions_controller.rb +26 -10
  31. data/app/controllers/shopify_app/webhooks_controller.rb +6 -5
  32. data/app/views/shopify_app/partials/_button_styles.html.erb +41 -36
  33. data/app/views/shopify_app/partials/_card_styles.html.erb +3 -3
  34. data/app/views/shopify_app/partials/_empty_state_styles.html.erb +28 -59
  35. data/app/views/shopify_app/partials/_form_styles.html.erb +56 -0
  36. data/app/views/shopify_app/partials/_layout_styles.html.erb +16 -1
  37. data/app/views/shopify_app/partials/_typography_styles.html.erb +6 -6
  38. data/app/views/shopify_app/sessions/enable_cookies.html.erb +2 -7
  39. data/app/views/shopify_app/sessions/new.html.erb +38 -110
  40. data/app/views/shopify_app/sessions/request_storage_access.html.erb +1 -1
  41. data/app/views/shopify_app/sessions/top_level_interaction.html.erb +21 -22
  42. data/app/views/shopify_app/shared/post_redirect_to_auth_shopify.html.erb +13 -0
  43. data/config/locales/de.yml +11 -11
  44. data/config/locales/fi.yml +1 -1
  45. data/config/locales/nl.yml +8 -8
  46. data/config/locales/th.yml +4 -4
  47. data/config/locales/vi.yml +22 -0
  48. data/config/locales/zh-CN.yml +1 -1
  49. data/config/routes.rb +1 -0
  50. data/docs/Quickstart.md +15 -87
  51. data/docs/Releasing.md +18 -14
  52. data/docs/Troubleshooting.md +147 -4
  53. data/docs/Upgrading.md +126 -0
  54. data/docs/shopify_app/authentication.md +124 -0
  55. data/docs/shopify_app/engine.md +82 -0
  56. data/docs/shopify_app/generators.md +127 -0
  57. data/docs/shopify_app/handling-access-scopes-changes.md +14 -0
  58. data/docs/shopify_app/script-tags.md +28 -0
  59. data/docs/shopify_app/session-repository.md +88 -0
  60. data/docs/shopify_app/testing.md +38 -0
  61. data/docs/shopify_app/webhooks.md +72 -0
  62. data/karma.conf.js +1 -1
  63. data/lib/generators/shopify_app/add_after_authenticate_job/add_after_authenticate_job_generator.rb +5 -3
  64. data/lib/generators/shopify_app/add_after_authenticate_job/templates/after_authenticate_job.rb +1 -0
  65. data/lib/generators/shopify_app/add_marketing_activity_extension/add_marketing_activity_extension_generator.rb +2 -1
  66. data/lib/generators/shopify_app/add_marketing_activity_extension/templates/marketing_activities_controller.rb +4 -4
  67. data/lib/generators/shopify_app/add_webhook/add_webhook_generator.rb +5 -4
  68. data/lib/generators/shopify_app/add_webhook/templates/{webhook_job.rb → webhook_job.rb.tt} +5 -0
  69. data/lib/generators/shopify_app/app_proxy_controller/app_proxy_controller_generator.rb +4 -3
  70. data/lib/generators/shopify_app/app_proxy_controller/templates/app_proxy_controller.rb +3 -3
  71. data/lib/generators/shopify_app/app_proxy_controller/templates/app_proxy_route.rb +10 -9
  72. data/lib/generators/shopify_app/authenticated_controller/authenticated_controller_generator.rb +1 -1
  73. data/lib/generators/shopify_app/controllers/controllers_generator.rb +2 -1
  74. data/lib/generators/shopify_app/home_controller/home_controller_generator.rb +31 -3
  75. data/lib/generators/shopify_app/home_controller/templates/home_controller.rb +10 -0
  76. data/lib/generators/shopify_app/home_controller/templates/index.html.erb +66 -16
  77. data/lib/generators/shopify_app/home_controller/templates/unauthenticated_home_controller.rb +12 -0
  78. data/lib/generators/shopify_app/install/install_generator.rb +46 -11
  79. data/lib/generators/shopify_app/install/templates/embedded_app.html.erb +3 -2
  80. data/lib/generators/shopify_app/install/templates/flash_messages.js +0 -2
  81. data/lib/generators/shopify_app/install/templates/omniauth.rb +3 -1
  82. data/lib/generators/shopify_app/install/templates/shopify_app.js +1 -1
  83. data/lib/generators/shopify_app/install/templates/shopify_app.rb.tt +25 -0
  84. data/lib/generators/shopify_app/install/templates/shopify_provider.rb.tt +8 -0
  85. data/lib/generators/shopify_app/install/templates/user_agent.rb +2 -1
  86. data/lib/generators/shopify_app/products_controller/products_controller_generator.rb +19 -0
  87. data/lib/generators/shopify_app/products_controller/templates/products_controller.rb +8 -0
  88. data/lib/generators/shopify_app/routes/routes_generator.rb +1 -0
  89. data/lib/generators/shopify_app/routes/templates/routes.rb +10 -9
  90. data/lib/generators/shopify_app/shop_model/shop_model_generator.rb +39 -7
  91. data/lib/generators/shopify_app/shop_model/templates/db/migrate/add_shop_access_scopes_column.erb +5 -0
  92. data/lib/generators/shopify_app/shop_model/templates/shop.rb +2 -1
  93. data/lib/generators/shopify_app/shopify_app_generator.rb +4 -3
  94. data/lib/generators/shopify_app/user_model/templates/db/migrate/add_user_access_scopes_column.erb +5 -0
  95. data/lib/generators/shopify_app/user_model/templates/user.rb +2 -1
  96. data/lib/generators/shopify_app/user_model/user_model_generator.rb +39 -7
  97. data/lib/generators/shopify_app/views/views_generator.rb +2 -1
  98. data/lib/shopify_app/access_scopes/noop_strategy.rb +13 -0
  99. data/lib/shopify_app/access_scopes/shop_strategy.rb +24 -0
  100. data/lib/shopify_app/access_scopes/user_strategy.rb +41 -0
  101. data/lib/shopify_app/configuration.rb +40 -8
  102. data/lib/shopify_app/controller_concerns/app_proxy_verification.rb +3 -3
  103. data/lib/shopify_app/controller_concerns/csrf_protection.rb +15 -0
  104. data/lib/shopify_app/controller_concerns/embedded_app.rb +3 -2
  105. data/lib/shopify_app/controller_concerns/localization.rb +1 -0
  106. data/lib/shopify_app/controller_concerns/login_protection.rb +81 -16
  107. data/lib/shopify_app/controller_concerns/payload_verification.rb +24 -0
  108. data/lib/shopify_app/controller_concerns/webhook_verification.rb +3 -18
  109. data/lib/shopify_app/engine.rb +27 -0
  110. data/lib/shopify_app/jobs/scripttags_manager_job.rb +1 -1
  111. data/lib/shopify_app/jobs/webhooks_manager_job.rb +1 -1
  112. data/lib/shopify_app/managers/scripttags_manager.rb +4 -3
  113. data/lib/shopify_app/managers/webhooks_manager.rb +4 -3
  114. data/lib/shopify_app/middleware/jwt_middleware.rb +42 -0
  115. data/lib/shopify_app/middleware/same_site_cookie_middleware.rb +3 -2
  116. data/lib/shopify_app/omniauth/omniauth_configuration.rb +64 -0
  117. data/lib/shopify_app/session/in_memory_session_store.rb +7 -3
  118. data/lib/shopify_app/session/in_memory_shop_session_store.rb +12 -0
  119. data/lib/shopify_app/session/in_memory_user_session_store.rb +12 -0
  120. data/lib/shopify_app/session/jwt.rb +63 -0
  121. data/lib/shopify_app/session/null_user_session_store.rb +22 -0
  122. data/lib/shopify_app/session/session_repository.rb +13 -16
  123. data/lib/shopify_app/session/session_storage.rb +1 -0
  124. data/lib/shopify_app/session/shop_session_storage.rb +21 -9
  125. data/lib/shopify_app/session/shop_session_storage_with_scopes.rb +58 -0
  126. data/lib/shopify_app/session/user_session_storage.rb +19 -8
  127. data/lib/shopify_app/session/user_session_storage_with_scopes.rb +58 -0
  128. data/lib/shopify_app/test_helpers/all.rb +2 -0
  129. data/lib/shopify_app/test_helpers/webhook_verification_helper.rb +17 -0
  130. data/lib/shopify_app/utils.rb +18 -5
  131. data/lib/shopify_app/version.rb +2 -1
  132. data/lib/shopify_app.rb +23 -5
  133. data/package.json +7 -8
  134. data/service.yml +1 -4
  135. data/shopify_app.gemspec +15 -9
  136. data/translation.yml +1 -1
  137. data/yarn.lock +2140 -2188
  138. metadata +107 -19
  139. data/.github/ISSUE_TEMPLATE.md +0 -14
  140. data/.travis.yml +0 -27
  141. data/docs/install-on-dev-shop.png +0 -0
  142. data/docs/test-your-app.png +0 -0
  143. data/lib/generators/shopify_app/install/templates/shopify_app.rb +0 -15
  144. data/lib/generators/shopify_app/install/templates/shopify_provider.rb +0 -20
  145. data/package-lock.json +0 -7245
@@ -1,26 +1,28 @@
1
+ # frozen_string_literal: true
1
2
  module ShopifyApp
2
3
  class Configuration
3
-
4
4
  # Shopify App settings. These values should match the configuration
5
5
  # for the app in your Shopify Partners page. Change your settings in
6
6
  # `config/initializers/shopify_app.rb`
7
7
  attr_accessor :application_name
8
- attr_accessor :api_key
8
+ attr_accessor :api_key
9
9
  attr_accessor :secret
10
10
  attr_accessor :old_secret
11
11
  attr_accessor :scope
12
+ attr_writer :shop_access_scopes
13
+ attr_writer :user_access_scopes
12
14
  attr_accessor :embedded_app
13
15
  alias_method :embedded_app?, :embedded_app
14
16
  attr_accessor :webhooks
15
17
  attr_accessor :scripttags
16
18
  attr_accessor :after_authenticate_job
17
- attr_reader :shop_session_repository
18
- attr_reader :user_session_repository
19
19
  attr_accessor :api_version
20
20
 
21
+ attr_accessor :reauth_on_access_scope_changes
22
+
21
23
  # customise urls
22
24
  attr_accessor :root_url
23
- attr_accessor :login_url
25
+ attr_writer :login_url
24
26
 
25
27
  # customise ActiveJob queue names
26
28
  attr_accessor :scripttags_manager_queue_name
@@ -36,7 +38,12 @@ module ShopifyApp
36
38
  attr_accessor :webhook_jobs_namespace
37
39
 
38
40
  # allow enabling of same site none on cookies
39
- attr_accessor :enable_same_site_none
41
+ attr_writer :enable_same_site_none
42
+
43
+ # allow enabling jwt headers for authentication
44
+ attr_accessor :allow_jwt_authentication
45
+
46
+ attr_accessor :allow_cookie_authentication
40
47
 
41
48
  def initialize
42
49
  @root_url = '/'
@@ -44,6 +51,7 @@ module ShopifyApp
44
51
  @scripttags_manager_queue_name = Rails.application.config.active_job.queue_name
45
52
  @webhooks_manager_queue_name = Rails.application.config.active_job.queue_name
46
53
  @disable_webpacker = ENV['SHOPIFY_APP_DISABLE_WEBPACKER'].present?
54
+ @allow_cookie_authentication = true
47
55
  end
48
56
 
49
57
  def login_url
@@ -51,15 +59,31 @@ module ShopifyApp
51
59
  end
52
60
 
53
61
  def user_session_repository=(klass)
54
- @user_session_repository = klass
55
62
  ShopifyApp::SessionRepository.user_storage = klass
56
63
  end
57
64
 
65
+ def user_session_repository
66
+ ShopifyApp::SessionRepository.user_storage
67
+ end
68
+
58
69
  def shop_session_repository=(klass)
59
- @shop_session_repository = klass
60
70
  ShopifyApp::SessionRepository.shop_storage = klass
61
71
  end
62
72
 
73
+ def shop_session_repository
74
+ ShopifyApp::SessionRepository.shop_storage
75
+ end
76
+
77
+ def shop_access_scopes_strategy
78
+ return ShopifyApp::AccessScopes::NoopStrategy unless reauth_on_access_scope_changes
79
+ ShopifyApp::AccessScopes::ShopStrategy
80
+ end
81
+
82
+ def user_access_scopes_strategy
83
+ return ShopifyApp::AccessScopes::NoopStrategy unless reauth_on_access_scope_changes
84
+ ShopifyApp::AccessScopes::UserStrategy
85
+ end
86
+
63
87
  def has_webhooks?
64
88
  webhooks.present?
65
89
  end
@@ -71,6 +95,14 @@ module ShopifyApp
71
95
  def enable_same_site_none
72
96
  !Rails.env.test? && (@enable_same_site_none.nil? ? embedded_app? : @enable_same_site_none)
73
97
  end
98
+
99
+ def shop_access_scopes
100
+ @shop_access_scopes || scope
101
+ end
102
+
103
+ def user_access_scopes
104
+ @user_access_scopes || scope
105
+ end
74
106
  end
75
107
 
76
108
  def self.configuration
@@ -1,14 +1,14 @@
1
+ # frozen_string_literal: true
1
2
  module ShopifyApp
2
3
  module AppProxyVerification
3
4
  extend ActiveSupport::Concern
4
-
5
5
  included do
6
6
  skip_before_action :verify_authenticity_token, raise: false
7
7
  before_action :verify_proxy_request
8
8
  end
9
9
 
10
10
  def verify_proxy_request
11
- return head :forbidden unless query_string_valid?(request.query_string)
11
+ return head(:forbidden) unless query_string_valid?(request.query_string)
12
12
  end
13
13
 
14
14
  private
@@ -26,7 +26,7 @@ module ShopifyApp
26
26
  end
27
27
 
28
28
  def calculated_signature(query_hash_without_signature)
29
- sorted_params = query_hash_without_signature.collect{|k,v| "#{k}=#{Array(v).join(',')}"}.sort.join
29
+ sorted_params = query_hash_without_signature.collect { |k, v| "#{k}=#{Array(v).join(',')}" }.sort.join
30
30
 
31
31
  OpenSSL::HMAC.hexdigest(
32
32
  OpenSSL::Digest.new('sha256'),
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+ module ShopifyApp
3
+ module CsrfProtection
4
+ extend ActiveSupport::Concern
5
+ included do
6
+ protect_from_forgery with: :exception, unless: :valid_session_token?
7
+ end
8
+
9
+ private
10
+
11
+ def valid_session_token?
12
+ request.env['jwt.shopify_domain']
13
+ end
14
+ end
15
+ end
@@ -1,11 +1,12 @@
1
+ # frozen_string_literal: true
1
2
  module ShopifyApp
2
3
  module EmbeddedApp
3
4
  extend ActiveSupport::Concern
4
5
 
5
6
  included do
6
7
  if ShopifyApp.configuration.embedded_app?
7
- after_action :set_esdk_headers
8
- layout 'embedded_app'
8
+ after_action(:set_esdk_headers)
9
+ layout('embedded_app')
9
10
  end
10
11
  end
11
12
 
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module ShopifyApp
2
3
  module Localization
3
4
  extend ActiveSupport::Concern
@@ -9,13 +9,23 @@ module ShopifyApp
9
9
 
10
10
  class ShopifyDomainNotFound < StandardError; end
11
11
 
12
+ class ShopifyHostNotFound < StandardError; end
13
+
12
14
  included do
13
15
  after_action :set_test_cookie
14
- rescue_from ActiveResource::UnauthorizedAccess, :with => :close_session
16
+ rescue_from ActiveResource::UnauthorizedAccess, with: :close_session
15
17
  end
16
18
 
19
+ ACCESS_TOKEN_REQUIRED_HEADER = 'X-Shopify-API-Request-Failure-Unauthorized'
20
+
17
21
  def activate_shopify_session
22
+ if user_session_expected? && user_session.blank?
23
+ signal_access_token_required
24
+ return redirect_to_login
25
+ end
26
+
18
27
  return redirect_to_login if current_shopify_session.blank?
28
+
19
29
  clear_top_level_oauth_cookie
20
30
 
21
31
  begin
@@ -27,30 +37,51 @@ module ShopifyApp
27
37
  end
28
38
 
29
39
  def current_shopify_session
30
- if session[:user_id].present?
31
- @current_shopify_session ||= user_session
32
- else
33
- @current_shopify_session ||= shop_session
40
+ @current_shopify_session ||= begin
41
+ user_session || shop_session
34
42
  end
35
43
  end
36
44
 
37
45
  def user_session
38
- return if session[:user_id].blank?
46
+ user_session_by_jwt || user_session_by_cookie
47
+ end
48
+
49
+ def user_session_by_jwt
50
+ return unless ShopifyApp.configuration.allow_jwt_authentication
51
+ return unless jwt_shopify_user_id
52
+ ShopifyApp::SessionRepository.retrieve_user_session_by_shopify_user_id(jwt_shopify_user_id)
53
+ end
54
+
55
+ def user_session_by_cookie
56
+ return unless ShopifyApp.configuration.allow_cookie_authentication
57
+ return unless session[:user_id].present?
39
58
  ShopifyApp::SessionRepository.retrieve_user_session(session[:user_id])
40
59
  end
41
60
 
42
61
  def shop_session
43
- return if session[:shop_id].blank?
62
+ shop_session_by_jwt || shop_session_by_cookie
63
+ end
64
+
65
+ def shop_session_by_jwt
66
+ return unless ShopifyApp.configuration.allow_jwt_authentication
67
+ return unless jwt_shopify_domain
68
+ ShopifyApp::SessionRepository.retrieve_shop_session_by_shopify_domain(jwt_shopify_domain)
69
+ end
70
+
71
+ def shop_session_by_cookie
72
+ return unless ShopifyApp.configuration.allow_cookie_authentication
73
+ return unless session[:shop_id].present?
44
74
  ShopifyApp::SessionRepository.retrieve_shop_session(session[:shop_id])
45
75
  end
46
76
 
47
77
  def login_again_if_different_user_or_shop
48
78
  if session[:user_session].present? && params[:session].present? # session data was sent/stored correctly
49
79
  clear_session = session[:user_session] != params[:session] # current user is different from stored user
50
-
51
80
  end
52
81
 
53
- if current_shopify_session && params[:shop] && params[:shop].is_a?(String) && (current_shopify_session.domain != params[:shop])
82
+ if current_shopify_session &&
83
+ params[:shop] && params[:shop].is_a?(String) &&
84
+ (current_shopify_session.domain != params[:shop])
54
85
  clear_session = true
55
86
  end
56
87
 
@@ -60,11 +91,29 @@ module ShopifyApp
60
91
  end
61
92
  end
62
93
 
94
+ def signal_access_token_required
95
+ response.set_header(ACCESS_TOKEN_REQUIRED_HEADER, "true")
96
+ end
97
+
63
98
  protected
64
99
 
100
+ def jwt_shopify_domain
101
+ request.env['jwt.shopify_domain']
102
+ end
103
+
104
+ def jwt_shopify_user_id
105
+ request.env['jwt.shopify_user_id']
106
+ end
107
+
108
+ def host
109
+ return params[:host] if params[:host].present?
110
+
111
+ raise ShopifyHostNotFound
112
+ end
113
+
65
114
  def redirect_to_login
66
115
  if request.xhr?
67
- head :unauthorized
116
+ head(:unauthorized)
68
117
  else
69
118
  if request.get?
70
119
  path = request.path
@@ -74,7 +123,7 @@ module ShopifyApp
74
123
  path = referer.path
75
124
  query = "#{referer.query}&#{sanitized_params.to_query}"
76
125
  end
77
- session[:return_to] = "#{path}?#{query}"
126
+ session[:return_to] = query.blank? ? path.to_s : "#{path}?#{query}"
78
127
  redirect_to(login_url_with_optional_shop)
79
128
  end
80
129
  end
@@ -105,7 +154,7 @@ module ShopifyApp
105
154
  query_params = {}
106
155
  query_params[:shop] = sanitized_params[:shop] if params[:shop].present?
107
156
 
108
- return_to = session[:return_to] || params[:return_to]
157
+ return_to = RedirectSafely.make_safe(session[:return_to] || params[:return_to], nil)
109
158
 
110
159
  if return_to.present? && return_to_param_required?
111
160
  query_params[:return_to] = return_to
@@ -128,14 +177,18 @@ module ShopifyApp
128
177
 
129
178
  def fullpage_redirect_to(url)
130
179
  if ShopifyApp.configuration.embedded_app?
131
- render 'shopify_app/shared/redirect', layout: false, locals: { url: url, current_shopify_domain: current_shopify_domain }
180
+ render('shopify_app/shared/redirect', layout: false,
181
+ locals: { url: url, current_shopify_domain: current_shopify_domain })
132
182
  else
133
- redirect_to url
183
+ redirect_to(url)
134
184
  end
135
185
  end
136
186
 
137
187
  def current_shopify_domain
138
- shopify_domain = sanitized_shop_name || session[:shopify_domain]
188
+ shopify_domain = sanitized_shop_name ||
189
+ jwt_shopify_domain ||
190
+ session[:shopify_domain]
191
+
139
192
  return shopify_domain if shopify_domain.present?
140
193
 
141
194
  raise ShopifyDomainNotFound
@@ -170,11 +223,17 @@ module ShopifyApp
170
223
  end
171
224
 
172
225
  def return_address
226
+ return_address_with_params(shop: current_shopify_domain, host: host)
227
+ rescue ShopifyDomainNotFound, ShopifyHostNotFound
228
+ base_return_address
229
+ end
230
+
231
+ def base_return_address
173
232
  session.delete(:return_to) || ShopifyApp.configuration.root_url
174
233
  end
175
234
 
176
235
  def return_address_with_params(params)
177
- uri = URI(return_address)
236
+ uri = URI(base_return_address)
178
237
  uri.query = CGI.parse(uri.query.to_s)
179
238
  .symbolize_keys
180
239
  .transform_values { |v| v.one? ? v.first : v }
@@ -182,5 +241,11 @@ module ShopifyApp
182
241
  .to_query
183
242
  uri.to_s
184
243
  end
244
+
245
+ private
246
+
247
+ def user_session_expected?
248
+ !ShopifyApp.configuration.user_session_repository.blank? && ShopifyApp::SessionRepository.user_storage.present?
249
+ end
185
250
  end
186
251
  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
@@ -1,6 +1,8 @@
1
+ # frozen_string_literal: true
1
2
  module ShopifyApp
2
3
  module WebhookVerification
3
4
  extend ActiveSupport::Concern
5
+ include ShopifyApp::PayloadVerification
4
6
 
5
7
  included do
6
8
  skip_before_action :verify_authenticity_token, raise: false
@@ -11,28 +13,11 @@ module ShopifyApp
11
13
 
12
14
  def verify_request
13
15
  data = request.raw_post
14
- return head :unauthorized unless hmac_valid?(data)
15
- end
16
-
17
- def hmac_valid?(data)
18
- secrets = [ShopifyApp.configuration.secret, ShopifyApp.configuration.old_secret].reject(&:blank?)
19
-
20
- secrets.any? do |secret|
21
- digest = OpenSSL::Digest.new('sha256')
22
-
23
- ActiveSupport::SecurityUtils.secure_compare(
24
- shopify_hmac,
25
- Base64.strict_encode64(OpenSSL::HMAC.digest(digest, secret, data))
26
- )
27
- end
16
+ return head(:unauthorized) unless hmac_valid?(data)
28
17
  end
29
18
 
30
19
  def shop_domain
31
20
  request.headers['HTTP_X_SHOPIFY_SHOP_DOMAIN']
32
21
  end
33
-
34
- def shopify_hmac
35
- request.headers['HTTP_X_SHOPIFY_HMAC_SHA256']
36
- end
37
22
  end
38
23
  end
@@ -1,4 +1,15 @@
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
@@ -6,6 +17,7 @@ module ShopifyApp
6
17
  initializer "shopify_app.assets.precompile" do |app|
7
18
  app.config.assets.precompile += %w[
8
19
  shopify_app/redirect.js
20
+ shopify_app/post_redirect.js
9
21
  shopify_app/top_level.js
10
22
  shopify_app/enable_cookies.js
11
23
  shopify_app/request_storage_access.js
@@ -15,6 +27,21 @@ module ShopifyApp
15
27
 
16
28
  initializer "shopify_app.middleware" do |app|
17
29
  app.config.middleware.insert_after(::Rack::Runtime, ShopifyApp::SameSiteCookieMiddleware)
30
+
31
+ if ShopifyApp.configuration.allow_jwt_authentication
32
+ app.config.middleware.insert_after(ShopifyApp::SameSiteCookieMiddleware, ShopifyApp::JWTMiddleware)
33
+ end
34
+ end
35
+
36
+ initializer "shopify_app.redact_job_params" do
37
+ ActiveSupport.on_load(:active_job) do
38
+ if ActiveJob::Base.respond_to?(:log_arguments?)
39
+ WebhooksManagerJob.log_arguments = false
40
+ ScripttagsManagerJob.log_arguments = false
41
+ elsif ActiveJob::Logging::LogSubscriber.private_method_defined?(:args_info)
42
+ ActiveJob::Logging::LogSubscriber.prepend(RedactJobParams)
43
+ end
44
+ end
18
45
  end
19
46
  end
20
47
  end