Skip to content

Commit 3c71599

Browse files
committed
fix: some last nits
Signed-off-by: Martin Auer <martin.auer97@gmail.com>
1 parent 57c7592 commit 3c71599

2 files changed

Lines changed: 28 additions & 26 deletions

File tree

packages/common/lib/dpop/DPoP.ts

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import { v4 as uuidv4 } from 'uuid';
66
import {
77
calculateJwkThumbprint,
88
CreateJwtCallback,
9+
epochTime,
10+
getNowSkewed,
911
JWK,
1012
JwtHeader,
1113
JwtIssuerJwk,
@@ -20,32 +22,6 @@ export interface DPoPJwtIssuerWithContext extends JwtIssuerJwk {
2022
dPoPSigningAlgValuesSupported?: string[];
2123
}
2224

23-
/**
24-
* The maximum allowed clock skew time in seconds. If an time based validation
25-
* is performed against current time (`now`), the validation can be of by the skew
26-
* time.
27-
*
28-
* See https://datatracker.ietf.org/doc/html/rfc7519#section-4.1.5
29-
*/
30-
const DEFAULT_SKEW_TIME = 300;
31-
32-
function getNowSkewed(now?: number, skewTime?: number) {
33-
const _now = now ? now : epochTime();
34-
const _skewTime = skewTime ? skewTime : DEFAULT_SKEW_TIME;
35-
36-
return {
37-
nowSkewedPast: _now - _skewTime,
38-
nowSkewedFuture: _now + _skewTime,
39-
};
40-
}
41-
42-
/**
43-
* Returns the current unix timestamp in seconds.
44-
*/
45-
function epochTime() {
46-
return Math.floor(Date.now() / 1000);
47-
}
48-
4925
export type DPoPJwtPayloadProps = {
5026
htu: string;
5127
iat: number;

packages/common/lib/jwt/jwtUtils.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,29 @@ export function parseJWT<Header = JwtHeader, Payload = JwtPayload>(jwt: string)
1515
}
1616
return { header, payload };
1717
}
18+
19+
/**
20+
* The maximum allowed clock skew time in seconds. If an time based validation
21+
* is performed against current time (`now`), the validation can be of by the skew
22+
* time.
23+
*
24+
* See https://datatracker.ietf.org/doc/html/rfc7519#section-4.1.5
25+
*/
26+
const DEFAULT_SKEW_TIME = 300;
27+
28+
export function getNowSkewed(now?: number, skewTime?: number) {
29+
const _now = now ? now : epochTime();
30+
const _skewTime = skewTime ? skewTime : DEFAULT_SKEW_TIME;
31+
32+
return {
33+
nowSkewedPast: _now - _skewTime,
34+
nowSkewedFuture: _now + _skewTime,
35+
};
36+
}
37+
38+
/**
39+
* Returns the current unix timestamp in seconds.
40+
*/
41+
export function epochTime() {
42+
return Math.floor(Date.now() / 1000);
43+
}

0 commit comments

Comments
 (0)