Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions packages/auth-foundation/src/oauth2/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { UserInfo } from './requests/UserInfo.ts';
import { PromiseQueue } from '../utils/PromiseQueue.ts';
import { EventEmitter } from '../utils/EventEmitter.ts';
import { hasSameValues } from '../utils/index.ts';
import TimeCoordinator, { Timestamp } from '../utils/TimeCoordinator.ts';


// ref: https://developer.okta.com/docs/reference/api/oidc/
Expand Down Expand Up @@ -106,6 +107,18 @@ export class OAuth2Client extends APIClient {
await this.dpopSigningAuthority.sign(request, { keyPairId: dpopPairId, nonce });
}

protected async processResponse(response: Response, request: APIRequest): Promise<void> {
await super.processResponse(response, request);

// NOTE: this logic will not work on CORS requests, the Date header needs to be allowlisted via access-control-expose-headers
const dateHeader = response.headers.get('date');
if (dateHeader) {
const serverTime = Timestamp.from(new Date(dateHeader));
const skew = Math.round(serverTime.timeSince(Date.now() / 1000));
TimeCoordinator.clockSkew = skew;
}
}

/** @internal */
protected async getJson (url: URL, options: OAuth2Client.GetJsonOptions = {}): Promise<JsonRecord> {
const { skipCache } = { ...OAuth2Client.DefaultGetJsonOptions, ...options };
Expand Down
18 changes: 14 additions & 4 deletions packages/auth-foundation/src/utils/TimeCoordinator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,26 @@ export class Timestamp {
*/
// TODO: implement (post beta)
class TimeCoordinator {
#skew = 0;
static #tolerance = 0;

// TODO: adjust from http time headers
// (backend change needed to allow Date header in CORS requests)
get clockSkew () {
return 0;
get clockSkew (): number {
return this.#skew;
}

set clockSkew (skew: number) {
this.#skew = skew;
}

// TODO: accept via config option
static get clockTolerance () {
return 0;
static get clockTolerance (): number {
return TimeCoordinator.#tolerance;
}

static set clockTolerance (tolerance: number) {
TimeCoordinator.#tolerance = tolerance;
}

now (): Timestamp {
Expand Down