@periodic/tungsten 1.0.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/CHANGELOG.md +44 -0
- package/LICENSE +21 -0
- package/README.md +814 -0
- package/dist/index.d.mts +357 -0
- package/dist/index.d.ts +357 -0
- package/dist/index.js +557 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +529 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +79 -0
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,357 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Core type definitions for Tungsten authentication primitives
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* Key provider abstraction for multi-tenant JWT signing/verification
|
|
6
|
+
*/
|
|
7
|
+
interface KeyProvider {
|
|
8
|
+
/**
|
|
9
|
+
* Get signing key for JWT creation
|
|
10
|
+
* @param kid - Optional key ID for rotation
|
|
11
|
+
*/
|
|
12
|
+
getSigningKey(kid?: string): Promise<SigningKey>;
|
|
13
|
+
/**
|
|
14
|
+
* Get verification key for JWT validation
|
|
15
|
+
* @param kid - Key ID from JWT header
|
|
16
|
+
*/
|
|
17
|
+
getVerificationKey(kid: string): Promise<VerificationKey>;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Signing key for JWT creation
|
|
21
|
+
*/
|
|
22
|
+
interface SigningKey {
|
|
23
|
+
kid: string;
|
|
24
|
+
algorithm: 'HS256' | 'RS256';
|
|
25
|
+
key: string | Uint8Array;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Verification key for JWT validation
|
|
29
|
+
*/
|
|
30
|
+
interface VerificationKey {
|
|
31
|
+
algorithm: 'HS256' | 'RS256';
|
|
32
|
+
key: string | Uint8Array;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* JWT payload structure
|
|
36
|
+
*/
|
|
37
|
+
interface JWTPayload {
|
|
38
|
+
sub: string;
|
|
39
|
+
iat?: number;
|
|
40
|
+
exp?: number;
|
|
41
|
+
iss?: string;
|
|
42
|
+
aud?: string | string[];
|
|
43
|
+
jti?: string;
|
|
44
|
+
[key: string]: unknown;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Options for signing access tokens
|
|
48
|
+
*/
|
|
49
|
+
interface SignAccessTokenOptions {
|
|
50
|
+
expiresIn: string | number;
|
|
51
|
+
issuer?: string;
|
|
52
|
+
audience?: string | string[];
|
|
53
|
+
keyProvider: KeyProvider;
|
|
54
|
+
kid?: string;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Options for verifying access tokens
|
|
58
|
+
*/
|
|
59
|
+
interface VerifyAccessTokenOptions {
|
|
60
|
+
keyProvider: KeyProvider;
|
|
61
|
+
issuer?: string;
|
|
62
|
+
audience?: string | string[];
|
|
63
|
+
clockTolerance?: number;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Options for refresh token rotation
|
|
67
|
+
*/
|
|
68
|
+
interface RotateRefreshTokenOptions {
|
|
69
|
+
keyProvider: KeyProvider;
|
|
70
|
+
onTokenReused?: (jti: string) => Promise<void>;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Result of refresh token rotation
|
|
74
|
+
*/
|
|
75
|
+
interface RefreshTokenRotationResult {
|
|
76
|
+
accessToken: string;
|
|
77
|
+
refreshToken: string;
|
|
78
|
+
payload: JWTPayload;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Options for API key generation
|
|
82
|
+
*/
|
|
83
|
+
interface GenerateApiKeyOptions {
|
|
84
|
+
prefix?: string;
|
|
85
|
+
length?: number;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Security event hook for monitoring
|
|
89
|
+
*/
|
|
90
|
+
interface SecurityEventHook {
|
|
91
|
+
onTokenReused?(jti: string, metadata: Record<string, unknown>): Promise<void>;
|
|
92
|
+
onTokenExpired?(jti: string, metadata: Record<string, unknown>): Promise<void>;
|
|
93
|
+
onInvalidSignature?(metadata: Record<string, unknown>): Promise<void>;
|
|
94
|
+
onPasswordHashingFailure?(error: Error): Promise<void>;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Instrumentation adapter for metrics
|
|
98
|
+
*/
|
|
99
|
+
interface InstrumentationAdapter {
|
|
100
|
+
recordTokenGeneration(duration: number, metadata: Record<string, unknown>): void;
|
|
101
|
+
recordTokenVerification(duration: number, success: boolean, metadata: Record<string, unknown>): void;
|
|
102
|
+
recordPasswordHashing(duration: number, metadata: Record<string, unknown>): void;
|
|
103
|
+
recordPasswordVerification(duration: number, success: boolean): void;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Cookie configuration options
|
|
107
|
+
*/
|
|
108
|
+
interface CookieOptions {
|
|
109
|
+
name: string;
|
|
110
|
+
domain?: string;
|
|
111
|
+
path?: string;
|
|
112
|
+
secure?: boolean;
|
|
113
|
+
httpOnly?: boolean;
|
|
114
|
+
sameSite?: 'strict' | 'lax' | 'none';
|
|
115
|
+
maxAge?: number;
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* TOTP configuration options
|
|
119
|
+
*/
|
|
120
|
+
interface TOTPOptions {
|
|
121
|
+
period?: number;
|
|
122
|
+
digits?: number;
|
|
123
|
+
algorithm?: 'SHA1' | 'SHA256' | 'SHA512';
|
|
124
|
+
window?: number;
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* TOTP verification result
|
|
128
|
+
*/
|
|
129
|
+
interface TOTPVerificationResult {
|
|
130
|
+
valid: boolean;
|
|
131
|
+
delta?: number;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* JWT access and refresh token utilities with HS256/RS256 support
|
|
136
|
+
*/
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Sign an access token with JWT
|
|
140
|
+
*/
|
|
141
|
+
declare function signAccessToken(payload: JWTPayload, options: SignAccessTokenOptions): Promise<string>;
|
|
142
|
+
/**
|
|
143
|
+
* Verify an access token
|
|
144
|
+
*/
|
|
145
|
+
declare function verifyAccessToken(token: string, options: VerifyAccessTokenOptions): Promise<JWTPayload>;
|
|
146
|
+
/**
|
|
147
|
+
* Rotate refresh token with replay protection
|
|
148
|
+
*/
|
|
149
|
+
declare function rotateRefreshToken(oldRefreshToken: string, options: RotateRefreshTokenOptions): Promise<RefreshTokenRotationResult>;
|
|
150
|
+
/**
|
|
151
|
+
* Timing-safe string comparison
|
|
152
|
+
*/
|
|
153
|
+
declare function timingSafeCompare(a: string, b: string): boolean;
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Argon2id password hashing with secure defaults
|
|
157
|
+
*/
|
|
158
|
+
declare function hashPassword(password: string): Promise<string>;
|
|
159
|
+
declare function verifyPassword(password: string, hash: string): Promise<boolean>;
|
|
160
|
+
declare function needsRehash(hash: string): Promise<boolean>;
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* API key generation and verification utilities
|
|
164
|
+
*/
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Generate a cryptographically secure API key
|
|
168
|
+
* @param options - Generation options
|
|
169
|
+
* @returns API key with optional prefix
|
|
170
|
+
*/
|
|
171
|
+
declare function generateApiKey(options?: GenerateApiKeyOptions): string;
|
|
172
|
+
/**
|
|
173
|
+
* Hash an API key for storage
|
|
174
|
+
* @param apiKey - Plain API key
|
|
175
|
+
* @returns Hashed key (hex encoded)
|
|
176
|
+
*/
|
|
177
|
+
declare function hashApiKey(apiKey: string): string;
|
|
178
|
+
/**
|
|
179
|
+
* Verify an API key against a stored hash
|
|
180
|
+
* @param apiKey - Plain API key
|
|
181
|
+
* @param hash - Stored hash
|
|
182
|
+
* @returns True if key matches hash
|
|
183
|
+
*/
|
|
184
|
+
declare function verifyApiKey(apiKey: string, hash: string): boolean;
|
|
185
|
+
/**
|
|
186
|
+
* Extract prefix from an API key
|
|
187
|
+
* @param apiKey - API key with potential prefix
|
|
188
|
+
* @returns Prefix or empty string
|
|
189
|
+
*/
|
|
190
|
+
declare function extractPrefix(apiKey: string): string;
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Opaque token generation for session identifiers
|
|
194
|
+
*/
|
|
195
|
+
/**
|
|
196
|
+
* Generate a cryptographically secure opaque token
|
|
197
|
+
* @returns Base64url encoded token
|
|
198
|
+
*/
|
|
199
|
+
declare function generateOpaqueToken(): string;
|
|
200
|
+
/**
|
|
201
|
+
* Hash an opaque token for storage
|
|
202
|
+
* @param token - Plain opaque token
|
|
203
|
+
* @returns Hashed token (hex encoded)
|
|
204
|
+
*/
|
|
205
|
+
declare function hashOpaqueToken(token: string): string;
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* TOTP (Time-based One-Time Password) implementation (RFC 6238)
|
|
209
|
+
*/
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* Generate a TOTP secret
|
|
213
|
+
* @returns Base32 encoded secret
|
|
214
|
+
*/
|
|
215
|
+
declare function generateTOTPSecret(): string;
|
|
216
|
+
/**
|
|
217
|
+
* Generate a TOTP code
|
|
218
|
+
* @param secret - Base32 encoded secret
|
|
219
|
+
* @param options - TOTP options
|
|
220
|
+
* @returns 6-digit TOTP code
|
|
221
|
+
*/
|
|
222
|
+
declare function generateTOTP(secret: string, options?: TOTPOptions): string;
|
|
223
|
+
/**
|
|
224
|
+
* Verify a TOTP code
|
|
225
|
+
* @param code - User-provided code
|
|
226
|
+
* @param secret - Base32 encoded secret
|
|
227
|
+
* @param options - TOTP options
|
|
228
|
+
* @returns Verification result with delta
|
|
229
|
+
*/
|
|
230
|
+
declare function verifyTOTP(code: string, secret: string, options?: TOTPOptions): TOTPVerificationResult;
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* HMAC request signing and verification
|
|
234
|
+
*/
|
|
235
|
+
/**
|
|
236
|
+
* Sign a payload with HMAC
|
|
237
|
+
* @param payload - Data to sign (string or object)
|
|
238
|
+
* @param secret - Signing secret
|
|
239
|
+
* @param algorithm - HMAC algorithm (default: sha256)
|
|
240
|
+
* @returns Hex-encoded signature
|
|
241
|
+
*/
|
|
242
|
+
declare function signPayload(payload: string | Record<string, unknown>, secret: string, algorithm?: string): string;
|
|
243
|
+
/**
|
|
244
|
+
* Verify a payload signature
|
|
245
|
+
* @param payload - Data to verify
|
|
246
|
+
* @param signature - Expected signature
|
|
247
|
+
* @param secret - Signing secret
|
|
248
|
+
* @param algorithm - HMAC algorithm (default: sha256)
|
|
249
|
+
* @returns True if signature is valid
|
|
250
|
+
*/
|
|
251
|
+
declare function verifySignature(payload: string | Record<string, unknown>, signature: string, secret: string, algorithm?: string): boolean;
|
|
252
|
+
/**
|
|
253
|
+
* Sign a request with timestamp to prevent replay attacks
|
|
254
|
+
* @param payload - Request data
|
|
255
|
+
* @param secret - Signing secret
|
|
256
|
+
* @param timestamp - Unix timestamp (default: current time)
|
|
257
|
+
* @returns Object with signature and timestamp
|
|
258
|
+
*/
|
|
259
|
+
declare function signRequest(payload: string | Record<string, unknown>, secret: string, timestamp?: number): {
|
|
260
|
+
signature: string;
|
|
261
|
+
timestamp: number;
|
|
262
|
+
};
|
|
263
|
+
/**
|
|
264
|
+
* Verify a signed request with timestamp validation
|
|
265
|
+
* @param payload - Request data
|
|
266
|
+
* @param signature - Request signature
|
|
267
|
+
* @param timestamp - Request timestamp
|
|
268
|
+
* @param secret - Signing secret
|
|
269
|
+
* @param maxAge - Maximum age in seconds (default: 300)
|
|
270
|
+
* @returns True if signature and timestamp are valid
|
|
271
|
+
*/
|
|
272
|
+
declare function verifyRequest(payload: string | Record<string, unknown>, signature: string, timestamp: number, secret: string, maxAge?: number): boolean;
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* Cookie configuration helpers
|
|
276
|
+
*/
|
|
277
|
+
|
|
278
|
+
/**
|
|
279
|
+
* Get secure cookie defaults for production
|
|
280
|
+
*/
|
|
281
|
+
declare function getSecureCookieDefaults(isProduction?: boolean): Partial<CookieOptions>;
|
|
282
|
+
/**
|
|
283
|
+
* Generate Set-Cookie header value
|
|
284
|
+
*/
|
|
285
|
+
declare function serializeCookie(name: string, value: string, options: CookieOptions): string;
|
|
286
|
+
/**
|
|
287
|
+
* Parse Cookie header value
|
|
288
|
+
*/
|
|
289
|
+
declare function parseCookie(cookieHeader: string): Record<string, string>;
|
|
290
|
+
/**
|
|
291
|
+
* Create a cookie deletion header
|
|
292
|
+
*/
|
|
293
|
+
declare function deleteCookie(name: string, options?: Partial<CookieOptions>): string;
|
|
294
|
+
|
|
295
|
+
/**
|
|
296
|
+
* Key provider implementations for multi-tenant JWT signing
|
|
297
|
+
*/
|
|
298
|
+
|
|
299
|
+
/**
|
|
300
|
+
* Simple in-memory key provider for single-tenant applications
|
|
301
|
+
*/
|
|
302
|
+
declare class SimpleKeyProvider implements KeyProvider {
|
|
303
|
+
private signingKey;
|
|
304
|
+
private verificationKey;
|
|
305
|
+
constructor(secret: string, algorithm?: 'HS256' | 'RS256', kid?: string);
|
|
306
|
+
getSigningKey(_kid?: string): Promise<SigningKey>;
|
|
307
|
+
getVerificationKey(_kid: string): Promise<VerificationKey>;
|
|
308
|
+
}
|
|
309
|
+
/**
|
|
310
|
+
* Rotating key provider with multiple keys
|
|
311
|
+
*/
|
|
312
|
+
declare class RotatingKeyProvider implements KeyProvider {
|
|
313
|
+
private keys;
|
|
314
|
+
private currentKid;
|
|
315
|
+
constructor(initialKey: {
|
|
316
|
+
kid: string;
|
|
317
|
+
secret: string;
|
|
318
|
+
algorithm: 'HS256' | 'RS256';
|
|
319
|
+
});
|
|
320
|
+
/**
|
|
321
|
+
* Add a new key to the rotation
|
|
322
|
+
*/
|
|
323
|
+
addKey(kid: string, secret: string, algorithm: 'HS256' | 'RS256'): void;
|
|
324
|
+
/**
|
|
325
|
+
* Set the current signing key
|
|
326
|
+
*/
|
|
327
|
+
setCurrentKey(kid: string): void;
|
|
328
|
+
/**
|
|
329
|
+
* Remove an old key from rotation
|
|
330
|
+
*/
|
|
331
|
+
removeKey(kid: string): void;
|
|
332
|
+
getSigningKey(kid?: string): Promise<SigningKey>;
|
|
333
|
+
getVerificationKey(kid: string): Promise<VerificationKey>;
|
|
334
|
+
/**
|
|
335
|
+
* Get all available key IDs
|
|
336
|
+
*/
|
|
337
|
+
getKeyIds(): string[];
|
|
338
|
+
}
|
|
339
|
+
/**
|
|
340
|
+
* Multi-tenant key provider
|
|
341
|
+
*/
|
|
342
|
+
declare class MultiTenantKeyProvider implements KeyProvider {
|
|
343
|
+
private tenants;
|
|
344
|
+
constructor();
|
|
345
|
+
/**
|
|
346
|
+
* Register a tenant with its key provider
|
|
347
|
+
*/
|
|
348
|
+
registerTenant(tenantId: string, keyProvider: KeyProvider): void;
|
|
349
|
+
/**
|
|
350
|
+
* Unregister a tenant
|
|
351
|
+
*/
|
|
352
|
+
unregisterTenant(tenantId: string): void;
|
|
353
|
+
getSigningKey(kid?: string): Promise<SigningKey>;
|
|
354
|
+
getVerificationKey(kid: string): Promise<VerificationKey>;
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
export { type CookieOptions, type GenerateApiKeyOptions, type InstrumentationAdapter, type JWTPayload, type KeyProvider, MultiTenantKeyProvider, type RefreshTokenRotationResult, type RotateRefreshTokenOptions, RotatingKeyProvider, type SecurityEventHook, type SignAccessTokenOptions, type SigningKey, SimpleKeyProvider, type TOTPOptions, type TOTPVerificationResult, type VerificationKey, type VerifyAccessTokenOptions, deleteCookie, extractPrefix, generateApiKey, generateOpaqueToken, generateTOTP, generateTOTPSecret, getSecureCookieDefaults, hashApiKey, hashOpaqueToken, hashPassword, needsRehash, parseCookie, rotateRefreshToken, serializeCookie, signAccessToken, signPayload, signRequest, timingSafeCompare, verifyAccessToken, verifyApiKey, verifyPassword, verifyRequest, verifySignature, verifyTOTP };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,357 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Core type definitions for Tungsten authentication primitives
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* Key provider abstraction for multi-tenant JWT signing/verification
|
|
6
|
+
*/
|
|
7
|
+
interface KeyProvider {
|
|
8
|
+
/**
|
|
9
|
+
* Get signing key for JWT creation
|
|
10
|
+
* @param kid - Optional key ID for rotation
|
|
11
|
+
*/
|
|
12
|
+
getSigningKey(kid?: string): Promise<SigningKey>;
|
|
13
|
+
/**
|
|
14
|
+
* Get verification key for JWT validation
|
|
15
|
+
* @param kid - Key ID from JWT header
|
|
16
|
+
*/
|
|
17
|
+
getVerificationKey(kid: string): Promise<VerificationKey>;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Signing key for JWT creation
|
|
21
|
+
*/
|
|
22
|
+
interface SigningKey {
|
|
23
|
+
kid: string;
|
|
24
|
+
algorithm: 'HS256' | 'RS256';
|
|
25
|
+
key: string | Uint8Array;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Verification key for JWT validation
|
|
29
|
+
*/
|
|
30
|
+
interface VerificationKey {
|
|
31
|
+
algorithm: 'HS256' | 'RS256';
|
|
32
|
+
key: string | Uint8Array;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* JWT payload structure
|
|
36
|
+
*/
|
|
37
|
+
interface JWTPayload {
|
|
38
|
+
sub: string;
|
|
39
|
+
iat?: number;
|
|
40
|
+
exp?: number;
|
|
41
|
+
iss?: string;
|
|
42
|
+
aud?: string | string[];
|
|
43
|
+
jti?: string;
|
|
44
|
+
[key: string]: unknown;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Options for signing access tokens
|
|
48
|
+
*/
|
|
49
|
+
interface SignAccessTokenOptions {
|
|
50
|
+
expiresIn: string | number;
|
|
51
|
+
issuer?: string;
|
|
52
|
+
audience?: string | string[];
|
|
53
|
+
keyProvider: KeyProvider;
|
|
54
|
+
kid?: string;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Options for verifying access tokens
|
|
58
|
+
*/
|
|
59
|
+
interface VerifyAccessTokenOptions {
|
|
60
|
+
keyProvider: KeyProvider;
|
|
61
|
+
issuer?: string;
|
|
62
|
+
audience?: string | string[];
|
|
63
|
+
clockTolerance?: number;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Options for refresh token rotation
|
|
67
|
+
*/
|
|
68
|
+
interface RotateRefreshTokenOptions {
|
|
69
|
+
keyProvider: KeyProvider;
|
|
70
|
+
onTokenReused?: (jti: string) => Promise<void>;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Result of refresh token rotation
|
|
74
|
+
*/
|
|
75
|
+
interface RefreshTokenRotationResult {
|
|
76
|
+
accessToken: string;
|
|
77
|
+
refreshToken: string;
|
|
78
|
+
payload: JWTPayload;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Options for API key generation
|
|
82
|
+
*/
|
|
83
|
+
interface GenerateApiKeyOptions {
|
|
84
|
+
prefix?: string;
|
|
85
|
+
length?: number;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Security event hook for monitoring
|
|
89
|
+
*/
|
|
90
|
+
interface SecurityEventHook {
|
|
91
|
+
onTokenReused?(jti: string, metadata: Record<string, unknown>): Promise<void>;
|
|
92
|
+
onTokenExpired?(jti: string, metadata: Record<string, unknown>): Promise<void>;
|
|
93
|
+
onInvalidSignature?(metadata: Record<string, unknown>): Promise<void>;
|
|
94
|
+
onPasswordHashingFailure?(error: Error): Promise<void>;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Instrumentation adapter for metrics
|
|
98
|
+
*/
|
|
99
|
+
interface InstrumentationAdapter {
|
|
100
|
+
recordTokenGeneration(duration: number, metadata: Record<string, unknown>): void;
|
|
101
|
+
recordTokenVerification(duration: number, success: boolean, metadata: Record<string, unknown>): void;
|
|
102
|
+
recordPasswordHashing(duration: number, metadata: Record<string, unknown>): void;
|
|
103
|
+
recordPasswordVerification(duration: number, success: boolean): void;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Cookie configuration options
|
|
107
|
+
*/
|
|
108
|
+
interface CookieOptions {
|
|
109
|
+
name: string;
|
|
110
|
+
domain?: string;
|
|
111
|
+
path?: string;
|
|
112
|
+
secure?: boolean;
|
|
113
|
+
httpOnly?: boolean;
|
|
114
|
+
sameSite?: 'strict' | 'lax' | 'none';
|
|
115
|
+
maxAge?: number;
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* TOTP configuration options
|
|
119
|
+
*/
|
|
120
|
+
interface TOTPOptions {
|
|
121
|
+
period?: number;
|
|
122
|
+
digits?: number;
|
|
123
|
+
algorithm?: 'SHA1' | 'SHA256' | 'SHA512';
|
|
124
|
+
window?: number;
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* TOTP verification result
|
|
128
|
+
*/
|
|
129
|
+
interface TOTPVerificationResult {
|
|
130
|
+
valid: boolean;
|
|
131
|
+
delta?: number;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* JWT access and refresh token utilities with HS256/RS256 support
|
|
136
|
+
*/
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Sign an access token with JWT
|
|
140
|
+
*/
|
|
141
|
+
declare function signAccessToken(payload: JWTPayload, options: SignAccessTokenOptions): Promise<string>;
|
|
142
|
+
/**
|
|
143
|
+
* Verify an access token
|
|
144
|
+
*/
|
|
145
|
+
declare function verifyAccessToken(token: string, options: VerifyAccessTokenOptions): Promise<JWTPayload>;
|
|
146
|
+
/**
|
|
147
|
+
* Rotate refresh token with replay protection
|
|
148
|
+
*/
|
|
149
|
+
declare function rotateRefreshToken(oldRefreshToken: string, options: RotateRefreshTokenOptions): Promise<RefreshTokenRotationResult>;
|
|
150
|
+
/**
|
|
151
|
+
* Timing-safe string comparison
|
|
152
|
+
*/
|
|
153
|
+
declare function timingSafeCompare(a: string, b: string): boolean;
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Argon2id password hashing with secure defaults
|
|
157
|
+
*/
|
|
158
|
+
declare function hashPassword(password: string): Promise<string>;
|
|
159
|
+
declare function verifyPassword(password: string, hash: string): Promise<boolean>;
|
|
160
|
+
declare function needsRehash(hash: string): Promise<boolean>;
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* API key generation and verification utilities
|
|
164
|
+
*/
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Generate a cryptographically secure API key
|
|
168
|
+
* @param options - Generation options
|
|
169
|
+
* @returns API key with optional prefix
|
|
170
|
+
*/
|
|
171
|
+
declare function generateApiKey(options?: GenerateApiKeyOptions): string;
|
|
172
|
+
/**
|
|
173
|
+
* Hash an API key for storage
|
|
174
|
+
* @param apiKey - Plain API key
|
|
175
|
+
* @returns Hashed key (hex encoded)
|
|
176
|
+
*/
|
|
177
|
+
declare function hashApiKey(apiKey: string): string;
|
|
178
|
+
/**
|
|
179
|
+
* Verify an API key against a stored hash
|
|
180
|
+
* @param apiKey - Plain API key
|
|
181
|
+
* @param hash - Stored hash
|
|
182
|
+
* @returns True if key matches hash
|
|
183
|
+
*/
|
|
184
|
+
declare function verifyApiKey(apiKey: string, hash: string): boolean;
|
|
185
|
+
/**
|
|
186
|
+
* Extract prefix from an API key
|
|
187
|
+
* @param apiKey - API key with potential prefix
|
|
188
|
+
* @returns Prefix or empty string
|
|
189
|
+
*/
|
|
190
|
+
declare function extractPrefix(apiKey: string): string;
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Opaque token generation for session identifiers
|
|
194
|
+
*/
|
|
195
|
+
/**
|
|
196
|
+
* Generate a cryptographically secure opaque token
|
|
197
|
+
* @returns Base64url encoded token
|
|
198
|
+
*/
|
|
199
|
+
declare function generateOpaqueToken(): string;
|
|
200
|
+
/**
|
|
201
|
+
* Hash an opaque token for storage
|
|
202
|
+
* @param token - Plain opaque token
|
|
203
|
+
* @returns Hashed token (hex encoded)
|
|
204
|
+
*/
|
|
205
|
+
declare function hashOpaqueToken(token: string): string;
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* TOTP (Time-based One-Time Password) implementation (RFC 6238)
|
|
209
|
+
*/
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* Generate a TOTP secret
|
|
213
|
+
* @returns Base32 encoded secret
|
|
214
|
+
*/
|
|
215
|
+
declare function generateTOTPSecret(): string;
|
|
216
|
+
/**
|
|
217
|
+
* Generate a TOTP code
|
|
218
|
+
* @param secret - Base32 encoded secret
|
|
219
|
+
* @param options - TOTP options
|
|
220
|
+
* @returns 6-digit TOTP code
|
|
221
|
+
*/
|
|
222
|
+
declare function generateTOTP(secret: string, options?: TOTPOptions): string;
|
|
223
|
+
/**
|
|
224
|
+
* Verify a TOTP code
|
|
225
|
+
* @param code - User-provided code
|
|
226
|
+
* @param secret - Base32 encoded secret
|
|
227
|
+
* @param options - TOTP options
|
|
228
|
+
* @returns Verification result with delta
|
|
229
|
+
*/
|
|
230
|
+
declare function verifyTOTP(code: string, secret: string, options?: TOTPOptions): TOTPVerificationResult;
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* HMAC request signing and verification
|
|
234
|
+
*/
|
|
235
|
+
/**
|
|
236
|
+
* Sign a payload with HMAC
|
|
237
|
+
* @param payload - Data to sign (string or object)
|
|
238
|
+
* @param secret - Signing secret
|
|
239
|
+
* @param algorithm - HMAC algorithm (default: sha256)
|
|
240
|
+
* @returns Hex-encoded signature
|
|
241
|
+
*/
|
|
242
|
+
declare function signPayload(payload: string | Record<string, unknown>, secret: string, algorithm?: string): string;
|
|
243
|
+
/**
|
|
244
|
+
* Verify a payload signature
|
|
245
|
+
* @param payload - Data to verify
|
|
246
|
+
* @param signature - Expected signature
|
|
247
|
+
* @param secret - Signing secret
|
|
248
|
+
* @param algorithm - HMAC algorithm (default: sha256)
|
|
249
|
+
* @returns True if signature is valid
|
|
250
|
+
*/
|
|
251
|
+
declare function verifySignature(payload: string | Record<string, unknown>, signature: string, secret: string, algorithm?: string): boolean;
|
|
252
|
+
/**
|
|
253
|
+
* Sign a request with timestamp to prevent replay attacks
|
|
254
|
+
* @param payload - Request data
|
|
255
|
+
* @param secret - Signing secret
|
|
256
|
+
* @param timestamp - Unix timestamp (default: current time)
|
|
257
|
+
* @returns Object with signature and timestamp
|
|
258
|
+
*/
|
|
259
|
+
declare function signRequest(payload: string | Record<string, unknown>, secret: string, timestamp?: number): {
|
|
260
|
+
signature: string;
|
|
261
|
+
timestamp: number;
|
|
262
|
+
};
|
|
263
|
+
/**
|
|
264
|
+
* Verify a signed request with timestamp validation
|
|
265
|
+
* @param payload - Request data
|
|
266
|
+
* @param signature - Request signature
|
|
267
|
+
* @param timestamp - Request timestamp
|
|
268
|
+
* @param secret - Signing secret
|
|
269
|
+
* @param maxAge - Maximum age in seconds (default: 300)
|
|
270
|
+
* @returns True if signature and timestamp are valid
|
|
271
|
+
*/
|
|
272
|
+
declare function verifyRequest(payload: string | Record<string, unknown>, signature: string, timestamp: number, secret: string, maxAge?: number): boolean;
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* Cookie configuration helpers
|
|
276
|
+
*/
|
|
277
|
+
|
|
278
|
+
/**
|
|
279
|
+
* Get secure cookie defaults for production
|
|
280
|
+
*/
|
|
281
|
+
declare function getSecureCookieDefaults(isProduction?: boolean): Partial<CookieOptions>;
|
|
282
|
+
/**
|
|
283
|
+
* Generate Set-Cookie header value
|
|
284
|
+
*/
|
|
285
|
+
declare function serializeCookie(name: string, value: string, options: CookieOptions): string;
|
|
286
|
+
/**
|
|
287
|
+
* Parse Cookie header value
|
|
288
|
+
*/
|
|
289
|
+
declare function parseCookie(cookieHeader: string): Record<string, string>;
|
|
290
|
+
/**
|
|
291
|
+
* Create a cookie deletion header
|
|
292
|
+
*/
|
|
293
|
+
declare function deleteCookie(name: string, options?: Partial<CookieOptions>): string;
|
|
294
|
+
|
|
295
|
+
/**
|
|
296
|
+
* Key provider implementations for multi-tenant JWT signing
|
|
297
|
+
*/
|
|
298
|
+
|
|
299
|
+
/**
|
|
300
|
+
* Simple in-memory key provider for single-tenant applications
|
|
301
|
+
*/
|
|
302
|
+
declare class SimpleKeyProvider implements KeyProvider {
|
|
303
|
+
private signingKey;
|
|
304
|
+
private verificationKey;
|
|
305
|
+
constructor(secret: string, algorithm?: 'HS256' | 'RS256', kid?: string);
|
|
306
|
+
getSigningKey(_kid?: string): Promise<SigningKey>;
|
|
307
|
+
getVerificationKey(_kid: string): Promise<VerificationKey>;
|
|
308
|
+
}
|
|
309
|
+
/**
|
|
310
|
+
* Rotating key provider with multiple keys
|
|
311
|
+
*/
|
|
312
|
+
declare class RotatingKeyProvider implements KeyProvider {
|
|
313
|
+
private keys;
|
|
314
|
+
private currentKid;
|
|
315
|
+
constructor(initialKey: {
|
|
316
|
+
kid: string;
|
|
317
|
+
secret: string;
|
|
318
|
+
algorithm: 'HS256' | 'RS256';
|
|
319
|
+
});
|
|
320
|
+
/**
|
|
321
|
+
* Add a new key to the rotation
|
|
322
|
+
*/
|
|
323
|
+
addKey(kid: string, secret: string, algorithm: 'HS256' | 'RS256'): void;
|
|
324
|
+
/**
|
|
325
|
+
* Set the current signing key
|
|
326
|
+
*/
|
|
327
|
+
setCurrentKey(kid: string): void;
|
|
328
|
+
/**
|
|
329
|
+
* Remove an old key from rotation
|
|
330
|
+
*/
|
|
331
|
+
removeKey(kid: string): void;
|
|
332
|
+
getSigningKey(kid?: string): Promise<SigningKey>;
|
|
333
|
+
getVerificationKey(kid: string): Promise<VerificationKey>;
|
|
334
|
+
/**
|
|
335
|
+
* Get all available key IDs
|
|
336
|
+
*/
|
|
337
|
+
getKeyIds(): string[];
|
|
338
|
+
}
|
|
339
|
+
/**
|
|
340
|
+
* Multi-tenant key provider
|
|
341
|
+
*/
|
|
342
|
+
declare class MultiTenantKeyProvider implements KeyProvider {
|
|
343
|
+
private tenants;
|
|
344
|
+
constructor();
|
|
345
|
+
/**
|
|
346
|
+
* Register a tenant with its key provider
|
|
347
|
+
*/
|
|
348
|
+
registerTenant(tenantId: string, keyProvider: KeyProvider): void;
|
|
349
|
+
/**
|
|
350
|
+
* Unregister a tenant
|
|
351
|
+
*/
|
|
352
|
+
unregisterTenant(tenantId: string): void;
|
|
353
|
+
getSigningKey(kid?: string): Promise<SigningKey>;
|
|
354
|
+
getVerificationKey(kid: string): Promise<VerificationKey>;
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
export { type CookieOptions, type GenerateApiKeyOptions, type InstrumentationAdapter, type JWTPayload, type KeyProvider, MultiTenantKeyProvider, type RefreshTokenRotationResult, type RotateRefreshTokenOptions, RotatingKeyProvider, type SecurityEventHook, type SignAccessTokenOptions, type SigningKey, SimpleKeyProvider, type TOTPOptions, type TOTPVerificationResult, type VerificationKey, type VerifyAccessTokenOptions, deleteCookie, extractPrefix, generateApiKey, generateOpaqueToken, generateTOTP, generateTOTPSecret, getSecureCookieDefaults, hashApiKey, hashOpaqueToken, hashPassword, needsRehash, parseCookie, rotateRefreshToken, serializeCookie, signAccessToken, signPayload, signRequest, timingSafeCompare, verifyAccessToken, verifyApiKey, verifyPassword, verifyRequest, verifySignature, verifyTOTP };
|