@technomoron/api-server-base 1.1.12 → 2.0.0-beta.1
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/dist/cjs/api-server-base.cjs +199 -74
- package/dist/cjs/api-server-base.d.ts +70 -29
- package/dist/cjs/auth-api/auth-module.d.ts +96 -0
- package/dist/cjs/auth-api/auth-module.js +1032 -0
- package/dist/cjs/auth-api/compat-auth-storage.d.ts +55 -0
- package/dist/cjs/auth-api/compat-auth-storage.js +116 -0
- package/dist/cjs/auth-api/mem-auth-store.d.ts +66 -0
- package/dist/cjs/auth-api/mem-auth-store.js +135 -0
- package/dist/cjs/{auth-module.d.ts → auth-api/module.d.ts} +7 -7
- package/dist/cjs/{auth-module.cjs → auth-api/module.js} +1 -1
- package/dist/cjs/auth-api/sql-auth-store.d.ts +75 -0
- package/dist/cjs/auth-api/sql-auth-store.js +166 -0
- package/dist/cjs/auth-api/storage.d.ts +36 -0
- package/dist/cjs/{auth-storage.cjs → auth-api/storage.js} +2 -2
- package/dist/cjs/auth-api/types.d.ts +29 -0
- package/dist/cjs/auth-api/types.js +2 -0
- package/dist/cjs/index.cjs +41 -7
- package/dist/cjs/index.d.ts +29 -5
- package/dist/cjs/oauth/base.d.ts +10 -0
- package/dist/cjs/oauth/base.js +6 -0
- package/dist/cjs/oauth/memory.d.ts +16 -0
- package/dist/cjs/oauth/memory.js +99 -0
- package/dist/cjs/oauth/models.d.ts +45 -0
- package/dist/cjs/oauth/models.js +58 -0
- package/dist/cjs/oauth/sequelize.d.ts +68 -0
- package/dist/cjs/oauth/sequelize.js +210 -0
- package/dist/cjs/oauth/types.d.ts +50 -0
- package/dist/cjs/oauth/types.js +3 -0
- package/dist/cjs/passkey/base.d.ts +15 -0
- package/dist/cjs/passkey/base.js +6 -0
- package/dist/cjs/passkey/memory.d.ts +26 -0
- package/dist/cjs/passkey/memory.js +82 -0
- package/dist/cjs/passkey/models.d.ts +25 -0
- package/dist/cjs/passkey/models.js +115 -0
- package/dist/cjs/passkey/sequelize.d.ts +54 -0
- package/dist/cjs/passkey/sequelize.js +211 -0
- package/dist/cjs/passkey/service.d.ts +17 -0
- package/dist/cjs/passkey/service.js +221 -0
- package/dist/cjs/passkey/types.d.ts +75 -0
- package/dist/cjs/passkey/types.js +2 -0
- package/dist/cjs/token/base.d.ts +38 -0
- package/dist/cjs/token/base.js +114 -0
- package/dist/cjs/token/memory.d.ts +19 -0
- package/dist/cjs/token/memory.js +149 -0
- package/dist/cjs/token/sequelize.d.ts +58 -0
- package/dist/cjs/token/sequelize.js +404 -0
- package/dist/cjs/token/types.d.ts +27 -0
- package/dist/cjs/token/types.js +2 -0
- package/dist/cjs/user/base.d.ts +26 -0
- package/dist/cjs/user/base.js +44 -0
- package/dist/cjs/user/memory.d.ts +35 -0
- package/dist/cjs/user/memory.js +173 -0
- package/dist/cjs/user/sequelize.d.ts +41 -0
- package/dist/cjs/user/sequelize.js +182 -0
- package/dist/cjs/user/types.d.ts +11 -0
- package/dist/cjs/user/types.js +2 -0
- package/dist/esm/api-server-base.d.ts +70 -29
- package/dist/esm/api-server-base.js +197 -72
- package/dist/esm/auth-api/auth-module.d.ts +96 -0
- package/dist/esm/auth-api/auth-module.js +1030 -0
- package/dist/esm/auth-api/compat-auth-storage.d.ts +55 -0
- package/dist/esm/auth-api/compat-auth-storage.js +112 -0
- package/dist/esm/auth-api/mem-auth-store.d.ts +66 -0
- package/dist/esm/auth-api/mem-auth-store.js +131 -0
- package/dist/esm/{auth-module.d.ts → auth-api/module.d.ts} +7 -7
- package/dist/esm/{auth-module.js → auth-api/module.js} +1 -1
- package/dist/esm/auth-api/sql-auth-store.d.ts +75 -0
- package/dist/esm/auth-api/sql-auth-store.js +162 -0
- package/dist/esm/auth-api/storage.d.ts +36 -0
- package/dist/esm/{auth-storage.js → auth-api/storage.js} +2 -2
- package/dist/esm/auth-api/types.d.ts +29 -0
- package/dist/esm/auth-api/types.js +1 -0
- package/dist/esm/index.d.ts +29 -5
- package/dist/esm/index.js +19 -2
- package/dist/esm/oauth/base.d.ts +10 -0
- package/dist/esm/oauth/base.js +2 -0
- package/dist/esm/oauth/memory.d.ts +16 -0
- package/dist/esm/oauth/memory.js +92 -0
- package/dist/esm/oauth/models.d.ts +45 -0
- package/dist/esm/oauth/models.js +51 -0
- package/dist/esm/oauth/sequelize.d.ts +68 -0
- package/dist/esm/oauth/sequelize.js +199 -0
- package/dist/esm/oauth/types.d.ts +50 -0
- package/dist/esm/oauth/types.js +2 -0
- package/dist/esm/passkey/base.d.ts +15 -0
- package/dist/esm/passkey/base.js +2 -0
- package/dist/esm/passkey/memory.d.ts +26 -0
- package/dist/esm/passkey/memory.js +78 -0
- package/dist/esm/passkey/models.d.ts +25 -0
- package/dist/esm/passkey/models.js +108 -0
- package/dist/esm/passkey/sequelize.d.ts +54 -0
- package/dist/esm/passkey/sequelize.js +207 -0
- package/dist/esm/passkey/service.d.ts +17 -0
- package/dist/esm/passkey/service.js +217 -0
- package/dist/esm/passkey/types.d.ts +75 -0
- package/dist/esm/passkey/types.js +1 -0
- package/dist/esm/token/base.d.ts +38 -0
- package/dist/esm/token/base.js +107 -0
- package/dist/esm/token/memory.d.ts +19 -0
- package/dist/esm/token/memory.js +145 -0
- package/dist/esm/token/sequelize.d.ts +58 -0
- package/dist/esm/token/sequelize.js +400 -0
- package/dist/esm/token/types.d.ts +27 -0
- package/dist/esm/token/types.js +1 -0
- package/dist/esm/user/base.d.ts +26 -0
- package/dist/esm/user/base.js +37 -0
- package/dist/esm/user/memory.d.ts +35 -0
- package/dist/esm/user/memory.js +169 -0
- package/dist/esm/user/sequelize.d.ts +41 -0
- package/dist/esm/user/sequelize.js +176 -0
- package/dist/esm/user/types.d.ts +11 -0
- package/dist/esm/user/types.js +1 -0
- package/package.json +11 -3
- package/dist/cjs/auth-storage.d.ts +0 -133
- package/dist/esm/auth-storage.d.ts +0 -133
|
@@ -4,13 +4,34 @@
|
|
|
4
4
|
* This source code is licensed under the MIT license found in the
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*/
|
|
7
|
+
import { randomUUID } from 'node:crypto';
|
|
7
8
|
import cookieParser from 'cookie-parser';
|
|
8
9
|
import cors from 'cors';
|
|
9
10
|
import express from 'express';
|
|
10
|
-
import jwt from 'jsonwebtoken';
|
|
11
11
|
import multer from 'multer';
|
|
12
|
-
import { nullAuthModule } from './auth-module.js';
|
|
13
|
-
import { nullAuthStorage } from './auth-storage.js';
|
|
12
|
+
import { nullAuthModule } from './auth-api/module.js';
|
|
13
|
+
import { nullAuthStorage } from './auth-api/storage.js';
|
|
14
|
+
import { TokenStore } from './token/base.js';
|
|
15
|
+
class JwtHelperStore extends TokenStore {
|
|
16
|
+
async save() {
|
|
17
|
+
throw new Error('Token store is not configured');
|
|
18
|
+
}
|
|
19
|
+
async get() {
|
|
20
|
+
throw new Error('Token store is not configured');
|
|
21
|
+
}
|
|
22
|
+
async delete() {
|
|
23
|
+
throw new Error('Token store is not configured');
|
|
24
|
+
}
|
|
25
|
+
async update() {
|
|
26
|
+
throw new Error('Token store is not configured');
|
|
27
|
+
}
|
|
28
|
+
async list() {
|
|
29
|
+
return [];
|
|
30
|
+
}
|
|
31
|
+
async close() {
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
14
35
|
export { ApiModule } from './api-module.js';
|
|
15
36
|
function guess_exception_text(error, defMsg = 'Unknown Error') {
|
|
16
37
|
const msg = [];
|
|
@@ -322,23 +343,45 @@ function fillConfig(config) {
|
|
|
322
343
|
authApi: config.authApi ?? false,
|
|
323
344
|
devMode: config.devMode ?? false,
|
|
324
345
|
hydrateGetBody: config.hydrateGetBody ?? true,
|
|
325
|
-
validateTokens: config.validateTokens ?? false
|
|
346
|
+
validateTokens: config.validateTokens ?? false,
|
|
347
|
+
apiVersion: config.apiVersion ?? '',
|
|
348
|
+
minClientVersion: config.minClientVersion ?? '',
|
|
349
|
+
tokenStore: config.tokenStore,
|
|
350
|
+
authStores: config.authStores
|
|
326
351
|
};
|
|
327
352
|
}
|
|
328
353
|
export class ApiServer {
|
|
329
354
|
constructor(config = {}) {
|
|
330
355
|
this.currReq = null;
|
|
331
356
|
this.apiNotFoundHandler = null;
|
|
357
|
+
this.tokenStoreAdapter = null;
|
|
358
|
+
this.userStoreAdapter = null;
|
|
359
|
+
this.passkeyServiceAdapter = null;
|
|
360
|
+
this.oauthStoreAdapter = null;
|
|
361
|
+
this.canImpersonateAdapter = null;
|
|
332
362
|
this.config = fillConfig(config);
|
|
333
363
|
this.apiBasePath = this.normalizeApiBasePath(this.config.apiBasePath);
|
|
364
|
+
this.startedAt = Date.now();
|
|
334
365
|
this.storageAdapter = nullAuthStorage;
|
|
335
366
|
this.moduleAdapter = nullAuthModule;
|
|
367
|
+
this.jwtHelper = new JwtHelperStore();
|
|
368
|
+
this.tokenStoreAdapter = this.config.tokenStore ?? null;
|
|
369
|
+
if (this.config.authStores) {
|
|
370
|
+
const { userStore, tokenStore, passkeyService, oauthStore, canImpersonate } = this.config.authStores;
|
|
371
|
+
this.userStoreAdapter = userStore;
|
|
372
|
+
this.tokenStoreAdapter = tokenStore;
|
|
373
|
+
this.passkeyServiceAdapter = passkeyService ?? null;
|
|
374
|
+
this.oauthStoreAdapter = oauthStore ?? null;
|
|
375
|
+
this.canImpersonateAdapter = canImpersonate ?? null;
|
|
376
|
+
this.storageAdapter = this;
|
|
377
|
+
}
|
|
336
378
|
this.app = express();
|
|
337
379
|
if (config.uploadPath) {
|
|
338
380
|
const upload = multer({ dest: config.uploadPath });
|
|
339
381
|
this.app.use(upload.any());
|
|
340
382
|
}
|
|
341
383
|
this.middlewares();
|
|
384
|
+
this.installPingHandler();
|
|
342
385
|
// addSwaggerUi(this.app);
|
|
343
386
|
this.installApiNotFoundHandler();
|
|
344
387
|
}
|
|
@@ -368,72 +411,143 @@ export class ApiServer {
|
|
|
368
411
|
getAuthModule() {
|
|
369
412
|
return this.moduleAdapter;
|
|
370
413
|
}
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
return {
|
|
377
|
-
success: true,
|
|
378
|
-
token
|
|
379
|
-
};
|
|
414
|
+
setTokenStore(store) {
|
|
415
|
+
this.tokenStoreAdapter = store;
|
|
416
|
+
// If using direct stores, expose self as the auth storage.
|
|
417
|
+
if (this.userStoreAdapter) {
|
|
418
|
+
this.storageAdapter = this;
|
|
380
419
|
}
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
420
|
+
return this;
|
|
421
|
+
}
|
|
422
|
+
getTokenStore() {
|
|
423
|
+
return this.tokenStoreAdapter;
|
|
424
|
+
}
|
|
425
|
+
ensureUserStore() {
|
|
426
|
+
if (!this.userStoreAdapter) {
|
|
427
|
+
throw new Error('User store is not configured');
|
|
386
428
|
}
|
|
429
|
+
return this.userStoreAdapter;
|
|
387
430
|
}
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
const data = jwt.verify(token, secret, options);
|
|
392
|
-
return {
|
|
393
|
-
success: true,
|
|
394
|
-
data
|
|
395
|
-
};
|
|
431
|
+
ensureTokenStore() {
|
|
432
|
+
if (!this.tokenStoreAdapter) {
|
|
433
|
+
throw new Error('Token store is not configured');
|
|
396
434
|
}
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
error: 'Token expired'
|
|
403
|
-
};
|
|
404
|
-
}
|
|
405
|
-
else {
|
|
406
|
-
return {
|
|
407
|
-
success: false,
|
|
408
|
-
expired: false,
|
|
409
|
-
error: error instanceof Error ? error.message : String(error)
|
|
410
|
-
};
|
|
411
|
-
}
|
|
435
|
+
return this.tokenStoreAdapter;
|
|
436
|
+
}
|
|
437
|
+
ensurePasskeyService() {
|
|
438
|
+
if (!this.passkeyServiceAdapter) {
|
|
439
|
+
throw new Error('Passkey service is not configured');
|
|
412
440
|
}
|
|
441
|
+
return this.passkeyServiceAdapter;
|
|
413
442
|
}
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
const data = jwt.decode(token, options);
|
|
418
|
-
// jwt.decode returns null for invalid tokens rather than throwing
|
|
419
|
-
if (data === null) {
|
|
420
|
-
return {
|
|
421
|
-
success: false,
|
|
422
|
-
error: 'Invalid token format'
|
|
423
|
-
};
|
|
424
|
-
}
|
|
425
|
-
return {
|
|
426
|
-
success: true,
|
|
427
|
-
data
|
|
428
|
-
};
|
|
443
|
+
ensureOAuthStore() {
|
|
444
|
+
if (!this.oauthStoreAdapter) {
|
|
445
|
+
throw new Error('OAuth store is not configured');
|
|
429
446
|
}
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
447
|
+
return this.oauthStoreAdapter;
|
|
448
|
+
}
|
|
449
|
+
// AuthStorage-compatible helpers (used by AuthModule)
|
|
450
|
+
async getUser(identifier) {
|
|
451
|
+
return this.userStoreAdapter ? this.userStoreAdapter.findUser(identifier) : null;
|
|
452
|
+
}
|
|
453
|
+
getUserPasswordHash(user) {
|
|
454
|
+
return this.ensureUserStore().getPasswordHash(user) ?? '';
|
|
455
|
+
}
|
|
456
|
+
getUserId(user) {
|
|
457
|
+
return this.ensureUserStore().getUserId(user);
|
|
458
|
+
}
|
|
459
|
+
filterUser(user) {
|
|
460
|
+
return this.ensureUserStore().toPublic(user);
|
|
461
|
+
}
|
|
462
|
+
async verifyPassword(password, hash) {
|
|
463
|
+
return this.ensureUserStore().verifyPassword(password, hash);
|
|
464
|
+
}
|
|
465
|
+
async storeToken(data) {
|
|
466
|
+
if (this.tokenStoreAdapter) {
|
|
467
|
+
return this.tokenStoreAdapter.save(data);
|
|
468
|
+
}
|
|
469
|
+
if (typeof this.storageAdapter.storeToken === 'function') {
|
|
470
|
+
return this.storageAdapter.storeToken(data);
|
|
471
|
+
}
|
|
472
|
+
throw new Error('Token store is not configured');
|
|
473
|
+
}
|
|
474
|
+
async getToken(query, opts) {
|
|
475
|
+
const normalized = {
|
|
476
|
+
...query,
|
|
477
|
+
userId: query.userId !== undefined && query.userId !== null ? String(query.userId) : undefined,
|
|
478
|
+
ruid: query.ruid !== undefined && query.ruid !== null ? String(query.ruid) : undefined
|
|
479
|
+
};
|
|
480
|
+
if (this.tokenStoreAdapter) {
|
|
481
|
+
return this.tokenStoreAdapter.get(normalized, opts);
|
|
482
|
+
}
|
|
483
|
+
if (typeof this.storageAdapter.getToken === 'function') {
|
|
484
|
+
return this.storageAdapter.getToken(normalized, opts);
|
|
485
|
+
}
|
|
486
|
+
return null;
|
|
487
|
+
}
|
|
488
|
+
async deleteToken(query) {
|
|
489
|
+
const normalized = {
|
|
490
|
+
...query,
|
|
491
|
+
userId: query.userId !== undefined && query.userId !== null ? String(query.userId) : undefined,
|
|
492
|
+
ruid: query.ruid !== undefined && query.ruid !== null ? String(query.ruid) : undefined
|
|
493
|
+
};
|
|
494
|
+
if (this.tokenStoreAdapter) {
|
|
495
|
+
return this.tokenStoreAdapter.delete(normalized);
|
|
496
|
+
}
|
|
497
|
+
if (typeof this.storageAdapter.deleteToken === 'function') {
|
|
498
|
+
return this.storageAdapter.deleteToken(normalized);
|
|
499
|
+
}
|
|
500
|
+
return 0;
|
|
501
|
+
}
|
|
502
|
+
async createPasskeyChallenge(params) {
|
|
503
|
+
return this.ensurePasskeyService().createChallenge(params);
|
|
504
|
+
}
|
|
505
|
+
async verifyPasskeyResponse(params) {
|
|
506
|
+
return this.ensurePasskeyService().verifyResponse(params);
|
|
507
|
+
}
|
|
508
|
+
async getClient(clientId) {
|
|
509
|
+
return this.oauthStoreAdapter ? this.oauthStoreAdapter.getClient(clientId) : null;
|
|
510
|
+
}
|
|
511
|
+
async verifyClientSecret(client, clientSecret) {
|
|
512
|
+
return this.ensureOAuthStore().verifyClientSecret(client.clientId, clientSecret);
|
|
513
|
+
}
|
|
514
|
+
async createAuthCode(request) {
|
|
515
|
+
const expiresAt = new Date(Date.now() + (request.expiresInSeconds ?? 300) * 1000);
|
|
516
|
+
const code = request.code ?? randomUUID();
|
|
517
|
+
await this.ensureOAuthStore().createAuthCode({ ...request, code, expiresAt });
|
|
518
|
+
return {
|
|
519
|
+
code,
|
|
520
|
+
clientId: request.clientId,
|
|
521
|
+
userId: request.userId,
|
|
522
|
+
redirectUri: request.redirectUri,
|
|
523
|
+
scope: request.scope ?? [],
|
|
524
|
+
codeChallenge: request.codeChallenge,
|
|
525
|
+
codeChallengeMethod: request.codeChallengeMethod,
|
|
526
|
+
expiresAt,
|
|
527
|
+
metadata: request.metadata
|
|
528
|
+
};
|
|
529
|
+
}
|
|
530
|
+
async consumeAuthCode(code, clientId) {
|
|
531
|
+
const consumed = await this.ensureOAuthStore().consumeAuthCode(code);
|
|
532
|
+
if (!consumed || consumed.clientId !== clientId) {
|
|
533
|
+
return null;
|
|
534
|
+
}
|
|
535
|
+
return consumed;
|
|
536
|
+
}
|
|
537
|
+
async canImpersonate(params) {
|
|
538
|
+
if (this.canImpersonateAdapter) {
|
|
539
|
+
return !!(await this.canImpersonateAdapter(params));
|
|
436
540
|
}
|
|
541
|
+
return params.realUserId === params.effectiveUserId;
|
|
542
|
+
}
|
|
543
|
+
jwtSign(payload, secret, expiresInSeconds, options) {
|
|
544
|
+
return (this.tokenStoreAdapter ?? this.jwtHelper).jwtSign(payload, secret, expiresInSeconds, options);
|
|
545
|
+
}
|
|
546
|
+
jwtVerify(token, secret, options) {
|
|
547
|
+
return (this.tokenStoreAdapter ?? this.jwtHelper).jwtVerify(token, secret, options);
|
|
548
|
+
}
|
|
549
|
+
jwtDecode(token, options) {
|
|
550
|
+
return (this.tokenStoreAdapter ?? this.jwtHelper).jwtDecode(token, options);
|
|
437
551
|
}
|
|
438
552
|
async getApiKey(token) {
|
|
439
553
|
void token;
|
|
@@ -451,16 +565,13 @@ export class ApiServer {
|
|
|
451
565
|
return this.storageAdapter.verifyPassword(params.password, hash);
|
|
452
566
|
}
|
|
453
567
|
async updateToken(updates) {
|
|
454
|
-
if (
|
|
455
|
-
return
|
|
568
|
+
if (this.tokenStoreAdapter) {
|
|
569
|
+
return this.tokenStoreAdapter.update(updates);
|
|
456
570
|
}
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
clientId: updates.clientId,
|
|
462
|
-
scope: updates.scope
|
|
463
|
-
});
|
|
571
|
+
if (typeof this.storageAdapter.updateToken === 'function') {
|
|
572
|
+
return this.storageAdapter.updateToken(updates);
|
|
573
|
+
}
|
|
574
|
+
return false;
|
|
464
575
|
}
|
|
465
576
|
guessExceptionText(error, defMsg = 'Unkown Error') {
|
|
466
577
|
return guess_exception_text(error, defMsg);
|
|
@@ -489,6 +600,20 @@ export class ApiServer {
|
|
|
489
600
|
};
|
|
490
601
|
this.app.use(cors(corsOptions));
|
|
491
602
|
}
|
|
603
|
+
installPingHandler() {
|
|
604
|
+
const path = `${this.apiBasePath}/v1/ping`;
|
|
605
|
+
this.app.get(path, (_req, res) => {
|
|
606
|
+
const payload = {
|
|
607
|
+
status: 'ok',
|
|
608
|
+
apiVersion: this.config.apiVersion ?? '',
|
|
609
|
+
minClientVersion: this.config.minClientVersion ?? '',
|
|
610
|
+
uptimeSec: process.uptime(),
|
|
611
|
+
startedAt: this.startedAt,
|
|
612
|
+
timestamp: new Date().toISOString()
|
|
613
|
+
};
|
|
614
|
+
res.status(200).json({ code: 200, message: 'Success', data: payload });
|
|
615
|
+
});
|
|
616
|
+
}
|
|
492
617
|
normalizeApiBasePath(path) {
|
|
493
618
|
if (!path || typeof path !== 'string') {
|
|
494
619
|
return '/api';
|
|
@@ -660,7 +785,7 @@ export class ApiServer {
|
|
|
660
785
|
return this.config.validateTokens || authType === 'strict';
|
|
661
786
|
}
|
|
662
787
|
async assertStoredAccessToken(apiReq, token, tokenData) {
|
|
663
|
-
const userId = this.extractTokenUserId(tokenData);
|
|
788
|
+
const userId = String(this.extractTokenUserId(tokenData));
|
|
664
789
|
const stored = await this.storageAdapter.getToken({
|
|
665
790
|
accessToken: token,
|
|
666
791
|
userId
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { type ApiRequest, type ApiRoute, type ApiServer } from '../api-server-base.js';
|
|
2
|
+
import { BaseAuthModule, type AuthProviderModule } from './module.js';
|
|
3
|
+
import type { AuthIdentifier, AuthStorage } from './types.js';
|
|
4
|
+
import type { OAuthCallbackParams, OAuthCallbackResult, OAuthStartParams, OAuthStartResult } from '../oauth/types.js';
|
|
5
|
+
import type { TokenPair, Token } from '../token/types.js';
|
|
6
|
+
interface CanImpersonateContext<UserEntity> {
|
|
7
|
+
apiReq: ApiRequest;
|
|
8
|
+
realUser: UserEntity;
|
|
9
|
+
realUserId: AuthIdentifier;
|
|
10
|
+
targetUser: UserEntity;
|
|
11
|
+
effectiveUserId: AuthIdentifier;
|
|
12
|
+
}
|
|
13
|
+
interface AuthModuleOptions<UserEntity> {
|
|
14
|
+
namespace?: string;
|
|
15
|
+
defaultDomain?: string;
|
|
16
|
+
canImpersonate?: (context: CanImpersonateContext<UserEntity>) => Promise<boolean> | boolean;
|
|
17
|
+
}
|
|
18
|
+
type TokenMetadata = Partial<Token> & {
|
|
19
|
+
sessionCookie?: boolean;
|
|
20
|
+
};
|
|
21
|
+
interface TokenIssueOptions extends TokenMetadata {
|
|
22
|
+
expires?: Date;
|
|
23
|
+
sessionCookie?: boolean;
|
|
24
|
+
}
|
|
25
|
+
interface NormalizedTokenMetadata extends TokenMetadata {
|
|
26
|
+
domain: string;
|
|
27
|
+
fingerprint: string;
|
|
28
|
+
label: string;
|
|
29
|
+
browser: string;
|
|
30
|
+
device: string;
|
|
31
|
+
ip: string;
|
|
32
|
+
os: string;
|
|
33
|
+
}
|
|
34
|
+
type TokenClaims = TokenMetadata & {
|
|
35
|
+
uid: string;
|
|
36
|
+
exp?: number;
|
|
37
|
+
iat?: number;
|
|
38
|
+
};
|
|
39
|
+
type AuthCapableServer<PublicUser> = ApiServer & {
|
|
40
|
+
initiateOAuth?: (params: OAuthStartParams) => Promise<OAuthStartResult>;
|
|
41
|
+
completeOAuth?: (params: OAuthCallbackParams) => Promise<OAuthCallbackResult<PublicUser>>;
|
|
42
|
+
};
|
|
43
|
+
export default class AuthModule<UserEntity, PublicUser> extends BaseAuthModule<UserEntity> implements AuthProviderModule<UserEntity> {
|
|
44
|
+
static defaultNamespace: string;
|
|
45
|
+
server: AuthCapableServer<PublicUser>;
|
|
46
|
+
private readonly defaultDomain?;
|
|
47
|
+
private readonly canImpersonateHook?;
|
|
48
|
+
constructor(options?: AuthModuleOptions<UserEntity>);
|
|
49
|
+
protected get storage(): AuthStorage<UserEntity, PublicUser>;
|
|
50
|
+
protected canImpersonate(apiReq: ApiRequest, realUser: UserEntity, targetUser: UserEntity): Promise<boolean>;
|
|
51
|
+
protected ensureImpersonationAllowed(apiReq: ApiRequest, realUser: UserEntity, targetUser: UserEntity): Promise<void>;
|
|
52
|
+
protected buildTokenPayload(user: UserEntity, metadata?: TokenMetadata): TokenClaims;
|
|
53
|
+
protected buildTokenMetadata(metadata?: TokenMetadata): NormalizedTokenMetadata;
|
|
54
|
+
protected enrichTokenMetadata(apiReq: ApiRequest, metadata?: TokenMetadata): TokenMetadata;
|
|
55
|
+
private sessionRefreshTtlSeconds;
|
|
56
|
+
private normalizeRefreshTtlSeconds;
|
|
57
|
+
private resolveSessionPreferences;
|
|
58
|
+
private mergeSessionPreferences;
|
|
59
|
+
private sessionPrefsFromRecord;
|
|
60
|
+
private cookieOptions;
|
|
61
|
+
private setJwtCookies;
|
|
62
|
+
issueTokens(apiReq: ApiRequest, user: UserEntity, metadata?: TokenIssueOptions): Promise<TokenPair>;
|
|
63
|
+
private assertAuthReady;
|
|
64
|
+
private parseLoginBody;
|
|
65
|
+
private parseImpersonationRequest;
|
|
66
|
+
private resolveImpersonationIdentifier;
|
|
67
|
+
private buildImpersonationMetadata;
|
|
68
|
+
private getUserOrThrow;
|
|
69
|
+
private getRealUserIdentifier;
|
|
70
|
+
private resolveActorContext;
|
|
71
|
+
private extractRefreshToken;
|
|
72
|
+
private normalizeScope;
|
|
73
|
+
private postLogin;
|
|
74
|
+
private postRefresh;
|
|
75
|
+
private postLogout;
|
|
76
|
+
private postWhoAmI;
|
|
77
|
+
private postPasskeyChallenge;
|
|
78
|
+
private postPasskeyVerify;
|
|
79
|
+
private postImpersonation;
|
|
80
|
+
private deleteImpersonation;
|
|
81
|
+
private getUserFromPasskey;
|
|
82
|
+
private postOAuthStart;
|
|
83
|
+
private postOAuthCallback;
|
|
84
|
+
private postOAuthAuthorize;
|
|
85
|
+
private postOAuthToken;
|
|
86
|
+
private handleAuthorizationCodeGrant;
|
|
87
|
+
private handleRefreshTokenGrant;
|
|
88
|
+
private clearOAuthCookies;
|
|
89
|
+
private buildTokenResponse;
|
|
90
|
+
private resolveScope;
|
|
91
|
+
private resolveClientAuthentication;
|
|
92
|
+
private assertRedirectUriAllowed;
|
|
93
|
+
private resolveUserForOAuth;
|
|
94
|
+
defineRoutes(): ApiRoute[];
|
|
95
|
+
}
|
|
96
|
+
export {};
|