You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/usage/oauth.md
+88-16Lines changed: 88 additions & 16 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff 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
5
5
To do this, you can follow the steps below.
6
6
For more information on authenticating a Shopify app please see the [Types of Authentication](https://shopify.dev/docs/apps/auth#types-of-authentication) page.
7
7
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
+
8
17
## Session Persistence
9
18
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.
10
19
This API library's focus is on making requests and facilitate session creation.
11
20
12
21
⚠️ 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.
13
22
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
- 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
+
14
39
## Note about Rails
15
40
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.
16
41
- See `ShopifyApp`'s [documentation on session storage](https://github.com/Shopify/shopify_app/blob/main/docs/shopify_app/sessions.md#sessions)
17
42
18
43
If you aren't using Rails, you can look at how the `ShopifyApp` gem handles OAuth flow for further examples:
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 |
|`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`
5.[Using OAuth Session to make authenticated API calls](#5-using-oauth-session-to-make-authenticated-api-calls)
31
103
32
-
### 1. Add a route to start OAuth
104
+
####1. Add a route to start OAuth
33
105
Add a route to your app to start the OAuth process.
34
106
35
107
```ruby
@@ -40,7 +112,7 @@ class ShopifyAuthController < ApplicationController
40
112
end
41
113
```
42
114
43
-
### 2. Add an OAuth callback route
115
+
####2. Add an OAuth callback route
44
116
After the app is authenticated with Shopify, the Shopify platform will send a request back to your app using this route
45
117
(which you will provide as the `redirect_path` parameter to `begin_auth` method, in [step 3 - Begin OAuth](#3-begin-oauth)).
46
118
```ruby
@@ -50,7 +122,7 @@ class ShopifyCallbackController < ApplicationController
50
122
end
51
123
```
52
124
53
-
### 3. Begin OAuth
125
+
####3. Begin OAuth
54
126
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.
55
127
56
128
#### Input
@@ -74,7 +146,7 @@ Use [`ShopifyAPI::Auth::Oauth.begin_auth`](https://github.com/Shopify/shopify-ap
74
146
|`auth_route`|`String`|URI that will be used for redirecting the user to the Shopify Authentication screen|
75
147
|`cookie`|`ShopifyAPI::Auth::Oauth::SessionCookie`|A session cookie to store on the user's browser. |
76
148
77
-
#### Example
149
+
#####Example
78
150
Your app should take the returned values from the `begin_auth` method and:
79
151
80
152
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
109
181
110
182
⚠️ 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).
111
183
112
-
### 4. Handle OAuth Callback
184
+
####4. Handle OAuth Callback
113
185
When the user grants permission to the app in Shopify admin, they'll be redirected back to the app's callback route
114
186
(configured in [Step 2 - Add an OAuth callback route](#2-add-an-oauth-callback-route)).
115
187
116
188
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.
|`cookies`|`Hash`| All browser cookies in a hash format with key and value as `String`|
122
194
|`auth_query`|`ShopifyAPI::Auth::Oauth::AuthQuery`| An `AuthQuery` containing the authorization request information used to validate the request.|
123
195
124
-
#### Output
196
+
#####Output
125
197
This method returns a hash containing the new session and a cookie to be set in the browser in form of:
126
198
```ruby
127
199
{
@@ -134,12 +206,12 @@ This method returns a hash containing the new session and a cookie to be set in
134
206
|`session`|`ShopifyAPI::Auth::Session`|A session object that contains necessary information to identify the session like `shop`, `access_token`, `scope`, etc.|
135
207
|`cookie`|`ShopifyAPI::Auth::Oauth::SessionCookie`|A session cookie to store on the user's browser. |
136
208
137
-
#### Example
209
+
#####Example
138
210
Your app should call `validate_auth_callback` to construct the `Session` object and cookie that will be used later for authenticated API requests.
139
211
140
212
1. Call `validate_auth_callback` to construct `Session` and `SessionCookie`.
141
213
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).
143
215
- See [Make a GraphQL API call](https://github.com/Shopify/shopify-api-ruby/blob/main/docs/usage/graphql.md), or
144
216
[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.
145
217
@@ -182,8 +254,8 @@ end
182
254
183
255
⚠️ 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).
184
256
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.
0 commit comments