Skip to content

Commit c86e550

Browse files
authoredMay 22, 2024··
fix(oauth): correctly expire cached token (#164)
fixes #163
1 parent 41fdca0 commit c86e550

File tree

5 files changed

+608
-478
lines changed

5 files changed

+608
-478
lines changed
 

‎package-lock.json

+111-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,13 @@
9393
"devDependencies": {
9494
"@commitlint/cli": "^18.4.3",
9595
"@commitlint/config-conventional": "^18.4.3",
96-
"@mokuteki/jwt": "^1.0.2",
9796
"@semantic-release/changelog": "^6.0.3",
9897
"@semantic-release/git": "^10.0.1",
9998
"@sitapati/testcontainers": "^2.8.1",
10099
"@types/debug": "^4.1.12",
101100
"@types/express": "^4.17.21",
102101
"@types/jest": "^29.5.11",
102+
"@types/jsonwebtoken": "^9.0.6",
103103
"@types/lodash.mergewith": "^4.6.9",
104104
"@types/node": "^20.9.4",
105105
"@types/node-fetch": "^2.6.11",
@@ -118,6 +118,7 @@
118118
"grpc-tools": "^1.12.4",
119119
"husky": "^8.0.3",
120120
"jest": "^29.7.0",
121+
"jsonwebtoken": "^9.0.2",
121122
"lint-staged": "^15.2.0",
122123
"prettier": "^3.1.1",
123124
"semantic-release": "^22.0.12",

‎src/__tests__/admin/admin.integration.spec.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,14 @@ test('createClient', async () => {
1818
const c = new AdminApiClient()
1919
const clusters = await c.getClusters()
2020
const clusterUuid = clusters[0].uuid
21-
c.getClient(clusterUuid, 'testors')
21+
const clientName = 'test_generated-delete-me'
22+
c.getClient(clusterUuid, clientName)
2223
.then((res) => c.deleteClient(clusterUuid, res.ZEEBE_CLIENT_ID))
2324
.catch((e) => e)
2425

2526
const res = await c.createClient({
2627
clusterUuid,
27-
clientName: 'testors',
28+
clientName,
2829
permissions: ['Zeebe'],
2930
})
3031
const client = await c.getClient(clusterUuid, res.clientId)

‎src/__tests__/oauth/OAuthProvider.unit.spec.ts

+478-466
Large diffs are not rendered by default.

‎src/oauth/lib/OAuthProvider.ts

+14-2
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,8 @@ export class OAuthProvider implements IOAuthProvider {
353353
const key = this.getCacheKey(audience)
354354
try {
355355
const decoded = jwtDecode(token.access_token)
356-
356+
trace(`Caching token: ${JSON.stringify(decoded, null, 2)}`)
357+
trace(`Caching token for ${audience} in memory. Expiry: ${decoded.exp}`)
357358
token.expiry = decoded.exp ?? 0
358359
this.tokenCache[key] = token
359360
} catch (e) {
@@ -371,9 +372,11 @@ export class OAuthProvider implements IOAuthProvider {
371372
const tokenFileName = this.getCachedTokenFileName(clientId, audience)
372373
const tokenCachedInFile = fs.existsSync(tokenFileName)
373374
if (!tokenCachedInFile) {
375+
trace(`No file cached token for ${audience} found`)
374376
return null
375377
}
376378
try {
379+
trace(`Reading file cached token for ${audience}`)
377380
token = JSON.parse(
378381
fs.readFileSync(this.getCachedTokenFileName(clientId, audience), 'utf8')
379382
)
@@ -422,10 +425,19 @@ export class OAuthProvider implements IOAuthProvider {
422425
private isExpired(token: Token) {
423426
const d = new Date()
424427
const currentTime = d.setSeconds(d.getSeconds())
428+
429+
// token.expiry is seconds since Unix Epoch
430+
// The Date constructor expects milliseconds since Unix Epoch
431+
const tokenExpiryMs = token.expiry * 1000
432+
433+
trace(`Checking token expiry for ${token.audience}`)
434+
trace(` Current time: ${currentTime}`)
435+
trace(` Token expiry: ${tokenExpiryMs}`)
436+
425437
// If the token has 10 seconds (by default) or less left, renew it.
426438
// The Identity server token cache is cleared 30 seconds before the token expires, allowing us to renew it
427439
// See: https://github.com/camunda/camunda-8-js-sdk/issues/125
428-
const tokenIsExpired = currentTime >= token.expiry - this.refreshWindow
440+
const tokenIsExpired = currentTime >= tokenExpiryMs - this.refreshWindow
429441
return tokenIsExpired
430442
}
431443

0 commit comments

Comments
 (0)
Please sign in to comment.