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,4 +1,7 @@
1
- (function() {
1
+ //= require ./app_bridge_redirect.js
2
+ //= require ./app_bridge_utils_3.1.1.js
3
+
4
+ (function () {
2
5
  function redirect() {
3
6
  var redirectTargetElement = document.getElementById("redirection-target");
4
7
 
@@ -6,21 +9,14 @@
6
9
  return;
7
10
  }
8
11
 
9
- var targetInfo = JSON.parse(redirectTargetElement.dataset.target)
12
+ var targetInfo = JSON.parse(redirectTargetElement.dataset.target);
10
13
 
11
- if (window.top == window.self) {
12
- // If the current window is the 'parent', change the URL by setting location.href
13
- window.top.location.href = targetInfo.url;
14
- } else {
15
- // If the current window is the 'child', change the parent's URL with postMessage
16
- normalizedLink = document.createElement('a');
17
- normalizedLink.href = targetInfo.url;
14
+ var appBridgeUtils = window['app-bridge-utils'];
18
15
 
19
- data = JSON.stringify({
20
- message: 'Shopify.API.remoteRedirect',
21
- data: {location: normalizedLink.href}
22
- });
23
- window.parent.postMessage(data, targetInfo.myshopifyUrl);
16
+ if (appBridgeUtils.isShopifyEmbedded()) {
17
+ window.appBridgeRedirect(targetInfo.url);
18
+ } else {
19
+ window.top.location.href = targetInfo.url;
24
20
  }
25
21
  }
26
22
 
@@ -1,3 +1,5 @@
1
+ //= require ./app_bridge_redirect.js
2
+
1
3
  (function() {
2
4
  var ACCESS_GRANTED_STATUS = 'storage_access_granted';
3
5
  var ACCESS_DENIED_STATUS = 'storage_access_denied';
@@ -13,15 +15,7 @@
13
15
  StorageAccessHelper.prototype.redirectToAppTLD = function(storageAccessStatus) {
14
16
  var normalizedLink = document.createElement('a');
15
17
 
16
- normalizedLink.href = this.setNormalizedLink(storageAccessStatus);
17
-
18
- data = JSON.stringify({
19
- message: 'Shopify.API.remoteRedirect',
20
- data: {
21
- location: normalizedLink.href,
22
- }
23
- });
24
- window.parent.postMessage(data, this.redirectData.myshopifyUrl);
18
+ window.appBridgeRedirect(this.setNormalizedLink(storageAccessStatus));
25
19
  }
26
20
 
27
21
  StorageAccessHelper.prototype.redirectToAppsIndex = function() {
@@ -132,7 +126,8 @@
132
126
 
133
127
  /* ITP 2.0 solution: handles cookie partitioning */
134
128
  StorageAccessHelper.prototype.setUpHelper = function() {
135
- return new ITPHelper({redirectUrl: window.shopOrigin + "/admin/apps/" + window.apiKey + window.returnTo});
129
+ var shopifyData = document.body.dataset;
130
+ return new ITPHelper({redirectUrl: "https://" + shopifyData.shopOrigin + "/admin/apps/" + shopifyData.apiKey + shopifyData.returnTo});
136
131
  }
137
132
 
138
133
  StorageAccessHelper.prototype.setCookieAndRedirect = function() {
@@ -1,7 +1,7 @@
1
1
  (function() {
2
2
  function setUpTopLevelInteraction() {
3
3
  var TopLevelInteraction = new ITPHelper({
4
- redirectUrl: window.redirectUrl,
4
+ redirectUrl: document.body.dataset.redirectUrl,
5
5
  });
6
6
 
7
7
  TopLevelInteraction.execute();
@@ -7,9 +7,13 @@ module ShopifyApp
7
7
  included do
8
8
  include ShopifyApp::Localization
9
9
  include ShopifyApp::LoginProtection
10
+ include ShopifyApp::CsrfProtection
10
11
  include ShopifyApp::EmbeddedApp
12
+ include ShopifyApp::EnsureBilling
13
+
11
14
  before_action :login_again_if_different_user_or_shop
12
15
  around_action :activate_shopify_session
16
+ after_action :add_top_level_redirection_headers
13
17
  end
14
18
  end
15
19
  end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ShopifyApp
4
+ module EnsureAuthenticatedLinks
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ before_action :redirect_to_splash_page, if: :missing_expected_jwt?
9
+ end
10
+
11
+ private
12
+
13
+ def splash_page
14
+ splash_page_with_params(
15
+ return_to: request.fullpath,
16
+ shop: current_shopify_domain,
17
+ host: params[:host]
18
+ )
19
+ end
20
+
21
+ def splash_page_with_params(params)
22
+ uri = URI(root_path)
23
+ uri.query = params.compact.to_query
24
+ uri.to_s
25
+ end
26
+
27
+ def redirect_to_splash_page
28
+ redirect_to(splash_page)
29
+ rescue ::ShopifyApp::ShopifyDomainNotFound => error
30
+ Rails.logger.warn("[ShopifyApp::EnsureAuthenticatedLinks] Redirecting to login: [#{error.class}] "\
31
+ "Could not determine current shop domain")
32
+ redirect_to(ShopifyApp.configuration.login_url)
33
+ end
34
+
35
+ def missing_expected_jwt?
36
+ jwt_shopify_domain.blank?
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ShopifyApp
4
+ module RequireKnownShop
5
+ extend ActiveSupport::Concern
6
+ include ShopifyApp::RedirectForEmbedded
7
+
8
+ included do
9
+ before_action :check_shop_domain
10
+ before_action :check_shop_known
11
+ end
12
+
13
+ def current_shopify_domain
14
+ return if params[:shop].blank?
15
+
16
+ @shopify_domain ||= ShopifyApp::Utils.sanitize_shop_domain(params[:shop])
17
+ end
18
+
19
+ private
20
+
21
+ def check_shop_domain
22
+ redirect_to(ShopifyApp.configuration.login_url) unless current_shopify_domain
23
+ end
24
+
25
+ def check_shop_known
26
+ @shop = SessionRepository.retrieve_shop_session_by_shopify_domain(current_shopify_domain)
27
+ unless @shop
28
+ if embedded_param?
29
+ redirect_for_embedded
30
+ else
31
+ redirect_to(shop_login)
32
+ end
33
+ end
34
+ end
35
+
36
+ def shop_login
37
+ url = URI(ShopifyApp.configuration.login_url)
38
+
39
+ url.query = URI.encode_www_form(
40
+ shop: params[:shop],
41
+ host: params[:host],
42
+ return_to: request.fullpath,
43
+ )
44
+
45
+ url.to_s
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ShopifyApp
4
+ module ShopAccessScopesVerification
5
+ extend ActiveSupport::Concern
6
+ include ShopifyApp::RedirectForEmbedded
7
+
8
+ included do
9
+ before_action :login_on_scope_changes
10
+ end
11
+
12
+ protected
13
+
14
+ def login_on_scope_changes
15
+ if scopes_mismatch?
16
+ if embedded_param?
17
+ redirect_for_embedded
18
+ else
19
+ redirect_to(shop_login)
20
+ end
21
+ end
22
+ end
23
+
24
+ private
25
+
26
+ def scopes_mismatch?
27
+ ShopifyApp.configuration.shop_access_scopes_strategy.update_access_scopes?(current_shopify_domain)
28
+ end
29
+
30
+ def current_shopify_domain
31
+ return if params[:shop].blank?
32
+
33
+ ShopifyApp::Utils.sanitize_shop_domain(params[:shop])
34
+ end
35
+
36
+ def shop_login
37
+ ShopifyApp::Utils.shop_login_url(shop: params[:shop], host: params[:host], return_to: request.fullpath)
38
+ end
39
+ end
40
+ end
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  module ShopifyApp
3
4
  class AuthenticatedController < ActionController::Base
4
5
  include ShopifyApp::Authenticated
@@ -4,123 +4,102 @@ module ShopifyApp
4
4
  # Performs login after OAuth completes
5
5
  class CallbackController < ActionController::Base
6
6
  include ShopifyApp::LoginProtection
7
+ include ShopifyApp::EnsureBilling
7
8
 
8
9
  def callback
9
- unless auth_hash
10
+ begin
11
+ filtered_params = request.parameters.symbolize_keys.slice(:code, :shop, :timestamp, :state, :host, :hmac)
12
+
13
+ auth_result = ShopifyAPI::Auth::Oauth.validate_auth_callback(
14
+ cookies: {
15
+ ShopifyAPI::Auth::Oauth::SessionCookie::SESSION_COOKIE_NAME =>
16
+ cookies.encrypted[ShopifyAPI::Auth::Oauth::SessionCookie::SESSION_COOKIE_NAME],
17
+ },
18
+ auth_query: ShopifyAPI::Auth::Oauth::AuthQuery.new(**filtered_params)
19
+ )
20
+ rescue
10
21
  return respond_with_error
11
22
  end
12
23
 
13
- if jwt_request? && !valid_jwt_auth?
14
- return respond_with_error
15
- end
24
+ cookies.encrypted[auth_result[:cookie].name] = {
25
+ expires: auth_result[:cookie].expires,
26
+ secure: true,
27
+ http_only: true,
28
+ value: auth_result[:cookie].value,
29
+ }
16
30
 
17
- if jwt_request?
18
- set_shopify_session
19
- head(:ok)
20
- else
21
- reset_session_options
22
- set_shopify_session
31
+ session[:shopify_user_id] = auth_result[:session].associated_user.id if auth_result[:session].online?
23
32
 
24
- if redirect_for_user_token?
25
- return redirect_to(login_url_with_optional_shop)
26
- end
33
+ if start_user_token_flow?(auth_result[:session])
34
+ return respond_with_user_token_flow
35
+ end
27
36
 
28
- install_webhooks
29
- install_scripttags
30
- perform_after_authenticate_job
37
+ perform_post_authenticate_jobs(auth_result[:session])
38
+ has_payment = check_billing(auth_result[:session])
31
39
 
32
- redirect_to(return_address)
33
- end
40
+ respond_successfully if has_payment
34
41
  end
35
42
 
36
43
  private
37
44
 
38
- def respond_with_error
39
- if jwt_request?
40
- head(:unauthorized)
45
+ def respond_successfully
46
+ if ShopifyAPI::Context.embedded?
47
+ return_to = session.delete(:return_to) || ""
48
+ redirect_to(ShopifyAPI::Auth.embedded_app_url(params[:host]) + return_to, allow_other_host: true)
41
49
  else
42
- flash[:error] = I18n.t('could_not_log_in')
43
- redirect_to(login_url_with_optional_shop)
50
+ redirect_to(return_address)
44
51
  end
45
52
  end
46
53
 
47
- def redirect_for_user_token?
48
- ShopifyApp::SessionRepository.user_storage.present? && user_session.blank?
49
- end
50
-
51
- def jwt_request?
52
- jwt_shopify_domain || jwt_shopify_user_id
53
- end
54
-
55
- def valid_jwt_auth?
56
- auth_hash && jwt_shopify_domain == shop_name && jwt_shopify_user_id == associated_user_id
57
- end
58
-
59
- def auth_hash
60
- request.env['omniauth.auth']
54
+ def respond_with_error
55
+ flash[:error] = I18n.t("could_not_log_in")
56
+ redirect_to(login_url_with_optional_shop)
61
57
  end
62
58
 
63
- def shop_name
64
- auth_hash.uid
59
+ def respond_with_user_token_flow
60
+ redirect_to(login_url_with_optional_shop)
65
61
  end
66
62
 
67
- def associated_user
68
- return unless auth_hash['extra'].present?
63
+ def start_user_token_flow?(shopify_session)
64
+ return false unless ShopifyApp::SessionRepository.user_storage.present?
65
+ return false if shopify_session.online?
69
66
 
70
- auth_hash['extra']['associated_user']
67
+ update_user_access_scopes?
71
68
  end
72
69
 
73
- def associated_user_id
74
- associated_user && associated_user['id']
75
- end
70
+ def update_user_access_scopes?
71
+ return true if session[:shopify_user_id].nil?
76
72
 
77
- def token
78
- auth_hash['credentials']['token']
73
+ user_access_scopes_strategy.update_access_scopes?(shopify_user_id: session[:shopify_user_id])
79
74
  end
80
75
 
81
- def reset_session_options
82
- request.session_options[:renew] = true
83
- session.delete(:_csrf_token)
76
+ def user_access_scopes_strategy
77
+ ShopifyApp.configuration.user_access_scopes_strategy
84
78
  end
85
79
 
86
- def set_shopify_session
87
- session_store = ShopifyAPI::Session.new(
88
- domain: shop_name,
89
- token: token,
90
- api_version: ShopifyApp.configuration.api_version
91
- )
92
-
93
- session[:shopify_user] = associated_user
94
- if session[:shopify_user].present?
95
- session[:user_id] = ShopifyApp::SessionRepository.store_user_session(session_store, associated_user)
96
- else
97
- session[:shop_id] = ShopifyApp::SessionRepository.store_shop_session(session_store)
98
- end
99
- session[:shopify_domain] = shop_name
100
- session[:user_session] = auth_hash&.extra&.session
80
+ def perform_post_authenticate_jobs(session)
81
+ install_webhooks(session)
82
+ install_scripttags(session)
83
+ perform_after_authenticate_job(session)
101
84
  end
102
85
 
103
- def install_webhooks
86
+ def install_webhooks(session)
104
87
  return unless ShopifyApp.configuration.has_webhooks?
105
88
 
106
- WebhooksManager.queue(
107
- shop_name,
108
- shop_session&.token || user_session.token,
109
- ShopifyApp.configuration.webhooks
110
- )
89
+ WebhooksManager.queue(session.shop, session.access_token)
111
90
  end
112
91
 
113
- def install_scripttags
92
+ def install_scripttags(session)
114
93
  return unless ShopifyApp.configuration.has_scripttags?
115
94
 
116
95
  ScripttagsManager.queue(
117
- shop_name,
118
- shop_session&.token || user_session.token,
96
+ session.shop,
97
+ session.access_token,
119
98
  ShopifyApp.configuration.scripttags
120
99
  )
121
100
  end
122
101
 
123
- def perform_after_authenticate_job
102
+ def perform_after_authenticate_job(session)
124
103
  config = ShopifyApp.configuration.after_authenticate_job
125
104
 
126
105
  return unless config && config[:job].present?
@@ -129,9 +108,9 @@ module ShopifyApp
129
108
  job = job.constantize if job.is_a?(String)
130
109
 
131
110
  if config[:inline] == true
132
- job.perform_now(shop_domain: session[:shopify_domain])
111
+ job.perform_now(shop_domain: session.shop)
133
112
  else
134
- job.perform_later(shop_domain: session[:shopify_domain])
113
+ job.perform_later(shop_domain: session.shop)
135
114
  end
136
115
  end
137
116
  end
@@ -2,19 +2,14 @@
2
2
 
3
3
  module ShopifyApp
4
4
  class ExtensionVerificationController < ActionController::Base
5
+ include ShopifyApp::PayloadVerification
5
6
  protect_from_forgery with: :null_session
6
7
  before_action :verify_request
7
8
 
8
9
  private
9
10
 
10
11
  def verify_request
11
- hmac_header = request.headers['HTTP_X_SHOPIFY_HMAC_SHA256']
12
- request_body = request.body.read
13
- secret = ShopifyApp.configuration.secret
14
- digest = OpenSSL::Digest.new('sha256')
15
-
16
- expected_hmac = Base64.strict_encode64(OpenSSL::HMAC.digest(digest, secret, request_body))
17
- head(:unauthorized) unless ActiveSupport::SecurityUtils.secure_compare(expected_hmac, hmac_header)
12
+ head(:unauthorized) unless hmac_valid?(request.body.read)
18
13
  end
19
14
  end
20
15
  end
@@ -1,12 +1,14 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  module ShopifyApp
3
4
  class SessionsController < ActionController::Base
4
5
  include ShopifyApp::LoginProtection
6
+ include ShopifyApp::RedirectForEmbedded
5
7
 
6
8
  layout false, only: :new
7
9
 
8
10
  after_action only: [:new, :create] do |controller|
9
- controller.response.headers.except!('X-Frame-Options')
11
+ controller.response.headers.except!("X-Frame-Options")
10
12
  end
11
13
 
12
14
  def new
@@ -17,41 +19,14 @@ module ShopifyApp
17
19
  authenticate
18
20
  end
19
21
 
20
- def enable_cookies
21
- return unless validate_shop_presence
22
-
23
- render(:enable_cookies, layout: false, locals: {
24
- does_not_have_storage_access_url: top_level_interaction_path(
25
- shop: sanitized_shop_name,
26
- return_to: params[:return_to]
27
- ),
28
- has_storage_access_url: login_url_with_optional_shop(top_level: true),
29
- app_target_url: granted_storage_access_path(
30
- shop: sanitized_shop_name,
31
- return_to: params[:return_to]
32
- ),
33
- current_shopify_domain: current_shopify_domain,
34
- })
35
- end
36
-
37
22
  def top_level_interaction
38
23
  @url = login_url_with_optional_shop(top_level: true)
39
24
  validate_shop_presence
40
25
  end
41
26
 
42
- def granted_storage_access
43
- return unless validate_shop_presence
44
-
45
- session['shopify.granted_storage_access'] = true
46
-
47
- copy_return_to_param_to_session
48
-
49
- redirect_to(return_address_with_params({ shop: @shop }))
50
- end
51
-
52
27
  def destroy
53
28
  reset_session
54
- flash[:notice] = I18n.t('.logged_out')
29
+ flash[:notice] = I18n.t(".logged_out")
55
30
  redirect_to(login_url_with_optional_shop)
56
31
  end
57
32
 
@@ -59,60 +34,39 @@ module ShopifyApp
59
34
 
60
35
  def authenticate
61
36
  return render_invalid_shop_error unless sanitized_shop_name.present?
62
- session['shopify.omniauth_params'] = { shop: sanitized_shop_name }
63
37
 
64
38
  copy_return_to_param_to_session
65
39
 
66
- set_user_tokens_option
67
-
68
- if user_agent_can_partition_cookies
69
- authenticate_with_partitioning
70
- else
71
- authenticate_normally
72
- end
73
- end
74
-
75
- def authenticate_normally
76
- if request_storage_access?
77
- redirect_to_request_storage_access
78
- elsif authenticate_in_context?
79
- authenticate_in_context
80
- else
81
- authenticate_at_top_level
82
- end
83
- end
84
-
85
- def authenticate_with_partitioning
86
- if session['shopify.cookies_persist']
87
- clear_top_level_oauth_cookie
88
- authenticate_in_context
40
+ if embedded_redirect_url?
41
+ if embedded_param?
42
+ redirect_for_embedded
43
+ else
44
+ start_oauth
45
+ end
46
+ elsif top_level?
47
+ start_oauth
89
48
  else
90
- set_top_level_oauth_cookie
91
- enable_cookie_access
49
+ redirect_auth_to_top_level
92
50
  end
93
51
  end
94
52
 
95
- # rubocop:disable Lint/SuppressedException
96
- def set_user_tokens_option
97
- if shop_session.blank?
98
- session[:user_tokens] = false
99
- return
100
- end
53
+ def start_oauth
54
+ callback_url = ShopifyApp.configuration.login_callback_url.gsub(%r{^/}, "")
101
55
 
102
- session[:user_tokens] = ShopifyApp::SessionRepository.user_storage.present?
56
+ auth_attributes = ShopifyAPI::Auth::Oauth.begin_auth(
57
+ shop: sanitized_shop_name,
58
+ redirect_path: "/#{callback_url}",
59
+ is_online: user_session_expected?
60
+ )
61
+ cookies.encrypted[auth_attributes[:cookie].name] = {
62
+ expires: auth_attributes[:cookie].expires,
63
+ secure: true,
64
+ http_only: true,
65
+ value: auth_attributes[:cookie].value,
66
+ }
103
67
 
104
- ShopifyAPI::Session.temp(
105
- domain: shop_session.domain,
106
- token: shop_session.token,
107
- api_version: shop_session.api_version
108
- ) do
109
- ShopifyAPI::Metafield.find(:token_validity_bogus_check)
110
- end
111
- rescue ActiveResource::UnauthorizedAccess
112
- session[:user_tokens] = false
113
- rescue StandardError
68
+ redirect_to(auth_attributes[:auth_route], allow_other_host: true)
114
69
  end
115
- # rubocop:enable Lint/SuppressedException
116
70
 
117
71
  def validate_shop_presence
118
72
  @shop = sanitized_shop_name
@@ -125,60 +79,22 @@ module ShopifyApp
125
79
  end
126
80
 
127
81
  def copy_return_to_param_to_session
128
- session[:return_to] = RedirectSafely.make_safe(params[:return_to], '/') if params[:return_to]
82
+ session[:return_to] = RedirectSafely.make_safe(params[:return_to], "/") if params[:return_to]
129
83
  end
130
84
 
131
85
  def render_invalid_shop_error
132
- flash[:error] = I18n.t('invalid_shop_url')
86
+ flash[:error] = I18n.t("invalid_shop_url")
133
87
  redirect_to(return_address)
134
88
  end
135
89
 
136
- def enable_cookie_access
137
- fullpage_redirect_to(enable_cookies_path(
138
- shop: sanitized_shop_name,
139
- return_to: session[:return_to]
140
- ))
141
- end
142
-
143
- def authenticate_in_context
144
- redirect_to("#{main_app.root_path}auth/shopify")
145
- end
146
-
147
- def authenticate_at_top_level
148
- fullpage_redirect_to(login_url_with_optional_shop(top_level: true))
149
- end
150
-
151
- def authenticate_in_context?
90
+ def top_level?
152
91
  return true unless ShopifyApp.configuration.embedded_app?
153
- params[:top_level]
154
- end
155
-
156
- def request_storage_access?
157
- return false unless ShopifyApp.configuration.embedded_app?
158
- return false if params[:top_level]
159
- return false if user_agent_is_mobile
160
- return false if user_agent_is_pos
161
92
 
162
- !session['shopify.granted_storage_access']
93
+ !params[:top_level].nil?
163
94
  end
164
95
 
165
- def redirect_to_request_storage_access
166
- render(
167
- :request_storage_access,
168
- layout: false,
169
- locals: {
170
- does_not_have_storage_access_url: top_level_interaction_path(
171
- shop: sanitized_shop_name,
172
- return_to: session[:return_to]
173
- ),
174
- has_storage_access_url: login_url_with_optional_shop(top_level: true),
175
- app_target_url: granted_storage_access_path(
176
- shop: sanitized_shop_name,
177
- return_to: session[:return_to]
178
- ),
179
- current_shopify_domain: current_shopify_domain,
180
- }
181
- )
96
+ def redirect_auth_to_top_level
97
+ fullpage_redirect_to(login_url_with_optional_shop(top_level: true))
182
98
  end
183
99
  end
184
100
  end