Skip to content

Commit d8a2e8c

Browse files
Merge branch 'Shopify:main' into fix/registry-process-with-response-as-struct
2 parents 60baf35 + 7c4ecb3 commit d8a2e8c

File tree

6 files changed

+138
-19
lines changed

6 files changed

+138
-19
lines changed

BREAKING_CHANGES_FOR_V15.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Breaking change notice for version 15.0.0
2+
3+
## Removal of `ShopifyAPI::Webhooks::Handler`
4+
5+
The `ShopifyAPI::Webhooks::Handler` class has been removed in favor of `ShopifyAPI::Webhooks::WebhookHandler`. The `ShopifyAPI::Webhooks::WebhookHandler` class is now the recommended way to handle webhooks.
6+
7+
Make a module or class that includes or extends `ShopifyAPI::Webhooks::WebhookHandler` and implement the `handle` method which accepts the following named parameters: data: `WebhookMetadata`.
8+
9+
In v14, adding new fields to the callback would become a breaking change. To make this code more flexible, handlers will now receive an object that can be typed and extended.
10+
11+
`data` will have the following keys
12+
- `topic`, `String` - The topic of the webhook
13+
- `shop`, `String` - The shop domain of the webhook
14+
- `body`, `T::Hash[String, T.untyped]`- The body of the webhook
15+
- `webhook_id`, `String` - The id of the webhook event to [avoid duplicates](https://shopify.dev/docs/apps/webhooks/best-practices#ignore-duplicates)
16+
- `api_version`, `String` - The api version of the webhook
17+
18+
### New implementation
19+
```ruby
20+
module WebhookHandler
21+
extend ShopifyAPI::Webhooks::WebhookHandler
22+
23+
class << self
24+
def handle_webhook(data:)
25+
puts "Received webhook! topic: #{data.topic} shop: #{data.shop} body: #{data.body} webhook_id: #{data.webhook_id} api_version: #{data.api_version"
26+
end
27+
end
28+
end
29+
```
30+
31+
### Previous implementation
32+
```ruby
33+
module WebhookHandler
34+
include ShopifyAPI::Webhooks::Handler
35+
36+
class << self
37+
def handle(topic:, shop:, body:)
38+
puts "Received webhook! topic: #{topic} shop: #{shop} body: #{body}"
39+
end
40+
end
41+
end
42+
```

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
Note: For changes to the API, see https://shopify.dev/changelog?filter=api
44

55
## Unreleased
6+
7+
## 14.3.0
68
- [#1312](https://github.com/Shopify/shopify-api-ruby/pull/1312) Use same leeway for `exp` and `nbf` when parsing JWT
79
- [#1313](https://github.com/Shopify/shopify-api-ruby/pull/1313) Fix: Webhook Registry now working with response_as_struct enabled
810
- [#1314](https://github.com/Shopify/shopify-api-ruby/pull/1314)

Gemfile.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
shopify_api (14.2.0)
4+
shopify_api (14.3.0)
55
activesupport
66
concurrent-ruby
77
hash_diff
@@ -160,4 +160,4 @@ DEPENDENCIES
160160
webmock
161161

162162
BUNDLED WITH
163-
2.3.4
163+
2.5.9

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ Once your app can perform OAuth, it can now make authenticated Shopify API calls
7979

8080
## Breaking Change Notices
8181

82+
### Breaking change notice for version 15.0.0
83+
See [BREAKING_CHANGES_FOR_V15](BREAKING_CHANGES_FOR_V15.md)
84+
8285
### Breaking change notice for version 10.0.0
8386
See [BREAKING_CHANGES_FOR_V10](BREAKING_CHANGES_FOR_V10.md)
8487

docs/usage/oauth.md

Lines changed: 88 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,103 @@ Once the library is set up for your project, you'll be able to use it to start a
55
To do this, you can follow the steps below.
66
For more information on authenticating a Shopify app please see the [Types of Authentication](https://shopify.dev/docs/apps/auth#types-of-authentication) page.
77

8+
#### Table of contents
9+
- [Session Persistence](#session-persistence)
10+
- [Supported types of OAuth Flow](#supported-types-of-oauth)
11+
- [Note about Rails](#note-about-rails)
12+
- [Performing OAuth](#performing-oauth-1)
13+
- [Token Exchange](#token-exchange)
14+
- [Authorization Code Grant Flow](#authorization-code-grant-flow)
15+
- [Using OAuth Session to make authenticated API calls](#using-oauth-session-to-make-authenticated-api-calls)
16+
817
## Session Persistence
918
Session persistence is deprecated from the `ShopifyAPI` library gem since [version 12.3.0](https://github.com/Shopify/shopify-api-ruby/blob/main/CHANGELOG.md#version-1230). The responsibility of session storage typically is fulfilled by the web framework middleware.
1019
This API library's focus is on making requests and facilitate session creation.
1120

1221
⚠️ If you're not using the [ShopifyApp](https://github.com/Shopify/shopify_app) gem, you may use ShopifyAPI to perform OAuth to create sessions, but you must implement your own session storage method to persist the session information to be used in authenticated API calls.
1322

23+
## Supported Types of OAuth
24+
> [!TIP]
25+
> If you are building an embedded app, we **strongly** recommend using [Shopify managed installation](https://shopify.dev/docs/apps/auth/installation#shopify-managed-installation)
26+
with [token exchange](#token-exchange) instead of the authorization code grant flow.
27+
28+
1. [Token Exchange](#token-exchange)
29+
- OAuth flow by exchanging the current user's [session token (shopify id token)](https://shopify.dev/docs/apps/auth/session-tokens) for an
30+
[access token](https://shopify.dev/docs/apps/auth/access-token-types/online.md).
31+
- Recommended and is only available for embedded apps
32+
- Doesn't require redirects, which makes authorization faster and prevents flickering when loading the app
33+
- Access scope changes are handled by [Shopify managed installation](https://shopify.dev/docs/apps/auth/installation#shopify-managed-installation)
34+
2. [Authorization Code Grant Flow](#authorization-code-grant-flow)
35+
- OAuth flow that requires the app to redirect the user to Shopify for installation/authorization of the app to access the shop's data.
36+
- Suitable for non-embedded apps
37+
- Installations, and access scope changes are managed by the app
38+
1439
## Note about Rails
1540
If using in the Rails framework, we highly recommend you use the [shopify_app](https://github.com/Shopify/shopify_app) gem to perform OAuth, you won't have to follow the instructions below to start your own OAuth flow.
1641
- See `ShopifyApp`'s [documentation on session storage](https://github.com/Shopify/shopify_app/blob/main/docs/shopify_app/sessions.md#sessions)
1742

1843
If you aren't using Rails, you can look at how the `ShopifyApp` gem handles OAuth flow for further examples:
19-
- [Session Controller](https://github.com/Shopify/shopify_app/blob/main/app/controllers/shopify_app/sessions_controller.rb)
20-
- Triggering and redirecting user to **begin** OAuth flow
21-
- [Callback Controller](https://github.com/Shopify/shopify_app/blob/main/app/controllers/shopify_app/callback_controller.rb)
22-
- Creating / storing sessions to **complete** the OAuth flow
44+
- Token Exchange Flow
45+
- [Token Exchange](https://github.com/Shopify/shopify_app/blob/main/lib/shopify_app/auth/token_exchange.rb)
46+
- Completes token exchange flow to get online and offline access tokens
47+
- Authorization Code Grant Flow
48+
- [Session Controller](https://github.com/Shopify/shopify_app/blob/main/app/controllers/shopify_app/sessions_controller.rb)
49+
- Triggering and redirecting user to **begin** OAuth flow
50+
- [Callback Controller](https://github.com/Shopify/shopify_app/blob/main/app/controllers/shopify_app/callback_controller.rb)
51+
- Creating / storing sessions to **complete** the OAuth flow
2352

2453
## Performing OAuth
54+
### Token Exchange
2555
#### Steps
56+
1. Enable [Shopify managed installation](https://shopify.dev/docs/apps/auth/installation#shopify-managed-installation)
57+
by configuring your scopes [through the Shopify CLI](https://shopify.dev/docs/apps/tools/cli/configuration).
58+
2. [Perform token exchange](#perform-token-exchange) to get an access token.
59+
60+
#### Perform Token Exchange
61+
Use [`ShopifyAPI::Auth::TokenExchange`](https://github.com/Shopify/shopify-api-ruby/blob/main/lib/shopify_api/auth/token_exchange.rb) to
62+
exchange a [session token](https://shopify.dev/docs/apps/auth/session-tokens) (Shopify Id Token) for an [access token](https://shopify.dev/docs/apps/auth/access-token-types/online.md).
63+
64+
#### Input
65+
| Parameter | Type | Required? | Default Value | Notes |
66+
| -------------- | ---------------------- | :-------: | :-----------: | ----------------------------------------------------------------------------------------------------------- |
67+
| `shop` | `String` | Yes | - | A Shopify domain name in the form `{exampleshop}.myshopify.com`. |
68+
| `session_token` | `String` | Yes| - | The session token (Shopify Id Token) provided by App Bridge in either the request 'Authorization' header or URL param when the app is loaded in Admin. |
69+
| `requested_token_type` | `TokenExchange::RequestedTokenType` | Yes | - | The type of token requested. Online: `TokenExchange::RequestedTokenType::ONLINE_ACCESS_TOKEN` or offline: `TokenExchange::RequestedTokenType::OFFLINE_ACCESS_TOKEN`. |
70+
71+
#### Output
72+
This method returns the new `ShopifyAPI::Auth::Session` object from the token exchange,
73+
your app should store this `Session` object to be used later [when making authenticated API calls](#using-oauth-session-to-make-authenticated-api-calls).
74+
75+
#### Example
76+
```ruby
77+
78+
# `shop` is the shop domain name - "this-is-my-example-shop.myshopify.com"
79+
# `session_token` is the session token provided by App Bridge either in:
80+
# - the request 'Authorization' header as `Bearer this-is-the-session_token`
81+
# - or as a URL param `id_token=this-is-the-session_token`
82+
83+
def authenticate(shop, session_token)
84+
session = ShopifyAPI::Auth::TokenExchange.exchange_token(
85+
shop: shop,
86+
session_token: session_token,
87+
requested_token_type: ShopifyAPI::Auth::TokenExchange::RequestedTokenType::OFFLINE_ACCESS_TOKEN,
88+
# or if you're requesting an online access token:
89+
# requested_token_type: ShopifyAPI::Auth::TokenExchange::RequestedTokenType::ONLINE_ACCESS_TOKEN,
90+
)
91+
92+
SessionRepository.store_session(session)
93+
end
94+
95+
```
96+
97+
### Authorization Code Grant Flow
98+
##### Steps
2699
1. [Add a route to start OAuth](#1-add-a-route-to-start-oauth)
27100
2. [Add an Oauth callback route](#2-add-an-oauth-callback-route)
28101
3. [Begin OAuth](#3-begin-oauth)
29102
4. [Handle OAuth Callback](#4-handle-oauth-callback)
30-
5. [Using OAuth Session to make authenticated API calls](#5-using-oauth-session-to-make-authenticated-api-calls)
31103

32-
### 1. Add a route to start OAuth
104+
#### 1. Add a route to start OAuth
33105
Add a route to your app to start the OAuth process.
34106

35107
```ruby
@@ -40,7 +112,7 @@ class ShopifyAuthController < ApplicationController
40112
end
41113
```
42114

43-
### 2. Add an OAuth callback route
115+
#### 2. Add an OAuth callback route
44116
After the app is authenticated with Shopify, the Shopify platform will send a request back to your app using this route
45117
(which you will provide as the `redirect_path` parameter to `begin_auth` method, in [step 3 - Begin OAuth](#3-begin-oauth)).
46118
```ruby
@@ -50,7 +122,7 @@ class ShopifyCallbackController < ApplicationController
50122
end
51123
```
52124

53-
### 3. Begin OAuth
125+
#### 3. Begin OAuth
54126
Use [`ShopifyAPI::Auth::Oauth.begin_auth`](https://github.com/Shopify/shopify-api-ruby/blob/main/lib/shopify_api/auth/oauth.rb#L22) method to start OAuth process for your app.
55127

56128
#### Input
@@ -74,7 +146,7 @@ Use [`ShopifyAPI::Auth::Oauth.begin_auth`](https://github.com/Shopify/shopify-ap
74146
|`auth_route`|`String`|URI that will be used for redirecting the user to the Shopify Authentication screen|
75147
|`cookie`|`ShopifyAPI::Auth::Oauth::SessionCookie`|A session cookie to store on the user's browser. |
76148

77-
#### Example
149+
##### Example
78150
Your app should take the returned values from the `begin_auth` method and:
79151

80152
1. Set the cookie in the user's browser. We strongly recommend that you use secure, httpOnly cookies for this to help prevent session hijacking.
@@ -109,19 +181,19 @@ end
109181

110182
⚠️ You can see a concrete example in the `ShopifyApp` gem's [SessionController](https://github.com/Shopify/shopify_app/blob/main/app/controllers/shopify_app/sessions_controller.rb).
111183

112-
### 4. Handle OAuth Callback
184+
#### 4. Handle OAuth Callback
113185
When the user grants permission to the app in Shopify admin, they'll be redirected back to the app's callback route
114186
(configured in [Step 2 - Add an OAuth callback route](#2-add-an-oauth-callback-route)).
115187

116188
Use [`ShopifyAPI::AuthL::Oauth.validate_auth_callback`](https://github.com/Shopify/shopify-api-ruby/blob/main/lib/shopify_api/auth/oauth.rb#L60) method to finalize the OAuth process.
117189

118-
#### Input
190+
##### Input
119191
| Parameter | Type | Notes |
120192
| ------------ | --------| ----------------------------------------------------------------------------------------------------------- |
121193
| `cookies` | `Hash` | All browser cookies in a hash format with key and value as `String` |
122194
| `auth_query` | `ShopifyAPI::Auth::Oauth::AuthQuery`| An `AuthQuery` containing the authorization request information used to validate the request.|
123195

124-
#### Output
196+
##### Output
125197
This method returns a hash containing the new session and a cookie to be set in the browser in form of:
126198
```ruby
127199
{
@@ -134,12 +206,12 @@ This method returns a hash containing the new session and a cookie to be set in
134206
|`session`|`ShopifyAPI::Auth::Session`|A session object that contains necessary information to identify the session like `shop`, `access_token`, `scope`, etc.|
135207
|`cookie` |`ShopifyAPI::Auth::Oauth::SessionCookie`|A session cookie to store on the user's browser. |
136208

137-
#### Example
209+
##### Example
138210
Your app should call `validate_auth_callback` to construct the `Session` object and cookie that will be used later for authenticated API requests.
139211

140212
1. Call `validate_auth_callback` to construct `Session` and `SessionCookie`.
141213
2. Update browser cookies with the new value for the session.
142-
3. Store the `Session` object to be used later when making authenticated API calls.
214+
3. Store the `Session` object to be used later when [making authenticated API calls](#using-oauth-session-to-make-authenticated-api-calls).
143215
- See [Make a GraphQL API call](https://github.com/Shopify/shopify-api-ruby/blob/main/docs/usage/graphql.md), or
144216
[Make a REST API call](https://github.com/Shopify/shopify-api-ruby/blob/main/docs/usage/rest.md) for examples on how to use the result `Session` object.
145217

@@ -182,8 +254,8 @@ end
182254

183255
⚠️ You can see a concrete example in the `ShopifyApp` gem's [CallbackController](https://github.com/Shopify/shopify_app/blob/main/app/controllers/shopify_app/callback_controller.rb).
184256

185-
### 5. Using OAuth Session to make authenticated API calls
186-
Once your OAuth flow is complete, and you have stored your `Session` object from [Step 4 - Handle OAuth Callback](#4-handle-oauth-callback), you may use that `Session` object to make authenticated API calls.
257+
## Using OAuth Session to make authenticated API calls
258+
Once your OAuth flow is complete, and you have persisted your `Session` object, you may use that `Session` object to make authenticated API calls.
187259

188260
Example:
189261
```ruby

lib/shopify_api/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
# frozen_string_literal: true
33

44
module ShopifyAPI
5-
VERSION = "14.2.0"
5+
VERSION = "14.3.0"
66
end

0 commit comments

Comments
 (0)