Skip to content

Commit

Permalink
fix: some last nits
Browse files Browse the repository at this point in the history
Signed-off-by: Martin Auer <[email protected]>
  • Loading branch information
auer-martin committed Jul 29, 2024
1 parent 57c7592 commit 3c71599
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 26 deletions.
28 changes: 2 additions & 26 deletions packages/common/lib/dpop/DPoP.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { v4 as uuidv4 } from 'uuid';
import {
calculateJwkThumbprint,
CreateJwtCallback,
epochTime,
getNowSkewed,
JWK,
JwtHeader,
JwtIssuerJwk,
Expand All @@ -20,32 +22,6 @@ export interface DPoPJwtIssuerWithContext extends JwtIssuerJwk {
dPoPSigningAlgValuesSupported?: string[];
}

/**
* The maximum allowed clock skew time in seconds. If an time based validation
* is performed against current time (`now`), the validation can be of by the skew
* time.
*
* See https://datatracker.ietf.org/doc/html/rfc7519#section-4.1.5
*/
const DEFAULT_SKEW_TIME = 300;

function getNowSkewed(now?: number, skewTime?: number) {
const _now = now ? now : epochTime();
const _skewTime = skewTime ? skewTime : DEFAULT_SKEW_TIME;

return {
nowSkewedPast: _now - _skewTime,
nowSkewedFuture: _now + _skewTime,
};
}

/**
* Returns the current unix timestamp in seconds.
*/
function epochTime() {
return Math.floor(Date.now() / 1000);
}

export type DPoPJwtPayloadProps = {
htu: string;
iat: number;
Expand Down
26 changes: 26 additions & 0 deletions packages/common/lib/jwt/jwtUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,29 @@ export function parseJWT<Header = JwtHeader, Payload = JwtPayload>(jwt: string)
}
return { header, payload };
}

/**
* The maximum allowed clock skew time in seconds. If an time based validation
* is performed against current time (`now`), the validation can be of by the skew
* time.
*
* See https://datatracker.ietf.org/doc/html/rfc7519#section-4.1.5
*/
const DEFAULT_SKEW_TIME = 300;

export function getNowSkewed(now?: number, skewTime?: number) {
const _now = now ? now : epochTime();
const _skewTime = skewTime ? skewTime : DEFAULT_SKEW_TIME;

return {
nowSkewedPast: _now - _skewTime,
nowSkewedFuture: _now + _skewTime,
};
}

/**
* Returns the current unix timestamp in seconds.
*/
export function epochTime() {
return Math.floor(Date.now() / 1000);
}

0 comments on commit 3c71599

Please sign in to comment.