Skip to content

Commit 4beac98

Browse files
committed
Complete auth code flow grant with expiring offline token based on configuration
1 parent c689206 commit 4beac98

File tree

2 files changed

+49
-4
lines changed

2 files changed

+49
-4
lines changed

lib/shopify_api/auth/oauth.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,12 @@ def validate_auth_callback(cookies:, auth_query:)
7171
"Invalid state in OAuth callback." unless state == auth_query.state
7272

7373
null_session = Auth::Session.new(shop: auth_query.shop)
74-
body = { client_id: Context.api_key, client_secret: Context.api_secret_key, code: auth_query.code }
74+
body = {
75+
client_id: Context.api_key,
76+
client_secret: Context.api_secret_key,
77+
code: auth_query.code,
78+
expiring: Context.expiring_offline_access_tokens ? 1 : 0, # Only applicable for offline tokens
79+
}
7580

7681
client = Clients::HttpClient.new(session: null_session, base_path: "/admin/oauth")
7782
response = begin
@@ -100,7 +105,7 @@ def validate_auth_callback(cookies:, auth_query:)
100105
else
101106
SessionCookie.new(
102107
value: session.id,
103-
expires: session.online? ? session.expires : nil,
108+
expires: session.expires ? session.expires : nil,
104109
)
105110
end
106111

test/auth/oauth_test.rb

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,24 @@ def setup
4040
client_id: ShopifyAPI::Context.api_key,
4141
client_secret: ShopifyAPI::Context.api_secret_key,
4242
code: @callback_code,
43+
expiring: 0,
4344
}
4445

46+
@expiring_access_token_request = @access_token_request.merge({ expiring: 1 })
47+
4548
@offline_token_response = {
4649
access_token: SecureRandom.alphanumeric(10),
4750
scope: "scope1,scope2",
4851
}
52+
53+
@expiring_offline_token_response = @offline_token_response.merge(
54+
{
55+
expires_in: 1000,
56+
refresh_token: SecureRandom.alphanumeric(10),
57+
refresh_token_expires_in: 2000,
58+
},
59+
)
60+
4961
@online_token_response = {
5062
access_token: SecureRandom.alphanumeric(10),
5163
scope: "scope1,scope2",
@@ -123,7 +135,7 @@ def test_begin_auth_private_app
123135
end
124136

125137
def test_validate_auth_callback_offline
126-
modify_context(is_embedded: false)
138+
modify_context(is_embedded: false, expiring_offline_access_tokens: false)
127139

128140
stub_request(:post, "https://#{@shop}/admin/oauth/access_token")
129141
.with(body: @access_token_request)
@@ -155,7 +167,7 @@ def test_validate_auth_callback_offline_embedded
155167
)
156168
expected_cookie = ShopifyAPI::Auth::Oauth::SessionCookie.new(value: "", expires: @stubbed_time_now)
157169

158-
modify_context(is_embedded: true)
170+
modify_context(is_embedded: true, expiring_offline_access_tokens: false)
159171

160172
got = Time.stub(:now, @stubbed_time_now) do
161173
ShopifyAPI::Auth::Oauth.validate_auth_callback(cookies: @cookies, auth_query: @auth_query)
@@ -164,6 +176,34 @@ def test_validate_auth_callback_offline_embedded
164176
verify_oauth_complete(got: got, expected_session: expected_session, expected_cookie: expected_cookie)
165177
end
166178

179+
def test_validate_auth_callback_offline_token_with_expiring_token_enabled
180+
modify_context(expiring_offline_access_tokens: true)
181+
182+
stub_request(:post, "https://#{@shop}/admin/oauth/access_token")
183+
.with(body: @expiring_access_token_request)
184+
.to_return(body: @expiring_offline_token_response.to_json, headers: { content_type: "application/json" })
185+
186+
expected_session = ShopifyAPI::Auth::Session.new(
187+
id: "offline_#{@shop}",
188+
shop: @shop,
189+
access_token: @offline_token_response[:access_token],
190+
scope: @offline_token_response[:scope],
191+
expires: @stubbed_time_now + @online_token_response[:expires_in].to_i,
192+
refresh_token: @expiring_offline_token_response[:refresh_token],
193+
refresh_token_expires: @stubbed_time_now + @expiring_offline_token_response[:refresh_token_expires_in].to_i,
194+
)
195+
expected_cookie = ShopifyAPI::Auth::Oauth::SessionCookie.new(
196+
value: "offline_#{@shop}",
197+
expires: expected_session.expires,
198+
)
199+
200+
got = Time.stub(:now, @stubbed_time_now) do
201+
ShopifyAPI::Auth::Oauth.validate_auth_callback(cookies: @cookies, auth_query: @auth_query)
202+
end
203+
204+
verify_oauth_complete(got:, expected_session:, expected_cookie:)
205+
end
206+
167207
def test_validate_auth_callback_online
168208
stub_request(:post, "https://#{@shop}/admin/oauth/access_token")
169209
.with(body: @access_token_request)

0 commit comments

Comments
 (0)