Skip to content

Commit 336fcb8

Browse files
authored
Merge pull request #5340 from Shopify/02-03-dont_introspect_token_if_already_expired
Dont introspect token if already expired
2 parents 0fcd4da + 9cd73ef commit 336fcb8

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

packages/cli-kit/src/private/node/session/validate.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ describe('validateSession', () => {
167167

168168
// Then
169169
expect(got).toBe('needs_refresh')
170+
expect(validateIdentityToken).not.toHaveBeenCalled()
170171
})
171172

172173
test('returns needs_refresh if requesting partners and is expired', async () => {
@@ -184,6 +185,7 @@ describe('validateSession', () => {
184185

185186
// Then
186187
expect(got).toBe('needs_refresh')
188+
expect(validateIdentityToken).not.toHaveBeenCalled()
187189
})
188190

189191
test('returns needs_refresh if requesting storefront and is expired', async () => {
@@ -201,6 +203,7 @@ describe('validateSession', () => {
201203

202204
// Then
203205
expect(got).toBe('needs_refresh')
206+
expect(validateIdentityToken).not.toHaveBeenCalled()
204207
})
205208

206209
test('returns needs_refresh if requesting admin and is expired', async () => {
@@ -218,6 +221,7 @@ describe('validateSession', () => {
218221

219222
// Then
220223
expect(got).toBe('needs_refresh')
224+
expect(validateIdentityToken).not.toHaveBeenCalled()
221225
})
222226

223227
test('returns needs_refresh if session does not include requested store', async () => {
@@ -235,5 +239,6 @@ describe('validateSession', () => {
235239

236240
// Then
237241
expect(got).toBe('needs_refresh')
242+
expect(validateIdentityToken).not.toHaveBeenCalled()
238243
})
239244
})

packages/cli-kit/src/private/node/session/validate.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import {applicationId} from './identity.js'
33
import {ApplicationToken, IdentityToken, validateCachedIdentityTokenStructure} from './schema.js'
44
import {validateIdentityToken} from './identity-token-validation.js'
55
import {sessionConstants} from '../constants.js'
6-
import {outputDebug} from '../../../public/node/output.js'
76
import {firstPartyDev} from '../../../public/node/context/local.js'
87
import {OAuthApplications} from '../session.js'
8+
import {outputDebug} from '@shopify/cli-kit/node/output'
99

1010
type ValidationResult = 'needs_refresh' | 'needs_full_auth' | 'ok'
1111

@@ -35,7 +35,6 @@ export async function validateSession(
3535
): Promise<ValidationResult> {
3636
if (!session) return 'needs_full_auth'
3737
const scopesAreValid = validateScopes(scopes, session.identity)
38-
const identityIsValid = await validateIdentityToken(session.identity.accessToken)
3938
if (!scopesAreValid) return 'needs_full_auth'
4039
let tokensAreExpired = isTokenExpired(session.identity)
4140

@@ -64,18 +63,18 @@ export async function validateSession(
6463
tokensAreExpired = tokensAreExpired || isTokenExpired(token)
6564
}
6665

67-
outputDebug(`
68-
The validation of the token for application/identity completed with the following results:
69-
- It's expired: ${tokensAreExpired}
70-
- It's invalid in identity: ${!identityIsValid}
71-
`)
66+
outputDebug(`- Token validation -> It's expired: ${tokensAreExpired}`)
7267

7368
if (!validateCachedIdentityTokenStructure(session.identity)) {
7469
return 'needs_full_auth'
7570
}
7671

7772
if (tokensAreExpired) return 'needs_refresh'
73+
74+
const identityIsValid = await validateIdentityToken(session.identity.accessToken)
75+
outputDebug(`- Token validation -> It's invalid in identity: ${!identityIsValid}`)
7876
if (!identityIsValid) return 'needs_full_auth'
77+
7978
return 'ok'
8079
}
8180

0 commit comments

Comments
 (0)