shopify_app 11.4.0 → 11.7.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +0 -2
- data/CHANGELOG.md +25 -0
- data/README.md +123 -115
- data/app/controllers/concerns/shopify_app/authenticated.rb +1 -1
- data/app/controllers/shopify_app/callback_controller.rb +8 -2
- data/app/controllers/shopify_app/extension_verification_controller.rb +20 -0
- data/config/locales/nl.yml +1 -1
- data/docs/Quickstart.md +44 -16
- data/docs/install-on-dev-shop.png +0 -0
- data/docs/test-your-app.png +0 -0
- data/lib/generators/shopify_app/add_marketing_activity_extension/templates/marketing_activities_controller.rb +1 -1
- data/lib/generators/shopify_app/install/install_generator.rb +0 -4
- data/lib/generators/shopify_app/install/templates/shopify_app.rb +1 -1
- data/lib/generators/shopify_app/install/templates/shopify_provider.rb +1 -0
- data/lib/generators/shopify_app/user_model/templates/db/migrate/create_users.erb +16 -0
- data/lib/generators/shopify_app/user_model/templates/user.rb +7 -0
- data/lib/generators/shopify_app/user_model/templates/users.yml +4 -0
- data/lib/generators/shopify_app/user_model/user_model_generator.rb +38 -0
- data/lib/shopify_app.rb +5 -3
- data/lib/shopify_app/configuration.rb +13 -8
- data/lib/shopify_app/controller_concerns/login_protection.rb +22 -3
- data/lib/shopify_app/engine.rb +4 -0
- data/lib/shopify_app/middleware/same_site_cookie_middleware.rb +60 -0
- data/lib/shopify_app/session/in_memory_session_store.rb +1 -1
- data/lib/shopify_app/session/session_repository.rb +2 -2
- data/lib/shopify_app/session/session_storage.rb +10 -22
- data/lib/shopify_app/session/storage_strategies/shop_storage_strategy.rb +23 -0
- data/lib/shopify_app/session/storage_strategies/user_storage_strategy.rb +24 -0
- data/lib/shopify_app/version.rb +1 -1
- data/package-lock.json +33 -35
- data/package.json +3 -2
- data/service.yml +1 -1
- data/shopify_app.gemspec +4 -1
- data/yarn.lock +14 -14
- metadata +54 -3
- data/lib/shopify_app/controllers/extension_verification_controller.rb +0 -17
data/docs/Quickstart.md
CHANGED
@@ -1,11 +1,13 @@
|
|
1
1
|
Quickstart
|
2
2
|
==========
|
3
3
|
|
4
|
-
|
4
|
+
Get started building and deploying a new Shopify App to Heroku in just a few minutes. This guide assumes you have Ruby/Rails installed on your computer already; if you haven't done that already start with [this guide.](https://guides.rubyonrails.org/v5.0/getting_started.html#installing-rails)
|
5
5
|
|
6
6
|
1. New Rails App (with postgres)
|
7
7
|
--------------------------------
|
8
8
|
|
9
|
+
To create a new Rails app and use this generator, open your terminal and run the following commands:
|
10
|
+
|
9
11
|
```sh
|
10
12
|
$ rails new test-app --database=postgresql
|
11
13
|
$ cd test-app
|
@@ -17,43 +19,51 @@ $ git commit -m 'new rails app'
|
|
17
19
|
2. Create a new Heroku app
|
18
20
|
--------------------------
|
19
21
|
|
20
|
-
The next step is to create a new
|
22
|
+
The next step is to create a new Heroku app to host your application. If you haven't got a Heroku account yet, create a free account [here](https://www.heroku.com/).
|
23
|
+
|
24
|
+
Head to the Heroku dashboard and create a new app, or run the following commands with the [Heroku CLI](https://devcenter.heroku.com/articles/heroku-cli#download-and-install) installed, substituting `name` for the name of your own app:
|
21
25
|
|
22
|
-
|
26
|
+
CLI:
|
23
27
|
```sh
|
24
28
|
$ heroku create name
|
25
29
|
$ heroku git:remote -a name
|
26
30
|
```
|
27
31
|
|
28
|
-
|
32
|
+
Once you have created an app on Heroku, we need to let Git know where the Heroku server is so we can deploy to it later. Copy the app's name from your Heroku dashboard and substitute `appname.git` with the name you chose earlier:
|
29
33
|
|
30
34
|
web:
|
31
35
|
```sh
|
32
36
|
# https://dashboard.heroku.com/new
|
33
|
-
$ git remote add heroku git@heroku.com:
|
37
|
+
$ git remote add heroku git@heroku.com:appname.git
|
34
38
|
```
|
35
39
|
|
36
|
-
3. Create a new App in the
|
40
|
+
3. Create a new App in the Shopify Partner dashboard
|
37
41
|
-----------------------------------------
|
42
|
+
* Create a Shopify app in the [Partners dashboard](https://partner.shopify.com). For this tutorial, you can choose either a public or custom app, but you can [learn about App Types here.](https://help.shopify.com/en/manual/apps/app-types)
|
38
43
|
[https://app.shopify.com/services/partners/api_clients](https://app.shopify.com/services/partners/api_clients)
|
39
|
-
*
|
40
|
-
*
|
41
|
-
*
|
42
|
-
|
44
|
+
* Set the callback url to `https://<appname>.herokuapp.com/`
|
45
|
+
* Choose an embedded app
|
46
|
+
* Set the app's `redirect_uri` to `https://<appname>.herokuapp.com/auth/shopify/callback`
|
43
47
|
|
44
|
-
4. Add ShopifyApp to
|
48
|
+
4. Add ShopifyApp to Gemfile
|
45
49
|
----------------------------
|
50
|
+
|
51
|
+
Run these commands to add the `shopify_app` Gem to your app:
|
52
|
+
|
46
53
|
```sh
|
47
54
|
$ echo "gem 'shopify_app'" >> Gemfile
|
48
55
|
$ bundle install
|
49
56
|
```
|
50
57
|
|
51
|
-
Note
|
58
|
+
**Note:** we recommend using the latest version of Shopify Gem. Check the [Git tags](https://github.com/Shopify/shopify_app/tags) to see the latest release version and then add it to your Gemfile e.g `gem 'shopify_app', '~> 7.0.0'`
|
52
59
|
|
53
60
|
5. Run the ShopifyApp generator
|
54
61
|
-------------------------------
|
62
|
+
|
63
|
+
Generate the code for your app by running these commands:
|
64
|
+
|
55
65
|
```sh
|
56
|
-
#
|
66
|
+
# Use the keys from your app you created in the partners area
|
57
67
|
$ rails generate shopify_app --api_key <shopify_api_key> --secret <shopify_api_secret>
|
58
68
|
$ git add .
|
59
69
|
$ git commit -m 'generated shopify app'
|
@@ -61,10 +71,12 @@ $ git commit -m 'generated shopify app'
|
|
61
71
|
|
62
72
|
If you forget to set your keys or redirect uri above, you will find them in the shopify_app initializer at: `/config/initializers/shopify_app.rb`.
|
63
73
|
|
64
|
-
We recommend adding a gem or utilizing
|
74
|
+
We recommend adding a gem or utilizing environment variables (`.env`) to handle your keys before releasing your app. [Learn more about using environment variables.](https://www.honeybadger.io/blog/ruby-guide-environment-variables/)
|
65
75
|
|
66
|
-
6. Deploy
|
76
|
+
6. Deploy your app
|
67
77
|
---------
|
78
|
+
|
79
|
+
Once you've generated your app, push it into your Heroku environment to see it up and running:
|
68
80
|
```sh
|
69
81
|
$ git push heroku
|
70
82
|
$ heroku run rake db:migrate
|
@@ -72,4 +84,20 @@ $ heroku run rake db:migrate
|
|
72
84
|
|
73
85
|
7. Install the App!
|
74
86
|
-------------------
|
75
|
-
|
87
|
+
|
88
|
+
Ensure you have created a [development store](https://help.shopify.com/en/api/getting-started/making-your-first-request#create-a-development-store) using the Shopify Partner Dashboard. If you don't already have one, [create one by following these instructions](https://help.shopify.com/en/api/getting-started/making-your-first-request#create-a-development-store).
|
89
|
+
|
90
|
+
##### Note: The following step will cause your store to become `transfer-disabled.` Read more about store transfer and why it's important [here](https://help.shopify.com/en/api/guides/store-transfers#transfer-disabled-stores). This is an irreversible change, so be sure you don't plan to transfer this store to a merchant.
|
91
|
+
|
92
|
+
Install the app onto your new development store using the Partner Dashboard. Log in to your account, visit the apps page, click the app you created earlier, and looking for the `Test your app` instructions where you can select a store to install your app on.
|
93
|
+
|
94
|
+
![Installing an app on the partners dashboard dropdown](/docs/install-on-dev-shop.png)
|
95
|
+
|
96
|
+
### OR
|
97
|
+
|
98
|
+
![Installing an app on the partners dashboard card](/docs/test-your-app.png)
|
99
|
+
|
100
|
+
8. Great work!
|
101
|
+
-------------------
|
102
|
+
|
103
|
+
You're done creating your first app on Shopify. Keep going and learn more by [diving into our full documentation](https://help.shopify.com/en/api/getting-started), or join our [community of developers.](https://community.shopify.com/c/Shopify-Apps/bd-p/shopify-apps)
|
Binary file
|
Binary file
|
@@ -11,10 +11,6 @@ module ShopifyApp
|
|
11
11
|
class_option :embedded, type: :string, default: 'true'
|
12
12
|
class_option :api_version, type: :string, default: nil
|
13
13
|
|
14
|
-
def add_dotenv_gem
|
15
|
-
gem('dotenv-rails', group: [:test, :development])
|
16
|
-
end
|
17
|
-
|
18
14
|
def create_shopify_app_initializer
|
19
15
|
@application_name = format_array_argument(options['application_name'])
|
20
16
|
@scope = format_array_argument(options['scope'])
|
@@ -8,7 +8,7 @@ ShopifyApp.configure do |config|
|
|
8
8
|
config.embedded_app = <%= embedded_app? %>
|
9
9
|
config.after_authenticate_job = false
|
10
10
|
config.api_version = "<%= @api_version %>"
|
11
|
-
config.session_repository = ShopifyApp::InMemorySessionStore
|
11
|
+
config.session_repository = 'ShopifyApp::InMemorySessionStore'
|
12
12
|
end
|
13
13
|
|
14
14
|
# ShopifyApp::Utils.fetch_known_api_versions # Uncomment to fetch known api versions from shopify servers on boot
|
@@ -0,0 +1,16 @@
|
|
1
|
+
class CreateUsers < ActiveRecord::Migration[<%= rails_migration_version %>]
|
2
|
+
def self.up
|
3
|
+
create_table :users do |t|
|
4
|
+
t.bigint :shopify_user_id, null: false
|
5
|
+
t.string :shopify_domain, null: false
|
6
|
+
t.string :shopify_token, null: false
|
7
|
+
t.timestamps
|
8
|
+
end
|
9
|
+
|
10
|
+
add_index :users, :shopify_user_id, unique: true
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.down
|
14
|
+
drop_table :users
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'rails/generators/base'
|
2
|
+
require 'rails/generators/active_record'
|
3
|
+
|
4
|
+
module ShopifyApp
|
5
|
+
module Generators
|
6
|
+
class UserModelGenerator < Rails::Generators::Base
|
7
|
+
include Rails::Generators::Migration
|
8
|
+
source_root File.expand_path('../templates', __FILE__)
|
9
|
+
|
10
|
+
def create_user_model
|
11
|
+
copy_file 'user.rb', 'app/models/user.rb'
|
12
|
+
end
|
13
|
+
|
14
|
+
def create_user_migration
|
15
|
+
migration_template 'db/migrate/create_users.erb', 'db/migrate/create_users.rb'
|
16
|
+
end
|
17
|
+
|
18
|
+
def update_shopify_app_initializer
|
19
|
+
gsub_file 'config/initializers/shopify_app.rb', 'ShopifyApp::InMemorySessionStore', 'User'
|
20
|
+
end
|
21
|
+
|
22
|
+
def create_user_fixtures
|
23
|
+
copy_file 'users.yml', 'test/fixtures/users.yml'
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def rails_migration_version
|
29
|
+
Rails.version.match(/\d\.\d/)[0]
|
30
|
+
end
|
31
|
+
|
32
|
+
# for generating a timestamp when using `create_migration`
|
33
|
+
def self.next_migration_number(dir)
|
34
|
+
ActiveRecord::Generators::Base.next_migration_number(dir)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
data/lib/shopify_app.rb
CHANGED
@@ -24,9 +24,6 @@ module ShopifyApp
|
|
24
24
|
# utils
|
25
25
|
require 'shopify_app/utils'
|
26
26
|
|
27
|
-
# controllers
|
28
|
-
require 'shopify_app/controllers/extension_verification_controller'
|
29
|
-
|
30
27
|
# controller concerns
|
31
28
|
require 'shopify_app/controller_concerns/localization'
|
32
29
|
require 'shopify_app/controller_concerns/itp'
|
@@ -43,7 +40,12 @@ module ShopifyApp
|
|
43
40
|
require 'shopify_app/managers/webhooks_manager'
|
44
41
|
require 'shopify_app/managers/scripttags_manager'
|
45
42
|
|
43
|
+
# middleware
|
44
|
+
require 'shopify_app/middleware/same_site_cookie_middleware'
|
45
|
+
|
46
46
|
# session
|
47
|
+
require 'shopify_app/session/storage_strategies/shop_storage_strategy'
|
48
|
+
require 'shopify_app/session/storage_strategies/user_storage_strategy'
|
47
49
|
require 'shopify_app/session/session_storage'
|
48
50
|
require 'shopify_app/session/session_repository'
|
49
51
|
require 'shopify_app/session/in_memory_session_store'
|
@@ -14,7 +14,9 @@ module ShopifyApp
|
|
14
14
|
attr_accessor :webhooks
|
15
15
|
attr_accessor :scripttags
|
16
16
|
attr_accessor :after_authenticate_job
|
17
|
-
|
17
|
+
attr_reader :session_repository
|
18
|
+
attr_accessor :per_user_tokens
|
19
|
+
alias_method :per_user_tokens?, :per_user_tokens
|
18
20
|
attr_accessor :api_version
|
19
21
|
|
20
22
|
# customise urls
|
@@ -34,11 +36,15 @@ module ShopifyApp
|
|
34
36
|
# allow namespacing webhook jobs
|
35
37
|
attr_accessor :webhook_jobs_namespace
|
36
38
|
|
39
|
+
# allow enabling of same site none on cookies
|
40
|
+
attr_accessor :enable_same_site_none
|
41
|
+
|
37
42
|
def initialize
|
38
43
|
@root_url = '/'
|
39
44
|
@myshopify_domain = 'myshopify.com'
|
40
45
|
@scripttags_manager_queue_name = Rails.application.config.active_job.queue_name
|
41
46
|
@webhooks_manager_queue_name = Rails.application.config.active_job.queue_name
|
47
|
+
@per_user_tokens = false
|
42
48
|
@disable_webpacker = ENV['SHOPIFY_APP_DISABLE_WEBPACKER'].present?
|
43
49
|
end
|
44
50
|
|
@@ -47,13 +53,8 @@ module ShopifyApp
|
|
47
53
|
end
|
48
54
|
|
49
55
|
def session_repository=(klass)
|
50
|
-
|
51
|
-
|
52
|
-
else
|
53
|
-
ActiveSupport::Reloader.to_prepare do
|
54
|
-
ShopifyApp::SessionRepository.storage = klass
|
55
|
-
end
|
56
|
-
end
|
56
|
+
@session_repository = klass
|
57
|
+
ShopifyApp::SessionRepository.storage = klass
|
57
58
|
end
|
58
59
|
|
59
60
|
def has_webhooks?
|
@@ -63,6 +64,10 @@ module ShopifyApp
|
|
63
64
|
def has_scripttags?
|
64
65
|
scripttags.present?
|
65
66
|
end
|
67
|
+
|
68
|
+
def enable_same_site_none
|
69
|
+
@enable_same_site_none.nil? ? embedded_app? : @enable_same_site_none
|
70
|
+
end
|
66
71
|
end
|
67
72
|
|
68
73
|
def self.configuration
|
@@ -27,12 +27,30 @@ module ShopifyApp
|
|
27
27
|
end
|
28
28
|
|
29
29
|
def shop_session
|
30
|
-
|
31
|
-
|
30
|
+
if ShopifyApp.configuration.per_user_tokens?
|
31
|
+
return unless session[:shopify_user]
|
32
|
+
@shop_session ||= ShopifyApp::SessionRepository.retrieve(session[:shopify_user]['id'])
|
33
|
+
else
|
34
|
+
return unless session[:shopify]
|
35
|
+
@shop_session ||= ShopifyApp::SessionRepository.retrieve(session[:shopify])
|
36
|
+
end
|
32
37
|
end
|
33
38
|
|
34
|
-
def
|
39
|
+
def login_again_if_different_user_or_shop
|
40
|
+
if ShopifyApp.configuration.per_user_tokens?
|
41
|
+
valid_session_data = session[:user_session].present? && params[:session].present? # session data was sent/stored correctly
|
42
|
+
sessions_do_not_match = session[:user_session] != params[:session] # current user is different from stored user
|
43
|
+
|
44
|
+
if valid_session_data && sessions_do_not_match
|
45
|
+
clear_session = true
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
35
49
|
if shop_session && params[:shop] && params[:shop].is_a?(String) && (shop_session.domain != params[:shop])
|
50
|
+
clear_session = true
|
51
|
+
end
|
52
|
+
|
53
|
+
if clear_session
|
36
54
|
clear_shop_session
|
37
55
|
redirect_to_login
|
38
56
|
end
|
@@ -60,6 +78,7 @@ module ShopifyApp
|
|
60
78
|
session[:shopify] = nil
|
61
79
|
session[:shopify_domain] = nil
|
62
80
|
session[:shopify_user] = nil
|
81
|
+
session[:user_session] = nil
|
63
82
|
end
|
64
83
|
|
65
84
|
def login_url_with_optional_shop(top_level: false)
|
data/lib/shopify_app/engine.rb
CHANGED
@@ -0,0 +1,60 @@
|
|
1
|
+
module ShopifyApp
|
2
|
+
class SameSiteCookieMiddleware
|
3
|
+
def initialize(app)
|
4
|
+
@app = app
|
5
|
+
end
|
6
|
+
|
7
|
+
def call(env)
|
8
|
+
_status, headers, _body = @app.call(env)
|
9
|
+
ensure
|
10
|
+
user_agent = env['HTTP_USER_AGENT']
|
11
|
+
|
12
|
+
if headers && headers['Set-Cookie'] && !SameSiteCookieMiddleware.same_site_none_incompatible?(user_agent) &&
|
13
|
+
ShopifyApp.configuration.enable_same_site_none
|
14
|
+
|
15
|
+
cookies = headers['Set-Cookie'].split("\n").compact
|
16
|
+
|
17
|
+
cookies.each do |cookie|
|
18
|
+
unless cookie.include?("; SameSite")
|
19
|
+
headers['Set-Cookie'] = headers['Set-Cookie'].gsub("#{cookie}", "#{cookie}; secure; SameSite=None")
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.same_site_none_incompatible?(user_agent)
|
26
|
+
sniffer = BrowserSniffer.new(user_agent)
|
27
|
+
|
28
|
+
webkit_same_site_bug?(sniffer) || drops_unrecognized_same_site_cookies?(sniffer)
|
29
|
+
rescue
|
30
|
+
true
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.webkit_same_site_bug?(sniffer)
|
34
|
+
(sniffer.os == :ios && sniffer.os_version.match?(/^([0-9]|1[12])[\.\_]/)) ||
|
35
|
+
(sniffer.os == :mac && sniffer.browser == :safari && sniffer.os_version.match?(/^10[\.\_]14/))
|
36
|
+
end
|
37
|
+
|
38
|
+
def self.drops_unrecognized_same_site_cookies?(sniffer)
|
39
|
+
(chromium_based?(sniffer) && sniffer.major_browser_version >= 51 && sniffer.major_browser_version <= 66) ||
|
40
|
+
(uc_browser?(sniffer) && !uc_browser_version_at_least?(sniffer: sniffer, major: 12, minor: 13, build: 2))
|
41
|
+
end
|
42
|
+
|
43
|
+
def self.chromium_based?(sniffer)
|
44
|
+
sniffer.browser_name.downcase.match?(/chrom(e|ium)/)
|
45
|
+
end
|
46
|
+
|
47
|
+
def self.uc_browser?(sniffer)
|
48
|
+
sniffer.user_agent.downcase.match?(/uc\s?browser/)
|
49
|
+
end
|
50
|
+
|
51
|
+
def self.uc_browser_version_at_least?(sniffer:, major:, minor:, build:)
|
52
|
+
digits = sniffer.browser_version.split('.').map(&:to_i)
|
53
|
+
return false unless digits.count >= 3
|
54
|
+
|
55
|
+
return digits[0] > major if digits[0] != major
|
56
|
+
return digits[1] > minor if digits[1] != minor
|
57
|
+
digits[2] >= build
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
@@ -3,9 +3,18 @@ module ShopifyApp
|
|
3
3
|
extend ActiveSupport::Concern
|
4
4
|
|
5
5
|
included do
|
6
|
-
|
6
|
+
if ShopifyApp.configuration.per_user_tokens?
|
7
|
+
extend ShopifyApp::SessionStorage::UserStorageStrategy
|
8
|
+
else
|
9
|
+
extend ShopifyApp::SessionStorage::ShopStorageStrategy
|
10
|
+
end
|
11
|
+
|
7
12
|
validates :shopify_token, presence: true
|
8
13
|
validates :api_version, presence: true
|
14
|
+
validates :shopify_domain, presence: true,
|
15
|
+
if: Proc.new {|_| ShopifyApp.configuration.per_user_tokens? }
|
16
|
+
validates :shopify_domain, presence: true, uniqueness: { case_sensitive: false },
|
17
|
+
if: Proc.new {|_| !ShopifyApp.configuration.per_user_tokens? }
|
9
18
|
end
|
10
19
|
|
11
20
|
def with_shopify_session(&block)
|
@@ -16,26 +25,5 @@ module ShopifyApp
|
|
16
25
|
&block
|
17
26
|
)
|
18
27
|
end
|
19
|
-
|
20
|
-
class_methods do
|
21
|
-
def store(session)
|
22
|
-
shop = find_or_initialize_by(shopify_domain: session.domain)
|
23
|
-
shop.shopify_token = session.token
|
24
|
-
shop.save!
|
25
|
-
shop.id
|
26
|
-
end
|
27
|
-
|
28
|
-
def retrieve(id)
|
29
|
-
return unless id
|
30
|
-
|
31
|
-
if shop = self.find_by(id: id)
|
32
|
-
ShopifyAPI::Session.new(
|
33
|
-
domain: shop.shopify_domain,
|
34
|
-
token: shop.shopify_token,
|
35
|
-
api_version: shop.api_version
|
36
|
-
)
|
37
|
-
end
|
38
|
-
end
|
39
|
-
end
|
40
28
|
end
|
41
29
|
end
|