@bongos/core 1.21.135 → 1.21.136
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/.bongos-core.json +34 -14
- package/docs/architecture.md +10 -0
- package/docs/module-api-changelog.md +2 -0
- package/migrations/core_277_passkey_foundation.sql +53 -0
- package/package-lock.json +2 -2
- package/package.json +1 -1
- package/release-notes.json +6 -0
- package/scripts/gds/run-unit-tests.js +3 -0
- package/src/bongos/passkeys.js +143 -0
- package/src/module-api.js +1 -1
- package/tests/passkey_foundation_db.mjs +90 -0
- package/tests/passkey_pure.mjs +24 -0
package/.bongos-core.json
CHANGED
|
@@ -2,22 +2,22 @@
|
|
|
2
2
|
"artifact": "bongos-core",
|
|
3
3
|
"manifest_schema": 1,
|
|
4
4
|
"generator": "scripts/gds/package-core.js",
|
|
5
|
-
"core_version": "1.21.
|
|
6
|
-
"core_contract": "1.21.
|
|
7
|
-
"source_commit": "
|
|
5
|
+
"core_version": "1.21.136",
|
|
6
|
+
"core_contract": "1.21.136",
|
|
7
|
+
"source_commit": "e4b8cee4f6222b363f9b2e8aa1ceb79c6426a2eb",
|
|
8
8
|
"source_ref": "HEAD",
|
|
9
|
-
"built_at": "2026-10-
|
|
9
|
+
"built_at": "2026-10-15T23:35:12.033Z",
|
|
10
10
|
"redaction": {
|
|
11
11
|
"model": "docs-redacted+functional-verbatim",
|
|
12
12
|
"docs_redacted": 608,
|
|
13
13
|
"agent_docs_stubbed": 28,
|
|
14
|
-
"functional_verbatim":
|
|
14
|
+
"functional_verbatim": 3153,
|
|
15
15
|
"rules": 3,
|
|
16
16
|
"gate_literals": 3,
|
|
17
17
|
"gate": "passed"
|
|
18
18
|
},
|
|
19
|
-
"file_count":
|
|
20
|
-
"tree_sha256": "
|
|
19
|
+
"file_count": 3790,
|
|
20
|
+
"tree_sha256": "2bb4525360079b817aa7bc06f384d604867c6896a6a2caa355d01487669160c2",
|
|
21
21
|
"files": [
|
|
22
22
|
{
|
|
23
23
|
"path": ".claude/skills/ask-for-help/SKILL.md",
|
|
@@ -2392,7 +2392,7 @@
|
|
|
2392
2392
|
{
|
|
2393
2393
|
"path": "docs/architecture.md",
|
|
2394
2394
|
"mode": "0000644",
|
|
2395
|
-
"sha256": "
|
|
2395
|
+
"sha256": "3850775c198edeece6ac5497a9f5788421a8082e21edb496a4b00a3fc7a6eb3d"
|
|
2396
2396
|
},
|
|
2397
2397
|
{
|
|
2398
2398
|
"path": "docs/branding-contract.md",
|
|
@@ -2927,7 +2927,7 @@
|
|
|
2927
2927
|
{
|
|
2928
2928
|
"path": "docs/module-api-changelog.md",
|
|
2929
2929
|
"mode": "0000644",
|
|
2930
|
-
"sha256": "
|
|
2930
|
+
"sha256": "72c0a2b3d8e44eb7ae5e0b0b9c27eb4c70055f9eb8755496c9f174db024fec57"
|
|
2931
2931
|
},
|
|
2932
2932
|
{
|
|
2933
2933
|
"path": "docs/modules-contract.md",
|
|
@@ -4164,6 +4164,11 @@
|
|
|
4164
4164
|
"mode": "0000644",
|
|
4165
4165
|
"sha256": "550884fa117e707b65f5c56faca0e4e3850905db94d3f275cad5d520aa68db03"
|
|
4166
4166
|
},
|
|
4167
|
+
{
|
|
4168
|
+
"path": "migrations/core_277_passkey_foundation.sql",
|
|
4169
|
+
"mode": "0000644",
|
|
4170
|
+
"sha256": "52ebc3ce9c3e006ef2dfe0d85c9a97c53896f7b0d8f8fb700a1488a24c7bcb3c"
|
|
4171
|
+
},
|
|
4167
4172
|
{
|
|
4168
4173
|
"path": "modules/agents/CLAUDE.md",
|
|
4169
4174
|
"mode": "0000644",
|
|
@@ -10522,12 +10527,12 @@
|
|
|
10522
10527
|
{
|
|
10523
10528
|
"path": "package-lock.json",
|
|
10524
10529
|
"mode": "0000644",
|
|
10525
|
-
"sha256": "
|
|
10530
|
+
"sha256": "ad4055f1eef9ede69c3eda6f957baa66186ec383ac30b9f3b411302d651aae2e"
|
|
10526
10531
|
},
|
|
10527
10532
|
{
|
|
10528
10533
|
"path": "package.json",
|
|
10529
10534
|
"mode": "0000644",
|
|
10530
|
-
"sha256": "
|
|
10535
|
+
"sha256": "af3b71b069ad704356796bf041f8a2798a07e9c20aca50aaf3758c0fb311f8ca"
|
|
10531
10536
|
},
|
|
10532
10537
|
{
|
|
10533
10538
|
"path": "public-docs/index.html",
|
|
@@ -10547,7 +10552,7 @@
|
|
|
10547
10552
|
{
|
|
10548
10553
|
"path": "release-notes.json",
|
|
10549
10554
|
"mode": "0000644",
|
|
10550
|
-
"sha256": "
|
|
10555
|
+
"sha256": "b170ded56d5ca6cc029de29e1ceade0100ef20de335fa810f4aa0d220012fc84"
|
|
10551
10556
|
},
|
|
10552
10557
|
{
|
|
10553
10558
|
"path": "scripts/cron/style_guide_audit.sh",
|
|
@@ -11832,7 +11837,7 @@
|
|
|
11832
11837
|
{
|
|
11833
11838
|
"path": "scripts/gds/run-unit-tests.js",
|
|
11834
11839
|
"mode": "0000644",
|
|
11835
|
-
"sha256": "
|
|
11840
|
+
"sha256": "213d8e4199ab429e64d0c65f4eda42a9d0cb4c64baa1424f793e223af3e66308"
|
|
11836
11841
|
},
|
|
11837
11842
|
{
|
|
11838
11843
|
"path": "scripts/gds/runner-drift.js",
|
|
@@ -12689,6 +12694,11 @@
|
|
|
12689
12694
|
"mode": "0000644",
|
|
12690
12695
|
"sha256": "b2f6d65e3463052609e51d135ea560370104d0457a0e7e86bf9b7120fe1cddfb"
|
|
12691
12696
|
},
|
|
12697
|
+
{
|
|
12698
|
+
"path": "src/bongos/passkeys.js",
|
|
12699
|
+
"mode": "0000644",
|
|
12700
|
+
"sha256": "2fe8018148a70b21515352084e3bc9047da0ee4749e27df6f68c93ba29a2368a"
|
|
12701
|
+
},
|
|
12692
12702
|
{
|
|
12693
12703
|
"path": "src/bongos/path-match.js",
|
|
12694
12704
|
"mode": "0000644",
|
|
@@ -12902,7 +12912,7 @@
|
|
|
12902
12912
|
{
|
|
12903
12913
|
"path": "src/module-api.js",
|
|
12904
12914
|
"mode": "0000644",
|
|
12905
|
-
"sha256": "
|
|
12915
|
+
"sha256": "5968c611fba761d24e4118debb2aeb34772640722e0306a0c61a351bf7455f0f"
|
|
12906
12916
|
},
|
|
12907
12917
|
{
|
|
12908
12918
|
"path": "src/module-loader/catalog.js",
|
|
@@ -16859,6 +16869,16 @@
|
|
|
16859
16869
|
"mode": "0000644",
|
|
16860
16870
|
"sha256": "fd69c44ebf3d246ecedf527275448389cca809e231d1c6cb34e4c12bfdc38111"
|
|
16861
16871
|
},
|
|
16872
|
+
{
|
|
16873
|
+
"path": "tests/passkey_foundation_db.mjs",
|
|
16874
|
+
"mode": "0000644",
|
|
16875
|
+
"sha256": "5ff76c608f9f7b687e6b65af6bc667bdb42a21d520425b16bf15d36ca03848d9"
|
|
16876
|
+
},
|
|
16877
|
+
{
|
|
16878
|
+
"path": "tests/passkey_pure.mjs",
|
|
16879
|
+
"mode": "0000644",
|
|
16880
|
+
"sha256": "d0466c6a46c2eadd023801640d5da2d395d68cab8c8a9a46eee9b2e88e8c5182"
|
|
16881
|
+
},
|
|
16862
16882
|
{
|
|
16863
16883
|
"path": "tests/paste_token_api_base.mjs",
|
|
16864
16884
|
"mode": "0000644",
|
package/docs/architecture.md
CHANGED
|
@@ -251,6 +251,16 @@ store_module_prices(id, module_key, price_cents, price_credits, set_by, set_at)
|
|
|
251
251
|
-- in that currency, at least one must be set.
|
|
252
252
|
```
|
|
253
253
|
|
|
254
|
+
Passkey foundation (migration `core_277`, task 1004750, [ADR 0382](adr/0382-a-hardware-card-is-a-passkey-step-up-an-archon-switches-on-per-builder.md) D1/D6 — storage only; the ceremonies and routes are their own tasks; code in `src/bongos/passkeys.js`):
|
|
255
|
+
|
|
256
|
+
```
|
|
257
|
+
passkey_credentials(id, credential_id UNIQUE, builder_id FK, public_key bytea, sign_count,
|
|
258
|
+
transports[], aaguid, nickname, created_at, last_used_at)
|
|
259
|
+
passkey_user_handles(builder_id PK FK, user_handle UNIQUE, created_at) -- opaque, never the email
|
|
260
|
+
passkey_challenges(challenge PK, purpose, builder_id NULL, created_at, expires_at)
|
|
261
|
+
-- single-use (DELETEd on consume), TTL swept; builder_id NULL for sign-in
|
|
262
|
+
```
|
|
263
|
+
|
|
254
264
|
Module release channels (migration `core_276`, task 1003815, [ADR 0344](adr/0344-a-tester-is-a-project-that-opts-in-once.md) — which channel a published version is on; the data only, the tester-first publish, promotion gate and channel-aware install are their own tasks):
|
|
255
265
|
|
|
256
266
|
```
|
|
@@ -3107,5 +3107,7 @@ is load-bearing: the script throws rather than guess if it is missing, and
|
|
|
3107
3107
|
landed since 1.21.133 with no explicit bump. run 38810062389. (task 1002620)
|
|
3108
3108
|
1.21.135 — CI auto-patch (publish-on-merge, ADR 0161): carrier for merges
|
|
3109
3109
|
landed since 1.21.134 with no explicit bump. run 38812449926. (task 1002620)
|
|
3110
|
+
1.21.136 — CI auto-patch (publish-on-merge, ADR 0161): carrier for merges
|
|
3111
|
+
landed since 1.21.135 with no explicit bump. run 38824385358. (task 1002620)
|
|
3110
3112
|
---------------------------------------------------------------------------
|
|
3111
3113
|
```
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
-- core_277 passkey foundation: credentials, per-builder user handle, challenge store
|
|
2
|
+
-- Applied under task 1004750 (ADR 0382 D1, D6).
|
|
3
|
+
--
|
|
4
|
+
-- WHY. A hardware card (a WebAuthn passkey) is a second sign-in step an Archon can
|
|
5
|
+
-- switch on per builder. Before any ceremony route can exist, the server needs three
|
|
6
|
+
-- things to keep: the public half of each registered card, an opaque handle that
|
|
7
|
+
-- stands in for the builder inside the card (never the email), and the challenges it
|
|
8
|
+
-- has issued and not yet seen answered.
|
|
9
|
+
--
|
|
10
|
+
-- SHAPE.
|
|
11
|
+
-- * passkey_credentials one row per registered card. credential_id is the
|
|
12
|
+
-- authenticator's own id (base64url) and is UNIQUE: a card can belong to one
|
|
13
|
+
-- builder. sign_count is kept so a counter that goes backwards can be caught.
|
|
14
|
+
-- * passkey_user_handles one random handle per builder, minted on first use.
|
|
15
|
+
-- * passkey_challenges short-lived, single-use. A row is DELETED when consumed;
|
|
16
|
+
-- expires_at lets a sweep remove the ones nobody answered. builder_id is nullable
|
|
17
|
+
-- because a sign-in ceremony starts before the builder is known.
|
|
18
|
+
|
|
19
|
+
CREATE TABLE IF NOT EXISTS passkey_credentials (
|
|
20
|
+
id bigserial PRIMARY KEY,
|
|
21
|
+
credential_id text NOT NULL UNIQUE,
|
|
22
|
+
builder_id bigint NOT NULL REFERENCES builders(id) ON DELETE CASCADE,
|
|
23
|
+
public_key bytea NOT NULL,
|
|
24
|
+
sign_count bigint NOT NULL DEFAULT 0,
|
|
25
|
+
transports text[] NOT NULL DEFAULT '{}',
|
|
26
|
+
aaguid text,
|
|
27
|
+
nickname text,
|
|
28
|
+
created_at timestamptz NOT NULL DEFAULT now(),
|
|
29
|
+
last_used_at timestamptz
|
|
30
|
+
);
|
|
31
|
+
|
|
32
|
+
CREATE INDEX IF NOT EXISTS idx_passkey_credentials_builder
|
|
33
|
+
ON passkey_credentials (builder_id);
|
|
34
|
+
|
|
35
|
+
CREATE TABLE IF NOT EXISTS passkey_user_handles (
|
|
36
|
+
builder_id bigint PRIMARY KEY REFERENCES builders(id) ON DELETE CASCADE,
|
|
37
|
+
user_handle text NOT NULL UNIQUE,
|
|
38
|
+
created_at timestamptz NOT NULL DEFAULT now()
|
|
39
|
+
);
|
|
40
|
+
|
|
41
|
+
CREATE TABLE IF NOT EXISTS passkey_challenges (
|
|
42
|
+
challenge text PRIMARY KEY,
|
|
43
|
+
purpose text NOT NULL CHECK (purpose IN ('register', 'authenticate')),
|
|
44
|
+
builder_id bigint REFERENCES builders(id) ON DELETE CASCADE,
|
|
45
|
+
created_at timestamptz NOT NULL DEFAULT now(),
|
|
46
|
+
expires_at timestamptz NOT NULL
|
|
47
|
+
);
|
|
48
|
+
|
|
49
|
+
CREATE INDEX IF NOT EXISTS idx_passkey_challenges_expires
|
|
50
|
+
ON passkey_challenges (expires_at);
|
|
51
|
+
|
|
52
|
+
INSERT INTO schema_migrations (version) VALUES ('core_277_passkey_foundation')
|
|
53
|
+
ON CONFLICT (version) DO NOTHING;
|
package/package-lock.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bongos/core",
|
|
3
|
-
"version": "1.21.
|
|
3
|
+
"version": "1.21.136",
|
|
4
4
|
"lockfileVersion": 3,
|
|
5
5
|
"requires": true,
|
|
6
6
|
"packages": {
|
|
7
7
|
"": {
|
|
8
8
|
"name": "@bongos/core",
|
|
9
|
-
"version": "1.21.
|
|
9
|
+
"version": "1.21.136",
|
|
10
10
|
"license": "AGPL-3.0-or-later",
|
|
11
11
|
"dependencies": {
|
|
12
12
|
"express": "^4.21.2",
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bongos/core",
|
|
3
|
-
"version": "1.21.
|
|
3
|
+
"version": "1.21.136",
|
|
4
4
|
"description": "Cloud Bongos — the AI-first build platform core (GDS + platform surfaces + module system), installed as a versioned dependency (ADR 0108).",
|
|
5
5
|
"license": "AGPL-3.0-or-later",
|
|
6
6
|
"main": "src/platform-server.js",
|
package/release-notes.json
CHANGED
|
@@ -9812,5 +9812,11 @@
|
|
|
9812
9812
|
"id": "1003462",
|
|
9813
9813
|
"text": "Adds an Add-a-task form to the Goals page: pick an open goal, name the task, optional description. Sends POST /tasks unchanged; the server's rank floor, goal/version and closed-goal refusals render as sentences. States that a"
|
|
9814
9814
|
}
|
|
9815
|
+
],
|
|
9816
|
+
"1.21.136": [
|
|
9817
|
+
{
|
|
9818
|
+
"id": "1004750",
|
|
9819
|
+
"text": "Groundwork for hardware-card sign-in: the database tables and storage code that remember registered cards and the short-lived one-time challenges. Nothing is visible to builders yet."
|
|
9820
|
+
}
|
|
9815
9821
|
]
|
|
9816
9822
|
}
|
|
@@ -232,6 +232,9 @@ const INTEGRATION = new Set([
|
|
|
232
232
|
// admission reads' statements run on Postgres, and the hub's notice restamp and
|
|
233
233
|
// listing. The DB-free sibling (application_lifecycle) interprets the same WHEREs.
|
|
234
234
|
'invite_expiry_db',
|
|
235
|
+
// task 1004750 (ADR 0382 D6): passkey tables from the real migration — single-use
|
|
236
|
+
// challenges, TTL sweep, unique credential id, sign-count regression.
|
|
237
|
+
'passkey_foundation_db',
|
|
235
238
|
// task 1004408: migration platform_identity_028 deletes the old hub invite's
|
|
236
239
|
// pending rows before a cutoff it reads from the core_upgrades ledger. Only a real
|
|
237
240
|
// engine runs the DO block, the numeric version compare and the one-shot guard.
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
// src/bongos/passkeys.js — passkey (WebAuthn) storage: credentials, the opaque user
|
|
2
|
+
// handle, and the single-use challenge store (task 1004750, ADR 0382 D1/D6).
|
|
3
|
+
//
|
|
4
|
+
// This file is the foundation only: it keeps and hands back state. The registration
|
|
5
|
+
// and sign-in ceremonies, and the routes that drive them, are their own tasks and
|
|
6
|
+
// build on these functions with @simplewebauthn/server.
|
|
7
|
+
//
|
|
8
|
+
// A challenge is single-use: consumeChallenge DELETEs the row and returns it, so a
|
|
9
|
+
// replay finds nothing. A challenge past its TTL is refused even before the sweep
|
|
10
|
+
// has removed it.
|
|
11
|
+
'use strict';
|
|
12
|
+
|
|
13
|
+
const crypto = require('node:crypto');
|
|
14
|
+
|
|
15
|
+
const CHALLENGE_TTL_SECONDS = 300;
|
|
16
|
+
const MAX_CHALLENGE_TTL_SECONDS = 600;
|
|
17
|
+
const PURPOSES = Object.freeze(['register', 'authenticate']);
|
|
18
|
+
|
|
19
|
+
function defaultPool() { return require('./pool').pool; }
|
|
20
|
+
|
|
21
|
+
function randomToken(bytes) { return crypto.randomBytes(bytes).toString('base64url'); }
|
|
22
|
+
|
|
23
|
+
// A TTL is a positive whole number of seconds no longer than the ceiling, so a caller
|
|
24
|
+
// forwarding an untrusted value cannot mint a long-lived (or already-dead) challenge.
|
|
25
|
+
function clampTtlSeconds(ttl) {
|
|
26
|
+
const n = Math.floor(Number(ttl));
|
|
27
|
+
if (!Number.isFinite(n) || n <= 0) return CHALLENGE_TTL_SECONDS;
|
|
28
|
+
return Math.min(n, MAX_CHALLENGE_TTL_SECONDS);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// The clone check. A counter that does not advance is suspect, unless the card
|
|
32
|
+
// does not keep one at all (stored and reported both zero).
|
|
33
|
+
function counterAdvances(stored, reported) {
|
|
34
|
+
if (stored === 0 && reported === 0) return true;
|
|
35
|
+
return reported > stored;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// Mint and store a challenge. Returns { challenge, expiresAt }.
|
|
39
|
+
async function createChallenge({ purpose, builderId = null, ttlSeconds = CHALLENGE_TTL_SECONDS, pool = defaultPool() } = {}) {
|
|
40
|
+
if (!PURPOSES.includes(purpose)) throw new Error(`passkeys: bad challenge purpose ${JSON.stringify(purpose)}`);
|
|
41
|
+
const challenge = randomToken(32);
|
|
42
|
+
// Housekeeping rides on issuing: expired rows are removed here, so the table is
|
|
43
|
+
// swept by use and needs no separate scheduler.
|
|
44
|
+
await sweepExpiredChallenges({ pool });
|
|
45
|
+
const { rows } = await pool.query(
|
|
46
|
+
`INSERT INTO passkey_challenges (challenge, purpose, builder_id, expires_at)
|
|
47
|
+
VALUES ($1, $2, $3, now() + make_interval(secs => $4::int))
|
|
48
|
+
RETURNING expires_at`,
|
|
49
|
+
[challenge, purpose, builderId, String(clampTtlSeconds(ttlSeconds))],
|
|
50
|
+
);
|
|
51
|
+
return { challenge, expiresAt: rows[0].expires_at };
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// Take a challenge: delete it and return { purpose, builderId } if it was live, else
|
|
55
|
+
// null. Pass expectPurpose (and expectBuilderId for a signed-in registration) and a
|
|
56
|
+
// challenge issued for something else is refused. It is still spent: a wrong-purpose
|
|
57
|
+
// presentation burns it rather than leaving it to be tried again.
|
|
58
|
+
async function consumeChallenge(challenge, { expectPurpose = null, expectBuilderId = null, pool = defaultPool() } = {}) {
|
|
59
|
+
if (typeof challenge !== 'string' || !challenge) return null;
|
|
60
|
+
const { rows } = await pool.query(
|
|
61
|
+
`DELETE FROM passkey_challenges WHERE challenge = $1
|
|
62
|
+
RETURNING purpose, builder_id, (expires_at > now()) AS live`,
|
|
63
|
+
[challenge],
|
|
64
|
+
);
|
|
65
|
+
const r = rows[0];
|
|
66
|
+
if (!r || !r.live) return null;
|
|
67
|
+
if (expectPurpose != null && r.purpose !== expectPurpose) return null;
|
|
68
|
+
if (expectBuilderId != null && String(r.builder_id) !== String(expectBuilderId)) return null;
|
|
69
|
+
return { purpose: r.purpose, builderId: r.builder_id == null ? null : String(r.builder_id) };
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// Remove every challenge past its TTL. Returns the number removed.
|
|
73
|
+
async function sweepExpiredChallenges({ pool = defaultPool() } = {}) {
|
|
74
|
+
const r = await pool.query('DELETE FROM passkey_challenges WHERE expires_at <= now()');
|
|
75
|
+
return r.rowCount;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// The builder's opaque WebAuthn user handle, minted on first use. Never the email.
|
|
79
|
+
async function getOrCreateUserHandle(builderId, { pool = defaultPool() } = {}) {
|
|
80
|
+
const { rows } = await pool.query(
|
|
81
|
+
`INSERT INTO passkey_user_handles (builder_id, user_handle) VALUES ($1, $2)
|
|
82
|
+
ON CONFLICT (builder_id) DO UPDATE SET builder_id = EXCLUDED.builder_id
|
|
83
|
+
RETURNING user_handle`,
|
|
84
|
+
[builderId, randomToken(32)],
|
|
85
|
+
);
|
|
86
|
+
return rows[0].user_handle;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
async function addCredential({ builderId, credentialId, publicKey, signCount = 0, transports = [], aaguid = null, nickname = null }, { pool = defaultPool() } = {}) {
|
|
90
|
+
const { rows } = await pool.query(
|
|
91
|
+
`INSERT INTO passkey_credentials (credential_id, builder_id, public_key, sign_count, transports, aaguid, nickname)
|
|
92
|
+
VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING id, created_at`,
|
|
93
|
+
[credentialId, builderId, Buffer.from(publicKey), signCount, transports, aaguid, nickname],
|
|
94
|
+
);
|
|
95
|
+
return rows[0];
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
async function getCredentialById(credentialId, { pool = defaultPool() } = {}) {
|
|
99
|
+
const { rows } = await pool.query(
|
|
100
|
+
`SELECT id, credential_id, builder_id, public_key, sign_count, transports, aaguid, nickname, created_at, last_used_at
|
|
101
|
+
FROM passkey_credentials WHERE credential_id = $1`,
|
|
102
|
+
[credentialId],
|
|
103
|
+
);
|
|
104
|
+
return rows[0] || null;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
async function listCredentials(builderId, { pool = defaultPool() } = {}) {
|
|
108
|
+
const { rows } = await pool.query(
|
|
109
|
+
`SELECT id, credential_id, transports, aaguid, nickname, created_at, last_used_at
|
|
110
|
+
FROM passkey_credentials WHERE builder_id = $1 ORDER BY created_at, id`,
|
|
111
|
+
[builderId],
|
|
112
|
+
);
|
|
113
|
+
return rows;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// Record a successful use. One guarded UPDATE both checks and writes, so two
|
|
117
|
+
// concurrent assertions carrying the same counter cannot both pass and the stored
|
|
118
|
+
// counter can never be lowered. A counter that does not advance (unless the card keeps
|
|
119
|
+
// none) is a possible clone: nothing is written and the reason says why.
|
|
120
|
+
async function recordUse(credentialId, newSignCount, { pool = defaultPool() } = {}) {
|
|
121
|
+
const r = await pool.query(
|
|
122
|
+
`UPDATE passkey_credentials SET sign_count = $2, last_used_at = now()
|
|
123
|
+
WHERE credential_id = $1 AND (($2 = 0 AND sign_count = 0) OR $2 > sign_count)`,
|
|
124
|
+
[credentialId, newSignCount],
|
|
125
|
+
);
|
|
126
|
+
if (r.rowCount > 0) return { ok: true };
|
|
127
|
+
const cred = await getCredentialById(credentialId, { pool });
|
|
128
|
+
return { ok: false, reason: cred ? 'sign_count_regressed' : 'unknown_credential' };
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
async function removeCredential(builderId, credentialId, { pool = defaultPool() } = {}) {
|
|
132
|
+
const r = await pool.query(
|
|
133
|
+
'DELETE FROM passkey_credentials WHERE builder_id = $1 AND credential_id = $2',
|
|
134
|
+
[builderId, credentialId],
|
|
135
|
+
);
|
|
136
|
+
return r.rowCount > 0;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
module.exports = {
|
|
140
|
+
CHALLENGE_TTL_SECONDS, MAX_CHALLENGE_TTL_SECONDS, PURPOSES, clampTtlSeconds, counterAdvances,
|
|
141
|
+
createChallenge, consumeChallenge, sweepExpiredChallenges,
|
|
142
|
+
getOrCreateUserHandle, addCredential, getCredentialById, listCredentials, recordUse, removeCredential,
|
|
143
|
+
};
|
package/src/module-api.js
CHANGED
|
@@ -75,7 +75,7 @@ const { responsibilityFor, ROLE_RESPONSIBILITIES } = require('./role-responsibil
|
|
|
75
75
|
// MAJOR (see allowBoxScope below): passes the request through untouched.
|
|
76
76
|
function deprecatedNoopMiddleware(_req, _res, next) { next(); }
|
|
77
77
|
|
|
78
|
-
const CORE_VERSION = '1.21.
|
|
78
|
+
const CORE_VERSION = '1.21.136'; // CI auto-patch carrier (ADR 0161); changelog: docs/module-api-changelog.md
|
|
79
79
|
|
|
80
80
|
// A namespaced logger so a module's log lines are attributable + consistent.
|
|
81
81
|
// Usage: const log = api.logger('discord'); log.info('mounted');
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
// tests/passkey_foundation_db.mjs — task 1004750 (ADR 0382 D1, D6). Real DB.
|
|
2
|
+
// The passkey tables built from the real migration in a throwaway schema, and the
|
|
3
|
+
// store's rules: single-use challenges, TTL, sweep, unique credential ids, sign-count.
|
|
4
|
+
// Self-skips without a reachable Postgres.
|
|
5
|
+
// Run: DATABASE_URL=... node tests/passkey_foundation_db.mjs
|
|
6
|
+
import { createRequire } from 'node:module';
|
|
7
|
+
import { readFileSync } from 'node:fs';
|
|
8
|
+
import { fileURLToPath } from 'node:url';
|
|
9
|
+
import { dirname, join } from 'node:path';
|
|
10
|
+
import { strict as assert } from 'node:assert';
|
|
11
|
+
const require = createRequire(import.meta.url);
|
|
12
|
+
const { Pool } = require('pg');
|
|
13
|
+
const here = dirname(fileURLToPath(import.meta.url));
|
|
14
|
+
|
|
15
|
+
const pgPool = new Pool({ connectionString: process.env.DATABASE_URL || undefined, max: 1 });
|
|
16
|
+
let client;
|
|
17
|
+
try { client = await pgPool.connect(); } catch (e) {
|
|
18
|
+
console.log(`passkey_foundation_db.mjs SKIPPED — no reachable Postgres (${e.code || e.message}).`);
|
|
19
|
+
await pgPool.end().catch(() => {});
|
|
20
|
+
process.exit(0);
|
|
21
|
+
}
|
|
22
|
+
const SCHEMA = `t1004750_${process.pid}`;
|
|
23
|
+
let pass = 0;
|
|
24
|
+
const ok = (c, m) => { assert.ok(c, m); pass++; console.log(` ok - ${m}`); };
|
|
25
|
+
|
|
26
|
+
try {
|
|
27
|
+
await client.query(`CREATE SCHEMA ${SCHEMA}`);
|
|
28
|
+
await client.query(`SET search_path TO ${SCHEMA}`);
|
|
29
|
+
await client.query('CREATE TABLE builders (id bigserial PRIMARY KEY)');
|
|
30
|
+
await client.query('CREATE TABLE schema_migrations (version text PRIMARY KEY)');
|
|
31
|
+
await client.query(readFileSync(join(here, '..', 'migrations', 'core_277_passkey_foundation.sql'), 'utf8'));
|
|
32
|
+
await client.query('INSERT INTO builders DEFAULT VALUES');
|
|
33
|
+
await client.query('INSERT INTO builders DEFAULT VALUES');
|
|
34
|
+
|
|
35
|
+
const pk = require('../src/bongos/passkeys.js');
|
|
36
|
+
const o = { pool: client };
|
|
37
|
+
|
|
38
|
+
const c = await pk.createChallenge({ purpose: 'register', builderId: 1, ...o });
|
|
39
|
+
ok(typeof c.challenge === 'string' && c.challenge.length >= 40, 'challenge is long random base64url');
|
|
40
|
+
const got = await pk.consumeChallenge(c.challenge, o);
|
|
41
|
+
ok(got && got.purpose === 'register' && got.builderId === '1', 'a live challenge is consumed once');
|
|
42
|
+
ok((await pk.consumeChallenge(c.challenge, o)) === null, 'a second consume (replay) finds nothing');
|
|
43
|
+
|
|
44
|
+
const expire = (ch) => client.query("UPDATE passkey_challenges SET expires_at = now() - interval '1 second' WHERE challenge = $1", [ch]);
|
|
45
|
+
const old = await pk.createChallenge({ purpose: 'authenticate', ...o });
|
|
46
|
+
await expire(old.challenge);
|
|
47
|
+
ok((await pk.consumeChallenge(old.challenge, o)) === null, 'an expired challenge is refused');
|
|
48
|
+
const stale = await pk.createChallenge({ purpose: 'authenticate', ...o });
|
|
49
|
+
await expire(stale.challenge);
|
|
50
|
+
const live = await pk.createChallenge({ purpose: 'authenticate', ...o });
|
|
51
|
+
ok((await client.query('SELECT 1 FROM passkey_challenges WHERE challenge = $1', [stale.challenge])).rowCount === 0, 'issuing a challenge sweeps expired rows');
|
|
52
|
+
const stale2 = await pk.createChallenge({ purpose: 'authenticate', ...o });
|
|
53
|
+
await expire(stale2.challenge);
|
|
54
|
+
ok((await pk.sweepExpiredChallenges(o)) === 1, 'sweep removes only expired challenges');
|
|
55
|
+
ok((await pk.consumeChallenge(live.challenge, o)) !== null, 'sweep left the live one');
|
|
56
|
+
await assert.rejects(pk.createChallenge({ purpose: 'nope', ...o }));
|
|
57
|
+
pass++; console.log(' ok - unknown purpose rejected');
|
|
58
|
+
await assert.rejects(client.query("INSERT INTO passkey_challenges (challenge, purpose, expires_at) VALUES ('x','bogus', now())"));
|
|
59
|
+
pass++; console.log(' ok - the table itself refuses an unknown purpose');
|
|
60
|
+
|
|
61
|
+
const wrong = await pk.createChallenge({ purpose: 'register', builderId: 1, ...o });
|
|
62
|
+
ok((await pk.consumeChallenge(wrong.challenge, { ...o, expectPurpose: 'authenticate' })) === null, 'a challenge for another purpose is refused');
|
|
63
|
+
ok((await pk.consumeChallenge(wrong.challenge, o)) === null, 'and it is spent by the wrong presentation');
|
|
64
|
+
const b1 = await pk.createChallenge({ purpose: 'register', builderId: 1, ...o });
|
|
65
|
+
ok((await pk.consumeChallenge(b1.challenge, { ...o, expectBuilderId: 2 })) === null, 'a challenge for another builder is refused');
|
|
66
|
+
const b2 = await pk.createChallenge({ purpose: 'register', builderId: 1, ...o });
|
|
67
|
+
ok((await pk.consumeChallenge(b2.challenge, { ...o, expectPurpose: 'register', expectBuilderId: 1 })) !== null, 'matching purpose and builder pass');
|
|
68
|
+
|
|
69
|
+
const h1 = await pk.getOrCreateUserHandle(1, o);
|
|
70
|
+
ok(h1 === await pk.getOrCreateUserHandle(1, o) && h1 !== await pk.getOrCreateUserHandle(2, o), 'user handle is stable per builder and distinct');
|
|
71
|
+
|
|
72
|
+
await pk.addCredential({ builderId: 1, credentialId: 'cred-a', publicKey: Buffer.from([1, 2, 3]), transports: ['usb'], nickname: 'card' }, o);
|
|
73
|
+
await assert.rejects(pk.addCredential({ builderId: 2, credentialId: 'cred-a', publicKey: Buffer.from([9]) }, o));
|
|
74
|
+
pass++; console.log(' ok - a credential id belongs to one builder');
|
|
75
|
+
ok((await pk.listCredentials(1, o)).length === 1 && (await pk.listCredentials(2, o)).length === 0, 'credentials listed per builder');
|
|
76
|
+
|
|
77
|
+
ok((await pk.recordUse('cred-a', 5, o)).ok, 'sign count advances');
|
|
78
|
+
ok((await pk.recordUse('cred-a', 5, o)).reason === 'sign_count_regressed', 'a non-advancing counter is refused');
|
|
79
|
+
ok((await pk.recordUse('cred-a', 3, o)).reason === 'sign_count_regressed' && Number((await pk.getCredentialById('cred-a', o)).sign_count) === 5, 'a lower counter never overwrites the stored one');
|
|
80
|
+
ok((await pk.recordUse('nope', 1, o)).reason === 'unknown_credential', 'unknown credential reported');
|
|
81
|
+
ok((await pk.getCredentialById('cred-a', o)).last_used_at != null, 'last_used_at is stamped');
|
|
82
|
+
ok(!(await pk.removeCredential(2, 'cred-a', o)), 'another builder cannot remove it');
|
|
83
|
+
ok(await pk.removeCredential(1, 'cred-a', o), 'owner removes it');
|
|
84
|
+
console.log(`passkey_foundation_db.mjs: ${pass} passed`);
|
|
85
|
+
} finally {
|
|
86
|
+
await client.query('SET search_path TO public').catch(() => {});
|
|
87
|
+
await client.query(`DROP SCHEMA IF EXISTS ${SCHEMA} CASCADE`).catch(() => {});
|
|
88
|
+
client.release();
|
|
89
|
+
await pgPool.end();
|
|
90
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// tests/passkey_pure.mjs — task 1004750 (ADR 0382 D6). DB-free: the pure rules of
|
|
2
|
+
// src/bongos/passkeys.js that the required unit lane can run. The real-SQL half is
|
|
3
|
+
// tests/passkey_foundation_db.mjs.
|
|
4
|
+
import { createRequire } from 'node:module';
|
|
5
|
+
import { strict as assert } from 'node:assert';
|
|
6
|
+
const require = createRequire(import.meta.url);
|
|
7
|
+
const pk = require('../src/bongos/passkeys.js');
|
|
8
|
+
|
|
9
|
+
let pass = 0;
|
|
10
|
+
const ok = (c, m) => { assert.ok(c, m); pass++; console.log(` ok - ${m}`); };
|
|
11
|
+
|
|
12
|
+
ok(pk.PURPOSES.includes('register') && pk.PURPOSES.includes('authenticate') && pk.PURPOSES.length === 2, 'two ceremony purposes');
|
|
13
|
+
ok(pk.clampTtlSeconds(60) === 60, 'a sane TTL passes through');
|
|
14
|
+
ok(pk.clampTtlSeconds(-5) === pk.CHALLENGE_TTL_SECONDS, 'a negative TTL falls back to the default');
|
|
15
|
+
ok(pk.clampTtlSeconds('abc') === pk.CHALLENGE_TTL_SECONDS, 'a non-number falls back to the default');
|
|
16
|
+
ok(pk.clampTtlSeconds(86400) === pk.MAX_CHALLENGE_TTL_SECONDS, 'a huge TTL is capped');
|
|
17
|
+
ok(pk.counterAdvances(0, 0), 'a card with no counter is accepted');
|
|
18
|
+
ok(pk.counterAdvances(5, 6), 'an advancing counter is accepted');
|
|
19
|
+
ok(!pk.counterAdvances(5, 5), 'a repeated counter is refused');
|
|
20
|
+
ok(!pk.counterAdvances(5, 3), 'a lower counter is refused');
|
|
21
|
+
ok(!pk.counterAdvances(5, 0), 'a counter reset to zero is refused');
|
|
22
|
+
await assert.rejects(pk.createChallenge({ purpose: 'bogus', pool: { query: async () => { throw new Error('should not query'); } } }));
|
|
23
|
+
pass++; console.log(' ok - an unknown purpose is rejected before any query');
|
|
24
|
+
console.log(`passkey_pure.mjs: ${pass} passed`);
|