Skip to content

Commit

Permalink
Merge branch 'main' into non-http-webhooks
Browse files Browse the repository at this point in the history
  • Loading branch information
kirillplatonov committed Jul 24, 2023
2 parents 42c3dec + 73b2863 commit a186e24
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 23 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
Unreleased
----------
* Fixes typo in webhook generator [#1704](https://github.com/Shopify/shopify_app/pull/1704)
* Fix registration of event_bridge and pub_sub webhooks [#1635](https://github.com/Shopify/shopify_app/pull/1635)

21.6.0 (July 11, 2023)
----------
* Adds support for toggling test charges within `EnsureBilling` by adding `test` field to `BillingConfiguration` and pulling in environment variable [#1688](https://github.com/Shopify/shopify_app/pull/1688)
* Fix registration of event_bridge and pub_sub webhooks [#1635](https://github.com/Shopify/shopify_app/pull/1635)
* Adds support for 2023-07 API version [#1706](https://github.com/Shopify/shopify_app/pull/1706)

21.5.0 (May 18, 2023)
----------
Expand Down
8 changes: 4 additions & 4 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
PATH
remote: .
specs:
shopify_app (21.5.0)
shopify_app (21.6.0)
activeresource
addressable (~> 2.7)
browser_sniffer (~> 2.0)
jwt (>= 2.2.3)
rails (> 5.2.1)
redirect_safely (~> 1.0)
shopify_api (~> 13.0)
shopify_api (~> 13.1)
sprockets-rails (>= 2.0.0)

GEM
Expand Down Expand Up @@ -217,7 +217,7 @@ GEM
ruby-progressbar (1.13.0)
ruby2_keywords (0.0.5)
securerandom (0.2.2)
shopify_api (13.0.0)
shopify_api (13.1.0)
activesupport
concurrent-ruby
hash_diff
Expand All @@ -227,7 +227,7 @@ GEM
openssl
securerandom
sorbet-runtime
zeitwerk (~> 2.5, < 2.6.5)
zeitwerk (~> 2.5)
sorbet-runtime (0.5.10835)
sprockets (4.2.0)
concurrent-ruby (~> 1.0)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ You can find documentation on gem usage, concepts, mixins, installation, and mor
* [Controller Concerns](/docs/shopify_app/controller-concerns.md)
* [Generators](/docs/shopify_app/generators.md)
* [ScriptTags](/docs/shopify_app/script-tags.md)
* [Session repository](/docs/shopify_app/session-repository.md)
* [Sessions](/docs/shopify_app/sessions.md)
* [Handling changes in access scopes](/docs/shopify_app/handling-access-scopes-changes.md)
* [Testing](/docs/shopify_app/testing.md)
* [Webhooks](/docs/shopify_app/webhooks.md)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,42 +1,69 @@
# Session repository
# Sessions

Sessions are used to make contextual API calls for either a shop (offline session) or a user (online session). This gem has ownership of session persistence.

#### Table of contents

[`ShopifyApp::SessionRepository`](#shopifyappsessionrepository)
[Sessions](#sessions)
* [Shop-based token storage](#shop-based-token-storage)
* [User-based token storage](#user-based-token-storage)
* [`ShopifyApp::SessionRepository`](#shopifyappsessionrepository)
* [Loading Sessions](#loading-sessions)

[Access scopes](#access-scopes)
* [`ShopifyApp::ShopSessionStorageWithScopes`](#shopifyappshopsessionstoragewithscopes)
* [``ShopifyApp::UserSessionStorageWithScopes``](#shopifyappusersessionstoragewithscopes)

[Migrating from shop-based to user-based token strategy](#migrating-from-shop-based-to-user-based-token-strategy)

## ShopifyApp::SessionRepository

`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.

### Shop-based token storage
### Shop-based token storage (offline token)

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.
```sh
rails generate shopify_app:shop_model
```
This will generate a shop model which will be the storage for the tokens necessary for authentication.
This will generate a shop model which will be the storage for the tokens necessary for authentication. To enable session persistance, you'll need to configure your `/initializers/shopify_app.rb` accordingly:

```ruby
config.shop_session_repository = 'Shop'
```

### User-based token storage
### User-based token storage (online token)

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.
```sh
rails generate shopify_app:shop_model
rails generate shopify_app:user_model
```
This will generate a shop model and user model, which will be the storage for the tokens necessary for authentication.

This will generate a user and shop model which will be the storage for the tokens necessary for authentication. To enable session persistance, you'll need to configure your `/initializers/shopify_app.rb` accordingly:

```ruby
config.shop_session_repository = 'Shop'
config.user_session_repository = 'User'
```

The current Shopify user will be stored in the rails session at `session[:shopify_user]`

Read more about Online vs. Offline access [here](https://shopify.dev/apps/auth/oauth/access-modes).

### Customized Session Storage - ShopifyApp::SessionRepository

`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.

### Loading Sessions
By using the appropriate controller concern, sessions are loaded for you. Note -- these controller concerns cannot both be included in the same controller.

#### Shop Sessions - `EnsureInstalled`
`EnsureInstalled` controller concern will load a shop session with the `installed_shop_session` helper. If a shop session is not found, meaning the app wasn't installed for this shop, the request will be redirected to be installed.

This controller concern should NOT be used if you don't need your app to make calls on behalf of a user.

#### User Sessions - `EnsureHasSession`
`EnsureHasSession` controller concern will load a user session via `current_shopify_session`. As part of loading this session, this concern will also ensure that the user session has the appropriate scopes needed for the application. If the user isn't found or has fewer permitted scopes than are required, they will be prompted to authorize the application.

This controller concern should be used if you don't need your app to make calls on behalf of a user. With that in mind, there are a few other embedded concerns that are mixed in to ensure that embedding, CSRF, localization, and billing allow the action for the user.

## Access scopes

If you want to customize how access scopes are stored for shops and users, you can implement the `access_scopes` getters and setters in the models that include `ShopifyApp::ShopSessionStorageWithScopes` and `ShopifyApp::UserSessionStorageWithScopes` as shown:
Expand Down Expand Up @@ -68,6 +95,7 @@ class User < ActiveRecord::Base
end
end
```

## Migrating from shop-based to user-based token strategy

1. Run the `user_model` generator as mentioned above.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ ShopifyApp.configure do |config|
config.webhooks = [
{ topic: "app/uninstalled", address: "webhooks/app_uninstalled"},
{ topic: "customers/data_request", address: "webhooks/customers_data_request" },
{ topic: "customer/redact", address: "webhooks/customers_redact"},
{ topic: "customers/redact", address: "webhooks/customers_redact"},
{ topic: "shop/redact", address: "webhooks/shop_redact"}
]

Expand Down
2 changes: 1 addition & 1 deletion lib/shopify_app/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module ShopifyApp
VERSION = "21.5.0"
VERSION = "21.6.0"
end
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "shopify_app",
"version": "21.4.1",
"version": "21.6.0",
"repository": "git@github.com:Shopify/shopify_app.git",
"author": "Shopify",
"license": "MIT",
Expand Down
2 changes: 1 addition & 1 deletion shopify_app.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Gem::Specification.new do |s|
s.add_runtime_dependency("jwt", ">= 2.2.3")
s.add_runtime_dependency("rails", "> 5.2.1")
s.add_runtime_dependency("redirect_safely", "~> 1.0")
s.add_runtime_dependency("shopify_api", "~> 13.0")
s.add_runtime_dependency("shopify_api", "~> 13.1")
s.add_runtime_dependency("sprockets-rails", ">= 2.0.0")

s.add_development_dependency("byebug")
Expand Down
6 changes: 3 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4321,9 +4321,9 @@ semver@7.0.0:
integrity sha512-+GB6zVA9LWh6zovYQLALHwv5rb2PHGlJi3lfiqIHxR0uuwCgefcOJc59v9fv1w8GbStwxuuqqAjI9NMAOOgq1A==

semver@^5.4.1, semver@^5.5.0, semver@^5.6.0:
version "5.7.1"
resolved "https://registry.yarnpkg.com/semver/-/semver-5.7.1.tgz#a954f931aeba508d307bbf069eff0c01c96116f7"
integrity sha512-sauaDf/PZdVgrLTNYHRtpXa1iRiKcaebiKQ1BJdpQlWH2lCvexQdX55snPFyK7QzpudqbCI0qXFfOasHdyNDGQ==
version "5.7.2"
resolved "https://registry.yarnpkg.com/semver/-/semver-5.7.2.tgz#48d55db737c3287cd4835e17fa13feace1c41ef8"
integrity sha512-cBznnQ9KjJqU67B52RMC65CMarK2600WFnbkcaiwWq3xy/5haFJlshgnpjovMVJ+Hff49d8GEn0b87C5pDQ10g==

serialize-javascript@4.0.0, serialize-javascript@^4.0.0:
version "4.0.0"
Expand Down

0 comments on commit a186e24

Please sign in to comment.