shopify_app 13.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (156) hide show
  1. checksums.yaml +7 -0
  2. data/.babelrc +5 -0
  3. data/.github/CODEOWNERS +1 -0
  4. data/.github/ISSUE_TEMPLATE.md +14 -0
  5. data/.github/PULL_REQUEST_TEMPLATE.md +6 -0
  6. data/.github/probots.yml +2 -0
  7. data/.github/workflows/rubocop.yml +28 -0
  8. data/.gitignore +16 -0
  9. data/.nvmrc +1 -0
  10. data/.rubocop.yml +17 -0
  11. data/.ruby-version +1 -0
  12. data/.travis.yml +28 -0
  13. data/CHANGELOG.md +505 -0
  14. data/Gemfile +11 -0
  15. data/LICENSE +19 -0
  16. data/README.md +620 -0
  17. data/Rakefile +7 -0
  18. data/SECURITY.md +59 -0
  19. data/app/assets/images/storage_access.svg +2 -0
  20. data/app/assets/javascripts/shopify_app/enable_cookies.js +3 -0
  21. data/app/assets/javascripts/shopify_app/itp_helper.js +40 -0
  22. data/app/assets/javascripts/shopify_app/partition_cookies.js +8 -0
  23. data/app/assets/javascripts/shopify_app/redirect.js +33 -0
  24. data/app/assets/javascripts/shopify_app/request_storage_access.js +3 -0
  25. data/app/assets/javascripts/shopify_app/storage_access.js +153 -0
  26. data/app/assets/javascripts/shopify_app/storage_access_redirect.js +17 -0
  27. data/app/assets/javascripts/shopify_app/top_level.js +2 -0
  28. data/app/assets/javascripts/shopify_app/top_level_interaction.js +11 -0
  29. data/app/controllers/concerns/shopify_app/authenticated.rb +16 -0
  30. data/app/controllers/concerns/shopify_app/require_known_shop.rb +39 -0
  31. data/app/controllers/shopify_app/authenticated_controller.rb +8 -0
  32. data/app/controllers/shopify_app/callback_controller.rb +140 -0
  33. data/app/controllers/shopify_app/extension_verification_controller.rb +15 -0
  34. data/app/controllers/shopify_app/sessions_controller.rb +184 -0
  35. data/app/controllers/shopify_app/webhooks_controller.rb +37 -0
  36. data/app/views/shopify_app/partials/_button_styles.html.erb +104 -0
  37. data/app/views/shopify_app/partials/_card_styles.html.erb +33 -0
  38. data/app/views/shopify_app/partials/_empty_state_styles.html.erb +129 -0
  39. data/app/views/shopify_app/partials/_layout_styles.html.erb +167 -0
  40. data/app/views/shopify_app/partials/_typography_styles.html.erb +35 -0
  41. data/app/views/shopify_app/sessions/enable_cookies.html.erb +75 -0
  42. data/app/views/shopify_app/sessions/new.html.erb +123 -0
  43. data/app/views/shopify_app/sessions/request_storage_access.html.erb +68 -0
  44. data/app/views/shopify_app/sessions/top_level_interaction.html.erb +64 -0
  45. data/app/views/shopify_app/shared/redirect.html.erb +23 -0
  46. data/config/locales/cs.yml +23 -0
  47. data/config/locales/da.yml +20 -0
  48. data/config/locales/de.yml +22 -0
  49. data/config/locales/en.yml +15 -0
  50. data/config/locales/es.yml +22 -0
  51. data/config/locales/fi.yml +20 -0
  52. data/config/locales/fr.yml +23 -0
  53. data/config/locales/hi.yml +23 -0
  54. data/config/locales/it.yml +21 -0
  55. data/config/locales/ja.yml +17 -0
  56. data/config/locales/ko.yml +19 -0
  57. data/config/locales/ms.yml +22 -0
  58. data/config/locales/nb.yml +21 -0
  59. data/config/locales/nl.yml +21 -0
  60. data/config/locales/pl.yml +21 -0
  61. data/config/locales/pt-BR.yml +21 -0
  62. data/config/locales/pt-PT.yml +22 -0
  63. data/config/locales/sv.yml +21 -0
  64. data/config/locales/th.yml +20 -0
  65. data/config/locales/tr.yml +22 -0
  66. data/config/locales/zh-CN.yml +16 -0
  67. data/config/locales/zh-TW.yml +16 -0
  68. data/config/routes.rb +23 -0
  69. data/docs/Quickstart.md +93 -0
  70. data/docs/Releasing.md +18 -0
  71. data/docs/Troubleshooting.md +16 -0
  72. data/docs/install-on-dev-shop.png +0 -0
  73. data/docs/test-your-app.png +0 -0
  74. data/images/app-proxy-screenshot.png +0 -0
  75. data/karma.conf.js +44 -0
  76. data/lib/generators/shopify_app/add_after_authenticate_job/add_after_authenticate_job_generator.rb +47 -0
  77. data/lib/generators/shopify_app/add_after_authenticate_job/templates/after_authenticate_job.rb +11 -0
  78. data/lib/generators/shopify_app/add_marketing_activity_extension/add_marketing_activity_extension_generator.rb +40 -0
  79. data/lib/generators/shopify_app/add_marketing_activity_extension/templates/marketing_activities_controller.rb +62 -0
  80. data/lib/generators/shopify_app/add_webhook/add_webhook_generator.rb +69 -0
  81. data/lib/generators/shopify_app/add_webhook/templates/webhook_job.rb.tt +13 -0
  82. data/lib/generators/shopify_app/app_proxy_controller/app_proxy_controller_generator.rb +26 -0
  83. data/lib/generators/shopify_app/app_proxy_controller/templates/app_proxy_controller.rb +8 -0
  84. data/lib/generators/shopify_app/app_proxy_controller/templates/app_proxy_route.rb +11 -0
  85. data/lib/generators/shopify_app/app_proxy_controller/templates/index.html.erb +19 -0
  86. data/lib/generators/shopify_app/authenticated_controller/authenticated_controller_generator.rb +15 -0
  87. data/lib/generators/shopify_app/authenticated_controller/templates/authenticated_controller.rb +5 -0
  88. data/lib/generators/shopify_app/controllers/controllers_generator.rb +30 -0
  89. data/lib/generators/shopify_app/home_controller/home_controller_generator.rb +26 -0
  90. data/lib/generators/shopify_app/home_controller/templates/home_controller.rb +8 -0
  91. data/lib/generators/shopify_app/home_controller/templates/index.html.erb +21 -0
  92. data/lib/generators/shopify_app/install/install_generator.rb +83 -0
  93. data/lib/generators/shopify_app/install/templates/_flash_messages.html.erb +3 -0
  94. data/lib/generators/shopify_app/install/templates/embedded_app.html.erb +41 -0
  95. data/lib/generators/shopify_app/install/templates/flash_messages.js +24 -0
  96. data/lib/generators/shopify_app/install/templates/omniauth.rb +3 -0
  97. data/lib/generators/shopify_app/install/templates/session_store.rb +4 -0
  98. data/lib/generators/shopify_app/install/templates/shopify_app.js +15 -0
  99. data/lib/generators/shopify_app/install/templates/shopify_app.rb.tt +15 -0
  100. data/lib/generators/shopify_app/install/templates/shopify_app_index.js +2 -0
  101. data/lib/generators/shopify_app/install/templates/shopify_provider.rb +20 -0
  102. data/lib/generators/shopify_app/install/templates/user_agent.rb +6 -0
  103. data/lib/generators/shopify_app/rotate_shopify_token_job/rotate_shopify_token_job_generator.rb +16 -0
  104. data/lib/generators/shopify_app/rotate_shopify_token_job/templates/rotate_shopify_token.rake +17 -0
  105. data/lib/generators/shopify_app/rotate_shopify_token_job/templates/rotate_shopify_token_job.rb +42 -0
  106. data/lib/generators/shopify_app/routes/routes_generator.rb +32 -0
  107. data/lib/generators/shopify_app/routes/templates/routes.rb +12 -0
  108. data/lib/generators/shopify_app/shop_model/shop_model_generator.rb +43 -0
  109. data/lib/generators/shopify_app/shop_model/templates/db/migrate/create_shops.erb +15 -0
  110. data/lib/generators/shopify_app/shop_model/templates/shop.rb +8 -0
  111. data/lib/generators/shopify_app/shop_model/templates/shops.yml +3 -0
  112. data/lib/generators/shopify_app/shopify_app_generator.rb +18 -0
  113. data/lib/generators/shopify_app/user_model/templates/db/migrate/create_users.erb +16 -0
  114. data/lib/generators/shopify_app/user_model/templates/user.rb +8 -0
  115. data/lib/generators/shopify_app/user_model/templates/users.yml +4 -0
  116. data/lib/generators/shopify_app/user_model/user_model_generator.rb +43 -0
  117. data/lib/generators/shopify_app/views/views_generator.rb +30 -0
  118. data/lib/shopify_app.rb +61 -0
  119. data/lib/shopify_app/configuration.rb +94 -0
  120. data/lib/shopify_app/controller_concerns/app_proxy_verification.rb +38 -0
  121. data/lib/shopify_app/controller_concerns/csrf_protection.rb +15 -0
  122. data/lib/shopify_app/controller_concerns/embedded_app.rb +20 -0
  123. data/lib/shopify_app/controller_concerns/itp.rb +45 -0
  124. data/lib/shopify_app/controller_concerns/localization.rb +23 -0
  125. data/lib/shopify_app/controller_concerns/login_protection.rb +231 -0
  126. data/lib/shopify_app/controller_concerns/payload_verification.rb +24 -0
  127. data/lib/shopify_app/controller_concerns/webhook_verification.rb +23 -0
  128. data/lib/shopify_app/engine.rb +25 -0
  129. data/lib/shopify_app/jobs/scripttags_manager_job.rb +16 -0
  130. data/lib/shopify_app/jobs/webhooks_manager_job.rb +16 -0
  131. data/lib/shopify_app/managers/scripttags_manager.rb +78 -0
  132. data/lib/shopify_app/managers/webhooks_manager.rb +62 -0
  133. data/lib/shopify_app/middleware/jwt_middleware.rb +42 -0
  134. data/lib/shopify_app/middleware/same_site_cookie_middleware.rb +34 -0
  135. data/lib/shopify_app/session/in_memory_session_store.rb +31 -0
  136. data/lib/shopify_app/session/in_memory_shop_session_store.rb +14 -0
  137. data/lib/shopify_app/session/in_memory_user_session_store.rb +14 -0
  138. data/lib/shopify_app/session/jwt.rb +61 -0
  139. data/lib/shopify_app/session/null_user_session_store.rb +22 -0
  140. data/lib/shopify_app/session/session_repository.rb +56 -0
  141. data/lib/shopify_app/session/session_storage.rb +20 -0
  142. data/lib/shopify_app/session/shop_session_storage.rb +42 -0
  143. data/lib/shopify_app/session/user_session_storage.rb +42 -0
  144. data/lib/shopify_app/test_helpers/all.rb +2 -0
  145. data/lib/shopify_app/test_helpers/webhook_verification_helper.rb +17 -0
  146. data/lib/shopify_app/utils.rb +24 -0
  147. data/lib/shopify_app/version.rb +4 -0
  148. data/package-lock.json +7177 -0
  149. data/package.json +28 -0
  150. data/service.yml +7 -0
  151. data/shipit.rubygems.yml +4 -0
  152. data/shopify_app.gemspec +37 -0
  153. data/translation.yml +7 -0
  154. data/webpack.config.js +24 -0
  155. data/yarn.lock +5263 -0
  156. metadata +420 -0
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+ source "https://rubygems.org"
3
+
4
+ # Specify your gem's dependencies in shopify_app.gemspec
5
+ gemspec
6
+
7
+ gem 'rails-controller-testing', group: :test
8
+
9
+ group :rubocop do
10
+ gem 'rubocop-shopify', require: false
11
+ end
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2013 Shopify
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
@@ -0,0 +1,620 @@
1
+ Shopify App
2
+ ===========
3
+ [![Version][gem]][gem_url] [![Build Status](https://travis-ci.org/Shopify/shopify_app.png)](https://travis-ci.org/Shopify/shopify_app)
4
+
5
+ [gem]: https://img.shields.io/gem/v/shopify_app.svg
6
+ [gem_url]: https://rubygems.org/gems/shopify_app
7
+
8
+
9
+ Shopify Application Rails engine and generator
10
+
11
+ #### NOTE: Versions 8.0.0 through 8.2.3 contained a CSRF vulnerability that was addressed in version 8.2.4. Please update to version 8.2.4 if you're using an old version.
12
+
13
+ Table of Contents
14
+ -----------------
15
+ - [Introduction](#introduction)
16
+ - [Become a Shopify App Developer](#become-a-shopify-app-developer)
17
+ - [Installation](#installation)
18
+ - [Generators](#generators)
19
+ - [Mounting the Engine](#mounting-the-engine)
20
+ - [Authentication](#authentication)
21
+ - [WebhooksManager](#webhooksmanager)
22
+ - [ScripttagsManager](#scripttagsmanager)
23
+ - [RotateShopifyTokenJob](#rotateshopifytokenjob)
24
+ - [App Tunneling](#app-tunneling)
25
+ - [AppProxyVerification](#appproxyverification)
26
+ - [Troubleshooting](#troubleshooting)
27
+ - [Testing an embedded app outside the Shopify admin](#testing-an-embedded-app-outside-the-shopify-admin)
28
+ - [Migration to 13.0.0](#migrating-to-1300)
29
+ - [Questions or problems?](#questions-or-problems-)
30
+ - [Rails 6 Compatibility](#rails-6-compatibility)
31
+ - [Upgrading from 8.6 to 9.0.0](#upgrading-from-86-to-900)
32
+
33
+ Introduction
34
+ -----------
35
+ Get started with the [Shopify Admin API](https://help.shopify.com/en/api/getting-started) faster; This gem includes a Rails Engine and generators for writing Rails applications using the Shopify API. The Engine provides a SessionsController and all the required code for authenticating with a shop via OAuth (other authentication methods are not supported).
36
+
37
+ *Note: It's recommended to use this on a new Rails project so that the generator won't overwrite/delete your files.*
38
+
39
+ Learn how to create and deploy a new Shopify App to Heroku with our [quickstart guide](https://github.com/Shopify/shopify_app/blob/master/docs/Quickstart.md), or dive in in less than 5 minutes with this quickstart video:
40
+
41
+ [https://www.youtube.com/watch?v=yGxeoAHlQOg](https://www.youtube.com/watch?v=yGxeoAHlQOg)
42
+
43
+ Become a Shopify App Developer
44
+ --------------------------------
45
+ To become a Shopify App Developer, you'll need a [Shopify Partner account.](http://shopify.com/partners) If you don't have a Shopify Partner account, head to http://shopify.com/partners to create one before you start.
46
+
47
+ Once you have a Partner account, [create a new application in the Partner Dashboard](https://help.shopify.com/en/api/tools/partner-dashboard/your-apps) to get an API key and other API credentials.
48
+
49
+ To create an application for development set your new app's `App URL` to the URL provided by [your tunnel](#app-tunneling), ensuring that you use `https://`. If you are not planning to embed your app inside the Shopify admin or receive webhooks, set your redirect URL to `http://localhost:3000/` and the `Whitelisted redirection URL(s)` to contain `<App URL>/auth/shopify/callback`.
50
+
51
+ Installation
52
+ ------------
53
+ To get started, add `shopify_app` to your Gemfile and run `bundle install`:
54
+
55
+ ``` sh
56
+ # Create a new rails app
57
+ $ rails new my_shopify_app
58
+ $ cd my_shopify_app
59
+
60
+ # Add the gem shopify_app to your Gemfile
61
+ $ bundle add shopify_app
62
+ ```
63
+
64
+ Now we are ready to run any of the [generators](#generators) included with `shopify_app`. The following section explains the generators and what you can do with them.
65
+
66
+
67
+ #### Rails Compatibility
68
+
69
+ The latest version of shopify_app is compatible with Rails `>= 5`. Use version `<= v7.2.8` if you need to work with Rails 4.
70
+
71
+
72
+ Generators
73
+ ----------
74
+
75
+ ### Default Generator
76
+
77
+ The default generator will run the `install`, `shop`, and `home_controller` generators. This is the recommended way to start a new app from scratch:
78
+
79
+ ```sh
80
+ $ rails generate shopify_app
81
+ ```
82
+
83
+ After running the generator, you will need to run `rails db:migrate` to add new tables to your database. You can start your app with `bundle exec rails server` and install your app by visiting `http://localhost` in your web browser.
84
+
85
+ ### API Keys
86
+
87
+ The default and install generators have been updated to source Shopify API key and secret from an Environment (`.env`) variables file, which you will need to create with the following format:
88
+
89
+ ```
90
+ SHOPIFY_API_KEY=your api key
91
+ SHOPIFY_API_SECRET=your api secret
92
+ ```
93
+
94
+ These values can be found on the "App Setup" page in the [Shopify Partners Dashboard][dashboard]. If you are checking your code into a code repository, ensure your `.gitignore` prevents your `.env` file from being checked into any publicly accessible code.
95
+
96
+ **You will need to load the ENV variables into your environment, you can do this with the [dot-env](https://github.com/bkeepers/dotenv) gem or any other method you wish to.**
97
+
98
+ ### Install Generator
99
+
100
+ ```sh
101
+ $ rails generate shopify_app:install
102
+ ```
103
+
104
+ Options include:
105
+ * `application_name` - the name of your app, it can be supplied with or without double-quotes if a whitespace is present. (e.g. `--application_name Example App` or `--application_name "Example App"`)
106
+ * `scope` - the OAuth access scope required for your app, e.g. **read_products, write_orders**. *Multiple options* need to be delimited by a comma-space and can be supplied with or without double-quotes
107
+ (e.g. `--scope read_products, write_orders, write_products` or `--scope "read_products, write_orders, write_products"`)
108
+ For more information, refer the [docs](http://docs.shopify.com/api/tutorials/oauth).
109
+ * `embedded` - the default is to generate an [embedded app](http://docs.shopify.com/embedded-app-sdk), if you want a legacy non-embedded app then set this to false, `--embedded false`
110
+
111
+ You can update any of these settings later on easily; the arguments are simply for convenience.
112
+
113
+ The generator adds ShopifyApp and the required initializers to the host Rails application.
114
+
115
+ After running the `install` generator, you can start your app with `bundle exec rails server` and install your app by visiting localhost.
116
+
117
+
118
+ ### Home Controller Generator
119
+
120
+ ```sh
121
+ $ rails generate shopify_app:home_controller
122
+ ```
123
+
124
+ This generator creates an example home controller and view which fetches and displays products using the Shopify API.
125
+
126
+
127
+ ### App Proxy Controller Generator
128
+
129
+ ```sh
130
+ $ rails generate shopify_app:app_proxy_controller
131
+ ```
132
+
133
+ This optional generator, not included with the default generator, creates the app proxy controller to handle proxy requests to the app from your shop storefront, modifies 'config/routes.rb' with a namespace route, and an example view which displays current shop information using the LiquidAPI.
134
+
135
+ ### Marketing Extension Generator
136
+
137
+ ```sh
138
+ $ rails generate shopify_app:add_marketing_activity_extension
139
+ ```
140
+
141
+ This will create a controller with the endpoints required to build a [marketing activities extension](https://help.shopify.com/en/api/embedded-apps/app-extensions/shopify-admin/marketing-activities). The extension will be generated with a base URL at `/marketing_activities`, which should also be configured in partners.
142
+
143
+ ### Controllers, Routes and Views
144
+
145
+ The last group of generators are for your convenience if you want to start overriding code included as part of the Rails engine. For example, by default the engine provides a simple SessionController, if you run the `rails generate shopify_app:controllers` generator then this code gets copied out into your app so you can start adding to it. Routes and views follow the exact same pattern.
146
+
147
+ Mounting the Engine
148
+ -------------------
149
+
150
+ Mounting the Engine will provide the basic routes to authenticating a shop with your application. By default it will provide:
151
+
152
+ | Verb | Route | Action |
153
+ |--------|-------------------------------|------------------------------|
154
+ |GET |'/login' |Login |
155
+ |POST |'/login' |Login |
156
+ |GET |'/auth/shopify/callback' |Authenticate Callback |
157
+ |GET |'/logout' |Logout |
158
+ |POST |'/webhooks/:type' |Webhook Callback |
159
+
160
+ ### Nested Routes
161
+
162
+ The engine may also be mounted at a nested route, for example:
163
+
164
+ ```ruby
165
+ mount ShopifyApp::Engine, at: '/nested'
166
+ ```
167
+
168
+ This will create the Shopify engine routes under the specified subpath. You'll also need to make some updates to your `shopify_app.rb` and `omniauth.rb` initializers. First, update the shopify_app initializer to include a custom `root_url` e.g.:
169
+
170
+ ```ruby
171
+ ShopifyApp.configure do |config|
172
+ config.root_url = '/nested'
173
+ end
174
+ ```
175
+
176
+ then update the omniauth initializer to include a custom `callback_path` e.g.:
177
+
178
+ ```ruby
179
+ provider :shopify,
180
+ ShopifyApp.configuration.api_key,
181
+ ShopifyApp.configuration.secret,
182
+ scope: ShopifyApp.configuration.scope,
183
+ callback_path: '/nested/auth/shopify/callback'
184
+ ```
185
+
186
+ You may also need to change your `config/routes.rb` to render a view for `/nested`, since this is what will be rendered in the Shopify Admin of any shops that have installed your app. The engine itself doesn't have a view for this, so you'll need something like this:
187
+
188
+ ```ruby
189
+ # config/routes.rb
190
+ Rails.application.routes.draw do
191
+ root :to => 'something_else#index'
192
+ get "/nested", to: "home#index"
193
+ mount ShopifyApp::Engine, at: '/nested'
194
+ end
195
+ ```
196
+
197
+ Finally, note that if you do this, to add your app to a store, you must navigate to `/nested` in order to render the `Enter your shop domain to log in or install this app.` UI.
198
+
199
+ ### Custom login URL
200
+
201
+ While you can customize the login view by creating a `/app/views/shopify_app/sessions/new.html.erb` file, you may also want to customize the URL entirely. You can modify your `shopify_app.rb` initializer to provide a custom `login_url` e.g.:
202
+
203
+ ```ruby
204
+ ShopifyApp.configure do |config|
205
+ config.login_url = 'https://my.domain.com/nested/login'
206
+ end
207
+ ```
208
+
209
+ Authentication
210
+ --------------
211
+
212
+ ### Callback
213
+
214
+ Upon completing the authentication flow, Shopify calls the app at the `callback_path` mentioned before. If the app needs to do some extra work, it can define and configure the route to a custom callback controller, inheriting from `ShopifyApp::CallbackController` and hook into or override any of the defined helper methods. The default callback controller already provides the following behaviour:
215
+ * Logging into the shop and resetting the session
216
+ * [Installing Webhooks](https://github.com/Shopify/shopify_app#webhooksmanager)
217
+ * [Setting Scripttags](https://github.com/Shopify/shopify_app#scripttagsmanager)
218
+ * [Performing the AfterAuthenticate Job](https://github.com/Shopify/shopify_app#afterauthenticatejob)
219
+ * Redirecting to the return address
220
+
221
+ **Note that starting with version 8.4.0, we have extracted the callback logic in its own controller. If you are upgrading from a version older than 8.4.0 the callback action and related helper methods were defined in `ShopifyApp::SessionsController` ==> you will have to extend `ShopifyApp::CallbackController` instead and port your logic to the new controller.**
222
+
223
+ ### ShopifyApp::SessionRepository
224
+
225
+ `ShopifyApp::SessionRepository` allows you as a developer to define how your sessions are stored and retrieved for shops. The `SessionRepository` is configured in the `config/initializers/shopify_app.rb` file and can be set to any object that implements `self.store(auth_session, *args)` which stores the session and returns a unique identifier and `self.retrieve(id)` which returns a `ShopifyAPI::Session` for the passed id. These methods are already implemented as part of the `ShopifyApp::SessionStorage` concern but can be overridden for custom implementation.
226
+
227
+ #### Shop-based token storage
228
+ Storing tokens on the store model means that any user login associated with the store will have equal access levels to whatever the original user granted the app.
229
+ ```sh
230
+ $ rails generate shopify_app:shop_model
231
+ ```
232
+ This will generate a shop model which will be the storage for the tokens necessary for authentication.
233
+
234
+ #### User-based token storage
235
+ A more granular control over the level of access per user on an app might be necessary, to which the shop-based token strategy is not sufficient. Shopify supports a user-based token storage strategy where a unique token to each user can be managed. Shop tokens must still be maintained if you are running background jobs so that you can make use of them when necessary.
236
+ ```sh
237
+ $ rails generate shopify_app:shop_model
238
+ $ rails generate shopify_app:user_model
239
+ ```
240
+ This will generate a shop model and user model, which will be the storage for the tokens necessary for authentication.
241
+
242
+ The current Shopify user will be stored in the rails session at `session[:shopify_user]`
243
+
244
+ Read more about Online vs. Offline access [here](https://help.shopify.com/api/getting-started/authentication/oauth).
245
+
246
+ #### Migrating from shop-based to user-based token strategy
247
+ 1. Run the `user_model` generator as mentioned above.
248
+ 2. Ensure that both your `Shop` model and `User` model includes the necessary concerns `ShopifyApp::ShopSessionStorage` and `ShopifyApp::UserSessionStorage`.
249
+ 3. Make changes to 2 initializer files as shown below:
250
+ ```ruby
251
+ # In the `omniauth.rb` initializer:
252
+ provider :shopify,
253
+ ...
254
+ setup: lambda { |env|
255
+ ...
256
+ # Add this line
257
+ strategy.options[:per_user_permissions] = strategy.session[:user_tokens]
258
+ ...
259
+ }
260
+
261
+ # In the `shopify_app.rb` initializer:
262
+ config.shop_session_repository = {YOUR_SHOP_MODEL_CLASS}
263
+ config.user_session_repository = {YOUR_USER_MODEL_CLASS}
264
+ ```
265
+
266
+ ### Authenticated
267
+
268
+ The engine provides a `ShopifyApp::Authenticated` concern which should be included in any controller that is intended to be behind Shopify OAuth. It adds `before_action`s to ensure that the user is authenticated and will redirect to the Shopify login page if not. It is best practice to include this concern in a base controller inheriting from your `ApplicationController`, from which all controllers that require Shopify authentication inherit.
269
+
270
+ For backwards compatibility, the engine still provides a controller called `ShopifyApp::AuthenticatedController` which includes the `ShopifyApp::Authenticated` concern. Note that it inherits directly from `ActionController::Base`, so you will not be able to share functionality between it and your application's `ApplicationController`.
271
+
272
+ ### AfterAuthenticate Job
273
+
274
+ If your app needs to perform specific actions after the user is authenticated successfully (i.e. every time a new session is created), ShopifyApp can queue or run a job of your choosing (note that we already provide support for automatically creating Webhooks and Scripttags). To configure the after authenticate job, update your initializer as follows:
275
+
276
+ ```ruby
277
+ ShopifyApp.configure do |config|
278
+ config.after_authenticate_job = { job: "Shopify::AfterAuthenticateJob" }
279
+ end
280
+ ```
281
+
282
+ The job can be configured as either a class or a class name string.
283
+
284
+ If you need the job to run synchronously add the `inline` flag:
285
+
286
+ ```ruby
287
+ ShopifyApp.configure do |config|
288
+ config.after_authenticate_job = { job: Shopify::AfterAuthenticateJob, inline: true }
289
+ end
290
+ ```
291
+
292
+ We've also provided a generator which creates a skeleton job and updates the initializer for you:
293
+
294
+ ```
295
+ bin/rails g shopify_app:add_after_authenticate_job
296
+ ```
297
+
298
+ If you want to perform that action only once, e.g. send a welcome email to the user when they install the app, you should make sure that this action is idempotent, meaning that it won't have an impact if run multiple times.
299
+
300
+ API Versioning
301
+ --------------
302
+
303
+ Shopify's API is versioned, and you can [read about that process in the Shopify Developers documentation page](https://shopify.dev/concepts/about-apis/versioning).
304
+
305
+ Since shopify_app gem version 1.11.0, the included shopify_api gem has also been updated to allow you to easily set and switch what version of the Shopify API you want your app or service to use, as well as surface warnings to Rails apps about [deprecated endpoints, GraphQL fields and more](https://shopify.dev/concepts/about-apis/versioning#deprecation-practices).
306
+
307
+ See the [shopify_api gem README](https://github.com/Shopify/shopify_api/) for more details.
308
+
309
+ WebhooksManager
310
+ ---------------
311
+
312
+ ShopifyApp can manage your app's webhooks for you if you set which webhooks you require in the initializer:
313
+
314
+ ```ruby
315
+ ShopifyApp.configure do |config|
316
+ config.webhooks = [
317
+ {topic: 'carts/update', address: 'https://example-app.com/webhooks/carts_update'}
318
+ ]
319
+ end
320
+ ```
321
+
322
+ When the OAuth callback is completed successfully, ShopifyApp will queue a background job which will ensure all the specified webhooks exist for that shop. Because this runs on every OAuth callback, it means your app will always have the webhooks it needs even if the user uninstalls and re-installs the app.
323
+
324
+ ShopifyApp also provides a WebhooksController that receives webhooks and queues a job based on the received topic. For example, if you register the webhook from above, then all you need to do is create a job called `CartsUpdateJob`. The job will be queued with 2 params: `shop_domain` and `webhook` (which is the webhook body).
325
+
326
+ If you would like to namespace your jobs, you may set `webhook_jobs_namespace` in the config. For example, if your app handles webhooks from other ecommerce applications as well, and you want Shopify cart update webhooks to be processed by a job living in `jobs/shopify/webhooks/carts_update_job.rb` rather than `jobs/carts_update_job.rb`):
327
+
328
+ ```ruby
329
+ ShopifyApp.configure do |config|
330
+ config.webhook_jobs_namespace = 'shopify/webhooks'
331
+ end
332
+ ```
333
+
334
+ If you are only interested in particular fields, you can optionally filter the data sent by Shopify by specifying the `fields` parameter in `config/webhooks`. Note that you will still receive a webhook request from Shopify every time the resource is updated, but only the specified fields will be sent.
335
+
336
+ ```ruby
337
+ ShopifyApp.configure do |config|
338
+ config.webhooks = [
339
+ {topic: 'products/update', address: 'https://example-app.com/webhooks/products_update', fields: ['title', 'vendor']}
340
+ ]
341
+ end
342
+ ```
343
+
344
+ If you'd rather implement your own controller then you'll want to use the WebhookVerification module to verify your webhooks, example:
345
+
346
+ ```ruby
347
+ class CustomWebhooksController < ApplicationController
348
+ include ShopifyApp::WebhookVerification
349
+
350
+ def carts_update
351
+ params.permit!
352
+ SomeJob.perform_later(shop_domain: shop_domain, webhook: webhook_params.to_h)
353
+ head :no_content
354
+ end
355
+
356
+ private
357
+
358
+ def webhook_params
359
+ params.except(:controller, :action, :type)
360
+ end
361
+ end
362
+ ```
363
+
364
+ The module skips the `verify_authenticity_token` before_action and adds an action to verify that the webhook came from Shopify. You can now add a post route to your application, pointing to the controller and action to accept the webhook data from Shopify.
365
+
366
+ The WebhooksManager uses ActiveJob. If ActiveJob is not configured then by default Rails will run the jobs inline. However, it is highly recommended to configure a proper background processing queue like Sidekiq or Resque in production.
367
+
368
+ ShopifyApp can create webhooks for you using the `add_webhook` generator. This will add the new webhook to your config and create the required job class for you.
369
+
370
+ ```
371
+ rails g shopify_app:add_webhook -t carts/update -a https://example.com/webhooks/carts_update
372
+ ```
373
+
374
+ Where `-t` is the topic and `-a` is the address the webhook should be sent to.
375
+
376
+ ScripttagsManager
377
+ -----------------
378
+
379
+ As with webhooks, ShopifyApp can manage your app's scripttags for you by setting which scripttags you require in the initializer:
380
+
381
+ ```ruby
382
+ ShopifyApp.configure do |config|
383
+ config.scripttags = [
384
+ {event:'onload', src: 'https://my-shopifyapp.herokuapp.com/fancy.js'},
385
+ {event:'onload', src: ->(domain) { dynamic_tag_url(domain) } }
386
+ ]
387
+ end
388
+ ```
389
+
390
+ You also need to have write_script_tags permission in the config scope in order to add script tags automatically:
391
+
392
+ ```ruby
393
+ config.scope = '... , write_script_tags'
394
+ ```
395
+
396
+ Scripttags are created in the same way as the Webhooks, with a background job which will create the required scripttags.
397
+
398
+ If `src` responds to `call` its return value will be used as the scripttag's source. It will be called on scripttag creation and deletion.
399
+
400
+ RotateShopifyTokenJob
401
+ ---------------------
402
+
403
+ If your Shopify secret key is leaked, you can use the RotateShopifyTokenJob to perform [API Credential Rotation](https://help.shopify.com/en/api/getting-started/authentication/oauth/api-credential-rotation).
404
+
405
+ Before running the job, you'll need to generate a new secret key from your Shopify Partner dashboard, and update the `/config/initializers/shopify_app.rb` to hold your new and old secret keys:
406
+
407
+ ```ruby
408
+ config.secret = Rails.application.secrets.shopify_secret
409
+ config.old_secret = Rails.application.secrets.old_shopify_secret
410
+ ```
411
+
412
+ We've provided a generator which creates the job and an example rake task:
413
+
414
+ ```sh
415
+ bin/rails g shopify_app:rotate_shopify_token_job
416
+ ```
417
+
418
+ The generated rake task will be found at `lib/tasks/shopify/rotate_shopify_token.rake` and is provided strictly for example purposes. It might not work with your application out of the box without some configuration.
419
+
420
+ ⚠️ Note: if you are updating `shopify_app` from a version prior to 8.4.2 (and do not wish to run the default/install generator again), you will need to add [the following line](https://github.com/Shopify/shopify_app/blob/4f7e6cca2a472d8f7af44b938bd0fcafe4d8e88a/lib/generators/shopify_app/install/templates/shopify_provider.rb#L18) to `config/intializers/omniauth.rb`:
421
+
422
+ ```ruby
423
+ strategy.options[:old_client_secret] = ShopifyApp.configuration.old_secret
424
+ ```
425
+
426
+ App Tunneling
427
+ -------------
428
+
429
+ Your local app needs to be accessible from the public Internet in order to install it on a Shopify store, to use the [App Proxy Controller](#app-proxy-controller-generator) or receive Webhooks.
430
+
431
+ Use a tunneling service like [ngrok](https://ngrok.com/), [Forward](https://forwardhq.com/), [Beeceptor](https://beeceptor.com/), [Mockbin](http://mockbin.org/), or [Hookbin](https://hookbin.com/) to make your development environment accessible to the internet.
432
+
433
+ For example with [ngrok](https://ngrok.com/), run this command to set up a tunnel proxy to Rails' default port:
434
+
435
+ ```sh
436
+ ngrok http 3000
437
+ ```
438
+
439
+ AppProxyVerification
440
+ --------------------
441
+
442
+ The engine provides a mixin for verifying incoming HTTP requests sent via an App Proxy. Any controller that `include`s `ShopifyApp::AppProxyVerification` will verify that each request has a valid `signature` query parameter that is calculated using the other query parameters and the app's shared secret.
443
+
444
+ ### Recommended Usage
445
+
446
+ The App Proxy Controller Generator automatically adds the mixin to the generated app_proxy_controller.rb
447
+ Additional controllers for resources within the App_Proxy namespace, will need to include the mixin like so:
448
+
449
+ ```ruby
450
+ # app/controllers/app_proxy/reviews_controller.rb
451
+ class ReviewsController < ApplicationController
452
+ include ShopifyApp::AppProxyVerification
453
+ # ...
454
+ end
455
+ ```
456
+
457
+ Create your app proxy URL in the [Shopify Partners' Dashboard][dashboard], making sure to point it to `https://your_app_website.com/app_proxy`.
458
+ ![Creating an App Proxy](/images/app-proxy-screenshot.png)
459
+
460
+ App Bridge
461
+ ---
462
+
463
+ A basic example of using [App Bridge][app-bridge] is included in the install generator. An app instance is automatically initialized in [shopify_app.js](https://github.com/Shopify/shopify_app/blob/master/lib/generators/shopify_app/install/templates/shopify_app.js) and [flash_messages.js](https://github.com/Shopify/shopify_app/blob/master/lib/generators/shopify_app/install/templates/flash_messages.js) converts Rails [flash messages](https://api.rubyonrails.org/classes/ActionDispatch/Flash.html) to App Bridge Toast actions automatically. By default, this library is included via [unpkg in the embedded_app layout](https://github.com/Shopify/shopify_app/blob/master/lib/generators/shopify_app/install/templates/embedded_app.html.erb#L27). For more advanced uses it is recommended to [install App Bridge via npm or yarn](https://help.shopify.com/en/api/embedded-apps/app-bridge/getting-started#set-up-shopify-app-bridge-in-your-app).
464
+
465
+ Troubleshooting
466
+ ---------------
467
+
468
+ see [TROUBLESHOOTING.md](https://github.com/Shopify/shopify_app/blob/master/docs/Troubleshooting.md)
469
+
470
+ Using Test Helpers inside your Application
471
+ -----------------------------------------
472
+
473
+ A test helper that will allow you to test `ShopifyApp::WebhookVerification` in the controller from your app, to use this test, you need to `require` it directly inside your app `test/controllers/webhook_verification_test.rb`.
474
+
475
+ ```ruby
476
+ require 'test_helper'
477
+ require 'action_controller'
478
+ require 'action_controller/base'
479
+ require 'shopify_app/test_helpers/webhook_verification_helper'
480
+ ```
481
+
482
+ Or you can require in your `test/test_helper.rb`.
483
+
484
+ ```ruby
485
+ ENV['RAILS_ENV'] ||= 'test'
486
+ require_relative '../config/environment'
487
+ require 'rails/test_help'
488
+ require 'byebug'
489
+ require 'shopify_app/test_helpers/all'
490
+ ```
491
+
492
+ With `lib/shopify_app/test_helpers/all'` more tests can be added and will only need to be required in once in your library.
493
+
494
+
495
+ Testing an embedded app outside the Shopify admin
496
+ -------------------------------------------------
497
+
498
+ By default, loading your embedded app will redirect to the Shopify admin, with the app view loaded in an `iframe`. If you need to load your app outside of the Shopify admin (e.g., for performance testing), you can change `forceRedirect: true` to `false` in `ShopifyApp.init` block in the `embedded_app` view. To keep the redirect on in production but off in your `development` and `test` environments, you can use:
499
+
500
+ ```javascript
501
+ forceRedirect: <%= Rails.env.development? || Rails.env.test? ? 'false' : 'true' %>
502
+ ```
503
+
504
+ Migrating to 13.0.0
505
+ -------------------
506
+
507
+ Version 13.0.0 adds the ability to use both user and shop sessions, concurrently. This however involved a large
508
+ change to how session stores work. Here are the steps to migrate to 13.x
509
+
510
+ ### Changes to `config/initializers/shopify_app.rb`
511
+ - *REMOVE* `config.per_user_tokens = [true|false]` this is no longer needed
512
+ - *CHANGE* `config.session_repository = 'Shop'` To `config.shop_session_repository = 'Shop'`
513
+ - *ADD (optional)* User Session Storage `config.user_session_repository = 'User'`
514
+
515
+ ### Shop Model Changes (normally `app/models/shop.rb`)
516
+ - *CHANGE* `include ShopifyApp::SessionStorage` to `include ShopifyApp::ShopSessionStorage`
517
+
518
+ ### Changes to the @shop_session instance variable (normally in `app/controllers/*.rb`)
519
+ - *CHANGE* if you are using shop sessions, `@shop_session` will need to be changed to `@current_shopify_session`.
520
+
521
+ ### Changes to Rails `session`
522
+ - *CHANGE* `session[:shopify]` is no longer set. Use `session[:user_id]` if your app uses user based tokens, or `session[:shop_id]` if your app uses shop based tokens.
523
+
524
+ ### Changes to `ShopifyApp::LoginProtection`
525
+ `ShopifyApp::LoginProtection`
526
+
527
+ - CHANGE if you are using `ShopifyApp::LoginProtection#shopify_session` in your code, it will need to be
528
+ changed to `ShopifyApp::LoginProtection#activate_shopify_session`
529
+ - CHANGE if you are using `ShopifyApp::LoginProtection#clear_shop_session` in your code, it will need to be
530
+ changed to `ShopifyApp::LoginProtection#clear_shopify_session`
531
+
532
+ ### Notes
533
+ You do not need a user model; a shop session is fine for most applications.
534
+
535
+ Questions or problems?
536
+ ----------------------
537
+
538
+ - [Ask questions!](https://ecommerce.shopify.com/c/shopify-apis-and-technology)
539
+ - [Read the docs!](https://help.shopify.com/api/guides)
540
+ - And don't forget to check the [Changelog](https://github.com/Shopify/shopify_app/blob/master/CHANGELOG.md) too!
541
+
542
+ Upgrading to 11.7.0
543
+ ---------------------------
544
+
545
+ ### Session storage method signature breaking change
546
+ If you override `def self.store(auth_session)` method in your session storage model (e.g. Shop), the method signature has changed to `def self.store(auth_session, *args)` in order to support user-based token storage. Please update your method signature to include the second argument.
547
+
548
+ Rails 6 Compatibility
549
+ ---------------------
550
+
551
+ ### Disable Webpacker
552
+ If you are using sprockets in rails 6 or want to generate a shopify_app without webpacker run the install task by running
553
+
554
+ ```
555
+ SHOPIFY_APP_DISABLE_WEBPACKER=1 rails generate shopify_app
556
+ ```
557
+
558
+ and then in your ShopifyApp configuration block, add
559
+
560
+ ```
561
+ ShopifyApp.configure do |config|
562
+ config.disable_webpacker = true
563
+ end
564
+ ```
565
+
566
+ Upgrading from 8.6 to 9.0.0
567
+ ---------------------------
568
+
569
+ ### Configuration change
570
+
571
+ Add an API version configuration in `config/initializers/shopify_app.rb`
572
+ Set this to the version you want to run against by default. See [Shopify API docs](https://help.shopify.com/en/api/versioning) for versions available.
573
+ ```ruby
574
+ config.api_version = '2019-04'
575
+ ```
576
+
577
+ ### Session storage change
578
+
579
+ You will need to add an `api_version` method to your session storage object. The default implementation for this is.
580
+ ```ruby
581
+ def api_version
582
+ ShopifyApp.configuration.api_version
583
+ end
584
+ ```
585
+
586
+ ### Generated file change
587
+
588
+ `embedded_app.html.erb` the usage of `shop_session.url` needs to be changed to `shop_session.domain`
589
+ ```erb
590
+ <script type="text/javascript">
591
+ ShopifyApp.init({
592
+ apiKey: "<%= ShopifyApp.configuration.api_key %>",
593
+
594
+ shopOrigin: "<%= "https://#{ @shop_session.url }" if @shop_session %>",
595
+
596
+ debug: false,
597
+ forceRedirect: true
598
+ });
599
+ </script>
600
+ ```
601
+ is changed to
602
+ ```erb
603
+ <script type="text/javascript">
604
+ ShopifyApp.init({
605
+ apiKey: "<%= ShopifyApp.configuration.api_key %>",
606
+
607
+ shopOrigin: "<%= "https://#{ @shop_session.domain }" if @shop_session %>",
608
+
609
+ debug: false,
610
+ forceRedirect: true
611
+ });
612
+ </script>
613
+ ```
614
+
615
+ ### ShopifyAPI changes
616
+
617
+ You will need to also follow the ShopifyAPI [upgrade guide](https://github.com/Shopify/shopify_api/blob/master/README.md#-breaking-change-notice-for-version-700-) to ensure your app is ready to work with API versioning.
618
+
619
+ [dashboard]:https://partners.shopify.com
620
+ [app-bridge]:https://help.shopify.com/en/api/embedded-apps/app-bridge