pico-engine 1.4.0 → 1.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +188 -0
- package/dist/RulesetRegistryLoaderFs.js +2 -0
- package/dist/auth/AuthService.js +601 -0
- package/dist/auth/index.js +8 -0
- package/dist/auth/webauthn.js +97 -0
- package/dist/bundledKrl.js +42 -0
- package/dist/cli.js +11 -0
- package/dist/identity/IdentityService.js +340 -0
- package/dist/identity/crossPico.js +238 -0
- package/dist/identity/dbHelpers.js +67 -0
- package/dist/identity/didcommIngressRoutes.js +44 -0
- package/dist/identity/didoModule.js +252 -0
- package/dist/identity/errors.js +11 -0
- package/dist/identity/establishSubscription.js +97 -0
- package/dist/identity/index.js +15 -0
- package/dist/identity/peerDid.js +9 -0
- package/dist/identity/skyIntro.js +194 -0
- package/dist/identity/skyProtocol.js +105 -0
- package/dist/identity/store.js +233 -0
- package/dist/identity/types.js +13 -0
- package/dist/identity/veramoDidComm.js +260 -0
- package/dist/identity/webvh.js +266 -0
- package/dist/identity/webvhCrypto.js +128 -0
- package/dist/identity/webvhRoutes.js +61 -0
- package/dist/index.js +100 -44
- package/dist/oauth/OAuthService.js +388 -0
- package/dist/oauth/acg.js +495 -0
- package/dist/oauth/channelEligibility.js +26 -0
- package/dist/oauth/errors.js +12 -0
- package/dist/oauth/index.js +21 -0
- package/dist/oauth/krlModule.js +27 -0
- package/dist/oauth/meshOAuth.js +43 -0
- package/dist/provisionRoot.js +118 -0
- package/dist/server.js +609 -15
- package/dist/uiChannelPolicies.js +48 -0
- package/krl/io.picolabs.did-o.krl +5 -1
- package/krl/io.picolabs.oauth.krl +48 -0
- package/krl/io.picolabs.pico-engine-ui.krl +106 -18
- package/krl/io.picolabs.subscription.krl +242 -23
- package/krl/io.picolabs.wrangler.krl +225 -16
- package/package.json +38 -9
- package/public/docs/krl/_sidebar.md +1 -0
- package/public/docs/krl/discovery.md +144 -0
- package/public/docs/krl/modules/ctx.md +8 -0
- package/public/docs/krl/modules/event.md +41 -0
- package/public/pico-engine-ui.css +1 -1
- package/public/pico-engine-ui.js +24 -20
- package/public/pico-labs-logo.png +0 -0
- package/src/RulesetRegistryLoaderFs.ts +2 -0
- package/src/auth/AuthService.ts +850 -0
- package/src/auth/index.ts +17 -0
- package/src/auth/webauthn.ts +182 -0
- package/src/bundledKrl.ts +42 -0
- package/src/cli.ts +11 -0
- package/src/identity/IdentityService.ts +571 -0
- package/src/identity/crossPico.ts +371 -0
- package/src/identity/dbHelpers.ts +51 -0
- package/src/identity/didcommIngressRoutes.ts +54 -0
- package/src/identity/didoModule.ts +388 -0
- package/src/identity/errors.ts +8 -0
- package/src/identity/establishSubscription.ts +160 -0
- package/src/identity/index.ts +24 -0
- package/src/identity/peerDid.ts +22 -0
- package/src/identity/skyIntro.ts +321 -0
- package/src/identity/skyProtocol.ts +192 -0
- package/src/identity/store.ts +343 -0
- package/src/identity/types.ts +63 -0
- package/src/identity/veramoDidComm.ts +363 -0
- package/src/identity/webvh.ts +325 -0
- package/src/identity/webvhCrypto.ts +200 -0
- package/src/identity/webvhRoutes.ts +64 -0
- package/src/index.ts +145 -53
- package/src/oauth/OAuthService.ts +510 -0
- package/src/oauth/acg.ts +666 -0
- package/src/oauth/channelEligibility.ts +26 -0
- package/src/oauth/errors.ts +10 -0
- package/src/oauth/index.ts +14 -0
- package/src/oauth/krlModule.ts +31 -0
- package/src/oauth/meshOAuth.ts +50 -0
- package/src/provisionRoot.ts +151 -0
- package/src/server.ts +700 -24
- package/src/uiChannelPolicies.ts +49 -0
|
@@ -0,0 +1,601 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __asyncValues = (this && this.__asyncValues) || function (o) {
|
|
3
|
+
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
|
|
4
|
+
var m = o[Symbol.asyncIterator], i;
|
|
5
|
+
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
|
|
6
|
+
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
|
|
7
|
+
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
|
|
8
|
+
};
|
|
9
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
+
exports.AuthService = exports.AuthError = void 0;
|
|
11
|
+
const crypto = require("crypto");
|
|
12
|
+
const CREDENTIAL_PREFIX = "auth-credential";
|
|
13
|
+
const ACCOUNT_PREFIX = "auth-account";
|
|
14
|
+
const SESSION_PREFIX = "auth-session";
|
|
15
|
+
const INVITE_PREFIX = "auth-invite";
|
|
16
|
+
const DEFAULT_SESSION_TTL_MS = 30 * 24 * 60 * 60 * 1000; // 30 days
|
|
17
|
+
const DEFAULT_INVITE_TTL_MS = 7 * 24 * 60 * 60 * 1000; // 7 days
|
|
18
|
+
const CHALLENGE_TTL_MS = 5 * 60 * 1000; // 5 minutes
|
|
19
|
+
const DEFAULT_RP_NAME = "Pico Engine";
|
|
20
|
+
class AuthError extends Error {
|
|
21
|
+
constructor(message, statusCode = 400) {
|
|
22
|
+
super(message);
|
|
23
|
+
this.name = "AuthError";
|
|
24
|
+
this.statusCode = statusCode;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
exports.AuthError = AuthError;
|
|
28
|
+
class AuthService {
|
|
29
|
+
constructor(deps) {
|
|
30
|
+
this.challenges = new Map();
|
|
31
|
+
this.deps = deps;
|
|
32
|
+
}
|
|
33
|
+
get sessionTtlMs() {
|
|
34
|
+
return this.deps.sessionTtlMs || DEFAULT_SESSION_TTL_MS;
|
|
35
|
+
}
|
|
36
|
+
/** Resolve the relying-party config from explicit overrides or base_url. */
|
|
37
|
+
rp() {
|
|
38
|
+
const base = this.deps.getBaseUrl() || "http://localhost:3000";
|
|
39
|
+
let hostname = "localhost";
|
|
40
|
+
let origin = base;
|
|
41
|
+
try {
|
|
42
|
+
const url = new URL(base);
|
|
43
|
+
hostname = url.hostname;
|
|
44
|
+
origin = url.origin;
|
|
45
|
+
}
|
|
46
|
+
catch (_e) {
|
|
47
|
+
// fall through to defaults
|
|
48
|
+
}
|
|
49
|
+
const originLabel = this.originLabelFromUrl(origin);
|
|
50
|
+
const rpName = this.deps.rpName ||
|
|
51
|
+
(originLabel
|
|
52
|
+
? `${DEFAULT_RP_NAME} (${originLabel})`
|
|
53
|
+
: DEFAULT_RP_NAME);
|
|
54
|
+
return {
|
|
55
|
+
rpID: this.deps.rpID || hostname,
|
|
56
|
+
rpName,
|
|
57
|
+
origin: this.deps.origin || origin,
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
/** Host[:port] label for disambiguating local multi-engine setups in WebAuthn UI. */
|
|
61
|
+
originLabelFromUrl(origin) {
|
|
62
|
+
try {
|
|
63
|
+
const url = new URL(origin);
|
|
64
|
+
return url.port ? `${url.hostname}:${url.port}` : url.hostname;
|
|
65
|
+
}
|
|
66
|
+
catch (_e) {
|
|
67
|
+
return "";
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
isSecure() {
|
|
71
|
+
return this.rp().origin.startsWith("https:");
|
|
72
|
+
}
|
|
73
|
+
allowSelfSignup() {
|
|
74
|
+
return this.deps.allowSelfSignup === true;
|
|
75
|
+
}
|
|
76
|
+
/** Legacy engine: primary root exists but no auth account has been claimed yet. */
|
|
77
|
+
async needsAuthMigration() {
|
|
78
|
+
return ((await this.accountCount()) === 0 &&
|
|
79
|
+
this.deps.getPrimaryRootPicoId() !== null);
|
|
80
|
+
}
|
|
81
|
+
// ---- legacy migration: claim the existing primary root ----
|
|
82
|
+
async claimPrimaryRootOptions(input) {
|
|
83
|
+
const rootPicoId = this.deps.getPrimaryRootPicoId();
|
|
84
|
+
if (!rootPicoId) {
|
|
85
|
+
throw new AuthError("No primary root to claim", 404);
|
|
86
|
+
}
|
|
87
|
+
if ((await this.accountCount()) > 0) {
|
|
88
|
+
throw new AuthError("This engine already has an account", 403);
|
|
89
|
+
}
|
|
90
|
+
const { rpID, rpName } = this.rp();
|
|
91
|
+
const accountId = randomId();
|
|
92
|
+
const displayName = await this.resolveRegistrationDisplayName(accountId, input.displayName);
|
|
93
|
+
const { userName, userDisplayName } = this.webauthnUserFields(displayName);
|
|
94
|
+
const options = await this.deps.webauthn.generateRegistrationOptions({
|
|
95
|
+
rpID,
|
|
96
|
+
rpName,
|
|
97
|
+
userID: new TextEncoder().encode(accountId),
|
|
98
|
+
userName,
|
|
99
|
+
userDisplayName,
|
|
100
|
+
excludeCredentials: [],
|
|
101
|
+
});
|
|
102
|
+
const ceremonyId = this.putCeremony({
|
|
103
|
+
type: "claim",
|
|
104
|
+
challenge: options.challenge,
|
|
105
|
+
accountId,
|
|
106
|
+
displayName,
|
|
107
|
+
rootPicoId,
|
|
108
|
+
expiresAt: Date.now() + CHALLENGE_TTL_MS,
|
|
109
|
+
});
|
|
110
|
+
return { options, ceremonyId };
|
|
111
|
+
}
|
|
112
|
+
async claimPrimaryRootVerify(input) {
|
|
113
|
+
const cer = this.takeCeremony(input.ceremonyId, "claim");
|
|
114
|
+
if ((await this.accountCount()) > 0) {
|
|
115
|
+
throw new AuthError("This engine already has an account", 403);
|
|
116
|
+
}
|
|
117
|
+
const primaryRootId = this.deps.getPrimaryRootPicoId();
|
|
118
|
+
if (!primaryRootId || primaryRootId !== cer.rootPicoId) {
|
|
119
|
+
throw new AuthError("Primary root is no longer available to claim", 409);
|
|
120
|
+
}
|
|
121
|
+
const { rpID, origin } = this.rp();
|
|
122
|
+
const result = await this.deps.webauthn.verifyRegistration({
|
|
123
|
+
response: input.response,
|
|
124
|
+
expectedChallenge: cer.challenge,
|
|
125
|
+
expectedOrigin: origin,
|
|
126
|
+
expectedRPID: rpID,
|
|
127
|
+
});
|
|
128
|
+
if (!result.verified || !result.credential) {
|
|
129
|
+
throw new AuthError("Registration could not be verified", 400);
|
|
130
|
+
}
|
|
131
|
+
const uiECI = await this.deps.getUiECI(cer.rootPicoId);
|
|
132
|
+
if (!uiECI) {
|
|
133
|
+
throw new AuthError("Primary root has no UI channel", 500);
|
|
134
|
+
}
|
|
135
|
+
const account = {
|
|
136
|
+
accountId: cer.accountId,
|
|
137
|
+
rootPicoId: cer.rootPicoId,
|
|
138
|
+
displayName: cer.displayName,
|
|
139
|
+
createdAt: new Date().toISOString(),
|
|
140
|
+
};
|
|
141
|
+
await this.deps.db.put([ACCOUNT_PREFIX, account.accountId], account);
|
|
142
|
+
await this.storeCredential(account.accountId, result.credential, cer.displayName);
|
|
143
|
+
if (this.deps.onAccountClaimed) {
|
|
144
|
+
await this.deps.onAccountClaimed(cer.rootPicoId, cer.displayName);
|
|
145
|
+
}
|
|
146
|
+
const session = await this.createSession(account.accountId, account.rootPicoId, result.credential.id);
|
|
147
|
+
return { account, session, uiECI };
|
|
148
|
+
}
|
|
149
|
+
// ---- registration invites ----
|
|
150
|
+
async createInvite(input) {
|
|
151
|
+
var _a;
|
|
152
|
+
const bootstrapUrl = (input.bootstrapUrl || "").trim() || undefined;
|
|
153
|
+
let bootstrapRid;
|
|
154
|
+
if (bootstrapUrl) {
|
|
155
|
+
try {
|
|
156
|
+
const flushed = await this.deps.flushBootstrapUrl(bootstrapUrl);
|
|
157
|
+
bootstrapRid = flushed.rid;
|
|
158
|
+
}
|
|
159
|
+
catch (err) {
|
|
160
|
+
const detail = err instanceof Error ? err.message : "Bootstrap ruleset URL could not be loaded";
|
|
161
|
+
throw new AuthError(`Invalid bootstrap ruleset URL: ${detail}`, 400);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
const token = randomToken();
|
|
165
|
+
const now = Date.now();
|
|
166
|
+
const ttl = (_a = this.deps.inviteTtlMs) !== null && _a !== void 0 ? _a : DEFAULT_INVITE_TTL_MS;
|
|
167
|
+
const invite = {
|
|
168
|
+
token,
|
|
169
|
+
createdByAccountId: input.createdByAccountId,
|
|
170
|
+
label: (input.label || "").trim() || undefined,
|
|
171
|
+
bootstrapUrl,
|
|
172
|
+
bootstrapRid,
|
|
173
|
+
createdAt: new Date(now).toISOString(),
|
|
174
|
+
expiresAt: now + ttl,
|
|
175
|
+
};
|
|
176
|
+
await this.deps.db.put([INVITE_PREFIX, token], invite);
|
|
177
|
+
return {
|
|
178
|
+
token: invite.token,
|
|
179
|
+
label: invite.label,
|
|
180
|
+
bootstrapUrl: invite.bootstrapUrl,
|
|
181
|
+
bootstrapRid: invite.bootstrapRid,
|
|
182
|
+
createdAt: invite.createdAt,
|
|
183
|
+
expiresAt: invite.expiresAt,
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
async peekInvite(token) {
|
|
187
|
+
const invite = await this.getInvite(token);
|
|
188
|
+
if (!invite) {
|
|
189
|
+
return { valid: false };
|
|
190
|
+
}
|
|
191
|
+
if (invite.expiresAt < Date.now()) {
|
|
192
|
+
return { valid: false };
|
|
193
|
+
}
|
|
194
|
+
return {
|
|
195
|
+
valid: true,
|
|
196
|
+
label: invite.label,
|
|
197
|
+
bootstrapUrl: invite.bootstrapUrl,
|
|
198
|
+
bootstrapRid: invite.bootstrapRid,
|
|
199
|
+
expiresAt: invite.expiresAt,
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
// ---- new-account registration (unauthenticated / bootstrap / invited) ----
|
|
203
|
+
async registerNewAccountOptions(input) {
|
|
204
|
+
if (await this.needsAuthMigration()) {
|
|
205
|
+
throw new AuthError("This engine has an existing mesh. Claim it with a passkey instead of registering a new root.", 403);
|
|
206
|
+
}
|
|
207
|
+
const inviteToken = (input.invite || "").trim() || undefined;
|
|
208
|
+
await this.assertRegistrationAllowed(inviteToken);
|
|
209
|
+
const { rpID, rpName } = this.rp();
|
|
210
|
+
const accountId = randomId();
|
|
211
|
+
const displayName = await this.resolveRegistrationDisplayName(accountId, input.displayName, inviteToken);
|
|
212
|
+
const { userName, userDisplayName } = this.webauthnUserFields(displayName);
|
|
213
|
+
const options = await this.deps.webauthn.generateRegistrationOptions({
|
|
214
|
+
rpID,
|
|
215
|
+
rpName,
|
|
216
|
+
userID: new TextEncoder().encode(accountId),
|
|
217
|
+
userName,
|
|
218
|
+
userDisplayName,
|
|
219
|
+
excludeCredentials: [],
|
|
220
|
+
});
|
|
221
|
+
const ceremonyId = this.putCeremony({
|
|
222
|
+
type: "register",
|
|
223
|
+
challenge: options.challenge,
|
|
224
|
+
accountId,
|
|
225
|
+
displayName,
|
|
226
|
+
inviteToken,
|
|
227
|
+
expiresAt: Date.now() + CHALLENGE_TTL_MS,
|
|
228
|
+
});
|
|
229
|
+
return { options, ceremonyId };
|
|
230
|
+
}
|
|
231
|
+
async registerNewAccountVerify(input) {
|
|
232
|
+
if (await this.needsAuthMigration()) {
|
|
233
|
+
throw new AuthError("This engine has an existing mesh. Claim it with a passkey instead of registering a new root.", 403);
|
|
234
|
+
}
|
|
235
|
+
const cer = this.takeCeremony(input.ceremonyId, "register");
|
|
236
|
+
await this.assertRegistrationAllowed(cer.inviteToken);
|
|
237
|
+
const { rpID, origin } = this.rp();
|
|
238
|
+
const result = await this.deps.webauthn.verifyRegistration({
|
|
239
|
+
response: input.response,
|
|
240
|
+
expectedChallenge: cer.challenge,
|
|
241
|
+
expectedOrigin: origin,
|
|
242
|
+
expectedRPID: rpID,
|
|
243
|
+
});
|
|
244
|
+
if (!result.verified || !result.credential) {
|
|
245
|
+
throw new AuthError("Registration could not be verified", 400);
|
|
246
|
+
}
|
|
247
|
+
let bootstrapUrl;
|
|
248
|
+
if (cer.inviteToken) {
|
|
249
|
+
const invite = await this.getInvite(cer.inviteToken);
|
|
250
|
+
bootstrapUrl = invite === null || invite === void 0 ? void 0 : invite.bootstrapUrl;
|
|
251
|
+
}
|
|
252
|
+
// Provision the account's root only after the ceremony verifies.
|
|
253
|
+
const { rootPicoId, uiECI } = await this.deps.provisionRoot({
|
|
254
|
+
name: cer.displayName,
|
|
255
|
+
bootstrapUrl,
|
|
256
|
+
});
|
|
257
|
+
const account = {
|
|
258
|
+
accountId: cer.accountId,
|
|
259
|
+
rootPicoId,
|
|
260
|
+
displayName: cer.displayName,
|
|
261
|
+
createdAt: new Date().toISOString(),
|
|
262
|
+
};
|
|
263
|
+
await this.deps.db.put([ACCOUNT_PREFIX, account.accountId], account);
|
|
264
|
+
await this.storeCredential(account.accountId, result.credential, cer.displayName);
|
|
265
|
+
if (cer.inviteToken) {
|
|
266
|
+
await this.consumeInvite(cer.inviteToken);
|
|
267
|
+
}
|
|
268
|
+
const session = await this.createSession(account.accountId, rootPicoId, result.credential.id);
|
|
269
|
+
return { account, session, uiECI };
|
|
270
|
+
}
|
|
271
|
+
// ---- usernameless login ----
|
|
272
|
+
async loginOptions() {
|
|
273
|
+
const { rpID } = this.rp();
|
|
274
|
+
const options = await this.deps.webauthn.generateAuthenticationOptions({ rpID });
|
|
275
|
+
const ceremonyId = this.putCeremony({
|
|
276
|
+
type: "login",
|
|
277
|
+
challenge: options.challenge,
|
|
278
|
+
expiresAt: Date.now() + CHALLENGE_TTL_MS,
|
|
279
|
+
});
|
|
280
|
+
return { options, ceremonyId };
|
|
281
|
+
}
|
|
282
|
+
async loginVerify(input) {
|
|
283
|
+
const cer = this.takeCeremony(input.ceremonyId, "login");
|
|
284
|
+
const credentialID = input.response.id;
|
|
285
|
+
const stored = await this.getCredential(credentialID);
|
|
286
|
+
if (!stored) {
|
|
287
|
+
throw new AuthError("Unknown credential", 400);
|
|
288
|
+
}
|
|
289
|
+
const { rpID, origin } = this.rp();
|
|
290
|
+
const result = await this.deps.webauthn.verifyAuthentication({
|
|
291
|
+
response: input.response,
|
|
292
|
+
expectedChallenge: cer.challenge,
|
|
293
|
+
expectedOrigin: origin,
|
|
294
|
+
expectedRPID: rpID,
|
|
295
|
+
credential: storedToKey(stored),
|
|
296
|
+
});
|
|
297
|
+
if (!result.verified) {
|
|
298
|
+
throw new AuthError("Authentication could not be verified", 400);
|
|
299
|
+
}
|
|
300
|
+
stored.counter = result.newCounter;
|
|
301
|
+
await this.deps.db.put([CREDENTIAL_PREFIX, credentialID], stored);
|
|
302
|
+
const account = await this.getAccount(stored.accountId);
|
|
303
|
+
if (!account) {
|
|
304
|
+
throw new AuthError("Account no longer exists", 400);
|
|
305
|
+
}
|
|
306
|
+
const session = await this.createSession(account.accountId, account.rootPicoId, credentialID);
|
|
307
|
+
return { account, session };
|
|
308
|
+
}
|
|
309
|
+
// ---- add / remove passkeys on an existing account (session-gated) ----
|
|
310
|
+
async addCredentialOptions(input) {
|
|
311
|
+
const account = await this.getAccount(input.accountId);
|
|
312
|
+
if (!account) {
|
|
313
|
+
throw new AuthError("Account not found", 404);
|
|
314
|
+
}
|
|
315
|
+
const { rpID, rpName } = this.rp();
|
|
316
|
+
const existing = await this.listStoredCredentials(input.accountId);
|
|
317
|
+
const { userName, userDisplayName } = this.webauthnUserFields(account.displayName);
|
|
318
|
+
const options = await this.deps.webauthn.generateRegistrationOptions({
|
|
319
|
+
rpID,
|
|
320
|
+
rpName,
|
|
321
|
+
userID: new TextEncoder().encode(account.accountId),
|
|
322
|
+
userName,
|
|
323
|
+
userDisplayName,
|
|
324
|
+
excludeCredentials: existing.map((c) => ({
|
|
325
|
+
id: c.credentialID,
|
|
326
|
+
transports: c.transports,
|
|
327
|
+
})),
|
|
328
|
+
additionalPasskey: true,
|
|
329
|
+
});
|
|
330
|
+
const ceremonyId = this.putCeremony({
|
|
331
|
+
type: "addcred",
|
|
332
|
+
challenge: options.challenge,
|
|
333
|
+
accountId: account.accountId,
|
|
334
|
+
expiresAt: Date.now() + CHALLENGE_TTL_MS,
|
|
335
|
+
});
|
|
336
|
+
return { options, ceremonyId };
|
|
337
|
+
}
|
|
338
|
+
async addCredentialVerify(input) {
|
|
339
|
+
const cer = this.takeCeremony(input.ceremonyId, "addcred");
|
|
340
|
+
if (cer.accountId !== input.accountId) {
|
|
341
|
+
throw new AuthError("Ceremony does not match this account", 403);
|
|
342
|
+
}
|
|
343
|
+
const { rpID, origin } = this.rp();
|
|
344
|
+
const result = await this.deps.webauthn.verifyRegistration({
|
|
345
|
+
response: input.response,
|
|
346
|
+
expectedChallenge: cer.challenge,
|
|
347
|
+
expectedOrigin: origin,
|
|
348
|
+
expectedRPID: rpID,
|
|
349
|
+
});
|
|
350
|
+
if (!result.verified || !result.credential) {
|
|
351
|
+
throw new AuthError("Registration could not be verified", 400);
|
|
352
|
+
}
|
|
353
|
+
const existing = await this.listStoredCredentials(input.accountId);
|
|
354
|
+
if (existing.some((c) => c.credentialID === result.credential.id)) {
|
|
355
|
+
throw new AuthError("This passkey is already registered for your account. To add another, enroll from a different device or security key.", 409);
|
|
356
|
+
}
|
|
357
|
+
// Reuses the existing account/user-handle — does NOT create a new root.
|
|
358
|
+
const stored = await this.storeCredential(input.accountId, result.credential, (input.label || "").trim() || "passkey");
|
|
359
|
+
return { credential: toCredentialInfo(stored) };
|
|
360
|
+
}
|
|
361
|
+
async deleteCredential(accountId, credentialID) {
|
|
362
|
+
const creds = await this.listStoredCredentials(accountId);
|
|
363
|
+
if (creds.length <= 1) {
|
|
364
|
+
throw new AuthError("Cannot remove the last passkey for an account", 409);
|
|
365
|
+
}
|
|
366
|
+
const target = creds.find((c) => c.credentialID === credentialID);
|
|
367
|
+
if (!target) {
|
|
368
|
+
throw new AuthError("Credential not found", 404);
|
|
369
|
+
}
|
|
370
|
+
await this.deps.db.del([CREDENTIAL_PREFIX, credentialID]);
|
|
371
|
+
}
|
|
372
|
+
async listCredentials(accountId) {
|
|
373
|
+
const creds = await this.listStoredCredentials(accountId);
|
|
374
|
+
return creds.map(toCredentialInfo);
|
|
375
|
+
}
|
|
376
|
+
// ---- sessions ----
|
|
377
|
+
async createSession(accountId, rootPicoId, credentialID) {
|
|
378
|
+
const session = {
|
|
379
|
+
token: randomToken(),
|
|
380
|
+
accountId,
|
|
381
|
+
rootPicoId,
|
|
382
|
+
credentialID,
|
|
383
|
+
expiry: Date.now() + this.sessionTtlMs,
|
|
384
|
+
};
|
|
385
|
+
await this.deps.db.put([SESSION_PREFIX, session.token], session);
|
|
386
|
+
return session;
|
|
387
|
+
}
|
|
388
|
+
async getSession(token) {
|
|
389
|
+
if (!token) {
|
|
390
|
+
return null;
|
|
391
|
+
}
|
|
392
|
+
const session = await dbGet(this.deps.db, [SESSION_PREFIX, token]);
|
|
393
|
+
if (!session) {
|
|
394
|
+
return null;
|
|
395
|
+
}
|
|
396
|
+
if (session.expiry < Date.now()) {
|
|
397
|
+
await this.deps.db.del([SESSION_PREFIX, token]);
|
|
398
|
+
return null;
|
|
399
|
+
}
|
|
400
|
+
return session;
|
|
401
|
+
}
|
|
402
|
+
async deleteSession(token) {
|
|
403
|
+
if (token) {
|
|
404
|
+
await this.deps.db.del([SESSION_PREFIX, token]);
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
async whoami(token) {
|
|
408
|
+
const session = await this.getSession(token);
|
|
409
|
+
if (!session) {
|
|
410
|
+
return { authenticated: false };
|
|
411
|
+
}
|
|
412
|
+
const account = await this.getAccount(session.accountId);
|
|
413
|
+
if (!account) {
|
|
414
|
+
return { authenticated: false };
|
|
415
|
+
}
|
|
416
|
+
const uiECI = await this.deps.getUiECI(account.rootPicoId);
|
|
417
|
+
const credentials = await this.listCredentials(account.accountId);
|
|
418
|
+
return {
|
|
419
|
+
authenticated: true,
|
|
420
|
+
accountId: account.accountId,
|
|
421
|
+
rootPicoId: account.rootPicoId,
|
|
422
|
+
displayName: account.displayName,
|
|
423
|
+
uiECI,
|
|
424
|
+
credentials,
|
|
425
|
+
};
|
|
426
|
+
}
|
|
427
|
+
// ---- stores ----
|
|
428
|
+
/** Mesh name for registration: explicit input, else invite label, else generated. */
|
|
429
|
+
async resolveRegistrationDisplayName(accountId, inputDisplayName, inviteToken) {
|
|
430
|
+
const fromInput = (inputDisplayName || "").trim();
|
|
431
|
+
if (fromInput) {
|
|
432
|
+
return fromInput;
|
|
433
|
+
}
|
|
434
|
+
const token = (inviteToken || "").trim();
|
|
435
|
+
if (token) {
|
|
436
|
+
const invite = await this.getInvite(token);
|
|
437
|
+
const fromInvite = ((invite === null || invite === void 0 ? void 0 : invite.label) || "").trim();
|
|
438
|
+
if (fromInvite) {
|
|
439
|
+
return fromInvite;
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
return `pico-${accountId.slice(0, 6)}`;
|
|
443
|
+
}
|
|
444
|
+
/** WebAuthn user.name is what password managers (e.g. 1Password) show alongside the site. */
|
|
445
|
+
webauthnUserFields(displayName) {
|
|
446
|
+
const name = (displayName || "").trim() || "My mesh";
|
|
447
|
+
const originLabel = this.originLabelFromUrl(this.rp().origin);
|
|
448
|
+
const unique = originLabel && !name.includes(originLabel)
|
|
449
|
+
? `${name} (${originLabel})`
|
|
450
|
+
: name;
|
|
451
|
+
return { userName: unique, userDisplayName: unique };
|
|
452
|
+
}
|
|
453
|
+
async assertRegistrationAllowed(inviteToken) {
|
|
454
|
+
const count = await this.accountCount();
|
|
455
|
+
if (count === 0) {
|
|
456
|
+
return;
|
|
457
|
+
}
|
|
458
|
+
if (this.deps.allowSelfSignup === true) {
|
|
459
|
+
return;
|
|
460
|
+
}
|
|
461
|
+
const token = (inviteToken || "").trim();
|
|
462
|
+
if (!token) {
|
|
463
|
+
throw new AuthError("Self-signup is disabled on this engine", 403);
|
|
464
|
+
}
|
|
465
|
+
const invite = await this.getInvite(token);
|
|
466
|
+
if (!invite) {
|
|
467
|
+
throw new AuthError("Invalid or expired invite", 403);
|
|
468
|
+
}
|
|
469
|
+
if (invite.expiresAt < Date.now()) {
|
|
470
|
+
throw new AuthError("Invalid or expired invite", 403);
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
async getInvite(token) {
|
|
474
|
+
const trimmed = (token || "").trim();
|
|
475
|
+
if (!trimmed) {
|
|
476
|
+
return null;
|
|
477
|
+
}
|
|
478
|
+
return dbGet(this.deps.db, [INVITE_PREFIX, trimmed]);
|
|
479
|
+
}
|
|
480
|
+
async consumeInvite(token) {
|
|
481
|
+
const trimmed = (token || "").trim();
|
|
482
|
+
if (!trimmed) {
|
|
483
|
+
return;
|
|
484
|
+
}
|
|
485
|
+
await this.deps.db.del([INVITE_PREFIX, trimmed]);
|
|
486
|
+
}
|
|
487
|
+
async getAccount(accountId) {
|
|
488
|
+
return dbGet(this.deps.db, [ACCOUNT_PREFIX, accountId]);
|
|
489
|
+
}
|
|
490
|
+
async accountCount() {
|
|
491
|
+
const rows = await dbList(this.deps.db, [ACCOUNT_PREFIX]);
|
|
492
|
+
return rows.length;
|
|
493
|
+
}
|
|
494
|
+
async getCredential(credentialID) {
|
|
495
|
+
return dbGet(this.deps.db, [CREDENTIAL_PREFIX, credentialID]);
|
|
496
|
+
}
|
|
497
|
+
async listStoredCredentials(accountId) {
|
|
498
|
+
const rows = await dbList(this.deps.db, [CREDENTIAL_PREFIX]);
|
|
499
|
+
return rows
|
|
500
|
+
.map((r) => r.value)
|
|
501
|
+
.filter((c) => c && c.accountId === accountId);
|
|
502
|
+
}
|
|
503
|
+
async storeCredential(accountId, key, label) {
|
|
504
|
+
const stored = {
|
|
505
|
+
credentialID: key.id,
|
|
506
|
+
accountId,
|
|
507
|
+
publicKey: Buffer.from(key.publicKey).toString("base64url"),
|
|
508
|
+
counter: key.counter,
|
|
509
|
+
transports: key.transports || [],
|
|
510
|
+
label,
|
|
511
|
+
createdAt: new Date().toISOString(),
|
|
512
|
+
};
|
|
513
|
+
await this.deps.db.put([CREDENTIAL_PREFIX, stored.credentialID], stored);
|
|
514
|
+
return stored;
|
|
515
|
+
}
|
|
516
|
+
// ---- challenge store (in-memory, per ceremony) ----
|
|
517
|
+
putCeremony(cer) {
|
|
518
|
+
this.pruneCeremonies();
|
|
519
|
+
const ceremonyId = randomId();
|
|
520
|
+
this.challenges.set(ceremonyId, cer);
|
|
521
|
+
return ceremonyId;
|
|
522
|
+
}
|
|
523
|
+
takeCeremony(ceremonyId, type) {
|
|
524
|
+
const cer = ceremonyId ? this.challenges.get(ceremonyId) : undefined;
|
|
525
|
+
if (!cer || cer.type !== type) {
|
|
526
|
+
throw new AuthError("Missing or invalid challenge; restart the ceremony", 400);
|
|
527
|
+
}
|
|
528
|
+
this.challenges.delete(ceremonyId);
|
|
529
|
+
if (cer.expiresAt < Date.now()) {
|
|
530
|
+
throw new AuthError("Challenge expired; restart the ceremony", 400);
|
|
531
|
+
}
|
|
532
|
+
return cer;
|
|
533
|
+
}
|
|
534
|
+
pruneCeremonies() {
|
|
535
|
+
const now = Date.now();
|
|
536
|
+
for (const [id, cer] of this.challenges) {
|
|
537
|
+
if (cer.expiresAt < now) {
|
|
538
|
+
this.challenges.delete(id);
|
|
539
|
+
}
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
exports.AuthService = AuthService;
|
|
544
|
+
function toCredentialInfo(c) {
|
|
545
|
+
return {
|
|
546
|
+
credentialID: c.credentialID,
|
|
547
|
+
label: c.label,
|
|
548
|
+
transports: c.transports,
|
|
549
|
+
createdAt: c.createdAt,
|
|
550
|
+
};
|
|
551
|
+
}
|
|
552
|
+
function storedToKey(c) {
|
|
553
|
+
return {
|
|
554
|
+
id: c.credentialID,
|
|
555
|
+
publicKey: new Uint8Array(Buffer.from(c.publicKey, "base64url")),
|
|
556
|
+
counter: c.counter,
|
|
557
|
+
transports: c.transports,
|
|
558
|
+
};
|
|
559
|
+
}
|
|
560
|
+
function randomId() {
|
|
561
|
+
return crypto.randomBytes(16).toString("base64url");
|
|
562
|
+
}
|
|
563
|
+
function randomToken() {
|
|
564
|
+
return crypto.randomBytes(32).toString("base64url");
|
|
565
|
+
}
|
|
566
|
+
async function dbGet(db, key) {
|
|
567
|
+
try {
|
|
568
|
+
const value = await db.get(key);
|
|
569
|
+
return value === undefined ? null : value;
|
|
570
|
+
}
|
|
571
|
+
catch (err) {
|
|
572
|
+
if (err && (err.notFound || err.code === "LEVEL_NOT_FOUND")) {
|
|
573
|
+
return null;
|
|
574
|
+
}
|
|
575
|
+
throw err;
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
async function dbList(db, prefix) {
|
|
579
|
+
var _a, e_1, _b, _c;
|
|
580
|
+
const out = [];
|
|
581
|
+
const iter = db.iterator({
|
|
582
|
+
gte: prefix,
|
|
583
|
+
lte: prefix.concat([undefined]),
|
|
584
|
+
});
|
|
585
|
+
try {
|
|
586
|
+
for (var _d = true, iter_1 = __asyncValues(iter), iter_1_1; iter_1_1 = await iter_1.next(), _a = iter_1_1.done, !_a; _d = true) {
|
|
587
|
+
_c = iter_1_1.value;
|
|
588
|
+
_d = false;
|
|
589
|
+
const [key, value] = _c;
|
|
590
|
+
out.push({ key, value });
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
594
|
+
finally {
|
|
595
|
+
try {
|
|
596
|
+
if (!_d && !_a && (_b = iter_1.return)) await _b.call(iter_1);
|
|
597
|
+
}
|
|
598
|
+
finally { if (e_1) throw e_1.error; }
|
|
599
|
+
}
|
|
600
|
+
return out;
|
|
601
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.makeWebAuthnAdapter = exports.AuthError = exports.AuthService = void 0;
|
|
4
|
+
var AuthService_1 = require("./AuthService");
|
|
5
|
+
Object.defineProperty(exports, "AuthService", { enumerable: true, get: function () { return AuthService_1.AuthService; } });
|
|
6
|
+
Object.defineProperty(exports, "AuthError", { enumerable: true, get: function () { return AuthService_1.AuthError; } });
|
|
7
|
+
var webauthn_1 = require("./webauthn");
|
|
8
|
+
Object.defineProperty(exports, "makeWebAuthnAdapter", { enumerable: true, get: function () { return webauthn_1.makeWebAuthnAdapter; } });
|