Skip to content

feat: add Clock offset for signed timestamps - #2482

Open
mfsa93 wants to merge 1 commit into
authts:mainfrom
mfsa93:feat/clock-offset
Open

mfsa93 wants to merge 1 commit into
authts:mainfrom
mfsa93:feat/clock-offset

Conversation

@mfsa93

@mfsa93 mfsa93 commented Nov 12, 2026

Copy link
Copy Markdown

Addresses #2481 with the simpler global approach discussed in #414.

Problem

The library signs some timestamps itself and the authorization server validates them against its own clock: the iat of DPoP proofs and of client_secret_jwt client assertions. Both use Date.now() directly, so a device whose clock is a couple of minutes off cannot log in at all (Keycloak, for example, rejects the proof with DPoP proof is not active, accepting iat only ~2 s ahead of its clock by default), and the application has no supported way to correct it.

Change

Following the global configuration pattern suggested in #414 (like Log), this adds a small Clock namespace:

  • Clock.setOffset(ms): offset added to the local clock (server time minus local time); throws on non-finite values
  • Clock.reset(): back to the local clock
  • Clock.getEpochTime(): used by CryptoUtils.generateDPoPProof and CryptoUtils.generateClientAssertionJwt

Token expiration and the access token timers are unchanged, since they are computed from relative durations. Without calling Clock.setOffset(), behavior is identical to today.

The application owns how it obtains the server time (for example the Date header of a same-origin response), so no network call or time endpoint is added to the library.

Tests

  • Clock: default, offset, reset, invalid values
  • generateDPoPProof: iat from the local clock by default and with the offset applied

Docs: new "Clock offset" section in docs/index.md.

@sivaraman-krishnan sivaraman-krishnan left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this, @mfsa93. It's a small, contained change, and following the global, Log-style approach from #414 keeps it simple. I went through where the library uses time, and the scope looks right to me. The DPoP proof iat and the client_secret_jwt iat/exp are the only timestamps it signs. Everything else uses the unadjusted local clock: token expiry calculated from expires_in (User.expires_at, SigninResponse.expires_at), State.created and its staleness cutoff, and the access-token timers. So changing the offset doesn't affect those, and leaving them on Timer is correct. The math checks out too: the millisecond offset is added before flooring, the default offset keeps today's behavior, NaN/Infinity are rejected, and Timer stays internal, so there's no public naming clash. The new Clock and CryptoUtils tests pass locally.

Two small suggestions inline: a test for the client-assertion path, and a note on where the server time comes from in the docs example.

One scope note, mostly for the maintainers: #2481 asks for a clock hook scoped to each code exchange or refresh, and describes itself as narrower than the general clock service from #414. This PR goes the other way and takes the simpler global approach from #414. That fixes the static-skew case, but it isn't the per-operation hook #2481 describes (for example, a fresh estimate for the nonce retry), so it might be worth confirming whether #2481 should be closed by this or stay open for that.

Comment thread src/utils/CryptoUtils.ts
*/
public static async generateClientAssertionJwt(client_id: string, client_secret: string, audience: string, algorithm: string = "HS256"): Promise<string> {
const now = Math.floor(Date.now() / 1000);
const now = Clock.getEpochTime();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the only signer that emits both iat and exp, and there's no test for it with an offset yet. The current tests use whole-second offsets (-120_000, 60_000), so a later "simplification" such as Math.floor(Date.now() / 1000) + Math.floor(offset / 1000) would still pass, even though it can be off by a second when the offset has a sub-second part. For example, with Date.now() = 1_000_400 and an offset of -300, the combined floor is 1000 but the separate floors give 999. A fake-timer test on generateClientAssertionJwt would cover both the second call site and the rounding. It needs a clock whose milliseconds don't line up with the offset, like that one (the existing fake times are on whole seconds, where both formulas agree), and should assert iat === 1000 and exp === iat + 300.

Comment thread docs/index.md
```javascript
import { Clock } from 'oidc-client-ts';

const response = await fetch('/', { method: 'HEAD', cache: 'no-store' });

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two things might be worth a sentence here. First, this works because the request is same-origin. Date isn't a CORS-safelisted response header, so for a cross-origin endpoint the response has to expose it, either with Access-Control-Expose-Headers: Date, or with * when the request's credentials mode isn't include. Otherwise get('date') returns null and the Number.isFinite guard silently skips the offset. Second, the offset is relative to whatever produced that Date header (the app server, or a CDN or proxy in front of it), which only helps if that clock is close enough to the verifier's: the authorization server, or for proofs from UserManager.dpopProof, the resource server. That's usually true with NTP, but worth stating, since the Date header's one-second granularity, the response latency and any difference between the clocks all eat into the verifier's acceptance window.

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants