@zero-server/sdk 0.9.1 → 0.9.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/LICENSE +21 -21
- package/README.md +460 -443
- package/index.js +414 -412
- package/lib/app.js +1172 -1172
- package/lib/auth/authorize.js +399 -399
- package/lib/auth/enrollment.js +367 -367
- package/lib/auth/index.js +57 -57
- package/lib/auth/jwt.js +731 -731
- package/lib/auth/oauth.js +362 -362
- package/lib/auth/session.js +588 -588
- package/lib/auth/trustedDevice.js +409 -409
- package/lib/auth/twoFactor.js +1150 -1150
- package/lib/auth/webauthn.js +946 -946
- package/lib/body/index.js +14 -14
- package/lib/body/json.js +109 -109
- package/lib/body/multipart.js +440 -440
- package/lib/body/raw.js +71 -71
- package/lib/body/rawBuffer.js +160 -160
- package/lib/body/sendError.js +25 -25
- package/lib/body/text.js +75 -75
- package/lib/body/typeMatch.js +41 -41
- package/lib/body/urlencoded.js +235 -235
- package/lib/cli.js +845 -845
- package/lib/cluster.js +666 -666
- package/lib/debug.js +372 -372
- package/lib/env/index.js +465 -465
- package/lib/errors.js +683 -683
- package/lib/fetch/index.js +256 -256
- package/lib/grpc/balancer.js +378 -378
- package/lib/grpc/call.js +708 -708
- package/lib/grpc/client.js +764 -764
- package/lib/grpc/codec.js +1221 -1221
- package/lib/grpc/credentials.js +398 -398
- package/lib/grpc/frame.js +262 -262
- package/lib/grpc/health.js +287 -287
- package/lib/grpc/index.js +121 -121
- package/lib/grpc/metadata.js +461 -461
- package/lib/grpc/proto.js +821 -821
- package/lib/grpc/reflection.js +590 -590
- package/lib/grpc/server.js +445 -445
- package/lib/grpc/status.js +118 -118
- package/lib/grpc/watch.js +173 -173
- package/lib/http/index.js +10 -10
- package/lib/http/request.js +727 -727
- package/lib/http/response.js +799 -799
- package/lib/lifecycle.js +557 -557
- package/lib/middleware/compress.js +230 -230
- package/lib/middleware/cookieParser.js +237 -237
- package/lib/middleware/cors.js +93 -93
- package/lib/middleware/csrf.js +137 -137
- package/lib/middleware/errorHandler.js +101 -101
- package/lib/middleware/helmet.js +175 -175
- package/lib/middleware/index.js +19 -17
- package/lib/middleware/logger.js +74 -74
- package/lib/middleware/rateLimit.js +88 -88
- package/lib/middleware/requestId.js +53 -53
- package/lib/middleware/static.js +326 -326
- package/lib/middleware/timeout.js +71 -71
- package/lib/middleware/validator.js +255 -255
- package/lib/observe/health.js +326 -326
- package/lib/observe/index.js +50 -50
- package/lib/observe/logger.js +359 -359
- package/lib/observe/metrics.js +805 -805
- package/lib/observe/tracing.js +592 -592
- package/lib/orm/adapters/json.js +290 -290
- package/lib/orm/adapters/memory.js +764 -764
- package/lib/orm/adapters/mongo.js +764 -764
- package/lib/orm/adapters/mysql.js +933 -933
- package/lib/orm/adapters/postgres.js +1144 -1144
- package/lib/orm/adapters/redis.js +1534 -1534
- package/lib/orm/adapters/sql-base.js +212 -212
- package/lib/orm/adapters/sqlite.js +858 -858
- package/lib/orm/audit.js +649 -649
- package/lib/orm/cache.js +394 -394
- package/lib/orm/geo.js +387 -387
- package/lib/orm/index.js +784 -784
- package/lib/orm/migrate.js +432 -432
- package/lib/orm/model.js +1706 -1706
- package/lib/orm/plugin.js +375 -375
- package/lib/orm/procedures.js +836 -836
- package/lib/orm/profiler.js +233 -233
- package/lib/orm/query.js +1772 -1772
- package/lib/orm/replicas.js +241 -241
- package/lib/orm/schema.js +307 -307
- package/lib/orm/search.js +380 -380
- package/lib/orm/seed/data/commerce.js +136 -136
- package/lib/orm/seed/data/internet.js +111 -111
- package/lib/orm/seed/data/locations.js +204 -204
- package/lib/orm/seed/data/names.js +338 -338
- package/lib/orm/seed/data/person.js +128 -128
- package/lib/orm/seed/data/phone.js +211 -211
- package/lib/orm/seed/data/words.js +134 -134
- package/lib/orm/seed/factory.js +178 -178
- package/lib/orm/seed/fake.js +1186 -1186
- package/lib/orm/seed/index.js +18 -18
- package/lib/orm/seed/rng.js +70 -70
- package/lib/orm/seed/seeder.js +124 -124
- package/lib/orm/seed/unique.js +68 -68
- package/lib/orm/snapshot.js +366 -366
- package/lib/orm/tenancy.js +605 -605
- package/lib/orm/views.js +350 -350
- package/lib/router/index.js +436 -436
- package/lib/sse/index.js +8 -8
- package/lib/sse/stream.js +349 -349
- package/lib/ws/connection.js +451 -451
- package/lib/ws/handshake.js +125 -125
- package/lib/ws/index.js +14 -14
- package/lib/ws/room.js +223 -223
- package/package.json +73 -73
- package/types/app.d.ts +223 -223
- package/types/auth.d.ts +520 -520
- package/types/body.d.ts +14 -0
- package/types/cli.d.ts +2 -0
- package/types/cluster.d.ts +75 -75
- package/types/env.d.ts +80 -80
- package/types/errors.d.ts +316 -316
- package/types/fetch.d.ts +43 -43
- package/types/grpc.d.ts +432 -432
- package/types/index.d.ts +384 -384
- package/types/lifecycle.d.ts +60 -60
- package/types/middleware.d.ts +320 -320
- package/types/observe.d.ts +304 -304
- package/types/orm.d.ts +1887 -1887
- package/types/request.d.ts +109 -109
- package/types/response.d.ts +157 -157
- package/types/router.d.ts +78 -78
- package/types/sse.d.ts +78 -78
- package/types/websocket.d.ts +126 -126
package/types/auth.d.ts
CHANGED
|
@@ -1,520 +1,520 @@
|
|
|
1
|
-
import { Request } from './request';
|
|
2
|
-
import { Response } from './response';
|
|
3
|
-
import { MiddlewareFunction } from './middleware';
|
|
4
|
-
|
|
5
|
-
// --- JWT ---------------------------------------------------------
|
|
6
|
-
|
|
7
|
-
export interface JwtHeader {
|
|
8
|
-
alg: string;
|
|
9
|
-
typ: string;
|
|
10
|
-
kid?: string;
|
|
11
|
-
[key: string]: any;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export interface JwtPayload {
|
|
15
|
-
iss?: string;
|
|
16
|
-
sub?: string;
|
|
17
|
-
aud?: string | string[];
|
|
18
|
-
exp?: number;
|
|
19
|
-
nbf?: number;
|
|
20
|
-
iat?: number;
|
|
21
|
-
jti?: string;
|
|
22
|
-
[key: string]: any;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export interface JwtDecoded {
|
|
26
|
-
header: JwtHeader;
|
|
27
|
-
payload: JwtPayload;
|
|
28
|
-
signature: string;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export interface JwtSignOptions {
|
|
32
|
-
algorithm?: string;
|
|
33
|
-
expiresIn?: number;
|
|
34
|
-
issuer?: string;
|
|
35
|
-
audience?: string;
|
|
36
|
-
subject?: string;
|
|
37
|
-
jwtId?: string;
|
|
38
|
-
notBefore?: number;
|
|
39
|
-
header?: Record<string, any>;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
export interface JwtVerifyOptions {
|
|
43
|
-
algorithms?: string | string[];
|
|
44
|
-
audience?: string | string[];
|
|
45
|
-
issuer?: string | string[];
|
|
46
|
-
subject?: string;
|
|
47
|
-
clockTolerance?: number;
|
|
48
|
-
maxAge?: number;
|
|
49
|
-
ignoreExpiration?: boolean;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
export interface JwtVerifyResult {
|
|
53
|
-
header: JwtHeader;
|
|
54
|
-
payload: JwtPayload;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
export interface JwtMiddlewareOptions {
|
|
58
|
-
secret?: string | Buffer;
|
|
59
|
-
publicKey?: string | Buffer;
|
|
60
|
-
getKey?: (header: JwtHeader, payload: JwtPayload) => string | Buffer | Promise<string | Buffer>;
|
|
61
|
-
jwksUri?: string;
|
|
62
|
-
algorithms?: string | string[];
|
|
63
|
-
getToken?: (req: Request) => string | null;
|
|
64
|
-
tokenLocation?: 'header' | 'cookie' | 'query';
|
|
65
|
-
cookieName?: string;
|
|
66
|
-
queryParam?: string;
|
|
67
|
-
audience?: string | string[];
|
|
68
|
-
issuer?: string | string[];
|
|
69
|
-
subject?: string;
|
|
70
|
-
clockTolerance?: number;
|
|
71
|
-
maxAge?: number;
|
|
72
|
-
credentialsRequired?: boolean;
|
|
73
|
-
isRevoked?: (payload: JwtPayload) => boolean | Promise<boolean>;
|
|
74
|
-
onError?: (err: { message: string; code: string; statusCode: number }, req: Request, res: Response) => void;
|
|
75
|
-
fetcher?: Function;
|
|
76
|
-
cacheTtl?: number;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
export function jwt(opts: JwtMiddlewareOptions): MiddlewareFunction;
|
|
80
|
-
export function jwtSign(payload: Record<string, any>, secret: string | Buffer, opts?: JwtSignOptions): string;
|
|
81
|
-
export function jwtVerify(token: string, secretOrKey: string | Buffer, opts?: JwtVerifyOptions): JwtVerifyResult;
|
|
82
|
-
export function jwtDecode(token: string): JwtDecoded | null;
|
|
83
|
-
|
|
84
|
-
export interface JwksGetKey {
|
|
85
|
-
(header: JwtHeader, payload?: JwtPayload): Promise<string>;
|
|
86
|
-
_clearCache(): void;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
export interface JwksOptions {
|
|
90
|
-
fetcher?: Function;
|
|
91
|
-
cacheTtl?: number;
|
|
92
|
-
requestTimeout?: number;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
export function jwks(jwksUri: string, opts?: JwksOptions): JwksGetKey;
|
|
96
|
-
|
|
97
|
-
export interface TokenPairConfig {
|
|
98
|
-
accessSecret: string | Buffer;
|
|
99
|
-
refreshSecret?: string | Buffer;
|
|
100
|
-
accessExpiresIn?: number;
|
|
101
|
-
refreshExpiresIn?: number;
|
|
102
|
-
algorithm?: string;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
export interface TokenPairInstance {
|
|
106
|
-
generateTokens(payload: Record<string, any>): { accessToken: string; refreshToken: string };
|
|
107
|
-
verifyRefreshToken(token: string): JwtVerifyResult;
|
|
108
|
-
verifyAccessToken(token: string): JwtVerifyResult;
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
export function tokenPair(config: TokenPairConfig): TokenPairInstance;
|
|
112
|
-
export function createRefreshToken(payload: Record<string, any>, secret: string | Buffer, opts?: { expiresIn?: number; algorithm?: string }): string;
|
|
113
|
-
export const SUPPORTED_ALGORITHMS: string[];
|
|
114
|
-
|
|
115
|
-
// --- Session -----------------------------------------------------
|
|
116
|
-
|
|
117
|
-
export interface SessionData {
|
|
118
|
-
[key: string]: any;
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
export declare class Session {
|
|
122
|
-
id: string;
|
|
123
|
-
constructor(id: string, data?: SessionData);
|
|
124
|
-
get(key: string): any;
|
|
125
|
-
set(key: string, value: any): Session;
|
|
126
|
-
has(key: string): boolean;
|
|
127
|
-
delete(key: string): boolean;
|
|
128
|
-
all(): SessionData;
|
|
129
|
-
readonly size: number;
|
|
130
|
-
clear(): Session;
|
|
131
|
-
destroy(): void;
|
|
132
|
-
regenerate(): void;
|
|
133
|
-
flash(key: string, value: any): Session;
|
|
134
|
-
flashes(key?: string): any[] | Record<string, any[]>;
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
export interface SessionCookieOptions {
|
|
138
|
-
maxAge?: number;
|
|
139
|
-
path?: string;
|
|
140
|
-
domain?: string;
|
|
141
|
-
secure?: boolean;
|
|
142
|
-
httpOnly?: boolean;
|
|
143
|
-
sameSite?: 'Strict' | 'Lax' | 'None' | string;
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
export interface SessionStore {
|
|
147
|
-
get(sid: string): Promise<string | null>;
|
|
148
|
-
set(sid: string, data: string, maxAge?: number): Promise<void>;
|
|
149
|
-
destroy(sid: string): Promise<void>;
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
export interface MemoryStoreOptions {
|
|
153
|
-
ttl?: number;
|
|
154
|
-
pruneInterval?: number;
|
|
155
|
-
maxSessions?: number;
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
export declare class MemoryStore implements SessionStore {
|
|
159
|
-
constructor(opts?: MemoryStoreOptions);
|
|
160
|
-
get(sid: string): Promise<string | null>;
|
|
161
|
-
set(sid: string, data: string, maxAge?: number): Promise<void>;
|
|
162
|
-
destroy(sid: string): Promise<void>;
|
|
163
|
-
readonly length: number;
|
|
164
|
-
clear(): void;
|
|
165
|
-
close(): void;
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
export interface SessionOptions {
|
|
169
|
-
secret: string | string[];
|
|
170
|
-
store?: SessionStore;
|
|
171
|
-
name?: string;
|
|
172
|
-
cookie?: SessionCookieOptions;
|
|
173
|
-
rolling?: boolean;
|
|
174
|
-
genid?: () => string;
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
export function session(opts: SessionOptions): MiddlewareFunction;
|
|
178
|
-
|
|
179
|
-
// --- OAuth2 ------------------------------------------------------
|
|
180
|
-
|
|
181
|
-
export interface OAuthProviderPreset {
|
|
182
|
-
authorizeUrl: string;
|
|
183
|
-
tokenUrl: string;
|
|
184
|
-
userInfoUrl: string | null;
|
|
185
|
-
scope: string;
|
|
186
|
-
pkce: boolean;
|
|
187
|
-
responseMode?: string;
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
export interface OAuthOptions {
|
|
191
|
-
provider?: 'google' | 'github' | 'microsoft' | 'apple';
|
|
192
|
-
clientId: string;
|
|
193
|
-
clientSecret: string;
|
|
194
|
-
callbackUrl: string;
|
|
195
|
-
authorizeUrl?: string;
|
|
196
|
-
tokenUrl?: string;
|
|
197
|
-
userInfoUrl?: string;
|
|
198
|
-
scope?: string;
|
|
199
|
-
pkce?: boolean;
|
|
200
|
-
responseMode?: string;
|
|
201
|
-
fetcher?: Function;
|
|
202
|
-
timeout?: number;
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
export interface OAuthAuthorizeResult {
|
|
206
|
-
url: string;
|
|
207
|
-
state: string;
|
|
208
|
-
codeVerifier?: string;
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
export interface OAuthTokens {
|
|
212
|
-
access_token: string;
|
|
213
|
-
token_type: string;
|
|
214
|
-
expires_in?: number;
|
|
215
|
-
refresh_token?: string;
|
|
216
|
-
id_token?: string;
|
|
217
|
-
scope?: string;
|
|
218
|
-
[key: string]: any;
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
export interface OAuthClient {
|
|
222
|
-
authorize(params?: { scope?: string; state?: string; extra?: Record<string, string> }): OAuthAuthorizeResult;
|
|
223
|
-
callback(query: { code?: string; state?: string; error?: string; error_description?: string }, verify?: { state?: string; codeVerifier?: string }): Promise<OAuthTokens>;
|
|
224
|
-
refresh(refreshToken: string): Promise<OAuthTokens>;
|
|
225
|
-
userInfo(accessToken: string): Promise<Record<string, any>>;
|
|
226
|
-
readonly config: Readonly<OAuthOptions>;
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
export function oauth(opts: OAuthOptions): OAuthClient;
|
|
230
|
-
export function generatePKCE(length?: number): { codeVerifier: string; codeChallenge: string };
|
|
231
|
-
export function generateState(bytes?: number): string;
|
|
232
|
-
export const OAUTH_PROVIDERS: Record<string, OAuthProviderPreset>;
|
|
233
|
-
|
|
234
|
-
// --- Authorization -----------------------------------------------
|
|
235
|
-
|
|
236
|
-
export function authorize(...roles: (string | string[])[]): MiddlewareFunction;
|
|
237
|
-
export function can(...permissions: (string | string[])[]): MiddlewareFunction;
|
|
238
|
-
export function canAny(...permissions: (string | string[])[]): MiddlewareFunction;
|
|
239
|
-
|
|
240
|
-
export declare class Policy {
|
|
241
|
-
before?(user: any, action: string, resource?: any): boolean | undefined;
|
|
242
|
-
check(action: string, user: any, resource?: any): boolean | Promise<boolean>;
|
|
243
|
-
[action: string]: any;
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
export function gate(
|
|
247
|
-
policy: Policy,
|
|
248
|
-
action: string,
|
|
249
|
-
getResource?: (req: Request) => any | Promise<any>,
|
|
250
|
-
): MiddlewareFunction;
|
|
251
|
-
|
|
252
|
-
export function attachUserHelpers(): MiddlewareFunction;
|
|
253
|
-
|
|
254
|
-
// --- Two-Factor Authentication -----------------------------------
|
|
255
|
-
|
|
256
|
-
export interface TwoFactorSecret {
|
|
257
|
-
raw: Buffer;
|
|
258
|
-
base32: string;
|
|
259
|
-
hex: string;
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
export interface TOTPOptions {
|
|
263
|
-
period?: number;
|
|
264
|
-
digits?: number;
|
|
265
|
-
algorithm?: 'sha1' | 'sha256' | 'sha512';
|
|
266
|
-
time?: number;
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
export interface TOTPVerifyOptions extends TOTPOptions {
|
|
270
|
-
window?: number;
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
export interface TOTPVerifyResult {
|
|
274
|
-
valid: boolean;
|
|
275
|
-
delta: number | null;
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
export interface OtpauthURIOptions {
|
|
279
|
-
secret: string | Buffer;
|
|
280
|
-
issuer: string;
|
|
281
|
-
account: string;
|
|
282
|
-
period?: number;
|
|
283
|
-
digits?: number;
|
|
284
|
-
algorithm?: 'sha1' | 'sha256' | 'sha512';
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
export interface BackupCodesResult {
|
|
288
|
-
codes: string[];
|
|
289
|
-
hashes: string[];
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
export interface BackupCodeVerifyResult {
|
|
293
|
-
valid: boolean;
|
|
294
|
-
index: number | null;
|
|
295
|
-
}
|
|
296
|
-
|
|
297
|
-
export interface Require2FAOptions {
|
|
298
|
-
sessionKey?: string;
|
|
299
|
-
errorMessage?: string;
|
|
300
|
-
statusCode?: number;
|
|
301
|
-
isEnabled?: (req: Request) => boolean | Promise<boolean>;
|
|
302
|
-
}
|
|
303
|
-
|
|
304
|
-
export interface VerifyTOTPMiddlewareOptions {
|
|
305
|
-
getSecret: (req: Request) => string | Buffer | Promise<string | Buffer>;
|
|
306
|
-
codeField?: string;
|
|
307
|
-
sessionKey?: string;
|
|
308
|
-
maxAttempts?: number;
|
|
309
|
-
lockoutMs?: number;
|
|
310
|
-
window?: number;
|
|
311
|
-
period?: number;
|
|
312
|
-
algorithm?: 'sha1' | 'sha256' | 'sha512';
|
|
313
|
-
digits?: number;
|
|
314
|
-
replayStore?: ReplayStore;
|
|
315
|
-
getUserId?: (req: Request) => string | Promise<string>;
|
|
316
|
-
onSuccess?: (req: Request, res: Response) => void;
|
|
317
|
-
onFailure?: (req: Request, res: Response, attemptsLeft: number) => void;
|
|
318
|
-
}
|
|
319
|
-
|
|
320
|
-
export interface ReplayStore {
|
|
321
|
-
get(userId: string): Promise<number | null>;
|
|
322
|
-
set(userId: string, counter: number, ttlMs: number): Promise<void>;
|
|
323
|
-
}
|
|
324
|
-
|
|
325
|
-
export declare class InMemoryReplayStore implements ReplayStore {
|
|
326
|
-
constructor();
|
|
327
|
-
get(userId: string): Promise<number | null>;
|
|
328
|
-
set(userId: string, counter: number, ttlMs: number): Promise<void>;
|
|
329
|
-
clear(): void;
|
|
330
|
-
destroy(): void;
|
|
331
|
-
}
|
|
332
|
-
|
|
333
|
-
export interface Verify2FAOptions {
|
|
334
|
-
getSecret: (req: Request) => string | Buffer | Promise<string | Buffer>;
|
|
335
|
-
getBackupHashes?: (req: Request) => string[] | Promise<string[]>;
|
|
336
|
-
onBackupUsed?: (req: Request, index: number) => void | Promise<void>;
|
|
337
|
-
getCredential?: (req: Request, credId: string) => any | Promise<any>;
|
|
338
|
-
updateCredentialCounter?: (req: Request, credId: string, newCounter: number) => void | Promise<void>;
|
|
339
|
-
expectedOrigin?: string;
|
|
340
|
-
expectedRPID?: string;
|
|
341
|
-
codeField?: string;
|
|
342
|
-
backupField?: string;
|
|
343
|
-
sessionKey?: string;
|
|
344
|
-
maxAttempts?: number;
|
|
345
|
-
lockoutMs?: number;
|
|
346
|
-
window?: number;
|
|
347
|
-
period?: number;
|
|
348
|
-
algorithm?: 'sha1' | 'sha256' | 'sha512';
|
|
349
|
-
digits?: number;
|
|
350
|
-
replayStore?: ReplayStore;
|
|
351
|
-
getUserId?: (req: Request) => string | Promise<string>;
|
|
352
|
-
onSuccess?: (req: Request, res: Response, method: 'totp' | 'backup' | 'webauthn') => void;
|
|
353
|
-
onFailure?: (req: Request, res: Response, attemptsLeft: number) => void;
|
|
354
|
-
}
|
|
355
|
-
|
|
356
|
-
export interface TwoFactor {
|
|
357
|
-
generateSecret(bytes?: number): TwoFactorSecret;
|
|
358
|
-
generateTOTP(secret: string | Buffer, opts?: TOTPOptions): string;
|
|
359
|
-
verifyTOTP(token: string, secret: string | Buffer, opts?: TOTPVerifyOptions): TOTPVerifyResult;
|
|
360
|
-
otpauthURI(opts: OtpauthURIOptions): string;
|
|
361
|
-
generateBackupCodes(count?: number, bytes?: number): BackupCodesResult;
|
|
362
|
-
verifyBackupCode(code: string, hashes: string[]): BackupCodeVerifyResult;
|
|
363
|
-
require2FA(opts?: Require2FAOptions): MiddlewareFunction;
|
|
364
|
-
verifyTOTPMiddleware(opts: VerifyTOTPMiddlewareOptions): MiddlewareFunction;
|
|
365
|
-
verify2FA(opts: Verify2FAOptions): MiddlewareFunction;
|
|
366
|
-
InMemoryReplayStore: typeof InMemoryReplayStore;
|
|
367
|
-
DEFAULT_PERIOD: number;
|
|
368
|
-
DEFAULT_DIGITS: number;
|
|
369
|
-
DEFAULT_ALGORITHM: string;
|
|
370
|
-
DEFAULT_WINDOW: number;
|
|
371
|
-
SUPPORTED_TOTP_ALGORITHMS: string[];
|
|
372
|
-
}
|
|
373
|
-
|
|
374
|
-
export const twoFactor: TwoFactor;
|
|
375
|
-
|
|
376
|
-
// --- WebAuthn / Passkeys -----------------------------------------
|
|
377
|
-
|
|
378
|
-
export interface WebAuthnRegistrationOptions {
|
|
379
|
-
rpName: string;
|
|
380
|
-
rpId: string;
|
|
381
|
-
userId: string;
|
|
382
|
-
userName: string;
|
|
383
|
-
userDisplayName?: string;
|
|
384
|
-
challenge?: Buffer;
|
|
385
|
-
challengeSize?: number;
|
|
386
|
-
attestation?: 'none' | 'direct' | 'indirect';
|
|
387
|
-
authenticatorSelection?: {
|
|
388
|
-
authenticatorAttachment?: 'platform' | 'cross-platform';
|
|
389
|
-
residentKey?: 'discouraged' | 'preferred' | 'required';
|
|
390
|
-
requireResidentKey?: boolean;
|
|
391
|
-
userVerification?: 'discouraged' | 'preferred' | 'required';
|
|
392
|
-
};
|
|
393
|
-
excludeCredentials?: Array<{ id: string | Buffer; type?: string; transports?: string[] }>;
|
|
394
|
-
timeout?: number;
|
|
395
|
-
}
|
|
396
|
-
|
|
397
|
-
export interface WebAuthnRegistrationResult {
|
|
398
|
-
verified: boolean;
|
|
399
|
-
credential: {
|
|
400
|
-
id: string;
|
|
401
|
-
publicKey: Buffer;
|
|
402
|
-
counter: number;
|
|
403
|
-
type: string;
|
|
404
|
-
transports?: string[];
|
|
405
|
-
};
|
|
406
|
-
authData: {
|
|
407
|
-
rpIdHash: Buffer;
|
|
408
|
-
flags: number;
|
|
409
|
-
signCount: number;
|
|
410
|
-
aaguid: Buffer;
|
|
411
|
-
credentialId: Buffer;
|
|
412
|
-
};
|
|
413
|
-
}
|
|
414
|
-
|
|
415
|
-
export interface WebAuthnVerifyRegistrationOptions {
|
|
416
|
-
response: any;
|
|
417
|
-
expectedChallenge: string;
|
|
418
|
-
expectedOrigin: string;
|
|
419
|
-
expectedRPID: string;
|
|
420
|
-
}
|
|
421
|
-
|
|
422
|
-
export interface WebAuthnAuthenticationOptions {
|
|
423
|
-
rpId: string;
|
|
424
|
-
challenge?: Buffer;
|
|
425
|
-
challengeSize?: number;
|
|
426
|
-
allowCredentials?: Array<{ id: string | Buffer; type?: string; transports?: string[] }>;
|
|
427
|
-
userVerification?: 'discouraged' | 'preferred' | 'required';
|
|
428
|
-
timeout?: number;
|
|
429
|
-
}
|
|
430
|
-
|
|
431
|
-
export interface WebAuthnAuthenticationResult {
|
|
432
|
-
verified: boolean;
|
|
433
|
-
authData: {
|
|
434
|
-
rpIdHash: Buffer;
|
|
435
|
-
flags: number;
|
|
436
|
-
signCount: number;
|
|
437
|
-
};
|
|
438
|
-
}
|
|
439
|
-
|
|
440
|
-
export interface WebAuthnVerifyAuthenticationOptions {
|
|
441
|
-
response: any;
|
|
442
|
-
expectedChallenge: string;
|
|
443
|
-
expectedOrigin: string;
|
|
444
|
-
expectedRPID: string;
|
|
445
|
-
credential: { publicKey: Buffer; counter: number; id?: string };
|
|
446
|
-
}
|
|
447
|
-
|
|
448
|
-
export interface WebAuthn {
|
|
449
|
-
generateRegistrationOptions(opts: WebAuthnRegistrationOptions): any;
|
|
450
|
-
verifyRegistration(opts: WebAuthnVerifyRegistrationOptions): Promise<WebAuthnRegistrationResult>;
|
|
451
|
-
generateAuthenticationOptions(opts: WebAuthnAuthenticationOptions): any;
|
|
452
|
-
verifyAuthentication(opts: WebAuthnVerifyAuthenticationOptions): Promise<WebAuthnAuthenticationResult>;
|
|
453
|
-
}
|
|
454
|
-
|
|
455
|
-
export const webauthn: WebAuthn;
|
|
456
|
-
|
|
457
|
-
// --- Trusted Device / Remember Me --------------------------------
|
|
458
|
-
|
|
459
|
-
export interface TrustedDeviceIssueOptions {
|
|
460
|
-
secret: string;
|
|
461
|
-
maxAge?: number;
|
|
462
|
-
cookieName?: string;
|
|
463
|
-
fingerprint?: (req: Request) => string | Promise<string>;
|
|
464
|
-
getUserId?: (req: Request) => string | Promise<string>;
|
|
465
|
-
}
|
|
466
|
-
|
|
467
|
-
export interface TrustedDeviceVerifyOptions {
|
|
468
|
-
secret: string;
|
|
469
|
-
previousSecrets?: string | string[];
|
|
470
|
-
cookieName?: string;
|
|
471
|
-
fingerprint?: (req: Request) => string | Promise<string>;
|
|
472
|
-
getUserId?: (req: Request) => string | Promise<string>;
|
|
473
|
-
checkIP?: boolean;
|
|
474
|
-
}
|
|
475
|
-
|
|
476
|
-
export interface TrustedDeviceRevokeOptions {
|
|
477
|
-
cookieName?: string;
|
|
478
|
-
}
|
|
479
|
-
|
|
480
|
-
export interface TrustedDevice {
|
|
481
|
-
issue(opts: TrustedDeviceIssueOptions): MiddlewareFunction;
|
|
482
|
-
verify(opts: TrustedDeviceVerifyOptions): (req: Request) => Promise<boolean>;
|
|
483
|
-
revoke(opts?: TrustedDeviceRevokeOptions): MiddlewareFunction;
|
|
484
|
-
}
|
|
485
|
-
|
|
486
|
-
export const trustedDevice: TrustedDevice;
|
|
487
|
-
|
|
488
|
-
// --- 2FA Enrollment Flow -----------------------------------------
|
|
489
|
-
|
|
490
|
-
export interface EnrollmentOptions {
|
|
491
|
-
saveSecret: (req: Request, secret: string, backupHashes: string[]) => Promise<void>;
|
|
492
|
-
removeSecret: (req: Request) => Promise<void>;
|
|
493
|
-
issuer?: string;
|
|
494
|
-
getAccount?: (req: Request) => string | Promise<string>;
|
|
495
|
-
sessionKey?: string;
|
|
496
|
-
ttl?: number;
|
|
497
|
-
backupCount?: number;
|
|
498
|
-
window?: number;
|
|
499
|
-
period?: number;
|
|
500
|
-
algorithm?: 'sha1' | 'sha256' | 'sha512';
|
|
501
|
-
digits?: number;
|
|
502
|
-
isEnabled?: (req: Request) => boolean | Promise<boolean>;
|
|
503
|
-
}
|
|
504
|
-
|
|
505
|
-
export interface EnrollmentDisableOptions {
|
|
506
|
-
confirm?: (req: Request) => boolean | Promise<boolean>;
|
|
507
|
-
}
|
|
508
|
-
|
|
509
|
-
export interface EnrollmentVerifyOptions {
|
|
510
|
-
codeField?: string;
|
|
511
|
-
}
|
|
512
|
-
|
|
513
|
-
export interface EnrollmentFlow {
|
|
514
|
-
start(): MiddlewareFunction;
|
|
515
|
-
verify(opts?: EnrollmentVerifyOptions): MiddlewareFunction;
|
|
516
|
-
complete(): MiddlewareFunction;
|
|
517
|
-
disable(opts?: EnrollmentDisableOptions): MiddlewareFunction;
|
|
518
|
-
}
|
|
519
|
-
|
|
520
|
-
export function enrollment(opts: EnrollmentOptions): EnrollmentFlow;
|
|
1
|
+
import { Request } from './request';
|
|
2
|
+
import { Response } from './response';
|
|
3
|
+
import { MiddlewareFunction } from './middleware';
|
|
4
|
+
|
|
5
|
+
// --- JWT ---------------------------------------------------------
|
|
6
|
+
|
|
7
|
+
export interface JwtHeader {
|
|
8
|
+
alg: string;
|
|
9
|
+
typ: string;
|
|
10
|
+
kid?: string;
|
|
11
|
+
[key: string]: any;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface JwtPayload {
|
|
15
|
+
iss?: string;
|
|
16
|
+
sub?: string;
|
|
17
|
+
aud?: string | string[];
|
|
18
|
+
exp?: number;
|
|
19
|
+
nbf?: number;
|
|
20
|
+
iat?: number;
|
|
21
|
+
jti?: string;
|
|
22
|
+
[key: string]: any;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface JwtDecoded {
|
|
26
|
+
header: JwtHeader;
|
|
27
|
+
payload: JwtPayload;
|
|
28
|
+
signature: string;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export interface JwtSignOptions {
|
|
32
|
+
algorithm?: string;
|
|
33
|
+
expiresIn?: number;
|
|
34
|
+
issuer?: string;
|
|
35
|
+
audience?: string;
|
|
36
|
+
subject?: string;
|
|
37
|
+
jwtId?: string;
|
|
38
|
+
notBefore?: number;
|
|
39
|
+
header?: Record<string, any>;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface JwtVerifyOptions {
|
|
43
|
+
algorithms?: string | string[];
|
|
44
|
+
audience?: string | string[];
|
|
45
|
+
issuer?: string | string[];
|
|
46
|
+
subject?: string;
|
|
47
|
+
clockTolerance?: number;
|
|
48
|
+
maxAge?: number;
|
|
49
|
+
ignoreExpiration?: boolean;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export interface JwtVerifyResult {
|
|
53
|
+
header: JwtHeader;
|
|
54
|
+
payload: JwtPayload;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export interface JwtMiddlewareOptions {
|
|
58
|
+
secret?: string | Buffer;
|
|
59
|
+
publicKey?: string | Buffer;
|
|
60
|
+
getKey?: (header: JwtHeader, payload: JwtPayload) => string | Buffer | Promise<string | Buffer>;
|
|
61
|
+
jwksUri?: string;
|
|
62
|
+
algorithms?: string | string[];
|
|
63
|
+
getToken?: (req: Request) => string | null;
|
|
64
|
+
tokenLocation?: 'header' | 'cookie' | 'query';
|
|
65
|
+
cookieName?: string;
|
|
66
|
+
queryParam?: string;
|
|
67
|
+
audience?: string | string[];
|
|
68
|
+
issuer?: string | string[];
|
|
69
|
+
subject?: string;
|
|
70
|
+
clockTolerance?: number;
|
|
71
|
+
maxAge?: number;
|
|
72
|
+
credentialsRequired?: boolean;
|
|
73
|
+
isRevoked?: (payload: JwtPayload) => boolean | Promise<boolean>;
|
|
74
|
+
onError?: (err: { message: string; code: string; statusCode: number }, req: Request, res: Response) => void;
|
|
75
|
+
fetcher?: Function;
|
|
76
|
+
cacheTtl?: number;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export function jwt(opts: JwtMiddlewareOptions): MiddlewareFunction;
|
|
80
|
+
export function jwtSign(payload: Record<string, any>, secret: string | Buffer, opts?: JwtSignOptions): string;
|
|
81
|
+
export function jwtVerify(token: string, secretOrKey: string | Buffer, opts?: JwtVerifyOptions): JwtVerifyResult;
|
|
82
|
+
export function jwtDecode(token: string): JwtDecoded | null;
|
|
83
|
+
|
|
84
|
+
export interface JwksGetKey {
|
|
85
|
+
(header: JwtHeader, payload?: JwtPayload): Promise<string>;
|
|
86
|
+
_clearCache(): void;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export interface JwksOptions {
|
|
90
|
+
fetcher?: Function;
|
|
91
|
+
cacheTtl?: number;
|
|
92
|
+
requestTimeout?: number;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export function jwks(jwksUri: string, opts?: JwksOptions): JwksGetKey;
|
|
96
|
+
|
|
97
|
+
export interface TokenPairConfig {
|
|
98
|
+
accessSecret: string | Buffer;
|
|
99
|
+
refreshSecret?: string | Buffer;
|
|
100
|
+
accessExpiresIn?: number;
|
|
101
|
+
refreshExpiresIn?: number;
|
|
102
|
+
algorithm?: string;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export interface TokenPairInstance {
|
|
106
|
+
generateTokens(payload: Record<string, any>): { accessToken: string; refreshToken: string };
|
|
107
|
+
verifyRefreshToken(token: string): JwtVerifyResult;
|
|
108
|
+
verifyAccessToken(token: string): JwtVerifyResult;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export function tokenPair(config: TokenPairConfig): TokenPairInstance;
|
|
112
|
+
export function createRefreshToken(payload: Record<string, any>, secret: string | Buffer, opts?: { expiresIn?: number; algorithm?: string }): string;
|
|
113
|
+
export const SUPPORTED_ALGORITHMS: string[];
|
|
114
|
+
|
|
115
|
+
// --- Session -----------------------------------------------------
|
|
116
|
+
|
|
117
|
+
export interface SessionData {
|
|
118
|
+
[key: string]: any;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export declare class Session {
|
|
122
|
+
id: string;
|
|
123
|
+
constructor(id: string, data?: SessionData);
|
|
124
|
+
get(key: string): any;
|
|
125
|
+
set(key: string, value: any): Session;
|
|
126
|
+
has(key: string): boolean;
|
|
127
|
+
delete(key: string): boolean;
|
|
128
|
+
all(): SessionData;
|
|
129
|
+
readonly size: number;
|
|
130
|
+
clear(): Session;
|
|
131
|
+
destroy(): void;
|
|
132
|
+
regenerate(): void;
|
|
133
|
+
flash(key: string, value: any): Session;
|
|
134
|
+
flashes(key?: string): any[] | Record<string, any[]>;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
export interface SessionCookieOptions {
|
|
138
|
+
maxAge?: number;
|
|
139
|
+
path?: string;
|
|
140
|
+
domain?: string;
|
|
141
|
+
secure?: boolean;
|
|
142
|
+
httpOnly?: boolean;
|
|
143
|
+
sameSite?: 'Strict' | 'Lax' | 'None' | string;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export interface SessionStore {
|
|
147
|
+
get(sid: string): Promise<string | null>;
|
|
148
|
+
set(sid: string, data: string, maxAge?: number): Promise<void>;
|
|
149
|
+
destroy(sid: string): Promise<void>;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
export interface MemoryStoreOptions {
|
|
153
|
+
ttl?: number;
|
|
154
|
+
pruneInterval?: number;
|
|
155
|
+
maxSessions?: number;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
export declare class MemoryStore implements SessionStore {
|
|
159
|
+
constructor(opts?: MemoryStoreOptions);
|
|
160
|
+
get(sid: string): Promise<string | null>;
|
|
161
|
+
set(sid: string, data: string, maxAge?: number): Promise<void>;
|
|
162
|
+
destroy(sid: string): Promise<void>;
|
|
163
|
+
readonly length: number;
|
|
164
|
+
clear(): void;
|
|
165
|
+
close(): void;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
export interface SessionOptions {
|
|
169
|
+
secret: string | string[];
|
|
170
|
+
store?: SessionStore;
|
|
171
|
+
name?: string;
|
|
172
|
+
cookie?: SessionCookieOptions;
|
|
173
|
+
rolling?: boolean;
|
|
174
|
+
genid?: () => string;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
export function session(opts: SessionOptions): MiddlewareFunction;
|
|
178
|
+
|
|
179
|
+
// --- OAuth2 ------------------------------------------------------
|
|
180
|
+
|
|
181
|
+
export interface OAuthProviderPreset {
|
|
182
|
+
authorizeUrl: string;
|
|
183
|
+
tokenUrl: string;
|
|
184
|
+
userInfoUrl: string | null;
|
|
185
|
+
scope: string;
|
|
186
|
+
pkce: boolean;
|
|
187
|
+
responseMode?: string;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
export interface OAuthOptions {
|
|
191
|
+
provider?: 'google' | 'github' | 'microsoft' | 'apple';
|
|
192
|
+
clientId: string;
|
|
193
|
+
clientSecret: string;
|
|
194
|
+
callbackUrl: string;
|
|
195
|
+
authorizeUrl?: string;
|
|
196
|
+
tokenUrl?: string;
|
|
197
|
+
userInfoUrl?: string;
|
|
198
|
+
scope?: string;
|
|
199
|
+
pkce?: boolean;
|
|
200
|
+
responseMode?: string;
|
|
201
|
+
fetcher?: Function;
|
|
202
|
+
timeout?: number;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
export interface OAuthAuthorizeResult {
|
|
206
|
+
url: string;
|
|
207
|
+
state: string;
|
|
208
|
+
codeVerifier?: string;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
export interface OAuthTokens {
|
|
212
|
+
access_token: string;
|
|
213
|
+
token_type: string;
|
|
214
|
+
expires_in?: number;
|
|
215
|
+
refresh_token?: string;
|
|
216
|
+
id_token?: string;
|
|
217
|
+
scope?: string;
|
|
218
|
+
[key: string]: any;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
export interface OAuthClient {
|
|
222
|
+
authorize(params?: { scope?: string; state?: string; extra?: Record<string, string> }): OAuthAuthorizeResult;
|
|
223
|
+
callback(query: { code?: string; state?: string; error?: string; error_description?: string }, verify?: { state?: string; codeVerifier?: string }): Promise<OAuthTokens>;
|
|
224
|
+
refresh(refreshToken: string): Promise<OAuthTokens>;
|
|
225
|
+
userInfo(accessToken: string): Promise<Record<string, any>>;
|
|
226
|
+
readonly config: Readonly<OAuthOptions>;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
export function oauth(opts: OAuthOptions): OAuthClient;
|
|
230
|
+
export function generatePKCE(length?: number): { codeVerifier: string; codeChallenge: string };
|
|
231
|
+
export function generateState(bytes?: number): string;
|
|
232
|
+
export const OAUTH_PROVIDERS: Record<string, OAuthProviderPreset>;
|
|
233
|
+
|
|
234
|
+
// --- Authorization -----------------------------------------------
|
|
235
|
+
|
|
236
|
+
export function authorize(...roles: (string | string[])[]): MiddlewareFunction;
|
|
237
|
+
export function can(...permissions: (string | string[])[]): MiddlewareFunction;
|
|
238
|
+
export function canAny(...permissions: (string | string[])[]): MiddlewareFunction;
|
|
239
|
+
|
|
240
|
+
export declare class Policy {
|
|
241
|
+
before?(user: any, action: string, resource?: any): boolean | undefined;
|
|
242
|
+
check(action: string, user: any, resource?: any): boolean | Promise<boolean>;
|
|
243
|
+
[action: string]: any;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
export function gate(
|
|
247
|
+
policy: Policy,
|
|
248
|
+
action: string,
|
|
249
|
+
getResource?: (req: Request) => any | Promise<any>,
|
|
250
|
+
): MiddlewareFunction;
|
|
251
|
+
|
|
252
|
+
export function attachUserHelpers(): MiddlewareFunction;
|
|
253
|
+
|
|
254
|
+
// --- Two-Factor Authentication -----------------------------------
|
|
255
|
+
|
|
256
|
+
export interface TwoFactorSecret {
|
|
257
|
+
raw: Buffer;
|
|
258
|
+
base32: string;
|
|
259
|
+
hex: string;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
export interface TOTPOptions {
|
|
263
|
+
period?: number;
|
|
264
|
+
digits?: number;
|
|
265
|
+
algorithm?: 'sha1' | 'sha256' | 'sha512';
|
|
266
|
+
time?: number;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
export interface TOTPVerifyOptions extends TOTPOptions {
|
|
270
|
+
window?: number;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
export interface TOTPVerifyResult {
|
|
274
|
+
valid: boolean;
|
|
275
|
+
delta: number | null;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
export interface OtpauthURIOptions {
|
|
279
|
+
secret: string | Buffer;
|
|
280
|
+
issuer: string;
|
|
281
|
+
account: string;
|
|
282
|
+
period?: number;
|
|
283
|
+
digits?: number;
|
|
284
|
+
algorithm?: 'sha1' | 'sha256' | 'sha512';
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
export interface BackupCodesResult {
|
|
288
|
+
codes: string[];
|
|
289
|
+
hashes: string[];
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
export interface BackupCodeVerifyResult {
|
|
293
|
+
valid: boolean;
|
|
294
|
+
index: number | null;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
export interface Require2FAOptions {
|
|
298
|
+
sessionKey?: string;
|
|
299
|
+
errorMessage?: string;
|
|
300
|
+
statusCode?: number;
|
|
301
|
+
isEnabled?: (req: Request) => boolean | Promise<boolean>;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
export interface VerifyTOTPMiddlewareOptions {
|
|
305
|
+
getSecret: (req: Request) => string | Buffer | Promise<string | Buffer>;
|
|
306
|
+
codeField?: string;
|
|
307
|
+
sessionKey?: string;
|
|
308
|
+
maxAttempts?: number;
|
|
309
|
+
lockoutMs?: number;
|
|
310
|
+
window?: number;
|
|
311
|
+
period?: number;
|
|
312
|
+
algorithm?: 'sha1' | 'sha256' | 'sha512';
|
|
313
|
+
digits?: number;
|
|
314
|
+
replayStore?: ReplayStore;
|
|
315
|
+
getUserId?: (req: Request) => string | Promise<string>;
|
|
316
|
+
onSuccess?: (req: Request, res: Response) => void;
|
|
317
|
+
onFailure?: (req: Request, res: Response, attemptsLeft: number) => void;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
export interface ReplayStore {
|
|
321
|
+
get(userId: string): Promise<number | null>;
|
|
322
|
+
set(userId: string, counter: number, ttlMs: number): Promise<void>;
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
export declare class InMemoryReplayStore implements ReplayStore {
|
|
326
|
+
constructor();
|
|
327
|
+
get(userId: string): Promise<number | null>;
|
|
328
|
+
set(userId: string, counter: number, ttlMs: number): Promise<void>;
|
|
329
|
+
clear(): void;
|
|
330
|
+
destroy(): void;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
export interface Verify2FAOptions {
|
|
334
|
+
getSecret: (req: Request) => string | Buffer | Promise<string | Buffer>;
|
|
335
|
+
getBackupHashes?: (req: Request) => string[] | Promise<string[]>;
|
|
336
|
+
onBackupUsed?: (req: Request, index: number) => void | Promise<void>;
|
|
337
|
+
getCredential?: (req: Request, credId: string) => any | Promise<any>;
|
|
338
|
+
updateCredentialCounter?: (req: Request, credId: string, newCounter: number) => void | Promise<void>;
|
|
339
|
+
expectedOrigin?: string;
|
|
340
|
+
expectedRPID?: string;
|
|
341
|
+
codeField?: string;
|
|
342
|
+
backupField?: string;
|
|
343
|
+
sessionKey?: string;
|
|
344
|
+
maxAttempts?: number;
|
|
345
|
+
lockoutMs?: number;
|
|
346
|
+
window?: number;
|
|
347
|
+
period?: number;
|
|
348
|
+
algorithm?: 'sha1' | 'sha256' | 'sha512';
|
|
349
|
+
digits?: number;
|
|
350
|
+
replayStore?: ReplayStore;
|
|
351
|
+
getUserId?: (req: Request) => string | Promise<string>;
|
|
352
|
+
onSuccess?: (req: Request, res: Response, method: 'totp' | 'backup' | 'webauthn') => void;
|
|
353
|
+
onFailure?: (req: Request, res: Response, attemptsLeft: number) => void;
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
export interface TwoFactor {
|
|
357
|
+
generateSecret(bytes?: number): TwoFactorSecret;
|
|
358
|
+
generateTOTP(secret: string | Buffer, opts?: TOTPOptions): string;
|
|
359
|
+
verifyTOTP(token: string, secret: string | Buffer, opts?: TOTPVerifyOptions): TOTPVerifyResult;
|
|
360
|
+
otpauthURI(opts: OtpauthURIOptions): string;
|
|
361
|
+
generateBackupCodes(count?: number, bytes?: number): BackupCodesResult;
|
|
362
|
+
verifyBackupCode(code: string, hashes: string[]): BackupCodeVerifyResult;
|
|
363
|
+
require2FA(opts?: Require2FAOptions): MiddlewareFunction;
|
|
364
|
+
verifyTOTPMiddleware(opts: VerifyTOTPMiddlewareOptions): MiddlewareFunction;
|
|
365
|
+
verify2FA(opts: Verify2FAOptions): MiddlewareFunction;
|
|
366
|
+
InMemoryReplayStore: typeof InMemoryReplayStore;
|
|
367
|
+
DEFAULT_PERIOD: number;
|
|
368
|
+
DEFAULT_DIGITS: number;
|
|
369
|
+
DEFAULT_ALGORITHM: string;
|
|
370
|
+
DEFAULT_WINDOW: number;
|
|
371
|
+
SUPPORTED_TOTP_ALGORITHMS: string[];
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
export const twoFactor: TwoFactor;
|
|
375
|
+
|
|
376
|
+
// --- WebAuthn / Passkeys -----------------------------------------
|
|
377
|
+
|
|
378
|
+
export interface WebAuthnRegistrationOptions {
|
|
379
|
+
rpName: string;
|
|
380
|
+
rpId: string;
|
|
381
|
+
userId: string;
|
|
382
|
+
userName: string;
|
|
383
|
+
userDisplayName?: string;
|
|
384
|
+
challenge?: Buffer;
|
|
385
|
+
challengeSize?: number;
|
|
386
|
+
attestation?: 'none' | 'direct' | 'indirect';
|
|
387
|
+
authenticatorSelection?: {
|
|
388
|
+
authenticatorAttachment?: 'platform' | 'cross-platform';
|
|
389
|
+
residentKey?: 'discouraged' | 'preferred' | 'required';
|
|
390
|
+
requireResidentKey?: boolean;
|
|
391
|
+
userVerification?: 'discouraged' | 'preferred' | 'required';
|
|
392
|
+
};
|
|
393
|
+
excludeCredentials?: Array<{ id: string | Buffer; type?: string; transports?: string[] }>;
|
|
394
|
+
timeout?: number;
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
export interface WebAuthnRegistrationResult {
|
|
398
|
+
verified: boolean;
|
|
399
|
+
credential: {
|
|
400
|
+
id: string;
|
|
401
|
+
publicKey: Buffer;
|
|
402
|
+
counter: number;
|
|
403
|
+
type: string;
|
|
404
|
+
transports?: string[];
|
|
405
|
+
};
|
|
406
|
+
authData: {
|
|
407
|
+
rpIdHash: Buffer;
|
|
408
|
+
flags: number;
|
|
409
|
+
signCount: number;
|
|
410
|
+
aaguid: Buffer;
|
|
411
|
+
credentialId: Buffer;
|
|
412
|
+
};
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
export interface WebAuthnVerifyRegistrationOptions {
|
|
416
|
+
response: any;
|
|
417
|
+
expectedChallenge: string;
|
|
418
|
+
expectedOrigin: string;
|
|
419
|
+
expectedRPID: string;
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
export interface WebAuthnAuthenticationOptions {
|
|
423
|
+
rpId: string;
|
|
424
|
+
challenge?: Buffer;
|
|
425
|
+
challengeSize?: number;
|
|
426
|
+
allowCredentials?: Array<{ id: string | Buffer; type?: string; transports?: string[] }>;
|
|
427
|
+
userVerification?: 'discouraged' | 'preferred' | 'required';
|
|
428
|
+
timeout?: number;
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
export interface WebAuthnAuthenticationResult {
|
|
432
|
+
verified: boolean;
|
|
433
|
+
authData: {
|
|
434
|
+
rpIdHash: Buffer;
|
|
435
|
+
flags: number;
|
|
436
|
+
signCount: number;
|
|
437
|
+
};
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
export interface WebAuthnVerifyAuthenticationOptions {
|
|
441
|
+
response: any;
|
|
442
|
+
expectedChallenge: string;
|
|
443
|
+
expectedOrigin: string;
|
|
444
|
+
expectedRPID: string;
|
|
445
|
+
credential: { publicKey: Buffer; counter: number; id?: string };
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
export interface WebAuthn {
|
|
449
|
+
generateRegistrationOptions(opts: WebAuthnRegistrationOptions): any;
|
|
450
|
+
verifyRegistration(opts: WebAuthnVerifyRegistrationOptions): Promise<WebAuthnRegistrationResult>;
|
|
451
|
+
generateAuthenticationOptions(opts: WebAuthnAuthenticationOptions): any;
|
|
452
|
+
verifyAuthentication(opts: WebAuthnVerifyAuthenticationOptions): Promise<WebAuthnAuthenticationResult>;
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
export const webauthn: WebAuthn;
|
|
456
|
+
|
|
457
|
+
// --- Trusted Device / Remember Me --------------------------------
|
|
458
|
+
|
|
459
|
+
export interface TrustedDeviceIssueOptions {
|
|
460
|
+
secret: string;
|
|
461
|
+
maxAge?: number;
|
|
462
|
+
cookieName?: string;
|
|
463
|
+
fingerprint?: (req: Request) => string | Promise<string>;
|
|
464
|
+
getUserId?: (req: Request) => string | Promise<string>;
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
export interface TrustedDeviceVerifyOptions {
|
|
468
|
+
secret: string;
|
|
469
|
+
previousSecrets?: string | string[];
|
|
470
|
+
cookieName?: string;
|
|
471
|
+
fingerprint?: (req: Request) => string | Promise<string>;
|
|
472
|
+
getUserId?: (req: Request) => string | Promise<string>;
|
|
473
|
+
checkIP?: boolean;
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
export interface TrustedDeviceRevokeOptions {
|
|
477
|
+
cookieName?: string;
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
export interface TrustedDevice {
|
|
481
|
+
issue(opts: TrustedDeviceIssueOptions): MiddlewareFunction;
|
|
482
|
+
verify(opts: TrustedDeviceVerifyOptions): (req: Request) => Promise<boolean>;
|
|
483
|
+
revoke(opts?: TrustedDeviceRevokeOptions): MiddlewareFunction;
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
export const trustedDevice: TrustedDevice;
|
|
487
|
+
|
|
488
|
+
// --- 2FA Enrollment Flow -----------------------------------------
|
|
489
|
+
|
|
490
|
+
export interface EnrollmentOptions {
|
|
491
|
+
saveSecret: (req: Request, secret: string, backupHashes: string[]) => Promise<void>;
|
|
492
|
+
removeSecret: (req: Request) => Promise<void>;
|
|
493
|
+
issuer?: string;
|
|
494
|
+
getAccount?: (req: Request) => string | Promise<string>;
|
|
495
|
+
sessionKey?: string;
|
|
496
|
+
ttl?: number;
|
|
497
|
+
backupCount?: number;
|
|
498
|
+
window?: number;
|
|
499
|
+
period?: number;
|
|
500
|
+
algorithm?: 'sha1' | 'sha256' | 'sha512';
|
|
501
|
+
digits?: number;
|
|
502
|
+
isEnabled?: (req: Request) => boolean | Promise<boolean>;
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
export interface EnrollmentDisableOptions {
|
|
506
|
+
confirm?: (req: Request) => boolean | Promise<boolean>;
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
export interface EnrollmentVerifyOptions {
|
|
510
|
+
codeField?: string;
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
export interface EnrollmentFlow {
|
|
514
|
+
start(): MiddlewareFunction;
|
|
515
|
+
verify(opts?: EnrollmentVerifyOptions): MiddlewareFunction;
|
|
516
|
+
complete(): MiddlewareFunction;
|
|
517
|
+
disable(opts?: EnrollmentDisableOptions): MiddlewareFunction;
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
export function enrollment(opts: EnrollmentOptions): EnrollmentFlow;
|