@saurbit/oauth2-jwt 0.1.0
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/LICENSE +21 -0
- package/README.md +152 -0
- package/esm/jose_jwks_authority.d.ts +90 -0
- package/esm/jose_jwks_authority.d.ts.map +1 -0
- package/esm/jose_jwks_authority.js +214 -0
- package/esm/jwks_key_store.d.ts +53 -0
- package/esm/jwks_key_store.d.ts.map +1 -0
- package/esm/jwks_key_store.js +86 -0
- package/esm/jwks_rotator.d.ts +36 -0
- package/esm/jwks_rotator.d.ts.map +1 -0
- package/esm/jwks_rotator.js +54 -0
- package/esm/methods.d.ts +39 -0
- package/esm/methods.d.ts.map +1 -0
- package/esm/methods.js +52 -0
- package/esm/mod.d.ts +6 -0
- package/esm/mod.d.ts.map +1 -0
- package/esm/mod.js +4 -0
- package/esm/package.json +3 -0
- package/esm/types.d.ts +179 -0
- package/esm/types.d.ts.map +1 -0
- package/esm/types.js +1 -0
- package/package.json +33 -0
- package/script/jose_jwks_authority.d.ts +90 -0
- package/script/jose_jwks_authority.d.ts.map +1 -0
- package/script/jose_jwks_authority.js +218 -0
- package/script/jwks_key_store.d.ts +53 -0
- package/script/jwks_key_store.d.ts.map +1 -0
- package/script/jwks_key_store.js +91 -0
- package/script/jwks_rotator.d.ts +36 -0
- package/script/jwks_rotator.d.ts.map +1 -0
- package/script/jwks_rotator.js +58 -0
- package/script/methods.d.ts +39 -0
- package/script/methods.d.ts.map +1 -0
- package/script/methods.js +58 -0
- package/script/mod.d.ts +6 -0
- package/script/mod.d.ts.map +1 -0
- package/script/mod.js +14 -0
- package/script/package.json +3 -0
- package/script/types.d.ts +179 -0
- package/script/types.d.ts.map +1 -0
- package/script/types.js +2 -0
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
import type { JWTPayload } from "jose";
|
|
2
|
+
/**
|
|
3
|
+
* Represents an RSA public or private key in JSON Web Key (JWK) format.
|
|
4
|
+
*
|
|
5
|
+
* When used as a public key only `kty`, `e`, and `n` are present.
|
|
6
|
+
* The private key fields (`d`, `p`, `q`, `dp`, `dq`, `qi`) are included on private keys.
|
|
7
|
+
*
|
|
8
|
+
* @see {@link https://www.rfc-editor.org/rfc/rfc7517} JSON Web Key (JWK)
|
|
9
|
+
*/
|
|
10
|
+
export interface RSA {
|
|
11
|
+
/** Key type. Always `"RSA"` for RSA keys. */
|
|
12
|
+
kty: "RSA";
|
|
13
|
+
/** RSA public exponent, Base64URL-encoded. */
|
|
14
|
+
e: string;
|
|
15
|
+
/** RSA modulus, Base64URL-encoded. */
|
|
16
|
+
n: string;
|
|
17
|
+
/** RSA private exponent, Base64URL-encoded. Present only on private keys. */
|
|
18
|
+
d?: string | undefined;
|
|
19
|
+
/** First prime factor, Base64URL-encoded. Present only on private keys. */
|
|
20
|
+
p?: string | undefined;
|
|
21
|
+
/** Second prime factor, Base64URL-encoded. Present only on private keys. */
|
|
22
|
+
q?: string | undefined;
|
|
23
|
+
/** First factor CRT exponent, Base64URL-encoded. Present only on private keys. */
|
|
24
|
+
dp?: string | undefined;
|
|
25
|
+
/** Second factor CRT exponent, Base64URL-encoded. Present only on private keys. */
|
|
26
|
+
dq?: string | undefined;
|
|
27
|
+
/** First CRT coefficient, Base64URL-encoded. Present only on private keys. */
|
|
28
|
+
qi?: string | undefined;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Represents a public key as exposed in a JSON Web Key Set (JWKS).
|
|
32
|
+
* This is the format returned by the JWKS endpoint and used by JWT verifiers
|
|
33
|
+
* to validate token signatures.
|
|
34
|
+
*
|
|
35
|
+
* @see {@link https://www.rfc-editor.org/rfc/rfc7517} JSON Web Key (JWK)
|
|
36
|
+
*/
|
|
37
|
+
export interface RawKey {
|
|
38
|
+
/** The algorithm intended for use with this key (e.g. `"RS256"`). */
|
|
39
|
+
alg: string;
|
|
40
|
+
/** Key type (e.g. `"RSA"`). */
|
|
41
|
+
kty: string;
|
|
42
|
+
/** Intended use of the key: signature (`"sig"`), encryption (`"enc"`), or description (`"desc"`). */
|
|
43
|
+
use: "sig" | "enc" | "desc";
|
|
44
|
+
/** Key ID - a unique identifier used to match a JWT `kid` header to the correct public key. */
|
|
45
|
+
kid: string;
|
|
46
|
+
/** RSA public exponent, Base64URL-encoded. Together with `n`, forms the RSA public key. */
|
|
47
|
+
e: string;
|
|
48
|
+
/** RSA modulus, Base64URL-encoded. Together with `e`, forms the RSA public key. */
|
|
49
|
+
n: string;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Persists the timestamp of the last JWKS key rotation.
|
|
53
|
+
* Used by {@link JwksRotator} to determine when the next rotation is due.
|
|
54
|
+
*
|
|
55
|
+
* Implement this interface backed by a persistent or distributed store
|
|
56
|
+
* (e.g. Redis, database) when running multiple service instances.
|
|
57
|
+
*/
|
|
58
|
+
export interface JwksRotationTimestampStore {
|
|
59
|
+
/**
|
|
60
|
+
* Retrieves the Unix timestamp (in milliseconds) of the last key rotation.
|
|
61
|
+
* @returns The timestamp of the last rotation, or `0` if no rotation has occurred.
|
|
62
|
+
*/
|
|
63
|
+
getLastRotationTimestamp(): Promise<number>;
|
|
64
|
+
/**
|
|
65
|
+
* Persists the Unix timestamp (in milliseconds) of the most recent key rotation.
|
|
66
|
+
* @param rotationTimestamp - The timestamp to store.
|
|
67
|
+
*/
|
|
68
|
+
setLastRotationTimestamp(rotationTimestamp: number): Promise<void>;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Persists the active signing key pair and the set of valid public keys
|
|
72
|
+
* that are exposed via the JWKS endpoint.
|
|
73
|
+
*
|
|
74
|
+
* Implement this interface backed by a persistent or distributed store
|
|
75
|
+
* (e.g. Redis, database) when running multiple service instances.
|
|
76
|
+
* The built-in {@link InMemoryKeyStore} is suitable for single-process deployments.
|
|
77
|
+
*/
|
|
78
|
+
export interface JwksKeyStore {
|
|
79
|
+
/**
|
|
80
|
+
* Stores the current active private key and its corresponding public key.
|
|
81
|
+
* The public key will be kept for the duration of the TTL for JWKS purposes.
|
|
82
|
+
*/
|
|
83
|
+
storeKeyPair(kid: string, privateKey: object, publicKey: object, ttl: number): void | Promise<void>;
|
|
84
|
+
/**
|
|
85
|
+
* Retrieves the current private key used for signing.
|
|
86
|
+
*/
|
|
87
|
+
getPrivateKey(): Promise<object | undefined>;
|
|
88
|
+
/**
|
|
89
|
+
* Retrieves all valid public keys that have not expired.
|
|
90
|
+
* These are used for exposing in JWKS.
|
|
91
|
+
*/
|
|
92
|
+
getPublicKeys(): Promise<object[]>;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Generates a new signing key pair and stores it in the associated {@link JwksKeyStore}.
|
|
96
|
+
* Used by {@link JwksRotator} to perform key rotation.
|
|
97
|
+
*/
|
|
98
|
+
export interface KeyGenerator {
|
|
99
|
+
/**
|
|
100
|
+
* Generates a new key pair and persists it to the key store.
|
|
101
|
+
*/
|
|
102
|
+
generateKeyPair(): Promise<void>;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* An object capable of signing a JWT payload and returning the compact serialized token.
|
|
106
|
+
*
|
|
107
|
+
* Implement this interface to plug in a custom JWT signing strategy.
|
|
108
|
+
*/
|
|
109
|
+
export interface JwtSigner {
|
|
110
|
+
/**
|
|
111
|
+
* Signs the given payload as a JWT.
|
|
112
|
+
* @param payload - The JWT payload to sign.
|
|
113
|
+
* @returns An object containing the signed compact JWT string and the `kid` of the key used for signing.
|
|
114
|
+
*/
|
|
115
|
+
sign(payload: JWTPayload): Promise<{
|
|
116
|
+
token: string;
|
|
117
|
+
kid: string;
|
|
118
|
+
}>;
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* An object capable of verifying a JWT and returning its typed payload.
|
|
122
|
+
*
|
|
123
|
+
* Implement this interface to plug in a custom JWT verification strategy
|
|
124
|
+
* (e.g. backed by a JWKS endpoint, a local key store, or a third-party library).
|
|
125
|
+
*/
|
|
126
|
+
export interface JwtVerifier {
|
|
127
|
+
/**
|
|
128
|
+
* Verifies the given JWT and returns its decoded payload.
|
|
129
|
+
*
|
|
130
|
+
* @template P - The expected shape of the JWT payload. Defaults to {@link JWTPayload}.
|
|
131
|
+
* @param token - The compact serialized JWT string to verify.
|
|
132
|
+
* @returns The verified and decoded payload.
|
|
133
|
+
* @throws If the token is invalid, expired, or cannot be verified.
|
|
134
|
+
*/
|
|
135
|
+
verify<P extends JWTPayload = JWTPayload>(token: string): Promise<P>;
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* A full JWT authority combining signing, verification, and JWKS key management.
|
|
139
|
+
*
|
|
140
|
+
* A `JwtAuthority` is responsible for:
|
|
141
|
+
* - Signing JWT payloads with the current private key ({@link JwtSigner.sign})
|
|
142
|
+
* - Verifying JWTs against the matching public key ({@link JwtVerifier.verify})
|
|
143
|
+
* - Exposing the JWKS (set of public keys) for external consumers
|
|
144
|
+
* - Generating new key pairs on demand or via a scheduled {@link JwksRotator}
|
|
145
|
+
*
|
|
146
|
+
* Use {@link JoseJwksAuthority} as the ready-made implementation backed by [jose](https://github.com/panva/jose).
|
|
147
|
+
*/
|
|
148
|
+
export interface JwtAuthority extends JwtVerifier, JwtSigner {
|
|
149
|
+
/**
|
|
150
|
+
* Retrieves all currently valid public keys from the key store.
|
|
151
|
+
* @returns An object whose `keys` array contains the public keys in JWK format.
|
|
152
|
+
*/
|
|
153
|
+
getPublicKeys(): Promise<{
|
|
154
|
+
keys: RawKey[];
|
|
155
|
+
}>;
|
|
156
|
+
/**
|
|
157
|
+
* Get current kid for observability/debugging
|
|
158
|
+
*/
|
|
159
|
+
getCurrentKid(): Promise<string | undefined>;
|
|
160
|
+
/**
|
|
161
|
+
* Helper for JWKS endpoint
|
|
162
|
+
*/
|
|
163
|
+
getJwksEndpointResponse(): Promise<{
|
|
164
|
+
keys: RawKey[];
|
|
165
|
+
}>;
|
|
166
|
+
/**
|
|
167
|
+
* Retrieves the public key corresponding to the given `kid` from the key store.
|
|
168
|
+
* @param kid - The key ID to look up.
|
|
169
|
+
* @returns The RSA public key if found, or `undefined` if not found or not an RSA key.
|
|
170
|
+
*/
|
|
171
|
+
getPublicKey(kid: string): Promise<RSA | undefined>;
|
|
172
|
+
/**
|
|
173
|
+
* Generates a new RSA key pair and stores it in the key store.
|
|
174
|
+
* The new key becomes the active signing key; the previous public key
|
|
175
|
+
* remains available in the JWKS until its TTL expires.
|
|
176
|
+
*/
|
|
177
|
+
generateKeyPair(): Promise<void>;
|
|
178
|
+
}
|
|
179
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,MAAM,CAAC;AAEvC;;;;;;;GAOG;AACH,MAAM,WAAW,GAAG;IAClB,6CAA6C;IAC7C,GAAG,EAAE,KAAK,CAAC;IACX,8CAA8C;IAC9C,CAAC,EAAE,MAAM,CAAC;IACV,sCAAsC;IACtC,CAAC,EAAE,MAAM,CAAC;IACV,6EAA6E;IAC7E,CAAC,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IACvB,2EAA2E;IAC3E,CAAC,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IACvB,4EAA4E;IAC5E,CAAC,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IACvB,kFAAkF;IAClF,EAAE,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IACxB,mFAAmF;IACnF,EAAE,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IACxB,8EAA8E;IAC9E,EAAE,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;CACzB;AAED;;;;;;GAMG;AACH,MAAM,WAAW,MAAM;IACrB,qEAAqE;IACrE,GAAG,EAAE,MAAM,CAAC;IACZ,+BAA+B;IAC/B,GAAG,EAAE,MAAM,CAAC;IACZ,qGAAqG;IACrG,GAAG,EAAE,KAAK,GAAG,KAAK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