@technomoron/api-server-base 2.0.0-beta.1 → 2.0.0-beta.11
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.txt +25 -2
- package/dist/cjs/api-server-base.cjs +277 -41
- package/dist/cjs/api-server-base.d.ts +27 -7
- package/dist/cjs/auth-api/auth-module.d.ts +11 -2
- package/dist/cjs/auth-api/auth-module.js +215 -46
- package/dist/cjs/auth-api/compat-auth-storage.d.ts +7 -5
- package/dist/cjs/auth-api/compat-auth-storage.js +15 -3
- package/dist/cjs/auth-api/mem-auth-store.d.ts +5 -3
- package/dist/cjs/auth-api/mem-auth-store.js +7 -1
- package/dist/cjs/auth-api/sql-auth-store.d.ts +5 -3
- package/dist/cjs/auth-api/sql-auth-store.js +7 -1
- package/dist/cjs/auth-api/storage.d.ts +6 -4
- package/dist/cjs/auth-api/storage.js +15 -5
- package/dist/cjs/auth-api/types.d.ts +7 -2
- package/dist/cjs/index.cjs +4 -4
- package/dist/cjs/index.d.ts +4 -4
- package/dist/cjs/oauth/sequelize.js +1 -1
- package/dist/cjs/passkey/base.d.ts +1 -0
- package/dist/cjs/passkey/memory.d.ts +1 -0
- package/dist/cjs/passkey/memory.js +4 -0
- package/dist/cjs/passkey/sequelize.d.ts +1 -0
- package/dist/cjs/passkey/sequelize.js +11 -2
- package/dist/cjs/passkey/service.d.ts +5 -2
- package/dist/cjs/passkey/service.js +145 -10
- package/dist/cjs/passkey/types.d.ts +3 -0
- package/dist/cjs/user/base.js +2 -1
- package/dist/esm/api-server-base.d.ts +27 -7
- package/dist/esm/api-server-base.js +278 -42
- package/dist/esm/auth-api/auth-module.d.ts +11 -2
- package/dist/esm/auth-api/auth-module.js +216 -47
- package/dist/esm/auth-api/compat-auth-storage.d.ts +7 -5
- package/dist/esm/auth-api/compat-auth-storage.js +13 -1
- package/dist/esm/auth-api/mem-auth-store.d.ts +5 -3
- package/dist/esm/auth-api/mem-auth-store.js +8 -2
- package/dist/esm/auth-api/sql-auth-store.d.ts +5 -3
- package/dist/esm/auth-api/sql-auth-store.js +8 -2
- package/dist/esm/auth-api/storage.d.ts +6 -4
- package/dist/esm/auth-api/storage.js +13 -3
- package/dist/esm/auth-api/types.d.ts +7 -2
- package/dist/esm/index.d.ts +4 -4
- package/dist/esm/index.js +2 -2
- package/dist/esm/oauth/sequelize.js +1 -1
- package/dist/esm/passkey/base.d.ts +1 -0
- package/dist/esm/passkey/memory.d.ts +1 -0
- package/dist/esm/passkey/memory.js +4 -0
- package/dist/esm/passkey/sequelize.d.ts +1 -0
- package/dist/esm/passkey/sequelize.js +11 -2
- package/dist/esm/passkey/service.d.ts +5 -2
- package/dist/esm/passkey/service.js +113 -11
- package/dist/esm/passkey/types.d.ts +3 -0
- package/dist/esm/user/base.js +2 -1
- package/package.json +13 -11
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
const node_crypto_1 = require("node:crypto");
|
|
4
|
+
const helpers_1 = require("@simplewebauthn/server/helpers");
|
|
4
5
|
const api_server_base_js_1 = require("../api-server-base.js");
|
|
5
6
|
const module_js_1 = require("./module.js");
|
|
7
|
+
const storage_js_1 = require("./storage.js");
|
|
6
8
|
function isAuthIdentifier(value) {
|
|
7
9
|
return typeof value === 'string' || typeof value === 'number';
|
|
8
10
|
}
|
|
@@ -74,9 +76,21 @@ class AuthModule extends module_js_1.BaseAuthModule {
|
|
|
74
76
|
}
|
|
75
77
|
buildTokenMetadata(metadata = {}) {
|
|
76
78
|
const scope = metadata.scope;
|
|
79
|
+
const domain = metadata.domain ?? this.defaultDomain ?? '';
|
|
80
|
+
let fingerprint = metadata.fingerprint ?? metadata.clientId ?? '';
|
|
81
|
+
if (typeof fingerprint === 'string') {
|
|
82
|
+
fingerprint = fingerprint.trim();
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
fingerprint = '';
|
|
86
|
+
}
|
|
87
|
+
// Avoid every client sharing the empty-string fingerprint which collapses sessions into one bucket.
|
|
88
|
+
if (!fingerprint) {
|
|
89
|
+
fingerprint = `srv-${(0, node_crypto_1.randomUUID)()}`;
|
|
90
|
+
}
|
|
77
91
|
return {
|
|
78
|
-
domain
|
|
79
|
-
fingerprint
|
|
92
|
+
domain,
|
|
93
|
+
fingerprint,
|
|
80
94
|
label: metadata.label ?? (Array.isArray(scope) ? scope.join(' ') : typeof scope === 'string' ? scope : ''),
|
|
81
95
|
clientId: metadata.clientId,
|
|
82
96
|
ruid: metadata.ruid,
|
|
@@ -183,6 +197,44 @@ class AuthModule extends module_js_1.BaseAuthModule {
|
|
|
183
197
|
}
|
|
184
198
|
return prefs;
|
|
185
199
|
}
|
|
200
|
+
validateCredentialId(apiReq) {
|
|
201
|
+
const paramId = toStringOrNull(apiReq.req.params?.credentialId);
|
|
202
|
+
const bodyId = toStringOrNull(apiReq.req.body?.credentialId);
|
|
203
|
+
const credentialId = paramId ?? bodyId;
|
|
204
|
+
if (!credentialId) {
|
|
205
|
+
throw new api_server_base_js_1.ApiError({ code: 400, message: 'credentialId is required' });
|
|
206
|
+
}
|
|
207
|
+
try {
|
|
208
|
+
helpers_1.isoBase64URL.toBuffer(credentialId);
|
|
209
|
+
}
|
|
210
|
+
catch {
|
|
211
|
+
throw new api_server_base_js_1.ApiError({ code: 400, message: 'Invalid credentialId' });
|
|
212
|
+
}
|
|
213
|
+
return credentialId;
|
|
214
|
+
}
|
|
215
|
+
normalizeCredentialId(value) {
|
|
216
|
+
if (Buffer.isBuffer(value)) {
|
|
217
|
+
return value;
|
|
218
|
+
}
|
|
219
|
+
try {
|
|
220
|
+
return Buffer.from(helpers_1.isoBase64URL.toBuffer(value));
|
|
221
|
+
}
|
|
222
|
+
catch {
|
|
223
|
+
try {
|
|
224
|
+
return Buffer.from(value, 'base64');
|
|
225
|
+
}
|
|
226
|
+
catch {
|
|
227
|
+
return Buffer.from(value);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
toIsoDate(value) {
|
|
232
|
+
if (!value) {
|
|
233
|
+
return undefined;
|
|
234
|
+
}
|
|
235
|
+
const date = value instanceof Date ? value : new Date(value);
|
|
236
|
+
return Number.isNaN(date.getTime()) ? undefined : date.toISOString();
|
|
237
|
+
}
|
|
186
238
|
cookieOptions(apiReq) {
|
|
187
239
|
const conf = this.server.config;
|
|
188
240
|
const forwarded = apiReq.req.headers['x-forwarded-proto'];
|
|
@@ -513,7 +565,28 @@ class AuthModule extends module_js_1.BaseAuthModule {
|
|
|
513
565
|
: cookiePrefs.refreshTtlSeconds;
|
|
514
566
|
this.setJwtCookies(apiReq, { accessToken: access.token, refreshToken }, { sessionCookie: cookiePrefs.sessionCookie ?? false, refreshTtlSeconds: refreshTtlForCookie });
|
|
515
567
|
}
|
|
516
|
-
|
|
568
|
+
const tokenClaims = verify.data;
|
|
569
|
+
const effectiveUserId = this.storage.getUserId(user);
|
|
570
|
+
const effectiveId = String(effectiveUserId);
|
|
571
|
+
const rawRealId = stored.ruid ?? tokenClaims.ruid;
|
|
572
|
+
const normalizedRealId = rawRealId === undefined || rawRealId === null ? null : String(rawRealId).trim() || null;
|
|
573
|
+
const isImpersonating = normalizedRealId !== null && normalizedRealId !== effectiveId;
|
|
574
|
+
let realUser;
|
|
575
|
+
let realUserId;
|
|
576
|
+
if (isImpersonating && normalizedRealId !== null) {
|
|
577
|
+
const realUserEntity = await this.getUserOrThrow(normalizedRealId, 'Real user not found');
|
|
578
|
+
realUser = this.storage.filterUser(realUserEntity);
|
|
579
|
+
realUserId = this.storage.getUserId(realUserEntity);
|
|
580
|
+
}
|
|
581
|
+
return [
|
|
582
|
+
200,
|
|
583
|
+
{
|
|
584
|
+
user: this.storage.filterUser(user),
|
|
585
|
+
isImpersonating,
|
|
586
|
+
realUser,
|
|
587
|
+
realUserId
|
|
588
|
+
}
|
|
589
|
+
];
|
|
517
590
|
}
|
|
518
591
|
async postPasskeyChallenge(apiReq) {
|
|
519
592
|
if (typeof this.storage.createPasskeyChallenge !== 'function') {
|
|
@@ -598,6 +671,44 @@ class AuthModule extends module_js_1.BaseAuthModule {
|
|
|
598
671
|
const publicUser = this.storage.filterUser(user);
|
|
599
672
|
return [200, { ...tokens, user: publicUser }];
|
|
600
673
|
}
|
|
674
|
+
async getPasskeys(apiReq) {
|
|
675
|
+
if (typeof this.storage.listUserCredentials !== 'function') {
|
|
676
|
+
throw new api_server_base_js_1.ApiError({ code: 501, message: 'Passkey credential listing is not configured' });
|
|
677
|
+
}
|
|
678
|
+
const { userId } = await this.resolveActorContext(apiReq);
|
|
679
|
+
const credentials = await this.storage.listUserCredentials(userId);
|
|
680
|
+
const safeCredentials = credentials.map((credential) => {
|
|
681
|
+
const bufferId = this.normalizeCredentialId(credential.credentialId);
|
|
682
|
+
return {
|
|
683
|
+
id: helpers_1.isoBase64URL.fromBuffer(new Uint8Array(bufferId)),
|
|
684
|
+
transports: credential.transports,
|
|
685
|
+
backedUp: credential.backedUp,
|
|
686
|
+
deviceType: credential.deviceType,
|
|
687
|
+
createdAt: this.toIsoDate(credential.createdAt),
|
|
688
|
+
updatedAt: this.toIsoDate(credential.updatedAt)
|
|
689
|
+
};
|
|
690
|
+
});
|
|
691
|
+
return [200, { credentials: safeCredentials }];
|
|
692
|
+
}
|
|
693
|
+
async deletePasskey(apiReq) {
|
|
694
|
+
if (typeof this.storage.listUserCredentials !== 'function' ||
|
|
695
|
+
typeof this.storage.deletePasskeyCredential !== 'function') {
|
|
696
|
+
throw new api_server_base_js_1.ApiError({ code: 501, message: 'Passkey credential management is not configured' });
|
|
697
|
+
}
|
|
698
|
+
const { userId } = await this.resolveActorContext(apiReq);
|
|
699
|
+
const credentialId = this.validateCredentialId(apiReq);
|
|
700
|
+
const bufferId = Buffer.from(helpers_1.isoBase64URL.toBuffer(credentialId));
|
|
701
|
+
const credentials = await this.storage.listUserCredentials(userId);
|
|
702
|
+
const owns = credentials.some((credential) => {
|
|
703
|
+
const candidateId = this.normalizeCredentialId(credential.credentialId);
|
|
704
|
+
return helpers_1.isoBase64URL.fromBuffer(new Uint8Array(candidateId)) === credentialId;
|
|
705
|
+
});
|
|
706
|
+
if (!owns) {
|
|
707
|
+
throw new api_server_base_js_1.ApiError({ code: 404, message: 'Passkey not found' });
|
|
708
|
+
}
|
|
709
|
+
const deleted = await this.storage.deletePasskeyCredential(bufferId);
|
|
710
|
+
return [200, { deleted }];
|
|
711
|
+
}
|
|
601
712
|
async postImpersonation(apiReq) {
|
|
602
713
|
this.assertAuthReady();
|
|
603
714
|
const { targetIdentifier, metadata } = this.parseImpersonationRequest(apiReq);
|
|
@@ -941,47 +1052,91 @@ class AuthModule extends module_js_1.BaseAuthModule {
|
|
|
941
1052
|
}
|
|
942
1053
|
throw new api_server_base_js_1.ApiError({ code: 401, message: 'Authorization requires user authentication' });
|
|
943
1054
|
}
|
|
1055
|
+
hasPasskeyService() {
|
|
1056
|
+
const storageAny = this.storage;
|
|
1057
|
+
if (storageAny.passkeyService || storageAny.passkeyStore) {
|
|
1058
|
+
return true;
|
|
1059
|
+
}
|
|
1060
|
+
if (storageAny.adapter?.passkeyService || storageAny.adapter?.passkeyStore) {
|
|
1061
|
+
return true;
|
|
1062
|
+
}
|
|
1063
|
+
const serverAny = this.server;
|
|
1064
|
+
return !!serverAny.passkeyServiceAdapter;
|
|
1065
|
+
}
|
|
1066
|
+
hasOAuthStore() {
|
|
1067
|
+
const storageAny = this.storage;
|
|
1068
|
+
if (storageAny.oauthStore) {
|
|
1069
|
+
return true;
|
|
1070
|
+
}
|
|
1071
|
+
if (storageAny.adapter?.oauthStore) {
|
|
1072
|
+
return true;
|
|
1073
|
+
}
|
|
1074
|
+
const serverAny = this.server;
|
|
1075
|
+
return !!serverAny.oauthStoreAdapter;
|
|
1076
|
+
}
|
|
1077
|
+
storageImplements(key) {
|
|
1078
|
+
const candidate = this.storage[key];
|
|
1079
|
+
if (typeof candidate !== 'function') {
|
|
1080
|
+
return false;
|
|
1081
|
+
}
|
|
1082
|
+
const baseImpl = storage_js_1.BaseAuthAdapter.prototype[key];
|
|
1083
|
+
return candidate !== baseImpl;
|
|
1084
|
+
}
|
|
1085
|
+
storageImplementsAll(keys) {
|
|
1086
|
+
return keys.every((key) => this.storageImplements(key));
|
|
1087
|
+
}
|
|
944
1088
|
defineRoutes() {
|
|
945
|
-
const routes = [
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
}
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
1089
|
+
const routes = [];
|
|
1090
|
+
const coreAuthSupported = this.storageImplementsAll([
|
|
1091
|
+
'getUser',
|
|
1092
|
+
'getUserPasswordHash',
|
|
1093
|
+
'getUserId',
|
|
1094
|
+
'verifyPassword',
|
|
1095
|
+
'filterUser',
|
|
1096
|
+
'storeToken',
|
|
1097
|
+
'getToken',
|
|
1098
|
+
'deleteToken'
|
|
1099
|
+
]);
|
|
1100
|
+
if (!coreAuthSupported) {
|
|
1101
|
+
return routes;
|
|
1102
|
+
}
|
|
1103
|
+
routes.push({
|
|
1104
|
+
method: 'post',
|
|
1105
|
+
path: '/v1/login',
|
|
1106
|
+
handler: (req) => this.postLogin(req),
|
|
1107
|
+
auth: { type: 'none', req: 'any' }
|
|
1108
|
+
}, {
|
|
1109
|
+
method: 'post',
|
|
1110
|
+
path: '/v1/refresh',
|
|
1111
|
+
handler: (req) => this.postRefresh(req),
|
|
1112
|
+
auth: { type: 'none', req: 'any' }
|
|
1113
|
+
}, {
|
|
1114
|
+
method: 'post',
|
|
1115
|
+
path: '/v1/logout',
|
|
1116
|
+
handler: (req) => this.postLogout(req),
|
|
1117
|
+
auth: { type: 'maybe', req: 'any' }
|
|
1118
|
+
}, {
|
|
1119
|
+
method: 'post',
|
|
1120
|
+
path: '/v1/whoami',
|
|
1121
|
+
handler: (req) => this.postWhoAmI(req),
|
|
1122
|
+
auth: { type: 'maybe', req: 'any' }
|
|
1123
|
+
}, {
|
|
1124
|
+
method: 'post',
|
|
1125
|
+
path: '/v1/impersonations',
|
|
1126
|
+
handler: (req) => this.postImpersonation(req),
|
|
1127
|
+
auth: { type: 'strict', req: 'any' }
|
|
1128
|
+
}, {
|
|
1129
|
+
method: 'delete',
|
|
1130
|
+
path: '/v1/impersonations',
|
|
1131
|
+
handler: (req) => this.deleteImpersonation(req),
|
|
1132
|
+
auth: { type: 'strict', req: 'any' }
|
|
1133
|
+
});
|
|
1134
|
+
const passkeysSupported = this.hasPasskeyService() &&
|
|
1135
|
+
this.storageImplements('createPasskeyChallenge') &&
|
|
1136
|
+
this.storageImplements('verifyPasskeyResponse');
|
|
1137
|
+
const passkeyCredentialsSupported = passkeysSupported &&
|
|
1138
|
+
this.storageImplements('listUserCredentials') &&
|
|
1139
|
+
this.storageImplements('deletePasskeyCredential');
|
|
985
1140
|
if (passkeysSupported) {
|
|
986
1141
|
routes.push({
|
|
987
1142
|
method: 'post',
|
|
@@ -994,6 +1149,19 @@ class AuthModule extends module_js_1.BaseAuthModule {
|
|
|
994
1149
|
handler: (req) => this.postPasskeyVerify(req),
|
|
995
1150
|
auth: { type: 'none', req: 'any' }
|
|
996
1151
|
});
|
|
1152
|
+
if (passkeyCredentialsSupported) {
|
|
1153
|
+
routes.push({
|
|
1154
|
+
method: 'get',
|
|
1155
|
+
path: '/v1/passkeys',
|
|
1156
|
+
handler: (req) => this.getPasskeys(req),
|
|
1157
|
+
auth: { type: 'strict', req: 'any' }
|
|
1158
|
+
}, {
|
|
1159
|
+
method: 'delete',
|
|
1160
|
+
path: '/v1/passkeys/:credentialId?',
|
|
1161
|
+
handler: (req) => this.deletePasskey(req),
|
|
1162
|
+
auth: { type: 'strict', req: 'any' }
|
|
1163
|
+
});
|
|
1164
|
+
}
|
|
997
1165
|
}
|
|
998
1166
|
const externalOAuthSupported = typeof this.server.initiateOAuth === 'function' && typeof this.server.completeOAuth === 'function';
|
|
999
1167
|
if (externalOAuthSupported) {
|
|
@@ -1009,9 +1177,10 @@ class AuthModule extends module_js_1.BaseAuthModule {
|
|
|
1009
1177
|
auth: { type: 'none', req: 'any' }
|
|
1010
1178
|
});
|
|
1011
1179
|
}
|
|
1012
|
-
const oauthStorageSupported =
|
|
1013
|
-
|
|
1014
|
-
|
|
1180
|
+
const oauthStorageSupported = this.hasOAuthStore() &&
|
|
1181
|
+
this.storageImplements('getClient') &&
|
|
1182
|
+
this.storageImplements('createAuthCode') &&
|
|
1183
|
+
this.storageImplements('consumeAuthCode');
|
|
1015
1184
|
if (oauthStorageSupported) {
|
|
1016
1185
|
routes.push({
|
|
1017
1186
|
method: 'post',
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { PasskeyService } from '../passkey/service.js';
|
|
2
|
-
import type {
|
|
2
|
+
import type { AuthAdapter, AuthIdentifier } from './types.js';
|
|
3
3
|
import type { OAuthStore } from '../oauth/base.js';
|
|
4
4
|
import type { AuthCodeData, AuthCodeRequest, OAuthClient } from '../oauth/types.js';
|
|
5
5
|
import type { PasskeyStore } from '../passkey/base.js';
|
|
6
|
-
import type { PasskeyChallenge, PasskeyChallengeParams, PasskeyServiceConfig, PasskeyVerificationParams, PasskeyVerificationResult } from '../passkey/types.js';
|
|
6
|
+
import type { PasskeyChallenge, PasskeyChallengeParams, PasskeyServiceConfig, StoredPasskeyCredential, PasskeyVerificationParams, PasskeyVerificationResult } from '../passkey/types.js';
|
|
7
7
|
import type { TokenStore } from '../token/base.js';
|
|
8
8
|
import type { Token } from '../token/types.js';
|
|
9
9
|
import type { UserStore } from '../user/base.js';
|
|
@@ -11,7 +11,7 @@ interface PasskeyAdapterOptions {
|
|
|
11
11
|
store: PasskeyStore;
|
|
12
12
|
config: PasskeyServiceConfig;
|
|
13
13
|
}
|
|
14
|
-
export interface
|
|
14
|
+
export interface AuthAdapterOptions<UserRow, PublicUser> {
|
|
15
15
|
userStore: UserStore<UserRow, PublicUser>;
|
|
16
16
|
tokenStore: TokenStore;
|
|
17
17
|
passkeys?: PasskeyAdapterOptions | PasskeyService;
|
|
@@ -21,13 +21,13 @@ export interface AuthStorageAdapterOptions<UserRow, PublicUser> {
|
|
|
21
21
|
effectiveUserId: AuthIdentifier;
|
|
22
22
|
}) => boolean | Promise<boolean>;
|
|
23
23
|
}
|
|
24
|
-
export declare class
|
|
24
|
+
export declare class CompositeAuthAdapter<UserRow, PublicUser> implements AuthAdapter<UserRow, PublicUser> {
|
|
25
25
|
private readonly userStore;
|
|
26
26
|
private readonly tokenStore;
|
|
27
27
|
private readonly oauthStore?;
|
|
28
28
|
private readonly passkeyService?;
|
|
29
29
|
private readonly canImpersonateFn?;
|
|
30
|
-
constructor(options:
|
|
30
|
+
constructor(options: AuthAdapterOptions<UserRow, PublicUser>);
|
|
31
31
|
getUser(identifier: AuthIdentifier): Promise<UserRow | null>;
|
|
32
32
|
getUserPasswordHash(user: UserRow): string;
|
|
33
33
|
getUserId(user: UserRow): AuthIdentifier;
|
|
@@ -43,6 +43,8 @@ export declare class AuthStorageAdapter<UserRow, PublicUser> implements AuthStor
|
|
|
43
43
|
}): Promise<boolean>;
|
|
44
44
|
createPasskeyChallenge(params: PasskeyChallengeParams): Promise<PasskeyChallenge>;
|
|
45
45
|
verifyPasskeyResponse(params: PasskeyVerificationParams): Promise<PasskeyVerificationResult>;
|
|
46
|
+
listUserCredentials(userId: AuthIdentifier): Promise<StoredPasskeyCredential[]>;
|
|
47
|
+
deletePasskeyCredential(credentialId: Buffer | string): Promise<boolean>;
|
|
46
48
|
getClient(clientId: string): Promise<OAuthClient | null>;
|
|
47
49
|
verifyClientSecret(client: OAuthClient, clientSecret: string | null): Promise<boolean>;
|
|
48
50
|
createAuthCode(request: AuthCodeRequest): Promise<AuthCodeData>;
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.CompositeAuthAdapter = void 0;
|
|
4
4
|
const node_crypto_1 = require("node:crypto");
|
|
5
5
|
const service_js_1 = require("../passkey/service.js");
|
|
6
|
-
class
|
|
6
|
+
class CompositeAuthAdapter {
|
|
7
7
|
constructor(options) {
|
|
8
8
|
this.userStore = options.userStore;
|
|
9
9
|
this.tokenStore = options.tokenStore;
|
|
@@ -55,6 +55,18 @@ class AuthStorageAdapter {
|
|
|
55
55
|
}
|
|
56
56
|
return this.passkeyService.verifyResponse(params);
|
|
57
57
|
}
|
|
58
|
+
async listUserCredentials(userId) {
|
|
59
|
+
if (!this.passkeyService) {
|
|
60
|
+
throw new Error('Passkey storage is not configured');
|
|
61
|
+
}
|
|
62
|
+
return this.passkeyService.listUserCredentials(userId);
|
|
63
|
+
}
|
|
64
|
+
async deletePasskeyCredential(credentialId) {
|
|
65
|
+
if (!this.passkeyService) {
|
|
66
|
+
throw new Error('Passkey storage is not configured');
|
|
67
|
+
}
|
|
68
|
+
return this.passkeyService.deleteCredential(credentialId);
|
|
69
|
+
}
|
|
58
70
|
async getClient(clientId) {
|
|
59
71
|
if (!this.oauthStore) {
|
|
60
72
|
return null;
|
|
@@ -113,4 +125,4 @@ class AuthStorageAdapter {
|
|
|
113
125
|
return params.realUserId === params.effectiveUserId;
|
|
114
126
|
}
|
|
115
127
|
}
|
|
116
|
-
exports.
|
|
128
|
+
exports.CompositeAuthAdapter = CompositeAuthAdapter;
|
|
@@ -2,11 +2,11 @@ import { MemoryOAuthStore } from '../oauth/memory.js';
|
|
|
2
2
|
import { MemoryPasskeyStore } from '../passkey/memory.js';
|
|
3
3
|
import { TokenStore } from '../token/base.js';
|
|
4
4
|
import { MemoryUserStore } from '../user/memory.js';
|
|
5
|
-
import type {
|
|
5
|
+
import type { AuthAdapter, AuthIdentifier } from './types.js';
|
|
6
6
|
import type { MemoryOAuthStoreOptions } from '../oauth/memory.js';
|
|
7
7
|
import type { AuthCodeData, AuthCodeRequest, OAuthClient } from '../oauth/types.js';
|
|
8
8
|
import type { MemoryPasskeyStoreOptions } from '../passkey/memory.js';
|
|
9
|
-
import type { PasskeyChallenge, PasskeyChallengeParams, PasskeyServiceConfig, PasskeyUserDescriptor, PasskeyVerificationParams, PasskeyVerificationResult } from '../passkey/types.js';
|
|
9
|
+
import type { PasskeyChallenge, PasskeyChallengeParams, PasskeyServiceConfig, PasskeyUserDescriptor, StoredPasskeyCredential, PasskeyVerificationParams, PasskeyVerificationResult } from '../passkey/types.js';
|
|
10
10
|
import type { Token } from '../token/types.js';
|
|
11
11
|
import type { MemoryUserAttributes, MemoryPublicUser, MemoryUserStoreOptions } from '../user/memory.js';
|
|
12
12
|
interface PasskeyOptions extends Partial<PasskeyServiceConfig> {
|
|
@@ -24,7 +24,7 @@ export interface MemAuthStoreParams<UserAttributes extends MemoryUserAttributes
|
|
|
24
24
|
}) => boolean | Promise<boolean>;
|
|
25
25
|
tokenStore?: TokenStore;
|
|
26
26
|
}
|
|
27
|
-
export declare class MemAuthStore<UserAttributes extends MemoryUserAttributes = MemoryUserAttributes, PublicUserShape extends MemoryPublicUser<UserAttributes> = MemoryPublicUser<UserAttributes>> implements
|
|
27
|
+
export declare class MemAuthStore<UserAttributes extends MemoryUserAttributes = MemoryUserAttributes, PublicUserShape extends MemoryPublicUser<UserAttributes> = MemoryPublicUser<UserAttributes>> implements AuthAdapter<UserAttributes, PublicUserShape> {
|
|
28
28
|
readonly userStore: MemoryUserStore<UserAttributes, PublicUserShape>;
|
|
29
29
|
readonly tokenStore: TokenStore;
|
|
30
30
|
readonly passkeyStore?: MemoryPasskeyStore;
|
|
@@ -54,6 +54,8 @@ export declare class MemAuthStore<UserAttributes extends MemoryUserAttributes =
|
|
|
54
54
|
}): Promise<boolean>;
|
|
55
55
|
createPasskeyChallenge(params: PasskeyChallengeParams): Promise<PasskeyChallenge>;
|
|
56
56
|
verifyPasskeyResponse(params: PasskeyVerificationParams): Promise<PasskeyVerificationResult>;
|
|
57
|
+
listUserCredentials(userId: AuthIdentifier): Promise<StoredPasskeyCredential[]>;
|
|
58
|
+
deletePasskeyCredential(credentialId: Buffer | string): Promise<boolean>;
|
|
57
59
|
getClient(clientId: string): Promise<OAuthClient | null>;
|
|
58
60
|
verifyClientSecret(client: OAuthClient, clientSecret: string | null): Promise<boolean>;
|
|
59
61
|
createAuthCode(request: AuthCodeRequest): Promise<AuthCodeData>;
|
|
@@ -59,7 +59,7 @@ class MemAuthStore {
|
|
|
59
59
|
passkeyStore = new memory_js_2.MemoryPasskeyStore({ resolveUser });
|
|
60
60
|
this.passkeyStore = passkeyStore;
|
|
61
61
|
}
|
|
62
|
-
this.adapter = new compat_auth_storage_js_1.
|
|
62
|
+
this.adapter = new compat_auth_storage_js_1.CompositeAuthAdapter({
|
|
63
63
|
userStore: this.userStore,
|
|
64
64
|
tokenStore: this.tokenStore,
|
|
65
65
|
passkeys: passkeyStore && passkeyConfig ? { store: passkeyStore, config: passkeyConfig } : undefined,
|
|
@@ -116,6 +116,12 @@ class MemAuthStore {
|
|
|
116
116
|
async verifyPasskeyResponse(params) {
|
|
117
117
|
return this.adapter.verifyPasskeyResponse(params);
|
|
118
118
|
}
|
|
119
|
+
async listUserCredentials(userId) {
|
|
120
|
+
return this.adapter.listUserCredentials(userId);
|
|
121
|
+
}
|
|
122
|
+
async deletePasskeyCredential(credentialId) {
|
|
123
|
+
return this.adapter.deletePasskeyCredential(credentialId);
|
|
124
|
+
}
|
|
119
125
|
async getClient(clientId) {
|
|
120
126
|
return this.adapter.getClient(clientId);
|
|
121
127
|
}
|
|
@@ -3,9 +3,9 @@ import { SequelizeOAuthStore, type SequelizeOAuthStoreOptions } from '../oauth/s
|
|
|
3
3
|
import { SequelizePasskeyStore } from '../passkey/sequelize.js';
|
|
4
4
|
import { type SequelizeTokenStoreOptions } from '../token/sequelize.js';
|
|
5
5
|
import { SequelizeUserStore, type AuthUserAttributes, GenericUserModel, GenericUserModelStatic } from '../user/sequelize.js';
|
|
6
|
-
import type {
|
|
6
|
+
import type { AuthAdapter, AuthIdentifier } from './types.js';
|
|
7
7
|
import type { AuthCodeData, AuthCodeRequest, OAuthClient } from '../oauth/types.js';
|
|
8
|
-
import type { PasskeyChallenge, PasskeyChallengeParams, PasskeyServiceConfig, PasskeyUserDescriptor, PasskeyVerificationParams, PasskeyVerificationResult } from '../passkey/types.js';
|
|
8
|
+
import type { PasskeyChallenge, PasskeyChallengeParams, PasskeyServiceConfig, PasskeyUserDescriptor, StoredPasskeyCredential, PasskeyVerificationParams, PasskeyVerificationResult } from '../passkey/types.js';
|
|
9
9
|
import type { TokenStore } from '../token/base.js';
|
|
10
10
|
import type { Token } from '../token/types.js';
|
|
11
11
|
interface PasskeyOptions extends Partial<PasskeyServiceConfig> {
|
|
@@ -30,7 +30,7 @@ export interface SqlAuthStoreParams<UserAttributes extends AuthUserAttributes =
|
|
|
30
30
|
tokenStoreOptions?: Omit<SequelizeTokenStoreOptions, 'sequelize'>;
|
|
31
31
|
oauthStoreOptions?: Omit<SequelizeOAuthStoreOptions, 'sequelize'>;
|
|
32
32
|
}
|
|
33
|
-
export declare class SqlAuthStore<UserAttributes extends AuthUserAttributes = AuthUserAttributes, PublicUserShape extends Omit<UserAttributes, 'password'> = Omit<UserAttributes, 'password'>> implements
|
|
33
|
+
export declare class SqlAuthStore<UserAttributes extends AuthUserAttributes = AuthUserAttributes, PublicUserShape extends Omit<UserAttributes, 'password'> = Omit<UserAttributes, 'password'>> implements AuthAdapter<UserAttributes, PublicUserShape> {
|
|
34
34
|
readonly userStore: SequelizeUserStore<UserAttributes, PublicUserShape>;
|
|
35
35
|
readonly tokenStore: TokenStore;
|
|
36
36
|
readonly passkeyStore?: SequelizePasskeyStore;
|
|
@@ -63,6 +63,8 @@ export declare class SqlAuthStore<UserAttributes extends AuthUserAttributes = Au
|
|
|
63
63
|
}): Promise<boolean>;
|
|
64
64
|
createPasskeyChallenge(params: PasskeyChallengeParams): Promise<PasskeyChallenge>;
|
|
65
65
|
verifyPasskeyResponse(params: PasskeyVerificationParams): Promise<PasskeyVerificationResult>;
|
|
66
|
+
listUserCredentials(userId: AuthIdentifier): Promise<StoredPasskeyCredential[]>;
|
|
67
|
+
deletePasskeyCredential(credentialId: Buffer | string): Promise<boolean>;
|
|
66
68
|
getClient(clientId: string): Promise<OAuthClient | null>;
|
|
67
69
|
verifyClientSecret(client: OAuthClient, clientSecret: string | null): Promise<boolean>;
|
|
68
70
|
createAuthCode(request: AuthCodeRequest): Promise<AuthCodeData>;
|
|
@@ -72,7 +72,7 @@ class SqlAuthStore {
|
|
|
72
72
|
passkeyStore = new sequelize_js_2.SequelizePasskeyStore({ sequelize: this.sequelize, resolveUser });
|
|
73
73
|
this.passkeyStore = passkeyStore;
|
|
74
74
|
}
|
|
75
|
-
this.adapter = new compat_auth_storage_js_1.
|
|
75
|
+
this.adapter = new compat_auth_storage_js_1.CompositeAuthAdapter({
|
|
76
76
|
userStore: this.userStore,
|
|
77
77
|
tokenStore: this.tokenStore,
|
|
78
78
|
passkeys: passkeyStore && passkeyConfig ? { store: passkeyStore, config: passkeyConfig } : undefined,
|
|
@@ -147,6 +147,12 @@ class SqlAuthStore {
|
|
|
147
147
|
async verifyPasskeyResponse(params) {
|
|
148
148
|
return this.adapter.verifyPasskeyResponse(params);
|
|
149
149
|
}
|
|
150
|
+
async listUserCredentials(userId) {
|
|
151
|
+
return this.adapter.listUserCredentials(userId);
|
|
152
|
+
}
|
|
153
|
+
async deletePasskeyCredential(credentialId) {
|
|
154
|
+
return this.adapter.deletePasskeyCredential(credentialId);
|
|
155
|
+
}
|
|
150
156
|
async getClient(clientId) {
|
|
151
157
|
return this.adapter.getClient(clientId);
|
|
152
158
|
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { AuthAdapter, AuthIdentifier } from './types.js';
|
|
2
2
|
import type { AuthCodeData, AuthCodeRequest, OAuthClient } from '../oauth/types.js';
|
|
3
|
-
import type { PasskeyChallenge, PasskeyChallengeParams, PasskeyVerificationParams, PasskeyVerificationResult } from '../passkey/types.js';
|
|
3
|
+
import type { PasskeyChallenge, PasskeyChallengeParams, PasskeyVerificationParams, PasskeyVerificationResult, StoredPasskeyCredential } from '../passkey/types.js';
|
|
4
4
|
import type { Token } from '../token/types.js';
|
|
5
|
-
export declare class
|
|
5
|
+
export declare class BaseAuthAdapter<UserRow = unknown, SafeUser = unknown> implements AuthAdapter<UserRow, SafeUser> {
|
|
6
6
|
getUser(identifier: AuthIdentifier): Promise<UserRow | null>;
|
|
7
7
|
getUserPasswordHash(user: UserRow): string;
|
|
8
8
|
getUserId(user: UserRow): AuthIdentifier;
|
|
@@ -24,6 +24,8 @@ export declare class BaseAuthStorage<UserRow = unknown, SafeUser = unknown> impl
|
|
|
24
24
|
}): Promise<boolean>;
|
|
25
25
|
createPasskeyChallenge(params: PasskeyChallengeParams): Promise<PasskeyChallenge>;
|
|
26
26
|
verifyPasskeyResponse(params: PasskeyVerificationParams): Promise<PasskeyVerificationResult>;
|
|
27
|
+
listUserCredentials(userId: AuthIdentifier): Promise<StoredPasskeyCredential[]>;
|
|
28
|
+
deletePasskeyCredential(credentialId: Buffer | string): Promise<boolean>;
|
|
27
29
|
getClient(clientId: string): Promise<OAuthClient | null>;
|
|
28
30
|
verifyClientSecret(client: OAuthClient, clientSecret: string | null): Promise<boolean>;
|
|
29
31
|
createAuthCode(request: AuthCodeRequest): Promise<AuthCodeData>;
|
|
@@ -33,4 +35,4 @@ export declare class BaseAuthStorage<UserRow = unknown, SafeUser = unknown> impl
|
|
|
33
35
|
effectiveUserId: AuthIdentifier;
|
|
34
36
|
}): Promise<boolean>;
|
|
35
37
|
}
|
|
36
|
-
export declare const
|
|
38
|
+
export declare const nullAuthAdapter: AuthAdapter<unknown, unknown>;
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
4
|
-
// Handy base you can extend when wiring a real
|
|
3
|
+
exports.nullAuthAdapter = exports.BaseAuthAdapter = void 0;
|
|
4
|
+
// Handy base you can extend when wiring a real auth adapter. Every method
|
|
5
5
|
// throws by default so unimplemented hooks fail loudly.
|
|
6
|
-
class
|
|
6
|
+
class BaseAuthAdapter {
|
|
7
7
|
// Override to load a user record by identifier
|
|
8
8
|
async getUser(identifier) {
|
|
9
9
|
void identifier;
|
|
@@ -60,6 +60,16 @@ class BaseAuthStorage {
|
|
|
60
60
|
void params;
|
|
61
61
|
throw new Error('Auth storage not configured');
|
|
62
62
|
}
|
|
63
|
+
// Override to list passkey credentials for a user
|
|
64
|
+
async listUserCredentials(userId) {
|
|
65
|
+
void userId;
|
|
66
|
+
throw new Error('Auth storage not configured');
|
|
67
|
+
}
|
|
68
|
+
// Override to delete a passkey credential
|
|
69
|
+
async deletePasskeyCredential(credentialId) {
|
|
70
|
+
void credentialId;
|
|
71
|
+
throw new Error('Auth storage not configured');
|
|
72
|
+
}
|
|
63
73
|
// Override to fetch an OAuth client by identifier
|
|
64
74
|
async getClient(clientId) {
|
|
65
75
|
void clientId;
|
|
@@ -88,5 +98,5 @@ class BaseAuthStorage {
|
|
|
88
98
|
return false;
|
|
89
99
|
}
|
|
90
100
|
}
|
|
91
|
-
exports.
|
|
92
|
-
exports.
|
|
101
|
+
exports.BaseAuthAdapter = BaseAuthAdapter;
|
|
102
|
+
exports.nullAuthAdapter = new BaseAuthAdapter();
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import type { AuthCodeData, AuthCodeRequest, OAuthClient } from '../oauth/types.js';
|
|
2
|
-
import type { PasskeyChallenge, PasskeyChallengeParams, PasskeyVerificationParams, PasskeyVerificationResult } from '../passkey/types.js';
|
|
2
|
+
import type { PasskeyChallenge, PasskeyChallengeParams, PasskeyVerificationParams, PasskeyVerificationResult, StoredPasskeyCredential } from '../passkey/types.js';
|
|
3
3
|
import type { Token } from '../token/types.js';
|
|
4
4
|
export type AuthIdentifier = string | number;
|
|
5
|
-
|
|
5
|
+
/** @internal */
|
|
6
|
+
export interface AuthAdapter<UserRow, SafeUser> {
|
|
6
7
|
getUser(identifier: AuthIdentifier): Promise<UserRow | null>;
|
|
7
8
|
getUserPasswordHash(user: UserRow): string;
|
|
8
9
|
getUserId(user: UserRow): AuthIdentifier;
|
|
@@ -18,6 +19,8 @@ export interface AuthStorage<UserRow, SafeUser> {
|
|
|
18
19
|
}): Promise<boolean>;
|
|
19
20
|
createPasskeyChallenge?(params: PasskeyChallengeParams): Promise<PasskeyChallenge>;
|
|
20
21
|
verifyPasskeyResponse?(params: PasskeyVerificationParams): Promise<PasskeyVerificationResult>;
|
|
22
|
+
listUserCredentials?(userId: AuthIdentifier): Promise<StoredPasskeyCredential[]>;
|
|
23
|
+
deletePasskeyCredential?(credentialId: Buffer | string): Promise<boolean>;
|
|
21
24
|
getClient?(clientId: string): Promise<OAuthClient | null>;
|
|
22
25
|
verifyClientSecret?(client: OAuthClient, clientSecret: string | null): Promise<boolean>;
|
|
23
26
|
createAuthCode?(request: AuthCodeRequest): Promise<AuthCodeData>;
|
|
@@ -27,3 +30,5 @@ export interface AuthStorage<UserRow, SafeUser> {
|
|
|
27
30
|
effectiveUserId: AuthIdentifier;
|
|
28
31
|
}): Promise<boolean>;
|
|
29
32
|
}
|
|
33
|
+
/** @internal */
|
|
34
|
+
export type AuthStorage<UserRow, SafeUser> = AuthAdapter<UserRow, SafeUser>;
|
package/dist/cjs/index.cjs
CHANGED
|
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.SequelizeOAuthStore = exports.MemoryOAuthStore = exports.OAuthStore = exports.SequelizePasskeyStore = exports.MemoryPasskeyStore = exports.PasskeyStore = exports.PasskeyService = exports.SequelizeTokenStore = exports.MemoryTokenStore = exports.TokenStore = exports.SequelizeUserStore = exports.MemoryUserStore = exports.UserStore = exports.AuthModule = exports.SqlAuthStore = exports.MemAuthStore = exports.
|
|
6
|
+
exports.SequelizeOAuthStore = exports.MemoryOAuthStore = exports.OAuthStore = exports.SequelizePasskeyStore = exports.MemoryPasskeyStore = exports.PasskeyStore = exports.PasskeyService = exports.SequelizeTokenStore = exports.MemoryTokenStore = exports.TokenStore = exports.SequelizeUserStore = exports.MemoryUserStore = exports.UserStore = exports.AuthModule = exports.SqlAuthStore = exports.MemAuthStore = exports.CompositeAuthAdapter = exports.BaseAuthModule = exports.nullAuthModule = exports.BaseAuthAdapter = exports.nullAuthAdapter = exports.ApiModule = exports.ApiError = exports.ApiServer = void 0;
|
|
7
7
|
var api_server_base_js_1 = require("./api-server-base.cjs");
|
|
8
8
|
Object.defineProperty(exports, "ApiServer", { enumerable: true, get: function () { return __importDefault(api_server_base_js_1).default; } });
|
|
9
9
|
var api_server_base_js_2 = require("./api-server-base.cjs");
|
|
@@ -11,13 +11,13 @@ Object.defineProperty(exports, "ApiError", { enumerable: true, get: function ()
|
|
|
11
11
|
var api_module_js_1 = require("./api-module.cjs");
|
|
12
12
|
Object.defineProperty(exports, "ApiModule", { enumerable: true, get: function () { return api_module_js_1.ApiModule; } });
|
|
13
13
|
var storage_js_1 = require("./auth-api/storage.js");
|
|
14
|
-
Object.defineProperty(exports, "
|
|
15
|
-
Object.defineProperty(exports, "
|
|
14
|
+
Object.defineProperty(exports, "nullAuthAdapter", { enumerable: true, get: function () { return storage_js_1.nullAuthAdapter; } });
|
|
15
|
+
Object.defineProperty(exports, "BaseAuthAdapter", { enumerable: true, get: function () { return storage_js_1.BaseAuthAdapter; } });
|
|
16
16
|
var module_js_1 = require("./auth-api/module.js");
|
|
17
17
|
Object.defineProperty(exports, "nullAuthModule", { enumerable: true, get: function () { return module_js_1.nullAuthModule; } });
|
|
18
18
|
Object.defineProperty(exports, "BaseAuthModule", { enumerable: true, get: function () { return module_js_1.BaseAuthModule; } });
|
|
19
19
|
var compat_auth_storage_js_1 = require("./auth-api/compat-auth-storage.js");
|
|
20
|
-
Object.defineProperty(exports, "
|
|
20
|
+
Object.defineProperty(exports, "CompositeAuthAdapter", { enumerable: true, get: function () { return compat_auth_storage_js_1.CompositeAuthAdapter; } });
|
|
21
21
|
var mem_auth_store_js_1 = require("./auth-api/mem-auth-store.js");
|
|
22
22
|
Object.defineProperty(exports, "MemAuthStore", { enumerable: true, get: function () { return mem_auth_store_js_1.MemAuthStore; } });
|
|
23
23
|
var sql_auth_store_js_1 = require("./auth-api/sql-auth-store.js");
|