Skip to content

fix: reject expired IMS tokens by comparing expiry in milliseconds - #265

Merged
kptdobe merged 2 commits into
mainfrom
fix/auth-token-expiry-ms-units
Apr 29, 2026
Merged

kptdobe merged 2 commits into
mainfrom
fix/auth-token-expiry-ms-units

Conversation

@kptdobe

@kptdobe kptdobe commented Apr 29, 2026

Copy link
Copy Markdown
Contributor

Issue

Regular 500 observed with message:

Error computing context
Error: KV PUT failed: 400 Invalid expiration of 1777442370. Please specify integer greater than the current number of seconds since the UNIX epoch.

Summary

  • IMS JWT created_at and expires_in fields are in milliseconds, but now was computed in seconds (Math.floor(Date.now() / 1000)), making the comparison expires < now always false — expired tokens were never rejected
  • Fix: use Date.now() for now so both sides of the comparison are in milliseconds
  • Update the jose mock and offline validation token fixture to use ms-scale timestamps, matching real IMS behavior

Test plan

  • New regression test in auth.test.js: token issued 2h ago valid for 1h → must resolve as anonymous; confirmed the test fails before the fix and passes after
  • Existing anonymous if expired test continues to pass
  • All 6 Offline Token Validation tests continue to pass
  • npm run lint && npm test — 0 failures

🤖 Generated with Claude Code

IMS JWT `created_at` and `expires_in` fields are in milliseconds, but
`now` was computed in seconds — making `expires` always ~1000x larger
than `now`, so expired tokens were never rejected.

Fix: use `Date.now()` (ms) for `now` to match the IMS field scale.

Also update the jose mock and offline validation token fixture to use
ms-scale timestamps, matching real IMS behavior.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@codecov

codecov Bot commented Apr 29, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@kptdobe
kptdobe requested a review from bosschaert April 29, 2026 10:10
Tokens with < 60s remaining cause DA_AUTH.put() to throw a 400
(Cloudflare KV requires expiration >= 60s in the future). The unhandled
error propagated through getUsers → getDaCtx → 500 response.

Fix: wrap the KV PUT in setUser() with a try-catch so near-expiry tokens
still authenticate the user for the current request; they just won't be
cached, which is the right behaviour for an almost-expired token anyway.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Comment thread src/utils/auth.js
@kptdobe
kptdobe merged commit 09e44e9 into main Apr 29, 2026
6 checks passed
@kptdobe
kptdobe deleted the fix/auth-token-expiry-ms-units branch April 29, 2026 11:11
adobe-bot pushed a commit that referenced this pull request Apr 29, 2026
## [1.6.2](v1.6.1...v1.6.2) (2026-04-29)

### Bug Fixes

* consumed body on retry ([#264](#264)) ([fffb056](fffb056))
* reject expired IMS tokens by comparing expiry in milliseconds ([#265](#265)) ([09e44e9](09e44e9))
@adobe-bot

Copy link
Copy Markdown
Collaborator

🎉 This PR is included in version 1.6.2 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants