Skip to content

Commit afe949b

Browse files
committed
Add token exchange API docs
1 parent 084626b commit afe949b

File tree

1 file changed

+88
-16
lines changed

1 file changed

+88
-16
lines changed

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

0 commit comments

Comments
 (0)