Skip to content

Commit 3f0c967

Browse files
committed
Modify migration helper input paramters
1 parent 7dc2372 commit 3f0c967

File tree

3 files changed

+17
-23
lines changed

3 files changed

+17
-23
lines changed

docs/usage/oauth.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ For more information on authenticating a Shopify app please see the [Types of Au
1414
- [Authorization Code Grant](#authorization-code-grant)
1515
- [Client Credentials Grant](#client-credentials-grant)
1616
- [Expiring Offline Access Tokens](#expiring-offline-access-tokens)
17-
- [Enabling Expiring Offline Access Tokens](#enabling-expiring-offline-access-tokens)
1817
- [Refreshing Access Tokens](#refreshing-access-tokens)
1918
- [Migrating Non-Expiring Tokens to Expiring Tokens](#migrating-non-expiring-tokens-to-expiring-tokens)
2019
- [Using OAuth Session to make authenticated API calls](#using-oauth-session-to-make-authenticated-api-calls)
@@ -398,7 +397,8 @@ If you have existing non-expiring offline access tokens and want to migrate them
398397
#### Input
399398
| Parameter | Type | Required? | Notes |
400399
| -------------- | -------- | :-------: | ----------------------------------------------------------------------------------------------- |
401-
| `non_expiring_offline_session` | `ShopifyAPI::Auth::Session` | Yes | A Session object containing the non-expiring offline access token to migrate. |
400+
| `shop` | `String` | Yes | A Shopify domain name in the form `{exampleshop}.myshopify.com`. |
401+
| `non_expiring_offline_token` | `String` | Yes | The non-expiring offline access token to migrate. |
402402

403403
#### Output
404404
This method returns a new `ShopifyAPI::Auth::Session` object with an expiring access token and refresh token. Your app should store this new session to replace the non-expiring one.
@@ -411,7 +411,8 @@ def migrate_shop_to_expiring_offline_token(shop)
411411

412412
# Migrate to expiring token
413413
new_session = ShopifyAPI::Auth::TokenExchange.migrate_to_expiring_token(
414-
non_expiring_offline_session: old_session
414+
shop: shop,
415+
non_expiring_offline_token: old_session.access_token
415416
)
416417

417418
# Store the new expiring session, replacing the old one

lib/shopify_api/auth/token_exchange.rb

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,26 +81,28 @@ def exchange_token(shop:, session_token:, requested_token_type:)
8181

8282
sig do
8383
params(
84-
non_expiring_offline_session: ShopifyAPI::Auth::Session,
84+
shop: String,
85+
non_expiring_offline_token: String,
8586
).returns(ShopifyAPI::Auth::Session)
8687
end
87-
def migrate_to_expiring_token(non_expiring_offline_session:)
88+
def migrate_to_expiring_token(shop:, non_expiring_offline_token:)
8889
unless ShopifyAPI::Context.setup?
8990
raise ShopifyAPI::Errors::ContextNotSetupError,
9091
"ShopifyAPI::Context not setup, please call ShopifyAPI::Context.setup"
9192
end
9293

94+
shop_session = ShopifyAPI::Auth::Session.new(shop: shop)
9395
body = {
9496
client_id: ShopifyAPI::Context.api_key,
9597
client_secret: ShopifyAPI::Context.api_secret_key,
9698
grant_type: TOKEN_EXCHANGE_GRANT_TYPE,
97-
subject_token: non_expiring_offline_session.access_token,
99+
subject_token: non_expiring_offline_token,
98100
subject_token_type: RequestedTokenType::OFFLINE_ACCESS_TOKEN.serialize,
99101
requested_token_type: RequestedTokenType::OFFLINE_ACCESS_TOKEN.serialize,
100102
expiring: "1",
101103
}
102104

103-
client = Clients::HttpClient.new(session: non_expiring_offline_session, base_path: "/admin/oauth")
105+
client = Clients::HttpClient.new(session: shop_session, base_path: "/admin/oauth")
104106
response = begin
105107
client.request(
106108
Clients::HttpRequest.new(
@@ -118,7 +120,7 @@ def migrate_to_expiring_token(non_expiring_offline_session:)
118120
session_params = T.cast(response.body, T::Hash[String, T.untyped]).to_h
119121

120122
Session.from(
121-
shop: non_expiring_offline_session.shop,
123+
shop: shop,
122124
access_token_response: Oauth::AccessTokenResponse.from_hash(session_params),
123125
)
124126
end

test/auth/token_exchange_test.rb

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -231,24 +231,17 @@ def test_exchange_token_online_token
231231

232232
def test_migrate_to_expiring_token_context_not_setup
233233
modify_context(api_key: "", api_secret_key: "", host: "")
234-
non_expiring_session = ShopifyAPI::Auth::Session.new(
235-
shop: @shop,
236-
access_token: "old-offline-token-123",
237-
)
238234

239235
assert_raises(ShopifyAPI::Errors::ContextNotSetupError) do
240236
ShopifyAPI::Auth::TokenExchange.migrate_to_expiring_token(
241-
non_expiring_offline_session: non_expiring_session,
237+
shop: @shop,
238+
non_expiring_offline_token: "old-offline-token-123",
242239
)
243240
end
244241
end
245242

246243
def test_migrate_to_expiring_token_success
247244
non_expiring_token = "old-offline-token-123"
248-
non_expiring_session = ShopifyAPI::Auth::Session.new(
249-
shop: @shop,
250-
access_token: non_expiring_token,
251-
)
252245
migration_request = {
253246
client_id: ShopifyAPI::Context.api_key,
254247
client_secret: ShopifyAPI::Context.api_secret_key,
@@ -280,7 +273,8 @@ def test_migrate_to_expiring_token_success
280273

281274
session = Time.stub(:now, @stubbed_time_now) do
282275
ShopifyAPI::Auth::TokenExchange.migrate_to_expiring_token(
283-
non_expiring_offline_session: non_expiring_session,
276+
shop: @shop,
277+
non_expiring_offline_token: non_expiring_token,
284278
)
285279
end
286280

@@ -289,10 +283,6 @@ def test_migrate_to_expiring_token_success
289283

290284
def test_migrate_to_expiring_token_http_error
291285
non_expiring_token = "old-offline-token-123"
292-
non_expiring_session = ShopifyAPI::Auth::Session.new(
293-
shop: @shop,
294-
access_token: non_expiring_token,
295-
)
296286
migration_request = {
297287
client_id: ShopifyAPI::Context.api_key,
298288
client_secret: ShopifyAPI::Context.api_secret_key,
@@ -316,7 +306,8 @@ def test_migrate_to_expiring_token_http_error
316306

317307
assert_raises(ShopifyAPI::Errors::HttpResponseError) do
318308
ShopifyAPI::Auth::TokenExchange.migrate_to_expiring_token(
319-
non_expiring_offline_session: non_expiring_session,
309+
shop: @shop,
310+
non_expiring_offline_token: non_expiring_token,
320311
)
321312
end
322313
end

0 commit comments

Comments
 (0)