shopify_app 7.2.0 → 17.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (199) hide show
  1. checksums.yaml +5 -5
  2. data/.babelrc +5 -0
  3. data/.github/CODEOWNERS +1 -0
  4. data/.github/ISSUE_TEMPLATE/bug-report.md +63 -0
  5. data/.github/ISSUE_TEMPLATE/config.yml +1 -0
  6. data/.github/ISSUE_TEMPLATE/feature-request.md +33 -0
  7. data/.github/PULL_REQUEST_TEMPLATE.md +22 -0
  8. data/.github/probots.yml +2 -0
  9. data/.github/workflows/build.yml +38 -0
  10. data/.github/workflows/release.yml +24 -0
  11. data/.github/workflows/rubocop.yml +22 -0
  12. data/.gitignore +4 -1
  13. data/.nvmrc +1 -0
  14. data/.rubocop.yml +18 -0
  15. data/.ruby-version +1 -0
  16. data/CHANGELOG.md +465 -0
  17. data/CONTRIBUTING.md +76 -0
  18. data/Gemfile +7 -0
  19. data/Gemfile.lock +256 -0
  20. data/README.md +73 -288
  21. data/Rakefile +1 -0
  22. data/SECURITY.md +59 -0
  23. data/app/assets/images/storage_access.svg +1 -0
  24. data/app/assets/javascripts/shopify_app/enable_cookies.js +3 -0
  25. data/app/assets/javascripts/shopify_app/itp_helper.js +40 -0
  26. data/app/assets/javascripts/shopify_app/partition_cookies.js +8 -0
  27. data/app/assets/javascripts/shopify_app/redirect.js +33 -0
  28. data/app/assets/javascripts/shopify_app/request_storage_access.js +3 -0
  29. data/app/assets/javascripts/shopify_app/storage_access.js +154 -0
  30. data/app/assets/javascripts/shopify_app/storage_access_redirect.js +17 -0
  31. data/app/assets/javascripts/shopify_app/top_level.js +2 -0
  32. data/app/assets/javascripts/shopify_app/top_level_interaction.js +11 -0
  33. data/app/controllers/concerns/shopify_app/authenticated.rb +16 -0
  34. data/app/controllers/concerns/shopify_app/ensure_authenticated_links.rb +26 -0
  35. data/app/controllers/concerns/shopify_app/require_known_shop.rb +39 -0
  36. data/app/controllers/concerns/shopify_app/shop_access_scopes_verification.rb +32 -0
  37. data/app/controllers/shopify_app/authenticated_controller.rb +5 -5
  38. data/app/controllers/shopify_app/callback_controller.rb +196 -0
  39. data/app/controllers/shopify_app/extension_verification_controller.rb +15 -0
  40. data/app/controllers/shopify_app/sessions_controller.rb +190 -2
  41. data/app/controllers/shopify_app/webhooks_controller.rb +16 -7
  42. data/app/views/shopify_app/partials/_button_styles.html.erb +109 -0
  43. data/app/views/shopify_app/partials/_card_styles.html.erb +33 -0
  44. data/app/views/shopify_app/partials/_empty_state_styles.html.erb +98 -0
  45. data/app/views/shopify_app/partials/_form_styles.html.erb +56 -0
  46. data/app/views/shopify_app/partials/_layout_styles.html.erb +182 -0
  47. data/app/views/shopify_app/partials/_typography_styles.html.erb +35 -0
  48. data/app/views/shopify_app/sessions/enable_cookies.html.erb +70 -0
  49. data/app/views/shopify_app/sessions/new.html.erb +39 -83
  50. data/app/views/shopify_app/sessions/request_storage_access.html.erb +68 -0
  51. data/app/views/shopify_app/sessions/top_level_interaction.html.erb +63 -0
  52. data/app/views/shopify_app/shared/redirect.html.erb +23 -0
  53. data/config/locales/cs.yml +23 -0
  54. data/config/locales/da.yml +20 -0
  55. data/config/locales/de.yml +22 -0
  56. data/config/locales/en.yml +12 -1
  57. data/config/locales/es.yml +21 -3
  58. data/config/locales/fi.yml +20 -0
  59. data/config/locales/fr.yml +23 -0
  60. data/config/locales/hi.yml +23 -0
  61. data/config/locales/it.yml +21 -0
  62. data/config/locales/ja.yml +17 -0
  63. data/config/locales/ko.yml +19 -0
  64. data/config/locales/ms.yml +22 -0
  65. data/config/locales/nb.yml +21 -0
  66. data/config/locales/nl.yml +21 -0
  67. data/config/locales/pl.yml +21 -0
  68. data/config/locales/pt-BR.yml +21 -0
  69. data/config/locales/pt-PT.yml +22 -0
  70. data/config/locales/sv.yml +21 -0
  71. data/config/locales/th.yml +20 -0
  72. data/config/locales/tr.yml +22 -0
  73. data/config/locales/vi.yml +22 -0
  74. data/config/locales/zh-CN.yml +16 -0
  75. data/config/locales/zh-TW.yml +16 -0
  76. data/config/routes.rb +12 -1
  77. data/docs/Quickstart.md +31 -0
  78. data/docs/Releasing.md +21 -0
  79. data/docs/Troubleshooting.md +16 -0
  80. data/docs/Upgrading.md +110 -0
  81. data/docs/shopify_app/authentication.md +124 -0
  82. data/docs/shopify_app/engine.md +82 -0
  83. data/docs/shopify_app/generators.md +127 -0
  84. data/docs/shopify_app/handling-access-scopes-changes.md +8 -0
  85. data/docs/shopify_app/script-tags.md +28 -0
  86. data/docs/shopify_app/session-repository.md +88 -0
  87. data/docs/shopify_app/testing.md +38 -0
  88. data/docs/shopify_app/webhooks.md +72 -0
  89. data/karma.conf.js +44 -0
  90. data/lib/generators/shopify_app/add_after_authenticate_job/add_after_authenticate_job_generator.rb +47 -0
  91. data/lib/generators/shopify_app/add_after_authenticate_job/templates/after_authenticate_job.rb +11 -0
  92. data/lib/generators/shopify_app/add_marketing_activity_extension/add_marketing_activity_extension_generator.rb +40 -0
  93. data/lib/generators/shopify_app/add_marketing_activity_extension/templates/marketing_activities_controller.rb +62 -0
  94. data/lib/generators/shopify_app/add_webhook/add_webhook_generator.rb +5 -4
  95. data/lib/generators/shopify_app/add_webhook/templates/{webhook_job.rb → webhook_job.rb.tt} +5 -0
  96. data/lib/generators/shopify_app/app_proxy_controller/app_proxy_controller_generator.rb +4 -3
  97. data/lib/generators/shopify_app/app_proxy_controller/templates/app_proxy_controller.rb +3 -3
  98. data/lib/generators/shopify_app/app_proxy_controller/templates/app_proxy_route.rb +10 -9
  99. data/lib/generators/shopify_app/app_proxy_controller/templates/index.html.erb +2 -2
  100. data/lib/generators/shopify_app/authenticated_controller/authenticated_controller_generator.rb +15 -0
  101. data/lib/generators/shopify_app/authenticated_controller/templates/authenticated_controller.rb +5 -0
  102. data/lib/generators/shopify_app/controllers/controllers_generator.rb +2 -1
  103. data/lib/generators/shopify_app/home_controller/home_controller_generator.rb +31 -9
  104. data/lib/generators/shopify_app/home_controller/templates/home_controller.rb +6 -1
  105. data/lib/generators/shopify_app/home_controller/templates/index.html.erb +70 -6
  106. data/lib/generators/shopify_app/home_controller/templates/unauthenticated_home_controller.rb +11 -0
  107. data/lib/generators/shopify_app/install/install_generator.rb +78 -27
  108. data/lib/generators/shopify_app/install/templates/_flash_messages.html.erb +1 -13
  109. data/lib/generators/shopify_app/install/templates/embedded_app.html.erb +12 -11
  110. data/lib/generators/shopify_app/install/templates/flash_messages.js +24 -0
  111. data/lib/generators/shopify_app/install/templates/omniauth.rb +3 -1
  112. data/lib/generators/shopify_app/install/templates/session_store.rb +4 -0
  113. data/lib/generators/shopify_app/install/templates/shopify_app.js +15 -0
  114. data/lib/generators/shopify_app/install/templates/shopify_app.rb.tt +25 -0
  115. data/lib/generators/shopify_app/install/templates/shopify_app_index.js +2 -0
  116. data/lib/generators/shopify_app/install/templates/shopify_provider.rb.tt +8 -0
  117. data/lib/generators/shopify_app/install/templates/user_agent.rb +6 -0
  118. data/lib/generators/shopify_app/products_controller/products_controller_generator.rb +19 -0
  119. data/lib/generators/shopify_app/products_controller/templates/products_controller.rb +8 -0
  120. data/lib/generators/shopify_app/rotate_shopify_token_job/rotate_shopify_token_job_generator.rb +16 -0
  121. data/lib/generators/shopify_app/rotate_shopify_token_job/templates/rotate_shopify_token.rake +17 -0
  122. data/lib/generators/shopify_app/rotate_shopify_token_job/templates/rotate_shopify_token_job.rb +42 -0
  123. data/lib/generators/shopify_app/routes/routes_generator.rb +1 -0
  124. data/lib/generators/shopify_app/routes/templates/routes.rb +10 -9
  125. data/lib/generators/shopify_app/shop_model/shop_model_generator.rb +42 -14
  126. data/lib/generators/shopify_app/shop_model/templates/db/migrate/add_shop_access_scopes_column.erb +5 -0
  127. data/lib/generators/shopify_app/shop_model/templates/db/migrate/{create_shops.rb → create_shops.erb} +1 -1
  128. data/lib/generators/shopify_app/shop_model/templates/shop.rb +6 -2
  129. data/lib/generators/shopify_app/shopify_app_generator.rb +5 -3
  130. data/lib/generators/shopify_app/user_model/templates/db/migrate/add_user_access_scopes_column.erb +5 -0
  131. data/lib/generators/shopify_app/user_model/templates/db/migrate/create_users.erb +16 -0
  132. data/lib/generators/shopify_app/user_model/templates/user.rb +8 -0
  133. data/lib/generators/shopify_app/user_model/templates/users.yml +4 -0
  134. data/lib/generators/shopify_app/user_model/user_model_generator.rb +70 -0
  135. data/lib/generators/shopify_app/views/views_generator.rb +2 -1
  136. data/lib/shopify_app/access_scopes/noop_strategy.rb +13 -0
  137. data/lib/shopify_app/access_scopes/shop_strategy.rb +24 -0
  138. data/lib/shopify_app/access_scopes/user_strategy.rb +41 -0
  139. data/lib/shopify_app/configuration.rb +69 -5
  140. data/lib/shopify_app/{app_proxy_verification.rb → controller_concerns/app_proxy_verification.rb} +4 -9
  141. data/lib/shopify_app/controller_concerns/csrf_protection.rb +15 -0
  142. data/lib/shopify_app/controller_concerns/embedded_app.rb +20 -0
  143. data/lib/shopify_app/controller_concerns/itp.rb +45 -0
  144. data/lib/shopify_app/controller_concerns/localization.rb +23 -0
  145. data/lib/shopify_app/controller_concerns/login_protection.rb +244 -0
  146. data/lib/shopify_app/controller_concerns/payload_verification.rb +24 -0
  147. data/lib/shopify_app/controller_concerns/webhook_verification.rb +23 -0
  148. data/lib/shopify_app/engine.rb +40 -0
  149. data/lib/shopify_app/jobs/scripttags_manager_job.rb +16 -0
  150. data/lib/shopify_app/{webhooks_manager_job.rb → jobs/webhooks_manager_job.rb} +3 -2
  151. data/lib/shopify_app/{scripttags_manager.rb → managers/scripttags_manager.rb} +25 -8
  152. data/lib/shopify_app/{webhooks_manager.rb → managers/webhooks_manager.rb} +6 -5
  153. data/lib/shopify_app/middleware/jwt_middleware.rb +42 -0
  154. data/lib/shopify_app/middleware/same_site_cookie_middleware.rb +34 -0
  155. data/lib/shopify_app/omniauth/omniauth_configuration.rb +64 -0
  156. data/lib/shopify_app/session/in_memory_session_store.rb +31 -0
  157. data/lib/shopify_app/session/in_memory_shop_session_store.rb +16 -0
  158. data/lib/shopify_app/session/in_memory_user_session_store.rb +16 -0
  159. data/lib/shopify_app/session/jwt.rb +63 -0
  160. data/lib/shopify_app/session/null_user_session_store.rb +22 -0
  161. data/lib/shopify_app/session/session_repository.rb +56 -0
  162. data/lib/shopify_app/session/session_storage.rb +20 -0
  163. data/lib/shopify_app/session/shop_session_storage.rb +42 -0
  164. data/lib/shopify_app/session/shop_session_storage_with_scopes.rb +58 -0
  165. data/lib/shopify_app/session/user_session_storage.rb +42 -0
  166. data/lib/shopify_app/session/user_session_storage_with_scopes.rb +58 -0
  167. data/lib/shopify_app/test_helpers/all.rb +2 -0
  168. data/lib/shopify_app/test_helpers/webhook_verification_helper.rb +17 -0
  169. data/lib/shopify_app/utils.rb +24 -4
  170. data/lib/shopify_app/version.rb +2 -1
  171. data/lib/shopify_app.rb +65 -24
  172. data/package.json +27 -0
  173. data/service.yml +7 -0
  174. data/shipit.rubygems.yml +3 -0
  175. data/shopify_app.gemspec +20 -9
  176. data/translation.yml +7 -0
  177. data/webpack.config.js +24 -0
  178. data/yarn.lock +5215 -0
  179. metadata +274 -43
  180. data/.travis.yml +0 -17
  181. data/Gemfile.rails50 +0 -5
  182. data/Gemfile.ruby22 +0 -6
  183. data/Gemfile.ruby22.rails50 +0 -9
  184. data/ISSUE_TEMPLATE.md +0 -14
  185. data/QUICKSTART.md +0 -72
  186. data/RELEASING +0 -13
  187. data/lib/generators/shopify_app/home_controller/templates/shopify_app_ready_script.html.erb +0 -11
  188. data/lib/generators/shopify_app/install/templates/shopify_app.rb +0 -9
  189. data/lib/generators/shopify_app/install/templates/shopify_provider.rb +0 -4
  190. data/lib/generators/shopify_app/install/templates/shopify_session_repository.rb +0 -23
  191. data/lib/generators/shopify_app/shop_model/templates/shopify_session_repository.rb +0 -7
  192. data/lib/shopify_app/in_memory_session_store.rb +0 -25
  193. data/lib/shopify_app/login_protection.rb +0 -103
  194. data/lib/shopify_app/scripttags_manager_job.rb +0 -15
  195. data/lib/shopify_app/session_storage.rb +0 -23
  196. data/lib/shopify_app/sessions_concern.rb +0 -101
  197. data/lib/shopify_app/shop.rb +0 -15
  198. data/lib/shopify_app/shopify_session_repository.rb +0 -34
  199. data/lib/shopify_app/webhook_verification.rb +0 -39
@@ -0,0 +1,64 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ShopifyApp
4
+ class OmniAuthConfiguration
5
+ attr_reader :strategy, :request
6
+ attr_writer :client_options_site, :scopes, :per_user_permissions
7
+
8
+ def initialize(strategy, request)
9
+ @strategy = strategy
10
+ @request = request
11
+ end
12
+
13
+ def build_options
14
+ strategy.options[:client_options][:site] = client_options_site
15
+ strategy.options[:scope] = scopes
16
+ strategy.options[:old_client_secret] = ShopifyApp.configuration.old_secret
17
+ strategy.options[:per_user_permissions] = request_online_tokens?
18
+ end
19
+
20
+ private
21
+
22
+ def request_online_tokens?
23
+ return @per_user_permissions unless @per_user_permissions.nil?
24
+ default_request_online_tokens?
25
+ end
26
+
27
+ def scopes
28
+ @scopes || default_scopes
29
+ end
30
+
31
+ def client_options_site
32
+ @client_options_site || default_client_options_site
33
+ end
34
+
35
+ def default_scopes
36
+ if request_online_tokens?
37
+ ShopifyApp.configuration.user_access_scopes
38
+ else
39
+ ShopifyApp.configuration.shop_access_scopes
40
+ end
41
+ end
42
+
43
+ def default_client_options_site
44
+ return '' unless shop_domain.present?
45
+ "https://#{shopify_auth_params[:shop]}"
46
+ end
47
+
48
+ def default_request_online_tokens?
49
+ strategy.session[:user_tokens] && !update_shop_scopes?
50
+ end
51
+
52
+ def update_shop_scopes?
53
+ ShopifyApp.configuration.shop_access_scopes_strategy.update_access_scopes?(shop_domain)
54
+ end
55
+
56
+ def shop_domain
57
+ request.params['shop'] || (shopify_auth_params && shopify_auth_params['shop'])
58
+ end
59
+
60
+ def shopify_auth_params
61
+ strategy.session['shopify.omniauth_params']&.with_indifferent_access
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+ module ShopifyApp
3
+ # rubocop:disable Style/ClassVars
4
+ # Class var repo is needed here in order to share data between the 2 child classes.
5
+ class InMemorySessionStore
6
+ class EnvironmentError < StandardError; end
7
+
8
+ def self.retrieve(id)
9
+ repo[id]
10
+ end
11
+
12
+ def self.store(session, *_args)
13
+ id = SecureRandom.uuid
14
+ repo[id] = session
15
+ id
16
+ end
17
+
18
+ def self.clear
19
+ @@repo = nil
20
+ end
21
+
22
+ def self.repo
23
+ if Rails.env.production?
24
+ raise EnvironmentError, "Cannot use InMemorySessionStore in a Production environment. \
25
+ Please initialize ShopifyApp with a model that can store and retrieve sessions"
26
+ end
27
+ @@repo ||= {}
28
+ end
29
+ end
30
+ # rubocop:enable Style/ClassVars
31
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+ module ShopifyApp
3
+ class InMemoryShopSessionStore < InMemorySessionStore
4
+ class << self
5
+ def store(session, *args)
6
+ id = super
7
+ repo[session.domain] = session
8
+ id
9
+ end
10
+
11
+ def retrieve_by_shopify_domain(shopify_domain)
12
+ repo[shopify_domain]
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+ module ShopifyApp
3
+ class InMemoryUserSessionStore < InMemorySessionStore
4
+ class << self
5
+ def store(session, user)
6
+ id = super
7
+ repo[user.shopify_user_id] = session
8
+ id
9
+ end
10
+
11
+ def retrieve_by_shopify_user_id(user_id)
12
+ repo[user_id]
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,63 @@
1
+ # frozen_string_literal: true
2
+ module ShopifyApp
3
+ class JWT
4
+ class InvalidDestinationError < StandardError; end
5
+
6
+ class MismatchedHostsError < StandardError; end
7
+
8
+ class InvalidAudienceError < StandardError; end
9
+
10
+ WARN_EXCEPTIONS = [
11
+ ::JWT::DecodeError,
12
+ ::JWT::ExpiredSignature,
13
+ ::JWT::ImmatureSignature,
14
+ ::JWT::VerificationError,
15
+ InvalidAudienceError,
16
+ InvalidDestinationError,
17
+ MismatchedHostsError,
18
+ ]
19
+
20
+ def initialize(token)
21
+ @token = token
22
+ set_payload
23
+ end
24
+
25
+ def shopify_domain
26
+ @payload && ShopifyApp::Utils.sanitize_shop_domain(@payload['dest'])
27
+ end
28
+
29
+ def shopify_user_id
30
+ @payload['sub'].to_i if @payload && @payload['sub']
31
+ end
32
+
33
+ private
34
+
35
+ def set_payload
36
+ payload, _ = parse_token_data(ShopifyApp.configuration&.secret, ShopifyApp.configuration&.old_secret)
37
+ @payload = validate_payload(payload)
38
+ rescue *WARN_EXCEPTIONS => error
39
+ Rails.logger.warn("[ShopifyApp::JWT] Failed to validate JWT: [#{error.class}] #{error}")
40
+ nil
41
+ end
42
+
43
+ def parse_token_data(secret, old_secret)
44
+ ::JWT.decode(@token, secret, true, { algorithm: 'HS256' })
45
+ rescue ::JWT::VerificationError
46
+ raise unless old_secret
47
+
48
+ ::JWT.decode(@token, old_secret, true, { algorithm: 'HS256' })
49
+ end
50
+
51
+ def validate_payload(payload)
52
+ dest_host = ShopifyApp::Utils.sanitize_shop_domain(payload['dest'])
53
+ iss_host = ShopifyApp::Utils.sanitize_shop_domain(payload['iss'])
54
+ api_key = ShopifyApp.configuration.api_key
55
+
56
+ raise InvalidAudienceError, "'aud' claim does not match api_key" unless payload['aud'] == api_key
57
+ raise InvalidDestinationError, "'dest' claim host not a valid shopify host" unless dest_host
58
+ raise MismatchedHostsError, "'dest' claim host does not match 'iss' claim host" unless dest_host == iss_host
59
+
60
+ payload
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+ module ShopifyApp
3
+ class NullUserSessionStore
4
+ class << self
5
+ def retrieve(_)
6
+ nil
7
+ end
8
+
9
+ def store(_, _)
10
+ raise SessionRepository::ConfigurationError, 'user_storage is not configured'
11
+ end
12
+
13
+ def retrieve_by_shopify_user_id(_)
14
+ nil
15
+ end
16
+
17
+ def blank?
18
+ true
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+ module ShopifyApp
3
+ class SessionRepository
4
+ class ConfigurationError < StandardError; end
5
+
6
+ class << self
7
+ attr_writer :shop_storage
8
+
9
+ attr_writer :user_storage
10
+
11
+ def retrieve_shop_session(id)
12
+ shop_storage.retrieve(id)
13
+ end
14
+
15
+ def retrieve_user_session(id)
16
+ user_storage.retrieve(id)
17
+ end
18
+
19
+ def retrieve_shop_session_by_shopify_domain(shopify_domain)
20
+ shop_storage.retrieve_by_shopify_domain(shopify_domain)
21
+ end
22
+
23
+ def retrieve_user_session_by_shopify_user_id(user_id)
24
+ user_storage.retrieve_by_shopify_user_id(user_id)
25
+ end
26
+
27
+ def store_shop_session(session)
28
+ shop_storage.store(session)
29
+ end
30
+
31
+ def store_user_session(session, user)
32
+ user_storage.store(session, user)
33
+ end
34
+
35
+ def shop_storage
36
+ load_shop_storage || raise(ConfigurationError, "ShopifySessionRepository.shop_storage is not configured!")
37
+ end
38
+
39
+ def user_storage
40
+ load_user_storage
41
+ end
42
+
43
+ private
44
+
45
+ def load_shop_storage
46
+ return unless @shop_storage
47
+ @shop_storage.respond_to?(:safe_constantize) ? @shop_storage.safe_constantize : @shop_storage
48
+ end
49
+
50
+ def load_user_storage
51
+ return NullUserSessionStore unless @user_storage
52
+ @user_storage.respond_to?(:safe_constantize) ? @user_storage.safe_constantize : @user_storage
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+ module ShopifyApp
3
+ module SessionStorage
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+ validates :shopify_token, presence: true
8
+ validates :api_version, presence: true
9
+ end
10
+
11
+ def with_shopify_session(&block)
12
+ ShopifyAPI::Session.temp(
13
+ domain: shopify_domain,
14
+ token: shopify_token,
15
+ api_version: api_version,
16
+ &block
17
+ )
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+ module ShopifyApp
3
+ module ShopSessionStorage
4
+ extend ActiveSupport::Concern
5
+ include ::ShopifyApp::SessionStorage
6
+
7
+ included do
8
+ validates :shopify_domain, presence: true, uniqueness: { case_sensitive: false }
9
+ end
10
+
11
+ class_methods do
12
+ def store(auth_session, *_args)
13
+ shop = find_or_initialize_by(shopify_domain: auth_session.domain)
14
+ shop.shopify_token = auth_session.token
15
+ shop.save!
16
+ shop.id
17
+ end
18
+
19
+ def retrieve(id)
20
+ shop = find_by(id: id)
21
+ construct_session(shop)
22
+ end
23
+
24
+ def retrieve_by_shopify_domain(domain)
25
+ shop = find_by(shopify_domain: domain)
26
+ construct_session(shop)
27
+ end
28
+
29
+ private
30
+
31
+ def construct_session(shop)
32
+ return unless shop
33
+
34
+ ShopifyAPI::Session.new(
35
+ domain: shop.shopify_domain,
36
+ token: shop.shopify_token,
37
+ api_version: shop.api_version,
38
+ )
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ShopifyApp
4
+ module ShopSessionStorageWithScopes
5
+ extend ActiveSupport::Concern
6
+ include ::ShopifyApp::SessionStorage
7
+
8
+ included do
9
+ validates :shopify_domain, presence: true, uniqueness: { case_sensitive: false }
10
+ end
11
+
12
+ class_methods do
13
+ def store(auth_session, *_args)
14
+ shop = find_or_initialize_by(shopify_domain: auth_session.domain)
15
+ shop.shopify_token = auth_session.token
16
+ shop.access_scopes = auth_session.access_scopes
17
+
18
+ shop.save!
19
+ shop.id
20
+ end
21
+
22
+ def retrieve(id)
23
+ shop = find_by(id: id)
24
+ construct_session(shop)
25
+ end
26
+
27
+ def retrieve_by_shopify_domain(domain)
28
+ shop = find_by(shopify_domain: domain)
29
+ construct_session(shop)
30
+ end
31
+
32
+ private
33
+
34
+ def construct_session(shop)
35
+ return unless shop
36
+
37
+ ShopifyAPI::Session.new(
38
+ domain: shop.shopify_domain,
39
+ token: shop.shopify_token,
40
+ api_version: shop.api_version,
41
+ access_scopes: shop.access_scopes
42
+ )
43
+ end
44
+ end
45
+
46
+ def access_scopes=(scopes)
47
+ super(scopes)
48
+ rescue NotImplementedError, NoMethodError
49
+ raise NotImplementedError, "#access_scopes= must be defined to handle storing access scopes: #{scopes}"
50
+ end
51
+
52
+ def access_scopes
53
+ super
54
+ rescue NotImplementedError, NoMethodError
55
+ raise NotImplementedError, "#access_scopes= must be defined to hook into stored access scopes"
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+ module ShopifyApp
3
+ module UserSessionStorage
4
+ extend ActiveSupport::Concern
5
+ include ::ShopifyApp::SessionStorage
6
+
7
+ included do
8
+ validates :shopify_domain, presence: true
9
+ end
10
+
11
+ class_methods do
12
+ def store(auth_session, user)
13
+ user = find_or_initialize_by(shopify_user_id: user[:id])
14
+ user.shopify_token = auth_session.token
15
+ user.shopify_domain = auth_session.domain
16
+ user.save!
17
+ user.id
18
+ end
19
+
20
+ def retrieve(id)
21
+ user = find_by(id: id)
22
+ construct_session(user)
23
+ end
24
+
25
+ def retrieve_by_shopify_user_id(user_id)
26
+ user = find_by(shopify_user_id: user_id)
27
+ construct_session(user)
28
+ end
29
+
30
+ private
31
+
32
+ def construct_session(user)
33
+ return unless user
34
+ ShopifyAPI::Session.new(
35
+ domain: user.shopify_domain,
36
+ token: user.shopify_token,
37
+ api_version: user.api_version,
38
+ )
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+ module ShopifyApp
3
+ module UserSessionStorageWithScopes
4
+ extend ActiveSupport::Concern
5
+ include ::ShopifyApp::SessionStorage
6
+
7
+ included do
8
+ validates :shopify_domain, presence: true
9
+ end
10
+
11
+ class_methods do
12
+ def store(auth_session, user)
13
+ user = find_or_initialize_by(shopify_user_id: user[:id])
14
+ user.shopify_token = auth_session.token
15
+ user.shopify_domain = auth_session.domain
16
+ user.access_scopes = auth_session.access_scopes
17
+
18
+ user.save!
19
+ user.id
20
+ end
21
+
22
+ def retrieve(id)
23
+ user = find_by(id: id)
24
+ construct_session(user)
25
+ end
26
+
27
+ def retrieve_by_shopify_user_id(user_id)
28
+ user = find_by(shopify_user_id: user_id)
29
+ construct_session(user)
30
+ end
31
+
32
+ private
33
+
34
+ def construct_session(user)
35
+ return unless user
36
+
37
+ ShopifyAPI::Session.new(
38
+ domain: user.shopify_domain,
39
+ token: user.shopify_token,
40
+ api_version: user.api_version,
41
+ access_scopes: user.access_scopes
42
+ )
43
+ end
44
+ end
45
+
46
+ def access_scopes=(scopes)
47
+ super(scopes)
48
+ rescue NotImplementedError, NoMethodError
49
+ raise NotImplementedError, "#access_scopes= must be defined to handle storing access scopes: #{scopes}"
50
+ end
51
+
52
+ def access_scopes
53
+ super
54
+ rescue NotImplementedError, NoMethodError
55
+ raise NotImplementedError, "#access_scopes= must be defined to hook into stored access scopes"
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,2 @@
1
+ # frozen_string_literal: true
2
+ require 'shopify_app/test_helpers/webhook_verification_helper'
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+ module ShopifyApp
3
+ module TestHelpers
4
+ module WebhookVerificationHelper
5
+ def authorized_webhook_verification_headers!(params = {})
6
+ digest = OpenSSL::Digest.new('sha256')
7
+ secret = ShopifyApp.configuration.secret
8
+ valid_hmac = Base64.encode64(OpenSSL::HMAC.digest(digest, secret, params.to_query)).strip
9
+ @request.headers['HTTP_X_SHOPIFY_HMAC_SHA256'] = valid_hmac
10
+ end
11
+
12
+ def unauthorized_webhook_verification_headers!
13
+ @request.headers['HTTP_X_SHOPIFY_HMAC_SHA256'] = "invalid_hmac"
14
+ end
15
+ end
16
+ end
17
+ end
@@ -1,16 +1,36 @@
1
+ # frozen_string_literal: true
1
2
  module ShopifyApp
2
3
  module Utils
3
-
4
4
  def self.sanitize_shop_domain(shop_domain)
5
- name = shop_domain.to_s.strip
6
- name += ".#{ShopifyApp.configuration.myshopify_domain}" if !name.include?("#{ShopifyApp.configuration.myshopify_domain}") && !name.include?(".")
5
+ myshopify_domain = ShopifyApp.configuration.myshopify_domain
6
+ name = shop_domain.to_s.downcase.strip
7
+ name += ".#{myshopify_domain}" if !name.include?(myshopify_domain.to_s) && !name.include?(".")
7
8
  name.sub!(%r|https?://|, '')
8
9
 
9
10
  u = URI("http://#{name}")
10
- u.host && u.host.ends_with?(".#{ShopifyApp.configuration.myshopify_domain}") ? u.host : nil
11
+ u.host if u.host&.match(/^[a-z0-9][a-z0-9\-]*[a-z0-9]\.#{Regexp.escape(myshopify_domain)}$/)
11
12
  rescue URI::InvalidURIError
12
13
  nil
13
14
  end
14
15
 
16
+ def self.fetch_known_api_versions
17
+ Rails.logger.info("[ShopifyAPI::ApiVersion] Fetching known Admin API Versions from Shopify...")
18
+ ShopifyAPI::ApiVersion.fetch_known_versions
19
+ Rails.logger.info("[ShopifyAPI::ApiVersion] Known API Versions: #{ShopifyAPI::ApiVersion.versions.keys}")
20
+ rescue ActiveResource::ConnectionError
21
+ logger.error("[ShopifyAPI::ApiVersion] Unable to fetch api_versions from Shopify")
22
+ end
23
+
24
+ def self.shop_login_url(shop:, return_to:)
25
+ return ShopifyApp.configuration.login_url unless shop
26
+ url = URI(ShopifyApp.configuration.login_url)
27
+
28
+ url.query = URI.encode_www_form(
29
+ shop: shop,
30
+ return_to: return_to,
31
+ )
32
+
33
+ url.to_s
34
+ end
15
35
  end
16
36
  end
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module ShopifyApp
2
- VERSION = '7.2.0'
3
+ VERSION = '17.1.0'
3
4
  end
data/lib/shopify_app.rb CHANGED
@@ -1,30 +1,71 @@
1
+ # frozen_string_literal: true
1
2
  require 'shopify_app/version'
2
3
 
3
4
  # deps
4
5
  require 'shopify_api'
5
6
  require 'omniauth-shopify-oauth2'
7
+ require 'redirect_safely'
6
8
 
7
- # config
8
- require 'shopify_app/configuration'
9
-
10
- # engine
11
- require 'shopify_app/engine'
12
-
13
- # jobs
14
- require 'shopify_app/webhooks_manager_job'
15
- require 'shopify_app/scripttags_manager_job'
16
-
17
- # helpers and concerns
18
- require 'shopify_app/shop'
19
- require 'shopify_app/session_storage'
20
- require 'shopify_app/sessions_concern'
21
- require 'shopify_app/login_protection'
22
- require 'shopify_app/webhooks_manager'
23
- require 'shopify_app/scripttags_manager'
24
- require 'shopify_app/webhook_verification'
25
- require 'shopify_app/app_proxy_verification'
26
- require 'shopify_app/utils'
27
-
28
- # session repository
29
- require 'shopify_app/shopify_session_repository'
30
- require 'shopify_app/in_memory_session_store'
9
+ module ShopifyApp
10
+ def self.rails6?
11
+ Rails::VERSION::MAJOR >= 6
12
+ end
13
+
14
+ def self.use_webpacker?
15
+ rails6? &&
16
+ defined?(Webpacker) == 'constant' &&
17
+ !configuration.disable_webpacker
18
+ end
19
+
20
+ # config
21
+ require 'shopify_app/configuration'
22
+
23
+ # engine
24
+ require 'shopify_app/engine'
25
+
26
+ # utils
27
+ require 'shopify_app/utils'
28
+
29
+ # controller concerns
30
+ require 'shopify_app/controller_concerns/csrf_protection'
31
+ require 'shopify_app/controller_concerns/localization'
32
+ require 'shopify_app/controller_concerns/itp'
33
+ require 'shopify_app/controller_concerns/login_protection'
34
+ require 'shopify_app/controller_concerns/embedded_app'
35
+ require 'shopify_app/controller_concerns/payload_verification'
36
+ require 'shopify_app/controller_concerns/app_proxy_verification'
37
+ require 'shopify_app/controller_concerns/webhook_verification'
38
+
39
+ # jobs
40
+ require 'shopify_app/jobs/webhooks_manager_job'
41
+ require 'shopify_app/jobs/scripttags_manager_job'
42
+
43
+ # managers
44
+ require 'shopify_app/managers/webhooks_manager'
45
+ require 'shopify_app/managers/scripttags_manager'
46
+
47
+ # middleware
48
+ require 'shopify_app/middleware/jwt_middleware'
49
+ require 'shopify_app/middleware/same_site_cookie_middleware'
50
+
51
+ # session
52
+ require 'shopify_app/session/in_memory_session_store'
53
+ require 'shopify_app/session/in_memory_shop_session_store'
54
+ require 'shopify_app/session/in_memory_user_session_store'
55
+ require 'shopify_app/session/jwt'
56
+ require 'shopify_app/session/null_user_session_store'
57
+ require 'shopify_app/session/session_repository'
58
+ require 'shopify_app/session/session_storage'
59
+ require 'shopify_app/session/shop_session_storage'
60
+ require 'shopify_app/session/shop_session_storage_with_scopes'
61
+ require 'shopify_app/session/user_session_storage'
62
+ require 'shopify_app/session/user_session_storage_with_scopes'
63
+
64
+ # access scopes strategies
65
+ require 'shopify_app/access_scopes/shop_strategy'
66
+ require 'shopify_app/access_scopes/user_strategy'
67
+ require 'shopify_app/access_scopes/noop_strategy'
68
+
69
+ # omniauth_configuration
70
+ require 'shopify_app/omniauth/omniauth_configuration'
71
+ end