sealtok 0.0.0-stage → 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/README.md +546 -2
- package/dist/index.d.mts +645 -0
- package/dist/index.d.ts +645 -0
- package/dist/index.js +80 -0
- package/dist/index.mjs +80 -0
- package/package.json +84 -4
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,645 @@
|
|
|
1
|
+
interface CompositeKeyConfig {
|
|
2
|
+
fields: string[];
|
|
3
|
+
separator?: string;
|
|
4
|
+
}
|
|
5
|
+
interface IdentifierConfig {
|
|
6
|
+
userIdField?: string | string[];
|
|
7
|
+
userIdPath?: string;
|
|
8
|
+
sessionIdPath?: string;
|
|
9
|
+
extractIdentifiers?: (tokenData: unknown) => ExtractedIdentifiers;
|
|
10
|
+
compositeKey?: CompositeKeyConfig;
|
|
11
|
+
fallbackChain?: string[];
|
|
12
|
+
}
|
|
13
|
+
interface ExtractedIdentifiers {
|
|
14
|
+
userId?: string;
|
|
15
|
+
sessionId?: string;
|
|
16
|
+
jti?: string;
|
|
17
|
+
compositeKey?: string;
|
|
18
|
+
metadata?: Record<string, unknown>;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Token revocation types and interfaces
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Revocation identifier with all possible fields
|
|
27
|
+
*/
|
|
28
|
+
interface RevocationIdentifier {
|
|
29
|
+
/** Token ID (jti claim) */
|
|
30
|
+
jti: string;
|
|
31
|
+
/** User ID (from payload.data or sub claim) */
|
|
32
|
+
userId?: string;
|
|
33
|
+
/** Session ID (from payload.data.sessionId) */
|
|
34
|
+
sessionId?: string;
|
|
35
|
+
/** Token expiration timestamp (for TTL) */
|
|
36
|
+
exp: number;
|
|
37
|
+
/** Revocation timestamp */
|
|
38
|
+
revokedAt: number;
|
|
39
|
+
/** Optional reason for revocation */
|
|
40
|
+
reason?: string;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Configuration for revocation stores
|
|
44
|
+
*/
|
|
45
|
+
interface RevocationStoreConfig {
|
|
46
|
+
/** Auto cleanup interval in milliseconds */
|
|
47
|
+
autoCleanupInterval?: number;
|
|
48
|
+
/** Prefix for keys (Redis) */
|
|
49
|
+
keyPrefix?: string;
|
|
50
|
+
/** Enable debug logging */
|
|
51
|
+
debug?: boolean;
|
|
52
|
+
/** Identifier configuration for extracting tokens */
|
|
53
|
+
identifierConfig?: IdentifierConfig;
|
|
54
|
+
/** Seconds a revocation outlives the token's exp; keep ≥ the largest verify clockTolerance (default 300) */
|
|
55
|
+
expiryGraceSeconds?: number;
|
|
56
|
+
/** Seconds a session revocation is kept when the session's token lifetime is unknown (Redis; default 30 days) */
|
|
57
|
+
sessionRevocationTtl?: number;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Redis store specific configuration
|
|
61
|
+
*/
|
|
62
|
+
interface RedisStoreConfig extends RevocationStoreConfig {
|
|
63
|
+
/** Redis host */
|
|
64
|
+
host?: string;
|
|
65
|
+
/** Redis port */
|
|
66
|
+
port?: number;
|
|
67
|
+
/** Redis password */
|
|
68
|
+
password?: string;
|
|
69
|
+
/** Redis database number */
|
|
70
|
+
db?: number;
|
|
71
|
+
/** Redis connection URL (overrides host/port) */
|
|
72
|
+
url?: string;
|
|
73
|
+
/** Existing Redis client instance */
|
|
74
|
+
client?: any;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* SQLite store specific configuration
|
|
78
|
+
*/
|
|
79
|
+
interface SQLiteStoreConfig extends RevocationStoreConfig {
|
|
80
|
+
/** Database file path */
|
|
81
|
+
database?: string;
|
|
82
|
+
/** Use in-memory database */
|
|
83
|
+
memory?: boolean;
|
|
84
|
+
/** Existing database connection */
|
|
85
|
+
connection?: any;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Abstract base class for revocation stores
|
|
89
|
+
*/
|
|
90
|
+
declare abstract class RevocationStore {
|
|
91
|
+
protected config: RevocationStoreConfig;
|
|
92
|
+
protected cleanupTimer?: NodeJS.Timeout;
|
|
93
|
+
/** Identifier configuration for extracting tokens */
|
|
94
|
+
protected identifierConfig?: IdentifierConfig;
|
|
95
|
+
constructor(config?: RevocationStoreConfig);
|
|
96
|
+
/** Revocations with exp below this unix time are expired (exp + grace, so they cover verify's clockTolerance) */
|
|
97
|
+
protected cutoff(): number;
|
|
98
|
+
/**
|
|
99
|
+
* Revoke a token by its identifier
|
|
100
|
+
*/
|
|
101
|
+
abstract revoke(identifier: RevocationIdentifier): Promise<void>;
|
|
102
|
+
/**
|
|
103
|
+
* Check if a token is revoked by jti
|
|
104
|
+
*/
|
|
105
|
+
abstract isRevoked(jti: string): Promise<boolean>;
|
|
106
|
+
/**
|
|
107
|
+
* Get all revoked tokens for a user
|
|
108
|
+
*/
|
|
109
|
+
abstract getRevokedTokensForUser(userId: string): Promise<string[]>;
|
|
110
|
+
/**
|
|
111
|
+
* Revoke all tokens for a user
|
|
112
|
+
*/
|
|
113
|
+
abstract revokeAllForUser(userId: string, reason?: string): Promise<void>;
|
|
114
|
+
/**
|
|
115
|
+
* Revoke a session
|
|
116
|
+
*/
|
|
117
|
+
abstract revokeSession(sessionId: string, reason?: string): Promise<void>;
|
|
118
|
+
/**
|
|
119
|
+
* Check if a session is revoked
|
|
120
|
+
*/
|
|
121
|
+
abstract isSessionRevoked(sessionId: string): Promise<boolean>;
|
|
122
|
+
/**
|
|
123
|
+
* Check if a user's all tokens should be rejected
|
|
124
|
+
*/
|
|
125
|
+
abstract isUserRevoked(userId: string): Promise<boolean>;
|
|
126
|
+
/**
|
|
127
|
+
* Revoke by composite key
|
|
128
|
+
*/
|
|
129
|
+
abstract revokeByCompositeKey(key: string, components: Record<string, string>, exp: number, reason?: string): Promise<void>;
|
|
130
|
+
/**
|
|
131
|
+
* Check if a composite key is revoked
|
|
132
|
+
*/
|
|
133
|
+
abstract isCompositeKeyRevoked(key: string): Promise<boolean>;
|
|
134
|
+
/**
|
|
135
|
+
* Manual cleanup of expired entries
|
|
136
|
+
*/
|
|
137
|
+
abstract cleanup(): Promise<number>;
|
|
138
|
+
/**
|
|
139
|
+
* Start automatic cleanup at intervals
|
|
140
|
+
*/
|
|
141
|
+
startAutoCleanup(intervalMs?: number): void;
|
|
142
|
+
/**
|
|
143
|
+
* Stop automatic cleanup
|
|
144
|
+
*/
|
|
145
|
+
stopAutoCleanup(): void;
|
|
146
|
+
/**
|
|
147
|
+
* Disconnect from store (cleanup resources)
|
|
148
|
+
*/
|
|
149
|
+
abstract disconnect(): Promise<void>;
|
|
150
|
+
/**
|
|
151
|
+
* Extract revocation identifier from decoded token
|
|
152
|
+
*/
|
|
153
|
+
static extractIdentifier(token: DecodedToken, reason?: string): RevocationIdentifier;
|
|
154
|
+
/**
|
|
155
|
+
* Check if identifier has valid jti
|
|
156
|
+
*/
|
|
157
|
+
protected hasValidJti(identifier: RevocationIdentifier): boolean;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Core types and interfaces for sigil
|
|
162
|
+
* Provides type-safe token operations with full TypeScript support
|
|
163
|
+
*/
|
|
164
|
+
type Algorithm = 'v1.local' | 'v1.public' | 'v2.local' | 'v3.public';
|
|
165
|
+
type TokenFormat = 'binary' | 'protobuf';
|
|
166
|
+
type TimeValue = number | string;
|
|
167
|
+
interface TokenHeader {
|
|
168
|
+
version: 'v1' | 'v2' | 'v3';
|
|
169
|
+
purpose: 'local' | 'public';
|
|
170
|
+
format: TokenFormat;
|
|
171
|
+
}
|
|
172
|
+
interface TokenPayload<T = Record<string, unknown>> {
|
|
173
|
+
iss?: string;
|
|
174
|
+
sub?: string;
|
|
175
|
+
aud?: string | string[];
|
|
176
|
+
exp?: number;
|
|
177
|
+
nbf?: number;
|
|
178
|
+
iat?: number;
|
|
179
|
+
jti?: string;
|
|
180
|
+
data?: T;
|
|
181
|
+
}
|
|
182
|
+
interface VerifyOptions {
|
|
183
|
+
secret?: string | Buffer;
|
|
184
|
+
publicKey?: string | Buffer;
|
|
185
|
+
algorithms?: Algorithm[];
|
|
186
|
+
issuer?: string | string[];
|
|
187
|
+
audience?: string | string[];
|
|
188
|
+
clockTolerance?: number;
|
|
189
|
+
maxAge?: TimeValue;
|
|
190
|
+
/** Reject tokens without `exp` (default: true) */
|
|
191
|
+
requireExpiration?: boolean;
|
|
192
|
+
/** Reject tokens longer than this many characters before decoding (default: 262144) */
|
|
193
|
+
maxTokenLength?: number;
|
|
194
|
+
revocationStore?: RevocationStore;
|
|
195
|
+
skipRevocationCheck?: boolean;
|
|
196
|
+
onRevoked?: (token: DecodedToken) => void;
|
|
197
|
+
identifierConfig?: IdentifierConfig;
|
|
198
|
+
}
|
|
199
|
+
interface DecodedToken<TPayload = Record<string, unknown>> {
|
|
200
|
+
header: TokenHeader;
|
|
201
|
+
payload: TokenPayload<TPayload>;
|
|
202
|
+
signature: Buffer;
|
|
203
|
+
raw: {
|
|
204
|
+
header: Buffer;
|
|
205
|
+
payload: Buffer;
|
|
206
|
+
signature: Buffer;
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
declare class SigilError extends Error {
|
|
210
|
+
code: string;
|
|
211
|
+
constructor(message: string, code: string);
|
|
212
|
+
}
|
|
213
|
+
declare class TokenExpiredError extends SigilError {
|
|
214
|
+
expiredAt: Date;
|
|
215
|
+
constructor(expiredAt: Date);
|
|
216
|
+
}
|
|
217
|
+
declare class TokenNotBeforeError extends SigilError {
|
|
218
|
+
notBefore: Date;
|
|
219
|
+
constructor(notBefore: Date);
|
|
220
|
+
}
|
|
221
|
+
declare class InvalidTokenError extends SigilError {
|
|
222
|
+
constructor(message: string);
|
|
223
|
+
}
|
|
224
|
+
declare class InvalidSignatureError extends SigilError {
|
|
225
|
+
constructor();
|
|
226
|
+
}
|
|
227
|
+
declare class AlgorithmMismatchError extends SigilError {
|
|
228
|
+
constructor(expected: Algorithm, received: Algorithm);
|
|
229
|
+
}
|
|
230
|
+
declare class TokenRevokedError extends SigilError {
|
|
231
|
+
jti: string;
|
|
232
|
+
revokedAt: Date;
|
|
233
|
+
reason?: string | undefined;
|
|
234
|
+
revokedIdentifierType?: "jti" | "user" | "session" | "composite" | undefined;
|
|
235
|
+
revokedIdentifierValue?: string | undefined;
|
|
236
|
+
constructor(jti: string, revokedAt: Date, reason?: string | undefined, revokedIdentifierType?: "jti" | "user" | "session" | "composite" | undefined, revokedIdentifierValue?: string | undefined);
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* Sigil — minimal API.
|
|
241
|
+
*
|
|
242
|
+
* const key = keys.secret()
|
|
243
|
+
* const token = await sign({ userId: 1 }, key, { expiresIn: '1h' })
|
|
244
|
+
* const { userId } = await verify(token, key)
|
|
245
|
+
*
|
|
246
|
+
* The algorithm is bound to the key type, so a verifier can never be tricked
|
|
247
|
+
* into accepting a token made with a different algorithm.
|
|
248
|
+
*/
|
|
249
|
+
|
|
250
|
+
type KeyKind = 'secret' | 'ed25519' | 'pq';
|
|
251
|
+
interface SecretKey {
|
|
252
|
+
readonly type: 'secret';
|
|
253
|
+
readonly bytes: Buffer;
|
|
254
|
+
}
|
|
255
|
+
interface PrivateKey {
|
|
256
|
+
readonly type: 'private';
|
|
257
|
+
readonly kind: 'ed25519' | 'pq';
|
|
258
|
+
readonly bytes: Buffer;
|
|
259
|
+
}
|
|
260
|
+
interface PublicKey {
|
|
261
|
+
readonly type: 'public';
|
|
262
|
+
readonly kind: 'ed25519' | 'pq';
|
|
263
|
+
readonly bytes: Buffer;
|
|
264
|
+
}
|
|
265
|
+
type SigningKey = SecretKey | PrivateKey;
|
|
266
|
+
type VerifyingKey = SecretKey | PublicKey;
|
|
267
|
+
type AnyKey = SecretKey | PrivateKey | PublicKey;
|
|
268
|
+
interface SignOptions {
|
|
269
|
+
expiresIn?: TimeValue;
|
|
270
|
+
notBefore?: TimeValue;
|
|
271
|
+
issuer?: string;
|
|
272
|
+
audience?: string | string[];
|
|
273
|
+
subject?: string;
|
|
274
|
+
jti?: string | (() => string);
|
|
275
|
+
format?: TokenFormat;
|
|
276
|
+
}
|
|
277
|
+
type SigilVerifyOptions = Omit<VerifyOptions, 'secret' | 'publicKey' | 'algorithms'> & {
|
|
278
|
+
complete?: boolean;
|
|
279
|
+
};
|
|
280
|
+
declare const keys: {
|
|
281
|
+
/**
|
|
282
|
+
* 32-byte symmetric key. Without an argument a random key is generated.
|
|
283
|
+
* A string (>= 32 chars, e.g. from env) is stretched with HKDF-SHA256.
|
|
284
|
+
*/
|
|
285
|
+
secret(from?: string | Buffer): SecretKey;
|
|
286
|
+
/** Signing key pair: Ed25519 (default, ~0.3 KB tokens) or 'pq' ML-DSA-65 (post-quantum, ~4.5 KB tokens) */
|
|
287
|
+
pair(kind?: "ed25519" | "pq"): Promise<{
|
|
288
|
+
privateKey: PrivateKey;
|
|
289
|
+
publicKey: PublicKey;
|
|
290
|
+
}>;
|
|
291
|
+
/** Public key for a private key */
|
|
292
|
+
publicOf(key: PrivateKey): PublicKey;
|
|
293
|
+
/** Serialize a key to a prefixed string (store in env / secret manager) */
|
|
294
|
+
export(key: AnyKey): string;
|
|
295
|
+
/** Parse a string produced by keys.export() */
|
|
296
|
+
import(str: string): AnyKey;
|
|
297
|
+
};
|
|
298
|
+
/** Create a token. Default expiry: 1 hour. */
|
|
299
|
+
declare function sign<T extends Record<string, unknown>>(payload: T, key: SigningKey, options?: SignOptions): Promise<string>;
|
|
300
|
+
/** Verify a token and return its payload (or the full decoded token with `complete: true`). */
|
|
301
|
+
declare function verify<T = Record<string, unknown>>(token: string, key: VerifyingKey, options?: SigilVerifyOptions & {
|
|
302
|
+
complete?: false;
|
|
303
|
+
}): Promise<T>;
|
|
304
|
+
declare function verify<T = Record<string, unknown>>(token: string, key: VerifyingKey, options: SigilVerifyOptions & {
|
|
305
|
+
complete: true;
|
|
306
|
+
}): Promise<DecodedToken<T>>;
|
|
307
|
+
/** Read claims WITHOUT verifying (signed tokens only). Never trust the result. */
|
|
308
|
+
declare function inspect<T = Record<string, unknown>>(token: string): DecodedToken<T>;
|
|
309
|
+
|
|
310
|
+
/**
|
|
311
|
+
* In-memory revocation store implementation
|
|
312
|
+
* Best for single-server applications with ephemeral data
|
|
313
|
+
*/
|
|
314
|
+
|
|
315
|
+
declare class MemoryStore extends RevocationStore {
|
|
316
|
+
private revokedTokens;
|
|
317
|
+
private userTokens;
|
|
318
|
+
private revokedSessions;
|
|
319
|
+
private revokedUsers;
|
|
320
|
+
private compositeKeys;
|
|
321
|
+
constructor(config?: RevocationStoreConfig);
|
|
322
|
+
/**
|
|
323
|
+
* Revoke a token by its identifier
|
|
324
|
+
*/
|
|
325
|
+
revoke(identifier: RevocationIdentifier): Promise<void>;
|
|
326
|
+
/**
|
|
327
|
+
* Check if a token is revoked by jti
|
|
328
|
+
*/
|
|
329
|
+
isRevoked(jti: string): Promise<boolean>;
|
|
330
|
+
/**
|
|
331
|
+
* Get all revoked tokens for a user
|
|
332
|
+
*/
|
|
333
|
+
getRevokedTokensForUser(userId: string): Promise<string[]>;
|
|
334
|
+
/**
|
|
335
|
+
* Revoke all tokens for a user
|
|
336
|
+
* Note: This only revokes tokens that are already tracked.
|
|
337
|
+
* Future tokens from this user will need to be rejected at application level.
|
|
338
|
+
*/
|
|
339
|
+
revokeAllForUser(userId: string, reason?: string): Promise<void>;
|
|
340
|
+
/**
|
|
341
|
+
* Revoke a session
|
|
342
|
+
*/
|
|
343
|
+
revokeSession(sessionId: string, reason?: string): Promise<void>;
|
|
344
|
+
/**
|
|
345
|
+
* Check if a session is revoked
|
|
346
|
+
*/
|
|
347
|
+
isSessionRevoked(sessionId: string): Promise<boolean>;
|
|
348
|
+
/**
|
|
349
|
+
* Check if a user's all tokens should be rejected
|
|
350
|
+
*/
|
|
351
|
+
isUserRevoked(userId: string): Promise<boolean>;
|
|
352
|
+
/**
|
|
353
|
+
* Revoke by composite key
|
|
354
|
+
*/
|
|
355
|
+
revokeByCompositeKey(key: string, components: Record<string, string>, exp: number, reason?: string): Promise<void>;
|
|
356
|
+
/**
|
|
357
|
+
* Check if a composite key is revoked
|
|
358
|
+
*/
|
|
359
|
+
isCompositeKeyRevoked(key: string): Promise<boolean>;
|
|
360
|
+
/**
|
|
361
|
+
* Manual cleanup of expired entries
|
|
362
|
+
*/
|
|
363
|
+
cleanup(): Promise<number>;
|
|
364
|
+
/**
|
|
365
|
+
* Disconnect (clear all data)
|
|
366
|
+
*/
|
|
367
|
+
disconnect(): Promise<void>;
|
|
368
|
+
/**
|
|
369
|
+
* Get store statistics
|
|
370
|
+
*/
|
|
371
|
+
getStats(): {
|
|
372
|
+
tokens: number;
|
|
373
|
+
users: number;
|
|
374
|
+
sessions: number;
|
|
375
|
+
compositeKeys: number;
|
|
376
|
+
};
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
/**
|
|
380
|
+
* Redis-based revocation store implementation
|
|
381
|
+
* Best for distributed systems with native TTL support
|
|
382
|
+
*/
|
|
383
|
+
|
|
384
|
+
interface RedisClient {
|
|
385
|
+
status?: string;
|
|
386
|
+
connect(): Promise<void>;
|
|
387
|
+
setex(key: string, seconds: number, value: string): Promise<string>;
|
|
388
|
+
get(key: string): Promise<string | null>;
|
|
389
|
+
del(...keys: string[]): Promise<number>;
|
|
390
|
+
sadd(key: string, ...members: string[]): Promise<number>;
|
|
391
|
+
smembers(key: string): Promise<string[]>;
|
|
392
|
+
srem(key: string, ...members: string[]): Promise<number>;
|
|
393
|
+
sismember(key: string, member: string): Promise<number>;
|
|
394
|
+
expire(key: string, seconds: number): Promise<number>;
|
|
395
|
+
ttl(key: string): Promise<number>;
|
|
396
|
+
scan(cursor: string, ...args: (string | number)[]): Promise<[string, string[]]>;
|
|
397
|
+
pipeline(): RedisPipeline;
|
|
398
|
+
hset(key: string, field: string, value: string): Promise<number>;
|
|
399
|
+
hmset(key: string, values: Record<string, string>): Promise<string>;
|
|
400
|
+
hgetall(key: string): Promise<Record<string, string>>;
|
|
401
|
+
hexists(key: string, field: string): Promise<number>;
|
|
402
|
+
quit(): Promise<void>;
|
|
403
|
+
}
|
|
404
|
+
interface RedisPipeline {
|
|
405
|
+
setex(key: string, seconds: number, value: string): RedisPipeline;
|
|
406
|
+
sadd(key: string, member: string): RedisPipeline;
|
|
407
|
+
expire(key: string, seconds: number): RedisPipeline;
|
|
408
|
+
del(key: string): RedisPipeline;
|
|
409
|
+
hset(key: string, field: string, value: string): RedisPipeline;
|
|
410
|
+
hmset(key: string, values: Record<string, string>): RedisPipeline;
|
|
411
|
+
exec(): Promise<any[]>;
|
|
412
|
+
}
|
|
413
|
+
declare class RedisStore extends RevocationStore {
|
|
414
|
+
protected config: RedisStoreConfig;
|
|
415
|
+
private client;
|
|
416
|
+
private Redis;
|
|
417
|
+
identifierConfig?: IdentifierConfig;
|
|
418
|
+
constructor(config?: RedisStoreConfig);
|
|
419
|
+
private initialize;
|
|
420
|
+
private getKey;
|
|
421
|
+
private calculateTTL;
|
|
422
|
+
private ensureConnected;
|
|
423
|
+
revoke(identifier: RevocationIdentifier): Promise<void>;
|
|
424
|
+
isRevoked(jti: string): Promise<boolean>;
|
|
425
|
+
getRevokedTokensForUser(userId: string): Promise<string[]>;
|
|
426
|
+
revokeAllForUser(userId: string, reason?: string): Promise<void>;
|
|
427
|
+
revokeSession(sessionId: string, reason?: string): Promise<void>;
|
|
428
|
+
isSessionRevoked(sessionId: string): Promise<boolean>;
|
|
429
|
+
isUserRevoked(userId: string): Promise<boolean>;
|
|
430
|
+
revokeByCompositeKey(key: string, components: Record<string, string>, exp: number, reason?: string): Promise<void>;
|
|
431
|
+
isCompositeKeyRevoked(key: string): Promise<boolean>;
|
|
432
|
+
cleanup(): Promise<number>;
|
|
433
|
+
disconnect(): Promise<void>;
|
|
434
|
+
getClient(): RedisClient | null;
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
/**
|
|
438
|
+
* SQLite-based revocation store implementation
|
|
439
|
+
* Best for single-server applications with persistent storage
|
|
440
|
+
*/
|
|
441
|
+
|
|
442
|
+
interface DatabaseInstance {
|
|
443
|
+
prepare(sql: string): Statement;
|
|
444
|
+
exec(sql: string): void;
|
|
445
|
+
close(): void;
|
|
446
|
+
}
|
|
447
|
+
interface Statement {
|
|
448
|
+
run(...params: any[]): {
|
|
449
|
+
changes: number;
|
|
450
|
+
lastInsertRowid: number;
|
|
451
|
+
};
|
|
452
|
+
get(...params: any[]): any;
|
|
453
|
+
all(...params: any[]): any[];
|
|
454
|
+
}
|
|
455
|
+
declare class SQLiteStore extends RevocationStore {
|
|
456
|
+
protected config: SQLiteStoreConfig;
|
|
457
|
+
private db;
|
|
458
|
+
private initialized;
|
|
459
|
+
private Database;
|
|
460
|
+
constructor(config?: SQLiteStoreConfig);
|
|
461
|
+
private initialize;
|
|
462
|
+
private createTables;
|
|
463
|
+
private ensureInitialized;
|
|
464
|
+
revoke(identifier: RevocationIdentifier): Promise<void>;
|
|
465
|
+
revokeByCompositeKey(key: string, components: Record<string, string>, exp: number, reason?: string): Promise<void>;
|
|
466
|
+
isRevoked(jti: string): Promise<boolean>;
|
|
467
|
+
isCompositeKeyRevoked(key: string): Promise<boolean>;
|
|
468
|
+
getRevokedTokensForUser(userId: string): Promise<string[]>;
|
|
469
|
+
revokeAllForUser(userId: string, reason?: string): Promise<void>;
|
|
470
|
+
revokeSession(sessionId: string, reason?: string): Promise<void>;
|
|
471
|
+
isSessionRevoked(sessionId: string): Promise<boolean>;
|
|
472
|
+
isUserRevoked(userId: string): Promise<boolean>;
|
|
473
|
+
cleanup(): Promise<number>;
|
|
474
|
+
disconnect(): Promise<void>;
|
|
475
|
+
getStats(): {
|
|
476
|
+
tokens: number;
|
|
477
|
+
users: number;
|
|
478
|
+
sessions: number;
|
|
479
|
+
};
|
|
480
|
+
getDatabase(): DatabaseInstance | null;
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
/**
|
|
484
|
+
* High-level revocation manager with convenient API
|
|
485
|
+
*/
|
|
486
|
+
|
|
487
|
+
interface RevocationManagerConfig {
|
|
488
|
+
store: RevocationStore;
|
|
489
|
+
/** Throw error on revocation check fail (default: true) */
|
|
490
|
+
throwOnRevoked?: boolean;
|
|
491
|
+
/** Enable debug logging */
|
|
492
|
+
debug?: boolean;
|
|
493
|
+
/** Identifier configuration for flexible extraction */
|
|
494
|
+
identifierConfig?: IdentifierConfig;
|
|
495
|
+
}
|
|
496
|
+
declare class RevocationManager {
|
|
497
|
+
private store;
|
|
498
|
+
private config;
|
|
499
|
+
constructor(config: RevocationManagerConfig);
|
|
500
|
+
/**
|
|
501
|
+
* Revoke a token
|
|
502
|
+
*/
|
|
503
|
+
revokeToken(token: string | DecodedToken, reason?: string): Promise<void>;
|
|
504
|
+
/**
|
|
505
|
+
* Revoke a token by JTI directly
|
|
506
|
+
*/
|
|
507
|
+
revokeByJti(jti: string, exp: number, options?: {
|
|
508
|
+
userId?: string;
|
|
509
|
+
sessionId?: string;
|
|
510
|
+
reason?: string;
|
|
511
|
+
}): Promise<void>;
|
|
512
|
+
/**
|
|
513
|
+
* Revoke by composite key components
|
|
514
|
+
*/
|
|
515
|
+
revokeByCompositeKey(components: string[], exp: number, reason?: string): Promise<void>;
|
|
516
|
+
/**
|
|
517
|
+
* Revoke a token with flexible identifier extraction
|
|
518
|
+
*/
|
|
519
|
+
revokeTokenFlexible(token: string | DecodedToken, config?: IdentifierConfig, reason?: string): Promise<void>;
|
|
520
|
+
/**
|
|
521
|
+
* Revoke all tokens for a user
|
|
522
|
+
*/
|
|
523
|
+
revokeUser(userId: string, reason?: string): Promise<void>;
|
|
524
|
+
/**
|
|
525
|
+
* Revoke a session
|
|
526
|
+
*/
|
|
527
|
+
revokeSession(sessionId: string, reason?: string): Promise<void>;
|
|
528
|
+
/**
|
|
529
|
+
* Check if a token is revoked during verification
|
|
530
|
+
* This is called automatically by verifyToken
|
|
531
|
+
*/
|
|
532
|
+
checkRevoked(token: DecodedToken, tokenConfig?: IdentifierConfig): Promise<boolean>;
|
|
533
|
+
/**
|
|
534
|
+
* Check if a specific token is revoked
|
|
535
|
+
*/
|
|
536
|
+
isRevoked(jti: string): Promise<boolean>;
|
|
537
|
+
/**
|
|
538
|
+
* Check if a user is revoked
|
|
539
|
+
*/
|
|
540
|
+
isUserRevoked(userId: string): Promise<boolean>;
|
|
541
|
+
/**
|
|
542
|
+
* Check if a session is revoked
|
|
543
|
+
*/
|
|
544
|
+
isSessionRevoked(sessionId: string): Promise<boolean>;
|
|
545
|
+
/**
|
|
546
|
+
* Get all revoked tokens for a user
|
|
547
|
+
*/
|
|
548
|
+
getRevokedTokensForUser(userId: string): Promise<string[]>;
|
|
549
|
+
/**
|
|
550
|
+
* Start automatic cleanup
|
|
551
|
+
*/
|
|
552
|
+
startAutoCleanup(intervalMs?: number): void;
|
|
553
|
+
/**
|
|
554
|
+
* Stop automatic cleanup
|
|
555
|
+
*/
|
|
556
|
+
stopAutoCleanup(): void;
|
|
557
|
+
/**
|
|
558
|
+
* Manual cleanup
|
|
559
|
+
*/
|
|
560
|
+
cleanup(): Promise<number>;
|
|
561
|
+
/**
|
|
562
|
+
* Disconnect and cleanup resources
|
|
563
|
+
*/
|
|
564
|
+
disconnect(): Promise<void>;
|
|
565
|
+
/**
|
|
566
|
+
* Get the underlying store
|
|
567
|
+
*/
|
|
568
|
+
getStore(): RevocationStore;
|
|
569
|
+
/**
|
|
570
|
+
* Extract identifiers from a token payload using IdentifierExtractor
|
|
571
|
+
*/
|
|
572
|
+
extractIdentifiers(payload: unknown, config?: IdentifierConfig): ExtractedIdentifiers;
|
|
573
|
+
/**
|
|
574
|
+
* Merge global config with per-token config
|
|
575
|
+
* Per-token config takes precedence
|
|
576
|
+
*/
|
|
577
|
+
mergeConfigs(storeConfig?: IdentifierConfig, tokenConfig?: IdentifierConfig): IdentifierConfig | undefined;
|
|
578
|
+
}
|
|
579
|
+
/**
|
|
580
|
+
* Create a revocation manager with the given store
|
|
581
|
+
*/
|
|
582
|
+
declare function createRevocationManager(store: RevocationStore, options?: {
|
|
583
|
+
throwOnRevoked?: boolean;
|
|
584
|
+
debug?: boolean;
|
|
585
|
+
identifierConfig?: IdentifierConfig;
|
|
586
|
+
}): RevocationManager;
|
|
587
|
+
|
|
588
|
+
/**
|
|
589
|
+
* AES-256-GCM (v1.local / v2.local) — native node:crypto, synchronous core.
|
|
590
|
+
* Format: [iv (12)] [ciphertext] [tag (16)]
|
|
591
|
+
*/
|
|
592
|
+
declare class AES256GCM {
|
|
593
|
+
static generateKey(): Promise<Buffer>;
|
|
594
|
+
static encrypt(plaintext: Buffer, key: Buffer, aad?: Buffer): Promise<Buffer>;
|
|
595
|
+
static decrypt(ciphertext: Buffer, key: Buffer, aad?: Buffer): Promise<Buffer>;
|
|
596
|
+
/** Derive key from password using PBKDF2-SHA256 (OWASP 2023: >= 600k iterations) */
|
|
597
|
+
static deriveKey(password: string, salt: Buffer, iterations?: number): Promise<Buffer>;
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
/**
|
|
601
|
+
* Ed25519 (v1.public) — native node:crypto with a bounded KeyObject cache.
|
|
602
|
+
* Keys are raw 32-byte buffers.
|
|
603
|
+
*/
|
|
604
|
+
interface Ed25519KeyPair {
|
|
605
|
+
privateKey: Buffer;
|
|
606
|
+
publicKey: Buffer;
|
|
607
|
+
}
|
|
608
|
+
declare class Ed25519 {
|
|
609
|
+
static generateKeyPair(): Promise<Ed25519KeyPair>;
|
|
610
|
+
static sign(data: Buffer, privateKey: Buffer): Promise<Buffer>;
|
|
611
|
+
static verify(data: Buffer, signature: Buffer, publicKey: Buffer): Promise<boolean>;
|
|
612
|
+
/** Derive the real Ed25519 public key from a raw private key */
|
|
613
|
+
static getPublicKeyFromPrivate(privateKey: Buffer): Buffer;
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
/**
|
|
617
|
+
* ML-DSA-65 (NIST FIPS 204, CNSA 2.0) — post-quantum signatures for v3.public.
|
|
618
|
+
* Native node:crypto (Node >= 24.7). Keys: 32-byte seed (private), 1952-byte raw public key.
|
|
619
|
+
*/
|
|
620
|
+
interface MLDSA65KeyPair {
|
|
621
|
+
privateKey: Buffer;
|
|
622
|
+
publicKey: Buffer;
|
|
623
|
+
}
|
|
624
|
+
declare class MLDSA65 {
|
|
625
|
+
static isSupported(): boolean;
|
|
626
|
+
static generateKeyPair(): Promise<MLDSA65KeyPair>;
|
|
627
|
+
static sign(data: Buffer, privateKey: Buffer): Promise<Buffer>;
|
|
628
|
+
static verify(data: Buffer, signature: Buffer, publicKey: Buffer): Promise<boolean>;
|
|
629
|
+
static getPublicKeyFromPrivate(privateKey: Buffer): Buffer;
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
/**
|
|
633
|
+
* Sigil — fast, minimal, post-quantum ready tokens for Node.js.
|
|
634
|
+
*
|
|
635
|
+
* @example
|
|
636
|
+
* import { sign, verify, keys } from 'sealtok'
|
|
637
|
+
* const key = keys.secret(process.env.SIGIL_SECRET!)
|
|
638
|
+
* const token = await sign({ userId: 1 }, key, { expiresIn: '1h' })
|
|
639
|
+
* const { userId } = await verify(token, key)
|
|
640
|
+
*/
|
|
641
|
+
|
|
642
|
+
declare const VERSION = "1.0.0";
|
|
643
|
+
|
|
644
|
+
export { AES256GCM, AlgorithmMismatchError, Ed25519, InvalidSignatureError, InvalidTokenError, MLDSA65, MemoryStore, RedisStore, RevocationManager, RevocationStore, SQLiteStore, SigilError, TokenExpiredError, TokenNotBeforeError, TokenRevokedError, VERSION, createRevocationManager, inspect, keys, sign, verify };
|
|
645
|
+
export type { AnyKey, CompositeKeyConfig, DecodedToken, IdentifierConfig, KeyKind, PrivateKey, PublicKey, SecretKey, SignOptions, SigningKey, TokenHeader, TokenPayload, SigilVerifyOptions as VerifyOptions, VerifyingKey };
|