diff --git a/packages/common/lib/dpop/DPoP.ts b/packages/common/lib/dpop/DPoP.ts index 2b174ed3..1186361a 100644 --- a/packages/common/lib/dpop/DPoP.ts +++ b/packages/common/lib/dpop/DPoP.ts @@ -6,6 +6,8 @@ import { v4 as uuidv4 } from 'uuid'; import { calculateJwkThumbprint, CreateJwtCallback, + epochTime, + getNowSkewed, JWK, JwtHeader, JwtIssuerJwk, @@ -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; diff --git a/packages/common/lib/jwt/jwtUtils.ts b/packages/common/lib/jwt/jwtUtils.ts index 6303b51c..61353032 100644 --- a/packages/common/lib/jwt/jwtUtils.ts +++ b/packages/common/lib/jwt/jwtUtils.ts @@ -15,3 +15,29 @@ export function parseJWT
(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); +}