@saurbit/oauth2-jwt 0.1.1 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/esm/jose_jwks_authority.js +3 -3
- package/esm/jwks_rotator.d.ts +3 -3
- package/esm/jwks_rotator.d.ts.map +1 -1
- package/esm/jwks_rotator.js +5 -5
- package/package.json +1 -1
- package/script/jose_jwks_authority.js +3 -3
- package/script/jwks_rotator.d.ts +3 -3
- package/script/jwks_rotator.d.ts.map +1 -1
- package/script/jwks_rotator.js +5 -5
package/README.md
CHANGED
|
@@ -132,7 +132,7 @@ const authority = new JoseJwksAuthority(store, 8.64e+6); // 100 days key TTL
|
|
|
132
132
|
|
|
133
133
|
const rotator = new JwksRotator({
|
|
134
134
|
keyGenerator: authority,
|
|
135
|
-
|
|
135
|
+
rotationTimestampStore: store,
|
|
136
136
|
rotationIntervalMs: 7.884e9, // 91 days
|
|
137
137
|
});
|
|
138
138
|
```
|
|
@@ -124,7 +124,7 @@ export class JoseJwksAuthority {
|
|
|
124
124
|
}
|
|
125
125
|
const privateKey = await importJWK(key, "RS256");
|
|
126
126
|
const token = await new SignJWT(payload)
|
|
127
|
-
.setProtectedHeader({ typ: "
|
|
127
|
+
.setProtectedHeader({ typ: "JWT", alg: "RS256", kid: key.kid })
|
|
128
128
|
.sign(privateKey);
|
|
129
129
|
return { token, kid: key.kid };
|
|
130
130
|
}
|
|
@@ -154,7 +154,7 @@ export class JoseJwksAuthority {
|
|
|
154
154
|
if ("jwk" in protectedHeader) {
|
|
155
155
|
throw new Error("Unexpected JWK in header - potential forgery attempt");
|
|
156
156
|
}
|
|
157
|
-
if (protectedHeader.typ && protectedHeader.typ.
|
|
157
|
+
if (protectedHeader.typ && protectedHeader.typ.toUpperCase() !== "JWT") {
|
|
158
158
|
throw new Error(`Unexpected typ: ${protectedHeader.typ}`);
|
|
159
159
|
}
|
|
160
160
|
return payload;
|
|
@@ -178,7 +178,7 @@ export class JoseJwksAuthority {
|
|
|
178
178
|
const result = [];
|
|
179
179
|
for (const payload of payloads) {
|
|
180
180
|
const token = await new SignJWT(payload)
|
|
181
|
-
.setProtectedHeader({ typ: "
|
|
181
|
+
.setProtectedHeader({ typ: "JWT", alg: "RS256", kid: key.kid })
|
|
182
182
|
.sign(privateKey);
|
|
183
183
|
result.push({ token, kid: key.kid });
|
|
184
184
|
}
|
package/esm/jwks_rotator.d.ts
CHANGED
|
@@ -6,7 +6,7 @@ export interface JwksRotatorOptions {
|
|
|
6
6
|
/** The key generator used to produce new signing key pairs during rotation. */
|
|
7
7
|
keyGenerator: KeyGenerator;
|
|
8
8
|
/** The store used to read and persist the last rotation timestamp. */
|
|
9
|
-
|
|
9
|
+
rotationTimestampStore: JwksRotationTimestampStore;
|
|
10
10
|
/** How often (in milliseconds) new keys should be generated. For example, `7.884e9` for 91 days. */
|
|
11
11
|
rotationIntervalMs: number;
|
|
12
12
|
}
|
|
@@ -20,12 +20,12 @@ export interface JwksRotatorOptions {
|
|
|
20
20
|
*/
|
|
21
21
|
export declare class JwksRotator {
|
|
22
22
|
private readonly keyGenerator;
|
|
23
|
-
private readonly
|
|
23
|
+
private readonly rotationTimestampStore;
|
|
24
24
|
private readonly rotationIntervalMs;
|
|
25
25
|
/**
|
|
26
26
|
* @param options - Rotation configuration: key generator, timestamp store, and interval.
|
|
27
27
|
*/
|
|
28
|
-
constructor({ keyGenerator, rotationIntervalMs,
|
|
28
|
+
constructor({ keyGenerator, rotationIntervalMs, rotationTimestampStore }: JwksRotatorOptions);
|
|
29
29
|
/**
|
|
30
30
|
* Checks if rotation is due, and performs rotation if necessary.
|
|
31
31
|
* Should be called at service startup or on a schedule (e.g. every hour).
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"jwks_rotator.d.ts","sourceRoot":"","sources":["../src/jwks_rotator.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,0BAA0B,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAEtE;;GAEG;AACH,MAAM,WAAW,kBAAkB;IACjC,+EAA+E;IAC/E,YAAY,EAAE,YAAY,CAAC;IAC3B,sEAAsE;IACtE,
|
|
1
|
+
{"version":3,"file":"jwks_rotator.d.ts","sourceRoot":"","sources":["../src/jwks_rotator.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,0BAA0B,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAEtE;;GAEG;AACH,MAAM,WAAW,kBAAkB;IACjC,+EAA+E;IAC/E,YAAY,EAAE,YAAY,CAAC;IAC3B,sEAAsE;IACtE,sBAAsB,EAAE,0BAA0B,CAAC;IACnD,oGAAoG;IACpG,kBAAkB,EAAE,MAAM,CAAC;CAC5B;AAED;;;;;;;GAOG;AACH,qBAAa,WAAW;IACtB,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAe;IAC5C,OAAO,CAAC,QAAQ,CAAC,sBAAsB,CAA6B;IACpE,OAAO,CAAC,QAAQ,CAAC,kBAAkB,CAAS;IAE5C;;OAEG;gBACS,EAAE,YAAY,EAAE,kBAAkB,EAAE,sBAAsB,EAAE,EAAE,kBAAkB;IAM5F;;;OAGG;IACU,kBAAkB,IAAI,OAAO,CAAC,IAAI,CAAC;YAYlC,UAAU;CAGzB"}
|
package/esm/jwks_rotator.js
CHANGED
|
@@ -10,14 +10,14 @@ export class JwksRotator {
|
|
|
10
10
|
/**
|
|
11
11
|
* @param options - Rotation configuration: key generator, timestamp store, and interval.
|
|
12
12
|
*/
|
|
13
|
-
constructor({ keyGenerator, rotationIntervalMs,
|
|
13
|
+
constructor({ keyGenerator, rotationIntervalMs, rotationTimestampStore }) {
|
|
14
14
|
Object.defineProperty(this, "keyGenerator", {
|
|
15
15
|
enumerable: true,
|
|
16
16
|
configurable: true,
|
|
17
17
|
writable: true,
|
|
18
18
|
value: void 0
|
|
19
19
|
});
|
|
20
|
-
Object.defineProperty(this, "
|
|
20
|
+
Object.defineProperty(this, "rotationTimestampStore", {
|
|
21
21
|
enumerable: true,
|
|
22
22
|
configurable: true,
|
|
23
23
|
writable: true,
|
|
@@ -31,7 +31,7 @@ export class JwksRotator {
|
|
|
31
31
|
});
|
|
32
32
|
this.keyGenerator = keyGenerator;
|
|
33
33
|
this.rotationIntervalMs = rotationIntervalMs;
|
|
34
|
-
this.
|
|
34
|
+
this.rotationTimestampStore = rotationTimestampStore;
|
|
35
35
|
}
|
|
36
36
|
/**
|
|
37
37
|
* Checks if rotation is due, and performs rotation if necessary.
|
|
@@ -39,10 +39,10 @@ export class JwksRotator {
|
|
|
39
39
|
*/
|
|
40
40
|
async checkAndRotateKeys() {
|
|
41
41
|
const now = Date.now();
|
|
42
|
-
const lastRotation = await this.
|
|
42
|
+
const lastRotation = await this.rotationTimestampStore.getLastRotationTimestamp();
|
|
43
43
|
if (isNaN(lastRotation) || now - lastRotation >= this.rotationIntervalMs) {
|
|
44
44
|
await this.rotateKeys();
|
|
45
|
-
await this.
|
|
45
|
+
await this.rotationTimestampStore.setLastRotationTimestamp(now);
|
|
46
46
|
}
|
|
47
47
|
else {
|
|
48
48
|
const _nextIn = this.rotationIntervalMs - (now - lastRotation);
|
package/package.json
CHANGED
|
@@ -127,7 +127,7 @@ class JoseJwksAuthority {
|
|
|
127
127
|
}
|
|
128
128
|
const privateKey = await (0, jose_1.importJWK)(key, "RS256");
|
|
129
129
|
const token = await new jose_1.SignJWT(payload)
|
|
130
|
-
.setProtectedHeader({ typ: "
|
|
130
|
+
.setProtectedHeader({ typ: "JWT", alg: "RS256", kid: key.kid })
|
|
131
131
|
.sign(privateKey);
|
|
132
132
|
return { token, kid: key.kid };
|
|
133
133
|
}
|
|
@@ -157,7 +157,7 @@ class JoseJwksAuthority {
|
|
|
157
157
|
if ("jwk" in protectedHeader) {
|
|
158
158
|
throw new Error("Unexpected JWK in header - potential forgery attempt");
|
|
159
159
|
}
|
|
160
|
-
if (protectedHeader.typ && protectedHeader.typ.
|
|
160
|
+
if (protectedHeader.typ && protectedHeader.typ.toUpperCase() !== "JWT") {
|
|
161
161
|
throw new Error(`Unexpected typ: ${protectedHeader.typ}`);
|
|
162
162
|
}
|
|
163
163
|
return payload;
|
|
@@ -181,7 +181,7 @@ class JoseJwksAuthority {
|
|
|
181
181
|
const result = [];
|
|
182
182
|
for (const payload of payloads) {
|
|
183
183
|
const token = await new jose_1.SignJWT(payload)
|
|
184
|
-
.setProtectedHeader({ typ: "
|
|
184
|
+
.setProtectedHeader({ typ: "JWT", alg: "RS256", kid: key.kid })
|
|
185
185
|
.sign(privateKey);
|
|
186
186
|
result.push({ token, kid: key.kid });
|
|
187
187
|
}
|
package/script/jwks_rotator.d.ts
CHANGED
|
@@ -6,7 +6,7 @@ export interface JwksRotatorOptions {
|
|
|
6
6
|
/** The key generator used to produce new signing key pairs during rotation. */
|
|
7
7
|
keyGenerator: KeyGenerator;
|
|
8
8
|
/** The store used to read and persist the last rotation timestamp. */
|
|
9
|
-
|
|
9
|
+
rotationTimestampStore: JwksRotationTimestampStore;
|
|
10
10
|
/** How often (in milliseconds) new keys should be generated. For example, `7.884e9` for 91 days. */
|
|
11
11
|
rotationIntervalMs: number;
|
|
12
12
|
}
|
|
@@ -20,12 +20,12 @@ export interface JwksRotatorOptions {
|
|
|
20
20
|
*/
|
|
21
21
|
export declare class JwksRotator {
|
|
22
22
|
private readonly keyGenerator;
|
|
23
|
-
private readonly
|
|
23
|
+
private readonly rotationTimestampStore;
|
|
24
24
|
private readonly rotationIntervalMs;
|
|
25
25
|
/**
|
|
26
26
|
* @param options - Rotation configuration: key generator, timestamp store, and interval.
|
|
27
27
|
*/
|
|
28
|
-
constructor({ keyGenerator, rotationIntervalMs,
|
|
28
|
+
constructor({ keyGenerator, rotationIntervalMs, rotationTimestampStore }: JwksRotatorOptions);
|
|
29
29
|
/**
|
|
30
30
|
* Checks if rotation is due, and performs rotation if necessary.
|
|
31
31
|
* Should be called at service startup or on a schedule (e.g. every hour).
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"jwks_rotator.d.ts","sourceRoot":"","sources":["../src/jwks_rotator.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,0BAA0B,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAEtE;;GAEG;AACH,MAAM,WAAW,kBAAkB;IACjC,+EAA+E;IAC/E,YAAY,EAAE,YAAY,CAAC;IAC3B,sEAAsE;IACtE,
|
|
1
|
+
{"version":3,"file":"jwks_rotator.d.ts","sourceRoot":"","sources":["../src/jwks_rotator.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,0BAA0B,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAEtE;;GAEG;AACH,MAAM,WAAW,kBAAkB;IACjC,+EAA+E;IAC/E,YAAY,EAAE,YAAY,CAAC;IAC3B,sEAAsE;IACtE,sBAAsB,EAAE,0BAA0B,CAAC;IACnD,oGAAoG;IACpG,kBAAkB,EAAE,MAAM,CAAC;CAC5B;AAED;;;;;;;GAOG;AACH,qBAAa,WAAW;IACtB,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAe;IAC5C,OAAO,CAAC,QAAQ,CAAC,sBAAsB,CAA6B;IACpE,OAAO,CAAC,QAAQ,CAAC,kBAAkB,CAAS;IAE5C;;OAEG;gBACS,EAAE,YAAY,EAAE,kBAAkB,EAAE,sBAAsB,EAAE,EAAE,kBAAkB;IAM5F;;;OAGG;IACU,kBAAkB,IAAI,OAAO,CAAC,IAAI,CAAC;YAYlC,UAAU;CAGzB"}
|
package/script/jwks_rotator.js
CHANGED
|
@@ -13,14 +13,14 @@ class JwksRotator {
|
|
|
13
13
|
/**
|
|
14
14
|
* @param options - Rotation configuration: key generator, timestamp store, and interval.
|
|
15
15
|
*/
|
|
16
|
-
constructor({ keyGenerator, rotationIntervalMs,
|
|
16
|
+
constructor({ keyGenerator, rotationIntervalMs, rotationTimestampStore }) {
|
|
17
17
|
Object.defineProperty(this, "keyGenerator", {
|
|
18
18
|
enumerable: true,
|
|
19
19
|
configurable: true,
|
|
20
20
|
writable: true,
|
|
21
21
|
value: void 0
|
|
22
22
|
});
|
|
23
|
-
Object.defineProperty(this, "
|
|
23
|
+
Object.defineProperty(this, "rotationTimestampStore", {
|
|
24
24
|
enumerable: true,
|
|
25
25
|
configurable: true,
|
|
26
26
|
writable: true,
|
|
@@ -34,7 +34,7 @@ class JwksRotator {
|
|
|
34
34
|
});
|
|
35
35
|
this.keyGenerator = keyGenerator;
|
|
36
36
|
this.rotationIntervalMs = rotationIntervalMs;
|
|
37
|
-
this.
|
|
37
|
+
this.rotationTimestampStore = rotationTimestampStore;
|
|
38
38
|
}
|
|
39
39
|
/**
|
|
40
40
|
* Checks if rotation is due, and performs rotation if necessary.
|
|
@@ -42,10 +42,10 @@ class JwksRotator {
|
|
|
42
42
|
*/
|
|
43
43
|
async checkAndRotateKeys() {
|
|
44
44
|
const now = Date.now();
|
|
45
|
-
const lastRotation = await this.
|
|
45
|
+
const lastRotation = await this.rotationTimestampStore.getLastRotationTimestamp();
|
|
46
46
|
if (isNaN(lastRotation) || now - lastRotation >= this.rotationIntervalMs) {
|
|
47
47
|
await this.rotateKeys();
|
|
48
|
-
await this.
|
|
48
|
+
await this.rotationTimestampStore.setLastRotationTimestamp(now);
|
|
49
49
|
}
|
|
50
50
|
else {
|
|
51
51
|
const _nextIn = this.rotationIntervalMs - (now - lastRotation);
|