| … | … | @@ -1,115 +1,15 @@ |
| 1 | | // admission.js — proving that a caller CONTROLS the identity it claims a name under. |
| 1 | // admission.js — re-exported from favela-identity, which is where it lives now. |
| 2 | 2 | // |
| 3 | | // ⛔ WHY THIS IS A SEPARATE FILE. `registrations.js` holds the registry and says, in its own header, |
| 4 | | // that it deliberately does not do this: "it does not verify the chain proof: that is |
| 5 | | // favela-identity's `readAttested`… it belongs at the route where the challenge lives. Here a `root` |
| 6 | | // is required and its SHAPE checked; proving the caller controls it is the caller's job and must |
| 7 | | // happen before claim() is reached." No route had a challenge, so that sentence described a thing |
| 8 | | // that did not exist, and `readAttested` appeared in this package only inside that comment. |
| 3 | // ⛔ ONE CEREMONY, TWO BRIDGES. This file held the implementation. atproto-bridge needs the same |
| 4 | // thing — a single-use challenge and a chain proof pinned to the root it names — because "may this |
| 5 | // caller speak for this identity" is the same question whether the answer is a NAME on a domain or |
| 6 | // an identity CARRIED onto a network. A second copy would be the one that forgot the nonce is |
| 7 | // single-use, or that the root must be pinned inside verify rather than compared afterwards. |
| 9 | 8 | // |
| 10 | | // What that cost: an identity root is PUBLIC. `claim()` accepted any 64-hex string, so anyone could |
| 11 | | // take a name under anyone's identity; and `release()` compared a bare root, so anyone could BURN |
| 12 | | // someone's actor — permanently, because the generation counter deliberately makes a reused name a |
| 13 | | // different actor and there is no way back. |
| 9 | // favela-identity is where it belongs: it already owns `readAttested` and the bytes both sides sign, |
| 10 | // and its own header says it was "EXTRACTED SO THERE IS ONE COPY". |
| 14 | 11 | // |
| 15 | | // ⛔ A CHALLENGE IS REQUIRED, NOT DECORATION. `readAttested` is a pure function of the proof's bytes |
| 16 | | // — no disk, no network, NO CLOCK — which is exactly the property that lets it run inside an |
| 17 | | // autobase apply. A verifier with no clock cannot detect replay. Freshness therefore has to come |
| 18 | | // from something the bridge issues and remembers, and there is nowhere else to put it. |
| 19 | | // |
| 20 | | // ⛔ AND THE REQUEST IS INSIDE THE SIGNED BYTES. A proof covering only the nonce could be captured |
| 21 | | // and replayed to claim a DIFFERENT name under the signer's root — consent proven, control proven, |
| 22 | | // and the wrong name registered. Binding username (and feed, on a claim) is what stops that. |
| 23 | | // |
| 24 | | // ⚠ TOTAL. This runs on a request path fed by strangers, so every path returns { ok, error } and |
| 25 | | // nothing throws: `readAttested` is already guarded that way for the same reason. |
| 26 | | |
| 27 | | import crypto from 'bare-crypto' |
| 28 | | import b4a from 'b4a' |
| 29 | | import { readAttested } from 'favela-identity/lib/attestation.js' |
| 30 | | // ⛔ THE BYTES COME FROM THE SHARED PACKAGE, not from here. The claimant builds them too, and a |
| 31 | | // disagreement of one byte surfaces as "proof does not attest that identity" — indistinguishable |
| 32 | | // from a forged proof. One definition, next to the verifier that consumes it. |
| 33 | | import { claimBytes, releaseBytes } from 'favela-identity/lib/bridge-claim.js' |
| 34 | | |
| 35 | | const HEX64 = /^[0-9a-f]{64}$/i |
| 36 | | // Deliberately the same shape registrations.js enforces, so bytes signed here can never describe a |
| 37 | | // name the registry would reject. |
| 38 | | const NAME = /^[a-z0-9][a-z0-9_-]{0,31}$/ |
| 39 | | |
| 40 | | |
| 41 | | const DEFAULT_TTL = 120_000 // two minutes — a round trip, not a session |
| 42 | | |
| 43 | | export class Admission { |
| 44 | | constructor ({ ttlMs = DEFAULT_TTL, now = Date.now } = {}) { |
| 45 | | this._ttl = ttlMs |
| 46 | | this._now = now |
| 47 | | this._open = new Map() // nonce -> expiresAt |
| 48 | | } |
| 49 | | |
| 50 | | /** |
| 51 | | * Mint a single-use challenge. |
| 52 | | * |
| 53 | | * ⛔ From a CSPRNG. `registrations.js`'s invite token used `Math.random()`, which on an invite-only |
| 54 | | * bridge is the entire admission gate; a guessable nonce here would be the same mistake one layer |
| 55 | | * down. bare-crypto is already a dependency. |
| 56 | | */ |
| 57 | | challenge () { |
| 58 | | this._sweep() |
| 59 | | const nonce = b4a.toString(crypto.randomBytes(32), 'hex') |
| 60 | | const expiresAt = this._now() + this._ttl |
| 61 | | this._open.set(nonce, expiresAt) |
| 62 | | return { nonce, expiresAt } |
| 63 | | } |
| 64 | | |
| 65 | | /** The exact bytes a claimant must sign. Public, because the caller has to produce them too. */ |
| 66 | | claimBytes (params) { return claimBytes(params) } |
| 67 | | |
| 68 | | /** The exact bytes a releaser must sign. */ |
| 69 | | releaseBytes (params) { return releaseBytes(params) } |
| 70 | | |
| 71 | | verifyClaim ({ nonce, username, feedKey, root, proof } = {}) { |
| 72 | | if (badName(username)) return fail('bad username') |
| 73 | | if (feedKey !== null && feedKey !== undefined && !HEX64.test(String(feedKey))) return fail('bad feed key') |
| 74 | | return this._verify(nonce, root, proof, () => this.claimBytes({ nonce, username, feedKey })) |
| 75 | | } |
| 76 | | |
| 77 | | verifyRelease ({ nonce, username, root, proof } = {}) { |
| 78 | | if (badName(username)) return fail('bad username') |
| 79 | | return this._verify(nonce, root, proof, () => this.releaseBytes({ nonce, username })) |
| 80 | | } |
| 81 | | |
| 82 | | /** |
| 83 | | * ⛔ THE NONCE IS BURNED WHETHER OR NOT THE PROOF VERIFIES. Consuming only on success would leave a |
| 84 | | * live challenge for an attacker to keep trying proofs against; consuming always costs a legitimate |
| 85 | | * caller one extra round trip in the rare case they got it wrong, and costs an attacker everything. |
| 86 | | */ |
| 87 | | _verify (nonce, root, proof, bytesOf) { |
| 88 | | if (typeof nonce !== 'string' || !this._open.has(nonce)) return fail('unknown or expired challenge') |
| 89 | | const expiresAt = this._open.get(nonce) |
| 90 | | this._open.delete(nonce) |
| 91 | | if (this._now() > expiresAt) return fail('unknown or expired challenge') |
| 92 | | if (typeof root !== 'string' || !HEX64.test(root)) return fail('identity root required') |
| 93 | | |
| 94 | | // ⛔ The root is pinned INSIDE verify rather than compared afterwards. attestation.js: "a |
| 95 | | // comparison the caller has to remember to write is a comparison someone eventually forgets." |
| 96 | | // A non-null return means exactly: this root attested this device, and that device signed these |
| 97 | | // bytes. Neither half is trusted separately. |
| 98 | | const seen = readAttested(proof, bytesOf(), root.toLowerCase()) |
| 99 | | if (!seen) return fail('proof does not attest that identity') |
| 100 | | return { ok: true, root: seen.root, device: seen.device } |
| 101 | | } |
| 102 | | |
| 103 | | /** Expired challenges are not evidence of anything — drop them rather than grow a map for ever. */ |
| 104 | | _sweep () { |
| 105 | | const now = this._now() |
| 106 | | for (const [nonce, expiresAt] of this._open) if (now > expiresAt) this._open.delete(nonce) |
| 107 | | } |
| 108 | | |
| 109 | | get open () { return this._open.size } |
| 110 | | } |
| 111 | | |
| 112 | | const badName = (s) => typeof s !== 'string' || !NAME.test(s) |
| 113 | | const fail = (error) => ({ ok: false, error }) |
| 114 | | |
| 115 | | export default { Admission } |
| 12 | // ⚠ Kept as a re-export rather than deleted — this path is imported by index.js and by the tests |
| 13 | // that prove the gate, and relocating an implementation is not a reason to rewrite its callers. |
| 14 | export { Admission } from 'favela-identity/lib/admission.js' |
| 15 | export { default } from 'favela-identity/lib/admission.js' |