Repository navigation
Conversation
sivaraman-krishnan
left a comment
There was a problem hiding this comment.
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.
| */ | ||
| 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(); |
There was a problem hiding this comment.
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.
| ```javascript | ||
| import { Clock } from 'oidc-client-ts'; | ||
|
|
||
| const response = await fetch('/', { method: 'HEAD', cache: 'no-store' }); |
There was a problem hiding this comment.
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.
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
iatof DPoP proofs and ofclient_secret_jwtclient assertions. Both useDate.now()directly, so a device whose clock is a couple of minutes off cannot log in at all (Keycloak, for example, rejects the proof withDPoP proof is not active, acceptingiatonly ~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 smallClocknamespace:Clock.setOffset(ms): offset added to the local clock (server time minus local time); throws on non-finite valuesClock.reset(): back to the local clockClock.getEpochTime(): used byCryptoUtils.generateDPoPProofandCryptoUtils.generateClientAssertionJwtToken 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
Dateheader of a same-origin response), so no network call or time endpoint is added to the library.Tests
Clock: default, offset, reset, invalid valuesgenerateDPoPProof:iatfrom the local clock by default and with the offset appliedDocs: new "Clock offset" section in
docs/index.md.