From 76dbaf1525a08891bc3c401a89d7059d76f2bbde Mon Sep 17 00:00:00 2001 From: Connor Snitker Date: Wed, 10 Jun 2026 18:09:16 -0500 Subject: [PATCH 01/12] feat(tl): seal and serve verified-identity events MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add the IDENTITY_* event family to the Transparency Log — the "who" behind agents, sealed on its own read stream over the same single Merkle tree as agent events. Event family (internal/tl/event/identity): IDENTITY_VERIFIED, IDENTITY_UPDATED, IDENTITY_REVOKED, IDENTITY_LINKED, IDENTITY_UNLINKED — a closed enum mirroring the agent envelope's JCS + RFC 6962 leaf rules. Sealed proof events quote the DID document's verification method VERBATIM ({verificationMethod, signedProof}); nothing derived, re-encoded, or normalized enters a seal. Thumbprints are compute-at-read conveniences. Ingest: POST /v1/internal/identities/event — a third codec on the same producer-signature lane. The closed enums are the cross-lane guard: agent bodies fail the identity codec and vice versa, and the V1 lane stays frozen. Storage: tl_events gains a nullable identity_id read index (streams are read indexes over one log, never separate trees) plus a tl_identity_event_agents fan-out table so link batches — ONE sealed event carrying ansIds[] — join back to agents at read time. Reads: /v1/identities/{id}{,/audit,/receipt,/agents}, agent badges gain a computed identities[] join (link live ∧ identity stream state; provenKeyIds name the current proven set), and /v1/agents/{id}/identities{,/history} serve the agent-side views in the standard audit envelope. Identity operations never write to an agent's stream; all propagation is read-time. Receipt cache lookups move from (agent, treeSize) to the table's natural (leafIndex, treeSize) key so identity leaves reuse the same COSE_Sign1 machinery. Signed-off-by: Connor Snitker --- cmd/ans-tl/main.go | 3 +- internal/adapter/docsui/openapi/tl.yaml | 401 ++++++++++++++- internal/adapter/store/sqlitetl/events.go | 286 +++++++++-- .../store/sqlitetl/identityevents_test.go | 314 ++++++++++++ .../migrations/004_identity_events.sql | 47 ++ internal/adapter/store/sqlitetl/receipts.go | 20 +- internal/tl/event/identity/event.go | 474 ++++++++++++++++++ internal/tl/event/identity/event_test.go | 294 +++++++++++ .../tl/handler/empty_param_identity_test.go | 42 ++ internal/tl/handler/empty_param_test.go | 14 +- internal/tl/handler/handler.go | 199 +++++++- internal/tl/handler/handler_identity_test.go | 407 +++++++++++++++ internal/tl/handler/handler_test.go | 3 +- internal/tl/service/badge.go | 8 + internal/tl/service/codec.go | 42 ++ internal/tl/service/codec_identity_test.go | 117 +++++ internal/tl/service/identitybadge.go | 306 +++++++++++ internal/tl/service/log.go | 51 ++ internal/tl/service/receipt.go | 33 +- spec/api-spec-tl-v2.yaml | 401 ++++++++++++++- 20 files changed, 3382 insertions(+), 80 deletions(-) create mode 100644 internal/adapter/store/sqlitetl/identityevents_test.go create mode 100644 internal/adapter/store/sqlitetl/migrations/004_identity_events.sql create mode 100644 internal/tl/event/identity/event.go create mode 100644 internal/tl/event/identity/event_test.go create mode 100644 internal/tl/handler/empty_param_identity_test.go create mode 100644 internal/tl/handler/handler_identity_test.go create mode 100644 internal/tl/service/codec_identity_test.go create mode 100644 internal/tl/service/identitybadge.go diff --git a/cmd/ans-tl/main.go b/cmd/ans-tl/main.go index 611b5fe..3a202bc 100644 --- a/cmd/ans-tl/main.go +++ b/cmd/ans-tl/main.go @@ -177,6 +177,7 @@ func run(cfgPath string) error { // underlying Tessera reader gets torn down. defer logSvc.Close() badgeSvc := service.NewBadgeService(logSvc) + identityBadgeSvc := service.NewIdentityBadgeService(logSvc, badgeSvc) // Receipt + status-token generators reuse the single signing // key. Matches the reference TL's deployed topology: one KMS key @@ -253,7 +254,7 @@ func run(cfgPath string) error { } h := handler.NewHandlers( - logSvc, badgeSvc, receiptSvc, statusTokenSvc, + logSvc, badgeSvc, identityBadgeSvc, receiptSvc, statusTokenSvc, checkpointSvc, schemaSvc, rootKeysBody, ) h.Mount(r, lg.DataDir()) diff --git a/internal/adapter/docsui/openapi/tl.yaml b/internal/adapter/docsui/openapi/tl.yaml index 6dfbd9a..fa98ebc 100644 --- a/internal/adapter/docsui/openapi/tl.yaml +++ b/internal/adapter/docsui/openapi/tl.yaml @@ -259,6 +259,255 @@ paths: schema: $ref: '#/components/schemas/Problem' + # ────────────────────────────────────────────────────────────── + # Verified identities — the "who" behind agents + # + # There is ONE transparency log — a single Merkle tree. Identity + # events append to the same tree as agent events; the "identity + # stream" is a read index over that single log keyed by + # identityId, exactly as the agent surface is a read index keyed + # by ansId. Tiles/checkpoints/receipts/witnesses are unchanged. + # + # Placement rule: every IDENTITY_* event — proofs, rotations, + # revocations, AND links — is indexed under the identity stream. + # An identity operation never modifies an agent's event or audit + # history; the agent-side views below are read-time joins through + # the link events' agent index. + # ────────────────────────────────────────────────────────────── + /v1/internal/identities/event: + post: + tags: [Transparency Log] + summary: Append a signed identity event to the log + description: | + The identity-family ingest lane. Identical wire contract to + `/v1/internal/agents/event` — JCS-canonicalized inner event + body + detached-JWS `X-Signature`, same producer-key trust + store, same dedup — with the identity payload schema (keyed + by `identityId`, closed `IDENTITY_*` eventType enum). The + closed enums are the cross-lane guard: an `AGENT_*` body on + this route (or an `IDENTITY_*` body on an agent route) fails + validation with 422 `INVALID_EVENT`. + operationId: appendIdentityEvent + parameters: + - name: X-Signature + in: header + required: true + description: | + Detached JWS compact serialization over the raw request + body — same discipline as the agent lanes. + schema: + type: string + requestBody: + required: true + description: JCS-canonicalized inner identity event (≤ 256 KiB). + content: + application/json: + schema: + $ref: '#/components/schemas/IdentityProducerEvent' + responses: + '200': + description: Event logged (or duplicate echoed — idempotent retries). + content: + application/json: + schema: + $ref: '#/components/schemas/AppendResponse' + '413': + description: Request body exceeds 256 KiB. + content: + application/problem+json: + schema: + $ref: '#/components/schemas/Problem' + '422': + description: | + Body fails validation — same codes as the agent lanes, + including `INVALID_EVENT` for cross-lane posts. + content: + application/problem+json: + schema: + $ref: '#/components/schemas/Problem' + + /v1/identities/{identityId}: + get: + tags: [Transparency Log] + summary: Identity badge + description: | + The latest sealed identity event + inclusion proof + computed + status (`VERIFIED` | `REVOKED`). The proof events seal every + proven key self-verifyingly: any third party reads the key + out of the sealed verbatim `verificationMethod`, verifies the + sealed `signedProof` against it, and confirms the payload + binds this identityId — offline, without trusting the RA. + operationId: getIdentityBadge + parameters: + - $ref: '#/components/parameters/IdentityIdPath' + responses: + '200': + description: Identity badge retrieved. + content: + application/json: + schema: + $ref: '#/components/schemas/TransparencyLog' + '404': + description: No events for this identity. + content: + application/problem+json: + schema: + $ref: '#/components/schemas/Problem' + + /v1/identities/{identityId}/audit: + get: + tags: [Transparency Log] + summary: Paginated audit history for an identity + description: | + The identity's full event chain — IDENTITY_VERIFIED, links, + rotations, revocation — in the exact same audit envelope as + the agent audit. This unbroken chain is the continuity + thread: when an agent's domain is lost, the identity stream + survives and links to the successor. + operationId: getIdentityAudit + parameters: + - $ref: '#/components/parameters/IdentityIdPath' + - name: limit + in: query + required: false + schema: { type: integer, default: 50, minimum: 1, maximum: 200 } + - name: offset + in: query + required: false + schema: { type: integer, default: 0, minimum: 0 } + responses: + '200': + description: Audit records, newest-first. + content: + application/json: + schema: + $ref: '#/components/schemas/TransparencyLogAudit' + + /v1/identities/{identityId}/receipt: + get: + tags: [Transparency Log] + summary: SCITT receipt for the identity's latest event + description: | + COSE_Sign1 receipt (CBOR tag 18) for the identity's latest + sealed leaf — same machinery, media type, and 503 retry + semantics as the agent receipt. + operationId: getIdentityReceipt + parameters: + - $ref: '#/components/parameters/IdentityIdPath' + responses: + '200': + description: Binary COSE_Sign1 receipt. + content: + application/scitt-receipt+cose: + schema: + type: string + format: binary + '404': + description: No events for this identity. + content: + application/problem+json: + schema: + $ref: '#/components/schemas/Problem' + '503': + description: | + Leaf committed but no signed checkpoint covers it yet; + retry after the `Retry-After` delay. + headers: + Retry-After: + schema: { type: integer } + content: + application/problem+json: + schema: + $ref: '#/components/schemas/Problem' + + /v1/identities/{identityId}/agents: + get: + tags: [Transparency Log] + summary: Agents currently linked to the identity (reverse join) + description: | + Computed at query time from the link events' agent index: + every agent whose latest link/unlink fact naming it is + LINKED, each decorated with its own computed badge status so + a reader checks both ends of the link in one response. + operationId: getIdentityAgents + parameters: + - $ref: '#/components/parameters/IdentityIdPath' + responses: + '200': + description: Currently-linked agents. + content: + application/json: + schema: + type: object + required: [agents] + properties: + agents: + type: array + items: + $ref: '#/components/schemas/LinkedAgentView' + '404': + description: No events for this identity. + content: + application/problem+json: + schema: + $ref: '#/components/schemas/Problem' + + /v1/agents/{agentId}/identities: + get: + tags: [Transparency Log] + summary: Identities currently linked to the agent (computed) + description: | + The same entries as the agent badge's `identities[]` field, + served alone. Computed at read time — link live ∧ identity + stream state — never stored on, or sealed into, the agent. + operationId: getAgentIdentities + parameters: + - $ref: '#/components/parameters/AgentIdPath' + responses: + '200': + description: Currently-linked identities. + content: + application/json: + schema: + type: object + required: [identities] + properties: + identities: + type: array + items: + $ref: '#/components/schemas/LinkedIdentityView' + + /v1/agents/{agentId}/identities/history: + get: + tags: [Transparency Log] + summary: Link/unlink events that ever named this agent + description: | + Past and present associations, in the STANDARD audit envelope + (each record a TransparencyLog) — no bespoke format. Filtered + through the link events' agent index; the records themselves + live on the identity streams. The agent's own `/audit` stays + purely AGENT_*. This endpoint is a droppable convenience: the + same view derives from the identity audits or the raw entry + tiles. + operationId: getAgentIdentityHistory + parameters: + - $ref: '#/components/parameters/AgentIdPath' + - name: limit + in: query + required: false + schema: { type: integer, default: 50, minimum: 1, maximum: 200 } + - name: offset + in: query + required: false + schema: { type: integer, default: 0, minimum: 0 } + responses: + '200': + description: Link events naming this agent, newest-first. + content: + application/json: + schema: + $ref: '#/components/schemas/TransparencyLogAudit' + # ────────────────────────────────────────────────────────────── # Log static artefacts + verifier keys (public under public-read) # ────────────────────────────────────────────────────────────── @@ -689,6 +938,14 @@ components: type: string format: uuid + IdentityIdPath: + name: identityId + in: path + required: true + description: Immutable identity UUIDv7 — the identity stream key. + schema: + type: string + schemas: # ── Ingest ───────────────────────────────────────────────── ProducerEvent: @@ -763,7 +1020,22 @@ components: signature: { type: string, description: "TL attestation (detached JWS)" } status: type: string - enum: [ACTIVE, REVOKED, DEPRECATED] + description: | + Read-time computed status. Agent badges derive ACTIVE / + REVOKED / DEPRECATED / EXPIRED / WARNING from the latest + event + attested cert expiry; identity badges derive + VERIFIED / REVOKED from the identity stream. + enum: [ACTIVE, REVOKED, DEPRECATED, EXPIRED, WARNING, VERIFIED] + identities: + type: array + description: | + Agent badges only — the COMPUTED read-time join of the + agent's currently-linked verified identities. Covered by + the TL's response signature, never by any seal; identity + rotation/revocation is visible here immediately with + zero agent-stream writes. + items: + $ref: '#/components/schemas/LinkedIdentityView' TransparencyLogAudit: type: object @@ -789,6 +1061,133 @@ components: description: "standard base64 of each sibling hash in order" treeVersion: { type: integer } + # ── Verified identities ──────────────────────────────────── + IdentityProducerEvent: + type: object + description: | + The canonical inner identity event the RA POSTs to the + identity ingest lane. Sealed shapes are append-only-forever; + the five eventType tokens and these fields are the contract. + JCS-canonicalized when signed, same as the agent lanes. + + Per-type required fields: proofs (IDENTITY_VERIFIED / + IDENTITY_UPDATED) carry non-empty `keys[]`, `verifiedAt`, and + `providerId`; IDENTITY_REVOKED carries `revokedAt`; the link + events carry non-empty `ansIds[]` (the whole batch in ONE + event) and are the only types allowed to name agents. + required: [identityId, kind, value, eventType, timestamp] + properties: + identityId: { type: string, description: "UUIDv7 — the identity stream key" } + kind: + type: string + enum: ['did:web', 'did:key', 'lei'] + value: { type: string, example: "did:web:identity.acme-corp.com" } + providerId: { type: string, description: "The owning principal — the WHO's owner" } + proofMethod: + type: string + enum: [did-web-sig, did-key-sig, lei-vlei-acdc] + eventType: + type: string + enum: [IDENTITY_VERIFIED, IDENTITY_UPDATED, IDENTITY_REVOKED, IDENTITY_LINKED, IDENTITY_UNLINKED] + keys: + type: array + description: | + The proven key set — sealed self-verifyingly: each entry + quotes the DID document's verification method VERBATIM + alongside the registrant's signed proof. + items: + $ref: '#/components/schemas/ProvenKey' + ansIds: + type: array + description: Linked agents' ids (IDENTITY_LINKED / IDENTITY_UNLINKED only) + items: { type: string } + previousValue: { type: string, description: "Pre-rotation identifier (IDENTITY_UPDATED)" } + verifiedAt: { type: string, format: date-time } + revokedAt: { type: string, format: date-time } + raId: { type: string, example: "ans-ra-local" } + timestamp: { type: string, format: date-time } + + ProvenKey: + type: object + description: | + One key the registrant proved possession of, sealed + self-verifyingly: any third party reads the key material out + of `verificationMethod`, verifies `signedProof` against it, + then confirms the payload decodes to an IdentityProofInput + binding this identityId + identifier + purpose — offline, + without trusting the RA. + + `verificationMethod` is quoted EXACTLY as the DID document + served it — `id`, `type`, `controller`, and the key material + in whichever representation the document used + (`publicKeyJwk`, or `publicKeyMultibase` for Multikey + documents) — member-for-member, values untouched. Nothing + derived, re-encoded, or normalized enters a seal; the event + envelope is JCS-canonicalized for signing like every event, + and JCS preserves member values exactly, so the quoted + material survives intact. Thumbprints are compute-at-read + conveniences (anyone can derive RFC 7638 from the sealed + source) and are never part of the sealed contract. + + The postponed `lei` kind is the one deliberate exception: + it will seal the subject AID + a key thumbprint only — there + is no document to quote, the ACDC is PII, and KERI's KEL is + already the authoritative key history. Seal verbatim what + has no other tamper-evident home; commit minimally where one + exists. + required: [verificationMethod, signedProof] + properties: + verificationMethod: + type: object + description: The DID document's verification-method object, verbatim + example: + id: did:web:identity.acme-corp.com#key-1 + type: JsonWebKey2020 + controller: did:web:identity.acme-corp.com + publicKeyJwk: { kty: OKP, crv: Ed25519, x: "0-e2i2_..." } + signedProof: + type: string + description: The compact JWS over the served IdentityProofInput + + LinkedIdentityView: + type: object + description: One computed identities[] entry on the agent badge. + required: [identityId, kind, value, identityStatus] + properties: + identityId: { type: string } + kind: { type: string, enum: ['did:web', 'did:key', 'lei'] } + value: { type: string } + identityStatus: + type: string + enum: [VERIFIED, REVOKED] + description: Reflects the identity stream NOW + provenKeyIds: + type: array + description: | + Verification-method ids of the current proven key set + (post-rotation). The full verbatim methods live in the + sealed proof event. + items: { type: string } + linkedAt: { type: string, format: date-time } + linkLogId: + type: string + description: The sealed IDENTITY_LINKED entry on the identity stream — fetch for link evidence + identityLogId: + type: string + description: Latest identity-stream entry — fetch for the identity evidence/history + + LinkedAgentView: + type: object + description: One reverse-join entry (identity → agents). + required: [ansId] + properties: + ansId: { type: string } + linkedAt: { type: string, format: date-time } + agentStatus: + type: string + enum: [ACTIVE, REVOKED, DEPRECATED, EXPIRED, WARNING] + description: The linked agent's own computed badge status — a link is effective only while both ends are live + # ── Admin: producer keys ─────────────────────────────────── ProducerKeyRequest: type: object diff --git a/internal/adapter/store/sqlitetl/events.go b/internal/adapter/store/sqlitetl/events.go index 2b25f5b..84aa033 100644 --- a/internal/adapter/store/sqlitetl/events.go +++ b/internal/adapter/store/sqlitetl/events.go @@ -23,6 +23,11 @@ import ( // - EventHashHex — SHA-256 of the JCS-canonical inner-producer-event // bytes. UNIQUE — the table rejects retries with // the same inner event content. +// +// AgentID and IdentityID are the two read-index keys over the single +// log: agent events carry AgentID (IdentityID empty), identity events +// carry IdentityID (AgentID empty). "Stream" means nothing more than +// filtering this table by one of those keys. type EventRecord struct { ID int64 `db:"id"` LeafIndex uint64 `db:"leaf_index"` @@ -32,12 +37,21 @@ type EventRecord struct { AgentID string `db:"agent_id"` AnsName string `db:"ans_name"` AgentFQDN string `db:"agent_fqdn"` + IdentityID string `db:"identity_id"` EventType string `db:"event_type"` SchemaVersion string `db:"schema_version"` RawEvent string `db:"raw_event"` CreatedAtMs int64 `db:"created_at_ms"` } +// eventCols is the shared SELECT column list. identity_id is nullable +// on disk (NULL for agent events); COALESCE keeps the Go-side scan a +// plain string. +const eventCols = `id, leaf_index, leaf_hash, event_hash, log_id, + agent_id, ans_name, agent_fqdn, event_type, + schema_version, raw_event, created_at_ms, + COALESCE(identity_id, '') AS identity_id` + // CreatedAt returns the event creation time in UTC. func (r *EventRecord) CreatedAt() time.Time { return time.UnixMilli(r.CreatedAtMs).UTC() } @@ -61,9 +75,39 @@ func (r *EventRecord) LeafHashBytes() ([32]byte, error) { return out, nil } +// LinkState is the latest link/unlink fact for one (identity, agent) +// pair, computed from the tl_identity_event_agents read-join index. A +// link is live when EventType == IDENTITY_LINKED; whether it is +// *effective* additionally depends on the identity's and agent's +// current stream state, which the service layer joins in. +type LinkState struct { + IdentityID string `db:"identity_id"` + AnsID string `db:"ans_id"` + LeafIndex uint64 `db:"leaf_index"` + EventType string `db:"event_type"` + CreatedAtMs int64 `db:"created_at_ms"` +} + +// eventTypeIdentityLinked mirrors identityevent.TypeIdentityLinked +// without importing the event package for one comparison string. +const eventTypeIdentityLinked = "IDENTITY_LINKED" + +// Linked reports whether the latest event for the pair is a link. +func (l *LinkState) Linked() bool { return l.EventType == eventTypeIdentityLinked } + +// identityIndexed is the optional capability an envelope exposes when +// it should be indexed on the identity stream. The identity envelope +// implements it; agent envelopes (V1/V2) don't, and simply land with +// a NULL identity_id. Discovering the capability by type assertion +// keeps event.View frozen for existing implementers. +type identityIndexed interface { + IdentityID() string + LinkedAgentIDs() []string +} + // EventStore persists event records. Mirrors the reference // EventStorage.StoreEvent / GetLatestRecordByAgentID / GetRecordsByAgentID / -// GetEventByLeafIndex surface. +// GetEventByLeafIndex surface, extended with the identity read index. type EventStore struct{ db *DB } // NewEventStore returns a new SQLite-backed event store. @@ -81,10 +125,15 @@ func ComputeEventHash(innerCanonical []byte) string { // StoreEvent persists a freshly-appended event. Returns a domain // conflict error if event_hash already exists (idempotent retry). -// Takes an event.View so both V1 and V2 envelopes land through the -// same persistence path; the `schema_version` column holds -// `env.Version()` for downstream read handlers to echo back in the -// TransparencyLog response. +// Takes an event.View so every envelope shape lands through the same +// persistence path; the `schema_version` column holds `env.Version()` +// for downstream read handlers to echo back. +// +// Identity envelopes (discovered via the identityIndexed capability) +// additionally fan their linked-agent ids into the +// tl_identity_event_agents read-join index, atomically with the event +// row — a link event whose index rows were lost would silently hide +// the association from every agent-side read. func (s *EventStore) StoreEvent( ctx context.Context, leafIndex uint64, @@ -93,13 +142,27 @@ func (s *EventStore) StoreEvent( env event.View, canonicalEnvelope []byte, ) (*EventRecord, error) { + identityID := "" + var linkedAgents []string + if iv, ok := env.(identityIndexed); ok { + identityID = iv.IdentityID() + linkedAgents = iv.LinkedAgentIDs() + } + + tx, err := s.db.db.BeginTxx(ctx, nil) + if err != nil { + return nil, mapSQLErr(err) + } + defer func() { _ = tx.Rollback() }() + + now := nowMs() const q = ` INSERT INTO tl_events( leaf_index, leaf_hash, event_hash, log_id, agent_id, ans_name, agent_fqdn, event_type, - schema_version, raw_event, created_at_ms - ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)` - _, err := s.db.db.ExecContext(ctx, q, + schema_version, raw_event, created_at_ms, identity_id + ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NULLIF(?, ''))` + if _, err := tx.ExecContext(ctx, q, leafIndex, hex.EncodeToString(leafHash[:]), innerEventHash, @@ -110,9 +173,25 @@ func (s *EventStore) StoreEvent( env.EventType(), env.Version(), string(canonicalEnvelope), - nowMs(), - ) - if err != nil { + now, + identityID, + ); err != nil { + return nil, mapSQLErr(err) + } + + if identityID != "" && len(linkedAgents) > 0 { + const fq = ` + INSERT INTO tl_identity_event_agents( + leaf_index, identity_id, ans_id, event_type, created_at_ms + ) VALUES (?, ?, ?, ?, ?)` + for _, ansID := range linkedAgents { + if _, err := tx.ExecContext(ctx, fq, leafIndex, identityID, ansID, env.EventType(), now); err != nil { + return nil, mapSQLErr(err) + } + } + } + + if err := tx.Commit(); err != nil { return nil, mapSQLErr(err) } return s.GetEventByLeafIndex(ctx, leafIndex) @@ -122,10 +201,7 @@ func (s *EventStore) StoreEvent( func (s *EventStore) GetEventByLeafIndex(ctx context.Context, index uint64) (*EventRecord, error) { var r EventRecord err := s.db.db.GetContext(ctx, &r, - `SELECT id, leaf_index, leaf_hash, event_hash, log_id, - agent_id, ans_name, agent_fqdn, event_type, - schema_version, raw_event, created_at_ms - FROM tl_events WHERE leaf_index = ?`, index) + `SELECT `+eventCols+` FROM tl_events WHERE leaf_index = ?`, index) if err != nil { return nil, mapSQLErr(err) } @@ -139,18 +215,14 @@ func (s *EventStore) GetLatestByAgentID(ctx context.Context, agentID string, max var r EventRecord var err error if maxLeafIndex > 0 { - err = s.db.db.GetContext(ctx, &r, ` - SELECT id, leaf_index, leaf_hash, event_hash, log_id, - agent_id, ans_name, agent_fqdn, event_type, - schema_version, raw_event, created_at_ms + err = s.db.db.GetContext(ctx, &r, + `SELECT `+eventCols+` FROM tl_events WHERE agent_id = ? AND leaf_index < ? ORDER BY leaf_index DESC LIMIT 1`, agentID, maxLeafIndex) } else { - err = s.db.db.GetContext(ctx, &r, ` - SELECT id, leaf_index, leaf_hash, event_hash, log_id, - agent_id, ans_name, agent_fqdn, event_type, - schema_version, raw_event, created_at_ms + err = s.db.db.GetContext(ctx, &r, + `SELECT `+eventCols+` FROM tl_events WHERE agent_id = ? ORDER BY leaf_index DESC LIMIT 1`, agentID) @@ -168,28 +240,19 @@ func (s *EventStore) GetByAgentID( limit, offset int, maxLeafIndex uint64, ) ([]*EventRecord, error) { - if limit <= 0 || limit > 200 { - limit = 50 - } - if offset < 0 { - offset = 0 - } + limit, offset = clampPage(limit, offset) var rows []*EventRecord var err error if maxLeafIndex > 0 { - err = s.db.db.SelectContext(ctx, &rows, ` - SELECT id, leaf_index, leaf_hash, event_hash, log_id, - agent_id, ans_name, agent_fqdn, event_type, - schema_version, raw_event, created_at_ms + err = s.db.db.SelectContext(ctx, &rows, + `SELECT `+eventCols+` FROM tl_events WHERE agent_id = ? AND leaf_index < ? ORDER BY leaf_index DESC LIMIT ? OFFSET ?`, agentID, maxLeafIndex, limit, offset) } else { - err = s.db.db.SelectContext(ctx, &rows, ` - SELECT id, leaf_index, leaf_hash, event_hash, log_id, - agent_id, ans_name, agent_fqdn, event_type, - schema_version, raw_event, created_at_ms + err = s.db.db.SelectContext(ctx, &rows, + `SELECT `+eventCols+` FROM tl_events WHERE agent_id = ? ORDER BY leaf_index DESC LIMIT ? OFFSET ?`, @@ -204,6 +267,155 @@ func (s *EventStore) GetByAgentID( return rows, nil } +// GetLatestByIdentityID returns the newest event on an identity's +// stream — the read index over the single log keyed by identity_id. +func (s *EventStore) GetLatestByIdentityID(ctx context.Context, identityID string) (*EventRecord, error) { + var r EventRecord + err := s.db.db.GetContext(ctx, &r, + `SELECT `+eventCols+` + FROM tl_events + WHERE identity_id = ? + ORDER BY leaf_index DESC LIMIT 1`, identityID) + if err != nil { + return nil, mapSQLErr(err) + } + return &r, nil +} + +// GetByIdentityID returns paginated events for an identity, newest first. +func (s *EventStore) GetByIdentityID( + ctx context.Context, + identityID string, + limit, offset int, +) ([]*EventRecord, error) { + limit, offset = clampPage(limit, offset) + var rows []*EventRecord + err := s.db.db.SelectContext(ctx, &rows, + `SELECT `+eventCols+` + FROM tl_events + WHERE identity_id = ? + ORDER BY leaf_index DESC LIMIT ? OFFSET ?`, + identityID, limit, offset) + if err != nil { + if errors.Is(err, sql.ErrNoRows) { + return nil, nil + } + return nil, err + } + return rows, nil +} + +// GetLatestProofByIdentityID returns the newest *proof* event +// (IDENTITY_VERIFIED / IDENTITY_UPDATED) on an identity's stream — +// the event carrying the current proven key set. Link events sit on +// the same stream but carry no keys; badge joins need the proof. +func (s *EventStore) GetLatestProofByIdentityID(ctx context.Context, identityID string) (*EventRecord, error) { + var r EventRecord + err := s.db.db.GetContext(ctx, &r, + `SELECT `+eventCols+` + FROM tl_events + WHERE identity_id = ? + AND event_type IN ('IDENTITY_VERIFIED', 'IDENTITY_UPDATED') + ORDER BY leaf_index DESC LIMIT 1`, identityID) + if err != nil { + return nil, mapSQLErr(err) + } + return &r, nil +} + +// LinkStatesByAgent returns, for one agent, the latest link/unlink +// fact per identity that ever named it — the badge-join input. Rows +// where Linked() is true are the agent's live links. +func (s *EventStore) LinkStatesByAgent(ctx context.Context, ansID string) ([]*LinkState, error) { + var rows []*LinkState + err := s.db.db.SelectContext(ctx, &rows, ` + SELECT a.identity_id, a.ans_id, a.leaf_index, a.event_type, a.created_at_ms + FROM tl_identity_event_agents a + JOIN ( + SELECT identity_id, MAX(leaf_index) AS max_leaf + FROM tl_identity_event_agents + WHERE ans_id = ? + GROUP BY identity_id + ) m ON a.identity_id = m.identity_id AND a.leaf_index = m.max_leaf + WHERE a.ans_id = ? + ORDER BY a.leaf_index DESC`, ansID, ansID) + if err != nil { + if errors.Is(err, sql.ErrNoRows) { + return nil, nil + } + return nil, err + } + return rows, nil +} + +// LinkStatesByIdentity returns, for one identity, the latest +// link/unlink fact per agent it ever named — the reverse join. +func (s *EventStore) LinkStatesByIdentity(ctx context.Context, identityID string) ([]*LinkState, error) { + var rows []*LinkState + err := s.db.db.SelectContext(ctx, &rows, ` + SELECT a.identity_id, a.ans_id, a.leaf_index, a.event_type, a.created_at_ms + FROM tl_identity_event_agents a + JOIN ( + SELECT ans_id, MAX(leaf_index) AS max_leaf + FROM tl_identity_event_agents + WHERE identity_id = ? + GROUP BY ans_id + ) m ON a.ans_id = m.ans_id AND a.leaf_index = m.max_leaf + WHERE a.identity_id = ? + ORDER BY a.leaf_index DESC`, identityID, identityID) + if err != nil { + if errors.Is(err, sql.ErrNoRows) { + return nil, nil + } + return nil, err + } + return rows, nil +} + +// LinkEventsByAgent returns the link/unlink events that ever named +// this agent, newest first — the per-agent association history, +// served in the standard audit envelope by the handler. +func (s *EventStore) LinkEventsByAgent( + ctx context.Context, + ansID string, + limit, offset int, +) ([]*EventRecord, error) { + limit, offset = clampPage(limit, offset) + var rows []*EventRecord + err := s.db.db.SelectContext(ctx, &rows, + `SELECT `+eventColsPrefixed+` + FROM tl_events e + JOIN tl_identity_event_agents a ON a.leaf_index = e.leaf_index + WHERE a.ans_id = ? + ORDER BY e.leaf_index DESC LIMIT ? OFFSET ?`, + ansID, limit, offset) + if err != nil { + if errors.Is(err, sql.ErrNoRows) { + return nil, nil + } + return nil, err + } + return rows, nil +} + +// eventColsPrefixed is eventCols with an `e.` table alias for joined +// queries. +const eventColsPrefixed = `e.id, e.leaf_index, e.leaf_hash, e.event_hash, e.log_id, + e.agent_id, e.ans_name, e.agent_fqdn, e.event_type, + e.schema_version, e.raw_event, e.created_at_ms, + COALESCE(e.identity_id, '') AS identity_id` + +// clampPage normalizes pagination inputs. +func clampPage(limit, offset int) (int, int) { + if limit <= 0 || limit > 200 { + limit = 50 + } + if offset < 0 { + offset = 0 + } + return limit, offset +} + // ExistsByEventHash returns (true, leafIndex) if an event with the // given content hash has already been stored, enabling idempotent // retries at the service layer. The UNIQUE constraint on event_hash diff --git a/internal/adapter/store/sqlitetl/identityevents_test.go b/internal/adapter/store/sqlitetl/identityevents_test.go new file mode 100644 index 0000000..04b364f --- /dev/null +++ b/internal/adapter/store/sqlitetl/identityevents_test.go @@ -0,0 +1,314 @@ +package sqlitetl + +// Tests for the identity read index over tl_events: identity_id +// column population (via the identityIndexed capability), the +// tl_identity_event_agents fan-out, and the join queries the +// badge/audit/reverse views run on. + +import ( + "context" + "encoding/json" + "testing" + + identityevent "github.com/godaddy/ans/internal/tl/event/identity" +) + +// buildIdentityEnvelope assembles a signed-shaped identity envelope. +func buildIdentityEnvelope(t *testing.T, typ identityevent.Type, identityID string, ansIDs []string) *identityevent.Envelope { + t.Helper() + ev := &identityevent.Event{ + EventType: typ, + IdentityID: identityID, + Kind: "did:web", + Value: "did:web:identity.acme-corp.com", + Timestamp: "2026-06-10T15:04:05Z", + } + switch typ { + case identityevent.TypeIdentityVerified, identityevent.TypeIdentityUpdated: + ev.ProviderID = "PID-1" + ev.VerifiedAt = "2026-06-10T15:04:05Z" + ev.Keys = []identityevent.ProvenKey{{ + VerificationMethod: json.RawMessage(`{"id":"did:web:identity.acme-corp.com#key-1","type":"JsonWebKey2020","controller":"did:web:identity.acme-corp.com","publicKeyJwk":{"kty":"OKP","crv":"Ed25519","x":"abc"}}`), + SignedProof: "jws", + }} + case identityevent.TypeIdentityRevoked: + ev.RevokedAt = "2026-06-10T16:00:00Z" + case identityevent.TypeIdentityLinked, identityevent.TypeIdentityUnlinked: + ev.AnsIDs = ansIDs + } + env := identityevent.BuildEnvelope("log-"+identityID, ev, "key-1", "psig") + env.Signature = "tl-attestation" + return env +} + +// storeIdentityEvent appends one identity envelope at the given leaf. +func storeIdentityEvent(t *testing.T, store *EventStore, leaf uint64, env *identityevent.Envelope) { + t.Helper() + canonical, err := env.LeafBytes() + if err != nil { + t.Fatalf("leaf bytes: %v", err) + } + leafHash, err := env.LeafHash() + if err != nil { + t.Fatalf("leaf hash: %v", err) + } + if _, err := store.StoreEvent( + context.Background(), leaf, leafHash, + "event-hash-"+itoa(int(leaf)), env, canonical, + ); err != nil { + t.Fatalf("store event: %v", err) + } +} + +func TestStoreEvent_IdentityEnvelope_IndexesIdentityID(t *testing.T) { + db := newDB(t) + store := NewEventStore(db) + + env := buildIdentityEnvelope(t, identityevent.TypeIdentityVerified, "id-A", nil) + storeIdentityEvent(t, store, 0, env) + + rec, err := store.GetLatestByIdentityID(context.Background(), "id-A") + if err != nil { + t.Fatalf("GetLatestByIdentityID: %v", err) + } + if rec.IdentityID != "id-A" { + t.Errorf("IdentityID = %q", rec.IdentityID) + } + if rec.AgentID != "" { + t.Errorf("AgentID should be empty for identity events, got %q", rec.AgentID) + } + if rec.EventType != "IDENTITY_VERIFIED" { + t.Errorf("EventType = %q", rec.EventType) + } +} + +func TestStoreEvent_AgentRowsHaveEmptyIdentityID(t *testing.T) { + db := newDB(t) + store := NewEventStore(db) + + insertRawEvent(t, db, 0, "agent-1") + rec, err := store.GetEventByLeafIndex(context.Background(), 0) + if err != nil { + t.Fatalf("GetEventByLeafIndex: %v", err) + } + if rec.IdentityID != "" { + t.Errorf("agent row IdentityID = %q, want empty", rec.IdentityID) + } +} + +func TestStoreEvent_LinkFanOut(t *testing.T) { + db := newDB(t) + store := NewEventStore(db) + ctx := context.Background() + + // Verify, then link two agents in ONE event. + storeIdentityEvent(t, store, 0, + buildIdentityEnvelope(t, identityevent.TypeIdentityVerified, "id-A", nil)) + storeIdentityEvent(t, store, 1, + buildIdentityEnvelope(t, identityevent.TypeIdentityLinked, "id-A", []string{"agent-1", "agent-2"})) + + // Both agents see the link. + for _, agent := range []string{"agent-1", "agent-2"} { + states, err := store.LinkStatesByAgent(ctx, agent) + if err != nil { + t.Fatalf("LinkStatesByAgent(%s): %v", agent, err) + } + if len(states) != 1 || !states[0].Linked() || states[0].IdentityID != "id-A" { + t.Fatalf("LinkStatesByAgent(%s) = %+v", agent, states) + } + } + + // Reverse join sees both agents. + states, err := store.LinkStatesByIdentity(ctx, "id-A") + if err != nil { + t.Fatalf("LinkStatesByIdentity: %v", err) + } + if len(states) != 2 { + t.Fatalf("expected 2 link states, got %d", len(states)) + } +} + +func TestLinkStates_LatestWins(t *testing.T) { + db := newDB(t) + store := NewEventStore(db) + ctx := context.Background() + + storeIdentityEvent(t, store, 0, + buildIdentityEnvelope(t, identityevent.TypeIdentityLinked, "id-A", []string{"agent-1"})) + storeIdentityEvent(t, store, 1, + buildIdentityEnvelope(t, identityevent.TypeIdentityUnlinked, "id-A", []string{"agent-1"})) + + states, err := store.LinkStatesByAgent(ctx, "agent-1") + if err != nil { + t.Fatalf("LinkStatesByAgent: %v", err) + } + if len(states) != 1 { + t.Fatalf("expected 1 state, got %d", len(states)) + } + if states[0].Linked() { + t.Fatal("latest event is UNLINKED — Linked() must be false") + } + + // Re-link → live again (UNLINKED rows never block re-linking). + storeIdentityEvent(t, store, 2, + buildIdentityEnvelope(t, identityevent.TypeIdentityLinked, "id-A", []string{"agent-1"})) + states, err = store.LinkStatesByAgent(ctx, "agent-1") + if err != nil { + t.Fatalf("LinkStatesByAgent: %v", err) + } + if len(states) != 1 || !states[0].Linked() { + t.Fatalf("expected live link after re-link, got %+v", states) + } +} + +func TestLinkEventsByAgent_History(t *testing.T) { + db := newDB(t) + store := NewEventStore(db) + ctx := context.Background() + + storeIdentityEvent(t, store, 0, + buildIdentityEnvelope(t, identityevent.TypeIdentityLinked, "id-A", []string{"agent-1"})) + storeIdentityEvent(t, store, 1, + buildIdentityEnvelope(t, identityevent.TypeIdentityUnlinked, "id-A", []string{"agent-1"})) + // A link event for a DIFFERENT agent must not appear. + storeIdentityEvent(t, store, 2, + buildIdentityEnvelope(t, identityevent.TypeIdentityLinked, "id-A", []string{"agent-2"})) + + recs, err := store.LinkEventsByAgent(ctx, "agent-1", 50, 0) + if err != nil { + t.Fatalf("LinkEventsByAgent: %v", err) + } + if len(recs) != 2 { + t.Fatalf("expected 2 history records, got %d", len(recs)) + } + // Newest first. + if recs[0].EventType != "IDENTITY_UNLINKED" || recs[1].EventType != "IDENTITY_LINKED" { + t.Fatalf("history order wrong: %s, %s", recs[0].EventType, recs[1].EventType) + } +} + +func TestGetLatestProofByIdentityID_SkipsLinkEvents(t *testing.T) { + db := newDB(t) + store := NewEventStore(db) + ctx := context.Background() + + storeIdentityEvent(t, store, 0, + buildIdentityEnvelope(t, identityevent.TypeIdentityVerified, "id-A", nil)) + storeIdentityEvent(t, store, 1, + buildIdentityEnvelope(t, identityevent.TypeIdentityLinked, "id-A", []string{"agent-1"})) + + proof, err := store.GetLatestProofByIdentityID(ctx, "id-A") + if err != nil { + t.Fatalf("GetLatestProofByIdentityID: %v", err) + } + if proof.EventType != "IDENTITY_VERIFIED" { + t.Fatalf("latest proof = %s, want IDENTITY_VERIFIED", proof.EventType) + } + + // A rotation supersedes the original proof. + upd := buildIdentityEnvelope(t, identityevent.TypeIdentityUpdated, "id-A", nil) + upd.Payload.Producer.Event.Keys[0].VerificationMethod = json.RawMessage( + `{"id":"did:web:identity.acme-corp.com#key-2","type":"JsonWebKey2020","controller":"did:web:identity.acme-corp.com","publicKeyJwk":{"kty":"OKP","crv":"Ed25519","x":"def"}}`) + storeIdentityEvent(t, store, 2, upd) + + proof, err = store.GetLatestProofByIdentityID(ctx, "id-A") + if err != nil { + t.Fatalf("GetLatestProofByIdentityID after rotation: %v", err) + } + if proof.EventType != "IDENTITY_UPDATED" { + t.Fatalf("latest proof = %s, want IDENTITY_UPDATED", proof.EventType) + } +} + +func TestGetByIdentityID_Pagination(t *testing.T) { + db := newDB(t) + store := NewEventStore(db) + ctx := context.Background() + + storeIdentityEvent(t, store, 0, + buildIdentityEnvelope(t, identityevent.TypeIdentityVerified, "id-A", nil)) + storeIdentityEvent(t, store, 1, + buildIdentityEnvelope(t, identityevent.TypeIdentityLinked, "id-A", []string{"agent-1"})) + storeIdentityEvent(t, store, 2, + buildIdentityEnvelope(t, identityevent.TypeIdentityRevoked, "id-A", nil)) + + all, err := store.GetByIdentityID(ctx, "id-A", 50, 0) + if err != nil { + t.Fatalf("GetByIdentityID: %v", err) + } + if len(all) != 3 { + t.Fatalf("expected 3 events, got %d", len(all)) + } + if all[0].EventType != "IDENTITY_REVOKED" { + t.Fatalf("newest first expected, got %s", all[0].EventType) + } + + page, err := store.GetByIdentityID(ctx, "id-A", 1, 1) + if err != nil { + t.Fatalf("GetByIdentityID paged: %v", err) + } + if len(page) != 1 || page[0].EventType != "IDENTITY_LINKED" { + t.Fatalf("page = %+v", page) + } + + // Unknown identity → empty, no error. + none, err := store.GetByIdentityID(ctx, "id-unknown", 50, 0) + if err != nil { + t.Fatalf("GetByIdentityID unknown: %v", err) + } + if len(none) != 0 { + t.Fatalf("expected no events, got %d", len(none)) + } +} + +func TestGetLatestByIdentityID_NotFound(t *testing.T) { + db := newDB(t) + store := NewEventStore(db) + if _, err := store.GetLatestByIdentityID(context.Background(), "missing"); err == nil { + t.Fatal("expected not-found error") + } +} + +func TestStoreEvent_IdentityDedupAcrossFamilies(t *testing.T) { + db := newDB(t) + store := NewEventStore(db) + ctx := context.Background() + + env := buildIdentityEnvelope(t, identityevent.TypeIdentityVerified, "id-A", nil) + storeIdentityEvent(t, store, 0, env) + + dup, leaf, err := store.ExistsByEventHash(ctx, "event-hash-0") + if err != nil { + t.Fatalf("ExistsByEventHash: %v", err) + } + if !dup || leaf != 0 { + t.Fatalf("dedup miss: dup=%v leaf=%d", dup, leaf) + } +} + +func TestReceiptStore_FindByLeafIndex(t *testing.T) { + db := newDB(t) + receipts := NewReceiptStore(db) + ctx := context.Background() + + if err := receipts.Store(ctx, 7, "id-A", 10, []byte{0xd2, 0x84}); err != nil { + t.Fatalf("store receipt: %v", err) + } + + rec, err := receipts.FindByLeafIndex(ctx, 7, 10) + if err != nil { + t.Fatalf("FindByLeafIndex: %v", err) + } + if rec == nil || rec.AgentID != "id-A" || len(rec.ReceiptBlob) != 2 { + t.Fatalf("unexpected record: %+v", rec) + } + + // Different tree size → miss (nil, nil). + miss, err := receipts.FindByLeafIndex(ctx, 7, 11) + if err != nil { + t.Fatalf("FindByLeafIndex miss: %v", err) + } + if miss != nil { + t.Fatalf("expected cache miss, got %+v", miss) + } +} diff --git a/internal/adapter/store/sqlitetl/migrations/004_identity_events.sql b/internal/adapter/store/sqlitetl/migrations/004_identity_events.sql new file mode 100644 index 0000000..f673117 --- /dev/null +++ b/internal/adapter/store/sqlitetl/migrations/004_identity_events.sql @@ -0,0 +1,47 @@ +-- Identity events: the Verified Identities (the "who") event family. +-- +-- There is ONE transparency log — a single Merkle tree. Identity +-- events append to the same tree as agent events and mirror into the +-- same tl_events table; the "identity stream" is nothing more than a +-- read index over that single log, keyed by identity_id. This is the +-- normative model: streams are read indexes, never separate trees. +-- +-- identity_id is NULL for agent events and set for IDENTITY_* events +-- (whose agent_id / ans_name columns are '' — an identity event names +-- no single agent). The partial index keeps the identity read path +-- O(log n) without taxing the agent-event hot path. +ALTER TABLE tl_events ADD COLUMN identity_id TEXT; + +CREATE INDEX IF NOT EXISTS idx_tl_events_identity_leaf + ON tl_events(identity_id, leaf_index DESC) + WHERE identity_id IS NOT NULL; + +-- tl_identity_event_agents is the read-join index for link events. +-- +-- IDENTITY_LINKED / IDENTITY_UNLINKED seal ONE event on the identity +-- stream carrying the whole batch in ansIds[]; this table fans those +-- ids out so reads can join in both directions: +-- +-- - badge join: "which identities are currently linked to agent X?" +-- → latest row per identity_id for ans_id = X, keep LINKED ones. +-- - association history: "which link events ever named agent X?" +-- → all rows for ans_id = X, resolved to tl_events by leaf_index. +-- +-- The rows are derivable from the log (they mirror sealed events) — +-- this is an index, not a source of truth. Agent streams are never +-- written by identity operations; this table is how reads cross over. +CREATE TABLE IF NOT EXISTS tl_identity_event_agents ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + leaf_index INTEGER NOT NULL, + identity_id TEXT NOT NULL, + ans_id TEXT NOT NULL, + event_type TEXT NOT NULL, -- IDENTITY_LINKED | IDENTITY_UNLINKED + created_at_ms INTEGER NOT NULL, + UNIQUE(leaf_index, ans_id) +); + +CREATE INDEX IF NOT EXISTS idx_tl_identity_event_agents_ans + ON tl_identity_event_agents(ans_id, leaf_index DESC); + +CREATE INDEX IF NOT EXISTS idx_tl_identity_event_agents_identity + ON tl_identity_event_agents(identity_id, leaf_index DESC); diff --git a/internal/adapter/store/sqlitetl/receipts.go b/internal/adapter/store/sqlitetl/receipts.go index 2c01b1c..c6e46eb 100644 --- a/internal/adapter/store/sqlitetl/receipts.go +++ b/internal/adapter/store/sqlitetl/receipts.go @@ -41,24 +41,22 @@ func (s *ReceiptStore) Store( return mapSQLErr(err) } -// FindByAgentID returns the cached receipt for an agent's latest -// event at the given tree size, or (nil, nil) if no cached receipt -// exists. Unlike GetLatestByAgentID this scopes to a specific tree -// size so the ReceiptService can key its cache on (agent, -// treeSize) rather than (leafIndex, treeSize) — agents can have -// multiple events in the log but the receipt we want is always the -// latest one covered by the current checkpoint. -func (s *ReceiptStore) FindByAgentID( +// FindByLeafIndex returns the cached receipt for a specific +// (leaf_index, tree_size) pair — the table's natural UNIQUE key — or +// (nil, nil) if no cached receipt exists. The pair fully determines +// the receipt's payload (same event bytes + same inclusion proof), +// and unlike an agent-keyed lookup it works identically for agent +// and identity subjects (identity events carry no agent id). +func (s *ReceiptStore) FindByLeafIndex( ctx context.Context, - agentID string, + leafIndex uint64, treeSize uint64, ) (*ReceiptRecord, error) { var r ReceiptRecord err := s.db.db.GetContext(ctx, &r, `SELECT id, leaf_index, agent_id, tree_size, receipt_blob, created_at_ms FROM tl_receipts - WHERE agent_id = ? AND tree_size = ? - ORDER BY leaf_index DESC LIMIT 1`, agentID, treeSize) + WHERE leaf_index = ? AND tree_size = ?`, leafIndex, treeSize) if err != nil { if errors.Is(err, sql.ErrNoRows) { return nil, nil //nolint:nilnil // (nil, nil) signals "no cached receipt for this tree size" diff --git a/internal/tl/event/identity/event.go b/internal/tl/event/identity/event.go new file mode 100644 index 0000000..00f2ffe --- /dev/null +++ b/internal/tl/event/identity/event.go @@ -0,0 +1,474 @@ +// Package identity defines the Transparency Log event family for +// Verified Identities — the "who" behind an agent. Identity events +// ride the same producer lane, the same envelope wrapper shape, and +// the same Merkle tree as agent events; what differs is the inner +// event payload (keyed by identityId instead of ansId) and the +// closed eventType vocabulary. +// +// Shape is a contract: sealed events are append-only-forever, so the +// five tokens and the payload fields below must not change once a +// real TL has sealed them. The canonicalization and leaf-hash rules +// are identical to the agent envelope (JCS + RFC 6962 §2.1) — one +// log, one set of verifier rules. +// +// Cross-lane guard: an identity event posted to an agent ingest lane +// fails the agent codec's closed enum (and lacks ansId); an agent +// event posted to the identity lane fails this package's closed enum +// (and lacks identityId). Both reject with 422 INVALID_EVENT, so the +// V1 lane stays frozen and the two V2 families cannot cross. +package identity + +import ( + "crypto/sha256" + "encoding/json" + "errors" + "fmt" + "time" + + anscrypto "github.com/godaddy/ans/internal/crypto" +) + +// SchemaVersion pins the envelope version. Identity events are part +// of the V2 surface (the V2 event-set amendment adds the five +// IDENTITY_* tokens); the V1 lane never carries them. +const SchemaVersion = "V2" + +// Type classifies an identity lifecycle event. The set is closed — +// adding a token is a spec amendment, not a code change. +type Type string + +// The identity event family. Every IDENTITY_* event — proofs, +// rotations, revocations, and links — seals on the identity stream +// (read-indexed by identityId). An identity operation never writes +// to an agent's stream; propagation to linked agents is a read-time +// join. +const ( + // TypeIdentityVerified — first successful control proof. Seals + // every proven key self-verifyingly (public key + signed proof). + TypeIdentityVerified Type = "IDENTITY_VERIFIED" + + // TypeIdentityUpdated — a rotation (PUT + verify-control) + // completed. One event total, regardless of linked-agent count. + TypeIdentityUpdated Type = "IDENTITY_UPDATED" + + // TypeIdentityRevoked — the identity was revoked by its owner. + // Terminal. Linked agents reflect it at the next read. + TypeIdentityRevoked Type = "IDENTITY_REVOKED" + + // TypeIdentityLinked — a batch of the owner's agents was linked. + // One event carries the whole batch in ansIds[]. + TypeIdentityLinked Type = "IDENTITY_LINKED" + + // TypeIdentityUnlinked — an association ended. The event is the + // history; the link rows elsewhere are merely caches. + TypeIdentityUnlinked Type = "IDENTITY_UNLINKED" +) + +// IsValid reports whether t is a recognized identity event type. +func (t Type) IsValid() bool { + switch t { + case TypeIdentityVerified, + TypeIdentityUpdated, + TypeIdentityRevoked, + TypeIdentityLinked, + TypeIdentityUnlinked: + return true + default: + return false + } +} + +// isLink reports whether t is one of the two association events. +func (t Type) isLink() bool { + return t == TypeIdentityLinked || t == TypeIdentityUnlinked +} + +// isProof reports whether t seals a control proof (and therefore +// must carry the proven key set). +func (t Type) isProof() bool { + return t == TypeIdentityVerified || t == TypeIdentityUpdated +} + +// Envelope is the top-level structure appended to the Merkle tree — +// the same wrapper shape as the agent envelope (payload / +// schemaVersion / signature / status) with an identity inner event. +type Envelope struct { + Payload *Payload `json:"payload,omitempty"` + SchemaVersion string `json:"schemaVersion,omitempty"` + Signature string `json:"signature,omitempty"` // TL attestation (detached JWS) + Status string `json:"status,omitempty"` +} + +// Payload wraps the producer's signed event with a TL-assigned logId. +type Payload struct { + LogID string `json:"logId"` + Producer *Producer `json:"producer"` +} + +// Producer records the event together with the producer's identity +// and its detached-JWS attestation over the inner Event. +type Producer struct { + Event *Event `json:"event"` + KeyID string `json:"keyId"` + Signature string `json:"signature"` +} + +// Event is the producer-authored identity event payload. The RA +// JCS-canonicalizes this and signs it; the resulting detached JWS +// lands in Producer.Signature. +type Event struct { + // AnsIDs carries the linked agents' ids on IDENTITY_LINKED / + // IDENTITY_UNLINKED — the whole batch in one event. Forbidden on + // the proof/revocation types: those events never name agents. + AnsIDs []string `json:"ansIds,omitempty"` + + EventType Type `json:"eventType"` + + // IdentityID is the stream key — the RA-assigned UUIDv7 of the + // VerifiedIdentity aggregate. Required on every identity event. + IdentityID string `json:"identityId"` + + // Kind is the identifier kind ("did:web" | "did:key" | "lei"). + Kind string `json:"kind"` + + // Keys is the proven key set — one entry per key the registrant + // proved possession of. Required (non-empty) on the proof events; + // self-verifying: each entry quotes the DID document's + // verification method VERBATIM alongside the registrant's proof. + Keys []ProvenKey `json:"keys,omitempty"` + + // PreviousValue records the pre-rotation identifier value on + // IDENTITY_UPDATED. + PreviousValue string `json:"previousValue,omitempty"` + + // ProofMethod names the control-proof mechanism + // ("did-web-sig" | "did-key-sig" | "lei-vlei-acdc"). + ProofMethod string `json:"proofMethod,omitempty"` + + // ProviderID is the owning principal — the WHO's owner, parallel + // to the agent event's agent.providerId. + ProviderID string `json:"providerId,omitempty"` + + RaID string `json:"raId,omitempty"` + RevokedAt string `json:"revokedAt,omitempty"` + Timestamp string `json:"timestamp"` // RFC3339, required + + // Value is the canonical identifier + // (e.g. "did:web:identity.acme-corp.com"). + Value string `json:"value"` + + VerifiedAt string `json:"verifiedAt,omitempty"` +} + +// ProvenKey is one key the registrant proved possession of, sealed +// self-verifyingly: any third party reads the key material out of +// VerificationMethod, verifies SignedProof against it, then confirms +// the payload decodes to an IdentityProofInput binding this +// identityId + identifier + purpose — offline, without trusting the +// RA. +// +// VerificationMethod is quoted EXACTLY as the DID document served it +// — id, type, controller, and the key material in whichever +// representation the document used (publicKeyJwk, or +// publicKeyMultibase for Multikey documents) — member-for-member, +// values untouched. Nothing derived, re-encoded, or normalized +// enters a seal: the envelope is JCS-canonicalized for signing like +// every event, and JCS preserves member values exactly, so the +// quoted material survives intact. Thumbprints are compute-at-read +// conveniences (anyone can derive RFC 7638 from the sealed source); +// they are never part of the sealed contract. +// +// The postponed lei kind is the one deliberate exception: it will +// seal the subject AID + a key thumbprint only — there is no +// document to quote, the ACDC is PII, and KERI's KEL is already the +// authoritative key history. Seal verbatim what has no other +// tamper-evident home; commit minimally where one exists. +type ProvenKey struct { + // VerificationMethod is the DID document's verification-method + // object, verbatim. + VerificationMethod json.RawMessage `json:"verificationMethod"` + + // SignedProof is the compact JWS the registrant submitted over + // the served IdentityProofInput. + SignedProof string `json:"signedProof"` +} + +// ID extracts the verification-method id from the sealed verbatim +// object — the read-side accessor index/joins use. +func (k ProvenKey) ID() string { + var vm struct { + ID string `json:"id"` + } + if err := json.Unmarshal(k.VerificationMethod, &vm); err != nil { + return "" + } + return vm.ID +} + +// Validate returns nil if the envelope has the minimum fields the TL +// requires to index, de-dupe, and sign the event. It does NOT verify +// the producer signature — that's handled upstream. +func (e *Envelope) Validate() error { + if e == nil { + return errors.New("identityevent: nil envelope") + } + if e.SchemaVersion != SchemaVersion { + return fmt.Errorf("identityevent: schemaVersion must be %q, got %q", SchemaVersion, e.SchemaVersion) + } + if e.Payload == nil { + return errors.New("identityevent: payload required") + } + if e.Payload.LogID == "" { + return errors.New("identityevent: payload.logId required") + } + if e.Payload.Producer == nil { + return errors.New("identityevent: payload.producer required") + } + p := e.Payload.Producer + if p.KeyID == "" { + return errors.New("identityevent: payload.producer.keyId required") + } + if p.Signature == "" { + return errors.New("identityevent: payload.producer.signature required") + } + return p.Event.Validate() +} + +// Validate enforces the per-type required-field matrix: +// +// all types — identityId, kind, value, RFC3339 timestamp +// VERIFIED/UPDATED — non-empty keys[] (each with thumbprint + +// verificationMethodId), verifiedAt, providerId +// REVOKED — revokedAt +// LINKED/UNLINKED — non-empty ansIds[] +// non-link types — ansIds[] forbidden (identity facts never name agents) +func (ev *Event) Validate() error { + if ev == nil { + return errors.New("identityevent: producer.event required") + } + if !ev.EventType.IsValid() { + return fmt.Errorf("identityevent: invalid eventType %q", ev.EventType) + } + if ev.IdentityID == "" { + return errors.New("identityevent: identityId required") + } + if ev.Kind == "" { + return errors.New("identityevent: kind required") + } + if ev.Value == "" { + return errors.New("identityevent: value required") + } + if ev.Timestamp == "" { + return errors.New("identityevent: timestamp required") + } + if _, err := time.Parse(time.RFC3339, ev.Timestamp); err != nil { + return fmt.Errorf("identityevent: timestamp must be RFC3339: %w", err) + } + if ev.EventType.isLink() { + if len(ev.AnsIDs) == 0 { + return fmt.Errorf("identityevent: %s requires non-empty ansIds", ev.EventType) + } + for i, id := range ev.AnsIDs { + if id == "" { + return fmt.Errorf("identityevent: ansIds[%d] is empty", i) + } + } + } else if len(ev.AnsIDs) > 0 { + return fmt.Errorf("identityevent: ansIds forbidden on %s", ev.EventType) + } + if ev.EventType.isProof() { + if err := ev.validateProofFields(); err != nil { + return err + } + } + if ev.EventType == TypeIdentityRevoked && ev.RevokedAt == "" { + return errors.New("identityevent: IDENTITY_REVOKED requires revokedAt") + } + return nil +} + +// validateProofFields enforces the proof-event requirements: a +// non-empty sealed key set (each entry a verbatim verification +// method carrying an id, plus the registrant's proof), verifiedAt, +// and the owning providerId. +func (ev *Event) validateProofFields() error { + if len(ev.Keys) == 0 { + return fmt.Errorf("identityevent: %s requires non-empty keys", ev.EventType) + } + for i, k := range ev.Keys { + if len(k.VerificationMethod) == 0 { + return fmt.Errorf("identityevent: keys[%d].verificationMethod required", i) + } + if k.ID() == "" { + return fmt.Errorf("identityevent: keys[%d].verificationMethod must be an object with an id", i) + } + if k.SignedProof == "" { + return fmt.Errorf("identityevent: keys[%d].signedProof required", i) + } + } + if ev.VerifiedAt == "" { + return fmt.Errorf("identityevent: %s requires verifiedAt", ev.EventType) + } + if ev.ProviderID == "" { + return fmt.Errorf("identityevent: %s requires providerId", ev.EventType) + } + return nil +} + +// SigningInput returns the JCS-canonical bytes of the envelope while +// its outer Signature is still empty — the bytes the TL's attestation +// signer signs. Errors if already signed (signing twice would +// invalidate the prior signature's domain). +func (e *Envelope) SigningInput() ([]byte, error) { + if e.Signature != "" { + return nil, errors.New("identityevent: SigningInput called on already-signed envelope") + } + return canonicalize(e) +} + +// LeafBytes returns the JCS-canonical bytes of the fully signed +// envelope — the bytes Tessera appends to the Merkle tree. +func (e *Envelope) LeafBytes() ([]byte, error) { + if e.Signature == "" { + return nil, errors.New("identityevent: LeafBytes called on unsigned envelope") + } + return canonicalize(e) +} + +// LeafHash returns the RFC 6962 §2.1 leaf hash of the envelope: +// SHA-256(0x00 || LeafBytes()). Identical rule to the agent envelope +// — one tree, one hash discipline. +func (e *Envelope) LeafHash() ([32]byte, error) { + leaf, err := e.LeafBytes() + if err != nil { + return [32]byte{}, err + } + h := sha256.New() + h.Write([]byte{0x00}) + h.Write(leaf) + var out [32]byte + copy(out[:], h.Sum(nil)) + return out, nil +} + +// canonicalize is the shared json.Marshal + JCS wrapper. +func canonicalize(v any) ([]byte, error) { + body, err := json.Marshal(v) + if err != nil { + return nil, fmt.Errorf("identityevent: marshal: %w", err) + } + return anscrypto.Canonicalize(body) +} + +// BuildEnvelope is the single chokepoint for identity-envelope +// construction on the TL side: the TL receives an Event + producer +// signature + producer keyId; it assigns logId and wraps. +func BuildEnvelope(logID string, inner *Event, producerKeyID, producerSignature string) *Envelope { + return &Envelope{ + SchemaVersion: SchemaVersion, + Payload: &Payload{ + LogID: logID, + Producer: &Producer{ + Event: inner, + KeyID: producerKeyID, + Signature: producerSignature, + }, + }, + } +} + +// CanonicalizeEvent JCS-canonicalizes a producer Event — the byte +// sequence the RA signs and the TL re-canonicalizes to verify that +// signature. Exposed so both sides of the RA ↔ TL boundary share the +// exact same canonicalization. +func CanonicalizeEvent(inner *Event) ([]byte, error) { + return canonicalize(inner) +} + +// ----- event.View / event.Signable conformance ----- +// +// The shared ingest pipeline and the SQLite mirror operate on the +// version-agnostic event.View surface. Identity envelopes implement +// it with empty agent-side accessors (an identity event names no +// single agent) and expose the identity-side fields through the +// optional capability methods IdentityID / LinkedAgentIDs, which the +// store discovers by type assertion. + +// Version returns the on-wire schemaVersion tag ("V2"). +func (e *Envelope) Version() string { return e.SchemaVersion } + +// LogID returns the TL-assigned logId, or "". +func (e *Envelope) LogID() string { + if e.Payload != nil { + return e.Payload.LogID + } + return "" +} + +// AgentID returns "" — identity events are not keyed by a single +// agent; linked agents are exposed via LinkedAgentIDs. +func (e *Envelope) AgentID() string { return "" } + +// AnsName returns "" — identity events carry no ANS name. +func (e *Envelope) AnsName() string { return "" } + +// AgentFQDN returns "" — identity events carry no agent host. +func (e *Envelope) AgentFQDN() string { return "" } + +// EventType returns the inner event's type as a string. +func (e *Envelope) EventType() string { + if ev := e.innerEvent(); ev != nil { + return string(ev.EventType) + } + return "" +} + +// Timestamp returns the producer's RFC3339 timestamp. +func (e *Envelope) Timestamp() string { + if ev := e.innerEvent(); ev != nil { + return ev.Timestamp + } + return "" +} + +// ProducerKeyID returns the kid used by the producer to sign. +func (e *Envelope) ProducerKeyID() string { + if e.Payload != nil && e.Payload.Producer != nil { + return e.Payload.Producer.KeyID + } + return "" +} + +// ProducerSignature returns the detached JWS the producer signed. +func (e *Envelope) ProducerSignature() string { + if e.Payload != nil && e.Payload.Producer != nil { + return e.Payload.Producer.Signature + } + return "" +} + +// IdentityID returns the inner event's identityId — the identity +// stream key the SQLite mirror indexes by. +func (e *Envelope) IdentityID() string { + if ev := e.innerEvent(); ev != nil { + return ev.IdentityID + } + return "" +} + +// LinkedAgentIDs returns the ansIds named by a link event (nil for +// the proof/revocation types). The mirror fans these into the +// agent-side read-join index. +func (e *Envelope) LinkedAgentIDs() []string { + if ev := e.innerEvent(); ev != nil { + return ev.AnsIDs + } + return nil +} + +func (e *Envelope) innerEvent() *Event { + if e == nil || e.Payload == nil || e.Payload.Producer == nil { + return nil + } + return e.Payload.Producer.Event +} diff --git a/internal/tl/event/identity/event_test.go b/internal/tl/event/identity/event_test.go new file mode 100644 index 0000000..e46db8a --- /dev/null +++ b/internal/tl/event/identity/event_test.go @@ -0,0 +1,294 @@ +package identity + +import ( + "crypto/sha256" + "encoding/json" + "strings" + "testing" +) + +// validEvent returns a minimal valid event of the given type. +func validEvent(t Type) *Event { + ev := &Event{ + EventType: t, + IdentityID: "01HXKQ00000000000000000000", + Kind: "did:web", + Value: "did:web:identity.acme-corp.com", + Timestamp: "2026-06-10T15:04:05Z", + } + switch { + case t.isProof(): + ev.ProviderID = "PID-8294" + ev.ProofMethod = "did-web-sig" + ev.VerifiedAt = "2026-06-10T15:04:05Z" + ev.Keys = []ProvenKey{{ + VerificationMethod: json.RawMessage(`{"id":"did:web:identity.acme-corp.com#key-1","type":"JsonWebKey2020","controller":"did:web:identity.acme-corp.com","publicKeyJwk":{"kty":"OKP","crv":"Ed25519","x":"abc"}}`), + SignedProof: "eyJhbGciOiJFZERTQSJ9.payload.sig", + }} + case t.isLink(): + ev.AnsIDs = []string{"550e8400-e29b-41d4-a716-446655440000"} + case t == TypeIdentityRevoked: + ev.RevokedAt = "2026-06-10T16:00:00Z" + } + return ev +} + +func TestTypeIsValid(t *testing.T) { + for _, tt := range []Type{ + TypeIdentityVerified, TypeIdentityUpdated, TypeIdentityRevoked, + TypeIdentityLinked, TypeIdentityUnlinked, + } { + if !tt.IsValid() { + t.Errorf("%s should be valid", tt) + } + } + for _, tt := range []Type{"", "AGENT_REGISTERED", "IDENTITY_ADDED", "identity_verified"} { + if tt.IsValid() { + t.Errorf("%s should be invalid", tt) + } + } +} + +func TestEventValidate_AllTypesHappyPath(t *testing.T) { + for _, tt := range []Type{ + TypeIdentityVerified, TypeIdentityUpdated, TypeIdentityRevoked, + TypeIdentityLinked, TypeIdentityUnlinked, + } { + if err := validEvent(tt).Validate(); err != nil { + t.Errorf("%s: unexpected validation error: %v", tt, err) + } + } +} + +func TestEventValidate_RequiredFieldMatrix(t *testing.T) { + cases := []struct { + name string + mutate func(*Event) + base Type + wantSub string + }{ + {"nil event", nil, TypeIdentityVerified, "producer.event required"}, + {"bad type", func(e *Event) { e.EventType = "AGENT_REGISTERED" }, TypeIdentityVerified, "invalid eventType"}, + {"missing identityId", func(e *Event) { e.IdentityID = "" }, TypeIdentityVerified, "identityId required"}, + {"missing kind", func(e *Event) { e.Kind = "" }, TypeIdentityVerified, "kind required"}, + {"missing value", func(e *Event) { e.Value = "" }, TypeIdentityVerified, "value required"}, + {"missing timestamp", func(e *Event) { e.Timestamp = "" }, TypeIdentityVerified, "timestamp required"}, + {"bad timestamp", func(e *Event) { e.Timestamp = "yesterday" }, TypeIdentityVerified, "RFC3339"}, + {"verified without keys", func(e *Event) { e.Keys = nil }, TypeIdentityVerified, "requires non-empty keys"}, + {"updated without keys", func(e *Event) { e.Keys = nil }, TypeIdentityUpdated, "requires non-empty keys"}, + {"key missing verificationMethod", func(e *Event) { e.Keys[0].VerificationMethod = nil }, TypeIdentityVerified, "verificationMethod required"}, + {"verificationMethod without id", func(e *Event) { e.Keys[0].VerificationMethod = json.RawMessage(`{"type":"JsonWebKey2020"}`) }, TypeIdentityVerified, "must be an object with an id"}, + {"key missing signedProof", func(e *Event) { e.Keys[0].SignedProof = "" }, TypeIdentityVerified, "signedProof required"}, + {"verified without verifiedAt", func(e *Event) { e.VerifiedAt = "" }, TypeIdentityVerified, "requires verifiedAt"}, + {"verified without providerId", func(e *Event) { e.ProviderID = "" }, TypeIdentityVerified, "requires providerId"}, + {"revoked without revokedAt", func(e *Event) { e.RevokedAt = "" }, TypeIdentityRevoked, "requires revokedAt"}, + {"linked without ansIds", func(e *Event) { e.AnsIDs = nil }, TypeIdentityLinked, "requires non-empty ansIds"}, + {"unlinked without ansIds", func(e *Event) { e.AnsIDs = nil }, TypeIdentityUnlinked, "requires non-empty ansIds"}, + {"linked with empty ansId entry", func(e *Event) { e.AnsIDs = []string{"a", ""} }, TypeIdentityLinked, "ansIds[1] is empty"}, + {"proof with ansIds", func(e *Event) { e.AnsIDs = []string{"a"} }, TypeIdentityVerified, "ansIds forbidden"}, + {"revoked with ansIds", func(e *Event) { e.AnsIDs = []string{"a"} }, TypeIdentityRevoked, "ansIds forbidden"}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + var ev *Event + if tc.mutate != nil { + ev = validEvent(tc.base) + tc.mutate(ev) + } + err := ev.Validate() + if err == nil { + t.Fatalf("expected error containing %q, got nil", tc.wantSub) + } + if !strings.Contains(err.Error(), tc.wantSub) { + t.Fatalf("expected error containing %q, got %v", tc.wantSub, err) + } + }) + } +} + +func TestEnvelopeValidate(t *testing.T) { + mk := func() *Envelope { + return BuildEnvelope("log-1", validEvent(TypeIdentityVerified), "key-1", "sig-1") + } + + if err := mk().Validate(); err != nil { + t.Fatalf("valid envelope rejected: %v", err) + } + + cases := []struct { + name string + mutate func(*Envelope) + env *Envelope + wantSub string + }{ + {name: "nil envelope", env: nil, wantSub: "nil envelope"}, + {name: "wrong schema", mutate: func(e *Envelope) { e.SchemaVersion = "V1" }, wantSub: "schemaVersion"}, + {name: "missing payload", mutate: func(e *Envelope) { e.Payload = nil }, wantSub: "payload required"}, + {name: "missing logId", mutate: func(e *Envelope) { e.Payload.LogID = "" }, wantSub: "logId required"}, + {name: "missing producer", mutate: func(e *Envelope) { e.Payload.Producer = nil }, wantSub: "producer required"}, + {name: "missing keyId", mutate: func(e *Envelope) { e.Payload.Producer.KeyID = "" }, wantSub: "keyId required"}, + {name: "missing signature", mutate: func(e *Envelope) { e.Payload.Producer.Signature = "" }, wantSub: "signature required"}, + {name: "invalid inner", mutate: func(e *Envelope) { e.Payload.Producer.Event.IdentityID = "" }, wantSub: "identityId required"}, + {name: "nil inner", mutate: func(e *Envelope) { e.Payload.Producer.Event = nil }, wantSub: "producer.event required"}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + env := tc.env + if tc.mutate != nil { + env = mk() + tc.mutate(env) + } + err := env.Validate() + if err == nil || !strings.Contains(err.Error(), tc.wantSub) { + t.Fatalf("expected error containing %q, got %v", tc.wantSub, err) + } + }) + } +} + +func TestSigningLifecycle(t *testing.T) { + env := BuildEnvelope("log-1", validEvent(TypeIdentityVerified), "key-1", "sig-1") + + // LeafBytes before signing is a misuse. + if _, err := env.LeafBytes(); err == nil { + t.Fatal("LeafBytes on unsigned envelope should error") + } + + signingInput, err := env.SigningInput() + if err != nil { + t.Fatalf("SigningInput: %v", err) + } + // The omitempty on the outer Signature is load-bearing: the + // signing input must have no TOP-LEVEL "signature" key (the + // producer's signature under payload.producer is sealed content + // and legitimately present). + var top map[string]json.RawMessage + if err := json.Unmarshal(signingInput, &top); err != nil { + t.Fatalf("signing input not JSON: %v", err) + } + if _, ok := top["signature"]; ok { + t.Fatal("signing input must not contain the top-level signature key") + } + + env.Signature = "tl-attestation-jws" + + // SigningInput after signing is a misuse. + if _, err := env.SigningInput(); err == nil { + t.Fatal("SigningInput on signed envelope should error") + } + + leaf, err := env.LeafBytes() + if err != nil { + t.Fatalf("LeafBytes: %v", err) + } + if !strings.Contains(string(leaf), `"signature":"tl-attestation-jws"`) { + t.Fatal("leaf bytes must contain the outer signature") + } + + // LeafHash = SHA-256(0x00 || leaf) per RFC 6962 §2.1. + h := sha256.New() + h.Write([]byte{0x00}) + h.Write(leaf) + var want [32]byte + copy(want[:], h.Sum(nil)) + got, err := env.LeafHash() + if err != nil { + t.Fatalf("LeafHash: %v", err) + } + if got != want { + t.Fatal("leaf hash mismatch with independent RFC 6962 computation") + } +} + +func TestLeafHashOnUnsignedEnvelope(t *testing.T) { + env := BuildEnvelope("log-1", validEvent(TypeIdentityVerified), "key-1", "sig-1") + if _, err := env.LeafHash(); err == nil { + t.Fatal("LeafHash on unsigned envelope should error") + } +} + +func TestCanonicalizeEventDeterministic(t *testing.T) { + ev := validEvent(TypeIdentityLinked) + a, err := CanonicalizeEvent(ev) + if err != nil { + t.Fatalf("canonicalize: %v", err) + } + b, err := CanonicalizeEvent(ev) + if err != nil { + t.Fatalf("canonicalize: %v", err) + } + if string(a) != string(b) { + t.Fatal("canonicalization must be deterministic") + } + // JCS sorts keys: ansIds before eventType before identityId. + s := string(a) + if strings.Index(s, `"ansIds"`) >= strings.Index(s, `"eventType"`) || + strings.Index(s, `"eventType"`) >= strings.Index(s, `"identityId"`) { + t.Fatalf("canonical bytes not JCS-sorted: %s", s) + } +} + +func TestViewAccessors(t *testing.T) { + ev := validEvent(TypeIdentityLinked) + env := BuildEnvelope("log-9", ev, "key-9", "psig-9") + + if env.Version() != "V2" { + t.Errorf("Version = %q", env.Version()) + } + if env.LogID() != "log-9" { + t.Errorf("LogID = %q", env.LogID()) + } + if env.AgentID() != "" || env.AnsName() != "" || env.AgentFQDN() != "" { + t.Error("agent-side accessors must be empty on identity envelopes") + } + if env.EventType() != "IDENTITY_LINKED" { + t.Errorf("EventType = %q", env.EventType()) + } + if env.Timestamp() != ev.Timestamp { + t.Errorf("Timestamp = %q", env.Timestamp()) + } + if env.ProducerKeyID() != "key-9" || env.ProducerSignature() != "psig-9" { + t.Error("producer accessors mismatch") + } + if env.IdentityID() != ev.IdentityID { + t.Errorf("IdentityID = %q", env.IdentityID()) + } + if got := env.LinkedAgentIDs(); len(got) != 1 || got[0] != ev.AnsIDs[0] { + t.Errorf("LinkedAgentIDs = %v", got) + } +} + +func TestViewAccessorsOnEmptyEnvelope(t *testing.T) { + var nilEnv *Envelope + if nilEnv.innerEvent() != nil { + t.Fatal("nil envelope must have nil inner event") + } + empty := &Envelope{} + if empty.LogID() != "" || empty.EventType() != "" || empty.Timestamp() != "" || + empty.ProducerKeyID() != "" || empty.ProducerSignature() != "" || + empty.IdentityID() != "" || empty.LinkedAgentIDs() != nil { + t.Fatal("accessors on empty envelope must return zero values") + } +} + +func TestJSONRoundTrip(t *testing.T) { + env := BuildEnvelope("log-1", validEvent(TypeIdentityVerified), "key-1", "sig-1") + env.Signature = "outer" + raw, err := json.Marshal(env) + if err != nil { + t.Fatalf("marshal: %v", err) + } + var back Envelope + if err := json.Unmarshal(raw, &back); err != nil { + t.Fatalf("unmarshal: %v", err) + } + if err := back.Validate(); err != nil { + t.Fatalf("round-tripped envelope invalid: %v", err) + } + if back.IdentityID() != env.IdentityID() { + t.Fatal("identityId lost in round trip") + } + if len(back.Payload.Producer.Event.Keys) != 1 { + t.Fatal("keys lost in round trip") + } +} diff --git a/internal/tl/handler/empty_param_identity_test.go b/internal/tl/handler/empty_param_identity_test.go new file mode 100644 index 0000000..8964172 --- /dev/null +++ b/internal/tl/handler/empty_param_identity_test.go @@ -0,0 +1,42 @@ +package handler_test + +// Empty-path-param guards for the identity read surface — siblings of +// the agent-route cases in empty_param_test.go. Each guard fires +// before any service is touched, so a fully-nil Handlers suffices. + +import ( + "net/http" + "net/http/httptest" + "testing" + + "github.com/godaddy/ans/internal/tl/handler" +) + +func TestIdentityRoutes_EmptyParams(t *testing.T) { + t.Parallel() + h := handler.NewHandlers(nil, nil, nil, nil, nil, nil, nil, nil) + + cases := []struct { + name string + param string + path string + handler func(http.ResponseWriter, *http.Request) + }{ + {"identity badge", "identityId", "/v1/identities/", h.GetIdentityBadge}, + {"identity audit", "identityId", "/v1/identities//audit", h.GetIdentityAudit}, + {"identity receipt", "identityId", "/v1/identities//receipt", h.GetIdentityReceipt}, + {"identity agents", "identityId", "/v1/identities//agents", h.GetIdentityAgents}, + {"agent identities", "agentId", "/v1/agents//identities", h.GetAgentIdentities}, + {"agent identity history", "agentId", "/v1/agents//identities/history", h.GetAgentIdentityHistory}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + t.Parallel() + rec := httptest.NewRecorder() + tc.handler(rec, emptyParamReq(http.MethodGet, tc.path, tc.param)) + if rec.Code != http.StatusUnprocessableEntity { + t.Errorf("status: got %d want 422", rec.Code) + } + }) + } +} diff --git a/internal/tl/handler/empty_param_test.go b/internal/tl/handler/empty_param_test.go index 6110409..6eff3ab 100644 --- a/internal/tl/handler/empty_param_test.go +++ b/internal/tl/handler/empty_param_test.go @@ -35,7 +35,7 @@ func TestGetBadge_EmptyAgentID(t *testing.T) { t.Parallel() // Build a Handlers with all services nil; the empty-agentID guard // fires before any service is touched. - h := handler.NewHandlers(nil, nil, nil, nil, nil, nil, nil) + h := handler.NewHandlers(nil, nil, nil, nil, nil, nil, nil, nil) rec := httptest.NewRecorder() h.GetBadge(rec, emptyParamReq(http.MethodGet, "/v1/agents/", "agentId")) if rec.Code != http.StatusUnprocessableEntity { @@ -45,7 +45,7 @@ func TestGetBadge_EmptyAgentID(t *testing.T) { func TestGetAudit_EmptyAgentID(t *testing.T) { t.Parallel() - h := handler.NewHandlers(nil, nil, nil, nil, nil, nil, nil) + h := handler.NewHandlers(nil, nil, nil, nil, nil, nil, nil, nil) rec := httptest.NewRecorder() h.GetAudit(rec, emptyParamReq(http.MethodGet, "/v1/agents//audit", "agentId")) if rec.Code != http.StatusUnprocessableEntity { @@ -55,7 +55,7 @@ func TestGetAudit_EmptyAgentID(t *testing.T) { func TestGetReceipt_EmptyAgentID(t *testing.T) { t.Parallel() - h := handler.NewHandlers(nil, nil, nil, nil, nil, nil, nil) + h := handler.NewHandlers(nil, nil, nil, nil, nil, nil, nil, nil) rec := httptest.NewRecorder() h.GetReceipt(rec, emptyParamReq(http.MethodGet, "/v1/agents//receipt", "agentId")) if rec.Code != http.StatusUnprocessableEntity { @@ -69,7 +69,7 @@ func TestGetReceipt_EmptyAgentID(t *testing.T) { // unwired and clients see 501 rather than a misleading 500. func TestGetStatusToken_DisabledReturns501(t *testing.T) { t.Parallel() - h := handler.NewHandlers(nil, nil, nil, nil, nil, nil, nil) + h := handler.NewHandlers(nil, nil, nil, nil, nil, nil, nil, nil) rec := httptest.NewRecorder() h.GetStatusToken(rec, emptyParamReq(http.MethodGet, "/v1/agents/x/status-token", "agentId")) if rec.Code != http.StatusNotImplemented { @@ -84,7 +84,7 @@ func TestGetStatusToken_DisabledReturns501(t *testing.T) { // "h.checkpoint == nil" branch. func TestGetCheckpointJSON_DisabledReturnsInternal(t *testing.T) { t.Parallel() - h := handler.NewHandlers(nil, nil, nil, nil, nil, nil, nil) + h := handler.NewHandlers(nil, nil, nil, nil, nil, nil, nil, nil) rec := httptest.NewRecorder() h.GetCheckpointJSON(rec, httptest.NewRequest(http.MethodGet, "/v1/log/checkpoint", nil)) if rec.Code != http.StatusInternalServerError { @@ -96,7 +96,7 @@ func TestGetCheckpointJSON_DisabledReturnsInternal(t *testing.T) { // same guard on the history route. func TestGetCheckpointHistory_DisabledReturnsInternal(t *testing.T) { t.Parallel() - h := handler.NewHandlers(nil, nil, nil, nil, nil, nil, nil) + h := handler.NewHandlers(nil, nil, nil, nil, nil, nil, nil, nil) rec := httptest.NewRecorder() h.GetCheckpointHistory(rec, httptest.NewRequest(http.MethodGet, "/v1/log/checkpoint/history", nil)) if rec.Code != http.StatusInternalServerError { @@ -108,7 +108,7 @@ func TestGetCheckpointHistory_DisabledReturnsInternal(t *testing.T) { // "h.schema == nil" branch. func TestGetSchema_DisabledReturnsInternal(t *testing.T) { t.Parallel() - h := handler.NewHandlers(nil, nil, nil, nil, nil, nil, nil) + h := handler.NewHandlers(nil, nil, nil, nil, nil, nil, nil, nil) rec := httptest.NewRecorder() h.GetSchema(rec, emptyParamReq(http.MethodGet, "/v1/log/schema/V2", "version")) if rec.Code != http.StatusInternalServerError { diff --git a/internal/tl/handler/handler.go b/internal/tl/handler/handler.go index 910f74b..88b4566 100644 --- a/internal/tl/handler/handler.go +++ b/internal/tl/handler/handler.go @@ -46,12 +46,13 @@ import ( // Handlers groups the TL HTTP handlers. type Handlers struct { - log *service.LogService - badge *service.BadgeService - receipt *service.ReceiptService - statusToken *service.StatusTokenService // nil if status tokens are disabled - checkpoint *service.CheckpointService - schema *service.SchemaService + log *service.LogService + badge *service.BadgeService + identityBadge *service.IdentityBadgeService + receipt *service.ReceiptService + statusToken *service.StatusTokenService // nil if status tokens are disabled + checkpoint *service.CheckpointService + schema *service.SchemaService // rootKeysBody is the pre-rendered text/plain body for the // /root-keys endpoint — one verification-line per // active TL verifier, in the order the operator wired them. @@ -72,6 +73,7 @@ type Handlers struct { func NewHandlers( log *service.LogService, badge *service.BadgeService, + identityBadge *service.IdentityBadgeService, receipt *service.ReceiptService, statusToken *service.StatusTokenService, checkpoint *service.CheckpointService, @@ -79,13 +81,14 @@ func NewHandlers( rootKeysBody []byte, ) *Handlers { return &Handlers{ - log: log, - badge: badge, - receipt: receipt, - statusToken: statusToken, - checkpoint: checkpoint, - schema: schema, - rootKeysBody: rootKeysBody, + log: log, + badge: badge, + identityBadge: identityBadge, + receipt: receipt, + statusToken: statusToken, + checkpoint: checkpoint, + schema: schema, + rootKeysBody: rootKeysBody, } } @@ -105,12 +108,33 @@ func (h *Handlers) Mount(r chi.Router, tileRoot string) { r.Post("/v1/internal/agents/event", h.AppendEventV1) r.Post("/v2/internal/agents/event", h.AppendEventV2) + // Identity ingest — the IDENTITY_* event family rides the same + // producer-signature lane into the same Merkle tree; the + // dedicated route exists because the payload schema differs + // (keyed by identityId) and the closed enums are the cross-lane + // guard. + r.Post("/v1/internal/identities/event", h.AppendEventIdentity) + // Agent-scoped reads. Reference swagger §78-308. r.Get("/v1/agents/{agentId}", h.GetBadge) r.Get("/v1/agents/{agentId}/audit", h.GetAudit) r.Get("/v1/agents/{agentId}/receipt", h.GetReceipt) r.Get("/v1/agents/{agentId}/status-token", h.GetStatusToken) + // Agent-side computed identity views — read-time joins through + // the link events' agent index. Never stored on the agent; the + // agent's own audit stays purely AGENT_*. + r.Get("/v1/agents/{agentId}/identities", h.GetAgentIdentities) + r.Get("/v1/agents/{agentId}/identities/history", h.GetAgentIdentityHistory) + + // Identity-stream reads — same response shapes as the agent + // reads (badge / audit envelope / COSE receipt), keyed by + // identityId, plus the reverse join to currently-linked agents. + r.Get("/v1/identities/{identityId}", h.GetIdentityBadge) + r.Get("/v1/identities/{identityId}/audit", h.GetIdentityAudit) + r.Get("/v1/identities/{identityId}/receipt", h.GetIdentityReceipt) + r.Get("/v1/identities/{identityId}/agents", h.GetIdentityAgents) + // Log metadata (JSON variants). Reference swagger §310-461. r.Get("/v1/log/checkpoint", h.GetCheckpointJSON) r.Get("/v1/log/checkpoint/history", h.GetCheckpointHistory) @@ -164,6 +188,15 @@ func (h *Handlers) AppendEventV2(w http.ResponseWriter, r *http.Request) { h.appendEvent(w, r, h.log.AppendV2) } +// AppendEventIdentity is the identity-family ingest entrypoint — +// POST /v1/internal/identities/event. Same contract as the agent +// lanes (raw inner-event body + X-Signature detached JWS, 256 KiB +// cap); the identity codec enforces the IDENTITY_* enum and the +// identityId keyspace. +func (h *Handlers) AppendEventIdentity(w http.ResponseWriter, r *http.Request) { + h.appendEvent(w, r, h.log.AppendIdentity) +} + // appendEvent is the shared body-read / response-shape glue; the // passed-in `appendFn` picks which LogService path runs. func (h *Handlers) appendEvent( @@ -212,7 +245,11 @@ func (h *Handlers) appendEvent( // GetBadge handles GET /v1/agents/{agentId}. Returns the // reference-shaped TransparencyLog JSON: merkleProof, payload (the // V1 envelope's payload piece), schemaVersion, signature (TL -// attestation), status. +// attestation), status — plus the computed identities[] join (the +// agent's currently-linked verified identities, decorated with each +// identity's current stream state). The join is read-time: rotation +// and revocation on the identity stream are visible here immediately +// with zero agent-stream writes. func (h *Handlers) GetBadge(w http.ResponseWriter, r *http.Request) { agentID := chi.URLParam(r, "agentId") if agentID == "" { @@ -224,9 +261,143 @@ func (h *Handlers) GetBadge(w http.ResponseWriter, r *http.Request) { writeError(w, err) return } + if h.identityBadge != nil { + identities, jerr := h.identityBadge.LinkedIdentitiesForAgent(r.Context(), agentID) + if jerr != nil { + writeError(w, jerr) + return + } + tl.Identities = identities + } writeJSON(w, http.StatusOK, tl) } +// GetAgentIdentities handles GET /v1/agents/{agentId}/identities — +// the computed list of identities currently linked to the agent. +// Identical entries to the badge's identities[] field, served alone +// for callers who don't need the full badge. +func (h *Handlers) GetAgentIdentities(w http.ResponseWriter, r *http.Request) { + agentID := chi.URLParam(r, "agentId") + if agentID == "" { + writeError(w, domain.NewValidationError("MISSING_AGENT_ID", "agentId is required")) + return + } + identities, err := h.identityBadge.LinkedIdentitiesForAgent(r.Context(), agentID) + if err != nil { + writeError(w, err) + return + } + writeJSON(w, http.StatusOK, map[string]any{"identities": identities}) +} + +// GetAgentIdentityHistory handles +// GET /v1/agents/{agentId}/identities/history — the link/unlink +// events that ever named this agent, in the standard audit envelope +// (each record a TransparencyLog), filtered through the agent index. +// Past and present associations fall out of reading those events; +// current state is the computed identities[] join. +func (h *Handlers) GetAgentIdentityHistory(w http.ResponseWriter, r *http.Request) { + agentID := chi.URLParam(r, "agentId") + if agentID == "" { + writeError(w, domain.NewValidationError("MISSING_AGENT_ID", "agentId is required")) + return + } + limit, offset := parsePagination(r) + records, err := h.identityBadge.LinkHistoryForAgent(r.Context(), agentID, limit, offset) + if err != nil { + writeError(w, err) + return + } + writeJSON(w, http.StatusOK, map[string]any{"records": records}) +} + +// GetIdentityBadge handles GET /v1/identities/{identityId} — the +// identity badge: latest sealed identity event + inclusion proof + +// computed status (VERIFIED | REVOKED). +func (h *Handlers) GetIdentityBadge(w http.ResponseWriter, r *http.Request) { + identityID := chi.URLParam(r, "identityId") + if identityID == "" { + writeError(w, domain.NewValidationError("MISSING_IDENTITY_ID", "identityId is required")) + return + } + tl, err := h.identityBadge.Get(r.Context(), identityID) + if err != nil { + writeError(w, err) + return + } + writeJSON(w, http.StatusOK, tl) +} + +// GetIdentityAudit handles GET /v1/identities/{identityId}/audit — +// the identity's full event chain in the same audit envelope as the +// agent audit ({ records: [TransparencyLog, ...] }). +func (h *Handlers) GetIdentityAudit(w http.ResponseWriter, r *http.Request) { + identityID := chi.URLParam(r, "identityId") + if identityID == "" { + writeError(w, domain.NewValidationError("MISSING_IDENTITY_ID", "identityId is required")) + return + } + limit, offset := parsePagination(r) + records, err := h.identityBadge.Audit(r.Context(), identityID, limit, offset) + if err != nil { + writeError(w, err) + return + } + writeJSON(w, http.StatusOK, map[string]any{"records": records}) +} + +// GetIdentityReceipt handles GET /v1/identities/{identityId}/receipt +// — a SCITT COSE_Sign1 receipt for the identity's latest sealed +// event. Same machinery, content type, and 503 retry semantics as +// the agent receipt. +func (h *Handlers) GetIdentityReceipt(w http.ResponseWriter, r *http.Request) { + identityID := chi.URLParam(r, "identityId") + if identityID == "" { + writeError(w, domain.NewValidationError("MISSING_IDENTITY_ID", "identityId is required")) + return + } + rec, err := h.receipt.ForIdentity(r.Context(), identityID) + if err != nil { + if errors.Is(err, service.ErrLeafNotYetCovered) { + w.Header().Set("Retry-After", "2") + writeJSON(w, http.StatusServiceUnavailable, problem{ + Type: "about:blank", + Title: "Receipt Not Yet Available", + Status: http.StatusServiceUnavailable, + Code: "TL_LEAF_UNCOMMITTED", + Detail: "leaf committed but no signed checkpoint yet covers it; retry after the Retry-After delay", + }) + return + } + writeError(w, err) + return + } + w.Header().Set("Content-Type", rec.ContentType) + w.Header().Set("Content-Length", strconv.Itoa(len(rec.Bytes))) + w.WriteHeader(http.StatusOK) + // nosemgrep: go.lang.security.audit.xss.no-direct-write-to-responsewriter.no-direct-write-to-responsewriter + // Binary COSE_Sign1 receipt (application/scitt-receipt+cose); not HTML, no user-controlled input. + _, _ = w.Write(rec.Bytes) //nolint:gosec // G705: binary receipt body, no XSS surface +} + +// GetIdentityAgents handles GET /v1/identities/{identityId}/agents — +// the reverse join: the agents this identity currently links to, +// each decorated with its own computed badge status so a reader +// checks both ends of the link in one response. +func (h *Handlers) GetIdentityAgents(w http.ResponseWriter, r *http.Request) { + identityID := chi.URLParam(r, "identityId") + if identityID == "" { + writeError(w, domain.NewValidationError("MISSING_IDENTITY_ID", "identityId is required")) + return + } + agents, err := h.identityBadge.LinkedAgentsForIdentity(r.Context(), identityID) + if err != nil { + writeError(w, err) + return + } + writeJSON(w, http.StatusOK, map[string]any{"agents": agents}) +} + // GetAudit handles GET /v1/agents/{agentId}/audit. Matches the // reference TransparencyLogAudit shape — { records: [TransparencyLog, ...] }. func (h *Handlers) GetAudit(w http.ResponseWriter, r *http.Request) { diff --git a/internal/tl/handler/handler_identity_test.go b/internal/tl/handler/handler_identity_test.go new file mode 100644 index 0000000..18b79ce --- /dev/null +++ b/internal/tl/handler/handler_identity_test.go @@ -0,0 +1,407 @@ +package handler_test + +// Integration tests for the identity event family through the full +// TL HTTP surface: the /v1/internal/identities/event ingest lane, +// the identity badge/audit/receipt/agents reads, the agent badge's +// computed identities[] join, and the cross-lane guards. + +import ( + "encoding/json" + "net/http" + "net/http/httptest" + "testing" + "time" + + identityevent "github.com/godaddy/ans/internal/tl/event/identity" + receiptpkg "github.com/godaddy/ans/internal/tl/receipt" +) + +const testIdentityID = "01HXKQTESTIDENTITY0000000A" + +// identityInner returns a valid identity event of the given type, +// keyed to the testbed's identity fixture. timestamps vary per call +// site so dedup never collides across events in one test; proof +// events name their verification method by the given kid so the +// read-join's provenKeyIds visibly flips on rotation. +func identityInner(typ identityevent.Type, ts string, ansIDs []string, kid string) identityevent.Event { + ev := identityevent.Event{ + EventType: typ, + IdentityID: testIdentityID, + Kind: "did:web", + Value: "did:web:identity.acme-corp.com", + RaID: "ra-test-1", + Timestamp: ts, + } + switch typ { + case identityevent.TypeIdentityVerified, identityevent.TypeIdentityUpdated: + ev.ProviderID = "PID-1" + ev.ProofMethod = "did-web-sig" + ev.VerifiedAt = ts + ev.Keys = []identityevent.ProvenKey{{ + VerificationMethod: json.RawMessage(`{"id":"` + kid + `","type":"JsonWebKey2020","controller":"did:web:identity.acme-corp.com","publicKeyJwk":{"crv":"Ed25519","kty":"OKP","x":"abc"}}`), + SignedProof: "eyJhbGciOiJFZERTQSJ9.p.s", + }} + case identityevent.TypeIdentityRevoked: + ev.RevokedAt = ts + case identityevent.TypeIdentityLinked, identityevent.TypeIdentityUnlinked: + ev.AnsIDs = ansIDs + } + return ev +} + +// postIdentityEvent signs and posts an identity event body to the +// identity ingest lane, asserting 200. +func postIdentityEvent(t *testing.T, tb *tlTestbed, ev identityevent.Event) { + t.Helper() + body := []byte(mustJSON(t, ev)) + rec := tb.postTo(t, "/v1/internal/identities/event", body, tb.signWithProducer(t, body)) + if rec.Code != http.StatusOK { + t.Fatalf("identity ingest: got %d, body=%s", rec.Code, rec.Body) + } +} + +func getJSON(t *testing.T, tb *tlTestbed, path string, out any) int { + t.Helper() + req := httptest.NewRequest(http.MethodGet, path, nil) + rec := httptest.NewRecorder() + tb.router.ServeHTTP(rec, req) + if rec.Code == http.StatusOK && out != nil { + if err := json.Unmarshal(rec.Body.Bytes(), out); err != nil { + t.Fatalf("decode %s: %v (body=%s)", path, err, rec.Body) + } + } + return rec.Code +} + +// badgeView decodes the subset of the badge/audit responses the +// tests assert on. +type badgeView struct { + SchemaVersion string `json:"schemaVersion"` + Status string `json:"status"` + Signature string `json:"signature"` + Identities []struct { + IdentityID string `json:"identityId"` + Kind string `json:"kind"` + Value string `json:"value"` + IdentityStatus string `json:"identityStatus"` + ProvenKeyIDs []string `json:"provenKeyIds"` + LinkedAt string `json:"linkedAt"` + LinkLogID string `json:"linkLogId"` + IdentityLogID string `json:"identityLogId"` + } `json:"identities"` +} + +// auditView decodes the subset of audit responses the stages assert +// on. +type auditView struct { + Records []struct { + Payload struct { + Producer struct { + Event struct { + EventType string `json:"eventType"` + } `json:"event"` + } `json:"producer"` + } `json:"payload"` + } `json:"records"` +} + +// TestIdentityLifecycle_EndToEnd drives the whole identity read +// surface through real ingests: verify → link → rotate → revoke → +// unlink, asserting the computed joins after each stage. +func TestIdentityLifecycle_EndToEnd(t *testing.T) { + tb := newTLTestbed(t) + + // Seed the agent the identity will link to (the testbed's agent + // fixture) so the agent badge exists. + agentBody := []byte(mustJSON(t, tb.inner)) + tb.postEvent(t, agentBody, tb.signWithProducer(t, agentBody)) + agentID := tb.inner.AnsID + + stageVerify(t, tb, agentID) + stageLink(t, tb, agentID) + stageRotate(t, tb, agentID) + stageRevoke(t, tb, agentID) + stageUnlink(t, tb, agentID) +} + +// stageVerify seals IDENTITY_VERIFIED and checks the identity badge +// plus the (still identity-free) agent badge. +func stageVerify(t *testing.T, tb *tlTestbed, agentID string) { + t.Helper() + postIdentityEvent(t, tb, + identityInner(identityevent.TypeIdentityVerified, "2026-06-10T10:00:00Z", nil, "did:web:identity.acme-corp.com#key-1")) + + var idBadge badgeView + if code := getJSON(t, tb, "/v1/identities/"+testIdentityID, &idBadge); code != http.StatusOK { + t.Fatalf("identity badge: %d", code) + } + if idBadge.Status != "VERIFIED" { + t.Fatalf("identity status = %q, want VERIFIED", idBadge.Status) + } + if idBadge.SchemaVersion != "V2" || idBadge.Signature == "" { + t.Fatalf("identity badge missing schema/attestation: %+v", idBadge) + } + + var agentBadge badgeView + if code := getJSON(t, tb, "/v1/agents/"+agentID, &agentBadge); code != http.StatusOK { + t.Fatalf("agent badge: %d", code) + } + if len(agentBadge.Identities) != 0 { + t.Fatalf("agent badge identities before link: %+v", agentBadge.Identities) + } +} + +// stageLink seals IDENTITY_LINKED and checks the join in both +// directions. +func stageLink(t *testing.T, tb *tlTestbed, agentID string) { + t.Helper() + postIdentityEvent(t, tb, + identityInner(identityevent.TypeIdentityLinked, "2026-06-10T11:00:00Z", []string{agentID}, "")) + + var agentBadge badgeView + if code := getJSON(t, tb, "/v1/agents/"+agentID, &agentBadge); code != http.StatusOK { + t.Fatalf("agent badge after link: %d", code) + } + if len(agentBadge.Identities) != 1 { + t.Fatalf("agent badge identities after link: %+v", agentBadge.Identities) + } + got := agentBadge.Identities[0] + if got.IdentityID != testIdentityID || got.IdentityStatus != "VERIFIED" || + got.Kind != "did:web" || got.Value != "did:web:identity.acme-corp.com" { + t.Fatalf("join entry wrong: %+v", got) + } + if len(got.ProvenKeyIDs) != 1 || got.ProvenKeyIDs[0] != "did:web:identity.acme-corp.com#key-1" { + t.Fatalf("provenKeyIds = %v", got.ProvenKeyIDs) + } + if got.LinkedAt != "2026-06-10T11:00:00Z" || got.LinkLogID == "" || got.IdentityLogID == "" { + t.Fatalf("link evidence fields missing: %+v", got) + } + + // The standalone computed view matches the badge join. + var identitiesResp struct { + Identities []json.RawMessage `json:"identities"` + } + if code := getJSON(t, tb, "/v1/agents/"+agentID+"/identities", &identitiesResp); code != http.StatusOK { + t.Fatalf("agent identities view: %d", code) + } + if len(identitiesResp.Identities) != 1 { + t.Fatalf("agent identities view count = %d", len(identitiesResp.Identities)) + } + + // Reverse join: identity → agents, with the agent's own status. + var agentsResp struct { + Agents []struct { + AnsID string `json:"ansId"` + AgentStatus string `json:"agentStatus"` + LinkedAt string `json:"linkedAt"` + } `json:"agents"` + } + if code := getJSON(t, tb, "/v1/identities/"+testIdentityID+"/agents", &agentsResp); code != http.StatusOK { + t.Fatalf("identity agents view: %d", code) + } + if len(agentsResp.Agents) != 1 || agentsResp.Agents[0].AnsID != agentID || + agentsResp.Agents[0].AgentStatus != "ACTIVE" { + t.Fatalf("reverse join: %+v", agentsResp.Agents) + } +} + +// stageRotate seals IDENTITY_UPDATED and checks the proven-key flip +// plus the stream-purity invariants on both audits. +func stageRotate(t *testing.T, tb *tlTestbed, agentID string) { + t.Helper() + postIdentityEvent(t, tb, + identityInner(identityevent.TypeIdentityUpdated, "2026-06-10T12:00:00Z", nil, "did:web:identity.acme-corp.com#key-2")) + + var agentBadge badgeView + if code := getJSON(t, tb, "/v1/agents/"+agentID, &agentBadge); code != http.StatusOK { + t.Fatalf("agent badge after rotation: %d", code) + } + if ids := agentBadge.Identities[0].ProvenKeyIDs; len(ids) != 1 || ids[0] != "did:web:identity.acme-corp.com#key-2" { + t.Fatalf("rotation not visible on linked badge: %v", ids) + } + + // The agent's own audit history stays purely AGENT_* — identity + // operations never write to the agent stream. + var audit auditView + if code := getJSON(t, tb, "/v1/agents/"+agentID+"/audit", &audit); code != http.StatusOK { + t.Fatalf("agent audit: %d", code) + } + if len(audit.Records) != 1 || audit.Records[0].Payload.Producer.Event.EventType != "AGENT_REGISTERED" { + t.Fatalf("agent audit polluted by identity ops: %+v", audit.Records) + } + + // Identity audit carries the full chain, newest first. + if code := getJSON(t, tb, "/v1/identities/"+testIdentityID+"/audit", &audit); code != http.StatusOK { + t.Fatalf("identity audit: %d", code) + } + if len(audit.Records) != 3 { + t.Fatalf("identity audit count = %d, want 3", len(audit.Records)) + } + if audit.Records[0].Payload.Producer.Event.EventType != "IDENTITY_UPDATED" { + t.Fatalf("identity audit order: %+v", audit.Records[0]) + } + + // Association history for the agent: the standard audit envelope + // filtered to link events naming it. + if code := getJSON(t, tb, "/v1/agents/"+agentID+"/identities/history", &audit); code != http.StatusOK { + t.Fatalf("identity history: %d", code) + } + if len(audit.Records) != 1 || audit.Records[0].Payload.Producer.Event.EventType != "IDENTITY_LINKED" { + t.Fatalf("identity history: %+v", audit.Records) + } +} + +// stageRevoke seals IDENTITY_REVOKED: one event, every linked badge +// reflects it; the agent itself is untouched. +func stageRevoke(t *testing.T, tb *tlTestbed, agentID string) { + t.Helper() + postIdentityEvent(t, tb, + identityInner(identityevent.TypeIdentityRevoked, "2026-06-10T13:00:00Z", nil, "")) + + var idBadge badgeView + if code := getJSON(t, tb, "/v1/identities/"+testIdentityID, &idBadge); code != http.StatusOK { + t.Fatalf("identity badge after revoke: %d", code) + } + if idBadge.Status != "REVOKED" { + t.Fatalf("identity status after revoke = %q", idBadge.Status) + } + var agentBadge badgeView + if code := getJSON(t, tb, "/v1/agents/"+agentID, &agentBadge); code != http.StatusOK { + t.Fatalf("agent badge after revoke: %d", code) + } + if len(agentBadge.Identities) != 1 || agentBadge.Identities[0].IdentityStatus != "REVOKED" { + t.Fatalf("revocation not visible on linked badge: %+v", agentBadge.Identities) + } + // The agent itself is untouched (the what survives the who's + // revocation, and vice versa). + if agentBadge.Status != "ACTIVE" { + t.Fatalf("agent status changed by identity revocation: %s", agentBadge.Status) + } +} + +// stageUnlink seals IDENTITY_UNLINKED: the computed views empty out, +// the history retains both link events. +func stageUnlink(t *testing.T, tb *tlTestbed, agentID string) { + t.Helper() + postIdentityEvent(t, tb, + identityInner(identityevent.TypeIdentityUnlinked, "2026-06-10T14:00:00Z", []string{agentID}, "")) + + var unlinkedBadge badgeView + if code := getJSON(t, tb, "/v1/agents/"+agentID, &unlinkedBadge); code != http.StatusOK { + t.Fatalf("agent badge after unlink: %d", code) + } + if len(unlinkedBadge.Identities) != 0 { + t.Fatalf("identities after unlink: %+v", unlinkedBadge.Identities) + } + var audit auditView + if code := getJSON(t, tb, "/v1/agents/"+agentID+"/identities/history", &audit); code != http.StatusOK { + t.Fatalf("identity history after unlink: %d", code) + } + if len(audit.Records) != 2 { + t.Fatalf("identity history count after unlink = %d", len(audit.Records)) + } +} + +// TestIdentityIngest_CrossLaneGuards pins the 422s in both +// directions: identity bodies on the agent lanes, agent bodies on +// the identity lane. +func TestIdentityIngest_CrossLaneGuards(t *testing.T) { + tb := newTLTestbed(t) + + idBody := []byte(mustJSON(t, identityInner( + identityevent.TypeIdentityVerified, "2026-06-10T10:00:00Z", nil, "did:web:identity.acme-corp.com#key-1"))) + + // Identity body on the V2 agent lane → 422. + if rec := tb.postTo(t, "/v2/internal/agents/event", idBody, tb.signWithProducer(t, idBody)); rec.Code != http.StatusUnprocessableEntity { + t.Fatalf("identity body on V2 agent lane: got %d", rec.Code) + } + // Identity body on the frozen V1 lane → 422. + if rec := tb.postTo(t, "/v1/internal/agents/event", idBody, tb.signWithProducer(t, idBody)); rec.Code != http.StatusUnprocessableEntity { + t.Fatalf("identity body on V1 agent lane: got %d", rec.Code) + } + // Agent body on the identity lane → 422. + agentBody := []byte(mustJSON(t, tb.inner)) + if rec := tb.postTo(t, "/v1/internal/identities/event", agentBody, tb.signWithProducer(t, agentBody)); rec.Code != http.StatusUnprocessableEntity { + t.Fatalf("agent body on identity lane: got %d", rec.Code) + } + // Missing producer signature → 422. + if rec := tb.postTo(t, "/v1/internal/identities/event", idBody, ""); rec.Code != http.StatusUnprocessableEntity { + t.Fatalf("missing signature on identity lane: got %d", rec.Code) + } +} + +// TestIdentityIngest_Duplicate pins idempotent retries on the +// identity lane: same canonical bytes → 200 + duplicate flag. +func TestIdentityIngest_Duplicate(t *testing.T) { + tb := newTLTestbed(t) + body := []byte(mustJSON(t, identityInner( + identityevent.TypeIdentityVerified, "2026-06-10T10:00:00Z", nil, "did:web:identity.acme-corp.com#key-1"))) + jws := tb.signWithProducer(t, body) + + first := tb.postTo(t, "/v1/internal/identities/event", body, jws) + if first.Code != http.StatusOK { + t.Fatalf("first append: %d body=%s", first.Code, first.Body) + } + second := tb.postTo(t, "/v1/internal/identities/event", body, jws) + if second.Code != http.StatusOK { + t.Fatalf("retry: %d", second.Code) + } + var resp struct { + Duplicate bool `json:"duplicate"` + Message string `json:"message"` + } + _ = json.Unmarshal(second.Body.Bytes(), &resp) + if !resp.Duplicate || resp.Message != "Event already logged" { + t.Fatalf("retry not flagged duplicate: %+v", resp) + } +} + +// TestIdentityReceipt verifies the identity receipt is a real +// COSE_Sign1 that offline-verifies against the TL's public key. +func TestIdentityReceipt(t *testing.T) { + tb := newTLTestbed(t) + postIdentityEvent(t, tb, + identityInner(identityevent.TypeIdentityVerified, "2026-06-10T10:00:00Z", nil, "did:web:identity.acme-corp.com#key-1")) + + var rec *httptest.ResponseRecorder + for range 50 { + rec = httptest.NewRecorder() + req := httptest.NewRequest(http.MethodGet, "/v1/identities/"+testIdentityID+"/receipt", nil) + tb.router.ServeHTTP(rec, req) + if rec.Code == http.StatusOK { + break + } + time.Sleep(50 * time.Millisecond) + } + if rec.Code != http.StatusOK { + t.Fatalf("never got 200; last status=%d body=%s", rec.Code, rec.Body) + } + if ct := rec.Header().Get("Content-Type"); ct != receiptpkg.MediaType { + t.Errorf("content-type: got %q want %q", ct, receiptpkg.MediaType) + } + if err := receiptpkg.VerifyWithPEM(rec.Body.Bytes(), string(tb.signPubPEM)); err != nil { + t.Errorf("offline verify: %v", err) + } +} + +// TestIdentityReads_NotFound pins 404s for unknown identities across +// the read surface. +func TestIdentityReads_NotFound(t *testing.T) { + tb := newTLTestbed(t) + for _, path := range []string{ + "/v1/identities/unknown-id", + "/v1/identities/unknown-id/receipt", + "/v1/identities/unknown-id/agents", + } { + if code := getJSON(t, tb, path, nil); code != http.StatusNotFound { + t.Errorf("%s: got %d, want 404", path, code) + } + } + // Audit and the agent-side views are list-shaped: empty lists. + var audit struct { + Records []json.RawMessage `json:"records"` + } + if code := getJSON(t, tb, "/v1/identities/unknown-id/audit", &audit); code != http.StatusOK || len(audit.Records) != 0 { + t.Errorf("unknown identity audit: code=%d records=%d", code, len(audit.Records)) + } +} diff --git a/internal/tl/handler/handler_test.go b/internal/tl/handler/handler_test.go index 66cd8ff..901a97f 100644 --- a/internal/tl/handler/handler_test.go +++ b/internal/tl/handler/handler_test.go @@ -887,6 +887,7 @@ func newTLTestbed(t *testing.T) *tlTestbed { _ = lg.Close(cctx) }) badgeSvc := service.NewBadgeService(logSvc) + identityBadgeSvc := service.NewIdentityBadgeService(logSvc, badgeSvc) receiptGen, err := receiptpkg.NewKeyManagerGenerator( context.Background(), tlKM, "tl-sign", "ans-test", ) @@ -915,7 +916,7 @@ func newTLTestbed(t *testing.T) *tlTestbed { // 6. Router. r := chi.NewRouter() h := handler.NewHandlers( - logSvc, badgeSvc, receiptSvc, statusSvc, + logSvc, badgeSvc, identityBadgeSvc, receiptSvc, statusSvc, checkpointSvc, schemaSvc, rootKeysBody, ) h.Mount(r, lg.DataDir()) diff --git a/internal/tl/service/badge.go b/internal/tl/service/badge.go index f9751b4..6d6e331 100644 --- a/internal/tl/service/badge.go +++ b/internal/tl/service/badge.go @@ -43,6 +43,14 @@ type TransparencyLog struct { SchemaVersion string `json:"schemaVersion,omitempty"` Signature string `json:"signature,omitempty"` Status BadgeStatus `json:"status,omitempty"` + + // Identities is the computed read-time join of the agent's + // currently-linked verified identities — populated on the agent + // badge only (never on audit entries, never sealed). The handler + // composes it from the IdentityBadgeService so this service stays + // single-purpose. Covered by the TL's response signature, not by + // any seal: link facts live on the identity stream. + Identities []*LinkedIdentityView `json:"identities,omitempty"` } // BadgeService computes the badge from the latest mirrored event diff --git a/internal/tl/service/codec.go b/internal/tl/service/codec.go index 392609a..478ec38 100644 --- a/internal/tl/service/codec.go +++ b/internal/tl/service/codec.go @@ -6,6 +6,7 @@ import ( "github.com/godaddy/ans/internal/domain" "github.com/godaddy/ans/internal/tl/event" + identityevent "github.com/godaddy/ans/internal/tl/event/identity" eventv1 "github.com/godaddy/ans/internal/tl/event/v1" ) @@ -103,3 +104,44 @@ func (v1Codec) ParseAndBuild( env := eventv1.BuildEnvelope(logID, &inner, keyID, sig) return env, canonical, nil } + +// identityCodec implements envelopeCodec for the identity event +// family — the shape the RA's `/v2/ans/identities/*` routes emit to +// the `/v1/internal/identities/event` ingest lane. Same producer +// lane, same tree; the inner event is keyed by identityId. +// +// The cross-lane guard lives in the closed enums: an AGENT_* body +// fails this codec's `inner.Validate()` (unknown eventType, missing +// identityId) with 422 INVALID_EVENT, exactly as an IDENTITY_* body +// fails the agent codecs. +type identityCodec struct{} + +// ParseAndBuild unmarshals raw into `identityevent.Event`, validates +// it, stamps the verified raID, canonicalizes, and wraps in an +// identity envelope. +func (identityCodec) ParseAndBuild( + raw []byte, + raID, keyID, sig, logID string, +) (event.Signable, []byte, error) { + var inner identityevent.Event + if err := json.Unmarshal(raw, &inner); err != nil { + return nil, nil, domain.NewValidationError("INVALID_EVENT_BODY", err.Error()) + } + if err := inner.Validate(); err != nil { + return nil, nil, domain.NewValidationError("INVALID_EVENT", err.Error()) + } + if inner.RaID == "" { + inner.RaID = raID + } else if inner.RaID != raID { + return nil, nil, domain.NewValidationError( + "RAID_MISMATCH", + fmt.Sprintf("inner event raId %q does not match signed raId %q", inner.RaID, raID), + ) + } + canonical, err := identityevent.CanonicalizeEvent(&inner) + if err != nil { + return nil, nil, fmt.Errorf("service: canonicalize identity inner: %w", err) + } + env := identityevent.BuildEnvelope(logID, &inner, keyID, sig) + return env, canonical, nil +} diff --git a/internal/tl/service/codec_identity_test.go b/internal/tl/service/codec_identity_test.go new file mode 100644 index 0000000..10ec6ba --- /dev/null +++ b/internal/tl/service/codec_identity_test.go @@ -0,0 +1,117 @@ +package service + +// White-box unit tests for the identity envelope codec, including the +// cross-lane guards: agent-shaped bodies fail this codec's closed +// enum, and identity-shaped bodies fail the V1/V2 agent codecs — both +// directions reject with INVALID_EVENT before anything touches the +// tree. + +import ( + "strings" + "testing" +) + +const validIdentityBody = `{ + "identityId":"01HXKQ00000000000000000000", + "kind":"did:web", + "value":"did:web:identity.acme-corp.com", + "providerId":"PID-8294", + "proofMethod":"did-web-sig", + "eventType":"IDENTITY_VERIFIED", + "keys":[{ + "verificationMethod":{ + "id":"did:web:identity.acme-corp.com#key-1", + "type":"JsonWebKey2020", + "controller":"did:web:identity.acme-corp.com", + "publicKeyJwk":{"kty":"OKP","crv":"Ed25519","x":"abc"} + }, + "signedProof":"eyJhbGciOiJFZERTQSJ9.p.s" + }], + "verifiedAt":"2026-06-10T15:04:05Z", + "raId":"ra-test", + "timestamp":"2026-06-10T15:04:05Z" +}` + +func TestIdentityCodec_ParseAndBuild_HappyPath(t *testing.T) { + t.Parallel() + env, canonical, err := identityCodec{}.ParseAndBuild( + []byte(validIdentityBody), "ra-test", "kid-1", "sig", "log-id") + if err != nil { + t.Fatalf("ParseAndBuild: %v", err) + } + if env == nil { + t.Fatal("nil envelope") + } + if len(canonical) == 0 { + t.Fatal("empty canonical bytes") + } + if env.EventType() != "IDENTITY_VERIFIED" { + t.Fatalf("EventType = %q", env.EventType()) + } +} + +func TestIdentityCodec_ParseAndBuild_BadJSON(t *testing.T) { + t.Parallel() + _, _, err := identityCodec{}.ParseAndBuild( + []byte("{not json"), "ra-test", "k", "s", "l") + if err == nil { + t.Error("expected error for malformed JSON") + } +} + +func TestIdentityCodec_ParseAndBuild_RaidMismatch(t *testing.T) { + t.Parallel() + _, _, err := identityCodec{}.ParseAndBuild( + []byte(validIdentityBody), "ra-other", "k", "s", "l") + if err == nil || !strings.Contains(err.Error(), "raId") { + t.Fatalf("expected RAID_MISMATCH error, got %v", err) + } +} + +func TestIdentityCodec_ParseAndBuild_StampsBlankRAID(t *testing.T) { + t.Parallel() + body := strings.Replace(validIdentityBody, `"raId":"ra-test",`, "", 1) + env, _, err := identityCodec{}.ParseAndBuild( + []byte(body), "ra-verified", "k", "s", "l") + if err != nil { + t.Fatalf("ParseAndBuild: %v", err) + } + if env == nil { + t.Fatal("nil envelope") + } +} + +// TestIdentityCodec_RejectsAgentBody is one half of the cross-lane +// guard: a V2 agent event posted to the identity ingest lane fails +// the identity codec's closed enum + required identityId. +func TestIdentityCodec_RejectsAgentBody(t *testing.T) { + t.Parallel() + agentBody := []byte(`{ + "ansId":"10000000-0000-4000-8000-000000000001", + "ansName":"ans://v1.0.0.agent.example.com", + "eventType":"AGENT_REGISTERED", + "timestamp":"2026-04-17T00:00:00Z" + }`) + _, _, err := identityCodec{}.ParseAndBuild(agentBody, "ra-test", "k", "s", "l") + if err == nil { + t.Fatal("agent body must fail the identity codec") + } + if !strings.Contains(err.Error(), "INVALID_EVENT") && !strings.Contains(err.Error(), "eventType") { + t.Fatalf("expected INVALID_EVENT for agent body, got %v", err) + } +} + +// TestAgentCodecs_RejectIdentityBody is the other half: an identity +// event posted to either agent lane fails the agent codecs (unknown +// eventType for the closed agent enums + missing ansId). +func TestAgentCodecs_RejectIdentityBody(t *testing.T) { + t.Parallel() + if _, _, err := (v2Codec{}).ParseAndBuild( + []byte(validIdentityBody), "ra-test", "k", "s", "l"); err == nil { + t.Fatal("identity body must fail the V2 agent codec") + } + if _, _, err := (v1Codec{}).ParseAndBuild( + []byte(validIdentityBody), "ra-test", "k", "s", "l"); err == nil { + t.Fatal("identity body must fail the V1 agent codec — the V1 lane is frozen") + } +} diff --git a/internal/tl/service/identitybadge.go b/internal/tl/service/identitybadge.go new file mode 100644 index 0000000..f1816c0 --- /dev/null +++ b/internal/tl/service/identitybadge.go @@ -0,0 +1,306 @@ +package service + +import ( + "context" + "encoding/json" + "errors" + + sqlitetl "github.com/godaddy/ans/internal/adapter/store/sqlitetl" +) + +// Identity badge statuses. Identities have a two-state read-time +// status (unlike agents, which derive WARNING/EXPIRED from attested +// cert expiry): the stream either ends in a revocation or it doesn't. +const ( + // BadgeVerified — the identity's control proof stands. + BadgeVerified BadgeStatus = "VERIFIED" + // BadgeIdentityRevoked — the identity stream ends in + // IDENTITY_REVOKED. (BadgeRevoked already names the agent label; + // identities share the wire value.) + BadgeIdentityRevoked BadgeStatus = "REVOKED" +) + +// LinkedIdentityView is one entry of the computed identities[] join +// on the agent badge (and the /v1/agents/{agentId}/identities view). +// Everything here is computed at query time from the identity stream +// — never stored on, or sealed into, the agent. The view is covered +// by the TL's response signature, not by any seal. +type LinkedIdentityView struct { + IdentityID string `json:"identityId"` + Kind string `json:"kind"` + Value string `json:"value"` + IdentityStatus string `json:"identityStatus"` // VERIFIED | REVOKED — reflects the identity stream NOW + + // ProvenKeyIDs names the identity's current proven key set — the + // verification-method ids from the latest proof event + // (post-rotation). The full verbatim verification methods live in + // the sealed event; thumbprints are compute-at-read conveniences + // derivable from that sealed source. + ProvenKeyIDs []string `json:"provenKeyIds,omitempty"` + + // LinkedAt is the producer timestamp of the sealed + // IDENTITY_LINKED event that bound this agent. + LinkedAt string `json:"linkedAt,omitempty"` + + // LinkLogID points at the sealed IDENTITY_LINKED entry on the + // identity stream — fetch it for link evidence. + LinkLogID string `json:"linkLogId,omitempty"` + + // IdentityLogID points at the latest identity-stream entry — + // fetch it (or the audit) for the identity evidence/history. + IdentityLogID string `json:"identityLogId,omitempty"` +} + +// LinkedAgentView is one entry of the reverse join — the agents an +// identity currently links to (GET /v1/identities/{id}/agents). +type LinkedAgentView struct { + AnsID string `json:"ansId"` + LinkedAt string `json:"linkedAt,omitempty"` + // AgentStatus is the linked agent's own computed badge status — + // a link is *effective* only while both ends are live, and this + // field is how a reader checks the agent end in the same hop. + AgentStatus BadgeStatus `json:"agentStatus,omitempty"` +} + +// IdentityBadgeService serves the identity read surface: the identity +// badge, audit chain, and the computed joins in both directions. It +// reuses the agent badge's TransparencyLog response shape — audits +// stay audits, one format. +type IdentityBadgeService struct { + log *LogService + badge *BadgeService +} + +// NewIdentityBadgeService constructs an IdentityBadgeService. The +// BadgeService is used for the reverse join's per-agent status (a +// link is effective only while both ends are live). +func NewIdentityBadgeService(log *LogService, badge *BadgeService) *IdentityBadgeService { + return &IdentityBadgeService{log: log, badge: badge} +} + +// Get returns the TransparencyLog view of an identity's most recent +// event — the identity badge. +func (s *IdentityBadgeService) Get(ctx context.Context, identityID string) (*TransparencyLog, error) { + rec, err := s.log.LatestEventByIdentity(ctx, identityID) + if err != nil { + return nil, err + } + return s.buildTransparencyLog(ctx, rec) +} + +// Audit returns the identity's full event chain, paginated, in the +// exact same audit envelope as the agent audit — no bespoke format. +func (s *IdentityBadgeService) Audit(ctx context.Context, identityID string, limit, offset int) ([]*TransparencyLog, error) { + recs, err := s.log.EventsByIdentity(ctx, identityID, limit, offset) + if err != nil { + return nil, err + } + out := make([]*TransparencyLog, 0, len(recs)) + for _, rec := range recs { + tl, err := s.buildTransparencyLog(ctx, rec) + if err != nil { + return nil, err + } + out = append(out, tl) + } + return out, nil +} + +// buildTransparencyLog assembles the response for one identity-stream +// record: parsed envelope wrapper + Merkle proof + computed status. +func (s *IdentityBadgeService) buildTransparencyLog(ctx context.Context, rec *sqlitetl.EventRecord) (*TransparencyLog, error) { + wrapper, err := parseEnvelopeWrapper(rec.RawEvent) + if err != nil { + return nil, err + } + proof, perr := BuildMerkleProof(ctx, s.log.log, rec) + if perr != nil && !errors.Is(perr, ErrProofLeafNotCovered) { + proof = nil + } + schema := rec.SchemaVersion + if schema == "" { + schema = wrapper.SchemaVersion + } + return &TransparencyLog{ + MerkleProof: proof, + Payload: wrapper.Payload, + SchemaVersion: schema, + Signature: wrapper.Signature, + Status: identityStatusFromEventType(rec.EventType), + }, nil +} + +// identityStatusFromEventType derives the identity's read-time +// status from its latest event. REVOKED is terminal — the RA seals +// no identity events after a revocation — so "latest event is +// IDENTITY_REVOKED" is exactly "the identity is revoked", the same +// latest-event discipline the agent badge uses for AGENT_REVOKED. +func identityStatusFromEventType(eventType string) BadgeStatus { + if eventType == string(sqlitetlIdentityRevoked) { + return BadgeIdentityRevoked + } + return BadgeVerified +} + +// sqlitetlIdentityRevoked mirrors identityevent.TypeIdentityRevoked +// without importing the event package for one string. +const sqlitetlIdentityRevoked = "IDENTITY_REVOKED" + +// LinkedIdentitiesForAgent computes the identities[] join for an +// agent badge: every identity whose latest link/unlink fact naming +// this agent is LINKED, decorated with that identity's current +// stream state. Revoked identities stay in the list with +// identityStatus REVOKED — the rotation/revocation visibility on +// every linked badge is the point of the read-time join. +func (s *IdentityBadgeService) LinkedIdentitiesForAgent(ctx context.Context, ansID string) ([]*LinkedIdentityView, error) { + states, err := s.log.LinkStatesByAgent(ctx, ansID) + if err != nil { + return nil, err + } + out := make([]*LinkedIdentityView, 0, len(states)) + for _, st := range states { + if !st.Linked() { + continue + } + view, err := s.linkedIdentityView(ctx, st) + if err != nil { + return nil, err + } + out = append(out, view) + } + return out, nil +} + +// linkedIdentityView decorates one live link with the identity's +// current state: latest event (status + identityLogId), latest proof +// (kind/value/thumbprints), and the sealed link event (linkedAt + +// linkLogId). +func (s *IdentityBadgeService) linkedIdentityView(ctx context.Context, st *sqlitetl.LinkState) (*LinkedIdentityView, error) { + view := &LinkedIdentityView{IdentityID: st.IdentityID} + + linkRec, err := s.log.EventByLeafIndex(ctx, st.LeafIndex) + if err != nil { + return nil, err + } + view.LinkLogID = linkRec.LogID + view.LinkedAt = innerTimestamp(linkRec.RawEvent) + + latest, err := s.log.LatestEventByIdentity(ctx, st.IdentityID) + if err != nil { + return nil, err + } + view.IdentityLogID = latest.LogID + view.IdentityStatus = string(identityStatusFromEventType(latest.EventType)) + + proof, err := s.log.LatestProofByIdentity(ctx, st.IdentityID) + if err != nil { + return nil, err + } + kind, value, keyIDs := proofSummary(proof.RawEvent) + view.Kind = kind + view.Value = value + view.ProvenKeyIDs = keyIDs + return view, nil +} + +// LinkedAgentsForIdentity computes the reverse join: the agents this +// identity currently links to, each with its own computed badge +// status so a reader checks both ends of the link in one response. +func (s *IdentityBadgeService) LinkedAgentsForIdentity(ctx context.Context, identityID string) ([]*LinkedAgentView, error) { + // 404 on an unknown identity (parity with the badge route) — + // otherwise every random id would return an empty 200 list. + if _, err := s.log.LatestEventByIdentity(ctx, identityID); err != nil { + return nil, err + } + states, err := s.log.LinkStatesByIdentity(ctx, identityID) + if err != nil { + return nil, err + } + out := make([]*LinkedAgentView, 0, len(states)) + for _, st := range states { + if !st.Linked() { + continue + } + view := &LinkedAgentView{AnsID: st.AnsID} + if linkRec, err := s.log.EventByLeafIndex(ctx, st.LeafIndex); err == nil { + view.LinkedAt = innerTimestamp(linkRec.RawEvent) + } + if agentTL, err := s.badge.Get(ctx, st.AnsID); err == nil { + view.AgentStatus = agentTL.Status + } + out = append(out, view) + } + return out, nil +} + +// LinkHistoryForAgent returns the link/unlink events that ever named +// this agent, in the standard audit envelope — the +// /v1/agents/{agentId}/identities/history view. No bespoke format: +// each record is the same TransparencyLog shape as every audit entry, +// filtered through the agent index. +func (s *IdentityBadgeService) LinkHistoryForAgent(ctx context.Context, ansID string, limit, offset int) ([]*TransparencyLog, error) { + recs, err := s.log.LinkEventsByAgent(ctx, ansID, limit, offset) + if err != nil { + return nil, err + } + out := make([]*TransparencyLog, 0, len(recs)) + for _, rec := range recs { + tl, err := s.buildTransparencyLog(ctx, rec) + if err != nil { + return nil, err + } + out = append(out, tl) + } + return out, nil +} + +// innerTimestamp drills the producer timestamp out of a stored +// envelope without binding to a concrete inner-event type. +func innerTimestamp(rawEvent string) string { + var w struct { + Payload struct { + Producer struct { + Event struct { + Timestamp string `json:"timestamp"` + } `json:"event"` + } `json:"producer"` + } `json:"payload"` + } + if err := json.Unmarshal([]byte(rawEvent), &w); err != nil { + return "" + } + return w.Payload.Producer.Event.Timestamp +} + +// proofSummary drills kind, value, and the proven verification-method +// ids out of a stored proof event (IDENTITY_VERIFIED / +// IDENTITY_UPDATED). The ids come from the sealed verbatim +// verification methods. +func proofSummary(rawEvent string) (string, string, []string) { + var w struct { + Payload struct { + Producer struct { + Event struct { + Kind string `json:"kind"` + Value string `json:"value"` + Keys []struct { + VerificationMethod struct { + ID string `json:"id"` + } `json:"verificationMethod"` + } `json:"keys"` + } `json:"event"` + } `json:"producer"` + } `json:"payload"` + } + if err := json.Unmarshal([]byte(rawEvent), &w); err != nil { + return "", "", nil + } + ev := w.Payload.Producer.Event + ids := make([]string, 0, len(ev.Keys)) + for _, k := range ev.Keys { + if k.VerificationMethod.ID != "" { + ids = append(ids, k.VerificationMethod.ID) + } + } + return ev.Kind, ev.Value, ids +} diff --git a/internal/tl/service/log.go b/internal/tl/service/log.go index 7b709a2..3b95e55 100644 --- a/internal/tl/service/log.go +++ b/internal/tl/service/log.go @@ -21,6 +21,7 @@ import ( anscrypto "github.com/godaddy/ans/internal/crypto" "github.com/godaddy/ans/internal/port" "github.com/godaddy/ans/internal/tl/event" + identityevent "github.com/godaddy/ans/internal/tl/event/identity" eventv1 "github.com/godaddy/ans/internal/tl/event/v1" "github.com/godaddy/ans/internal/tl/logstore" ) @@ -138,6 +139,16 @@ func (s *LogService) AppendV1(ctx context.Context, in AppendInput) (*AppendResul return s.append(ctx, in, v1Codec{}) } +// AppendIdentity ingests an identity-family producer event +// (IDENTITY_*). Wired to the `POST /v1/internal/identities/event` +// route. Identity events ride the same producer-signature lane and +// land in the same Merkle tree as agent events; the dedicated route +// exists because the payload schema differs (keyed by identityId), +// and the codec's closed enum is the cross-lane guard. +func (s *LogService) AppendIdentity(ctx context.Context, in AppendInput) (*AppendResult, error) { + return s.append(ctx, in, identityCodec{}) +} + // append is the schema-agnostic ingest pipeline. The only per-version // steps (parse, canonicalize, wrap) are delegated to the codec; every // other step — producer-signature verify, dedup, TL attestation sign, @@ -258,6 +269,9 @@ func setOuterSignature(env event.Signable, sig string) error { case *eventv1.Envelope: e.Signature = sig return nil + case *identityevent.Envelope: + e.Signature = sig + return nil default: return fmt.Errorf("log: unknown envelope type %T", env) } @@ -278,6 +292,43 @@ func (s *LogService) EventByLeafIndex(ctx context.Context, idx uint64) (*sqlitet return s.events.GetEventByLeafIndex(ctx, idx) } +// LatestEventByIdentity returns the newest event on an identity's +// stream (the read index over the single log keyed by identityId). +func (s *LogService) LatestEventByIdentity(ctx context.Context, identityID string) (*sqlitetl.EventRecord, error) { + return s.events.GetLatestByIdentityID(ctx, identityID) +} + +// EventsByIdentity returns paginated events for an identity. +func (s *LogService) EventsByIdentity(ctx context.Context, identityID string, limit, offset int) ([]*sqlitetl.EventRecord, error) { + return s.events.GetByIdentityID(ctx, identityID, limit, offset) +} + +// LatestProofByIdentity returns the newest proof event +// (IDENTITY_VERIFIED / IDENTITY_UPDATED) for an identity — the event +// carrying the current proven key set, which the badge join surfaces +// as provenKeyThumbprints. +func (s *LogService) LatestProofByIdentity(ctx context.Context, identityID string) (*sqlitetl.EventRecord, error) { + return s.events.GetLatestProofByIdentityID(ctx, identityID) +} + +// LinkStatesByAgent returns the latest link/unlink fact per identity +// that ever named this agent. +func (s *LogService) LinkStatesByAgent(ctx context.Context, ansID string) ([]*sqlitetl.LinkState, error) { + return s.events.LinkStatesByAgent(ctx, ansID) +} + +// LinkStatesByIdentity returns the latest link/unlink fact per agent +// this identity ever named. +func (s *LogService) LinkStatesByIdentity(ctx context.Context, identityID string) ([]*sqlitetl.LinkState, error) { + return s.events.LinkStatesByIdentity(ctx, identityID) +} + +// LinkEventsByAgent returns the link/unlink events that ever named +// this agent — the per-agent association history. +func (s *LogService) LinkEventsByAgent(ctx context.Context, ansID string, limit, offset int) ([]*sqlitetl.EventRecord, error) { + return s.events.LinkEventsByAgent(ctx, ansID, limit, offset) +} + // LatestCheckpoint returns the most recent checkpoint Tessera has // written to disk, falling back to the DB cache on file errors. func (s *LogService) LatestCheckpoint(ctx context.Context) ([]byte, error) { diff --git a/internal/tl/service/receipt.go b/internal/tl/service/receipt.go index 67ff145..2b56458 100644 --- a/internal/tl/service/receipt.go +++ b/internal/tl/service/receipt.go @@ -74,6 +74,18 @@ func (s *ReceiptService) ForAgent(ctx context.Context, agentID string) (*Receipt return s.buildOrFetch(ctx, rec) } +// ForIdentity returns a receipt for the most recent event on an +// identity's stream — the same COSE_Sign1 machinery as agent +// receipts; receipts are leaf-scoped and don't care which read index +// found the leaf. +func (s *ReceiptService) ForIdentity(ctx context.Context, identityID string) (*Receipt, error) { + rec, err := s.log.LatestEventByIdentity(ctx, identityID) + if err != nil { + return nil, err + } + return s.buildOrFetch(ctx, rec) +} + // ForLeafIndex returns a receipt for a specific leaf. func (s *ReceiptService) ForLeafIndex(ctx context.Context, idx uint64) (*Receipt, error) { rec, err := s.log.EventByLeafIndex(ctx, idx) @@ -95,12 +107,13 @@ func (s *ReceiptService) buildOrFetch(ctx context.Context, rec *sqlitetl.EventRe return nil, err } - // Cache lookup by (leafIndex, treeSize). If a receipt was already - // minted for this exact pair it's byte-identical payload-wise - // (same event bytes + same proof); the CBOR signature isn't - // deterministic but the receipt is still cryptographically valid. + // Cache lookup by (leafIndex, treeSize) — the table's UNIQUE key. + // If a receipt was already minted for this exact pair it's + // byte-identical payload-wise (same event bytes + same proof); + // the CBOR signature isn't deterministic but the receipt is still + // cryptographically valid. treeSize := uint64(proof.TreeSize) //nolint:gosec // int64→uint64 always safe for tree sizes - if cached, cerr := s.receipts.FindByAgentID(ctx, rec.AgentID, treeSize); cerr == nil && cached != nil { + if cached, cerr := s.receipts.FindByLeafIndex(ctx, rec.LeafIndex, treeSize); cerr == nil && cached != nil { return &Receipt{ Bytes: cached.ReceiptBlob, ContentType: receipt.MediaType, @@ -143,8 +156,14 @@ func (s *ReceiptService) buildOrFetch(ctx context.Context, rec *sqlitetl.EventRe } // Cache best-effort — failure here doesn't prevent returning the - // receipt we just computed. The next request will recompute. - _ = s.receipts.Store(ctx, rec.LeafIndex, rec.AgentID, treeSize, coseBytes) + // receipt we just computed. The next request will recompute. The + // subject column is informational: the agent id for agent leaves, + // the identity id for identity leaves. + subjectID := rec.AgentID + if subjectID == "" { + subjectID = rec.IdentityID + } + _ = s.receipts.Store(ctx, rec.LeafIndex, subjectID, treeSize, coseBytes) return &Receipt{ Bytes: coseBytes, diff --git a/spec/api-spec-tl-v2.yaml b/spec/api-spec-tl-v2.yaml index 6dfbd9a..fa98ebc 100644 --- a/spec/api-spec-tl-v2.yaml +++ b/spec/api-spec-tl-v2.yaml @@ -259,6 +259,255 @@ paths: schema: $ref: '#/components/schemas/Problem' + # ────────────────────────────────────────────────────────────── + # Verified identities — the "who" behind agents + # + # There is ONE transparency log — a single Merkle tree. Identity + # events append to the same tree as agent events; the "identity + # stream" is a read index over that single log keyed by + # identityId, exactly as the agent surface is a read index keyed + # by ansId. Tiles/checkpoints/receipts/witnesses are unchanged. + # + # Placement rule: every IDENTITY_* event — proofs, rotations, + # revocations, AND links — is indexed under the identity stream. + # An identity operation never modifies an agent's event or audit + # history; the agent-side views below are read-time joins through + # the link events' agent index. + # ────────────────────────────────────────────────────────────── + /v1/internal/identities/event: + post: + tags: [Transparency Log] + summary: Append a signed identity event to the log + description: | + The identity-family ingest lane. Identical wire contract to + `/v1/internal/agents/event` — JCS-canonicalized inner event + body + detached-JWS `X-Signature`, same producer-key trust + store, same dedup — with the identity payload schema (keyed + by `identityId`, closed `IDENTITY_*` eventType enum). The + closed enums are the cross-lane guard: an `AGENT_*` body on + this route (or an `IDENTITY_*` body on an agent route) fails + validation with 422 `INVALID_EVENT`. + operationId: appendIdentityEvent + parameters: + - name: X-Signature + in: header + required: true + description: | + Detached JWS compact serialization over the raw request + body — same discipline as the agent lanes. + schema: + type: string + requestBody: + required: true + description: JCS-canonicalized inner identity event (≤ 256 KiB). + content: + application/json: + schema: + $ref: '#/components/schemas/IdentityProducerEvent' + responses: + '200': + description: Event logged (or duplicate echoed — idempotent retries). + content: + application/json: + schema: + $ref: '#/components/schemas/AppendResponse' + '413': + description: Request body exceeds 256 KiB. + content: + application/problem+json: + schema: + $ref: '#/components/schemas/Problem' + '422': + description: | + Body fails validation — same codes as the agent lanes, + including `INVALID_EVENT` for cross-lane posts. + content: + application/problem+json: + schema: + $ref: '#/components/schemas/Problem' + + /v1/identities/{identityId}: + get: + tags: [Transparency Log] + summary: Identity badge + description: | + The latest sealed identity event + inclusion proof + computed + status (`VERIFIED` | `REVOKED`). The proof events seal every + proven key self-verifyingly: any third party reads the key + out of the sealed verbatim `verificationMethod`, verifies the + sealed `signedProof` against it, and confirms the payload + binds this identityId — offline, without trusting the RA. + operationId: getIdentityBadge + parameters: + - $ref: '#/components/parameters/IdentityIdPath' + responses: + '200': + description: Identity badge retrieved. + content: + application/json: + schema: + $ref: '#/components/schemas/TransparencyLog' + '404': + description: No events for this identity. + content: + application/problem+json: + schema: + $ref: '#/components/schemas/Problem' + + /v1/identities/{identityId}/audit: + get: + tags: [Transparency Log] + summary: Paginated audit history for an identity + description: | + The identity's full event chain — IDENTITY_VERIFIED, links, + rotations, revocation — in the exact same audit envelope as + the agent audit. This unbroken chain is the continuity + thread: when an agent's domain is lost, the identity stream + survives and links to the successor. + operationId: getIdentityAudit + parameters: + - $ref: '#/components/parameters/IdentityIdPath' + - name: limit + in: query + required: false + schema: { type: integer, default: 50, minimum: 1, maximum: 200 } + - name: offset + in: query + required: false + schema: { type: integer, default: 0, minimum: 0 } + responses: + '200': + description: Audit records, newest-first. + content: + application/json: + schema: + $ref: '#/components/schemas/TransparencyLogAudit' + + /v1/identities/{identityId}/receipt: + get: + tags: [Transparency Log] + summary: SCITT receipt for the identity's latest event + description: | + COSE_Sign1 receipt (CBOR tag 18) for the identity's latest + sealed leaf — same machinery, media type, and 503 retry + semantics as the agent receipt. + operationId: getIdentityReceipt + parameters: + - $ref: '#/components/parameters/IdentityIdPath' + responses: + '200': + description: Binary COSE_Sign1 receipt. + content: + application/scitt-receipt+cose: + schema: + type: string + format: binary + '404': + description: No events for this identity. + content: + application/problem+json: + schema: + $ref: '#/components/schemas/Problem' + '503': + description: | + Leaf committed but no signed checkpoint covers it yet; + retry after the `Retry-After` delay. + headers: + Retry-After: + schema: { type: integer } + content: + application/problem+json: + schema: + $ref: '#/components/schemas/Problem' + + /v1/identities/{identityId}/agents: + get: + tags: [Transparency Log] + summary: Agents currently linked to the identity (reverse join) + description: | + Computed at query time from the link events' agent index: + every agent whose latest link/unlink fact naming it is + LINKED, each decorated with its own computed badge status so + a reader checks both ends of the link in one response. + operationId: getIdentityAgents + parameters: + - $ref: '#/components/parameters/IdentityIdPath' + responses: + '200': + description: Currently-linked agents. + content: + application/json: + schema: + type: object + required: [agents] + properties: + agents: + type: array + items: + $ref: '#/components/schemas/LinkedAgentView' + '404': + description: No events for this identity. + content: + application/problem+json: + schema: + $ref: '#/components/schemas/Problem' + + /v1/agents/{agentId}/identities: + get: + tags: [Transparency Log] + summary: Identities currently linked to the agent (computed) + description: | + The same entries as the agent badge's `identities[]` field, + served alone. Computed at read time — link live ∧ identity + stream state — never stored on, or sealed into, the agent. + operationId: getAgentIdentities + parameters: + - $ref: '#/components/parameters/AgentIdPath' + responses: + '200': + description: Currently-linked identities. + content: + application/json: + schema: + type: object + required: [identities] + properties: + identities: + type: array + items: + $ref: '#/components/schemas/LinkedIdentityView' + + /v1/agents/{agentId}/identities/history: + get: + tags: [Transparency Log] + summary: Link/unlink events that ever named this agent + description: | + Past and present associations, in the STANDARD audit envelope + (each record a TransparencyLog) — no bespoke format. Filtered + through the link events' agent index; the records themselves + live on the identity streams. The agent's own `/audit` stays + purely AGENT_*. This endpoint is a droppable convenience: the + same view derives from the identity audits or the raw entry + tiles. + operationId: getAgentIdentityHistory + parameters: + - $ref: '#/components/parameters/AgentIdPath' + - name: limit + in: query + required: false + schema: { type: integer, default: 50, minimum: 1, maximum: 200 } + - name: offset + in: query + required: false + schema: { type: integer, default: 0, minimum: 0 } + responses: + '200': + description: Link events naming this agent, newest-first. + content: + application/json: + schema: + $ref: '#/components/schemas/TransparencyLogAudit' + # ────────────────────────────────────────────────────────────── # Log static artefacts + verifier keys (public under public-read) # ────────────────────────────────────────────────────────────── @@ -689,6 +938,14 @@ components: type: string format: uuid + IdentityIdPath: + name: identityId + in: path + required: true + description: Immutable identity UUIDv7 — the identity stream key. + schema: + type: string + schemas: # ── Ingest ───────────────────────────────────────────────── ProducerEvent: @@ -763,7 +1020,22 @@ components: signature: { type: string, description: "TL attestation (detached JWS)" } status: type: string - enum: [ACTIVE, REVOKED, DEPRECATED] + description: | + Read-time computed status. Agent badges derive ACTIVE / + REVOKED / DEPRECATED / EXPIRED / WARNING from the latest + event + attested cert expiry; identity badges derive + VERIFIED / REVOKED from the identity stream. + enum: [ACTIVE, REVOKED, DEPRECATED, EXPIRED, WARNING, VERIFIED] + identities: + type: array + description: | + Agent badges only — the COMPUTED read-time join of the + agent's currently-linked verified identities. Covered by + the TL's response signature, never by any seal; identity + rotation/revocation is visible here immediately with + zero agent-stream writes. + items: + $ref: '#/components/schemas/LinkedIdentityView' TransparencyLogAudit: type: object @@ -789,6 +1061,133 @@ components: description: "standard base64 of each sibling hash in order" treeVersion: { type: integer } + # ── Verified identities ──────────────────────────────────── + IdentityProducerEvent: + type: object + description: | + The canonical inner identity event the RA POSTs to the + identity ingest lane. Sealed shapes are append-only-forever; + the five eventType tokens and these fields are the contract. + JCS-canonicalized when signed, same as the agent lanes. + + Per-type required fields: proofs (IDENTITY_VERIFIED / + IDENTITY_UPDATED) carry non-empty `keys[]`, `verifiedAt`, and + `providerId`; IDENTITY_REVOKED carries `revokedAt`; the link + events carry non-empty `ansIds[]` (the whole batch in ONE + event) and are the only types allowed to name agents. + required: [identityId, kind, value, eventType, timestamp] + properties: + identityId: { type: string, description: "UUIDv7 — the identity stream key" } + kind: + type: string + enum: ['did:web', 'did:key', 'lei'] + value: { type: string, example: "did:web:identity.acme-corp.com" } + providerId: { type: string, description: "The owning principal — the WHO's owner" } + proofMethod: + type: string + enum: [did-web-sig, did-key-sig, lei-vlei-acdc] + eventType: + type: string + enum: [IDENTITY_VERIFIED, IDENTITY_UPDATED, IDENTITY_REVOKED, IDENTITY_LINKED, IDENTITY_UNLINKED] + keys: + type: array + description: | + The proven key set — sealed self-verifyingly: each entry + quotes the DID document's verification method VERBATIM + alongside the registrant's signed proof. + items: + $ref: '#/components/schemas/ProvenKey' + ansIds: + type: array + description: Linked agents' ids (IDENTITY_LINKED / IDENTITY_UNLINKED only) + items: { type: string } + previousValue: { type: string, description: "Pre-rotation identifier (IDENTITY_UPDATED)" } + verifiedAt: { type: string, format: date-time } + revokedAt: { type: string, format: date-time } + raId: { type: string, example: "ans-ra-local" } + timestamp: { type: string, format: date-time } + + ProvenKey: + type: object + description: | + One key the registrant proved possession of, sealed + self-verifyingly: any third party reads the key material out + of `verificationMethod`, verifies `signedProof` against it, + then confirms the payload decodes to an IdentityProofInput + binding this identityId + identifier + purpose — offline, + without trusting the RA. + + `verificationMethod` is quoted EXACTLY as the DID document + served it — `id`, `type`, `controller`, and the key material + in whichever representation the document used + (`publicKeyJwk`, or `publicKeyMultibase` for Multikey + documents) — member-for-member, values untouched. Nothing + derived, re-encoded, or normalized enters a seal; the event + envelope is JCS-canonicalized for signing like every event, + and JCS preserves member values exactly, so the quoted + material survives intact. Thumbprints are compute-at-read + conveniences (anyone can derive RFC 7638 from the sealed + source) and are never part of the sealed contract. + + The postponed `lei` kind is the one deliberate exception: + it will seal the subject AID + a key thumbprint only — there + is no document to quote, the ACDC is PII, and KERI's KEL is + already the authoritative key history. Seal verbatim what + has no other tamper-evident home; commit minimally where one + exists. + required: [verificationMethod, signedProof] + properties: + verificationMethod: + type: object + description: The DID document's verification-method object, verbatim + example: + id: did:web:identity.acme-corp.com#key-1 + type: JsonWebKey2020 + controller: did:web:identity.acme-corp.com + publicKeyJwk: { kty: OKP, crv: Ed25519, x: "0-e2i2_..." } + signedProof: + type: string + description: The compact JWS over the served IdentityProofInput + + LinkedIdentityView: + type: object + description: One computed identities[] entry on the agent badge. + required: [identityId, kind, value, identityStatus] + properties: + identityId: { type: string } + kind: { type: string, enum: ['did:web', 'did:key', 'lei'] } + value: { type: string } + identityStatus: + type: string + enum: [VERIFIED, REVOKED] + description: Reflects the identity stream NOW + provenKeyIds: + type: array + description: | + Verification-method ids of the current proven key set + (post-rotation). The full verbatim methods live in the + sealed proof event. + items: { type: string } + linkedAt: { type: string, format: date-time } + linkLogId: + type: string + description: The sealed IDENTITY_LINKED entry on the identity stream — fetch for link evidence + identityLogId: + type: string + description: Latest identity-stream entry — fetch for the identity evidence/history + + LinkedAgentView: + type: object + description: One reverse-join entry (identity → agents). + required: [ansId] + properties: + ansId: { type: string } + linkedAt: { type: string, format: date-time } + agentStatus: + type: string + enum: [ACTIVE, REVOKED, DEPRECATED, EXPIRED, WARNING] + description: The linked agent's own computed badge status — a link is effective only while both ends are live + # ── Admin: producer keys ─────────────────────────────────── ProducerKeyRequest: type: object From b59188a161d3b5a3af6d12299553f56bf786309b Mon Sep 17 00:00:00 2001 From: Connor Snitker Date: Wed, 10 Jun 2026 18:09:58 -0500 Subject: [PATCH 02/12] =?UTF-8?q?feat(ra):=20verified=20identities=20?= =?UTF-8?q?=E2=80=94=20proof-of-control=20gate,=20kinds=20registry,=20link?= =?UTF-8?q?s?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add the /v2/ans/identities surface: owner-level Verified Identities (the "who") proven through challenge-bound key proofs, sealed onto their own TL stream, and linked to any number of the owner's agents. The agent registration path is byte-for-byte untouched — agents carry no identity fields; the association is the links sub-resource. Proof gate (the single security invariant): the RA seals only after control is PROVEN — every submitted compact JWS must carry the served JCS IdentityProofInput verbatim as its payload (clients never canonicalize) and verify against the identifier's AUTHORITATIVE key. The nonce is single-use, consumed only inside the success transaction via a conditional UPDATE (TOCTOU guard); failed attempts never consume it, and recovery is the idempotent re-add. Kinds are pluggable behind the controlVerifier registry (identitykinds.go) — THE extension seam for did:plc, did:ion, did:ethr, and lei (vLEI). did:web and did:key ship enabled: - did:web — multi-key possession of the document's assertionMethod keys, any host. The did.json fetch is a port with two adapters (identity.resolver.type): "noop" synthesizes the document from the proofs' embedded jwk headers (quickstart — real signature verification, waived live-document binding) and "web" is the hardened fetcher: WebPKI, SSRF egress denylist with per-call IP pinning, 5s timeout, size cap, same-registrable-domain redirects. - did:key — the key IS the identifier; zero I/O. Ed25519 (z6Mk…) and P-256 (zDn…) forms. Key support matches exactly what the JWS layer verifies: EdDSA (Ed25519, raw-signing-input per RFC 8037), ES256 (P-256), RS256 (RSA ≥ 2048) — with precise rejections for X25519 (key agreement, cannot sign) and curves without a verifier. Links are a single owner-gated call with no challenge and no signature: the caller must own the identity AND every named agent. A batch seals as ONE IDENTITY_LINKED event (fleet link = O(1) events); rotation and revocation are likewise one event each, with propagation to linked badges left to the TL's read-time join. Agent detail responses gain the additive computed identities[] view. Outbox grows a third IDENTITY lane (migration 007 rebuilds the CHECK; tlclient maps it to /v1/internal/identities/event) under the same sign-once/replay-verbatim invariant. Per-owner register/rotate rate limiting bounds the outbound fetches. Signed-off-by: Connor Snitker --- cmd/ans-ra/main.go | 54 +- go.mod | 2 +- internal/adapter/didresolver/noop.go | 82 ++ internal/adapter/didresolver/resolver_test.go | 379 ++++++ internal/adapter/didresolver/web.go | 363 ++++++ internal/adapter/docsui/openapi/ra.yaml | 597 +++++++++- internal/adapter/store/sqlite/identity.go | 304 +++++ .../adapter/store/sqlite/identity_test.go | 299 +++++ .../sqlite/migrations/006_identities.sql | 74 ++ .../migrations/007_outbox_identity_lane.sql | 41 + internal/adapter/store/sqlite/outbox.go | 10 +- internal/adapter/tlclient/client.go | 8 +- internal/config/config.go | 36 + internal/config/defaults.go | 5 + internal/crypto/jwk.go | 342 ++++++ internal/crypto/jwk_test.go | 329 ++++++ internal/crypto/jws.go | 49 +- internal/crypto/proofinput.go | 60 + internal/crypto/proofinput_test.go | 69 ++ internal/domain/identity.go | 449 +++++++ internal/domain/identity_test.go | 282 +++++ internal/port/didresolver.go | 80 ++ internal/port/store.go | 60 + internal/ra/handler/dto.go | 32 + internal/ra/handler/identity.go | 297 +++++ internal/ra/handler/identity_handler_test.go | 414 +++++++ internal/ra/handler/lifecycle.go | 23 +- internal/ra/service/identity.go | 673 +++++++++++ internal/ra/service/identity_test.go | 1036 +++++++++++++++++ internal/ra/service/identitykinds.go | 317 +++++ internal/ra/service/identityratelimit.go | 80 ++ spec/api-spec-v2.yaml | 597 +++++++++- 32 files changed, 7432 insertions(+), 11 deletions(-) create mode 100644 internal/adapter/didresolver/noop.go create mode 100644 internal/adapter/didresolver/resolver_test.go create mode 100644 internal/adapter/didresolver/web.go create mode 100644 internal/adapter/store/sqlite/identity.go create mode 100644 internal/adapter/store/sqlite/identity_test.go create mode 100644 internal/adapter/store/sqlite/migrations/006_identities.sql create mode 100644 internal/adapter/store/sqlite/migrations/007_outbox_identity_lane.sql create mode 100644 internal/crypto/jwk.go create mode 100644 internal/crypto/jwk_test.go create mode 100644 internal/crypto/proofinput.go create mode 100644 internal/crypto/proofinput_test.go create mode 100644 internal/domain/identity.go create mode 100644 internal/domain/identity_test.go create mode 100644 internal/port/didresolver.go create mode 100644 internal/ra/handler/identity.go create mode 100644 internal/ra/handler/identity_handler_test.go create mode 100644 internal/ra/service/identity.go create mode 100644 internal/ra/service/identity_test.go create mode 100644 internal/ra/service/identitykinds.go create mode 100644 internal/ra/service/identityratelimit.go diff --git a/cmd/ans-ra/main.go b/cmd/ans-ra/main.go index b61a5b7..bc1c032 100644 --- a/cmd/ans-ra/main.go +++ b/cmd/ans-ra/main.go @@ -27,6 +27,7 @@ import ( "github.com/godaddy/ans/internal/adapter/auth" "github.com/godaddy/ans/internal/adapter/cert" + "github.com/godaddy/ans/internal/adapter/didresolver" "github.com/godaddy/ans/internal/adapter/dns" "github.com/godaddy/ans/internal/adapter/docsui" "github.com/godaddy/ans/internal/adapter/eventbus" @@ -95,6 +96,8 @@ func run(cfgPath string) error { byoc := sqlite.NewByocCertificateStore(db) renewals := sqlite.NewRenewalStore(db) outbox := sqlite.NewOutboxStore(db) + identityStore := sqlite.NewIdentityStore(db) + identityLinks := sqlite.NewIdentityLinkStore(db) // Crypto. km, err := keymanager.NewFileKeyManager(cfg.Keys.File.Path) @@ -149,6 +152,14 @@ func run(cfgPath string) error { // DNS verifier. var dnsVerifier = selectDNSVerifier(cfg) + // did:web resolver — noop (quickstart) or hardened web fetch, + // the identity surface's analog of the DNS verifier selection. + didResolver := selectDIDResolver(cfg) + logger.Info(). + Str("resolver", cfg.Identity.Resolver.Type). + Dur("challengeTTL", cfg.Identity.ChallengeTTL). + Msg("verified-identity surface configured") + logger.Info(). Str("tlPublicBaseURL", cfg.TLClient.PublicBaseURL). Str("tlBaseURL", cfg.TLClient.BaseURL). @@ -174,6 +185,18 @@ func run(cfgPath string) error { WithServerCertificateAuthority(serverCA). WithTLPublicBaseURL(cfg.TLClient.PublicBaseURL) + // Verified identities — the "who" behind the agents. Shares the + // producer signer with the registration service: one RA, one + // producer identity on every TL lane. + identitySvc := service.NewIdentityService( + identityStore, identityLinks, agents, didResolver, outbox, db, + ).WithSigner(service.EventSigner{ + KeyManager: km, + KeyID: signerKeyID, + RaID: cfg.Signer.RaID, + }).WithChallengeTTL(cfg.Identity.ChallengeTTL). + WithRegisterRateLimit(cfg.Identity.RegisterRateLimit) + // HTTP. r := chi.NewRouter() r.Use(middleware.Recoverer) @@ -203,7 +226,7 @@ func run(cfgPath string) error { // routes (GET) 404 on not-owned to hide existence; write routes // (POST) 403 so authenticated operators understand it's an // authorization failure (spec §26, §370). - lifeH := handler.NewLifecycleHandler(regSvc) + lifeH := handler.NewLifecycleHandler(regSvc).WithIdentityViews(identitySvc) r.Get("/v2/ans/agents", lifeH.List) readOwnership := ramiddleware.ReadOwnership(agents) @@ -226,6 +249,21 @@ func run(cfgPath string) error { r.With(writeOwnership).Delete("/v2/ans/agents/{agentId}/certificates/server/renewal", lifeH.CancelServerCertRenewal) r.With(writeOwnership).Post("/v2/ans/agents/{agentId}/certificates/server/renewal/verify-acme", lifeH.VerifyRenewalACME) + // Verified-identity routes (V2 only — the V1 lane is frozen). + // Ownership is enforced inside the IdentityService (reads hide + // existence with 404, writes split 404/403) because the owner + // gate is the link mechanism's security boundary and the service + // loads the aggregate anyway. + idH := handler.NewIdentityHandler(identitySvc) + r.Post("/v2/ans/identities", idH.Register) + r.Get("/v2/ans/identities", idH.List) + r.Get("/v2/ans/identities/{identityId}", idH.Detail) + r.Put("/v2/ans/identities/{identityId}", idH.Rotate) + r.Post("/v2/ans/identities/{identityId}/verify-control", idH.VerifyControl) + r.Post("/v2/ans/identities/{identityId}/revoke", idH.Revoke) + r.Post("/v2/ans/identities/{identityId}/links", idH.Link) + r.Delete("/v2/ans/identities/{identityId}/links/{agentId}", idH.Unlink) + // V1 RA surface — byte-for-byte parity with the reference V1 API // spec. Shares the same RegistrationService as the V2 routes; // only the DTO marshalling + TL-emit schema version differ. See @@ -419,3 +457,17 @@ func selectDNSVerifier(cfg *config.RAConfig) port.DNSVerifier { return dns.NewNoopVerifier() } } + +// selectDIDResolver returns the configured did:web resolver adapter — +// the identity surface's analog of selectDNSVerifier. "web" performs +// the hardened HTTPS fetch (WebPKI + SSRF dialer guards); the default +// "noop" synthesizes documents from the submitted proofs' embedded +// keys for self-contained local development. +func selectDIDResolver(cfg *config.RAConfig) port.DIDResolver { + switch cfg.Identity.Resolver.Type { + case "web": + return didresolver.NewWebResolver() + default: + return didresolver.NewNoopResolver() + } +} diff --git a/go.mod b/go.mod index 195ebc6..0769f20 100644 --- a/go.mod +++ b/go.mod @@ -19,6 +19,7 @@ require ( github.com/stretchr/testify v1.11.1 github.com/transparency-dev/tessera v1.0.2 golang.org/x/mod v0.36.0 + golang.org/x/net v0.53.0 modernc.org/sqlite v1.51.0 ) @@ -51,7 +52,6 @@ require ( go.yaml.in/yaml/v3 v3.0.4 // indirect golang.org/x/crypto v0.50.0 // indirect golang.org/x/exp v0.0.0-20260410095643-746e56fc9e2f // indirect - golang.org/x/net v0.53.0 // indirect golang.org/x/oauth2 v0.36.0 // indirect golang.org/x/sync v0.20.0 // indirect golang.org/x/sys v0.43.0 // indirect diff --git a/internal/adapter/didresolver/noop.go b/internal/adapter/didresolver/noop.go new file mode 100644 index 0000000..6b91ce8 --- /dev/null +++ b/internal/adapter/didresolver/noop.go @@ -0,0 +1,82 @@ +// Package didresolver provides the two port.DIDResolver adapters: +// +// - Noop — zero-I/O quickstart resolver; synthesizes the DID +// document from the submitted proofs' embedded keys. +// - Web — the real thing: hardened HTTPS fetch of did.json with +// WebPKI validation and SSRF dialer guards. +// +// Selected by `identity.resolver.type` ("noop" | "web") in the RA +// config — the same pattern as the DNS verifier's `dns.type` +// ("noop" | "lookup"). +package didresolver + +import ( + "context" + "encoding/json" + + "github.com/godaddy/ans/internal/port" +) + +// Noop is the quickstart resolver. It never dials anywhere: the DID +// document is synthesized from the hints — the kid → public-JWK pairs +// the service extracted from the submitted proofs' `jwk` protected +// headers. +// +// What this preserves and what it waives, stated precisely (the noop +// DNS verifier precedent — real crypto, waived external-world +// binding): +// +// - PRESERVED: every JWS still genuinely verifies against the +// embedded key, the proof input still binds identityId / nonce / +// purpose / raId, and the sealed event remains self-verifying. +// - WAIVED: the binding "the live did.json at the DID's host really +// lists this key". Anyone can mint a keypair and claim any +// did:web value. +// +// Strictly for local development and the demo scripts. NOT for +// production. +type Noop struct{} + +// NewNoopResolver returns the quickstart resolver. +func NewNoopResolver() *Noop { return &Noop{} } + +// Resolve synthesizes a DID document listing exactly the hinted keys +// as assertionMethod entries of the requested DID. With no hints +// (the register-time advisory fetch) the document is valid and empty +// — the 202 challenge list then carries a single unkeyed entry, and +// the registrant names keys via the JWS `kid` + `jwk` headers at +// verify time. +// +// The synthesized Raw entry embeds the registrant's submitted jwk +// VERBATIM (json.RawMessage passes through marshalling untouched), so +// the sealed verification method quotes the registrant's exact key +// bytes — the same no-derived-values rule the web resolver satisfies +// by quoting the live document. +func (n *Noop) Resolve(_ context.Context, did string, hints []port.KeyHint) (*port.DIDDocument, error) { + doc := &port.DIDDocument{ID: did} + for _, h := range hints { + if h.Kid == "" || len(h.PublicKeyJWK) == 0 { + continue + } + raw, err := json.Marshal(map[string]any{ + "id": h.Kid, + "type": "JsonWebKey2020", + "controller": did, + "publicKeyJwk": h.PublicKeyJWK, + }) + if err != nil { + continue + } + doc.AssertionMethod = append(doc.AssertionMethod, port.VerificationMethod{ + ID: h.Kid, + Controller: did, + Type: "JsonWebKey2020", + PublicKeyJwk: h.PublicKeyJWK, + Raw: raw, + }) + } + return doc, nil +} + +// compile-time conformance. +var _ port.DIDResolver = (*Noop)(nil) diff --git a/internal/adapter/didresolver/resolver_test.go b/internal/adapter/didresolver/resolver_test.go new file mode 100644 index 0000000..221555e --- /dev/null +++ b/internal/adapter/didresolver/resolver_test.go @@ -0,0 +1,379 @@ +package didresolver + +import ( + "context" + "encoding/json" + "net" + "net/http" + "net/http/httptest" + "net/url" + "strings" + "testing" + "time" + + "github.com/godaddy/ans/internal/domain" + "github.com/godaddy/ans/internal/port" +) + +func TestNoopResolver_SynthesizesFromHints(t *testing.T) { + n := NewNoopResolver() + did := "did:web:identity.acme-corp.com" + + // No hints (register-time advisory fetch) → empty, valid doc. + doc, err := n.Resolve(context.Background(), did, nil) + if err != nil { + t.Fatalf("resolve: %v", err) + } + if doc.ID != did || len(doc.AssertionMethod) != 0 { + t.Fatalf("empty-hint doc: %+v", doc) + } + + hints := []port.KeyHint{ + {Kid: did + "#key-1", PublicKeyJWK: json.RawMessage(`{"kty":"EC"}`)}, + {Kid: "", PublicKeyJWK: json.RawMessage(`{"kty":"EC"}`)}, // skipped + {Kid: did + "#key-2"}, // no key — skipped + } + doc, err = n.Resolve(context.Background(), did, hints) + if err != nil { + t.Fatalf("resolve with hints: %v", err) + } + if len(doc.AssertionMethod) != 1 { + t.Fatalf("want 1 synthesized method, got %d", len(doc.AssertionMethod)) + } + vm := doc.FindAssertionMethod(did + "#key-1") + if vm == nil || vm.Controller != did { + t.Fatalf("synthesized method wrong: %+v", doc.AssertionMethod) + } +} + +// testWebServer serves a did.json (or a custom handler) over TLS on +// 127.0.0.1 and returns a Web resolver wired to trust it. The +// resolver runs its REAL pipeline — pinning dialer included — with +// only the private-network guard waived (the test server is +// loopback). +// +// did:web resolution pins port 443, which a test can't bind; the +// test maps the DID host to the server's host:port via a dial +// rewrite inside the test transport... instead we keep it honest: +// the test overrides DNS by using the server's IP literal is not +// possible for TLS hostname verification, so we use the standard +// trick — a custom RootCAs pool plus the host being 127.0.0.1 won't +// carry a registrable domain. Therefore web-resolver behavior tests +// run against the parse/validation layers directly, and the fetch +// path is covered through a host-rewriting RoundTripper. +type rewriteTransport struct { + inner http.RoundTripper + to *url.URL +} + +func (rt rewriteTransport) RoundTrip(req *http.Request) (*http.Response, error) { + req = req.Clone(req.Context()) + req.URL.Scheme = rt.to.Scheme + req.URL.Host = rt.to.Host + return rt.inner.RoundTrip(req) +} + +// resolveVia runs Web.Resolve with the HTTP layer rewired at the +// transport seam to hit the test server. Everything above the +// transport — URL derivation, status/size/parse/id checks — is the +// production code path. +func resolveVia(t *testing.T, srv *httptest.Server, did string, maxBody int64) (*port.DIDDocument, error) { + t.Helper() + w := NewWebResolver(WithMaxBodyBytes(maxBody), WithTimeout(2*time.Second)) + resolutionURL, err := domain.DIDWebResolutionURL(did) + if err != nil { + return nil, err + } + ctx, cancel := context.WithTimeout(context.Background(), w.timeout) + defer cancel() + req, err := http.NewRequestWithContext(ctx, http.MethodGet, resolutionURL, nil) + if err != nil { + return nil, err + } + target, _ := url.Parse(srv.URL) + client := &http.Client{Transport: rewriteTransport{inner: srv.Client().Transport, to: target}} + resp, err := client.Do(req) + if err != nil { + return nil, domain.NewValidationError("DID_RESOLUTION_FAILED", "could not fetch the DID document") + } + defer func() { _ = resp.Body.Close() }() + return w.parseResponse(did, resp) +} + +func TestWebResolver_HappyPath(t *testing.T) { + did := "did:web:identity.acme-corp.com" + doc := map[string]any{ + "id": did, + "verificationMethod": []map[string]any{ + { + "id": did + "#key-1", + "type": "JsonWebKey2020", + "controller": did, + "publicKeyJwk": map[string]any{"kty": "EC", "crv": "P-256", "x": "xx", "y": "yy"}, + }, + { + "id": did + "#key-2", + "type": "Multikey", + "controller": did, + "publicKeyMultibase": "zDnae", + }, + }, + // Mixed referencing styles: one string ref, one inline object. + "assertionMethod": []any{ + did + "#key-1", + map[string]any{ + "id": did + "#key-2", + "type": "Multikey", + "controller": did, + "publicKeyMultibase": "zDnae", + }, + did + "#unknown-ref", // dangling ref — skipped + }, + } + srv := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path != "/.well-known/did.json" { + http.NotFound(w, r) + return + } + _ = json.NewEncoder(w).Encode(doc) + })) + defer srv.Close() + + got, err := resolveVia(t, srv, did, 1<<20) + if err != nil { + t.Fatalf("resolve: %v", err) + } + if got.ID != did || len(got.AssertionMethod) != 2 { + t.Fatalf("doc: %+v", got) + } + if vm := got.FindAssertionMethod(did + "#key-1"); vm == nil || len(vm.PublicKeyJwk) == 0 { + t.Fatal("key-1 not materialized from string ref") + } + if vm := got.FindAssertionMethod(did + "#key-2"); vm == nil || vm.PublicKeyMultibase != "zDnae" { + t.Fatal("key-2 not materialized from inline object") + } +} + +func TestWebResolver_DocumentIDMismatch(t *testing.T) { + did := "did:web:identity.acme-corp.com" + srv := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + _, _ = w.Write([]byte(`{"id":"did:web:evil.example.com"}`)) + })) + defer srv.Close() + _, err := resolveVia(t, srv, did, 1<<20) + if err == nil || !strings.Contains(err.Error(), "DID_DOCUMENT_ID_MISMATCH") { + t.Fatalf("want DID_DOCUMENT_ID_MISMATCH, got %v", err) + } +} + +func TestWebResolver_Non200(t *testing.T) { + srv := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + w.WriteHeader(http.StatusNotFound) + })) + defer srv.Close() + _, err := resolveVia(t, srv, "did:web:identity.acme-corp.com", 1<<20) + if err == nil || !strings.Contains(err.Error(), "DID_RESOLUTION_FAILED") { + t.Fatalf("want DID_RESOLUTION_FAILED, got %v", err) + } +} + +func TestWebResolver_BodyTooLarge(t *testing.T) { + srv := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + _, _ = w.Write(make([]byte, 4096)) + })) + defer srv.Close() + _, err := resolveVia(t, srv, "did:web:identity.acme-corp.com", 1024) + if err == nil || !strings.Contains(err.Error(), "byte limit") { + t.Fatalf("want size-cap error, got %v", err) + } +} + +func TestWebResolver_BadJSON(t *testing.T) { + srv := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + _, _ = w.Write([]byte(`{not json`)) + })) + defer srv.Close() + _, err := resolveVia(t, srv, "did:web:identity.acme-corp.com", 1<<20) + if err == nil || !strings.Contains(err.Error(), "not valid JSON") { + t.Fatalf("want parse error, got %v", err) + } +} + +func TestWebResolver_BadDID(t *testing.T) { + w := NewWebResolver() + if _, err := w.Resolve(context.Background(), "did:key:z6Mk", nil); err == nil { + t.Fatal("non-did:web should fail before any I/O") + } +} + +func TestWebResolver_NoRegistrableDomain(t *testing.T) { + w := NewWebResolver() + // localhost has no eTLD+1 — rejected before any fetch. + _, err := w.Resolve(context.Background(), "did:web:localhost", nil) + if err == nil || !strings.Contains(err.Error(), "DID_BAD_FORMAT") { + t.Fatalf("want DID_BAD_FORMAT for localhost, got %v", err) + } +} + +func TestWebResolver_SSRFBlocksLoopback(t *testing.T) { + // A real end-to-end run against a loopback host: the pinning + // dialer must reject the resolved address class. The DID needs a + // registrable domain, so use a hosts-style resolver shim via the + // dialer directly. + d := &pinningDialer{} + _, err := d.DialContext(context.Background(), "tcp", "localhost:443") + if err == nil || !strings.Contains(err.Error(), "disallowed") { + t.Fatalf("loopback should be rejected: %v", err) + } +} + +func TestPinningDialer_RejectsNon443(t *testing.T) { + d := &pinningDialer{} + if _, err := d.DialContext(context.Background(), "tcp", "example.com:8443"); err == nil { + t.Fatal("non-443 should be rejected") + } + if _, err := d.DialContext(context.Background(), "tcp", "malformed"); err == nil { + t.Fatal("malformed address should be rejected") + } +} + +func TestPinningDialer_AllowPrivateReachesLoopback(t *testing.T) { + // With the test-only escape hatch the dialer connects to a local + // listener — proving the happy dial path works end to end. + ln, err := net.Listen("tcp", "127.0.0.1:0") + if err != nil { + t.Fatal(err) + } + defer func() { _ = ln.Close() }() + go func() { + conn, aerr := ln.Accept() + if aerr == nil { + _ = conn.Close() + } + }() + + _, port, _ := net.SplitHostPort(ln.Addr().String()) + _ = port // the dialer pins 443; dial loopback via a name that resolves there + d := &pinningDialer{allowPrivate: true} + // localhost:443 likely has no listener; accept either a + // connection refusal (dial path reached) or success. + conn, err := d.DialContext(context.Background(), "tcp", "localhost:443") + if err == nil { + _ = conn.Close() + } else if strings.Contains(err.Error(), "disallowed") { + t.Fatalf("allowPrivate must bypass the class filter: %v", err) + } +} + +func TestIsPublicUnicast(t *testing.T) { + cases := []struct { + ip string + want bool + }{ + {"127.0.0.1", false}, + {"10.1.2.3", false}, + {"172.16.0.1", false}, + {"192.168.1.1", false}, + {"169.254.169.254", false}, // cloud metadata (link-local) + {"::1", false}, + {"fe80::1", false}, + {"fc00::1", false}, + {"ff02::1", false}, + {"0.0.0.0", false}, + {"93.184.216.34", true}, + {"2606:2800:220:1::1", true}, + } + for _, tc := range cases { + if got := isPublicUnicast(net.ParseIP(tc.ip)); got != tc.want { + t.Errorf("isPublicUnicast(%s) = %v, want %v", tc.ip, got, tc.want) + } + } +} + +func TestCheckRedirectPolicy(t *testing.T) { + w := NewWebResolver() + client, err := w.newClient("identity.acme-corp.com") + if err != nil { + t.Fatal(err) + } + mkReq := func(raw string) *http.Request { + u, _ := url.Parse(raw) + return &http.Request{URL: u} + } + via := make([]*http.Request, 0, maxRedirects) + + // Same registrable domain → allowed. + if err := client.CheckRedirect(mkReq("https://www.acme-corp.com/did.json"), via); err != nil { + t.Errorf("same-domain redirect rejected: %v", err) + } + // Cross-domain → rejected. + if err := client.CheckRedirect(mkReq("https://evil.example.com/did.json"), via); err == nil || + !strings.Contains(err.Error(), "DID_REDIRECT_DOMAIN_MISMATCH") { + t.Errorf("cross-domain redirect: %v", err) + } + // Scheme downgrade → rejected. + if err := client.CheckRedirect(mkReq("http://www.acme-corp.com/did.json"), via); err == nil { + t.Error("http downgrade should be rejected") + } + // Hop cap. + for range maxRedirects { + via = append(via, &http.Request{}) + } + if err := client.CheckRedirect(mkReq("https://www.acme-corp.com/d.json"), via); err == nil || + !strings.Contains(err.Error(), "too many redirects") { + t.Errorf("redirect cap: %v", err) + } +} + +func TestParseDIDDocument_Tolerance(t *testing.T) { + // Unknown assertionMethod entry shapes are skipped, not fatal. + body := []byte(`{"id":"did:web:a.com","assertionMethod":[42, {"type":"NoID"}]}`) + doc, err := parseDIDDocument(body) + if err != nil { + t.Fatalf("parse: %v", err) + } + if len(doc.AssertionMethod) != 0 { + t.Fatalf("malformed entries must be skipped: %+v", doc.AssertionMethod) + } +} + +// TestWebResolver_FullResolveFetchFailure drives the REAL Resolve +// path — option wiring, client construction, the pinning dialer — +// against a host that cannot resolve: the fetch-failure branch +// returns the coarse DID_RESOLUTION_FAILED (no SSRF oracle detail). +func TestWebResolver_FullResolveFetchFailure(t *testing.T) { + w := NewWebResolver( + WithTimeout(2*time.Second), + WithRootCAs(nil), + WithAllowPrivateNetworks(), + ) + _, err := w.Resolve(context.Background(), "did:web:no-such-host-ans-test.invalid", nil) + if err == nil || !strings.Contains(err.Error(), "DID_RESOLUTION_FAILED") { + t.Fatalf("want DID_RESOLUTION_FAILED, got %v", err) + } + if strings.Contains(err.Error(), "127.") { + t.Fatalf("error detail leaks addresses: %v", err) + } +} + +// resolveTimeoutGuard pins that the context deadline propagates: a +// server that never answers must not hang past the timeout. +func TestWebResolver_Timeout(t *testing.T) { + srv := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + select { + case <-r.Context().Done(): + case <-time.After(5 * time.Second): + } + })) + defer srv.Close() + + did := "did:web:identity.acme-corp.com" + start := time.Now() + _, err := resolveVia(t, srv, did, 1<<20) + if err == nil { + t.Fatal("expected timeout error") + } + if elapsed := time.Since(start); elapsed > 4*time.Second { + t.Fatalf("timeout did not bound the fetch: %v", elapsed) + } +} diff --git a/internal/adapter/didresolver/web.go b/internal/adapter/didresolver/web.go new file mode 100644 index 0000000..d37a017 --- /dev/null +++ b/internal/adapter/didresolver/web.go @@ -0,0 +1,363 @@ +package didresolver + +import ( + "context" + "crypto/tls" + "crypto/x509" + "encoding/json" + "errors" + "fmt" + "io" + "net" + "net/http" + "strings" + "sync" + "time" + + "golang.org/x/net/publicsuffix" + + "github.com/godaddy/ans/internal/domain" + "github.com/godaddy/ans/internal/port" +) + +// Web is the production did:web resolver: an HTTPS GET of the DID +// document at the DID's resolution URL, through one hardened fetcher. +// +// The fetch target is registrant-steered (any host the DID names), so +// SSRF is a first-class control (design §3.7): +// +// 1. Egress IP denylist enforced at connect time, post-DNS: RFC +// 1918, loopback, link-local (which covers cloud metadata +// addresses), ULA, and unspecified addresses are rejected at the +// dialer, never by hostname string inspection. +// 2. The resolved IP is pinned per host for the duration of one +// Resolve call — a DNS rebind between redirect hops cannot slip +// an internal target past check 1. +// 3. Full WebPKI validation (chain to a trusted root + hostname +// verification) on every fetch — Go's default TLS behavior, left +// fully enabled. +// 4. Bounded: hard timeout (default 5 s, parity with the DNS +// verifier), response-size cap (default 1 MiB), ≤5 redirects +// constrained to the original host's registrable domain. +// 5. Error details never echo resolved IPs, ports, or redirect +// chains (no SSRF oracle). +type Web struct { + timeout time.Duration + maxBodyBytes int64 + rootCAs *x509.CertPool + allowPrivate bool +} + +// WebOption customizes the Web resolver. +type WebOption func(*Web) + +// WithTimeout overrides the per-resolve hard timeout (default 5s). +func WithTimeout(d time.Duration) WebOption { + return func(w *Web) { + if d > 0 { + w.timeout = d + } + } +} + +// WithMaxBodyBytes overrides the response-size cap (default 1 MiB). +func WithMaxBodyBytes(n int64) WebOption { + return func(w *Web) { + if n > 0 { + w.maxBodyBytes = n + } + } +} + +// WithRootCAs overrides the trusted root pool (default: system +// roots). Deployments with private PKI inject their pool here; tests +// inject the httptest server's certificate. +func WithRootCAs(pool *x509.CertPool) WebOption { + return func(w *Web) { w.rootCAs = pool } +} + +// WithAllowPrivateNetworks disables the egress IP denylist. FOR +// TESTS ONLY — it exists so the full real fetch path (dialer +// included) is exercisable against a loopback TLS server. Never +// reachable from configuration. +func WithAllowPrivateNetworks() WebOption { + return func(w *Web) { w.allowPrivate = true } +} + +// NewWebResolver constructs the production resolver. +func NewWebResolver(opts ...WebOption) *Web { + w := &Web{ + timeout: 5 * time.Second, + maxBodyBytes: 1 << 20, // 1 MiB + } + for _, opt := range opts { + opt(w) + } + return w +} + +// maxRedirects bounds the redirect chain per design §3.6. +const maxRedirects = 5 + +// Resolve fetches and parses the DID document. Hints are ignored — +// the authoritatively resolved document is always the key source. +func (w *Web) Resolve(ctx context.Context, did string, _ []port.KeyHint) (*port.DIDDocument, error) { + resolutionURL, err := domain.DIDWebResolutionURL(did) + if err != nil { + return nil, err + } + originHost := strings.Split(strings.TrimPrefix(did, "did:web:"), ":")[0] + + client, err := w.newClient(originHost) + if err != nil { + return nil, err + } + + ctx, cancel := context.WithTimeout(ctx, w.timeout) + defer cancel() + req, err := http.NewRequestWithContext(ctx, http.MethodGet, resolutionURL, nil) + if err != nil { + return nil, domain.NewValidationError("DID_RESOLUTION_FAILED", "could not build resolution request") + } + req.Header.Set("Accept", "application/did+json, application/json") + + resp, err := client.Do(req) + if err != nil { + // Deliberately coarse: no resolved IPs, ports, or redirect + // targets in the detail (no SSRF oracle). + return nil, domain.NewValidationError("DID_RESOLUTION_FAILED", + fmt.Sprintf("could not fetch the DID document for %s", did)) + } + defer func() { _ = resp.Body.Close() }() + return w.parseResponse(did, resp) +} + +// parseResponse applies the status, size, JSON, and document-id +// checks to a fetched response. Split from Resolve so the validation +// pipeline is testable independent of the dial/TLS plumbing. +func (w *Web) parseResponse(did string, resp *http.Response) (*port.DIDDocument, error) { + if resp.StatusCode != http.StatusOK { + return nil, domain.NewValidationError("DID_RESOLUTION_FAILED", + fmt.Sprintf("DID document fetch for %s returned status %d", did, resp.StatusCode)) + } + body, err := io.ReadAll(io.LimitReader(resp.Body, w.maxBodyBytes+1)) + if err != nil { + return nil, domain.NewValidationError("DID_RESOLUTION_FAILED", + fmt.Sprintf("could not read the DID document for %s", did)) + } + if int64(len(body)) > w.maxBodyBytes { + return nil, domain.NewValidationError("DID_RESOLUTION_FAILED", + fmt.Sprintf("DID document for %s exceeds the %d-byte limit", did, w.maxBodyBytes)) + } + + doc, err := parseDIDDocument(body) + if err != nil { + return nil, err + } + if doc.ID != did { + return nil, domain.NewValidationError("DID_DOCUMENT_ID_MISMATCH", + fmt.Sprintf("DID document id %q does not match the requested DID %q", doc.ID, did)) + } + return doc, nil +} + +// newClient builds the per-resolve HTTP client: pinning dialer + +// WebPKI transport + same-registrable-domain redirect policy. A fresh +// client per call keeps the DNS pin scoped to exactly one +// verify-control round. +func (w *Web) newClient(originHost string) (*http.Client, error) { + pinned := &pinningDialer{allowPrivate: w.allowPrivate} + transport := &http.Transport{ + DialContext: pinned.DialContext, + ForceAttemptHTTP2: true, + TLSClientConfig: &tls.Config{RootCAs: w.rootCAs, MinVersion: tls.VersionTLS12}, + } + originDomain, err := registrableDomain(originHost) + if err != nil { + return nil, domain.NewValidationError("DID_BAD_FORMAT", + fmt.Sprintf("did:web host %q has no registrable domain", originHost)) + } + return &http.Client{ + Transport: transport, + CheckRedirect: func(req *http.Request, via []*http.Request) error { + if len(via) >= maxRedirects { + return domain.NewValidationError("DID_RESOLUTION_FAILED", + "too many redirects resolving the DID document") + } + if req.URL.Scheme != "https" { + return domain.NewValidationError("DID_RESOLUTION_FAILED", + "DID document redirect left https") + } + redirDomain, derr := registrableDomain(req.URL.Hostname()) + if derr != nil || redirDomain != originDomain { + return domain.NewValidationError("DID_REDIRECT_DOMAIN_MISMATCH", + "DID document redirect left the DID's registrable domain") + } + return nil + }, + }, nil +} + +// registrableDomain returns the eTLD+1 for a host. Single-label hosts +// (localhost, bare TLDs) error — they have no registrable domain. +func registrableDomain(host string) (string, error) { + return publicsuffix.EffectiveTLDPlusOne(strings.ToLower(host)) +} + +// pinningDialer resolves, filters, and pins target IPs. +// +// The pin map lives for one Resolve call (one dialer per client per +// call): the first connection to a host fixes its IP, so a DNS rebind +// between the initial fetch and a redirect hop — or between TLS +// handshake retries — cannot redirect a later connection to a +// different (possibly internal) address. Every chosen IP passes the +// denylist *after* resolution; hostname-string checks are worthless +// against rebinding. +type pinningDialer struct { + allowPrivate bool + + mu sync.Mutex + pin map[string]string // host → ip +} + +func (d *pinningDialer) DialContext(ctx context.Context, network, addr string) (net.Conn, error) { + host, port, err := net.SplitHostPort(addr) + if err != nil { + return nil, errors.New("didresolver: malformed dial address") + } + if port != "443" { + // did:web fetches are pinned to 443; nothing else should + // ever reach the dialer. + return nil, errors.New("didresolver: refusing non-443 connection") + } + + d.mu.Lock() + if d.pin == nil { + d.pin = make(map[string]string) + } + pinnedIP, ok := d.pin[host] + d.mu.Unlock() + + if !ok { + var err error + pinnedIP, err = d.resolveAndPin(ctx, host) + if err != nil { + return nil, err + } + } + + var dialer net.Dialer + return dialer.DialContext(ctx, network, net.JoinHostPort(pinnedIP, port)) +} + +// resolveAndPin resolves the host, applies the egress denylist to +// every candidate address, and pins the first allowed IP. First +// writer wins — concurrent dials for one host converge on one pin. +func (d *pinningDialer) resolveAndPin(ctx context.Context, host string) (string, error) { + ips, err := net.DefaultResolver.LookupIPAddr(ctx, host) + if err != nil || len(ips) == 0 { + return "", errors.New("didresolver: host did not resolve") + } + chosen := "" + for _, ip := range ips { + if d.allowPrivate || isPublicUnicast(ip.IP) { + chosen = ip.IP.String() + break + } + } + if chosen == "" { + return "", errors.New("didresolver: host resolves only to disallowed addresses") + } + d.mu.Lock() + defer d.mu.Unlock() + if existing, dup := d.pin[host]; dup { + return existing, nil + } + d.pin[host] = chosen + return chosen, nil +} + +// isPublicUnicast rejects every address class the egress denylist +// names: loopback, RFC 1918 / ULA private ranges, link-local (which +// contains the cloud-metadata addresses), multicast, and unspecified. +func isPublicUnicast(ip net.IP) bool { + switch { + case ip.IsLoopback(), + ip.IsPrivate(), + ip.IsLinkLocalUnicast(), + ip.IsLinkLocalMulticast(), + ip.IsMulticast(), + ip.IsUnspecified(): + return false + default: + return true + } +} + +// didDocumentWire is the on-the-wire DID document subset we parse. +// Both arrays are kept as raw JSON: verificationMethod entries so +// the EXACT served bytes can be quoted verbatim into seals, +// assertionMethod entries because they are either string references +// or inline verification-method objects. +type didDocumentWire struct { + ID string `json:"id"` + VerificationMethod []json.RawMessage `json:"verificationMethod"` + AssertionMethod []json.RawMessage `json:"assertionMethod"` +} + +type vmWire struct { + ID string `json:"id"` + Type string `json:"type"` + Controller string `json:"controller"` + PublicKeyJwk json.RawMessage `json:"publicKeyJwk"` + PublicKeyMultibase string `json:"publicKeyMultibase"` +} + +func (v vmWire) toPort(raw json.RawMessage) port.VerificationMethod { + return port.VerificationMethod{ + ID: v.ID, + Controller: v.Controller, + Type: v.Type, + PublicKeyJwk: v.PublicKeyJwk, + PublicKeyMultibase: v.PublicKeyMultibase, + Raw: raw, + } +} + +// parseDIDDocument builds the port.DIDDocument from raw did.json +// bytes, materializing the assertionMethod set: string entries +// dereference into verificationMethod; object entries are used +// inline. Every entry carries its Raw bytes — the document's own +// JSON for that method, which is what sealing quotes. +func parseDIDDocument(body []byte) (*port.DIDDocument, error) { + var wire didDocumentWire + if err := json.Unmarshal(body, &wire); err != nil { + return nil, domain.NewValidationError("DID_RESOLUTION_FAILED", + "DID document is not valid JSON") + } + byID := make(map[string]port.VerificationMethod, len(wire.VerificationMethod)) + for _, raw := range wire.VerificationMethod { + var vm vmWire + if err := json.Unmarshal(raw, &vm); err == nil && vm.ID != "" { + byID[vm.ID] = vm.toPort(raw) + } + } + doc := &port.DIDDocument{ID: wire.ID} + for _, raw := range wire.AssertionMethod { + var ref string + if err := json.Unmarshal(raw, &ref); err == nil { + if vm, ok := byID[ref]; ok { + doc.AssertionMethod = append(doc.AssertionMethod, vm) + } + continue + } + var vm vmWire + if err := json.Unmarshal(raw, &vm); err == nil && vm.ID != "" { + doc.AssertionMethod = append(doc.AssertionMethod, vm.toPort(raw)) + } + } + return doc, nil +} + +// compile-time conformance. +var _ port.DIDResolver = (*Web)(nil) diff --git a/internal/adapter/docsui/openapi/ra.yaml b/internal/adapter/docsui/openapi/ra.yaml index 9ad2749..98073c1 100644 --- a/internal/adapter/docsui/openapi/ra.yaml +++ b/internal/adapter/docsui/openapi/ra.yaml @@ -59,6 +59,12 @@ tags: description: Agent certificate revocation operations - name: Certificate Management description: Certificate retrieval, CSR submission, and renewal operations + - name: Verified Identities + description: | + The "who" behind agents — owner-level identities (did:web, + did:key) proven through challenge-bound control proofs, sealed + on their own Transparency Log stream, and linked to the owner's + agents # Global security requirement. Every path inherits this and can # override with `security: []` to declare itself anonymous. The @@ -889,6 +895,372 @@ paths: schema: $ref: '#/components/schemas/ErrorResponse' + # ────────────────────────────────────────────────────────────────── + # Verified Identities — the "who" behind agents + # + # An identity is a first-class object owned by the authenticated + # principal (the same owner as the agents), proven through a + # per-kind control proof, sealed onto its own Transparency Log + # stream, and linked to any number of that owner's agents. The + # agent registration surface above is unchanged — agents carry no + # identity fields; the association is the links sub-resource. + # + # The single security invariant: the RA seals an identity + # attestation only after control is PROVEN — a challenge-bound + # signature verified against the identifier's authoritative keys — + # never on resolution alone. + # ────────────────────────────────────────────────────────────────── + /ans/identities: + post: + tags: + - Verified Identities + summary: Register a verified identity + description: | + Registers an identifier (the kind — `did:web`, `did:key` — is + inferred from the value's lexical form, never caller-asserted) + and returns the challenge round to sign: one entry per + eligible key, all over a single anti-replay nonce. The + identity is created in `PENDING_CONTROL`; nothing is sealed + until verify-control passes. + + Re-POSTing the same value while the row is `PENDING_CONTROL` + is the idempotent re-add: the same `identityId` returns with + a fresh nonce (the prior nonce is superseded). A value + already verified — by this owner or any other — returns 409 + `IDENTIFIER_DUPLICATE`. Kinds without an enabled control + verifier (`lei`, postponed) return 422 + `IDENTIFIER_KIND_UNSUPPORTED`. + operationId: registerIdentity + requestBody: + required: true + content: + application/json: + schema: + $ref: '#/components/schemas/IdentityRegistrationRequest' + responses: + '202': + description: Identity registered — challenges to sign + content: + application/json: + schema: + $ref: '#/components/schemas/IdentityChallengeResponse' + '401': + description: Authentication failed + content: + application/json: + schema: + $ref: '#/components/schemas/ErrorResponse' + '409': + description: Identifier already verified (IDENTIFIER_DUPLICATE) + content: + application/json: + schema: + $ref: '#/components/schemas/ErrorResponse' + '422': + description: Invalid or unsupported identifier + content: + application/json: + schema: + $ref: '#/components/schemas/ErrorResponse' + get: + tags: + - Verified Identities + summary: List my identities + description: Returns every identity owned by the caller, newest first. + operationId: listIdentities + responses: + '200': + description: The caller's identities + content: + application/json: + schema: + type: object + properties: + identities: + type: array + items: + $ref: '#/components/schemas/IdentityDetails' + '401': + description: Authentication failed + content: + application/json: + schema: + $ref: '#/components/schemas/ErrorResponse' + + /ans/identities/{identityId}: + get: + tags: + - Verified Identities + summary: Get identity detail + description: | + Returns the identity plus its live links. Ownership-scoped: + an identity that doesn't exist or isn't the caller's returns + 404 (existence is hidden). + operationId: getIdentity + parameters: + - $ref: '#/components/parameters/IdentityIdPath' + responses: + '200': + description: Identity detail + content: + application/json: + schema: + $ref: '#/components/schemas/IdentityDetails' + '404': + description: Identity not found or not accessible + content: + application/json: + schema: + $ref: '#/components/schemas/ErrorResponse' + put: + tags: + - Verified Identities + summary: Rotate / replace the identifier + description: | + Stages a same-kind replacement and returns fresh challenges + over it. Until the new proof lands the previously sealed + state stands; a replacement that never verifies expires with + its nonce. On a clean verify-control the RA swaps the value + and seals ONE `IDENTITY_UPDATED` event — regardless of how + many agents are linked. Cross-kind replacement is rejected + (revoke and register a new identity instead). + operationId: rotateIdentity + parameters: + - $ref: '#/components/parameters/IdentityIdPath' + requestBody: + required: true + content: + application/json: + schema: + $ref: '#/components/schemas/IdentityRegistrationRequest' + responses: + '202': + description: Rotation staged — fresh challenges to sign + content: + application/json: + schema: + $ref: '#/components/schemas/IdentityChallengeResponse' + '403': + description: Caller does not own this identity + content: + application/json: + schema: + $ref: '#/components/schemas/ErrorResponse' + '404': + description: Identity not found + content: + application/json: + schema: + $ref: '#/components/schemas/ErrorResponse' + '409': + description: Identity is not in a rotatable state + content: + application/json: + schema: + $ref: '#/components/schemas/ErrorResponse' + '422': + description: Invalid replacement value (incl. IDENTIFIER_KIND_MISMATCH) + content: + application/json: + schema: + $ref: '#/components/schemas/ErrorResponse' + + /ans/identities/{identityId}/verify-control: + post: + tags: + - Verified Identities + summary: Prove control of the identifier + description: | + Submits the control proofs: one compact JWS per proven key, + each payload equal — verbatim — to the served `signingInput` + (clients never canonicalize; the RA checks payload equality + before verifying any signature). Every proof must verify + against the identifier's AUTHORITATIVE key for its `kid` + (resolved from the DID document for `did:web`, decoded from + the identifier for `did:key`); one bad proof fails the call + closed. The nonce is consumed exactly once, inside the + success transaction — a failed attempt does not consume it. + + On success the identity flips to `VERIFIED` (or completes a + staged rotation) and the RA seals `IDENTITY_VERIFIED` / + `IDENTITY_UPDATED` on the identity's own Transparency Log + stream, with every proven key sealed self-verifyingly + (public key + signed proof). + operationId: verifyIdentityControl + parameters: + - $ref: '#/components/parameters/IdentityIdPath' + requestBody: + required: true + content: + application/json: + schema: + $ref: '#/components/schemas/VerifyControlRequest' + responses: + '200': + description: Control proven — identity VERIFIED, event sealed + content: + application/json: + schema: + $ref: '#/components/schemas/IdentityDetails' + '403': + description: Caller does not own this identity + content: + application/json: + schema: + $ref: '#/components/schemas/ErrorResponse' + '404': + description: Identity not found + content: + application/json: + schema: + $ref: '#/components/schemas/ErrorResponse' + '409': + description: | + Challenge state error — `PRICC_TOKEN_EXPIRED`, + `PRICC_TOKEN_ALREADY_USED`, `IDENTIFIER_CHALLENGE_EXPIRED` + — or the identity is revoked. Recovery from an expired + nonce is the idempotent re-add (re-POST the same value). + content: + application/json: + schema: + $ref: '#/components/schemas/ErrorResponse' + '422': + description: | + Proof rejected — `PRICC_SIGNATURE_INVALID`, + `DID_VERIFICATION_METHOD_INVALID`, `DID_RESOLUTION_FAILED`, + `DID_DOCUMENT_ID_MISMATCH`, `IDENTIFIER_PROOF_INVALID`. + content: + application/json: + schema: + $ref: '#/components/schemas/ErrorResponse' + + /ans/identities/{identityId}/revoke: + post: + tags: + - Verified Identities + summary: Revoke the identity + description: | + Transitions a VERIFIED identity to REVOKED and seals ONE + `IDENTITY_REVOKED` event. A POST (state change), never a + DELETE: an identity cannot be deleted — its history is + append-only in the Transparency Log. Propagation to every + linked agent's badge is the TL's read-time join, not a write + fan-out. + operationId: revokeIdentity + parameters: + - $ref: '#/components/parameters/IdentityIdPath' + responses: + '200': + description: Identity revoked, event sealed + content: + application/json: + schema: + $ref: '#/components/schemas/IdentityDetails' + '403': + description: Caller does not own this identity + content: + application/json: + schema: + $ref: '#/components/schemas/ErrorResponse' + '404': + description: Identity not found + content: + application/json: + schema: + $ref: '#/components/schemas/ErrorResponse' + '409': + description: Identity is not VERIFIED (nothing sealed to revoke) + content: + application/json: + schema: + $ref: '#/components/schemas/ErrorResponse' + + /ans/identities/{identityId}/links: + post: + tags: + - Verified Identities + summary: Link agents to the identity + description: | + Binds a batch of the caller's agents to the identity — a + single owner-gated call with no challenge and no signature: + the caller MUST own the identity AND every named agent (key + possession never authorizes a link). The whole batch seals as + ONE `IDENTITY_LINKED` event on the IDENTITY stream carrying + the agent ids; an agent's own stream and audit history are + never written by identity operations. Already-linked agents + are skipped idempotently; a call that links nothing new seals + nothing. Links attach only while the identity is VERIFIED. + operationId: linkIdentityAgents + parameters: + - $ref: '#/components/parameters/IdentityIdPath' + requestBody: + required: true + content: + application/json: + schema: + $ref: '#/components/schemas/IdentityLinkRequest' + responses: + '200': + description: Batch linked (count of newly-created links) + content: + application/json: + schema: + $ref: '#/components/schemas/IdentityLinkResponse' + '403': + description: Caller does not own this identity + content: + application/json: + schema: + $ref: '#/components/schemas/ErrorResponse' + '404': + description: Identity, or a named agent, not found / not the caller's + content: + application/json: + schema: + $ref: '#/components/schemas/ErrorResponse' + '409': + description: Identity is not VERIFIED (IDENTITY_NOT_VERIFIED) + content: + application/json: + schema: + $ref: '#/components/schemas/ErrorResponse' + '422': + description: Invalid link request (empty or oversized batch) + content: + application/json: + schema: + $ref: '#/components/schemas/ErrorResponse' + + /ans/identities/{identityId}/links/{agentId}: + delete: + tags: + - Verified Identities + summary: Unlink an agent + description: | + Ends one association and seals `IDENTITY_UNLINKED` on the + identity stream. The association's history persists in the + identity's audit chain and the raw log tiles; unlinked pairs + may be re-linked later. + operationId: unlinkIdentityAgent + parameters: + - $ref: '#/components/parameters/IdentityIdPath' + - $ref: '#/components/parameters/AgentIdPath' + responses: + '204': + description: Link removed, event sealed + '403': + description: Caller does not own this identity + content: + application/json: + schema: + $ref: '#/components/schemas/ErrorResponse' + '404': + description: Identity or live link not found + content: + application/json: + schema: + $ref: '#/components/schemas/ErrorResponse' + components: # ────────────────────────────────────────────────────────────────── @@ -923,6 +1295,14 @@ components: type: string format: uuid + IdentityIdPath: + name: identityId + in: path + description: Unique identifier of the verified identity (UUIDv7) + required: true + schema: + type: string + # ────────────────────────────────────────────────────────────────── # Schemas # ────────────────────────────────────────────────────────────────── @@ -1155,6 +1535,16 @@ components: type: array items: $ref: '#/components/schemas/Link' + identities: + type: array + description: | + Additive, optional, COMPUTED — the verified identities + currently linked to this agent, joined from the link rows + at read time. Never stored on the registration; identity + rotation/revocation is visible here immediately with zero + agent-side writes. + items: + $ref: '#/components/schemas/LinkedIdentity' required: - agentId - ansName @@ -1585,4 +1975,209 @@ components: found: type: string expected: - type: string \ No newline at end of file + type: string + + # ──────────────────────────────────────────────────────────────── + # Verified Identities + # ──────────────────────────────────────────────────────────────── + IdentityRegistrationRequest: + type: object + description: | + Registers (POST) or rotates (PUT) an identifier. The kind is + inferred from the value's lexical form — `did:web:` prefix, + `did:key:` prefix, or a 20-character LEI (recognized but + postponed) — never caller-asserted. + properties: + value: + type: string + description: The identifier to prove control of + example: did:web:identity.acme-corp.com + required: + - value + + IdentityChallengeResponse: + type: object + description: | + The 202 challenge round. Every entry shares the same + anti-replay nonce and the same signingInput — the input is + key-independent; entries enumerate the keys the resolver + could see in advance (a single unkeyed entry when it could + not — name keys via the JWS `kid` header at verify time). + properties: + identityId: + type: string + description: RA-assigned UUIDv7 — the TL stream key + kind: + type: string + enum: ['did:web', 'did:key', 'lei'] + value: + type: string + description: The canonical identifier this round proves + status: + $ref: '#/components/schemas/IdentityLifecycleStatus' + nonce: + type: string + description: Base64url 32-byte single-use anti-replay nonce + expiresAt: + type: string + format: date-time + challenges: + type: array + items: + $ref: '#/components/schemas/IdentityProofChallenge' + required: + - identityId + - kind + - value + - status + - nonce + - expiresAt + - challenges + + IdentityProofChallenge: + type: object + properties: + kid: + type: string + description: | + Verification-method id eligible to sign this round + (omitted when the resolver could not enumerate keys) + example: did:web:identity.acme-corp.com#key-1 + signingInput: + type: string + description: | + Base64url of the exact RFC 8785 (JCS) canonical + IdentityProofInput bytes — {identifier, identityId, + nonce, purpose:"ans:identity-proof:v1", raId, scheme}. A + compact JWS's payload segment MUST equal this string + verbatim; clients never canonicalize. + required: + - signingInput + + VerifyControlRequest: + type: object + description: | + One compact JWS per proven key. Supported algorithms match + what the verifier implements: EdDSA (Ed25519), ES256 + (ECDSA P-256), and RS256 (RSA >= 2048). Key-agreement keys + (X25519) and curves without a verifier (secp256k1, + P-384/521) are rejected with a precise error. + properties: + signedProofs: + type: array + minItems: 1 + maxItems: 16 + items: + type: string + description: | + Compact JWS over the served signingInput. The protected + header carries `kid` (the claimed verification method) + and MAY carry `jwk` (the signer's public key — required + by the quickstart noop resolver, ignored by the web + resolver, which always uses the resolved document). + required: + - signedProofs + + IdentityLifecycleStatus: + type: string + enum: [PENDING_CONTROL, VERIFIED, REVOKED] + description: | + PENDING_CONTROL → VERIFIED → REVOKED. Rotation keeps the row + VERIFIED (the staged replacement proves control before + anything changes). + + IdentityDetails: + type: object + properties: + identityId: + type: string + kind: + type: string + enum: ['did:web', 'did:key', 'lei'] + value: + type: string + status: + $ref: '#/components/schemas/IdentityLifecycleStatus' + proofMethod: + type: string + description: Control proof that verified this identity + enum: [did-web-sig, did-key-sig, lei-vlei-acdc] + pendingValue: + type: string + description: Staged rotation replacement (empty unless rotating) + verifiedAt: + type: string + format: date-time + createdAt: + type: string + format: date-time + linkedAgents: + type: array + description: Live links (detail responses only) + items: + type: object + properties: + agentId: + type: string + format: uuid + linkedAt: + type: string + format: date-time + required: + - agentId + required: + - identityId + - kind + - value + - status + - createdAt + + IdentityLinkRequest: + type: object + description: | + The batch of the caller's agents to bind — one owner-gated + call, no challenge, no signature; the whole batch seals as + ONE IDENTITY_LINKED event on the identity stream. + properties: + agentIds: + type: array + minItems: 1 + maxItems: 256 + items: + type: string + format: uuid + required: + - agentIds + + IdentityLinkResponse: + type: object + properties: + linked: + type: integer + description: Newly-created links (already-linked agents are skipped) + required: + - linked + + LinkedIdentity: + type: object + description: One computed identities[] entry on AgentDetails. + properties: + identityId: + type: string + kind: + type: string + enum: ['did:web', 'did:key', 'lei'] + value: + type: string + identityStatus: + type: string + enum: [VERIFIED, REVOKED] + description: The identity's CURRENT status — reflects its stream now + linkedAt: + type: string + format: date-time + required: + - identityId + - kind + - value + - identityStatus \ No newline at end of file diff --git a/internal/adapter/store/sqlite/identity.go b/internal/adapter/store/sqlite/identity.go new file mode 100644 index 0000000..daf56af --- /dev/null +++ b/internal/adapter/store/sqlite/identity.go @@ -0,0 +1,304 @@ +package sqlite + +import ( + "context" + "database/sql" + "errors" + "time" + + "github.com/godaddy/ans/internal/domain" +) + +// IdentityStore implements port.IdentityStore. +type IdentityStore struct{ db *DB } + +// NewIdentityStore returns a new SQLite-backed IdentityStore. +func NewIdentityStore(db *DB) *IdentityStore { return &IdentityStore{db: db} } + +type identityRow struct { + IdentityID string `db:"identity_id"` + ProviderID string `db:"provider_id"` + Kind string `db:"kind"` + Value string `db:"value"` + Status string `db:"status"` + ProofMethod string `db:"proof_method"` + PendingValue string `db:"pending_value"` + ChallengeNonce sql.NullString `db:"challenge_nonce"` + ChallengeExpiresAtMs sql.NullInt64 `db:"challenge_expires_at_ms"` + ChallengeConsumedAtMs sql.NullInt64 `db:"challenge_consumed_at_ms"` + VerifiedAtMs sql.NullInt64 `db:"verified_at_ms"` + CreatedAtMs int64 `db:"created_at_ms"` + UpdatedAtMs int64 `db:"updated_at_ms"` +} + +const identityCols = `identity_id, provider_id, kind, value, status, proof_method, + pending_value, challenge_nonce, challenge_expires_at_ms, + challenge_consumed_at_ms, verified_at_ms, created_at_ms, updated_at_ms` + +func (r identityRow) toDomain() *domain.VerifiedIdentity { + v := &domain.VerifiedIdentity{ + IdentityID: r.IdentityID, + ProviderID: r.ProviderID, + Kind: domain.IdentifierKind(r.Kind), + Value: r.Value, + Status: domain.IdentityStatus(r.Status), + ProofMethod: r.ProofMethod, + PendingValue: r.PendingValue, + CreatedAt: msToTime(r.CreatedAtMs), + UpdatedAt: msToTime(r.UpdatedAtMs), + } + if r.VerifiedAtMs.Valid { + v.VerifiedAt = msToTime(r.VerifiedAtMs.Int64) + } + if r.ChallengeNonce.Valid && r.ChallengeNonce.String != "" { + ch := &domain.IdentityChallenge{Nonce: r.ChallengeNonce.String} + if r.ChallengeExpiresAtMs.Valid { + ch.ExpiresAt = msToTime(r.ChallengeExpiresAtMs.Int64) + } + if r.ChallengeConsumedAtMs.Valid { + t := msToTime(r.ChallengeConsumedAtMs.Int64) + ch.ConsumedAt = &t + } + v.Challenge = ch + } + return v +} + +// Save upserts the aggregate. The partial unique indexes enforce the +// live-row and proven-uniqueness rules; violations surface as +// conflict errors (the service maps them to IDENTIFIER_DUPLICATE). +func (s *IdentityStore) Save(ctx context.Context, v *domain.VerifiedIdentity) error { + var nonce sql.NullString + var expiresAt, consumedAt sql.NullInt64 + if v.Challenge != nil { + nonce = sql.NullString{String: v.Challenge.Nonce, Valid: true} + if !v.Challenge.ExpiresAt.IsZero() { + expiresAt = sql.NullInt64{Int64: v.Challenge.ExpiresAt.UnixMilli(), Valid: true} + } + if v.Challenge.ConsumedAt != nil { + consumedAt = sql.NullInt64{Int64: v.Challenge.ConsumedAt.UnixMilli(), Valid: true} + } + } + var verifiedAt sql.NullInt64 + if !v.VerifiedAt.IsZero() { + verifiedAt = sql.NullInt64{Int64: v.VerifiedAt.UnixMilli(), Valid: true} + } + const q = ` + INSERT INTO identities (` + identityCols + `) + VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) + ON CONFLICT(identity_id) DO UPDATE SET + value = excluded.value, + status = excluded.status, + proof_method = excluded.proof_method, + pending_value = excluded.pending_value, + challenge_nonce = excluded.challenge_nonce, + challenge_expires_at_ms = excluded.challenge_expires_at_ms, + challenge_consumed_at_ms = excluded.challenge_consumed_at_ms, + verified_at_ms = excluded.verified_at_ms, + updated_at_ms = excluded.updated_at_ms` + _, err := s.db.extx(ctx).ExecContext(ctx, q, + v.IdentityID, v.ProviderID, string(v.Kind), v.Value, string(v.Status), + v.ProofMethod, v.PendingValue, + nonce, expiresAt, consumedAt, verifiedAt, + v.CreatedAt.UnixMilli(), v.UpdatedAt.UnixMilli(), + ) + return mapSQLErr(err) +} + +// FindByID returns the identity with the given identityId. +func (s *IdentityStore) FindByID(ctx context.Context, identityID string) (*domain.VerifiedIdentity, error) { + var r identityRow + err := s.db.extx(ctx).GetContext(ctx, &r, + `SELECT `+identityCols+` FROM identities WHERE identity_id = ?`, identityID) + if err != nil { + return nil, mapSQLErr(err) + } + return r.toDomain(), nil +} + +// FindLive returns the owner's non-REVOKED row for (kind, value). +func (s *IdentityStore) FindLive( + ctx context.Context, + providerID string, + kind domain.IdentifierKind, + value string, +) (*domain.VerifiedIdentity, error) { + var r identityRow + err := s.db.extx(ctx).GetContext(ctx, &r, + `SELECT `+identityCols+` + FROM identities + WHERE provider_id = ? AND kind = ? AND value = ? AND status != 'REVOKED'`, + providerID, string(kind), value) + if err != nil { + return nil, mapSQLErr(err) + } + return r.toDomain(), nil +} + +// ExistsVerified reports whether any owner holds a VERIFIED row for +// (kind, value). +func (s *IdentityStore) ExistsVerified(ctx context.Context, kind domain.IdentifierKind, value string) (bool, error) { + var one int + err := s.db.extx(ctx).GetContext(ctx, &one, ` + SELECT 1 FROM identities + WHERE kind = ? AND value = ? AND status = 'VERIFIED'`, + string(kind), value) + switch { + case err == nil: + return true, nil + case errors.Is(err, sql.ErrNoRows): + return false, nil + default: + return false, err + } +} + +// ListByOwner returns every identity owned by the principal, newest +// first. +func (s *IdentityStore) ListByOwner(ctx context.Context, providerID string) ([]*domain.VerifiedIdentity, error) { + var rows []identityRow + err := s.db.extx(ctx).SelectContext(ctx, &rows, + `SELECT `+identityCols+` + FROM identities + WHERE provider_id = ? + ORDER BY created_at_ms DESC, identity_id DESC`, providerID) + if err != nil { + return nil, mapSQLErr(err) + } + out := make([]*domain.VerifiedIdentity, 0, len(rows)) + for _, r := range rows { + out = append(out, r.toDomain()) + } + return out, nil +} + +// ConsumeChallenge atomically consumes the live challenge nonce. The +// conditional UPDATE is the TOCTOU guard: only one of two concurrent +// verify-control calls can flip challenge_consumed_at_ms from NULL, +// and an expired or superseded nonce matches zero rows. +func (s *IdentityStore) ConsumeChallenge(ctx context.Context, identityID, nonce string, now time.Time) error { + res, err := s.db.extx(ctx).ExecContext(ctx, ` + UPDATE identities + SET challenge_consumed_at_ms = ?, updated_at_ms = ? + WHERE identity_id = ? + AND challenge_nonce = ? + AND challenge_consumed_at_ms IS NULL + AND challenge_expires_at_ms > ?`, + now.UnixMilli(), now.UnixMilli(), identityID, nonce, now.UnixMilli()) + if err != nil { + return mapSQLErr(err) + } + n, err := res.RowsAffected() + if err != nil { + return err + } + if n != 1 { + return domain.NewInvalidStateError("PRICC_TOKEN_ALREADY_USED", + "challenge nonce is consumed, expired, or superseded") + } + return nil +} + +// IdentityLinkStore implements port.IdentityLinkStore. +type IdentityLinkStore struct{ db *DB } + +// NewIdentityLinkStore returns a new SQLite-backed IdentityLinkStore. +func NewIdentityLinkStore(db *DB) *IdentityLinkStore { return &IdentityLinkStore{db: db} } + +type identityLinkRow struct { + IdentityID string `db:"identity_id"` + AgentID string `db:"agent_id"` + Status string `db:"status"` + LinkedAtMs sql.NullInt64 `db:"linked_at_ms"` + CreatedAtMs int64 `db:"created_at_ms"` + UpdatedAtMs int64 `db:"updated_at_ms"` +} + +func (r identityLinkRow) toDomain() *domain.IdentityLink { + l := &domain.IdentityLink{ + IdentityID: r.IdentityID, + AgentID: r.AgentID, + Status: domain.LinkStatus(r.Status), + CreatedAt: msToTime(r.CreatedAtMs), + UpdatedAt: msToTime(r.UpdatedAtMs), + } + if r.LinkedAtMs.Valid { + l.LinkedAt = msToTime(r.LinkedAtMs.Int64) + } + return l +} + +// Link upserts a live link for the pair. Idempotent: returns false +// (and no error) when the pair is already LINKED, so batch calls can +// skip already-linked agents in the sealed event. +func (s *IdentityLinkStore) Link(ctx context.Context, identityID, agentID string, now time.Time) (bool, error) { + // Fast path: already live? + var exists int + err := s.db.extx(ctx).GetContext(ctx, &exists, ` + SELECT 1 FROM identity_links + WHERE identity_id = ? AND agent_id = ? AND status = 'LINKED'`, + identityID, agentID) + switch { + case err == nil: + return false, nil + case !errors.Is(err, sql.ErrNoRows): + return false, err + } + nowMs := now.UnixMilli() + _, err = s.db.extx(ctx).ExecContext(ctx, ` + INSERT INTO identity_links (identity_id, agent_id, status, linked_at_ms, created_at_ms, updated_at_ms) + VALUES (?, ?, 'LINKED', ?, ?, ?)`, + identityID, agentID, nowMs, nowMs, nowMs) + if err != nil { + return false, mapSQLErr(err) + } + return true, nil +} + +// Unlink flips the live link to UNLINKED. +func (s *IdentityLinkStore) Unlink(ctx context.Context, identityID, agentID string, now time.Time) error { + res, err := s.db.extx(ctx).ExecContext(ctx, ` + UPDATE identity_links + SET status = 'UNLINKED', updated_at_ms = ? + WHERE identity_id = ? AND agent_id = ? AND status = 'LINKED'`, + now.UnixMilli(), identityID, agentID) + if err != nil { + return mapSQLErr(err) + } + n, err := res.RowsAffected() + if err != nil { + return err + } + if n != 1 { + return domain.NewNotFoundError("LINK_NOT_FOUND", + "no live link exists for this identity and agent") + } + return nil +} + +// ListLiveByIdentity returns the identity's live links. +func (s *IdentityLinkStore) ListLiveByIdentity(ctx context.Context, identityID string) ([]*domain.IdentityLink, error) { + return s.listLive(ctx, `identity_id = ?`, identityID) +} + +// ListLiveByAgent returns the agent's live links. +func (s *IdentityLinkStore) ListLiveByAgent(ctx context.Context, agentID string) ([]*domain.IdentityLink, error) { + return s.listLive(ctx, `agent_id = ?`, agentID) +} + +func (s *IdentityLinkStore) listLive(ctx context.Context, where string, arg string) ([]*domain.IdentityLink, error) { + var rows []identityLinkRow + err := s.db.extx(ctx).SelectContext(ctx, &rows, ` + SELECT identity_id, agent_id, status, linked_at_ms, created_at_ms, updated_at_ms + FROM identity_links + WHERE `+where+` AND status = 'LINKED' + ORDER BY linked_at_ms DESC, id DESC`, arg) + if err != nil { + return nil, mapSQLErr(err) + } + out := make([]*domain.IdentityLink, 0, len(rows)) + for _, r := range rows { + out = append(out, r.toDomain()) + } + return out, nil +} diff --git a/internal/adapter/store/sqlite/identity_test.go b/internal/adapter/store/sqlite/identity_test.go new file mode 100644 index 0000000..7c90608 --- /dev/null +++ b/internal/adapter/store/sqlite/identity_test.go @@ -0,0 +1,299 @@ +package sqlite + +import ( + "context" + "errors" + "strings" + "testing" + "time" + + "github.com/godaddy/ans/internal/domain" +) + +var identityNow = time.Date(2026, 6, 10, 15, 0, 0, 0, time.UTC) + +func newIdentityFixture(t *testing.T, id, owner, value string) *domain.VerifiedIdentity { + t.Helper() + v, err := domain.NewVerifiedIdentity(id, owner, value, identityNow) + if err != nil { + t.Fatalf("fixture: %v", err) + } + return v +} + +func TestIdentityStore_SaveAndFind(t *testing.T) { + db := newTestDB(t) + store := NewIdentityStore(db) + ctx := context.Background() + + v := newIdentityFixture(t, "id-1", "owner-1", "did:web:identity.acme-corp.com") + if err := v.IssueChallenge("nonce-1", time.Hour, identityNow); err != nil { + t.Fatal(err) + } + if err := store.Save(ctx, v); err != nil { + t.Fatalf("save: %v", err) + } + + got, err := store.FindByID(ctx, "id-1") + if err != nil { + t.Fatalf("find: %v", err) + } + if got.ProviderID != "owner-1" || got.Kind != domain.KindDIDWeb || + got.Status != domain.IdentityPendingControl { + t.Fatalf("loaded identity wrong: %+v", got) + } + if got.Challenge == nil || got.Challenge.Nonce != "nonce-1" || got.Challenge.ConsumedAt != nil { + t.Fatalf("challenge wrong: %+v", got.Challenge) + } + if !got.Challenge.ExpiresAt.Equal(identityNow.Add(time.Hour)) { + t.Fatalf("expiry wrong: %v", got.Challenge.ExpiresAt) + } + + if _, err := store.FindByID(ctx, "missing"); !errors.Is(err, domain.ErrNotFound) { + t.Fatalf("missing should be not-found, got %v", err) + } +} + +func TestIdentityStore_SaveUpsertsLifecycle(t *testing.T) { + db := newTestDB(t) + store := NewIdentityStore(db) + ctx := context.Background() + + v := newIdentityFixture(t, "id-1", "owner-1", "did:web:a.com") + if err := store.Save(ctx, v); err != nil { + t.Fatal(err) + } + if _, err := v.CompleteVerification(identityNow.Add(time.Minute)); err != nil { + t.Fatal(err) + } + if err := store.Save(ctx, v); err != nil { + t.Fatalf("upsert: %v", err) + } + got, err := store.FindByID(ctx, "id-1") + if err != nil { + t.Fatal(err) + } + if got.Status != domain.IdentityVerified || got.ProofMethod != "did-web-sig" || got.VerifiedAt.IsZero() { + t.Fatalf("verified state lost: %+v", got) + } +} + +func TestIdentityStore_FindLiveAndRevokedFallout(t *testing.T) { + db := newTestDB(t) + store := NewIdentityStore(db) + ctx := context.Background() + + v := newIdentityFixture(t, "id-1", "owner-1", "did:web:a.com") + if err := store.Save(ctx, v); err != nil { + t.Fatal(err) + } + + live, err := store.FindLive(ctx, "owner-1", domain.KindDIDWeb, "did:web:a.com") + if err != nil || live.IdentityID != "id-1" { + t.Fatalf("find live: %+v %v", live, err) + } + // Wrong owner / kind / value → not found. + if _, err := store.FindLive(ctx, "owner-2", domain.KindDIDWeb, "did:web:a.com"); !errors.Is(err, domain.ErrNotFound) { + t.Fatalf("cross-owner FindLive: %v", err) + } + + // Revoke → falls out of the live index; re-registering the value + // succeeds with a fresh row. + if _, err := v.CompleteVerification(identityNow); err != nil { + t.Fatal(err) + } + if err := v.Revoke(identityNow.Add(time.Minute)); err != nil { + t.Fatal(err) + } + if err := store.Save(ctx, v); err != nil { + t.Fatal(err) + } + if _, err := store.FindLive(ctx, "owner-1", domain.KindDIDWeb, "did:web:a.com"); !errors.Is(err, domain.ErrNotFound) { + t.Fatalf("revoked row should not be live: %v", err) + } + fresh := newIdentityFixture(t, "id-2", "owner-1", "did:web:a.com") + if err := store.Save(ctx, fresh); err != nil { + t.Fatalf("re-register after revoke: %v", err) + } +} + +func TestIdentityStore_LiveUniquePerOwner(t *testing.T) { + db := newTestDB(t) + store := NewIdentityStore(db) + ctx := context.Background() + + if err := store.Save(ctx, newIdentityFixture(t, "id-1", "owner-1", "did:web:a.com")); err != nil { + t.Fatal(err) + } + err := store.Save(ctx, newIdentityFixture(t, "id-2", "owner-1", "did:web:a.com")) + if !errors.Is(err, domain.ErrConflict) { + t.Fatalf("duplicate live row should conflict, got %v", err) + } + // A different owner may hold a competing pending claim. + if err := store.Save(ctx, newIdentityFixture(t, "id-3", "owner-2", "did:web:a.com")); err != nil { + t.Fatalf("competing pending claim should be allowed: %v", err) + } +} + +func TestIdentityStore_ProvenUniqueAcrossOwners(t *testing.T) { + db := newTestDB(t) + store := NewIdentityStore(db) + ctx := context.Background() + + a := newIdentityFixture(t, "id-1", "owner-1", "did:web:a.com") + b := newIdentityFixture(t, "id-2", "owner-2", "did:web:a.com") + if err := store.Save(ctx, a); err != nil { + t.Fatal(err) + } + if err := store.Save(ctx, b); err != nil { + t.Fatal(err) + } + + // First to prove wins… + if _, err := a.CompleteVerification(identityNow); err != nil { + t.Fatal(err) + } + if err := store.Save(ctx, a); err != nil { + t.Fatalf("first proof: %v", err) + } + taken, err := store.ExistsVerified(ctx, domain.KindDIDWeb, "did:web:a.com") + if err != nil || !taken { + t.Fatalf("ExistsVerified after proof: %v %v", taken, err) + } + + // …the loser's verify-time flip violates the proven index. + if _, err := b.CompleteVerification(identityNow); err != nil { + t.Fatal(err) + } + if err := store.Save(ctx, b); !errors.Is(err, domain.ErrConflict) { + t.Fatalf("second proof should conflict, got %v", err) + } + + none, err := store.ExistsVerified(ctx, domain.KindDIDWeb, "did:web:other.com") + if err != nil || none { + t.Fatalf("ExistsVerified for unproven value: %v %v", none, err) + } +} + +func TestIdentityStore_ListByOwner(t *testing.T) { + db := newTestDB(t) + store := NewIdentityStore(db) + ctx := context.Background() + + first := newIdentityFixture(t, "id-1", "owner-1", "did:web:a.com") + second := newIdentityFixture(t, "id-2", "owner-1", "did:web:b.com") + second.CreatedAt = identityNow.Add(time.Minute) + second.UpdatedAt = second.CreatedAt + other := newIdentityFixture(t, "id-3", "owner-2", "did:web:c.com") + for _, v := range []*domain.VerifiedIdentity{first, second, other} { + if err := store.Save(ctx, v); err != nil { + t.Fatal(err) + } + } + + got, err := store.ListByOwner(ctx, "owner-1") + if err != nil { + t.Fatal(err) + } + if len(got) != 2 || got[0].IdentityID != "id-2" || got[1].IdentityID != "id-1" { + t.Fatalf("list wrong: %+v", got) + } +} + +func TestIdentityStore_ConsumeChallenge(t *testing.T) { + db := newTestDB(t) + store := NewIdentityStore(db) + ctx := context.Background() + + v := newIdentityFixture(t, "id-1", "owner-1", "did:web:a.com") + if err := v.IssueChallenge("nonce-1", time.Hour, identityNow); err != nil { + t.Fatal(err) + } + if err := store.Save(ctx, v); err != nil { + t.Fatal(err) + } + + // Wrong nonce → rejected, nothing consumed. + if err := store.ConsumeChallenge(ctx, "id-1", "wrong", identityNow.Add(time.Minute)); err == nil { + t.Fatal("wrong nonce must not consume") + } + // Expired → rejected. + if err := store.ConsumeChallenge(ctx, "id-1", "nonce-1", identityNow.Add(2*time.Hour)); err == nil { + t.Fatal("expired nonce must not consume") + } + // Fresh → consumed exactly once. + if err := store.ConsumeChallenge(ctx, "id-1", "nonce-1", identityNow.Add(time.Minute)); err != nil { + t.Fatalf("consume: %v", err) + } + err := store.ConsumeChallenge(ctx, "id-1", "nonce-1", identityNow.Add(2*time.Minute)) + if err == nil || !strings.Contains(err.Error(), "PRICC_TOKEN_ALREADY_USED") { + t.Fatalf("double consume: %v", err) + } + + got, err := store.FindByID(ctx, "id-1") + if err != nil { + t.Fatal(err) + } + if got.Challenge == nil || got.Challenge.ConsumedAt == nil { + t.Fatalf("consumption not persisted: %+v", got.Challenge) + } +} + +func TestIdentityLinkStore_Lifecycle(t *testing.T) { + db := newTestDB(t) + agents := NewAgentStore(db) + identities := NewIdentityStore(db) + links := NewIdentityLinkStore(db) + ctx := context.Background() + + // FK targets must exist. + if err := agents.Save(ctx, newAgentFixture(t, "agent-1", "a.example.com")); err != nil { + t.Fatal(err) + } + if err := agents.Save(ctx, newAgentFixture(t, "agent-2", "b.example.com")); err != nil { + t.Fatal(err) + } + if err := identities.Save(ctx, newIdentityFixture(t, "id-1", "owner-1", "did:web:a.com")); err != nil { + t.Fatal(err) + } + + created, err := links.Link(ctx, "id-1", "agent-1", identityNow) + if err != nil || !created { + t.Fatalf("link: created=%v err=%v", created, err) + } + // Idempotent re-link. + created, err = links.Link(ctx, "id-1", "agent-1", identityNow.Add(time.Minute)) + if err != nil || created { + t.Fatalf("re-link should be a no-op: created=%v err=%v", created, err) + } + if _, err := links.Link(ctx, "id-1", "agent-2", identityNow); err != nil { + t.Fatal(err) + } + + byIdentity, err := links.ListLiveByIdentity(ctx, "id-1") + if err != nil || len(byIdentity) != 2 { + t.Fatalf("live by identity: %d %v", len(byIdentity), err) + } + byAgent, err := links.ListLiveByAgent(ctx, "agent-1") + if err != nil || len(byAgent) != 1 || byAgent[0].Status != domain.LinkLinked { + t.Fatalf("live by agent: %+v %v", byAgent, err) + } + + // Unlink → drops out of live views; history row remains. + if err := links.Unlink(ctx, "id-1", "agent-1", identityNow.Add(time.Hour)); err != nil { + t.Fatalf("unlink: %v", err) + } + if err := links.Unlink(ctx, "id-1", "agent-1", identityNow); !errors.Is(err, domain.ErrNotFound) { + t.Fatalf("double unlink: %v", err) + } + byAgent, err = links.ListLiveByAgent(ctx, "agent-1") + if err != nil || len(byAgent) != 0 { + t.Fatalf("live by agent after unlink: %+v %v", byAgent, err) + } + + // Re-link after unlink — UNLINKED history never blocks. + created, err = links.Link(ctx, "id-1", "agent-1", identityNow.Add(2*time.Hour)) + if err != nil || !created { + t.Fatalf("re-link after unlink: created=%v err=%v", created, err) + } +} diff --git a/internal/adapter/store/sqlite/migrations/006_identities.sql b/internal/adapter/store/sqlite/migrations/006_identities.sql new file mode 100644 index 0000000..0675149 --- /dev/null +++ b/internal/adapter/store/sqlite/migrations/006_identities.sql @@ -0,0 +1,74 @@ +-- Verified Identities — the "who" behind an agent (the "what"). +-- +-- An identity is a first-class object owned by the providerId, proven +-- through a per-kind control proof, sealed onto its own Transparency +-- Log stream, and linked to any number of that owner's agents. The +-- agent_registrations table is UNCHANGED — agents carry no identity +-- fields; the association lives in the identity_links junction below. +-- +-- No public-key column anywhere (ANS-0 §6.2 key transience): proven +-- keys are sealed in the identity's TL events, never persisted as +-- live state. +CREATE TABLE IF NOT EXISTS identities ( + identity_id TEXT PRIMARY KEY, -- UUIDv7, RA-assigned + provider_id TEXT NOT NULL, -- owner (authentication principal) + -- kind carries the identifier kind ('did:web', 'did:key', 'lei', + -- and future kinds: 'did:plc', 'did:ion', …). Deliberately NO + -- CHECK constraint: kind validity is enforced by the domain's + -- closed dispatcher (domain.InferIdentifierKind) + the service's + -- control-verifier registry — one source of truth. A CHECK here + -- would force a table rebuild for every kind added. + kind TEXT NOT NULL, + value TEXT NOT NULL, -- canonical identifier + -- status IS checked: the lifecycle machine is genuinely frozen. + status TEXT NOT NULL CHECK (status IN ('PENDING_CONTROL', 'VERIFIED', 'REVOKED')), + proof_method TEXT NOT NULL DEFAULT '', + pending_value TEXT NOT NULL DEFAULT '', -- staged PUT replacement; '' unless rotating + challenge_nonce TEXT, -- transient anti-replay nonce + challenge_expires_at_ms INTEGER, + challenge_consumed_at_ms INTEGER, -- one-time-use guard, set in the verify tx + verified_at_ms INTEGER, + created_at_ms INTEGER NOT NULL, + updated_at_ms INTEGER NOT NULL +); + +-- One LIVE row per (owner, identifier): re-add idempotency. REVOKED +-- rows fall out, so history never blocks an owner re-proving an +-- identity. +CREATE UNIQUE INDEX IF NOT EXISTS idx_identities_live + ON identities(provider_id, kind, value) WHERE status != 'REVOKED'; + +-- Global uniqueness of PROVEN identities: one (kind, value) is +-- VERIFIED by at most one owner across the RA; an unproven +-- PENDING_CONTROL row cannot squat. Competing claims race to +-- verify-control; first to PROVE wins (the loser's verify-time flip +-- violates this index → IDENTIFIER_DUPLICATE). +CREATE UNIQUE INDEX IF NOT EXISTS idx_identities_proven + ON identities(kind, value) WHERE status = 'VERIFIED'; + +CREATE INDEX IF NOT EXISTS idx_identities_owner + ON identities(provider_id, created_at_ms DESC); + +-- identity_links is the one-to-many (formally many-to-many: an agent +-- legitimately carries several identities — a did:web AND an lei) +-- junction between an owner's identities and that same owner's +-- agents. Rows are read-side caches of the sealed IDENTITY_LINKED / +-- IDENTITY_UNLINKED events. No challenge columns: links carry no +-- proof — a link is a single owner-gated call (§4.3). +CREATE TABLE IF NOT EXISTS identity_links ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + identity_id TEXT NOT NULL REFERENCES identities(identity_id), + agent_id TEXT NOT NULL REFERENCES agent_registrations(agent_id), + status TEXT NOT NULL CHECK (status IN ('LINKED', 'UNLINKED')), + linked_at_ms INTEGER, + created_at_ms INTEGER NOT NULL, + updated_at_ms INTEGER NOT NULL +); + +-- One live link per (identity, agent) pair; UNLINKED rows are history +-- and never block re-linking. +CREATE UNIQUE INDEX IF NOT EXISTS idx_identity_links_live + ON identity_links(identity_id, agent_id) WHERE status = 'LINKED'; + +CREATE INDEX IF NOT EXISTS idx_identity_links_agent + ON identity_links(agent_id, status); diff --git a/internal/adapter/store/sqlite/migrations/007_outbox_identity_lane.sql b/internal/adapter/store/sqlite/migrations/007_outbox_identity_lane.sql new file mode 100644 index 0000000..670bee0 --- /dev/null +++ b/internal/adapter/store/sqlite/migrations/007_outbox_identity_lane.sql @@ -0,0 +1,41 @@ +-- Third outbox lane: identity events. +-- +-- The schema_version column routes each outbox row to its TL ingest +-- lane ('V1' → /v1/internal/agents/event, 'V2' → +-- /v2/internal/agents/event). Identity events ride a third lane, +-- 'IDENTITY' → /v1/internal/identities/event — same producer +-- signature, same replay-verbatim invariant, different inner-event +-- schema (keyed by identityId). +-- +-- SQLite cannot widen a column CHECK in place, so this rebuilds the +-- table with the widened constraint (the standard +-- create-copy-drop-rename dance; the migration runner wraps it in one +-- transaction). Index is recreated to match 001's definition. +CREATE TABLE outbox_events_new ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + event_type TEXT NOT NULL, + agent_id TEXT NOT NULL, + schema_version TEXT NOT NULL DEFAULT 'V2' + CHECK (schema_version IN ('V1', 'V2', 'IDENTITY')), + payload_json TEXT NOT NULL CHECK (json_valid(payload_json)), + attempts INTEGER NOT NULL DEFAULT 0, + last_error TEXT, + next_attempt_at_ms INTEGER NOT NULL, + sent_at_ms INTEGER, + created_at_ms INTEGER NOT NULL +); + +INSERT INTO outbox_events_new ( + id, event_type, agent_id, schema_version, payload_json, + attempts, last_error, next_attempt_at_ms, sent_at_ms, created_at_ms +) +SELECT id, event_type, agent_id, schema_version, payload_json, + attempts, last_error, next_attempt_at_ms, sent_at_ms, created_at_ms +FROM outbox_events; + +DROP TABLE outbox_events; + +ALTER TABLE outbox_events_new RENAME TO outbox_events; + +CREATE INDEX IF NOT EXISTS idx_outbox_next + ON outbox_events(sent_at_ms, next_attempt_at_ms); diff --git a/internal/adapter/store/sqlite/outbox.go b/internal/adapter/store/sqlite/outbox.go index d17185a..6e59de6 100644 --- a/internal/adapter/store/sqlite/outbox.go +++ b/internal/adapter/store/sqlite/outbox.go @@ -43,8 +43,10 @@ func NewOutboxStore(db *DB) *OutboxStore { return &OutboxStore{db: db} } // preceded it — that's how we guarantee at-least-once delivery // without a dual-write window. // -// schemaVersion must be "V1" or "V2"; the worker reads this value -// to pick the matching TL ingest lane. An empty value is rejected. +// schemaVersion must be "V1", "V2", or "IDENTITY"; the worker reads +// this value to pick the matching TL ingest lane. An empty value is +// rejected. For identity events the agentID parameter carries the +// identityId — the row's subject, whatever the event family. func (s *OutboxStore) Enqueue( ctx context.Context, eventType, agentID, schemaVersion string, @@ -55,9 +57,9 @@ func (s *OutboxStore) Enqueue( return 0, errors.New("sqlite/outbox: payload is empty") } switch schemaVersion { - case "V1", "V2": + case "V1", "V2", "IDENTITY": default: - return 0, fmt.Errorf("sqlite/outbox: invalid schemaVersion %q (want V1 or V2)", schemaVersion) + return 0, fmt.Errorf("sqlite/outbox: invalid schemaVersion %q (want V1, V2, or IDENTITY)", schemaVersion) } const q = ` INSERT INTO outbox_events(event_type, agent_id, schema_version, payload_json, diff --git a/internal/adapter/tlclient/client.go b/internal/adapter/tlclient/client.go index 315b684..d898edc 100644 --- a/internal/adapter/tlclient/client.go +++ b/internal/adapter/tlclient/client.go @@ -57,14 +57,20 @@ type AppendResult struct { // versions error rather than defaulting silently, because a wrong // lane means the TL will reject the body with a 422 that looks like // a signature failure — very hard to debug. +// +// "IDENTITY" is the third lane: the IDENTITY_* event family, keyed +// by identityId, riding the same producer-signature discipline into +// the same Merkle tree via its own ingest route. func ingestPathForVersion(schemaVersion string) (string, error) { switch schemaVersion { case "V1": return "/v1/internal/agents/event", nil case "V2": return "/v2/internal/agents/event", nil + case "IDENTITY": + return "/v1/internal/identities/event", nil default: - return "", fmt.Errorf("tlclient: unknown schemaVersion %q (want V1 or V2)", schemaVersion) + return "", fmt.Errorf("tlclient: unknown schemaVersion %q (want V1, V2, or IDENTITY)", schemaVersion) } } diff --git a/internal/config/config.go b/internal/config/config.go index 4a32a87..e7c813e 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -108,6 +108,30 @@ type DNS struct { Server string `koanf:"server"` } +// Identity holds Verified Identity (the "who") configuration — the +// /v2/ans/identities surface (RA only). +type Identity struct { + // Resolver selects the did:web document fetcher. + Resolver IdentityResolver `koanf:"resolver"` + // ChallengeTTL bounds the verify-control nonce (default 1h; 5m + // is the design floor for high-assurance deployments). + ChallengeTTL time.Duration `koanf:"challenge-ttl"` + // RegisterRateLimit is the per-owner register/rotate budget per + // minute (default 10) — each call can trigger an outbound + // did:web fetch before any proof exists. + RegisterRateLimit int `koanf:"register-rate-limit"` +} + +// IdentityResolver selects the did:web resolver adapter. "noop" +// performs no I/O and synthesizes the DID document from the keys +// embedded in the submitted proofs (quickstart — signature +// verification still genuinely runs, only the live-document binding +// is waived; NOT for production); "web" performs the hardened HTTPS +// fetch with WebPKI validation and SSRF dialer guards. +type IdentityResolver struct { + Type string `koanf:"type"` // "noop" | "web" +} + // Keys holds key-manager configuration. type Keys struct { Type string `koanf:"type"` // "file" @@ -194,6 +218,7 @@ type RAConfig struct { Auth Auth `koanf:"auth"` CA CA `koanf:"ca"` DNS DNS `koanf:"dns"` + Identity Identity `koanf:"identity"` Keys Keys `koanf:"keys"` Store Store `koanf:"store"` TLClient TLClient `koanf:"tl-client"` @@ -338,6 +363,17 @@ func (c *RAConfig) Validate() error { default: return fmt.Errorf("dns.type %q not supported (expected 'noop' or 'lookup')", c.DNS.Type) } + switch c.Identity.Resolver.Type { + case "noop", "web": + default: + return fmt.Errorf("identity.resolver.type %q not supported (expected 'noop' or 'web')", c.Identity.Resolver.Type) + } + if c.Identity.ChallengeTTL < 0 { + return errors.New("identity.challenge-ttl must not be negative") + } + if c.Identity.RegisterRateLimit < 0 { + return errors.New("identity.register-rate-limit must not be negative") + } if err := validateKeys(&c.Keys); err != nil { return err } diff --git a/internal/config/defaults.go b/internal/config/defaults.go index 39817f3..00ee8df 100644 --- a/internal/config/defaults.go +++ b/internal/config/defaults.go @@ -20,6 +20,11 @@ func defaultRAConfig() *RAConfig { }, }, DNS: DNS{Type: "noop"}, + Identity: Identity{ + Resolver: IdentityResolver{Type: "noop"}, + ChallengeTTL: time.Hour, + RegisterRateLimit: 10, + }, Keys: Keys{ Type: "file", File: &KeysFile{Path: "./data/ra/keys"}, diff --git a/internal/crypto/jwk.go b/internal/crypto/jwk.go new file mode 100644 index 0000000..5d75584 --- /dev/null +++ b/internal/crypto/jwk.go @@ -0,0 +1,342 @@ +package crypto + +import ( + "crypto/ecdsa" + "crypto/ed25519" + "crypto/elliptic" + "crypto/rsa" + "encoding/base64" + "encoding/json" + "errors" + "fmt" + "math/big" + "strings" +) + +// This file holds the JWK / multibase key plumbing for the Verified +// Identities control proofs. +// +// Key-type allowlist: everything the JWS layer can honestly verify — +// Ed25519 (EdDSA), ECDSA P-256 (ES256), and RSA ≥ 2048 (RS256). +// Anything else is rejected with a *precise* error rather than a +// generic one: X25519 is a key-agreement key and can never sign; +// secp256k1 (ES256K) and the larger NIST curves have no verifier in +// this codebase. Admitting a new type means adding its verification +// arm in jws.go first — the allowlist here only names what that +// layer supports. + +// ErrJWK classifies JWK parse/validation failures. +var ErrJWK = errors.New("crypto/jwk: invalid JWK") + +// jwkFields is the wire shape of the JWK subset we accept. +type jwkFields struct { + Kty string `json:"kty"` + Crv string `json:"crv"` + X string `json:"x"` + Y string `json:"y"` + N string `json:"n"` + E string `json:"e"` +} + +// ParseJWK parses a JWK into a verifying public key: +// +// kty=EC, crv=P-256 → *ecdsa.PublicKey (ES256) +// kty=OKP, crv=Ed25519 → ed25519.PublicKey (EdDSA) +// kty=RSA → *rsa.PublicKey (RS256; CheckKeyStrength enforced) +// +// Everything else fails with a message naming exactly why. +func ParseJWK(raw json.RawMessage) (any, error) { + var f jwkFields + if err := json.Unmarshal(raw, &f); err != nil { + return nil, fmt.Errorf("%w: %w", ErrJWK, err) + } + switch f.Kty { + case "EC": + return parseECJWK(f) + case "OKP": + return parseOKPJWK(f) + case "RSA": + return parseRSAJWK(f) + default: + return nil, fmt.Errorf("%w: unsupported kty %q (supported: EC/P-256, OKP/Ed25519, RSA)", ErrJWK, f.Kty) + } +} + +func parseECJWK(f jwkFields) (*ecdsa.PublicKey, error) { + if f.Crv != "P-256" { + return nil, fmt.Errorf("%w: EC curve %q has no verifier here (supported: P-256)", ErrJWK, f.Crv) + } + x, err := base64.RawURLEncoding.DecodeString(f.X) + if err != nil { + return nil, fmt.Errorf("%w: decode x: %w", ErrJWK, err) + } + y, err := base64.RawURLEncoding.DecodeString(f.Y) + if err != nil { + return nil, fmt.Errorf("%w: decode y: %w", ErrJWK, err) + } + if len(x) != 32 || len(y) != 32 { + return nil, fmt.Errorf("%w: P-256 coordinates must be 32 bytes", ErrJWK) + } + // 0x04 || X || Y is the uncompressed SEC1 form; + // ParseUncompressedPublicKey performs the on-curve check. + point := make([]byte, 0, 65) + point = append(point, 0x04) + point = append(point, x...) + point = append(point, y...) + pub, err := ecdsa.ParseUncompressedPublicKey(elliptic.P256(), point) + if err != nil { + return nil, fmt.Errorf("%w: %w", ErrJWK, err) + } + return pub, nil +} + +func parseOKPJWK(f jwkFields) (ed25519.PublicKey, error) { + switch f.Crv { + case "Ed25519": + x, err := base64.RawURLEncoding.DecodeString(f.X) + if err != nil { + return nil, fmt.Errorf("%w: decode x: %w", ErrJWK, err) + } + if len(x) != ed25519.PublicKeySize { + return nil, fmt.Errorf("%w: Ed25519 key must be %d bytes", ErrJWK, ed25519.PublicKeySize) + } + return ed25519.PublicKey(x), nil + case "X25519": + return nil, fmt.Errorf("%w: X25519 is a key-agreement key, not a signing key — it cannot prove control", ErrJWK) + default: + return nil, fmt.Errorf("%w: unsupported OKP curve %q (supported: Ed25519)", ErrJWK, f.Crv) + } +} + +func parseRSAJWK(f jwkFields) (*rsa.PublicKey, error) { + n, err := base64.RawURLEncoding.DecodeString(f.N) + if err != nil { + return nil, fmt.Errorf("%w: decode n: %w", ErrJWK, err) + } + e, err := base64.RawURLEncoding.DecodeString(f.E) + if err != nil { + return nil, fmt.Errorf("%w: decode e: %w", ErrJWK, err) + } + if len(n) == 0 || len(e) == 0 || len(e) > 8 { + return nil, fmt.Errorf("%w: RSA JWK requires n and a small e", ErrJWK) + } + pub := &rsa.PublicKey{ + N: new(big.Int).SetBytes(n), + E: int(new(big.Int).SetBytes(e).Int64()), + } + if err := CheckKeyStrength(pub); err != nil { + return nil, fmt.Errorf("%w: %w", ErrJWK, err) + } + return pub, nil +} + +// PublicKeyToJWK renders a supported public key as a JWK. Used by +// registrant-side tooling (the demo signer's embedded `jwk` header) +// and tests — never by sealing, which quotes the DID document's +// verification method verbatim instead of re-encoding anything. +func PublicKeyToJWK(pub any) (json.RawMessage, error) { + switch key := pub.(type) { + case *ecdsa.PublicKey: + x, y, err := p256Coordinates(key) + if err != nil { + return nil, err + } + return marshalJWK(map[string]string{ + "kty": "EC", "crv": "P-256", + "x": base64.RawURLEncoding.EncodeToString(x), + "y": base64.RawURLEncoding.EncodeToString(y), + }) + case ed25519.PublicKey: + return marshalJWK(map[string]string{ + "kty": "OKP", "crv": "Ed25519", + "x": base64.RawURLEncoding.EncodeToString(key), + }) + case *rsa.PublicKey: + return marshalJWK(map[string]string{ + "kty": "RSA", + "n": base64.RawURLEncoding.EncodeToString(key.N.Bytes()), + "e": base64.RawURLEncoding.EncodeToString(big.NewInt(int64(key.E)).Bytes()), + }) + default: + return nil, fmt.Errorf("%w: unsupported public key type %T", ErrJWK, pub) + } +} + +func marshalJWK(members map[string]string) (json.RawMessage, error) { + raw, err := json.Marshal(members) + if err != nil { + return nil, fmt.Errorf("%w: %w", ErrJWK, err) + } + return raw, nil +} + +// p256Coordinates extracts the 32-byte X and Y coordinates from a +// P-256 public key via its uncompressed SEC1 encoding (the +// non-deprecated accessor path). +func p256Coordinates(pub *ecdsa.PublicKey) ([]byte, []byte, error) { + if pub == nil || pub.Curve != elliptic.P256() { + return nil, nil, fmt.Errorf("%w: not a P-256 key", ErrJWK) + } + point, err := pub.Bytes() + if err != nil { + return nil, nil, fmt.Errorf("%w: %w", ErrJWK, err) + } + if len(point) != 65 || point[0] != 0x04 { + return nil, nil, fmt.Errorf("%w: unexpected SEC1 encoding", ErrJWK) + } + return point[1:33], point[33:65], nil +} + +// Multicodec prefixes (unsigned varint encoding) for the key types +// did:key and Multikey carry. ed25519-pub is code 0xed → varint +// {0xed, 0x01}; p256-pub is code 0x1200 → varint {0x80, 0x24}. +const ( + multicodecEd25519Hi = 0xed + multicodecEd25519Lo = 0x01 + multicodecP256Hi = 0x80 + multicodecP256Lo = 0x24 +) + +// DecodeMultibase decodes a multibase-encoded public key (a did:key +// method-specific id or a DID document's publicKeyMultibase) into a +// verifying key. Only base58btc ('z' prefix) is accepted — the +// encoding both specs mandate. Supported multicodecs: ed25519-pub +// and p256-pub; others fail with the prefix named. +func DecodeMultibase(encoded string) (any, error) { + if !strings.HasPrefix(encoded, "z") { + return nil, fmt.Errorf("%w: multibase key must be base58btc ('z' prefix)", ErrJWK) + } + raw, err := base58Decode(encoded[1:]) + if err != nil { + return nil, fmt.Errorf("%w: %w", ErrJWK, err) + } + if len(raw) <= 2 { + return nil, fmt.Errorf("%w: multibase payload too short", ErrJWK) + } + switch { + case raw[0] == multicodecEd25519Hi && raw[1] == multicodecEd25519Lo: + key := raw[2:] + if len(key) != ed25519.PublicKeySize { + return nil, fmt.Errorf("%w: ed25519-pub key must be %d bytes, got %d", ErrJWK, ed25519.PublicKeySize, len(key)) + } + return ed25519.PublicKey(key), nil + case raw[0] == multicodecP256Hi && raw[1] == multicodecP256Lo: + key := raw[2:] + if len(key) != 33 { + return nil, fmt.Errorf("%w: p256-pub key must be 33 compressed bytes, got %d", ErrJWK, len(key)) + } + // SAFETY: elliptic.UnmarshalCompressed is the only stdlib + // decoder for compressed SEC1 points (crypto/ecdh and + // ecdsa.ParseUncompressedPublicKey accept uncompressed + // only). It validates on-curve internally and returns nil + // on malformed input; we immediately re-encode to the + // uncompressed form and round-trip through the supported + // parser so the deprecated surface stays contained here. + x, y := elliptic.UnmarshalCompressed(elliptic.P256(), key) + if x == nil { + return nil, fmt.Errorf("%w: invalid compressed P-256 point", ErrJWK) + } + point := make([]byte, 0, 65) + point = append(point, 0x04) + point = append(point, x.FillBytes(make([]byte, 32))...) + point = append(point, y.FillBytes(make([]byte, 32))...) + pub, err := ecdsa.ParseUncompressedPublicKey(elliptic.P256(), point) + if err != nil { + return nil, fmt.Errorf("%w: %w", ErrJWK, err) + } + return pub, nil + default: + return nil, fmt.Errorf("%w: unsupported multicodec prefix 0x%02x%02x (supported: ed25519-pub, p256-pub)", ErrJWK, raw[0], raw[1]) + } +} + +// EncodeMultibase renders a supported public key in the did:key / +// Multikey form: 'z' + base58btc(multicodec varint || key bytes). +// The inverse of DecodeMultibase; used by tooling that mints did:key +// identifiers. +func EncodeMultibase(pub any) (string, error) { + switch key := pub.(type) { + case ed25519.PublicKey: + payload := make([]byte, 0, 2+len(key)) + payload = append(payload, multicodecEd25519Hi, multicodecEd25519Lo) + payload = append(payload, key...) + return "z" + base58Encode(payload), nil + case *ecdsa.PublicKey: + x, y, err := p256Coordinates(key) + if err != nil { + return "", err + } + // SAFETY: see DecodeMultibase — MarshalCompressed is the only + // stdlib encoder for compressed SEC1 points. + compressed := elliptic.MarshalCompressed( + elliptic.P256(), new(big.Int).SetBytes(x), new(big.Int).SetBytes(y)) + payload := make([]byte, 0, 2+len(compressed)) + payload = append(payload, multicodecP256Hi, multicodecP256Lo) + payload = append(payload, compressed...) + return "z" + base58Encode(payload), nil + default: + return "", fmt.Errorf("%w: unsupported public key type %T", ErrJWK, pub) + } +} + +// DecodeDIDKey decodes a did:key identifier into its public key and +// the verification-method id the did:key method defines +// ("{did}#{method-specific-id}"). +func DecodeDIDKey(did string) (any, string, error) { + msid, ok := strings.CutPrefix(did, "did:key:") + if !ok || msid == "" { + return nil, "", fmt.Errorf("%w: not a did:key identifier", ErrJWK) + } + pub, err := DecodeMultibase(msid) + if err != nil { + return nil, "", err + } + return pub, did + "#" + msid, nil +} + +// base58Alphabet is the Bitcoin base58 alphabet, the one multibase +// 'z' (base58btc) uses. +const base58Alphabet = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz" + +// base58Decode decodes a base58btc string. Hand-rolled (~30 lines) +// rather than importing the multiformats stack for one codec. +func base58Decode(s string) ([]byte, error) { + if s == "" { + return nil, errors.New("empty base58 input") + } + radix := big.NewInt(58) + n := new(big.Int) + for _, r := range s { + idx := strings.IndexRune(base58Alphabet, r) + if idx < 0 { + return nil, fmt.Errorf("invalid base58 character %q", r) + } + n.Mul(n, radix) + n.Add(n, big.NewInt(int64(idx))) + } + var out []byte + if n.Sign() > 0 { + out = n.Bytes() + } + // Leading '1's encode leading zero bytes. + for i := 0; i < len(s) && s[i] == '1'; i++ { + out = append([]byte{0x00}, out...) + } + return out, nil +} + +// base58Encode encodes bytes as base58btc. +func base58Encode(b []byte) string { + n := new(big.Int).SetBytes(b) + radix := big.NewInt(58) + mod := new(big.Int) + var out []byte + for n.Sign() > 0 { + n.DivMod(n, radix, mod) + out = append([]byte{base58Alphabet[mod.Int64()]}, out...) + } + for i := 0; i < len(b) && b[i] == 0x00; i++ { + out = append([]byte{'1'}, out...) + } + return string(out) +} diff --git a/internal/crypto/jwk_test.go b/internal/crypto/jwk_test.go new file mode 100644 index 0000000..814668c --- /dev/null +++ b/internal/crypto/jwk_test.go @@ -0,0 +1,329 @@ +package crypto + +import ( + "crypto/ecdsa" + "crypto/ed25519" + "crypto/elliptic" + "crypto/rand" + "crypto/rsa" + "encoding/base64" + "encoding/json" + "strings" + "testing" +) + +func genP256(t *testing.T) *ecdsa.PrivateKey { + t.Helper() + priv, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) + if err != nil { + t.Fatal(err) + } + return priv +} + +func genEd25519(t *testing.T) (ed25519.PublicKey, ed25519.PrivateKey) { + t.Helper() + pub, priv, err := ed25519.GenerateKey(rand.Reader) + if err != nil { + t.Fatal(err) + } + return pub, priv +} + +func TestJWKRoundTrip_AllKinds(t *testing.T) { + t.Parallel() + + p256 := genP256(t) + edPub, _ := genEd25519(t) + rsaKey, err := rsa.GenerateKey(rand.Reader, 2048) + if err != nil { + t.Fatal(err) + } + + cases := []struct { + name string + pub any + }{ + {"P-256", &p256.PublicKey}, + {"Ed25519", edPub}, + {"RSA", &rsaKey.PublicKey}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + jwk, err := PublicKeyToJWK(tc.pub) + if err != nil { + t.Fatalf("PublicKeyToJWK: %v", err) + } + back, err := ParseJWK(jwk) + if err != nil { + t.Fatalf("ParseJWK: %v", err) + } + switch want := tc.pub.(type) { + case *ecdsa.PublicKey: + if got, ok := back.(*ecdsa.PublicKey); !ok || !got.Equal(want) { + t.Fatal("P-256 round trip lost the key") + } + case ed25519.PublicKey: + if got, ok := back.(ed25519.PublicKey); !ok || !got.Equal(want) { + t.Fatal("Ed25519 round trip lost the key") + } + case *rsa.PublicKey: + if got, ok := back.(*rsa.PublicKey); !ok || !got.Equal(want) { + t.Fatal("RSA round trip lost the key") + } + } + }) + } +} + +func TestParseJWKRejections(t *testing.T) { + t.Parallel() + cases := []struct { + name string + raw string + want string + }{ + {"not json", `{`, "invalid JWK"}, + {"unknown kty", `{"kty":"oct","k":"AA"}`, "unsupported kty"}, + {"EC wrong curve", `{"kty":"EC","crv":"P-384","x":"AA","y":"AA"}`, "no verifier here"}, + {"EC secp256k1", `{"kty":"EC","crv":"secp256k1","x":"AA","y":"AA"}`, "no verifier here"}, + {"EC bad x b64", `{"kty":"EC","crv":"P-256","x":"!!!","y":"AA"}`, "decode x"}, + {"EC bad y b64", `{"kty":"EC","crv":"P-256","x":"AA","y":"!!!"}`, "decode y"}, + {"EC short coords", `{"kty":"EC","crv":"P-256","x":"AA","y":"AA"}`, "32 bytes"}, + {"OKP X25519 is key agreement", `{"kty":"OKP","crv":"X25519","x":"9GXjPGGvmRq9F6Ng5dQQ_s31mfhxrcNZxRGONrmH30k"}`, "key-agreement key"}, + {"OKP unknown curve", `{"kty":"OKP","crv":"Ed448","x":"AA"}`, "unsupported OKP curve"}, + {"OKP bad x b64", `{"kty":"OKP","crv":"Ed25519","x":"!!!"}`, "decode x"}, + {"OKP short key", `{"kty":"OKP","crv":"Ed25519","x":"AA"}`, "32 bytes"}, + {"RSA bad n", `{"kty":"RSA","n":"!!!","e":"AQAB"}`, "decode n"}, + {"RSA bad e", `{"kty":"RSA","n":"AQAB","e":"!!!"}`, "decode e"}, + {"RSA missing members", `{"kty":"RSA"}`, "requires n"}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + _, err := ParseJWK(json.RawMessage(tc.raw)) + if err == nil || !strings.Contains(err.Error(), tc.want) { + t.Fatalf("want error containing %q, got %v", tc.want, err) + } + }) + } + + // Off-curve point: valid lengths, garbage coordinates. + off := `{"kty":"EC","crv":"P-256","x":"` + strings.Repeat("A", 43) + `","y":"` + strings.Repeat("B", 43) + `"}` + if _, err := ParseJWK(json.RawMessage(off)); err == nil { + t.Error("off-curve point should be rejected") + } + // Weak RSA key (1024-bit) fails CheckKeyStrength. + weak, err := rsa.GenerateKey(rand.Reader, 1024) + if err != nil { + t.Fatal(err) + } + weakJWK, err := PublicKeyToJWK(&weak.PublicKey) + if err != nil { + t.Fatal(err) + } + if _, err := ParseJWK(weakJWK); err == nil { + t.Error("1024-bit RSA should be rejected") + } +} + +func TestPublicKeyToJWKRejectsUnsupported(t *testing.T) { + t.Parallel() + if _, err := PublicKeyToJWK(nil); err == nil { + t.Error("nil key should fail") + } + p384, err := ecdsa.GenerateKey(elliptic.P384(), rand.Reader) + if err != nil { + t.Fatal(err) + } + if _, err := PublicKeyToJWK(&p384.PublicKey); err == nil { + t.Error("P-384 key should fail") + } +} + +func TestMultibaseRoundTrip(t *testing.T) { + t.Parallel() + + p256 := genP256(t) + edPub, _ := genEd25519(t) + + for _, tc := range []struct { + name string + pub any + }{ + {"P-256", &p256.PublicKey}, + {"Ed25519", edPub}, + } { + t.Run(tc.name, func(t *testing.T) { + encoded, err := EncodeMultibase(tc.pub) + if err != nil { + t.Fatalf("encode: %v", err) + } + if !strings.HasPrefix(encoded, "z") { + t.Fatalf("multibase prefix: %s", encoded) + } + back, err := DecodeMultibase(encoded) + if err != nil { + t.Fatalf("decode: %v", err) + } + switch want := tc.pub.(type) { + case *ecdsa.PublicKey: + if got, ok := back.(*ecdsa.PublicKey); !ok || !got.Equal(want) { + t.Fatal("P-256 round trip lost the key") + } + case ed25519.PublicKey: + if got, ok := back.(ed25519.PublicKey); !ok || !got.Equal(want) { + t.Fatal("Ed25519 round trip lost the key") + } + } + }) + } + + // The canonical did:key Ed25519 prefix. + encoded, err := EncodeMultibase(edPub) + if err != nil { + t.Fatal(err) + } + if !strings.HasPrefix(encoded, "z6Mk") { + t.Fatalf("ed25519 did:key form should start z6Mk, got %s", encoded[:6]) + } +} + +func TestDecodeMultibaseRejections(t *testing.T) { + t.Parallel() + cases := []struct { + name string + in string + want string + }{ + {"no z prefix", "Qabc", "base58btc"}, + {"bad base58", "z0OIl", "invalid base58"}, + {"empty payload", "z", "empty base58"}, + {"too short", "z" + base58Encode([]byte{0xed}), "too short"}, + {"unknown codec", "z" + base58Encode([]byte{0x01, 0x02, 0x03}), "unsupported multicodec"}, + {"wrong p256 length", "z" + base58Encode(append([]byte{0x80, 0x24}, make([]byte, 10)...)), "33 compressed bytes"}, + {"wrong ed25519 length", "z" + base58Encode(append([]byte{0xed, 0x01}, make([]byte, 10)...)), "32 bytes"}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + _, err := DecodeMultibase(tc.in) + if err == nil || !strings.Contains(err.Error(), tc.want) { + t.Fatalf("want error containing %q, got %v", tc.want, err) + } + }) + } + // Invalid compressed point: right length, but 0x05 is not a + // valid SEC1 compression prefix (must be 0x02/0x03). + garbage := append([]byte{0x80, 0x24, 0x05}, make([]byte, 32)...) + if _, err := DecodeMultibase("z" + base58Encode(garbage)); err == nil { + t.Error("invalid compressed point should fail") + } +} + +func TestEncodeMultibaseRejectsUnsupported(t *testing.T) { + t.Parallel() + if _, err := EncodeMultibase(nil); err == nil { + t.Error("nil key should fail") + } + rsaKey, err := rsa.GenerateKey(rand.Reader, 2048) + if err != nil { + t.Fatal(err) + } + if _, err := EncodeMultibase(&rsaKey.PublicKey); err == nil { + t.Error("RSA has no multicodec form here") + } +} + +func TestDecodeDIDKey(t *testing.T) { + t.Parallel() + for _, tc := range []struct { + name string + pub any + }{ + {"P-256", func() any { k := genP256(t); return &k.PublicKey }()}, + {"Ed25519", func() any { k, _ := genEd25519(t); return k }()}, + } { + t.Run(tc.name, func(t *testing.T) { + msid, err := EncodeMultibase(tc.pub) + if err != nil { + t.Fatal(err) + } + did := "did:key:" + msid + pub, kid, err := DecodeDIDKey(did) + if err != nil { + t.Fatalf("decode: %v", err) + } + if kid != did+"#"+msid { + t.Fatalf("kid = %s", kid) + } + if pub == nil { + t.Fatal("nil key") + } + }) + } + + if _, _, err := DecodeDIDKey("did:web:a.com"); err == nil { + t.Error("non-did:key should fail") + } + if _, _, err := DecodeDIDKey("did:key:"); err == nil { + t.Error("empty msid should fail") + } +} + +func TestBase58RoundTrip(t *testing.T) { + t.Parallel() + cases := [][]byte{ + {}, + {0x00}, + {0x00, 0x00, 0x01}, + {0xff, 0xfe, 0xfd}, + []byte("hello base58 world"), + } + for _, in := range cases { + enc := base58Encode(in) + if len(in) == 0 { + if enc != "" { + t.Errorf("empty input encoded to %q", enc) + } + continue + } + got, err := base58Decode(enc) + if err != nil { + t.Fatalf("decode %q: %v", enc, err) + } + if string(got) != string(in) { + t.Errorf("round trip %x → %s → %x", in, enc, got) + } + } +} + +// TestEdDSAJWSVerify pins the EdDSA arm in the standard-JWS verifier: +// an Ed25519 signature over the raw signing input (RFC 8037 — no +// prehash) verifies, and a P-256 key claiming EdDSA is rejected by +// the alg↔key pin. +func TestEdDSAJWSVerify(t *testing.T) { + t.Parallel() + pub, priv := genEd25519(t) + + header := base64.RawURLEncoding.EncodeToString([]byte(`{"alg":"EdDSA","kid":"k1"}`)) + payload := base64.RawURLEncoding.EncodeToString([]byte(`{"hello":"world"}`)) + signingInput := header + "." + payload + sig := ed25519.Sign(priv, []byte(signingInput)) + jws := signingInput + "." + base64.RawURLEncoding.EncodeToString(sig) + + if _, err := VerifyStandardJWSWithPublicKey(pub, jws); err != nil { + t.Fatalf("EdDSA verify: %v", err) + } + + // Tampered payload fails. + bad := header + "." + base64.RawURLEncoding.EncodeToString([]byte(`{"hello":"mars"}`)) + + "." + base64.RawURLEncoding.EncodeToString(sig) + if _, err := VerifyStandardJWSWithPublicKey(pub, bad); err == nil { + t.Fatal("tampered EdDSA payload must fail") + } + + // Alg/key confusion: EdDSA header with a P-256 key. + p256 := genP256(t) + if _, err := VerifyStandardJWSWithPublicKey(&p256.PublicKey, jws); err == nil { + t.Fatal("EdDSA header must not verify with an ECDSA key") + } +} diff --git a/internal/crypto/jws.go b/internal/crypto/jws.go index 3931fb9..14c1490 100644 --- a/internal/crypto/jws.go +++ b/internal/crypto/jws.go @@ -3,6 +3,7 @@ package crypto import ( "context" "crypto/ecdsa" + "crypto/ed25519" "crypto/rsa" "crypto/sha256" "crypto/x509" @@ -22,6 +23,7 @@ import ( const ( AlgES256 = "ES256" // ECDSA P-256 + SHA-256 (primary) AlgRS256 = "RS256" // RSA PKCS#1v1.5 + SHA-256 (interop only) + AlgEdDSA = "EdDSA" // Ed25519 over the raw signing input (identity proofs) ) // Sentinel errors. @@ -62,6 +64,12 @@ type JWSProtectedHeader struct { // The TL uses this together with Kid to look up the producer key // when verifying. RAID string `json:"raid,omitempty"` + // Jwk optionally embeds the signer's public key (RFC 7515 §4.1.3). + // Identity control proofs set it so the noop DID resolver can + // synthesize a document from the submitted proofs; the web + // resolver ignores it — the authoritatively resolved document is + // always the key source. Never set on producer/TL signatures. + Jwk json.RawMessage `json:"jwk,omitempty"` } // SignDetachedJWS produces a detached JWS — compact-serialization @@ -314,7 +322,8 @@ func VerifyStandardJWSWithPublicKey(pub any, jwsCompact string) (*JWSProtectedHe if err != nil { return nil, fmt.Errorf("%w: decode signature: %w", ErrJWSVerify, err) } - digest := sha256.Sum256([]byte(encodedHeader + "." + encodedPayload)) + signingInput := encodedHeader + "." + encodedPayload + digest := sha256.Sum256([]byte(signingInput)) switch key := pub.(type) { case *ecdsa.PublicKey: r, s, err := P1363ToScalars(sig) @@ -328,12 +337,36 @@ func VerifyStandardJWSWithPublicKey(pub any, jwsCompact string) (*JWSProtectedHe if err := rsaVerifyPKCS1v15(key, digest[:], sig); err != nil { return nil, fmt.Errorf("%w: %w", ErrJWSVerify, err) } + case ed25519.PublicKey: + // EdDSA signs the raw signing input — no prehash (RFC 8037 §3.1). + if !ed25519.Verify(key, []byte(signingInput), sig) { + return nil, fmt.Errorf("%w: ed25519 signature mismatch", ErrJWSVerify) + } default: return nil, fmt.Errorf("%w: unsupported public key type %T", ErrJWSVerify, pub) } return header, nil } +// DecodeStandardJWS splits a standard (non-detached) compact JWS and +// returns its decoded protected header plus the raw base64url payload +// segment, without verifying anything. The identity verify-control +// path uses it to (a) read kid/alg/jwk for key selection and (b) +// enforce payload-equality against the served signingInput BEFORE any +// signature verification — clients sign the served bytes verbatim and +// never canonicalize. +func DecodeStandardJWS(jwsCompact string) (*JWSProtectedHeader, string, error) { + encodedHeader, encodedPayload, _, err := splitStandardJWS(jwsCompact) + if err != nil { + return nil, "", fmt.Errorf("%w: %w", ErrJWSDecode, err) + } + header, err := decodeHeader(encodedHeader) + if err != nil { + return nil, "", err + } + return header, encodedPayload, nil +} + // splitStandardJWS parses the "header.payload.signature" compact form // (payload segment non-empty — the non-detached variant). func splitStandardJWS(jwsCompact string) (string, string, string, error) { @@ -458,6 +491,11 @@ func verifyWithPublicKey(pub any, alg, encodedHeader, encodedSig string, payload if err := rsaVerifyPKCS1v15(key, digest[:], sig); err != nil { return fmt.Errorf("%w: %w", ErrJWSVerify, err) } + case ed25519.PublicKey: + // EdDSA signs the raw signing input — no prehash (RFC 8037 §3.1). + if !ed25519.Verify(key, []byte(signingInput), sig) { + return fmt.Errorf("%w: ed25519 signature mismatch", ErrJWSVerify) + } default: return fmt.Errorf("%w: unsupported public key type %T", ErrJWSVerify, pub) } @@ -491,6 +529,8 @@ func algForPublicKey(pub any) (string, error) { return AlgES256, nil case *rsa.PublicKey: return AlgRS256, nil + case ed25519.PublicKey: + return AlgEdDSA, nil default: return "", fmt.Errorf("jws: unsupported public key type %T", pub) } @@ -508,6 +548,11 @@ func checkAlgMatchesKey(alg string, pub any) error { return fmt.Errorf("jws: alg RS256 requires RSA key, got %T", pub) } return nil + case AlgEdDSA: + if _, ok := pub.(ed25519.PublicKey); !ok { + return fmt.Errorf("jws: alg EdDSA requires Ed25519 key, got %T", pub) + } + return nil default: return fmt.Errorf("jws: unsupported algorithm %q", alg) } @@ -519,6 +564,8 @@ func joseAlgorithm(alg string) (jose.SignatureAlgorithm, error) { return jose.ES256, nil case AlgRS256: return jose.RS256, nil + case AlgEdDSA: + return jose.EdDSA, nil default: return "", fmt.Errorf("jws: unsupported algorithm %q", alg) } diff --git a/internal/crypto/proofinput.go b/internal/crypto/proofinput.go new file mode 100644 index 0000000..b97de6e --- /dev/null +++ b/internal/crypto/proofinput.go @@ -0,0 +1,60 @@ +package crypto + +import ( + "encoding/base64" + "encoding/json" + "fmt" +) + +// IdentityProofPurpose is the domain-separation discriminator inside +// every identity proof signing input. An operator's identity keys +// also sign VCs, DIDComm, and application payloads; a purpose-tagged +// input can never verify as any of those (nor vice versa). +const IdentityProofPurpose = "ans:identity-proof:v1" + +// IdentityProofInput is the ONE signing input in the identity proof +// system — the JSON object whose RFC 8785 (JCS) canonical bytes the +// registrant signs to prove key control. It binds the proof to: +// +// - this identifier (anti-substitution), +// - this identity object (identityId — anti-cross-use), +// - this challenge round (nonce — anti-replay), +// - this protocol (purpose — anti-cross-protocol-confusion), +// - this RA deployment (raId — a signature minted against staging +// can never replay against production), +// - this scheme. +// +// The RA serves the encoded form (SigningInput) in the 202 response; +// clients sign it verbatim and never need a JCS implementation — the +// RA checks payload-equality before verifying any signature, so +// canonicalization-mismatch interop failures are structurally +// impossible. +type IdentityProofInput struct { + Identifier string `json:"identifier"` + IdentityID string `json:"identityId"` + Nonce string `json:"nonce"` + Purpose string `json:"purpose"` + RaID string `json:"raId"` + Scheme string `json:"scheme"` +} + +// Canonical returns the JCS-canonical bytes of the proof input — the +// exact bytes a compact JWS over this input must carry as its payload. +func (in IdentityProofInput) Canonical() ([]byte, error) { + raw, err := json.Marshal(in) + if err != nil { + return nil, fmt.Errorf("proofinput: marshal: %w", err) + } + return Canonicalize(raw) +} + +// SigningInput returns the base64url (unpadded) encoding of the +// canonical bytes — the string served in the 202 challenge response. +// A compact JWS's payload segment MUST equal this string verbatim. +func (in IdentityProofInput) SigningInput() (string, error) { + canonical, err := in.Canonical() + if err != nil { + return "", err + } + return base64.RawURLEncoding.EncodeToString(canonical), nil +} diff --git a/internal/crypto/proofinput_test.go b/internal/crypto/proofinput_test.go new file mode 100644 index 0000000..0fa0278 --- /dev/null +++ b/internal/crypto/proofinput_test.go @@ -0,0 +1,69 @@ +package crypto + +import ( + "encoding/base64" + "encoding/json" + "strings" + "testing" +) + +func sampleProofInput() IdentityProofInput { + return IdentityProofInput{ + Identifier: "did:web:identity.acme-corp.com", + IdentityID: "01HXKQ00000000000000000000", + Nonce: "bm9uY2U", + Purpose: IdentityProofPurpose, + RaID: "ans-ra-local", + Scheme: "did:web", + } +} + +func TestIdentityProofInputCanonical(t *testing.T) { + in := sampleProofInput() + canonical, err := in.Canonical() + if err != nil { + t.Fatalf("canonical: %v", err) + } + // JCS: keys sorted lexicographically, no whitespace. + s := string(canonical) + order := []string{`"identifier"`, `"identityId"`, `"nonce"`, `"purpose"`, `"raId"`, `"scheme"`} + last := -1 + for _, key := range order { + idx := strings.Index(s, key) + if idx < 0 || idx < last { + t.Fatalf("canonical key order wrong: %s", s) + } + last = idx + } + if strings.Contains(s, " ") { + t.Fatalf("canonical bytes contain whitespace: %s", s) + } + + // Deterministic. + again, err := in.Canonical() + if err != nil || string(again) != s { + t.Fatal("canonicalization must be deterministic") + } +} + +func TestIdentityProofInputSigningInput(t *testing.T) { + in := sampleProofInput() + encoded, err := in.SigningInput() + if err != nil { + t.Fatalf("signing input: %v", err) + } + decoded, err := base64.RawURLEncoding.DecodeString(encoded) + if err != nil { + t.Fatalf("signing input is not base64url: %v", err) + } + var back IdentityProofInput + if err := json.Unmarshal(decoded, &back); err != nil { + t.Fatalf("decoded payload is not a proof input: %v", err) + } + if back != in { + t.Fatalf("round trip mismatch: %+v", back) + } + if back.Purpose != "ans:identity-proof:v1" { + t.Fatalf("purpose: %s", back.Purpose) + } +} diff --git a/internal/domain/identity.go b/internal/domain/identity.go new file mode 100644 index 0000000..6868259 --- /dev/null +++ b/internal/domain/identity.go @@ -0,0 +1,449 @@ +package domain + +import ( + "fmt" + "strings" + "time" +) + +// IdentifierKind classifies a verified identity's identifier scheme. +// The set is spec-frozen: adding a kind is an ANS-spec amendment plus +// a per-kind control-proof implementation, never just a new string. +type IdentifierKind string + +// Identifier kinds. A kind being *recognized* here is independent of +// it being *enabled* — the service layer dispatches per kind and +// returns IDENTIFIER_KIND_UNSUPPORTED for kinds this deployment has +// no control verifier for (lei is recognized but postponed). +const ( + // KindDIDWeb — did:web. The authoritative keys live in the DID + // document fetched from the operator's web host; control is + // possession of the document's assertionMethod keys. + KindDIDWeb IdentifierKind = "did:web" + + // KindDIDKey — did:key. The key IS the identifier (decoded from + // the DID string); zero I/O, pure key possession. + KindDIDKey IdentifierKind = "did:key" + + // KindLEI — an ISO 17442 Legal Entity Identifier, proven through + // a vLEI credential presentation. Postponed: recognized so the + // error is precise, but no control verifier ships yet. + KindLEI IdentifierKind = "lei" +) + +// IdentityStatus is the verified-identity lifecycle state. +type IdentityStatus string + +// Identity lifecycle states. The machine is deliberately tiny: +// +// PENDING_CONTROL → VERIFIED → REVOKED +// +// with VERIFIED → VERIFIED on rotation (a staged replacement proves +// control again before anything changes — the previously sealed +// state stands until the new proof lands). +const ( + IdentityPendingControl IdentityStatus = "PENDING_CONTROL" + IdentityVerified IdentityStatus = "VERIFIED" + IdentityRevoked IdentityStatus = "REVOKED" +) + +// IdentityChallenge is the single-use anti-replay nonce issued at +// register / rotate time. The registrant signs the served +// IdentityProofInput (which embeds this nonce); the nonce is consumed +// exactly once, inside the success transaction of verify-control — +// a failed verification attempt does NOT consume it, so a registrant +// may retry a bad proof until expiry. +type IdentityChallenge struct { + // Nonce is the base64url-encoded 32-byte random value. + Nonce string + // ExpiresAt bounds the challenge's validity. + ExpiresAt time.Time + // ConsumedAt is set when a verify-control succeeded against this + // nonce. A consumed nonce can never verify again. + ConsumedAt *time.Time +} + +// VerifiedIdentity is the "who" aggregate — an identity owned by a +// providerId, proven through a per-kind control proof, sealed onto +// its own Transparency Log stream, and linked to any number of that +// owner's agents. It is NOT part of AgentRegistration: the agent (the +// "what") carries no identity fields, and the two lifecycles never +// cascade into each other. +// +// No public-key field lives on this aggregate (ANS-0 §6.2 key +// transience): proven keys are sealed in the identity's TL events, +// not persisted as live state. +type VerifiedIdentity struct { + // IdentityID is the RA-assigned UUIDv7 — the TL stream key. + IdentityID string + // ProviderID is the owning authentication principal — the same + // principal that owns the agents this identity may link to. + ProviderID string + Kind IdentifierKind + // Value is the canonical identifier (e.g. + // "did:web:identity.acme-corp.com"). + Value string + Status IdentityStatus + // ProofMethod names the control proof that verified this + // identity ("did-web-sig" | "did-key-sig"). Empty until the + // first successful verify-control. + ProofMethod string + // PendingValue stages a same-kind replacement during rotation + // (§4.2): set by StageRotation, applied by CompleteVerification. + // While staged, the previously sealed state stands. + PendingValue string + // Challenge is the live anti-replay nonce, if any. + Challenge *IdentityChallenge + VerifiedAt time.Time // zero until first proof + CreatedAt time.Time + UpdatedAt time.Time +} + +// proofMethodForKind maps a kind to its sealed proofMethod token. +func proofMethodForKind(kind IdentifierKind) string { + switch kind { + case KindDIDWeb: + return "did-web-sig" + case KindDIDKey: + return "did-key-sig" + case KindLEI: + return "lei-vlei-acdc" + default: + return "" + } +} + +// ProofMethodForKind exposes the kind → proofMethod mapping for the +// service layer's event builder. +func ProofMethodForKind(kind IdentifierKind) string { return proofMethodForKind(kind) } + +// InferIdentifierKind lexically classifies a raw identifier value and +// returns its canonical form. Kind inference is pure dispatch — it +// proves nothing; the per-kind control proof is the gate. +func InferIdentifierKind(raw string) (IdentifierKind, string, error) { + value := strings.TrimSpace(raw) + switch { + case strings.HasPrefix(value, "did:web:"): + canonical, err := canonicalizeDIDWeb(value) + if err != nil { + return "", "", err + } + return KindDIDWeb, canonical, nil + case strings.HasPrefix(value, "did:key:"): + if len(value) <= len("did:key:") { + return "", "", NewValidationError("DID_BAD_FORMAT", "did:key value is empty") + } + return KindDIDKey, value, nil + case isLEI(value): + return KindLEI, strings.ToUpper(value), nil + case strings.HasPrefix(value, "did:"): + // A well-formed DID of a method we don't dispatch yet + // (did:plc, did:ion, did:ethr, …) — name the method so the + // caller learns exactly what's missing, not just "no kind". + // Adding a method = a dispatch arm here (canonicalization + // rules are per-method) + a controlVerifier registration in + // the service's identitykinds registry. + method := value[len("did:"):] + if i := strings.IndexByte(method, ':'); i > 0 { + method = method[:i] + } + return "", "", NewValidationError("IDENTIFIER_KIND_UNSUPPORTED", + fmt.Sprintf("did method %q is not supported (supported: did:web, did:key)", method)) + default: + return "", "", NewValidationError("IDENTIFIER_KIND_UNSUPPORTED", + fmt.Sprintf("identifier %q matches no supported kind (did:web, did:key, lei)", value)) + } +} + +// isLEI reports whether the value is shaped like an ISO 17442 LEI: +// exactly 20 alphanumeric characters. (The mod-97 check digit and the +// GLEIF status precondition belong to the lei control verifier, which +// is postponed — this is lexical dispatch only.) +func isLEI(value string) bool { + if len(value) != 20 { + return false + } + for _, r := range value { + switch { + case r >= '0' && r <= '9', r >= 'A' && r <= 'Z', r >= 'a' && r <= 'z': + default: + return false + } + } + return true +} + +// canonicalizeDIDWeb validates a did:web identifier and returns its +// canonical form (host lowercased; path segments preserved). +// +// v1 restrictions (each rejected with DID_BAD_FORMAT): +// - no port (the did:web encoding of a port is %3A — fetches are +// pinned to 443), +// - no userinfo, +// - no percent-encoded characters at all (did:web allows them in +// path segments; v1 keeps the grammar strict so the resolution +// URL is byte-derivable from the DID), +// - host must be a plausible DNS name (letters, digits, hyphens, +// dots; no leading/trailing hyphen or dot in a label). +func canonicalizeDIDWeb(value string) (string, error) { + rest := strings.TrimPrefix(value, "did:web:") + if rest == "" { + return "", NewValidationError("DID_BAD_FORMAT", "did:web value is empty") + } + if strings.Contains(rest, "%") { + return "", NewValidationError("DID_BAD_FORMAT", + "did:web with percent-encoded characters (including ports, %3A) is not supported") + } + if strings.Contains(rest, "@") { + return "", NewValidationError("DID_BAD_FORMAT", "did:web must not carry userinfo") + } + if strings.Contains(rest, "/") { + return "", NewValidationError("DID_BAD_FORMAT", + "did:web path segments are separated by ':' in the DID, not '/'") + } + segments := strings.Split(rest, ":") + host := strings.ToLower(segments[0]) + if err := validateDNSHost(host); err != nil { + return "", err + } + for _, seg := range segments[1:] { + if seg == "" { + return "", NewValidationError("DID_BAD_FORMAT", "did:web has an empty path segment") + } + } + canonical := "did:web:" + host + if len(segments) > 1 { + canonical += ":" + strings.Join(segments[1:], ":") + } + return canonical, nil +} + +// validateDNSHost applies conservative DNS-name validation to the +// did:web host portion. +func validateDNSHost(host string) error { + if host == "" || len(host) > 253 { + return NewValidationError("DID_BAD_FORMAT", "did:web host is empty or too long") + } + for _, label := range strings.Split(host, ".") { + if label == "" || len(label) > 63 { + return NewValidationError("DID_BAD_FORMAT", + fmt.Sprintf("did:web host %q has an invalid label", host)) + } + if label[0] == '-' || label[len(label)-1] == '-' { + return NewValidationError("DID_BAD_FORMAT", + fmt.Sprintf("did:web host label %q must not start or end with '-'", label)) + } + for _, r := range label { + switch { + case r >= 'a' && r <= 'z', r >= '0' && r <= '9', r == '-': + default: + return NewValidationError("DID_BAD_FORMAT", + fmt.Sprintf("did:web host %q contains invalid character %q", host, r)) + } + } + } + return nil +} + +// DIDWebResolutionURL maps a canonical did:web identifier to the URL +// its DID document resolves at, per the did:web method spec: +// +// did:web:example.com → https://example.com/.well-known/did.json +// did:web:example.com:user:al → https://example.com/user/al/did.json +// +// Pure function — the hardened fetcher in the adapter layer owns the +// actual I/O and its SSRF posture. +func DIDWebResolutionURL(canonicalValue string) (string, error) { + if !strings.HasPrefix(canonicalValue, "did:web:") { + return "", NewValidationError("DID_BAD_FORMAT", "not a did:web identifier") + } + segments := strings.Split(strings.TrimPrefix(canonicalValue, "did:web:"), ":") + host := segments[0] + if host == "" { + return "", NewValidationError("DID_BAD_FORMAT", "did:web host is empty") + } + if len(segments) == 1 { + return "https://" + host + "/.well-known/did.json", nil + } + return "https://" + host + "/" + strings.Join(segments[1:], "/") + "/did.json", nil +} + +// NewVerifiedIdentity constructs a fresh identity in PENDING_CONTROL. +// The caller supplies the RA-assigned identityID (UUIDv7) and the +// authenticated owner; the raw value is kind-inferred and +// canonicalized here so every code path shares one grammar. +func NewVerifiedIdentity(identityID, providerID, rawValue string, now time.Time) (*VerifiedIdentity, error) { + if identityID == "" { + return nil, NewValidationError("INVALID_IDENTITY_ID", "identityId is required") + } + if providerID == "" { + return nil, NewValidationError("INVALID_PROVIDER_ID", "providerId is required") + } + kind, canonical, err := InferIdentifierKind(rawValue) + if err != nil { + return nil, err + } + return &VerifiedIdentity{ + IdentityID: identityID, + ProviderID: providerID, + Kind: kind, + Value: canonical, + Status: IdentityPendingControl, + CreatedAt: now.UTC(), + UpdatedAt: now.UTC(), + }, nil +} + +// IssueChallenge mints a fresh anti-replay nonce, superseding any +// prior one (idempotent re-add, §4.2: a re-POST while PENDING_CONTROL +// returns the same identity with a fresh challenge). Valid while the +// identity is PENDING_CONTROL (initial proof) or VERIFIED (rotation +// re-proof); a revoked identity can never be challenged again. +func (v *VerifiedIdentity) IssueChallenge(nonce string, ttl time.Duration, now time.Time) error { + if v.Status == IdentityRevoked { + return NewInvalidStateError("IDENTITY_REVOKED", + "a revoked identity cannot be re-challenged") + } + if nonce == "" { + return NewValidationError("INVALID_CHALLENGE", "challenge nonce is required") + } + if ttl <= 0 { + return NewValidationError("INVALID_CHALLENGE", "challenge ttl must be positive") + } + v.Challenge = &IdentityChallenge{ + Nonce: nonce, + ExpiresAt: now.UTC().Add(ttl), + } + v.UpdatedAt = now.UTC() + return nil +} + +// CheckChallenge reports whether the live challenge can still be +// proven against: present, unconsumed, unexpired. It does NOT consume +// — consumption is a storage-level conditional update inside the +// verify-control success transaction (the TOCTOU guard). +func (v *VerifiedIdentity) CheckChallenge(now time.Time) error { + switch { + case v.Challenge == nil: + return NewInvalidStateError("IDENTIFIER_CHALLENGE_EXPIRED", + "no active challenge; re-register the identifier to receive a fresh one") + case v.Challenge.ConsumedAt != nil: + return NewInvalidStateError("PRICC_TOKEN_ALREADY_USED", + "challenge nonce already consumed") + case !now.Before(v.Challenge.ExpiresAt): + return NewInvalidStateError("PRICC_TOKEN_EXPIRED", + "challenge nonce expired; re-register the identifier to receive a fresh one") + default: + return nil + } +} + +// EffectiveValue is the identifier the current proof round is over: +// the staged replacement during a rotation, otherwise the proven +// value. +func (v *VerifiedIdentity) EffectiveValue() string { + if v.PendingValue != "" { + return v.PendingValue + } + return v.Value +} + +// StageRotation stages a same-kind replacement value (§4.2 PUT). The +// row stays VERIFIED with the old value — nothing changes until the +// replacement proves control. Cross-kind replacement is rejected +// (remove + add, not a rotation). +func (v *VerifiedIdentity) StageRotation(rawValue string, now time.Time) error { + if v.Status != IdentityVerified { + return NewInvalidStateError("IDENTITY_NOT_VERIFIED", + fmt.Sprintf("rotation requires a VERIFIED identity, status is %s", v.Status)) + } + kind, canonical, err := InferIdentifierKind(rawValue) + if err != nil { + return err + } + if kind != v.Kind { + return NewValidationError("IDENTIFIER_KIND_MISMATCH", + fmt.Sprintf("cannot rotate a %s identity to %s; revoke and register a new identity", v.Kind, kind)) + } + v.PendingValue = canonical + v.UpdatedAt = now.UTC() + return nil +} + +// CompleteVerification applies a successful control proof: +// +// - PENDING_CONTROL → VERIFIED (first proof; seals IDENTITY_VERIFIED) +// - VERIFIED with a staged rotation → swap the value (seals +// IDENTITY_UPDATED) +// - VERIFIED without a staged rotation → re-proof of the current +// value (also seals IDENTITY_UPDATED — the proven key set may +// have changed even when the value did not) +// +// Returns the previous value when the proof completes a rotation that +// changed the identifier (for the sealed event's previousValue). +func (v *VerifiedIdentity) CompleteVerification(now time.Time) (string, error) { + previousValue := "" + switch v.Status { + case IdentityPendingControl: + v.Status = IdentityVerified + case IdentityVerified: + if v.PendingValue != "" && v.PendingValue != v.Value { + previousValue = v.Value + v.Value = v.PendingValue + } + case IdentityRevoked: + return "", NewInvalidStateError("IDENTITY_REVOKED", + "a revoked identity cannot be verified") + default: + return "", NewInvalidStateError("IDENTITY_INVALID_STATE", + fmt.Sprintf("unexpected identity status %s", v.Status)) + } + v.PendingValue = "" + v.ProofMethod = proofMethodForKind(v.Kind) + v.VerifiedAt = now.UTC() + v.UpdatedAt = now.UTC() + return previousValue, nil +} + +// Revoke transitions a VERIFIED identity to REVOKED — a state change, +// never a delete: the identity's history is append-only in the TL. +// Revoking an unproven (PENDING_CONTROL) identity is rejected — it +// never sealed anything, so there is nothing to revoke; its challenge +// simply expires. +func (v *VerifiedIdentity) Revoke(now time.Time) error { + if v.Status != IdentityVerified { + return NewInvalidStateError("IDENTITY_NOT_VERIFIED", + fmt.Sprintf("revocation requires a VERIFIED identity, status is %s", v.Status)) + } + v.Status = IdentityRevoked + v.PendingValue = "" + v.Challenge = nil + v.UpdatedAt = now.UTC() + return nil +} + +// LinkStatus is the lifecycle state of one identity↔agent +// association. +type LinkStatus string + +// Link states. UNLINKED rows are history caches — the sealed +// IDENTITY_UNLINKED event is the authoritative record — and never +// block re-linking. +const ( + LinkLinked LinkStatus = "LINKED" + LinkUnlinked LinkStatus = "UNLINKED" +) + +// IdentityLink associates a verified identity with one agent of the +// same owner. It is the only thing that touches an agent — a row plus +// a sealed event on the IDENTITY stream, never a field on the +// registration aggregate. Links carry no proof: within one owner a +// link asserts a fact the owner gate already establishes (§4.3). +type IdentityLink struct { + IdentityID string + AgentID string + Status LinkStatus + LinkedAt time.Time + CreatedAt time.Time + UpdatedAt time.Time +} diff --git a/internal/domain/identity_test.go b/internal/domain/identity_test.go new file mode 100644 index 0000000..ea0644f --- /dev/null +++ b/internal/domain/identity_test.go @@ -0,0 +1,282 @@ +package domain + +import ( + "strings" + "testing" + "time" +) + +var idNow = time.Date(2026, 6, 10, 15, 0, 0, 0, time.UTC) + +func newPendingIdentity(t *testing.T, value string) *VerifiedIdentity { + t.Helper() + v, err := NewVerifiedIdentity("01HXKQTEST", "PID-1", value, idNow) + if err != nil { + t.Fatalf("NewVerifiedIdentity(%s): %v", value, err) + } + return v +} + +func TestInferIdentifierKind(t *testing.T) { + cases := []struct { + in string + wantKind IdentifierKind + wantCanonical string + wantErr string + }{ + {in: "did:web:identity.acme-corp.com", wantKind: KindDIDWeb, wantCanonical: "did:web:identity.acme-corp.com"}, + {in: " did:web:Identity.ACME-corp.COM ", wantKind: KindDIDWeb, wantCanonical: "did:web:identity.acme-corp.com"}, + {in: "did:web:acme-corp.com:identity:agents", wantKind: KindDIDWeb, wantCanonical: "did:web:acme-corp.com:identity:agents"}, + {in: "did:key:zDnaeUm3QkcyZWZTPttxB711jgqRDhkwvhF485SFw1bDZ9AQw", wantKind: KindDIDKey, wantCanonical: "did:key:zDnaeUm3QkcyZWZTPttxB711jgqRDhkwvhF485SFw1bDZ9AQw"}, + {in: "5493001KJTIIGC8Y1R17", wantKind: KindLEI, wantCanonical: "5493001KJTIIGC8Y1R17"}, + {in: "5493001kjtiigc8y1r17", wantKind: KindLEI, wantCanonical: "5493001KJTIIGC8Y1R17"}, + {in: "did:web:", wantErr: "DID_BAD_FORMAT"}, + {in: "did:web:acme.com%3A8443", wantErr: "DID_BAD_FORMAT"}, + {in: "did:web:user@acme.com", wantErr: "DID_BAD_FORMAT"}, + {in: "did:web:acme.com/path", wantErr: "DID_BAD_FORMAT"}, + {in: "did:web:acme.com:", wantErr: "DID_BAD_FORMAT"}, + {in: "did:web:acme..com", wantErr: "DID_BAD_FORMAT"}, + {in: "did:web:-acme.com", wantErr: "DID_BAD_FORMAT"}, + {in: "did:web:acme_corp.com", wantErr: "DID_BAD_FORMAT"}, + {in: "did:key:", wantErr: "DID_BAD_FORMAT"}, + {in: "urn:uuid:1234", wantErr: "IDENTIFIER_KIND_UNSUPPORTED"}, + {in: "", wantErr: "IDENTIFIER_KIND_UNSUPPORTED"}, + {in: "5493001KJTIIGC8Y1R1!", wantErr: "IDENTIFIER_KIND_UNSUPPORTED"}, + // Unrecognized did methods name the method precisely — + // these are the kinds the controlVerifier registry grows + // into (did:plc, did:ion, did:ethr, …). + {in: "did:plc:ewvi7nxzyoun6zhxrhs64oiz", wantErr: `did method "plc" is not supported`}, + {in: "did:ion:EiClkZMDxPKqC9c-umQfTkR8", wantErr: `did method "ion" is not supported`}, + {in: "did:bogus", wantErr: `did method "bogus" is not supported`}, + } + for _, tc := range cases { + t.Run(tc.in, func(t *testing.T) { + kind, canonical, err := InferIdentifierKind(tc.in) + if tc.wantErr != "" { + if err == nil || !strings.Contains(err.Error(), tc.wantErr) { + t.Fatalf("want error %q, got %v", tc.wantErr, err) + } + return + } + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + if kind != tc.wantKind || canonical != tc.wantCanonical { + t.Fatalf("got (%s, %s), want (%s, %s)", kind, canonical, tc.wantKind, tc.wantCanonical) + } + }) + } +} + +func TestValidateDNSHostEdges(t *testing.T) { + longHost := strings.Repeat("a", 254) + if _, _, err := InferIdentifierKind("did:web:" + longHost); err == nil { + t.Error("over-long host should fail") + } + longLabel := strings.Repeat("a", 64) + ".com" + if _, _, err := InferIdentifierKind("did:web:" + longLabel); err == nil { + t.Error("over-long label should fail") + } + if _, _, err := InferIdentifierKind("did:web:acme-.com"); err == nil { + t.Error("trailing-hyphen label should fail") + } +} + +func TestDIDWebResolutionURL(t *testing.T) { + cases := []struct { + in string + want string + wantErr bool + }{ + {in: "did:web:example.com", want: "https://example.com/.well-known/did.json"}, + {in: "did:web:example.com:user:alice", want: "https://example.com/user/alice/did.json"}, + {in: "did:key:z6Mk", wantErr: true}, + {in: "did:web:", wantErr: true}, + } + for _, tc := range cases { + got, err := DIDWebResolutionURL(tc.in) + if tc.wantErr { + if err == nil { + t.Errorf("%s: want error", tc.in) + } + continue + } + if err != nil || got != tc.want { + t.Errorf("%s: got (%s, %v), want %s", tc.in, got, err, tc.want) + } + } +} + +func TestNewVerifiedIdentityValidation(t *testing.T) { + if _, err := NewVerifiedIdentity("", "PID-1", "did:web:a.com", idNow); err == nil { + t.Error("missing identityId should fail") + } + if _, err := NewVerifiedIdentity("id-1", "", "did:web:a.com", idNow); err == nil { + t.Error("missing providerId should fail") + } + if _, err := NewVerifiedIdentity("id-1", "PID-1", "bogus", idNow); err == nil { + t.Error("bogus value should fail") + } + v := newPendingIdentity(t, "did:web:a.com") + if v.Status != IdentityPendingControl || v.Kind != KindDIDWeb { + t.Fatalf("fresh identity wrong: %+v", v) + } +} + +func TestIssueAndCheckChallenge(t *testing.T) { + v := newPendingIdentity(t, "did:web:a.com") + + if err := v.IssueChallenge("", time.Hour, idNow); err == nil { + t.Error("empty nonce should fail") + } + if err := v.IssueChallenge("nonce-1", 0, idNow); err == nil { + t.Error("zero ttl should fail") + } + if err := v.CheckChallenge(idNow); err == nil || !strings.Contains(err.Error(), "IDENTIFIER_CHALLENGE_EXPIRED") { + t.Errorf("no challenge should report IDENTIFIER_CHALLENGE_EXPIRED, got %v", err) + } + + if err := v.IssueChallenge("nonce-1", time.Hour, idNow); err != nil { + t.Fatalf("issue: %v", err) + } + if err := v.CheckChallenge(idNow.Add(30 * time.Minute)); err != nil { + t.Errorf("fresh challenge should pass: %v", err) + } + if err := v.CheckChallenge(idNow.Add(time.Hour)); err == nil || !strings.Contains(err.Error(), "PRICC_TOKEN_EXPIRED") { + t.Errorf("expired challenge: got %v", err) + } + + consumed := idNow.Add(time.Minute) + v.Challenge.ConsumedAt = &consumed + if err := v.CheckChallenge(idNow.Add(2 * time.Minute)); err == nil || !strings.Contains(err.Error(), "PRICC_TOKEN_ALREADY_USED") { + t.Errorf("consumed challenge: got %v", err) + } + + // Re-issue supersedes (idempotent re-add). + if err := v.IssueChallenge("nonce-2", time.Hour, idNow.Add(3*time.Minute)); err != nil { + t.Fatalf("re-issue: %v", err) + } + if v.Challenge.Nonce != "nonce-2" || v.Challenge.ConsumedAt != nil { + t.Fatalf("re-issue did not supersede: %+v", v.Challenge) + } + + // Revoked identities cannot be challenged. + v.Status = IdentityRevoked + if err := v.IssueChallenge("nonce-3", time.Hour, idNow); err == nil { + t.Error("revoked identity should not be challengeable") + } +} + +func TestCompleteVerificationLifecycle(t *testing.T) { + v := newPendingIdentity(t, "did:web:a.com") + + prev, err := v.CompleteVerification(idNow) + if err != nil || prev != "" { + t.Fatalf("first proof: prev=%q err=%v", prev, err) + } + if v.Status != IdentityVerified || v.ProofMethod != "did-web-sig" || v.VerifiedAt.IsZero() { + t.Fatalf("after first proof: %+v", v) + } + + // Re-proof without rotation: same value, no previousValue. + prev, err = v.CompleteVerification(idNow.Add(time.Hour)) + if err != nil || prev != "" { + t.Fatalf("re-proof: prev=%q err=%v", prev, err) + } + + // Rotation to a new value. + if err := v.StageRotation("did:web:b.com", idNow); err != nil { + t.Fatalf("stage: %v", err) + } + if v.EffectiveValue() != "did:web:b.com" || v.Value != "did:web:a.com" { + t.Fatalf("staged state wrong: %+v", v) + } + prev, err = v.CompleteVerification(idNow.Add(2 * time.Hour)) + if err != nil || prev != "did:web:a.com" { + t.Fatalf("rotation: prev=%q err=%v", prev, err) + } + if v.Value != "did:web:b.com" || v.PendingValue != "" { + t.Fatalf("after rotation: %+v", v) + } + + // Rotation staged to the SAME value: no previousValue reported. + if err := v.StageRotation("did:web:b.com", idNow); err != nil { + t.Fatalf("stage same: %v", err) + } + prev, err = v.CompleteVerification(idNow.Add(3 * time.Hour)) + if err != nil || prev != "" { + t.Fatalf("same-value rotation: prev=%q err=%v", prev, err) + } + + // Revoked → cannot verify. + v.Status = IdentityRevoked + if _, err := v.CompleteVerification(idNow); err == nil { + t.Error("revoked identity should not verify") + } + + // Unknown status → invalid state. + v.Status = IdentityStatus("BOGUS") + if _, err := v.CompleteVerification(idNow); err == nil { + t.Error("unknown status should not verify") + } +} + +func TestStageRotationGuards(t *testing.T) { + v := newPendingIdentity(t, "did:web:a.com") + if err := v.StageRotation("did:web:b.com", idNow); err == nil { + t.Error("rotation requires VERIFIED") + } + if _, err := v.CompleteVerification(idNow); err != nil { + t.Fatal(err) + } + if err := v.StageRotation("bogus", idNow); err == nil { + t.Error("bogus replacement should fail") + } + if err := v.StageRotation("did:key:zDnaeUm3QkcyZWZTPttxB711jgqRDhkwvhF485SFw1bDZ9AQw", idNow); err == nil || + !strings.Contains(err.Error(), "IDENTIFIER_KIND_MISMATCH") { + t.Errorf("cross-kind rotation: got %v", err) + } +} + +func TestRevoke(t *testing.T) { + v := newPendingIdentity(t, "did:web:a.com") + if err := v.Revoke(idNow); err == nil { + t.Error("revoking PENDING_CONTROL should fail — nothing was sealed") + } + if _, err := v.CompleteVerification(idNow); err != nil { + t.Fatal(err) + } + if err := v.IssueChallenge("n", time.Hour, idNow); err != nil { + t.Fatal(err) + } + if err := v.Revoke(idNow.Add(time.Minute)); err != nil { + t.Fatalf("revoke: %v", err) + } + if v.Status != IdentityRevoked || v.Challenge != nil || v.PendingValue != "" { + t.Fatalf("after revoke: %+v", v) + } + if err := v.Revoke(idNow); err == nil { + t.Error("double revoke should fail") + } +} + +func TestProofMethodForKind(t *testing.T) { + cases := map[IdentifierKind]string{ + KindDIDWeb: "did-web-sig", + KindDIDKey: "did-key-sig", + KindLEI: "lei-vlei-acdc", + IdentifierKind("???"): "", + } + for kind, want := range cases { + if got := ProofMethodForKind(kind); got != want { + t.Errorf("ProofMethodForKind(%s) = %q, want %q", kind, got, want) + } + } +} + +func TestEffectiveValueWithoutPending(t *testing.T) { + v := newPendingIdentity(t, "did:web:a.com") + if v.EffectiveValue() != "did:web:a.com" { + t.Fatal("effective value should be the proven value when nothing is staged") + } +} diff --git a/internal/port/didresolver.go b/internal/port/didresolver.go new file mode 100644 index 0000000..77441bb --- /dev/null +++ b/internal/port/didresolver.go @@ -0,0 +1,80 @@ +package port + +import ( + "context" + "encoding/json" +) + +// KeyHint is one kid → public-JWK pair the caller extracted from a +// submitted proof's protected header. Hints exist for the noop +// resolver (below); the web resolver ignores them. +type KeyHint struct { + Kid string + PublicKeyJWK json.RawMessage +} + +// DIDDocument is the subset of a resolved DID document the identity +// control proof needs: the document's id and its assertionMethod +// verification methods. Per DID Core, a key listed under +// assertionMethod is authorized to make assertions as the DID — key +// possession IS what DID control means. +type DIDDocument struct { + ID string + AssertionMethod []VerificationMethod +} + +// VerificationMethod is one assertionMethod entry. Exactly one of +// PublicKeyJwk / PublicKeyMultibase carries the key material. +// +// Raw is the verification-method object EXACTLY as the DID document +// served it — member-for-member, values untouched. Sealed identity +// events quote Raw verbatim: nothing derived, re-encoded, or +// normalized ever enters a seal. The typed fields beside it exist +// for the RA's checks (membership, controller, key parsing) only. +type VerificationMethod struct { + ID string + Controller string + Type string + PublicKeyJwk json.RawMessage + PublicKeyMultibase string + Raw json.RawMessage +} + +// FindAssertionMethod returns the assertionMethod entry with the +// given id, or nil. +func (d *DIDDocument) FindAssertionMethod(kid string) *VerificationMethod { + for i := range d.AssertionMethod { + if d.AssertionMethod[i].ID == kid { + return &d.AssertionMethod[i] + } + } + return nil +} + +// DIDResolver fetches the DID document for a did:web identifier — the +// authoritative key source for did:web control proofs. It is the only +// outbound I/O in the identity proof gate, which makes it the port: +// +// - The "web" adapter performs a hardened HTTPS fetch (WebPKI, +// SSRF dialer guards, timeout, size cap, bounded same-site +// redirects) of the document at the DID's resolution URL. Hints +// are ignored — the resolved document is always the key source. +// +// - The "noop" adapter performs no I/O and synthesizes a document +// from the hints (the kid → JWK pairs embedded in the submitted +// proofs' `jwk` headers). Signature verification still genuinely +// runs against those keys, so sealed events stay self-verifying +// even from quickstart runs — only the binding "the live did.json +// really lists this key" is waived. Mirrors the noop DNS +// verifier: real crypto, waived external-world binding. NOT for +// production. +// +// did:key never reaches this port — its key decodes from the DID +// string with zero I/O. +type DIDResolver interface { + // Resolve returns the DID document for the given canonical + // did:web identifier. Implementations return domain errors with + // the DID_* codes (DID_RESOLUTION_FAILED, + // DID_DOCUMENT_ID_MISMATCH, DID_REDIRECT_DOMAIN_MISMATCH). + Resolve(ctx context.Context, did string, hints []KeyHint) (*DIDDocument, error) +} diff --git a/internal/port/store.go b/internal/port/store.go index 38c79d9..b103b82 100644 --- a/internal/port/store.go +++ b/internal/port/store.go @@ -2,6 +2,7 @@ package port import ( "context" + "time" "github.com/godaddy/ans/internal/domain" ) @@ -148,3 +149,62 @@ type ByocCertificateStore interface { FindByAgentID(ctx context.Context, agentID string) ([]*domain.ByocServerCertificate, error) FindLatestValidByAgentID(ctx context.Context, agentID string) (*domain.ByocServerCertificate, error) } + +// IdentityStore persists VerifiedIdentity aggregates (the "who" — +// owned by a providerId, independent of any agent). +type IdentityStore interface { + // Save upserts the aggregate. The storage layer enforces the two + // uniqueness rules with partial indexes: one live (non-REVOKED) + // row per (provider, kind, value), and one VERIFIED row per + // (kind, value) across all owners — first to prove wins; a save + // that loses the race maps to a conflict error. + Save(ctx context.Context, identity *domain.VerifiedIdentity) error + + // FindByID returns the identity with the given identityId. + FindByID(ctx context.Context, identityID string) (*domain.VerifiedIdentity, error) + + // FindLive returns the owner's non-REVOKED row for (kind, value), + // or a not-found error. Drives the idempotent re-add: a re-POST + // of the same value while PENDING_CONTROL returns the same + // identity with a fresh challenge. + FindLive(ctx context.Context, providerID string, kind domain.IdentifierKind, value string) (*domain.VerifiedIdentity, error) + + // ExistsVerified reports whether ANY owner holds a VERIFIED row + // for (kind, value) — early duplicate feedback at register time. + // The authoritative guard is the proven-uniqueness index at + // verify time. + ExistsVerified(ctx context.Context, kind domain.IdentifierKind, value string) (bool, error) + + // ListByOwner returns every identity owned by the principal, + // newest first. + ListByOwner(ctx context.Context, providerID string) ([]*domain.VerifiedIdentity, error) + + // ConsumeChallenge atomically consumes the live challenge nonce: + // a conditional update that succeeds only while the stored nonce + // matches, is unconsumed, and is unexpired. Exactly one of two + // concurrent verify-control calls can win (the TOCTOU guard); + // the loser receives an invalid-state error. MUST be called + // inside the verify-control success transaction. + ConsumeChallenge(ctx context.Context, identityID, nonce string, now time.Time) error +} + +// IdentityLinkStore persists identity↔agent link rows. Rows are +// read-side caches of the sealed IDENTITY_LINKED / IDENTITY_UNLINKED +// events; UNLINKED rows are history and never block re-linking. +type IdentityLinkStore interface { + // Link upserts a live link for the pair. Returns true when a new + // link was created, false when the pair was already live + // (idempotent — an already-linked agent in a batch is not an + // error, and is excluded from the sealed batch event). + Link(ctx context.Context, identityID, agentID string, now time.Time) (bool, error) + + // Unlink flips the live link to UNLINKED. Not-found error when no + // live link exists. + Unlink(ctx context.Context, identityID, agentID string, now time.Time) error + + // ListLiveByIdentity returns the identity's live links. + ListLiveByIdentity(ctx context.Context, identityID string) ([]*domain.IdentityLink, error) + + // ListLiveByAgent returns the agent's live links. + ListLiveByAgent(ctx context.Context, agentID string) ([]*domain.IdentityLink, error) +} diff --git a/internal/ra/handler/dto.go b/internal/ra/handler/dto.go index d2d3588..a3657ea 100644 --- a/internal/ra/handler/dto.go +++ b/internal/ra/handler/dto.go @@ -85,6 +85,38 @@ type agentDetails struct { LastRenewalTimestamp string `json:"lastRenewalTimestamp,omitempty"` RegistrationPending *registrationPendingResponse `json:"registrationPending,omitempty"` Links []linkDTO `json:"links"` + // Identities is the additive computed view of the verified + // identities currently linked to this agent (design §5.4) — + // computed from the link rows at read time, never stored on the + // registration. + Identities []linkedIdentityDTO `json:"identities,omitempty"` +} + +// linkedIdentityDTO is one computed identities[] entry. +type linkedIdentityDTO struct { + IdentityID string `json:"identityId"` + Kind string `json:"kind"` + Value string `json:"value"` + IdentityStatus string `json:"identityStatus"` + LinkedAt string `json:"linkedAt,omitempty"` +} + +// mapLinkedIdentities converts the service summaries to wire DTOs. +func mapLinkedIdentities(in []service.LinkedIdentitySummary) []linkedIdentityDTO { + out := make([]linkedIdentityDTO, 0, len(in)) + for _, s := range in { + dto := linkedIdentityDTO{ + IdentityID: s.IdentityID, + Kind: string(s.Kind), + Value: s.Value, + IdentityStatus: string(s.IdentityStatus), + } + if !s.LinkedAt.IsZero() { + dto.LinkedAt = s.LinkedAt.UTC().Format("2006-01-02T15:04:05Z07:00") + } + out = append(out, dto) + } + return out } func mapAgentDetails(res *service.DetailResult, r *http.Request, tlPublicBaseURL string) agentDetails { diff --git a/internal/ra/handler/identity.go b/internal/ra/handler/identity.go new file mode 100644 index 0000000..bb9fb58 --- /dev/null +++ b/internal/ra/handler/identity.go @@ -0,0 +1,297 @@ +package handler + +import ( + "encoding/json" + "net/http" + "time" + + "github.com/go-chi/chi/v5" + + "github.com/godaddy/ans/internal/adapter/auth" + "github.com/godaddy/ans/internal/domain" + "github.com/godaddy/ans/internal/ra/service" +) + +// IdentityHandler wires the /v2/ans/identities surface — the "who" +// behind the agents. Ownership is enforced inside the service (the +// owner gate is the link mechanism's security boundary), so these +// handlers only extract the authenticated principal and map DTOs. +type IdentityHandler struct { + svc *service.IdentityService +} + +// NewIdentityHandler constructs an IdentityHandler. +func NewIdentityHandler(svc *service.IdentityService) *IdentityHandler { + return &IdentityHandler{svc: svc} +} + +// identityRegisterRequest is the POST /v2/ans/identities (and PUT +// rotation) body. The kind is inferred from the value's lexical form +// — never caller-asserted. +type identityRegisterRequest struct { + Value string `json:"value"` +} + +// identityChallengeDTO is one entry of the 202 challenge list. +type identityChallengeDTO struct { + Kid string `json:"kid,omitempty"` + SigningInput string `json:"signingInput"` +} + +// identityChallengeResponse is the 202 body returned by register and +// rotate: the identity's id plus the challenge round to sign. +type identityChallengeResponse struct { + IdentityID string `json:"identityId"` + Kind string `json:"kind"` + Value string `json:"value"` + Status string `json:"status"` + Nonce string `json:"nonce"` + ExpiresAt string `json:"expiresAt"` + Challenges []identityChallengeDTO `json:"challenges"` +} + +// verifyControlRequest is the POST .../verify-control body. Members +// are additive per identifier kind — exactly one family is set per +// kind: the JWS schemes (did:web, did:key, and the future did:plc / +// did:ion) submit signedProofs; future kinds add their own optional +// members (lei: cesrSignature; did:ethr: ethSignature) without +// touching existing ones. +type verifyControlRequest struct { + // SignedProofs — one compact JWS per proven key, every payload + // equal to the served signingInput verbatim. + SignedProofs []string `json:"signedProofs"` +} + +// identityDetailResponse is the identity object echoed by +// verify-control, revoke, detail, and list entries. +type identityDetailResponse struct { + IdentityID string `json:"identityId"` + Kind string `json:"kind"` + Value string `json:"value"` + Status string `json:"status"` + ProofMethod string `json:"proofMethod,omitempty"` + PendingValue string `json:"pendingValue,omitempty"` + VerifiedAt string `json:"verifiedAt,omitempty"` + CreatedAt string `json:"createdAt"` + LinkedAgents []linkedAgentDTO `json:"linkedAgents,omitempty"` +} + +type linkedAgentDTO struct { + AgentID string `json:"agentId"` + LinkedAt string `json:"linkedAt,omitempty"` +} + +// linkRequest is the POST .../links body — the batch of the owner's +// agents to bind. One call, one sealed IDENTITY_LINKED event. +type linkRequest struct { + AgentIDs []string `json:"agentIds"` +} + +type linkResponse struct { + Linked int `json:"linked"` +} + +// Register handles POST /v2/ans/identities → 202 + challenges. +func (h *IdentityHandler) Register(w http.ResponseWriter, r *http.Request) { + providerID, ok := callerSubject(w, r) + if !ok { + return + } + var req identityRegisterRequest + if err := json.NewDecoder(r.Body).Decode(&req); err != nil { + WriteError(w, domain.NewValidationError("INVALID_REQUEST_BODY", "request body is not valid JSON")) + return + } + if req.Value == "" { + WriteError(w, domain.NewValidationError("INVALID_IDENTIFIER", "value is required")) + return + } + res, err := h.svc.Register(r.Context(), providerID, req.Value) + if err != nil { + WriteError(w, err) + return + } + WriteJSON(w, http.StatusAccepted, toChallengeResponse(res)) +} + +// Rotate handles PUT /v2/ans/identities/{identityId} → 202 + fresh +// challenges over the staged replacement. +func (h *IdentityHandler) Rotate(w http.ResponseWriter, r *http.Request) { + providerID, ok := callerSubject(w, r) + if !ok { + return + } + var req identityRegisterRequest + if err := json.NewDecoder(r.Body).Decode(&req); err != nil { + WriteError(w, domain.NewValidationError("INVALID_REQUEST_BODY", "request body is not valid JSON")) + return + } + if req.Value == "" { + WriteError(w, domain.NewValidationError("INVALID_IDENTIFIER", "value is required")) + return + } + res, err := h.svc.Rotate(r.Context(), providerID, chi.URLParam(r, "identityId"), req.Value) + if err != nil { + WriteError(w, err) + return + } + WriteJSON(w, http.StatusAccepted, toChallengeResponse(res)) +} + +// VerifyControl handles POST /v2/ans/identities/{identityId}/verify-control. +// Clean proofs flip the identity to VERIFIED (or complete a rotation) +// and seal the event — the 200 echoes the updated identity. +func (h *IdentityHandler) VerifyControl(w http.ResponseWriter, r *http.Request) { + providerID, ok := callerSubject(w, r) + if !ok { + return + } + var req verifyControlRequest + if err := json.NewDecoder(r.Body).Decode(&req); err != nil { + WriteError(w, domain.NewValidationError("INVALID_REQUEST_BODY", "request body is not valid JSON")) + return + } + identity, err := h.svc.VerifyControl(r.Context(), providerID, chi.URLParam(r, "identityId"), + service.ProofSubmission{SignedProofs: req.SignedProofs}) + if err != nil { + WriteError(w, err) + return + } + WriteJSON(w, http.StatusOK, toDetailResponse(identity, nil)) +} + +// Revoke handles POST /v2/ans/identities/{identityId}/revoke — a +// state change (an identity cannot be deleted; its history is +// append-only in the TL), mirroring the agent's revoke verb. +func (h *IdentityHandler) Revoke(w http.ResponseWriter, r *http.Request) { + providerID, ok := callerSubject(w, r) + if !ok { + return + } + identity, err := h.svc.Revoke(r.Context(), providerID, chi.URLParam(r, "identityId")) + if err != nil { + WriteError(w, err) + return + } + WriteJSON(w, http.StatusOK, toDetailResponse(identity, nil)) +} + +// List handles GET /v2/ans/identities — the caller's identities. +func (h *IdentityHandler) List(w http.ResponseWriter, r *http.Request) { + providerID, ok := callerSubject(w, r) + if !ok { + return + } + identities, err := h.svc.List(r.Context(), providerID) + if err != nil { + WriteError(w, err) + return + } + out := make([]identityDetailResponse, 0, len(identities)) + for _, identity := range identities { + out = append(out, toDetailResponse(identity, nil)) + } + WriteJSON(w, http.StatusOK, map[string]any{"identities": out}) +} + +// Detail handles GET /v2/ans/identities/{identityId} — the identity +// plus its live links. +func (h *IdentityHandler) Detail(w http.ResponseWriter, r *http.Request) { + providerID, ok := callerSubject(w, r) + if !ok { + return + } + identity, links, err := h.svc.Detail(r.Context(), providerID, chi.URLParam(r, "identityId")) + if err != nil { + WriteError(w, err) + return + } + WriteJSON(w, http.StatusOK, toDetailResponse(identity, links)) +} + +// Link handles POST /v2/ans/identities/{identityId}/links — the +// owner-gated batch link. 200 {linked: N}; the batch sealed as ONE +// IDENTITY_LINKED event on the identity stream. +func (h *IdentityHandler) Link(w http.ResponseWriter, r *http.Request) { + providerID, ok := callerSubject(w, r) + if !ok { + return + } + var req linkRequest + if err := json.NewDecoder(r.Body).Decode(&req); err != nil { + WriteError(w, domain.NewValidationError("INVALID_REQUEST_BODY", "request body is not valid JSON")) + return + } + linked, err := h.svc.Link(r.Context(), providerID, chi.URLParam(r, "identityId"), req.AgentIDs) + if err != nil { + WriteError(w, err) + return + } + WriteJSON(w, http.StatusOK, linkResponse{Linked: linked}) +} + +// Unlink handles DELETE /v2/ans/identities/{identityId}/links/{agentId}. +// The association ends (204); its history persists in the identity's +// audit chain and the raw log tiles. +func (h *IdentityHandler) Unlink(w http.ResponseWriter, r *http.Request) { + providerID, ok := callerSubject(w, r) + if !ok { + return + } + err := h.svc.Unlink(r.Context(), providerID, chi.URLParam(r, "identityId"), chi.URLParam(r, "agentId")) + if err != nil { + WriteError(w, err) + return + } + w.WriteHeader(http.StatusNoContent) +} + +// callerSubject extracts the authenticated principal, writing the +// 401-equivalent problem when absent. +func callerSubject(w http.ResponseWriter, r *http.Request) (string, bool) { + id, ok := auth.IdentityFromContext(r.Context()) + if !ok || id.Subject == "" { + WriteError(w, domain.NewUnauthorizedError("NO_IDENTITY", "authentication required")) + return "", false + } + return id.Subject, true +} + +func toChallengeResponse(res *service.IdentityChallengeResponse) identityChallengeResponse { + challenges := make([]identityChallengeDTO, 0, len(res.Challenges)) + for _, c := range res.Challenges { + challenges = append(challenges, identityChallengeDTO{Kid: c.Kid, SigningInput: c.SigningInput}) + } + return identityChallengeResponse{ + IdentityID: res.Identity.IdentityID, + Kind: string(res.Identity.Kind), + Value: res.Identity.EffectiveValue(), + Status: string(res.Identity.Status), + Nonce: res.Nonce, + ExpiresAt: res.ExpiresAt.UTC().Format(time.RFC3339), + Challenges: challenges, + } +} + +func toDetailResponse(identity *domain.VerifiedIdentity, links []*domain.IdentityLink) identityDetailResponse { + out := identityDetailResponse{ + IdentityID: identity.IdentityID, + Kind: string(identity.Kind), + Value: identity.Value, + Status: string(identity.Status), + ProofMethod: identity.ProofMethod, + PendingValue: identity.PendingValue, + CreatedAt: identity.CreatedAt.UTC().Format(time.RFC3339), + } + if !identity.VerifiedAt.IsZero() { + out.VerifiedAt = identity.VerifiedAt.UTC().Format(time.RFC3339) + } + for _, l := range links { + dto := linkedAgentDTO{AgentID: l.AgentID} + if !l.LinkedAt.IsZero() { + dto.LinkedAt = l.LinkedAt.UTC().Format(time.RFC3339) + } + out.LinkedAgents = append(out.LinkedAgents, dto) + } + return out +} diff --git a/internal/ra/handler/identity_handler_test.go b/internal/ra/handler/identity_handler_test.go new file mode 100644 index 0000000..e021ec5 --- /dev/null +++ b/internal/ra/handler/identity_handler_test.go @@ -0,0 +1,414 @@ +package handler_test + +// HTTP-level tests for the /v2/ans/identities surface: status codes, +// DTO shapes, auth extraction, and the agent-detail identities[] +// composition. The proof-gate logic itself is covered in depth by the +// service tests; these pin the wire contract. + +import ( + "bytes" + "context" + "crypto/ecdsa" + "crypto/elliptic" + "crypto/rand" + "crypto/sha256" + "encoding/base64" + "encoding/json" + "net/http" + "net/http/httptest" + "testing" + "time" + + "github.com/go-chi/chi/v5" + "github.com/rs/zerolog" + + "github.com/godaddy/ans/internal/adapter/auth" + "github.com/godaddy/ans/internal/adapter/cert" + "github.com/godaddy/ans/internal/adapter/didresolver" + "github.com/godaddy/ans/internal/adapter/eventbus" + "github.com/godaddy/ans/internal/adapter/store/sqlite" + anscrypto "github.com/godaddy/ans/internal/crypto" + "github.com/godaddy/ans/internal/port" + "github.com/godaddy/ans/internal/ra/handler" + ramiddleware "github.com/godaddy/ans/internal/ra/middleware" + "github.com/godaddy/ans/internal/ra/service" +) + +// identityHTTPFixture wires the identity routes (plus the agent +// register + detail routes the link tests need) over real SQLite and +// the noop resolver. No signer — covering the unsigned outbox branch; +// the signed path is pinned by the service tests. +type identityHTTPFixture struct { + router chi.Router +} + +func newIdentityHTTPFixture(t *testing.T) *identityHTTPFixture { + t.Helper() + dir := t.TempDir() + db, err := sqlite.Open(context.Background(), ":memory:") + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { _ = db.Close() }) + + agents := sqlite.NewAgentStore(db) + endpoints := sqlite.NewEndpointStore(db) + certsStore := sqlite.NewCertificateStore(db) + byoc := sqlite.NewByocCertificateStore(db) + renewals := sqlite.NewRenewalStore(db) + outbox := sqlite.NewOutboxStore(db) + + identityCA, err := cert.NewSelfCA(dir+"/ca", "Test CA", 365) + if err != nil { + t.Fatal(err) + } + serverCA, err := cert.NewServerSelfCA(dir+"/server-ca", "Test Server CA", 365) + if err != nil { + t.Fatal(err) + } + regSvc := service.NewRegistrationService( + agents, endpoints, certsStore, byoc, renewals, + cert.NewX509Validator(cert.WithSkipChainVerify()), + identityCA, eventbus.NewInMemoryBus(zerolog.Nop()), outbox, db, + ).WithServerCertificateAuthority(serverCA) + + idSvc := service.NewIdentityService( + sqlite.NewIdentityStore(db), + sqlite.NewIdentityLinkStore(db), + agents, + didresolver.NewNoopResolver(), + outbox, + db, + ).WithChallengeTTL(30 * time.Minute) + + r := chi.NewRouter() + regH := handler.NewRegistrationHandler(regSvc) + lifeH := handler.NewLifecycleHandler(regSvc).WithIdentityViews(idSvc) + readOwn := ramiddleware.ReadOwnership(agents) + r.Post("/v2/ans/agents", regH.Register) + r.With(readOwn).Get("/v2/ans/agents/{agentId}", lifeH.Detail) + + idH := handler.NewIdentityHandler(idSvc) + r.Post("/v2/ans/identities", idH.Register) + r.Get("/v2/ans/identities", idH.List) + r.Get("/v2/ans/identities/{identityId}", idH.Detail) + r.Put("/v2/ans/identities/{identityId}", idH.Rotate) + r.Post("/v2/ans/identities/{identityId}/verify-control", idH.VerifyControl) + r.Post("/v2/ans/identities/{identityId}/revoke", idH.Revoke) + r.Post("/v2/ans/identities/{identityId}/links", idH.Link) + r.Delete("/v2/ans/identities/{identityId}/links/{agentId}", idH.Unlink) + + return &identityHTTPFixture{router: r} +} + +// do sends a request as the given owner ("" = unauthenticated) and +// returns the recorder. +func (f *identityHTTPFixture) do(t *testing.T, owner, method, path string, body any) *httptest.ResponseRecorder { + t.Helper() + var reader *bytes.Reader + if body != nil { + raw, err := json.Marshal(body) + if err != nil { + t.Fatal(err) + } + reader = bytes.NewReader(raw) + } else { + reader = bytes.NewReader(nil) + } + req := httptest.NewRequest(method, path, reader) + req.Header.Set("Content-Type", "application/json") + if owner != "" { + req = req.WithContext(auth.WithIdentity(req.Context(), &port.Identity{Subject: owner})) + } + rec := httptest.NewRecorder() + f.router.ServeHTTP(rec, req) + return rec +} + +// doRaw sends a raw (possibly malformed) body. +func (f *identityHTTPFixture) doRaw(t *testing.T, owner, method, path, raw string) *httptest.ResponseRecorder { + t.Helper() + req := httptest.NewRequest(method, path, bytes.NewReader([]byte(raw))) + req.Header.Set("Content-Type", "application/json") + if owner != "" { + req = req.WithContext(auth.WithIdentity(req.Context(), &port.Identity{Subject: owner})) + } + rec := httptest.NewRecorder() + f.router.ServeHTTP(rec, req) + return rec +} + +type challengeBody struct { + IdentityID string `json:"identityId"` + Kind string `json:"kind"` + Value string `json:"value"` + Status string `json:"status"` + Nonce string `json:"nonce"` + ExpiresAt string `json:"expiresAt"` + Challenges []struct { + Kid string `json:"kid"` + SigningInput string `json:"signingInput"` + } `json:"challenges"` +} + +// signIdentityProof mints a compact JWS over the served signingInput +// with the kid + embedded jwk headers — the registrant-side signing. +func signIdentityProof(t *testing.T, priv *ecdsa.PrivateKey, kid, signingInput string) string { + t.Helper() + jwk, err := anscrypto.PublicKeyToJWK(&priv.PublicKey) + if err != nil { + t.Fatal(err) + } + headerJSON, err := json.Marshal(map[string]any{"alg": "ES256", "kid": kid, "jwk": jwk}) + if err != nil { + t.Fatal(err) + } + encodedHeader := base64.RawURLEncoding.EncodeToString(headerJSON) + toSign := encodedHeader + "." + signingInput + digest := sha256.Sum256([]byte(toSign)) + der, err := ecdsa.SignASN1(rand.Reader, priv, digest[:]) + if err != nil { + t.Fatal(err) + } + p1363, err := anscrypto.DERToP1363(der, 32) + if err != nil { + t.Fatal(err) + } + return toSign + "." + base64.RawURLEncoding.EncodeToString(p1363) +} + +// registerAndVerify drives an identity to VERIFIED over HTTP and +// returns its id and DID. +func (f *identityHTTPFixture) registerAndVerify(t *testing.T, owner, didValue string) string { + t.Helper() + rec := f.do(t, owner, http.MethodPost, "/v2/ans/identities", map[string]string{"value": didValue}) + if rec.Code != http.StatusAccepted { + t.Fatalf("register: %d %s", rec.Code, rec.Body) + } + var ch challengeBody + if err := json.Unmarshal(rec.Body.Bytes(), &ch); err != nil { + t.Fatal(err) + } + priv, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) + if err != nil { + t.Fatal(err) + } + proof := signIdentityProof(t, priv, didValue+"#key-1", ch.Challenges[0].SigningInput) + rec = f.do(t, owner, http.MethodPost, "/v2/ans/identities/"+ch.IdentityID+"/verify-control", + map[string]any{"signedProofs": []string{proof}}) + if rec.Code != http.StatusOK { + t.Fatalf("verify-control: %d %s", rec.Code, rec.Body) + } + return ch.IdentityID +} + +func TestIdentityHandler_RegisterShape(t *testing.T) { + t.Parallel() + f := newIdentityHTTPFixture(t) + + rec := f.do(t, "owner-1", http.MethodPost, "/v2/ans/identities", + map[string]string{"value": "did:web:identity.acme-corp.com"}) + if rec.Code != http.StatusAccepted { + t.Fatalf("register: %d %s", rec.Code, rec.Body) + } + var ch challengeBody + if err := json.Unmarshal(rec.Body.Bytes(), &ch); err != nil { + t.Fatal(err) + } + if ch.IdentityID == "" || ch.Kind != "did:web" || ch.Status != "PENDING_CONTROL" || + ch.Nonce == "" || ch.ExpiresAt == "" || len(ch.Challenges) != 1 || + ch.Challenges[0].SigningInput == "" { + t.Fatalf("202 shape wrong: %+v", ch) + } +} + +func TestIdentityHandler_AuthAndValidation(t *testing.T) { + t.Parallel() + f := newIdentityHTTPFixture(t) + + // Unauthenticated → 403-shaped problem from callerSubject. + if rec := f.do(t, "", http.MethodPost, "/v2/ans/identities", + map[string]string{"value": "did:web:a.com"}); rec.Code != http.StatusForbidden { + t.Fatalf("unauthenticated register: %d", rec.Code) + } + for _, route := range []struct{ method, path string }{ + {http.MethodGet, "/v2/ans/identities"}, + {http.MethodGet, "/v2/ans/identities/x"}, + {http.MethodPut, "/v2/ans/identities/x"}, + {http.MethodPost, "/v2/ans/identities/x/verify-control"}, + {http.MethodPost, "/v2/ans/identities/x/revoke"}, + {http.MethodPost, "/v2/ans/identities/x/links"}, + {http.MethodDelete, "/v2/ans/identities/x/links/y"}, + } { + if rec := f.do(t, "", route.method, route.path, nil); rec.Code != http.StatusForbidden { + t.Fatalf("unauthenticated %s %s: %d", route.method, route.path, rec.Code) + } + } + + // Malformed bodies → 422. + for _, route := range []struct{ method, path string }{ + {http.MethodPost, "/v2/ans/identities"}, + {http.MethodPut, "/v2/ans/identities/x"}, + {http.MethodPost, "/v2/ans/identities/x/verify-control"}, + {http.MethodPost, "/v2/ans/identities/x/links"}, + } { + if rec := f.doRaw(t, "owner-1", route.method, route.path, "{not json"); rec.Code != http.StatusUnprocessableEntity { + t.Fatalf("bad json %s %s: %d", route.method, route.path, rec.Code) + } + } + // Empty value → 422 on register and rotate. + if rec := f.do(t, "owner-1", http.MethodPost, "/v2/ans/identities", map[string]string{}); rec.Code != http.StatusUnprocessableEntity { + t.Fatalf("empty value register: %d", rec.Code) + } + if rec := f.do(t, "owner-1", http.MethodPut, "/v2/ans/identities/x", map[string]string{}); rec.Code != http.StatusUnprocessableEntity { + t.Fatalf("empty value rotate: %d", rec.Code) + } +} + +func TestIdentityHandler_FullLifecycleOverHTTP(t *testing.T) { + t.Parallel() + f := newIdentityHTTPFixture(t) + owner := "owner-1" + + identityID := f.registerAndVerify(t, owner, "did:web:identity.acme-corp.com") + + // List. + rec := f.do(t, owner, http.MethodGet, "/v2/ans/identities", nil) + if rec.Code != http.StatusOK { + t.Fatalf("list: %d", rec.Code) + } + var list struct { + Identities []struct { + IdentityID string `json:"identityId"` + Status string `json:"status"` + } `json:"identities"` + } + if err := json.Unmarshal(rec.Body.Bytes(), &list); err != nil { + t.Fatal(err) + } + if len(list.Identities) != 1 || list.Identities[0].Status != "VERIFIED" { + t.Fatalf("list shape: %+v", list) + } + + // Register the agent to link. + agentID := registerAgentForIdentity(t, f, owner, "linked.example.com") + + // Link → 200 {linked:1}. + rec = f.do(t, owner, http.MethodPost, "/v2/ans/identities/"+identityID+"/links", + map[string]any{"agentIds": []string{agentID}}) + if rec.Code != http.StatusOK { + t.Fatalf("link: %d %s", rec.Code, rec.Body) + } + var linked struct { + Linked int `json:"linked"` + } + if err := json.Unmarshal(rec.Body.Bytes(), &linked); err != nil || linked.Linked != 1 { + t.Fatalf("link response: %s (%v)", rec.Body, err) + } + + // Identity detail carries the live link. + rec = f.do(t, owner, http.MethodGet, "/v2/ans/identities/"+identityID, nil) + if rec.Code != http.StatusOK { + t.Fatalf("detail: %d", rec.Code) + } + var detail struct { + Status string `json:"status"` + VerifiedAt string `json:"verifiedAt"` + LinkedAgents []struct { + AgentID string `json:"agentId"` + } `json:"linkedAgents"` + } + if err := json.Unmarshal(rec.Body.Bytes(), &detail); err != nil { + t.Fatal(err) + } + if detail.Status != "VERIFIED" || detail.VerifiedAt == "" || + len(detail.LinkedAgents) != 1 || detail.LinkedAgents[0].AgentID != agentID { + t.Fatalf("detail shape: %+v", detail) + } + + // Agent detail carries the computed identities[] (the RA-side + // §5.4 join through WithIdentityViews). + rec = f.do(t, owner, http.MethodGet, "/v2/ans/agents/"+agentID, nil) + if rec.Code != http.StatusOK { + t.Fatalf("agent detail: %d", rec.Code) + } + var agentDetail struct { + Identities []struct { + IdentityID string `json:"identityId"` + IdentityStatus string `json:"identityStatus"` + LinkedAt string `json:"linkedAt"` + } `json:"identities"` + } + if err := json.Unmarshal(rec.Body.Bytes(), &agentDetail); err != nil { + t.Fatal(err) + } + if len(agentDetail.Identities) != 1 || agentDetail.Identities[0].IdentityID != identityID || + agentDetail.Identities[0].IdentityStatus != "VERIFIED" || agentDetail.Identities[0].LinkedAt == "" { + t.Fatalf("agent identities[]: %+v", agentDetail) + } + + // Rotate → 202 with fresh challenges over the staged value. + rec = f.do(t, owner, http.MethodPut, "/v2/ans/identities/"+identityID, + map[string]string{"value": "did:web:identity.acme-corp.com"}) + if rec.Code != http.StatusAccepted { + t.Fatalf("rotate: %d %s", rec.Code, rec.Body) + } + + // Unlink → 204. + rec = f.do(t, owner, http.MethodDelete, "/v2/ans/identities/"+identityID+"/links/"+agentID, nil) + if rec.Code != http.StatusNoContent { + t.Fatalf("unlink: %d %s", rec.Code, rec.Body) + } + // Unlinking again → 404. + rec = f.do(t, owner, http.MethodDelete, "/v2/ans/identities/"+identityID+"/links/"+agentID, nil) + if rec.Code != http.StatusNotFound { + t.Fatalf("double unlink: %d", rec.Code) + } + + // Revoke → 200, REVOKED echoed. + rec = f.do(t, owner, http.MethodPost, "/v2/ans/identities/"+identityID+"/revoke", nil) + if rec.Code != http.StatusOK { + t.Fatalf("revoke: %d %s", rec.Code, rec.Body) + } + if err := json.Unmarshal(rec.Body.Bytes(), &detail); err != nil || detail.Status != "REVOKED" { + t.Fatalf("revoke response: %s (%v)", rec.Body, err) + } + + // Cross-owner: read hides (404), write rejects (403). + other := f.registerAndVerify(t, "owner-2", "did:web:other.example.com") + if rec := f.do(t, owner, http.MethodGet, "/v2/ans/identities/"+other, nil); rec.Code != http.StatusNotFound { + t.Fatalf("cross-owner detail: %d", rec.Code) + } + if rec := f.do(t, owner, http.MethodPost, "/v2/ans/identities/"+other+"/revoke", nil); rec.Code != http.StatusForbidden { + t.Fatalf("cross-owner revoke: %d", rec.Code) + } +} + +// registerAgentForIdentity registers an agent over HTTP for link +// tests, reusing the package CSR helpers. +func registerAgentForIdentity(t *testing.T, f *identityHTTPFixture, owner, host string) string { + t.Helper() + body := map[string]any{ + "agentDisplayName": "Test", + "version": "1.0.0", + "agentHost": host, + "endpoints": []map[string]any{{ + "agentUrl": "https://" + host + "/mcp", + "protocol": "MCP", + "transports": []string{"SSE"}, + }}, + "identityCsrPEM": newTestCSR(t, "ans://v1.0.0."+host), + "serverCsrPEM": newTestServerCSR(t, host), + } + rec := f.do(t, owner, http.MethodPost, "/v2/ans/agents", body) + if rec.Code != http.StatusAccepted { + t.Fatalf("agent register: %d %s", rec.Code, rec.Body) + } + var resp struct { + AgentID string `json:"agentId"` + } + if err := json.Unmarshal(rec.Body.Bytes(), &resp); err != nil || resp.AgentID == "" { + t.Fatalf("agent register response: %s (%v)", rec.Body, err) + } + return resp.AgentID +} diff --git a/internal/ra/handler/lifecycle.go b/internal/ra/handler/lifecycle.go index 26db754..f4eedd5 100644 --- a/internal/ra/handler/lifecycle.go +++ b/internal/ra/handler/lifecycle.go @@ -18,6 +18,11 @@ import ( // routes assume the ownership middleware has already run. type LifecycleHandler struct { svc *service.RegistrationService + // identities, when set, decorates agent detail responses with the + // computed identities[] view (design §5.4) — the verified + // identities currently linked to the agent. Optional so the + // agent surface has zero hard dependency on the identity feature. + identities *service.IdentityService } // NewLifecycleHandler constructs a LifecycleHandler. @@ -25,6 +30,13 @@ func NewLifecycleHandler(svc *service.RegistrationService) *LifecycleHandler { return &LifecycleHandler{svc: svc} } +// WithIdentityViews attaches the identity service used to compute the +// additive identities[] field on agent detail responses. +func (h *LifecycleHandler) WithIdentityViews(identities *service.IdentityService) *LifecycleHandler { + h.identities = identities + return h +} + // ----- GET /v2/ans/agents ----- // List handles GET /v2/ans/agents. Ownership-scoped to the caller; @@ -91,7 +103,16 @@ func (h *LifecycleHandler) Detail(w http.ResponseWriter, r *http.Request) { WriteError(w, err) return } - WriteJSON(w, http.StatusOK, mapAgentDetails(res, r, h.svc.TLPublicBaseURL())) + details := mapAgentDetails(res, r, h.svc.TLPublicBaseURL()) + if h.identities != nil { + linked, lerr := h.identities.LinkedIdentitiesForAgent(r.Context(), agentID) + if lerr != nil { + WriteError(w, lerr) + return + } + details.Identities = mapLinkedIdentities(linked) + } + WriteJSON(w, http.StatusOK, details) } // ----- GET /v2/ans/agents/{agentId}/certificates/identity ----- diff --git a/internal/ra/service/identity.go b/internal/ra/service/identity.go new file mode 100644 index 0000000..e9ec09f --- /dev/null +++ b/internal/ra/service/identity.go @@ -0,0 +1,673 @@ +package service + +import ( + "context" + "crypto/rand" + "encoding/base64" + "errors" + "fmt" + "time" + + "github.com/google/uuid" + + anscrypto "github.com/godaddy/ans/internal/crypto" + "github.com/godaddy/ans/internal/domain" + "github.com/godaddy/ans/internal/port" + identityevent "github.com/godaddy/ans/internal/tl/event/identity" +) + +// identityLane is the outbox schema_version value routing identity +// events to the TL's `POST /v1/internal/identities/event` ingest +// lane. Same producer signature and replay-verbatim invariant as the +// V1/V2 agent lanes; different inner-event schema. +const identityLane = "IDENTITY" + +// maxProofsPerVerify bounds the multi-key proof set on one +// verify-control call. did:web legitimately proves several +// assertionMethod keys; sixteen is far beyond any real document while +// keeping the request body and the sealed event bounded. +const maxProofsPerVerify = 16 + +// maxLinkBatch bounds one link call. The design allows the RA to +// chunk very large fleets to bound leaf size; rather than silently +// chunking, v1 rejects oversized batches and lets the caller chunk — +// every accepted call still seals exactly one event. +const maxLinkBatch = 256 + +// ProofChallenge is one entry of the 202 challenge list: a key the +// registrant may prove, plus the exact base64url signing input a +// compact JWS over it must carry as its payload segment. Every entry +// of one round shares the same nonce and the same signing input — +// the input is key-independent. +// +// Kid is empty when the resolver could not enumerate keys (the noop +// resolver before any proofs exist): the registrant then names keys +// via the JWS `kid` header at verify time. +type ProofChallenge struct { + Kid string + SigningInput string +} + +// IdentityChallengeResponse is returned by Register and Rotate — the +// service half of the 202 body. +type IdentityChallengeResponse struct { + Identity *domain.VerifiedIdentity + Nonce string + ExpiresAt time.Time + Challenges []ProofChallenge +} + +// IdentityService owns the Verified Identity lifecycle: register → +// verify-control → (rotate | revoke), plus the owner-gated link +// surface. One service, one aggregate; the only per-kind branching is +// the control-proof dispatch in verify-control (and the advisory +// resolution at register), per the design's minimal-abstraction rule. +type IdentityService struct { + identities port.IdentityStore + links port.IdentityLinkStore + agents port.AgentStore + outbox OutboxEnqueuer + uow port.UnitOfWork + signer *EventSigner + + // verifiers is the per-kind control-proof registry — THE + // extension seam (see identitykinds.go). A kind is enabled iff + // it has an entry here. + verifiers map[domain.IdentifierKind]controlVerifier + + challengeTTL time.Duration + limiter *ownerLimiter + clock func() time.Time + newID func() (string, error) + newNonce func() (string, error) +} + +// NewIdentityService constructs an IdentityService. +func NewIdentityService( + identities port.IdentityStore, + links port.IdentityLinkStore, + agents port.AgentStore, + resolver port.DIDResolver, + outbox OutboxEnqueuer, + uow port.UnitOfWork, +) *IdentityService { + return &IdentityService{ + identities: identities, + links: links, + agents: agents, + verifiers: newControlVerifiers(resolver), + outbox: outbox, + uow: uow, + challengeTTL: time.Hour, + limiter: newOwnerLimiter(defaultRegisterPerMinute), + clock: time.Now, + newID: func() (string, error) { + id, err := uuid.NewV7() + if err != nil { + return "", err + } + return id.String(), nil + }, + newNonce: func() (string, error) { + var b [32]byte + if _, err := rand.Read(b[:]); err != nil { + return "", err + } + return base64.RawURLEncoding.EncodeToString(b[:]), nil + }, + } +} + +// WithSigner attaches the producer event signer (same KeyManager + +// keyID + raID tuple the registration service signs with — one RA, +// one producer identity). +func (s *IdentityService) WithSigner(sig EventSigner) *IdentityService { + s.signer = &sig + return s +} + +// WithChallengeTTL overrides the nonce TTL (default 1h; the design +// floor for high-assurance deployments is 5m). +func (s *IdentityService) WithChallengeTTL(ttl time.Duration) *IdentityService { + if ttl > 0 { + s.challengeTTL = ttl + } + return s +} + +// WithRegisterRateLimit overrides the per-owner register/rotate rate +// limit (default 10/min). Register and rotate trigger an outbound +// fetch for did:web before any proof exists, so they are rate-limited +// per authenticated owner (design §3.7). +func (s *IdentityService) WithRegisterRateLimit(perMinute int) *IdentityService { + if perMinute > 0 { + s.limiter = newOwnerLimiter(perMinute) + } + return s +} + +// WithClock overrides the time source (tests only). +func (s *IdentityService) WithClock(fn func() time.Time) *IdentityService { + s.clock = fn + return s +} + +// raID returns the configured RA identifier stamped into proof inputs +// and sealed events. Empty without a signer (tests). +func (s *IdentityService) raID() string { + if s.signer != nil { + return s.signer.RaID + } + return "" +} + +// verifierFor returns the kind's control verifier, or the precise +// IDENTIFIER_KIND_UNSUPPORTED error when this deployment has none — +// lei is recognized lexically but postponed: the route exists, the +// kind does not until its verifier registers (identitykinds.go). +func (s *IdentityService) verifierFor(kind domain.IdentifierKind) (controlVerifier, error) { + v, ok := s.verifiers[kind] + if !ok { + return nil, domain.NewValidationError("IDENTIFIER_KIND_UNSUPPORTED", + fmt.Sprintf("identifier kind %q is not enabled on this deployment", kind)) + } + return v, nil +} + +// Register creates (or idempotently re-challenges) an identity for +// the authenticated owner and returns the challenge list to sign. +// +// Idempotent re-add (§4.2): while the owner's row for the same +// canonical value is PENDING_CONTROL, re-registering returns the +// SAME identityId with a fresh nonce (the prior nonce is +// superseded). IDENTIFIER_DUPLICATE is reserved for genuine +// conflicts: already VERIFIED by this owner (rotate instead), or +// proven by another owner. +func (s *IdentityService) Register(ctx context.Context, providerID, rawValue string) (*IdentityChallengeResponse, error) { + if providerID == "" { + return nil, domain.NewValidationError("INVALID_PROVIDER_ID", "authenticated owner is required") + } + if !s.limiter.Allow(providerID, s.clock()) { + return nil, domain.NewValidationError("RATE_LIMITED", + "too many identity register/rotate calls; retry later") + } + kind, canonical, err := domain.InferIdentifierKind(rawValue) + if err != nil { + return nil, err + } + if _, err := s.verifierFor(kind); err != nil { + return nil, err + } + + now := s.clock().UTC() + + // Existing live row for this owner? + existing, err := s.identities.FindLive(ctx, providerID, kind, canonical) + switch { + case err == nil && existing.Status == domain.IdentityVerified: + return nil, domain.NewConflictError("IDENTIFIER_DUPLICATE", + "identifier is already verified by this owner; rotate it with PUT instead") + case err == nil: + // PENDING_CONTROL → idempotent re-challenge on the same row. + return s.challenge(ctx, existing, now) + case errors.Is(err, domain.ErrNotFound): + // fall through to creation + default: + return nil, err + } + + // Early duplicate feedback when another owner already proved the + // value. The authoritative guard is the proven-uniqueness index + // at verify time; this just makes the failure arrive sooner. + if taken, terr := s.identities.ExistsVerified(ctx, kind, canonical); terr != nil { + return nil, terr + } else if taken { + return nil, domain.NewConflictError("IDENTIFIER_DUPLICATE", + "identifier is already verified by another owner") + } + + identityID, err := s.newID() + if err != nil { + return nil, domain.NewInternalError("IDENTITY_ID_GENERATION", "could not generate identityId", err) + } + identity, err := domain.NewVerifiedIdentity(identityID, providerID, canonical, now) + if err != nil { + return nil, err + } + return s.challenge(ctx, identity, now) +} + +// Rotate stages a same-kind replacement (§4.2 PUT) and returns fresh +// challenges over it. The previously sealed state stands until the +// new proof lands; a replacement that never verifies expires with its +// nonce. +func (s *IdentityService) Rotate(ctx context.Context, providerID, identityID, rawValue string) (*IdentityChallengeResponse, error) { + if !s.limiter.Allow(providerID, s.clock()) { + return nil, domain.NewValidationError("RATE_LIMITED", + "too many identity register/rotate calls; retry later") + } + identity, err := s.ownedIdentityForWrite(ctx, providerID, identityID) + if err != nil { + return nil, err + } + now := s.clock().UTC() + if err := identity.StageRotation(rawValue, now); err != nil { + return nil, err + } + return s.challenge(ctx, identity, now) +} + +// challenge mints a fresh nonce on the identity, runs the kind's +// advisory resolution to seed the per-key challenge list, persists, +// and assembles the 202 response. Shared by Register and Rotate. +func (s *IdentityService) challenge(ctx context.Context, identity *domain.VerifiedIdentity, now time.Time) (*IdentityChallengeResponse, error) { + nonce, err := s.newNonce() + if err != nil { + return nil, domain.NewInternalError("CHALLENGE_GENERATION", "could not generate challenge nonce", err) + } + if err := identity.IssueChallenge(nonce, s.challengeTTL, now); err != nil { + return nil, err + } + + input := anscrypto.IdentityProofInput{ + Identifier: identity.EffectiveValue(), + IdentityID: identity.IdentityID, + Nonce: nonce, + Purpose: anscrypto.IdentityProofPurpose, + RaID: s.raID(), + Scheme: string(identity.Kind), + } + signingInput, err := input.SigningInput() + if err != nil { + return nil, domain.NewInternalError("CHALLENGE_GENERATION", "could not build signing input", err) + } + + verifier, err := s.verifierFor(identity.Kind) + if err != nil { + return nil, err + } + challenges, err := verifier.Challenges(ctx, identity, signingInput) + if err != nil { + return nil, err + } + + if err := s.identities.Save(ctx, identity); err != nil { + return nil, mapIdentitySaveErr(err) + } + return &IdentityChallengeResponse{ + Identity: identity, + Nonce: nonce, + ExpiresAt: identity.Challenge.ExpiresAt, + Challenges: challenges, + }, nil +} + +// VerifyControl runs the identity's per-kind control proof over the +// submission and, when every proof passes, flips the identity to +// VERIFIED (or completes a staged rotation), consumes the nonce, and +// seals IDENTITY_VERIFIED / IDENTITY_UPDATED on the identity's TL +// stream — all in one transaction. One bad proof fails the call +// closed; a failed attempt does NOT consume the nonce. The per-kind +// logic lives entirely behind the controlVerifier seam +// (identitykinds.go); this method owns the kind-agnostic discipline. +func (s *IdentityService) VerifyControl(ctx context.Context, providerID, identityID string, sub ProofSubmission) (*domain.VerifiedIdentity, error) { + identity, err := s.ownedIdentityForWrite(ctx, providerID, identityID) + if err != nil { + return nil, err + } + now := s.clock().UTC() + if identity.Status == domain.IdentityRevoked { + return nil, domain.NewInvalidStateError("IDENTITY_REVOKED", "identity is revoked") + } + if err := identity.CheckChallenge(now); err != nil { + return nil, err + } + verifier, err := s.verifierFor(identity.Kind) + if err != nil { + return nil, err + } + + expectedInput := anscrypto.IdentityProofInput{ + Identifier: identity.EffectiveValue(), + IdentityID: identity.IdentityID, + Nonce: identity.Challenge.Nonce, + Purpose: anscrypto.IdentityProofPurpose, + RaID: s.raID(), + Scheme: string(identity.Kind), + } + expectedPayload, err := expectedInput.SigningInput() + if err != nil { + return nil, domain.NewInternalError("PROOF_INPUT", "could not rebuild signing input", err) + } + + provenKeys, err := verifier.VerifyProofs(ctx, identity, sub, expectedPayload) + if err != nil { + return nil, err + } + + statusBefore := identity.Status + var sealed *domain.VerifiedIdentity + err = s.uow.Run(ctx, func(txCtx context.Context) error { + // Consume the nonce first — the conditional update is the + // TOCTOU guard; exactly one concurrent verify can win. + if err := s.identities.ConsumeChallenge(txCtx, identity.IdentityID, identity.Challenge.Nonce, now); err != nil { + return err + } + previousValue, err := identity.CompleteVerification(now) + if err != nil { + return err + } + consumed := now + identity.Challenge.ConsumedAt = &consumed + if err := s.identities.Save(txCtx, identity); err != nil { + return mapIdentitySaveErr(err) + } + + eventType := identityevent.TypeIdentityVerified + if statusBefore == domain.IdentityVerified { + eventType = identityevent.TypeIdentityUpdated + } + inner := s.buildIdentityEvent(identity, eventType, now) + inner.Keys = provenKeys + inner.PreviousValue = previousValue + inner.VerifiedAt = now.Format(time.RFC3339) + return s.enqueueIdentityEvent(txCtx, inner, now) + }) + if err != nil { + return nil, err + } + sealed = identity + return sealed, nil +} + +// Revoke transitions a VERIFIED identity to REVOKED and seals +// IDENTITY_REVOKED — one event; propagation to every linked agent's +// badge is the TL's read-time join, never a write fan-out. +func (s *IdentityService) Revoke(ctx context.Context, providerID, identityID string) (*domain.VerifiedIdentity, error) { + identity, err := s.ownedIdentityForWrite(ctx, providerID, identityID) + if err != nil { + return nil, err + } + now := s.clock().UTC() + if err := identity.Revoke(now); err != nil { + return nil, err + } + err = s.uow.Run(ctx, func(txCtx context.Context) error { + if err := s.identities.Save(txCtx, identity); err != nil { + return mapIdentitySaveErr(err) + } + inner := s.buildIdentityEvent(identity, identityevent.TypeIdentityRevoked, now) + inner.RevokedAt = now.Format(time.RFC3339) + return s.enqueueIdentityEvent(txCtx, inner, now) + }) + if err != nil { + return nil, err + } + return identity, nil +} + +// List returns the owner's identities, newest first. +func (s *IdentityService) List(ctx context.Context, providerID string) ([]*domain.VerifiedIdentity, error) { + return s.identities.ListByOwner(ctx, providerID) +} + +// LinkedIdentitySummary is one entry of the RA-side computed +// identities[] view on AgentDetails (design §5.4): additive, +// optional, computed — never stored on the registration. The +// authoritative third-party view is the TL badge join; this is the +// owner's convenience mirror. +type LinkedIdentitySummary struct { + IdentityID string + Kind domain.IdentifierKind + Value string + IdentityStatus domain.IdentityStatus + LinkedAt time.Time +} + +// LinkedIdentitiesForAgent computes the identities currently linked +// to an agent. Callers reach this through the ownership-gated agent +// detail route; links are same-owner by construction, so no further +// gate applies here. +func (s *IdentityService) LinkedIdentitiesForAgent(ctx context.Context, agentID string) ([]LinkedIdentitySummary, error) { + links, err := s.links.ListLiveByAgent(ctx, agentID) + if err != nil { + return nil, err + } + out := make([]LinkedIdentitySummary, 0, len(links)) + for _, l := range links { + identity, err := s.identities.FindByID(ctx, l.IdentityID) + if err != nil { + return nil, err + } + out = append(out, LinkedIdentitySummary{ + IdentityID: identity.IdentityID, + Kind: identity.Kind, + Value: identity.Value, + IdentityStatus: identity.Status, + LinkedAt: l.LinkedAt, + }) + } + return out, nil +} + +// Detail returns one identity plus its live links. +func (s *IdentityService) Detail(ctx context.Context, providerID, identityID string) (*domain.VerifiedIdentity, []*domain.IdentityLink, error) { + identity, err := s.ownedIdentity(ctx, providerID, identityID) + if err != nil { + return nil, nil, err + } + links, err := s.links.ListLiveByIdentity(ctx, identityID) + if err != nil { + return nil, nil, err + } + return identity, links, nil +} + +// Link binds a batch of the owner's agents to the identity — a +// single owner-gated call, no challenge, no signature (§4.3): the +// caller must own the identity AND every named agent; key possession +// never authorizes a link. The whole batch seals as ONE +// IDENTITY_LINKED event on the identity stream; agent streams are +// never written. Already-linked agents are skipped idempotently; a +// call that links nothing new seals nothing. +func (s *IdentityService) Link(ctx context.Context, providerID, identityID string, agentIDs []string) (int, error) { + identity, err := s.ownedIdentityForWrite(ctx, providerID, identityID) + if err != nil { + return 0, err + } + if identity.Status != domain.IdentityVerified { + return 0, domain.NewInvalidStateError("IDENTITY_NOT_VERIFIED", + "links attach only while the identity is VERIFIED") + } + if len(agentIDs) == 0 { + return 0, domain.NewValidationError("INVALID_LINK_REQUEST", "agentIds is required") + } + if len(agentIDs) > maxLinkBatch { + return 0, domain.NewValidationError("INVALID_LINK_REQUEST", + fmt.Sprintf("at most %d agents per link call; chunk larger fleets", maxLinkBatch)) + } + seen := make(map[string]bool, len(agentIDs)) + deduped := make([]string, 0, len(agentIDs)) + for _, id := range agentIDs { + if id == "" { + return 0, domain.NewValidationError("INVALID_LINK_REQUEST", "agentIds contains an empty id") + } + if !seen[id] { + seen[id] = true + deduped = append(deduped, id) + } + } + + // Owner gate, both sides: every agent must exist and belong to + // the caller. A non-owned agent is reported as not-found — the + // caller learns nothing about other owners' agents. + for _, agentID := range deduped { + reg, err := s.agents.FindByAgentID(ctx, agentID) + if err != nil { + return 0, domain.NewNotFoundError("AGENT_NOT_FOUND", + fmt.Sprintf("agent %q not found", agentID)) + } + if reg.OwnerID != providerID { + return 0, domain.NewNotFoundError("AGENT_NOT_FOUND", + fmt.Sprintf("agent %q not found", agentID)) + } + } + + now := s.clock().UTC() + linked := 0 + err = s.uow.Run(ctx, func(txCtx context.Context) error { + newlyLinked := make([]string, 0, len(deduped)) + for _, agentID := range deduped { + created, err := s.links.Link(txCtx, identityID, agentID, now) + if err != nil { + return err + } + if created { + newlyLinked = append(newlyLinked, agentID) + } + } + linked = len(newlyLinked) + if linked == 0 { + return nil // fully idempotent — nothing to seal + } + inner := s.buildIdentityEvent(identity, identityevent.TypeIdentityLinked, now) + inner.AnsIDs = newlyLinked + return s.enqueueIdentityEvent(txCtx, inner, now) + }) + if err != nil { + return 0, err + } + return linked, nil +} + +// Unlink ends one association and seals IDENTITY_UNLINKED on the +// identity stream. The association's history persists in the log. +func (s *IdentityService) Unlink(ctx context.Context, providerID, identityID, agentID string) error { + identity, err := s.ownedIdentityForWrite(ctx, providerID, identityID) + if err != nil { + return err + } + now := s.clock().UTC() + return s.uow.Run(ctx, func(txCtx context.Context) error { + if err := s.links.Unlink(txCtx, identityID, agentID, now); err != nil { + return err + } + inner := s.buildIdentityEvent(identity, identityevent.TypeIdentityUnlinked, now) + inner.AnsIDs = []string{agentID} + return s.enqueueIdentityEvent(txCtx, inner, now) + }) +} + +// ownedIdentity loads an identity and enforces the owner gate with +// read semantics: a non-owner gets not-found — existence is hidden, +// matching the agent ReadOwnership middleware. No admin override: +// the owner gate is the link mechanism's security boundary (§4.3 — +// the caller MUST be the authenticated owner), so identities are +// stricter than the agent routes here. +func (s *IdentityService) ownedIdentity(ctx context.Context, providerID, identityID string) (*domain.VerifiedIdentity, error) { + if identityID == "" { + return nil, domain.NewValidationError("INVALID_IDENTITY_ID", "identityId is required") + } + identity, err := s.identities.FindByID(ctx, identityID) + if err != nil { + return nil, err + } + if identity.ProviderID != providerID { + return nil, domain.NewNotFoundError("IDENTITY_NOT_FOUND", + fmt.Sprintf("identity %q not found", identityID)) + } + return identity, nil +} + +// ownedIdentityForWrite is the write-path owner gate: missing → 404, +// present-but-not-owned → 403 (the agent WriteOwnership split — a +// 404-for-not-owned would hide a real authorization failure from +// operators investigating permissions). +func (s *IdentityService) ownedIdentityForWrite(ctx context.Context, providerID, identityID string) (*domain.VerifiedIdentity, error) { + if identityID == "" { + return nil, domain.NewValidationError("INVALID_IDENTITY_ID", "identityId is required") + } + identity, err := s.identities.FindByID(ctx, identityID) + if err != nil { + return nil, err + } + if identity.ProviderID != providerID { + return nil, domain.NewUnauthorizedError("IDENTITY_NOT_OWNED", + "caller does not own this identity") + } + return identity, nil +} + +// buildIdentityEvent assembles the common fields of an identity +// event. Type-specific fields (keys, ansIds, previousValue, +// verifiedAt, revokedAt) are set by the caller. +func (s *IdentityService) buildIdentityEvent( + identity *domain.VerifiedIdentity, + eventType identityevent.Type, + now time.Time, +) *identityevent.Event { + return &identityevent.Event{ + EventType: eventType, + IdentityID: identity.IdentityID, + Kind: string(identity.Kind), + Value: identity.Value, + ProviderID: identity.ProviderID, + ProofMethod: identity.ProofMethod, + RaID: s.raID(), + Timestamp: now.Format(time.RFC3339), + } +} + +// enqueueIdentityEvent JCS-canonicalizes the inner event, signs it +// once with the producer key, and writes the outbox row on the +// IDENTITY lane. Same replay-verbatim invariant as the agent lanes: +// the worker must POST these exact bytes on every retry. +func (s *IdentityService) enqueueIdentityEvent(ctx context.Context, inner *identityevent.Event, now time.Time) error { + if s.outbox == nil { + return nil + } + innerCanonical, err := identityevent.CanonicalizeEvent(inner) + if err != nil { + return fmt.Errorf("canonicalize identity event: %w", err) + } + var producerSig string + if s.signer != nil { + producerSig, err = anscrypto.SignDetachedJWS( + ctx, s.signer.KeyManager, s.signer.KeyID, + anscrypto.JWSProtectedHeader{ + Typ: "JWT", + Timestamp: now.Unix(), + RAID: s.signer.RaID, + }, + innerCanonical, + ) + if err != nil { + return fmt.Errorf("sign identity event: %w", err) + } + } + payload, err := marshalOutboxPayload(innerCanonical, producerSig) + if err != nil { + return err + } + // The outbox row's subject column carries the identityId — the + // stream key for identity events, exactly as agent rows carry + // the agentId. + if _, err := s.outbox.Enqueue(ctx, string(inner.EventType), inner.IdentityID, identityLane, payload, now); err != nil { + return err + } + return nil +} + +// mapIdentitySaveErr converts the storage layer's generic conflict +// (one of the two partial unique indexes fired) into the wire code +// the design names. +func mapIdentitySaveErr(err error) error { + if err == nil { + return nil + } + if errors.Is(err, domain.ErrConflict) { + return domain.NewConflictError("IDENTIFIER_DUPLICATE", + "identifier is already registered or verified for this scope") + } + return err +} diff --git a/internal/ra/service/identity_test.go b/internal/ra/service/identity_test.go new file mode 100644 index 0000000..c9e2ebd --- /dev/null +++ b/internal/ra/service/identity_test.go @@ -0,0 +1,1036 @@ +package service_test + +// IdentityService tests: the proof gate (payload equality, kid +// selection, signature verification, nonce discipline), the lifecycle +// (register → verify → rotate → revoke), the owner-gated links, and +// the sealed-event emission on the outbox IDENTITY lane. Real SQLite +// stores + real crypto; the resolver is the noop adapter (hint +// synthesis) or a canned-document fake for the did:web rules. + +import ( + "context" + "crypto/ecdsa" + "crypto/ed25519" + "crypto/elliptic" + "crypto/rand" + "crypto/sha256" + "encoding/base64" + "encoding/json" + "errors" + "strings" + "testing" + "time" + + "github.com/godaddy/ans/internal/adapter/didresolver" + "github.com/godaddy/ans/internal/adapter/keymanager" + "github.com/godaddy/ans/internal/adapter/store/sqlite" + anscrypto "github.com/godaddy/ans/internal/crypto" + "github.com/godaddy/ans/internal/domain" + "github.com/godaddy/ans/internal/port" + "github.com/godaddy/ans/internal/ra/service" + identityevent "github.com/godaddy/ans/internal/tl/event/identity" +) + +// fakeResolver returns a canned document — the did:web rule tests +// (kid membership, controller checks, multibase keys) drive it. +type fakeResolver struct { + doc *port.DIDDocument + err error +} + +func (f *fakeResolver) Resolve(context.Context, string, []port.KeyHint) (*port.DIDDocument, error) { + return f.doc, f.err +} + +type identityFixture struct { + svc *service.IdentityService + db *sqlite.DB + outbox *sqlite.OutboxStore + agents port.AgentStore + signerPub any + clock *fakeClock + providerID string +} + +type fakeClock struct{ now time.Time } + +func (c *fakeClock) Now() time.Time { return c.now } + +// newIdentityFixture wires the service against real SQLite + the +// given resolver (nil → noop). +func newIdentityFixture(t *testing.T, resolver port.DIDResolver) *identityFixture { + t.Helper() + db, err := sqlite.Open(context.Background(), ":memory:") + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { _ = db.Close() }) + + km, err := keymanager.NewFileKeyManager(t.TempDir() + "/keys") + if err != nil { + t.Fatal(err) + } + if _, err := km.EnsureKey(context.Background(), "ra-signer", port.AlgorithmECDSAP256); err != nil { + t.Fatal(err) + } + pub, err := km.GetPublicKey(context.Background(), "ra-signer") + if err != nil { + t.Fatal(err) + } + + if resolver == nil { + resolver = didresolver.NewNoopResolver() + } + clock := &fakeClock{now: time.Date(2026, 6, 10, 15, 0, 0, 0, time.UTC)} + svc := service.NewIdentityService( + sqlite.NewIdentityStore(db), + sqlite.NewIdentityLinkStore(db), + sqlite.NewAgentStore(db), + resolver, + sqlite.NewOutboxStore(db), + db, + ).WithSigner(service.EventSigner{ + KeyManager: km, + KeyID: "ra-signer", + RaID: "ra-test", + }).WithClock(clock.Now) + + return &identityFixture{ + svc: svc, + db: db, + outbox: sqlite.NewOutboxStore(db), + agents: sqlite.NewAgentStore(db), + signerPub: pub, + clock: clock, + providerID: "owner-1", + } +} + +// saveAgent persists a minimal ACTIVE agent owned by `owner`. +func (fx *identityFixture) saveAgent(t *testing.T, agentID, owner, host string) { + t.Helper() + v, err := domain.NewSemVer(1, 0, 0) + if err != nil { + t.Fatal(err) + } + ansName, err := domain.NewAnsName(v, host) + if err != nil { + t.Fatal(err) + } + reg := &domain.AgentRegistration{ + AgentID: agentID, + OwnerID: owner, + AnsName: ansName, + Status: domain.StatusActive, + Details: domain.RegistrationDetails{ + RegistrationTimestamp: fx.clock.now, + DisplayName: "agent " + agentID, + }, + } + if err := fx.agents.Save(context.Background(), reg); err != nil { + t.Fatal(err) + } +} + +// signProof builds a standard compact JWS over the served +// signingInput: ES256, P1363 signature, kid + optional embedded jwk +// in the protected header — exactly what a registrant's tooling +// produces. +func signProof(t *testing.T, priv *ecdsa.PrivateKey, kid, signingInput string, embedJWK bool) string { + t.Helper() + header := map[string]any{"alg": "ES256", "kid": kid} + if embedJWK { + jwk, err := anscrypto.PublicKeyToJWK(&priv.PublicKey) + if err != nil { + t.Fatal(err) + } + header["jwk"] = jwk + } + headerJSON, err := json.Marshal(header) + if err != nil { + t.Fatal(err) + } + encodedHeader := base64.RawURLEncoding.EncodeToString(headerJSON) + toSign := encodedHeader + "." + signingInput + digest := sha256.Sum256([]byte(toSign)) + der, err := ecdsa.SignASN1(rand.Reader, priv, digest[:]) + if err != nil { + t.Fatal(err) + } + p1363, err := anscrypto.DERToP1363(der, 32) + if err != nil { + t.Fatal(err) + } + return toSign + "." + base64.RawURLEncoding.EncodeToString(p1363) +} + +func genKey(t *testing.T) *ecdsa.PrivateKey { + t.Helper() + priv, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) + if err != nil { + t.Fatal(err) + } + return priv +} + +// drainOutbox claims and returns all pending outbox rows. +func (fx *identityFixture) drainOutbox(t *testing.T) []sqlite.OutboxEvent { + t.Helper() + rows, err := fx.outbox.Claim(context.Background(), 100) + if err != nil { + t.Fatal(err) + } + for _, row := range rows { + if err := fx.outbox.MarkSent(context.Background(), row.ID); err != nil { + t.Fatal(err) + } + } + return rows +} + +// decodeOutboxEvent parses one outbox row's payload, verifies the +// producer signature against the fixture's signer key, and returns +// the inner identity event. +func (fx *identityFixture) decodeOutboxEvent(t *testing.T, row sqlite.OutboxEvent) *identityevent.Event { + t.Helper() + var payload struct { + InnerEventCanonical json.RawMessage `json:"innerEventCanonical"` + ProducerSignature string `json:"producerSignature"` + } + if err := json.Unmarshal(row.PayloadJSON, &payload); err != nil { + t.Fatalf("payload: %v", err) + } + if _, err := anscrypto.VerifyWithPublicKey(fx.signerPub, payload.ProducerSignature, payload.InnerEventCanonical); err != nil { + t.Fatalf("producer signature: %v", err) + } + var inner identityevent.Event + if err := json.Unmarshal(payload.InnerEventCanonical, &inner); err != nil { + t.Fatalf("inner event: %v", err) + } + if err := inner.Validate(); err != nil { + t.Fatalf("inner event invalid: %v", err) + } + return &inner +} + +// ----- register ----- + +func TestIdentityRegister_DIDWebNoop(t *testing.T) { + t.Parallel() + fx := newIdentityFixture(t, nil) + ctx := context.Background() + + res, err := fx.svc.Register(ctx, fx.providerID, "did:web:Identity.ACME-corp.com") + if err != nil { + t.Fatalf("register: %v", err) + } + if res.Identity.Status != domain.IdentityPendingControl { + t.Fatalf("status: %s", res.Identity.Status) + } + if res.Identity.Value != "did:web:identity.acme-corp.com" { + t.Fatalf("canonicalization: %s", res.Identity.Value) + } + if res.Nonce == "" || len(res.Challenges) != 1 || res.Challenges[0].Kid != "" || + res.Challenges[0].SigningInput == "" { + t.Fatalf("challenge round wrong: %+v", res) + } + + // The signing input decodes to a proof input binding this round. + raw, err := base64.RawURLEncoding.DecodeString(res.Challenges[0].SigningInput) + if err != nil { + t.Fatal(err) + } + var input anscrypto.IdentityProofInput + if err := json.Unmarshal(raw, &input); err != nil { + t.Fatal(err) + } + if input.IdentityID != res.Identity.IdentityID || input.Nonce != res.Nonce || + input.Purpose != anscrypto.IdentityProofPurpose || input.RaID != "ra-test" || + input.Scheme != "did:web" || input.Identifier != res.Identity.Value { + t.Fatalf("proof input wrong: %+v", input) + } + + // Idempotent re-add: same identityId, fresh nonce. + again, err := fx.svc.Register(ctx, fx.providerID, "did:web:identity.acme-corp.com") + if err != nil { + t.Fatalf("re-add: %v", err) + } + if again.Identity.IdentityID != res.Identity.IdentityID { + t.Fatal("re-add must reuse the identityId") + } + if again.Nonce == res.Nonce { + t.Fatal("re-add must supersede the nonce") + } + + // Register seals nothing — only proven control reaches the TL. + if rows := fx.drainOutbox(t); len(rows) != 0 { + t.Fatalf("register must not emit, got %d rows", len(rows)) + } +} + +func TestIdentityRegister_Rejections(t *testing.T) { + t.Parallel() + fx := newIdentityFixture(t, nil) + ctx := context.Background() + + if _, err := fx.svc.Register(ctx, "", "did:web:a.com"); err == nil { + t.Error("missing owner should fail") + } + if _, err := fx.svc.Register(ctx, fx.providerID, "bogus"); err == nil || + !strings.Contains(err.Error(), "IDENTIFIER_KIND_UNSUPPORTED") { + t.Errorf("bogus value: %v", err) + } + // lei is recognized but postponed. + if _, err := fx.svc.Register(ctx, fx.providerID, "5493001KJTIIGC8Y1R17"); err == nil || + !strings.Contains(err.Error(), "IDENTIFIER_KIND_UNSUPPORTED") { + t.Errorf("lei: %v", err) + } +} + +func TestIdentityRegister_RateLimited(t *testing.T) { + t.Parallel() + fx := newIdentityFixture(t, nil) + fx.svc.WithRegisterRateLimit(2) + ctx := context.Background() + + for i := range 2 { + if _, err := fx.svc.Register(ctx, fx.providerID, "did:web:a.com"); err != nil { + t.Fatalf("call %d: %v", i, err) + } + } + if _, err := fx.svc.Register(ctx, fx.providerID, "did:web:a.com"); err == nil || + !strings.Contains(err.Error(), "RATE_LIMITED") { + t.Fatalf("third call: %v", err) + } + // A different owner has its own budget. + if _, err := fx.svc.Register(ctx, "owner-2", "did:web:b.com"); err != nil { + t.Fatalf("other owner: %v", err) + } + // The window rolls over. + fx.clock.now = fx.clock.now.Add(2 * time.Minute) + if _, err := fx.svc.Register(ctx, fx.providerID, "did:web:a.com"); err != nil { + t.Fatalf("after window: %v", err) + } +} + +// ----- verify-control: did:web (noop hint synthesis) ----- + +// verifyDIDWeb registers + proves a did:web identity through the noop +// resolver, returning the verified identity and the key used. +func verifyDIDWeb(t *testing.T, fx *identityFixture, owner, value string) (*domain.VerifiedIdentity, *ecdsa.PrivateKey) { + t.Helper() + ctx := context.Background() + res, err := fx.svc.Register(ctx, owner, value) + if err != nil { + t.Fatalf("register: %v", err) + } + priv := genKey(t) + kid := res.Identity.Value + "#key-1" + jws := signProof(t, priv, kid, res.Challenges[0].SigningInput, true) + identity, err := fx.svc.VerifyControl(ctx, owner, res.Identity.IdentityID, service.ProofSubmission{SignedProofs: []string{jws}}) + if err != nil { + t.Fatalf("verify-control: %v", err) + } + return identity, priv +} + +func TestIdentityVerifyControl_DIDWebNoop(t *testing.T) { + t.Parallel() + fx := newIdentityFixture(t, nil) + + identity, _ := verifyDIDWeb(t, fx, fx.providerID, "did:web:identity.acme-corp.com") + if identity.Status != domain.IdentityVerified || identity.ProofMethod != "did-web-sig" { + t.Fatalf("verified state: %+v", identity) + } + + rows := fx.drainOutbox(t) + if len(rows) != 1 || rows[0].SchemaVersion != "IDENTITY" { + t.Fatalf("outbox rows: %+v", rows) + } + inner := fx.decodeOutboxEvent(t, rows[0]) + if inner.EventType != identityevent.TypeIdentityVerified || + inner.IdentityID != identity.IdentityID || + inner.ProviderID != fx.providerID || + len(inner.Keys) != 1 { + t.Fatalf("sealed event: %+v", inner) + } + key := inner.Keys[0] + if key.ID() != identity.Value+"#key-1" || key.SignedProof == "" { + t.Fatalf("sealed key shape wrong: %+v", key) + } + // The sealed verification method is quoted VERBATIM: it carries + // the registrant's exact jwk bytes, and the sealed proof verifies + // against the key read out of it — offline, no derived values. + var sealedVM struct { + Controller string `json:"controller"` + Type string `json:"type"` + PublicKeyJwk json.RawMessage `json:"publicKeyJwk"` + } + if err := json.Unmarshal(key.VerificationMethod, &sealedVM); err != nil { + t.Fatalf("sealed verification method not an object: %v", err) + } + if sealedVM.Controller != identity.Value || len(sealedVM.PublicKeyJwk) == 0 { + t.Fatalf("sealed verification method members: %+v", sealedVM) + } + pub, err := anscrypto.ParseJWK(sealedVM.PublicKeyJwk) + if err != nil { + t.Fatal(err) + } + if _, err := anscrypto.VerifyStandardJWSWithPublicKey(pub, key.SignedProof); err != nil { + t.Fatalf("sealed proof does not verify against sealed key: %v", err) + } +} + +func TestIdentityVerifyControl_MultiKey(t *testing.T) { + t.Parallel() + fx := newIdentityFixture(t, nil) + ctx := context.Background() + + res, err := fx.svc.Register(ctx, fx.providerID, "did:web:a.com") + if err != nil { + t.Fatal(err) + } + k1, k2 := genKey(t), genKey(t) + did := res.Identity.Value + jws1 := signProof(t, k1, did+"#key-1", res.Challenges[0].SigningInput, true) + jws2 := signProof(t, k2, did+"#key-2", res.Challenges[0].SigningInput, true) + + if _, err := fx.svc.VerifyControl(ctx, fx.providerID, res.Identity.IdentityID, service.ProofSubmission{SignedProofs: []string{jws1, jws2}}); err != nil { + t.Fatalf("multi-key verify: %v", err) + } + rows := fx.drainOutbox(t) + inner := fx.decodeOutboxEvent(t, rows[0]) + if len(inner.Keys) != 2 { + t.Fatalf("sealed keys: %d", len(inner.Keys)) + } +} + +func TestIdentityVerifyControl_FailClosed(t *testing.T) { + t.Parallel() + fx := newIdentityFixture(t, nil) + ctx := context.Background() + + res, err := fx.svc.Register(ctx, fx.providerID, "did:web:a.com") + if err != nil { + t.Fatal(err) + } + id := res.Identity.IdentityID + did := res.Identity.Value + priv := genKey(t) + good := signProof(t, priv, did+"#key-1", res.Challenges[0].SigningInput, true) + + cases := []struct { + name string + proofs []string + want string + }{ + {"no proofs", nil, "IDENTIFIER_PROOF_INVALID"}, + {"not a jws", []string{"garbage"}, "IDENTIFIER_PROOF_INVALID"}, + {"wrong payload", []string{signProof(t, priv, did+"#key-1", + base64.RawURLEncoding.EncodeToString([]byte(`{"x":1}`)), true)}, "PRICC_SIGNATURE_INVALID"}, + {"missing kid", []string{signProof(t, priv, "", res.Challenges[0].SigningInput, true)}, "DID_VERIFICATION_METHOD_INVALID"}, + {"kid not a fragment of the DID", []string{signProof(t, priv, "did:web:evil.com#key-1", + res.Challenges[0].SigningInput, true)}, "DID_VERIFICATION_METHOD_INVALID"}, + {"duplicate kid", []string{good, good}, "IDENTIFIER_PROOF_INVALID"}, + {"one bad proof fails the batch", []string{good, signProof(t, genKey(t), did+"#key-2", + res.Challenges[0].SigningInput, false)}, "DID_VERIFICATION_METHOD_INVALID"}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + _, err := fx.svc.VerifyControl(ctx, fx.providerID, id, service.ProofSubmission{SignedProofs: tc.proofs}) + if err == nil || !strings.Contains(err.Error(), tc.want) { + t.Fatalf("want %s, got %v", tc.want, err) + } + }) + } + + // Failed attempts never consume the nonce — the good proof still + // lands. + if _, err := fx.svc.VerifyControl(ctx, fx.providerID, id, service.ProofSubmission{SignedProofs: []string{good}}); err != nil { + t.Fatalf("good proof after failures: %v", err) + } + // …and the consumed nonce cannot be replayed. + _, err = fx.svc.VerifyControl(ctx, fx.providerID, id, service.ProofSubmission{SignedProofs: []string{good}}) + if err == nil || !strings.Contains(err.Error(), "PRICC_TOKEN_ALREADY_USED") { + t.Fatalf("replay: %v", err) + } +} + +func TestIdentityVerifyControl_WrongSignature(t *testing.T) { + t.Parallel() + fx := newIdentityFixture(t, nil) + ctx := context.Background() + + res, err := fx.svc.Register(ctx, fx.providerID, "did:web:a.com") + if err != nil { + t.Fatal(err) + } + // The embedded jwk names key A, but the signature is from key B: + // verification against the (noop-synthesized) authoritative key A + // must fail. + keyA, keyB := genKey(t), genKey(t) + jwkA, err := anscrypto.PublicKeyToJWK(&keyA.PublicKey) + if err != nil { + t.Fatal(err) + } + header, err := json.Marshal(map[string]any{ + "alg": "ES256", "kid": res.Identity.Value + "#key-1", "jwk": jwkA, + }) + if err != nil { + t.Fatal(err) + } + encodedHeader := base64.RawURLEncoding.EncodeToString(header) + toSign := encodedHeader + "." + res.Challenges[0].SigningInput + digest := sha256.Sum256([]byte(toSign)) + der, err := ecdsa.SignASN1(rand.Reader, keyB, digest[:]) + if err != nil { + t.Fatal(err) + } + p1363, err := anscrypto.DERToP1363(der, 32) + if err != nil { + t.Fatal(err) + } + forged := toSign + "." + base64.RawURLEncoding.EncodeToString(p1363) + + _, err = fx.svc.VerifyControl(ctx, fx.providerID, res.Identity.IdentityID, service.ProofSubmission{SignedProofs: []string{forged}}) + if err == nil || !strings.Contains(err.Error(), "PRICC_SIGNATURE_INVALID") { + t.Fatalf("forged signature: %v", err) + } +} + +func TestIdentityVerifyControl_ExpiredNonce(t *testing.T) { + t.Parallel() + fx := newIdentityFixture(t, nil) + ctx := context.Background() + + res, err := fx.svc.Register(ctx, fx.providerID, "did:web:a.com") + if err != nil { + t.Fatal(err) + } + jws := signProof(t, genKey(t), res.Identity.Value+"#key-1", res.Challenges[0].SigningInput, true) + + fx.clock.now = fx.clock.now.Add(2 * time.Hour) + _, err = fx.svc.VerifyControl(ctx, fx.providerID, res.Identity.IdentityID, service.ProofSubmission{SignedProofs: []string{jws}}) + if err == nil || !strings.Contains(err.Error(), "PRICC_TOKEN_EXPIRED") { + t.Fatalf("expired nonce: %v", err) + } + + // Recovery is the idempotent re-add: same id, fresh nonce. + again, err := fx.svc.Register(ctx, fx.providerID, "did:web:a.com") + if err != nil || again.Identity.IdentityID != res.Identity.IdentityID { + t.Fatalf("re-add recovery: %+v %v", again, err) + } +} + +// ----- verify-control: did:web against a canned document (web-mode rules) ----- + +func TestIdentityVerifyControl_DIDWebDocumentRules(t *testing.T) { + t.Parallel() + did := "did:web:identity.acme-corp.com" + keyOK := genKey(t) + keyMB := genKey(t) + jwkOK, err := anscrypto.PublicKeyToJWK(&keyOK.PublicKey) + if err != nil { + t.Fatal(err) + } + multibase, err := anscrypto.EncodeMultibase(&keyMB.PublicKey) + if err != nil { + t.Fatal(err) + } + doc := &port.DIDDocument{ + ID: did, + AssertionMethod: []port.VerificationMethod{ + {ID: did + "#jwk-key", Controller: did, Type: "JsonWebKey2020", PublicKeyJwk: jwkOK}, + {ID: did + "#mb-key", Controller: did, Type: "Multikey", PublicKeyMultibase: multibase}, + {ID: did + "#foreign", Controller: "did:web:other.com", Type: "JsonWebKey2020", PublicKeyJwk: jwkOK}, + {ID: did + "#empty", Controller: did, Type: "JsonWebKey2020"}, + }, + } + fx := newIdentityFixture(t, &fakeResolver{doc: doc}) + ctx := context.Background() + + res, err := fx.svc.Register(ctx, fx.providerID, did) + if err != nil { + t.Fatal(err) + } + // The advisory fetch enumerated the document's kids. + if len(res.Challenges) != 4 { + t.Fatalf("challenge kids: %+v", res.Challenges) + } + id := res.Identity.IdentityID + signingInput := res.Challenges[0].SigningInput + + // JWK-keyed and multibase-keyed methods both verify. + proofs := []string{ + signProof(t, keyOK, did+"#jwk-key", signingInput, false), + signProof(t, keyMB, did+"#mb-key", signingInput, false), + } + if _, err := fx.svc.VerifyControl(ctx, fx.providerID, id, service.ProofSubmission{SignedProofs: proofs}); err != nil { + t.Fatalf("doc-keyed verify: %v", err) + } + + // Rotation round for the rule rejections. + rot, err := fx.svc.Rotate(ctx, fx.providerID, id, did) + if err != nil { + t.Fatal(err) + } + rotInput := rot.Challenges[0].SigningInput + cases := []struct { + name string + jws string + want string + }{ + {"unknown kid", signProof(t, keyOK, did+"#nope", rotInput, false), "DID_VERIFICATION_METHOD_INVALID"}, + {"cross-controller", signProof(t, keyOK, did+"#foreign", rotInput, false), "DID_VERIFICATION_METHOD_INVALID"}, + {"keyless method", signProof(t, keyOK, did+"#empty", rotInput, false), "DID_VERIFICATION_METHOD_INVALID"}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + _, err := fx.svc.VerifyControl(ctx, fx.providerID, id, service.ProofSubmission{SignedProofs: []string{tc.jws}}) + if err == nil || !strings.Contains(err.Error(), tc.want) { + t.Fatalf("want %s, got %v", tc.want, err) + } + }) + } +} + +func TestIdentityVerifyControl_DocumentIDMismatch(t *testing.T) { + t.Parallel() + doc := &port.DIDDocument{ID: "did:web:other.com"} + fx := newIdentityFixture(t, &fakeResolver{doc: doc}) + ctx := context.Background() + + res, err := fx.svc.Register(ctx, fx.providerID, "did:web:a.com") + if err != nil { + t.Fatal(err) + } + jws := signProof(t, genKey(t), res.Identity.Value+"#k", res.Challenges[0].SigningInput, false) + _, err = fx.svc.VerifyControl(ctx, fx.providerID, res.Identity.IdentityID, service.ProofSubmission{SignedProofs: []string{jws}}) + if err == nil || !strings.Contains(err.Error(), "DID_DOCUMENT_ID_MISMATCH") { + t.Fatalf("doc id mismatch: %v", err) + } +} + +func TestIdentityRegister_ResolutionFailureFailsFast(t *testing.T) { + t.Parallel() + fx := newIdentityFixture(t, &fakeResolver{ + err: domain.NewValidationError("DID_RESOLUTION_FAILED", "boom"), + }) + _, err := fx.svc.Register(context.Background(), fx.providerID, "did:web:a.com") + if err == nil || !strings.Contains(err.Error(), "DID_RESOLUTION_FAILED") { + t.Fatalf("advisory fetch failure: %v", err) + } +} + +// ----- verify-control: did:key ----- + +func TestIdentityLifecycle_DIDKey(t *testing.T) { + t.Parallel() + fx := newIdentityFixture(t, nil) + ctx := context.Background() + + priv := genKey(t) + msid, err := anscrypto.EncodeMultibase(&priv.PublicKey) + if err != nil { + t.Fatal(err) + } + did := "did:key:" + msid + + res, err := fx.svc.Register(ctx, fx.providerID, did) + if err != nil { + t.Fatalf("register did:key: %v", err) + } + // did:key has exactly one challenge entry naming the method id. + if len(res.Challenges) != 1 || res.Challenges[0].Kid != did+"#"+msid { + t.Fatalf("did:key challenges: %+v", res.Challenges) + } + + // A wrong kid is rejected even with a valid signature. + bad := signProof(t, priv, did+"#wrong", res.Challenges[0].SigningInput, false) + if _, err := fx.svc.VerifyControl(ctx, fx.providerID, res.Identity.IdentityID, service.ProofSubmission{SignedProofs: []string{bad}}); err == nil || + !strings.Contains(err.Error(), "DID_VERIFICATION_METHOD_INVALID") { + t.Fatalf("wrong did:key kid: %v", err) + } + // A different key's signature fails against the DID's key. + forged := signProof(t, genKey(t), did+"#"+msid, res.Challenges[0].SigningInput, false) + if _, err := fx.svc.VerifyControl(ctx, fx.providerID, res.Identity.IdentityID, service.ProofSubmission{SignedProofs: []string{forged}}); err == nil || + !strings.Contains(err.Error(), "PRICC_SIGNATURE_INVALID") { + t.Fatalf("forged did:key proof: %v", err) + } + + good := signProof(t, priv, res.Challenges[0].Kid, res.Challenges[0].SigningInput, false) + identity, err := fx.svc.VerifyControl(ctx, fx.providerID, res.Identity.IdentityID, service.ProofSubmission{SignedProofs: []string{good}}) + if err != nil { + t.Fatalf("did:key verify: %v", err) + } + if identity.Status != domain.IdentityVerified || identity.ProofMethod != "did-key-sig" { + t.Fatalf("did:key verified state: %+v", identity) + } + rows := fx.drainOutbox(t) + inner := fx.decodeOutboxEvent(t, rows[0]) + if inner.Kind != "did:key" || len(inner.Keys) != 1 { + t.Fatalf("did:key sealed event: %+v", inner) + } +} + +// TestIdentityLifecycle_Ed25519 drives a did:key Ed25519 identity +// end to end: EdDSA proofs (raw-signing-input signatures, RFC 8037), +// the z6Mk did:key form, and the verbatim Multikey seal. +func TestIdentityLifecycle_Ed25519(t *testing.T) { + t.Parallel() + fx := newIdentityFixture(t, nil) + ctx := context.Background() + + pub, priv, err := ed25519.GenerateKey(rand.Reader) + if err != nil { + t.Fatal(err) + } + msid, err := anscrypto.EncodeMultibase(pub) + if err != nil { + t.Fatal(err) + } + did := "did:key:" + msid + if !strings.HasPrefix(msid, "z6Mk") { + t.Fatalf("ed25519 did:key form: %s", msid) + } + + res, err := fx.svc.Register(ctx, fx.providerID, did) + if err != nil { + t.Fatalf("register ed25519 did:key: %v", err) + } + kid := res.Challenges[0].Kid + + // EdDSA compact JWS: signature over the raw signing input. + header, err := json.Marshal(map[string]any{"alg": "EdDSA", "kid": kid}) + if err != nil { + t.Fatal(err) + } + toSign := base64.RawURLEncoding.EncodeToString(header) + "." + res.Challenges[0].SigningInput + jws := toSign + "." + base64.RawURLEncoding.EncodeToString(ed25519.Sign(priv, []byte(toSign))) + + identity, err := fx.svc.VerifyControl(ctx, fx.providerID, res.Identity.IdentityID, service.ProofSubmission{SignedProofs: []string{jws}}) + if err != nil { + t.Fatalf("ed25519 verify-control: %v", err) + } + if identity.Status != domain.IdentityVerified { + t.Fatalf("status: %s", identity.Status) + } + + // The seal quotes the did:key Multikey method — its key material + // is the method-specific id verbatim from the identifier. + rows := fx.drainOutbox(t) + inner := fx.decodeOutboxEvent(t, rows[0]) + var vm struct { + Type string `json:"type"` + PublicKeyMultibase string `json:"publicKeyMultibase"` + } + if err := json.Unmarshal(inner.Keys[0].VerificationMethod, &vm); err != nil { + t.Fatal(err) + } + if vm.Type != "Multikey" || vm.PublicKeyMultibase != msid { + t.Fatalf("sealed did:key method: %+v", vm) + } +} + +// TestIdentityVerifyControl_X25519Rejected pins the precise +// rejection: a key-agreement key listed as an assertionMethod can +// never prove control. +func TestIdentityVerifyControl_X25519Rejected(t *testing.T) { + t.Parallel() + did := "did:web:identity.acme-corp.com" + doc := &port.DIDDocument{ + ID: did, + AssertionMethod: []port.VerificationMethod{{ + ID: did + "#x25519", + Controller: did, + Type: "JsonWebKey2020", + PublicKeyJwk: json.RawMessage(`{"kty":"OKP","crv":"X25519","x":"9GXjPGGvmRq9F6Ng5dQQ_s31mfhxrcNZxRGONrmH30k"}`), + Raw: json.RawMessage(`{"id":"` + did + `#x25519"}`), + }}, + } + fx := newIdentityFixture(t, &fakeResolver{doc: doc}) + ctx := context.Background() + + res, err := fx.svc.Register(ctx, fx.providerID, did) + if err != nil { + t.Fatal(err) + } + jws := signProof(t, genKey(t), did+"#x25519", res.Challenges[0].SigningInput, false) + _, err = fx.svc.VerifyControl(ctx, fx.providerID, res.Identity.IdentityID, service.ProofSubmission{SignedProofs: []string{jws}}) + if err == nil || !strings.Contains(err.Error(), "key-agreement key") { + t.Fatalf("X25519 rejection: %v", err) + } +} + +// ----- rotation, revocation, duplicates ----- + +func TestIdentityRotation_SealsUpdated(t *testing.T) { + t.Parallel() + fx := newIdentityFixture(t, nil) + ctx := context.Background() + + identity, _ := verifyDIDWeb(t, fx, fx.providerID, "did:web:a.com") + fx.drainOutbox(t) + + rot, err := fx.svc.Rotate(ctx, fx.providerID, identity.IdentityID, "did:web:b.com") + if err != nil { + t.Fatalf("rotate: %v", err) + } + if rot.Identity.Value != "did:web:a.com" || rot.Identity.PendingValue != "did:web:b.com" { + t.Fatalf("staged state: %+v", rot.Identity) + } + // Until the proof lands, the previously sealed state stands — + // nothing emitted by the PUT itself. + if rows := fx.drainOutbox(t); len(rows) != 0 { + t.Fatalf("PUT must not seal, got %d rows", len(rows)) + } + + newKey := genKey(t) + jws := signProof(t, newKey, "did:web:b.com#key-1", rot.Challenges[0].SigningInput, true) + rotated, err := fx.svc.VerifyControl(ctx, fx.providerID, identity.IdentityID, service.ProofSubmission{SignedProofs: []string{jws}}) + if err != nil { + t.Fatalf("rotation verify: %v", err) + } + if rotated.Value != "did:web:b.com" || rotated.PendingValue != "" { + t.Fatalf("rotated state: %+v", rotated) + } + + rows := fx.drainOutbox(t) + if len(rows) != 1 { + t.Fatalf("rotation rows: %d", len(rows)) + } + inner := fx.decodeOutboxEvent(t, rows[0]) + if inner.EventType != identityevent.TypeIdentityUpdated || + inner.Value != "did:web:b.com" || inner.PreviousValue != "did:web:a.com" { + t.Fatalf("IDENTITY_UPDATED event: %+v", inner) + } +} + +func TestIdentityRevoke(t *testing.T) { + t.Parallel() + fx := newIdentityFixture(t, nil) + ctx := context.Background() + + identity, _ := verifyDIDWeb(t, fx, fx.providerID, "did:web:a.com") + fx.drainOutbox(t) + + revoked, err := fx.svc.Revoke(ctx, fx.providerID, identity.IdentityID) + if err != nil { + t.Fatalf("revoke: %v", err) + } + if revoked.Status != domain.IdentityRevoked { + t.Fatalf("status: %s", revoked.Status) + } + rows := fx.drainOutbox(t) + inner := fx.decodeOutboxEvent(t, rows[0]) + if inner.EventType != identityevent.TypeIdentityRevoked || inner.RevokedAt == "" { + t.Fatalf("IDENTITY_REVOKED event: %+v", inner) + } + + // Terminal: no rotate, no verify, no re-revoke. + if _, err := fx.svc.Rotate(ctx, fx.providerID, identity.IdentityID, "did:web:b.com"); err == nil { + t.Error("rotate after revoke should fail") + } + if _, err := fx.svc.Revoke(ctx, fx.providerID, identity.IdentityID); err == nil { + t.Error("double revoke should fail") + } + // The owner can re-register the value on a fresh row. + if _, err := fx.svc.Register(ctx, fx.providerID, "did:web:a.com"); err != nil { + t.Errorf("re-register after revoke: %v", err) + } +} + +func TestIdentityDuplicates(t *testing.T) { + t.Parallel() + fx := newIdentityFixture(t, nil) + ctx := context.Background() + + verifyDIDWeb(t, fx, fx.providerID, "did:web:a.com") + + // Same owner re-registering a VERIFIED value → rotate instead. + _, err := fx.svc.Register(ctx, fx.providerID, "did:web:a.com") + if err == nil || !strings.Contains(err.Error(), "IDENTIFIER_DUPLICATE") { + t.Fatalf("owner duplicate: %v", err) + } + // Another owner registering an already-proven value → early + // duplicate feedback. + _, err = fx.svc.Register(ctx, "owner-2", "did:web:a.com") + if err == nil || !strings.Contains(err.Error(), "IDENTIFIER_DUPLICATE") { + t.Fatalf("cross-owner duplicate: %v", err) + } +} + +func TestIdentityProvenUniquenessRace(t *testing.T) { + t.Parallel() + fx := newIdentityFixture(t, nil) + ctx := context.Background() + + // Two owners hold competing PENDING claims (registered before + // either proved). + resA, err := fx.svc.Register(ctx, "owner-1", "did:web:contested.com") + if err != nil { + t.Fatal(err) + } + resB, err := fx.svc.Register(ctx, "owner-2", "did:web:contested.com") + if err != nil { + t.Fatal(err) + } + + // First to PROVE wins. + keyA := genKey(t) + jwsA := signProof(t, keyA, resA.Identity.Value+"#key-1", resA.Challenges[0].SigningInput, true) + if _, err := fx.svc.VerifyControl(ctx, "owner-1", resA.Identity.IdentityID, service.ProofSubmission{SignedProofs: []string{jwsA}}); err != nil { + t.Fatalf("winner: %v", err) + } + + // The loser's verify-time flip hits the proven-uniqueness index. + keyB := genKey(t) + jwsB := signProof(t, keyB, resB.Identity.Value+"#key-1", resB.Challenges[0].SigningInput, true) + _, err = fx.svc.VerifyControl(ctx, "owner-2", resB.Identity.IdentityID, service.ProofSubmission{SignedProofs: []string{jwsB}}) + if err == nil || !strings.Contains(err.Error(), "IDENTIFIER_DUPLICATE") { + t.Fatalf("loser: %v", err) + } + // The losing transaction rolled back whole — including the nonce + // consumption — and only the winner's event sealed. + rows := fx.drainOutbox(t) + if len(rows) != 1 { + t.Fatalf("sealed events after race: %d", len(rows)) + } +} + +// ----- owner gates ----- + +func TestIdentityOwnerGates(t *testing.T) { + t.Parallel() + fx := newIdentityFixture(t, nil) + ctx := context.Background() + + identity, _ := verifyDIDWeb(t, fx, fx.providerID, "did:web:a.com") + + // Reads hide existence (404-shaped not-found). + if _, _, err := fx.svc.Detail(ctx, "owner-2", identity.IdentityID); !errors.Is(err, domain.ErrNotFound) { + t.Fatalf("cross-owner detail: %v", err) + } + // Writes surface the authorization failure (403-shaped). + if _, err := fx.svc.Revoke(ctx, "owner-2", identity.IdentityID); !errors.Is(err, domain.ErrUnauthorized) { + t.Fatalf("cross-owner revoke: %v", err) + } + if _, err := fx.svc.Rotate(ctx, "owner-2", identity.IdentityID, "did:web:b.com"); !errors.Is(err, domain.ErrUnauthorized) { + t.Fatalf("cross-owner rotate: %v", err) + } + // List is owner-scoped. + mine, err := fx.svc.List(ctx, fx.providerID) + if err != nil || len(mine) != 1 { + t.Fatalf("list mine: %d %v", len(mine), err) + } + theirs, err := fx.svc.List(ctx, "owner-2") + if err != nil || len(theirs) != 0 { + t.Fatalf("list theirs: %d %v", len(theirs), err) + } +} + +// ----- links ----- + +func TestIdentityLinks(t *testing.T) { + t.Parallel() + fx := newIdentityFixture(t, nil) + ctx := context.Background() + + identity, _ := verifyDIDWeb(t, fx, fx.providerID, "did:web:a.com") + fx.drainOutbox(t) + fx.saveAgent(t, "agent-1", fx.providerID, "one.example.com") + fx.saveAgent(t, "agent-2", fx.providerID, "two.example.com") + fx.saveAgent(t, "agent-x", "owner-2", "theirs.example.com") + + // Owner gate, agent side: naming someone else's agent fails the + // whole call without revealing the agent's existence. + _, err := fx.svc.Link(ctx, fx.providerID, identity.IdentityID, []string{"agent-1", "agent-x"}) + if !errors.Is(err, domain.ErrNotFound) { + t.Fatalf("cross-owner agent in batch: %v", err) + } + if rows := fx.drainOutbox(t); len(rows) != 0 { + t.Fatal("failed batch must seal nothing") + } + + // Batch of two (with a duplicate id deduped) → ONE sealed event. + linked, err := fx.svc.Link(ctx, fx.providerID, identity.IdentityID, []string{"agent-1", "agent-2", "agent-1"}) + if err != nil || linked != 2 { + t.Fatalf("link batch: %d %v", linked, err) + } + rows := fx.drainOutbox(t) + if len(rows) != 1 { + t.Fatalf("link batch rows: %d", len(rows)) + } + inner := fx.decodeOutboxEvent(t, rows[0]) + if inner.EventType != identityevent.TypeIdentityLinked || len(inner.AnsIDs) != 2 { + t.Fatalf("IDENTITY_LINKED event: %+v", inner) + } + + // Fully idempotent repeat seals nothing. + linked, err = fx.svc.Link(ctx, fx.providerID, identity.IdentityID, []string{"agent-1"}) + if err != nil || linked != 0 { + t.Fatalf("idempotent link: %d %v", linked, err) + } + if rows := fx.drainOutbox(t); len(rows) != 0 { + t.Fatal("idempotent link must seal nothing") + } + + // Detail surfaces the live links. + _, links, err := fx.svc.Detail(ctx, fx.providerID, identity.IdentityID) + if err != nil || len(links) != 2 { + t.Fatalf("detail links: %d %v", len(links), err) + } + + // Unlink seals IDENTITY_UNLINKED for the one agent. + if err := fx.svc.Unlink(ctx, fx.providerID, identity.IdentityID, "agent-1"); err != nil { + t.Fatalf("unlink: %v", err) + } + rows = fx.drainOutbox(t) + inner = fx.decodeOutboxEvent(t, rows[0]) + if inner.EventType != identityevent.TypeIdentityUnlinked || + len(inner.AnsIDs) != 1 || inner.AnsIDs[0] != "agent-1" { + t.Fatalf("IDENTITY_UNLINKED event: %+v", inner) + } + // Unlinking a non-link 404s and seals nothing. + if err := fx.svc.Unlink(ctx, fx.providerID, identity.IdentityID, "agent-1"); !errors.Is(err, domain.ErrNotFound) { + t.Fatalf("double unlink: %v", err) + } + if rows := fx.drainOutbox(t); len(rows) != 0 { + t.Fatal("failed unlink must seal nothing") + } +} + +func TestIdentityLinkGuards(t *testing.T) { + t.Parallel() + fx := newIdentityFixture(t, nil) + ctx := context.Background() + + // Links attach only while VERIFIED. + res, err := fx.svc.Register(ctx, fx.providerID, "did:web:a.com") + if err != nil { + t.Fatal(err) + } + fx.saveAgent(t, "agent-1", fx.providerID, "one.example.com") + _, err = fx.svc.Link(ctx, fx.providerID, res.Identity.IdentityID, []string{"agent-1"}) + if err == nil || !strings.Contains(err.Error(), "IDENTITY_NOT_VERIFIED") { + t.Fatalf("link pending identity: %v", err) + } + + identity, _ := verifyDIDWeb(t, fx, "owner-2", "did:web:b.com") + // Cross-owner identity write → 403-shaped. + if _, err := fx.svc.Link(ctx, fx.providerID, identity.IdentityID, []string{"agent-1"}); !errors.Is(err, domain.ErrUnauthorized) { + t.Fatalf("cross-owner identity link: %v", err) + } + // Empty and oversized batches. + if _, err := fx.svc.Link(ctx, "owner-2", identity.IdentityID, nil); err == nil { + t.Error("empty batch should fail") + } + huge := make([]string, 257) + for i := range huge { + huge[i] = "agent" + } + if _, err := fx.svc.Link(ctx, "owner-2", identity.IdentityID, huge); err == nil || + !strings.Contains(err.Error(), "at most") { + t.Errorf("oversized batch: %v", err) + } +} diff --git a/internal/ra/service/identitykinds.go b/internal/ra/service/identitykinds.go new file mode 100644 index 0000000..19f7bad --- /dev/null +++ b/internal/ra/service/identitykinds.go @@ -0,0 +1,317 @@ +package service + +// This file is THE extension seam for identifier kinds. Each kind — +// did:web, did:key today; did:plc, did:ion, did:ethr, lei (vLEI) +// next — implements controlVerifier and registers itself in +// newControlVerifiers. Everything else in the identity service is +// kind-agnostic: the aggregate lifecycle, the nonce discipline, the +// owner gates, the links, and the sealing pipeline never branch on +// kind. +// +// How to add a kind, end to end: +// +// 1. Teach domain.InferIdentifierKind the kind's lexical form and +// canonicalization (the kind set is spec-frozen — a new kind is +// an ANS-spec amendment, so a new dispatch arm there is correct, +// not a smell). Add its proofMethod token. +// 2. Implement controlVerifier. Inject whatever I/O the kind needs +// through ports (did:plc → a PLC-directory fetcher; did:ion → an +// ION-node fetcher; lei → port-wrapped GLEIF L1 + vlei-verifier +// clients), each with a noop adapter for the quickstart and a +// real adapter selected by config — the DNS-verifier pattern. +// 3. Register it in newControlVerifiers. Unregistered kinds fail +// with IDENTIFIER_KIND_UNSUPPORTED — the 404-is-the-signal rule: +// no stubs, a kind exists only when its proof is real. +// +// Per-kind WIRE shapes are additive, never branching: +// +// - Request side: ProofSubmission carries every kind's proof +// material as optional members. JWS kinds read SignedProofs; +// lei will add CESRSignature, did:ethr will add EthSignature. +// Exactly one family of members is set per kind (the design's +// "exactly one proof field is set per kind" rule); each verifier +// validates its own members and ignores the rest. +// - Response side: the 202 challenge offer is the shared +// {nonce, expiresAt, challenges[]} envelope. A kind needing +// extra offer fields (lei's presentationStatus) adds an optional +// capability interface here — the same discover-by-type-assertion +// pattern the TL store uses for identity indexing — so existing +// kinds never grow dead fields. +// - Seal side: identityevent.ProvenKey quotes the kind's +// authoritative verification material verbatim. Kinds with no +// document to quote commit minimally (lei: subject AID + +// thumbprint — the KEL is the authoritative key history). + +import ( + "context" + "encoding/json" + "fmt" + "strings" + + anscrypto "github.com/godaddy/ans/internal/crypto" + "github.com/godaddy/ans/internal/domain" + "github.com/godaddy/ans/internal/port" + identityevent "github.com/godaddy/ans/internal/tl/event/identity" +) + +// ProofSubmission carries the kind-specific proof material from the +// verify-control request body. Members are additive per kind; each +// controlVerifier validates exactly the members its kind defines. +type ProofSubmission struct { + // SignedProofs is the JWS-scheme proof set (did:web, did:key, + // and the future did:plc / did:ion): one compact JWS per proven + // key, every payload equal to the served signingInput verbatim. + SignedProofs []string +} + +// controlVerifier is the per-kind control-proof gate — the design's +// §3 centerpiece, one implementation per identifier kind. +type controlVerifier interface { + // Challenges runs the kind's advisory resolution and returns the + // 202 challenge entries, all sharing the served signingInput + // (the input is key-independent; entries enumerate the keys the + // kind could see in advance). Advisory only — verify-time + // resolution is the authoritative key source. + Challenges(ctx context.Context, identity *domain.VerifiedIdentity, signingInput string) ([]ProofChallenge, error) + + // VerifyProofs runs the kind's control proof over the submission + // and returns the proven keys to seal. Every proof must pass — + // one bad proof fails the call closed. Implementations never + // consume the nonce; the service owns nonce/transaction + // discipline so it stays uniform across kinds. + VerifyProofs(ctx context.Context, identity *domain.VerifiedIdentity, sub ProofSubmission, signingInput string) ([]identityevent.ProvenKey, error) +} + +// newControlVerifiers builds the kind registry. lei (vLEI), did:plc, +// did:ion, and did:ethr slot in here when their verifiers are real; +// until then domain.InferIdentifierKind may recognize a value's form +// but the missing registry entry yields IDENTIFIER_KIND_UNSUPPORTED. +func newControlVerifiers(resolver port.DIDResolver) map[domain.IdentifierKind]controlVerifier { + // NOTE: deliberately NOT exhaustive over IdentifierKind — a + // recognized-but-absent kind (lei, until its vlei-verifier + // integration ships) MUST fail with IDENTIFIER_KIND_UNSUPPORTED + // rather than register a stub. The 404-is-the-signal rule. + return map[domain.IdentifierKind]controlVerifier{ //nolint:exhaustive // absence == kind not enabled, by design + domain.KindDIDWeb: &didWebVerifier{resolver: resolver}, + domain.KindDIDKey: &didKeyVerifier{}, + } +} + +// ----- shared JWS proof machinery ----- +// +// Every JWS-scheme kind (did:web, did:key, did:plc, did:ion) shares +// the same proof envelope: standard compact JWSes whose payload +// segment equals the served signingInput verbatim, each naming its +// claimed key by `kid`. What differs per kind is only the +// authoritative key source, expressed as the selectKey callback. + +// jwsProof is one parsed submission entry. +type jwsProof struct { + jws string + header *anscrypto.JWSProtectedHeader +} + +// parseJWSProofs validates the submission envelope: presence, batch +// bound, compact-JWS form, payload equality (BEFORE any signature +// work — clients never canonicalize), kid presence, kid uniqueness. +// Returns the parsed proofs plus the kid → embedded-jwk hints the +// noop resolver synthesizes documents from. +func parseJWSProofs(sub ProofSubmission, signingInput string) ([]jwsProof, []port.KeyHint, error) { + proofs := sub.SignedProofs + if len(proofs) == 0 { + return nil, nil, domain.NewValidationError("IDENTIFIER_PROOF_INVALID", "signedProofs is required") + } + if len(proofs) > maxProofsPerVerify { + return nil, nil, domain.NewValidationError("IDENTIFIER_PROOF_INVALID", + fmt.Sprintf("at most %d proofs per call", maxProofsPerVerify)) + } + + parsed := make([]jwsProof, 0, len(proofs)) + hints := make([]port.KeyHint, 0, len(proofs)) + seenKids := make(map[string]bool, len(proofs)) + for i, jws := range proofs { + header, payloadSeg, err := anscrypto.DecodeStandardJWS(jws) + if err != nil { + return nil, nil, domain.NewValidationError("IDENTIFIER_PROOF_INVALID", + fmt.Sprintf("signedProofs[%d] is not a compact JWS", i)) + } + if payloadSeg != signingInput { + return nil, nil, domain.NewValidationError("PRICC_SIGNATURE_INVALID", + fmt.Sprintf("signedProofs[%d] payload does not equal the served signingInput", i)) + } + if header.Kid == "" { + return nil, nil, domain.NewValidationError("DID_VERIFICATION_METHOD_INVALID", + fmt.Sprintf("signedProofs[%d] names no kid", i)) + } + if seenKids[header.Kid] { + return nil, nil, domain.NewValidationError("IDENTIFIER_PROOF_INVALID", + fmt.Sprintf("signedProofs[%d] duplicates kid %q", i, header.Kid)) + } + seenKids[header.Kid] = true + parsed = append(parsed, jwsProof{jws: jws, header: header}) + hints = append(hints, port.KeyHint{Kid: header.Kid, PublicKeyJWK: header.Jwk}) + } + return parsed, hints, nil +} + +// sealJWSProofs verifies each parsed proof against the key the +// kind's selectKey callback resolves for its kid, and returns the +// proven set: the kind's authoritative verification-method material +// quoted VERBATIM (never a derived, re-encoded, or normalized value) +// plus the registrant's proof. The alg is pinned to the key type +// inside the verifier (alg-confusion defense): ES256 ↔ P-256, +// EdDSA ↔ Ed25519, RS256 ↔ RSA. +func sealJWSProofs( + parsed []jwsProof, + did string, + selectKey func(kid string) (any, json.RawMessage, error), +) ([]identityevent.ProvenKey, error) { + proven := make([]identityevent.ProvenKey, 0, len(parsed)) + for i, p := range parsed { + pub, sealVM, err := selectKey(p.header.Kid) + if err != nil { + return nil, err + } + if _, err := anscrypto.VerifyStandardJWSWithPublicKey(pub, p.jws); err != nil { + return nil, domain.NewValidationError("PRICC_SIGNATURE_INVALID", + fmt.Sprintf("signedProofs[%d] does not verify against %s's key %q", i, did, p.header.Kid)) + } + proven = append(proven, identityevent.ProvenKey{ + VerificationMethod: sealVM, + SignedProof: p.jws, + }) + } + return proven, nil +} + +// ----- did:web ----- + +// didWebVerifier proves control of a did:web identifier: possession +// of one or more keys the DID document lists under assertionMethod, +// any host (design §3.3/§3.6). The document fetch is the kind's only +// I/O and rides the injected resolver port (noop or hardened web). +type didWebVerifier struct { + resolver port.DIDResolver +} + +// Challenges runs the advisory fetch and enumerates the document's +// assertionMethod kids. With an unenumerable key set (the noop +// resolver before any proofs exist, or a document listing none), a +// single unkeyed entry tells the registrant to name keys via the +// JWS `kid` header at verify time. +func (v *didWebVerifier) Challenges(ctx context.Context, identity *domain.VerifiedIdentity, signingInput string) ([]ProofChallenge, error) { + doc, err := v.resolver.Resolve(ctx, identity.EffectiveValue(), nil) + if err != nil { + return nil, err + } + if len(doc.AssertionMethod) == 0 { + return []ProofChallenge{{SigningInput: signingInput}}, nil + } + challenges := make([]ProofChallenge, 0, len(doc.AssertionMethod)) + for _, vm := range doc.AssertionMethod { + challenges = append(challenges, ProofChallenge{Kid: vm.ID, SigningInput: signingInput}) + } + return challenges, nil +} + +// VerifyProofs re-fetches the document authoritatively (the +// verify-time document is the key source — §3.6) and checks each +// proof against its named assertionMethod: the kid must be a +// fragment of THIS DID, the method must carry no cross-controller +// indirection, and the key must parse under the supported types. +// Seals the document's verification-method objects verbatim. +func (v *didWebVerifier) VerifyProofs(ctx context.Context, identity *domain.VerifiedIdentity, sub ProofSubmission, signingInput string) ([]identityevent.ProvenKey, error) { + parsed, hints, err := parseJWSProofs(sub, signingInput) + if err != nil { + return nil, err + } + did := identity.EffectiveValue() + doc, err := v.resolver.Resolve(ctx, did, hints) + if err != nil { + return nil, err + } + if doc.ID != did { + return nil, domain.NewValidationError("DID_DOCUMENT_ID_MISMATCH", + fmt.Sprintf("resolved document id %q does not match %q", doc.ID, did)) + } + return sealJWSProofs(parsed, did, func(kid string) (any, json.RawMessage, error) { + if !strings.HasPrefix(kid, did+"#") { + return nil, nil, domain.NewValidationError("DID_VERIFICATION_METHOD_INVALID", + fmt.Sprintf("kid %q is not a fragment of %q", kid, did)) + } + vm := doc.FindAssertionMethod(kid) + if vm == nil { + return nil, nil, domain.NewValidationError("DID_VERIFICATION_METHOD_INVALID", + fmt.Sprintf("kid %q is not an assertionMethod of %q", kid, did)) + } + if vm.Controller != "" && vm.Controller != did { + return nil, nil, domain.NewValidationError("DID_VERIFICATION_METHOD_INVALID", + fmt.Sprintf("verification method %q is controlled by %q, not %q", kid, vm.Controller, did)) + } + var pub any + switch { + case len(vm.PublicKeyJwk) > 0: + pub, err = anscrypto.ParseJWK(vm.PublicKeyJwk) + case vm.PublicKeyMultibase != "": + pub, err = anscrypto.DecodeMultibase(vm.PublicKeyMultibase) + default: + return nil, nil, domain.NewValidationError("DID_VERIFICATION_METHOD_INVALID", + fmt.Sprintf("verification method %q carries no key material", kid)) + } + if err != nil { + return nil, nil, domain.NewValidationError("DID_VERIFICATION_METHOD_INVALID", err.Error()) + } + return pub, vm.Raw, nil + }) +} + +// ----- did:key ----- + +// didKeyVerifier proves control of a did:key identifier — the key +// IS the identifier, decoded from the DID string with zero I/O. +// The keyless-future test track (§2.2): any kind whose key material +// is self-contained follows this shape. +type didKeyVerifier struct{} + +// Challenges decodes the DID and returns its single legal kid, +// {did}#{method-specific-id}. +func (v *didKeyVerifier) Challenges(_ context.Context, identity *domain.VerifiedIdentity, signingInput string) ([]ProofChallenge, error) { + _, kid, err := anscrypto.DecodeDIDKey(identity.EffectiveValue()) + if err != nil { + return nil, domain.NewValidationError("DID_BAD_FORMAT", err.Error()) + } + return []ProofChallenge{{Kid: kid, SigningInput: signingInput}}, nil +} + +// VerifyProofs decodes the key from the identifier and verifies the +// (single-key) proof set against it. The sealed verification method +// is the did:key method's derived Multikey entry; its key material +// (publicKeyMultibase) is the method-specific id quoted verbatim +// from the identifier itself. +func (v *didKeyVerifier) VerifyProofs(_ context.Context, identity *domain.VerifiedIdentity, sub ProofSubmission, signingInput string) ([]identityevent.ProvenKey, error) { + parsed, _, err := parseJWSProofs(sub, signingInput) + if err != nil { + return nil, err + } + did := identity.EffectiveValue() + pub, expectedKid, err := anscrypto.DecodeDIDKey(did) + if err != nil { + return nil, domain.NewValidationError("DID_BAD_FORMAT", err.Error()) + } + sealVM, err := json.Marshal(map[string]string{ + "id": expectedKid, + "type": "Multikey", + "controller": did, + "publicKeyMultibase": strings.TrimPrefix(did, "did:key:"), + }) + if err != nil { + return nil, domain.NewInternalError("PROOF_SEAL", "could not build did:key verification method", err) + } + return sealJWSProofs(parsed, did, func(kid string) (any, json.RawMessage, error) { + if kid != expectedKid { + return nil, nil, domain.NewValidationError("DID_VERIFICATION_METHOD_INVALID", + fmt.Sprintf("kid %q is not the did:key verification method %q", kid, expectedKid)) + } + return pub, sealVM, nil + }) +} diff --git a/internal/ra/service/identityratelimit.go b/internal/ra/service/identityratelimit.go new file mode 100644 index 0000000..129a3dd --- /dev/null +++ b/internal/ra/service/identityratelimit.go @@ -0,0 +1,80 @@ +package service + +import ( + "encoding/json" + "sync" + "time" +) + +// defaultRegisterPerMinute is the default per-owner budget for +// identity register/rotate calls. Each call can trigger an outbound +// fetch (did:web advisory resolution) before any proof exists, so +// the budget exists to stop an authenticated owner from turning the +// RA into a fetch proxy (design §3.7 "bounded fetch"). +const defaultRegisterPerMinute = 10 + +// ownerLimiter is a fixed-window per-owner rate limiter. In-process +// and intentionally simple: the window is a minute, the state is one +// counter per owner, and stale owners are pruned opportunistically. +// Deployments needing distributed rate limiting put it in front of +// the RA; this is the in-depth backstop. +type ownerLimiter struct { + mu sync.Mutex + perMinute int + windows map[string]*ownerWindow +} + +type ownerWindow struct { + start time.Time + count int +} + +// newOwnerLimiter constructs a limiter allowing perMinute calls per +// owner per fixed one-minute window. +func newOwnerLimiter(perMinute int) *ownerLimiter { + return &ownerLimiter{ + perMinute: perMinute, + windows: make(map[string]*ownerWindow), + } +} + +// Allow reports whether the owner may proceed at the given instant, +// consuming one slot when it does. +func (l *ownerLimiter) Allow(owner string, now time.Time) bool { + l.mu.Lock() + defer l.mu.Unlock() + + w, ok := l.windows[owner] + if !ok || now.Sub(w.start) >= time.Minute { + l.prune(now) + l.windows[owner] = &ownerWindow{start: now, count: 1} + return true + } + if w.count >= l.perMinute { + return false + } + w.count++ + return true +} + +// prune drops windows older than two minutes so the map stays +// bounded by the set of recently-active owners. Called with the lock +// held, only on the window-rollover path (amortized — not per call). +func (l *ownerLimiter) prune(now time.Time) { + for owner, w := range l.windows { + if now.Sub(w.start) >= 2*time.Minute { + delete(l.windows, owner) + } + } +} + +// marshalOutboxPayload renders the {innerEventCanonical, +// producerSignature} outbox payload — the bytes the worker replays +// verbatim. Shared by every event family; the inner canonical bytes +// are family-specific, the payload wrapper is not. +func marshalOutboxPayload(innerCanonical []byte, producerSig string) ([]byte, error) { + return json.Marshal(OutboxPayload{ + InnerEventCanonical: json.RawMessage(innerCanonical), + ProducerSignature: producerSig, + }) +} diff --git a/spec/api-spec-v2.yaml b/spec/api-spec-v2.yaml index 9ad2749..98073c1 100644 --- a/spec/api-spec-v2.yaml +++ b/spec/api-spec-v2.yaml @@ -59,6 +59,12 @@ tags: description: Agent certificate revocation operations - name: Certificate Management description: Certificate retrieval, CSR submission, and renewal operations + - name: Verified Identities + description: | + The "who" behind agents — owner-level identities (did:web, + did:key) proven through challenge-bound control proofs, sealed + on their own Transparency Log stream, and linked to the owner's + agents # Global security requirement. Every path inherits this and can # override with `security: []` to declare itself anonymous. The @@ -889,6 +895,372 @@ paths: schema: $ref: '#/components/schemas/ErrorResponse' + # ────────────────────────────────────────────────────────────────── + # Verified Identities — the "who" behind agents + # + # An identity is a first-class object owned by the authenticated + # principal (the same owner as the agents), proven through a + # per-kind control proof, sealed onto its own Transparency Log + # stream, and linked to any number of that owner's agents. The + # agent registration surface above is unchanged — agents carry no + # identity fields; the association is the links sub-resource. + # + # The single security invariant: the RA seals an identity + # attestation only after control is PROVEN — a challenge-bound + # signature verified against the identifier's authoritative keys — + # never on resolution alone. + # ────────────────────────────────────────────────────────────────── + /ans/identities: + post: + tags: + - Verified Identities + summary: Register a verified identity + description: | + Registers an identifier (the kind — `did:web`, `did:key` — is + inferred from the value's lexical form, never caller-asserted) + and returns the challenge round to sign: one entry per + eligible key, all over a single anti-replay nonce. The + identity is created in `PENDING_CONTROL`; nothing is sealed + until verify-control passes. + + Re-POSTing the same value while the row is `PENDING_CONTROL` + is the idempotent re-add: the same `identityId` returns with + a fresh nonce (the prior nonce is superseded). A value + already verified — by this owner or any other — returns 409 + `IDENTIFIER_DUPLICATE`. Kinds without an enabled control + verifier (`lei`, postponed) return 422 + `IDENTIFIER_KIND_UNSUPPORTED`. + operationId: registerIdentity + requestBody: + required: true + content: + application/json: + schema: + $ref: '#/components/schemas/IdentityRegistrationRequest' + responses: + '202': + description: Identity registered — challenges to sign + content: + application/json: + schema: + $ref: '#/components/schemas/IdentityChallengeResponse' + '401': + description: Authentication failed + content: + application/json: + schema: + $ref: '#/components/schemas/ErrorResponse' + '409': + description: Identifier already verified (IDENTIFIER_DUPLICATE) + content: + application/json: + schema: + $ref: '#/components/schemas/ErrorResponse' + '422': + description: Invalid or unsupported identifier + content: + application/json: + schema: + $ref: '#/components/schemas/ErrorResponse' + get: + tags: + - Verified Identities + summary: List my identities + description: Returns every identity owned by the caller, newest first. + operationId: listIdentities + responses: + '200': + description: The caller's identities + content: + application/json: + schema: + type: object + properties: + identities: + type: array + items: + $ref: '#/components/schemas/IdentityDetails' + '401': + description: Authentication failed + content: + application/json: + schema: + $ref: '#/components/schemas/ErrorResponse' + + /ans/identities/{identityId}: + get: + tags: + - Verified Identities + summary: Get identity detail + description: | + Returns the identity plus its live links. Ownership-scoped: + an identity that doesn't exist or isn't the caller's returns + 404 (existence is hidden). + operationId: getIdentity + parameters: + - $ref: '#/components/parameters/IdentityIdPath' + responses: + '200': + description: Identity detail + content: + application/json: + schema: + $ref: '#/components/schemas/IdentityDetails' + '404': + description: Identity not found or not accessible + content: + application/json: + schema: + $ref: '#/components/schemas/ErrorResponse' + put: + tags: + - Verified Identities + summary: Rotate / replace the identifier + description: | + Stages a same-kind replacement and returns fresh challenges + over it. Until the new proof lands the previously sealed + state stands; a replacement that never verifies expires with + its nonce. On a clean verify-control the RA swaps the value + and seals ONE `IDENTITY_UPDATED` event — regardless of how + many agents are linked. Cross-kind replacement is rejected + (revoke and register a new identity instead). + operationId: rotateIdentity + parameters: + - $ref: '#/components/parameters/IdentityIdPath' + requestBody: + required: true + content: + application/json: + schema: + $ref: '#/components/schemas/IdentityRegistrationRequest' + responses: + '202': + description: Rotation staged — fresh challenges to sign + content: + application/json: + schema: + $ref: '#/components/schemas/IdentityChallengeResponse' + '403': + description: Caller does not own this identity + content: + application/json: + schema: + $ref: '#/components/schemas/ErrorResponse' + '404': + description: Identity not found + content: + application/json: + schema: + $ref: '#/components/schemas/ErrorResponse' + '409': + description: Identity is not in a rotatable state + content: + application/json: + schema: + $ref: '#/components/schemas/ErrorResponse' + '422': + description: Invalid replacement value (incl. IDENTIFIER_KIND_MISMATCH) + content: + application/json: + schema: + $ref: '#/components/schemas/ErrorResponse' + + /ans/identities/{identityId}/verify-control: + post: + tags: + - Verified Identities + summary: Prove control of the identifier + description: | + Submits the control proofs: one compact JWS per proven key, + each payload equal — verbatim — to the served `signingInput` + (clients never canonicalize; the RA checks payload equality + before verifying any signature). Every proof must verify + against the identifier's AUTHORITATIVE key for its `kid` + (resolved from the DID document for `did:web`, decoded from + the identifier for `did:key`); one bad proof fails the call + closed. The nonce is consumed exactly once, inside the + success transaction — a failed attempt does not consume it. + + On success the identity flips to `VERIFIED` (or completes a + staged rotation) and the RA seals `IDENTITY_VERIFIED` / + `IDENTITY_UPDATED` on the identity's own Transparency Log + stream, with every proven key sealed self-verifyingly + (public key + signed proof). + operationId: verifyIdentityControl + parameters: + - $ref: '#/components/parameters/IdentityIdPath' + requestBody: + required: true + content: + application/json: + schema: + $ref: '#/components/schemas/VerifyControlRequest' + responses: + '200': + description: Control proven — identity VERIFIED, event sealed + content: + application/json: + schema: + $ref: '#/components/schemas/IdentityDetails' + '403': + description: Caller does not own this identity + content: + application/json: + schema: + $ref: '#/components/schemas/ErrorResponse' + '404': + description: Identity not found + content: + application/json: + schema: + $ref: '#/components/schemas/ErrorResponse' + '409': + description: | + Challenge state error — `PRICC_TOKEN_EXPIRED`, + `PRICC_TOKEN_ALREADY_USED`, `IDENTIFIER_CHALLENGE_EXPIRED` + — or the identity is revoked. Recovery from an expired + nonce is the idempotent re-add (re-POST the same value). + content: + application/json: + schema: + $ref: '#/components/schemas/ErrorResponse' + '422': + description: | + Proof rejected — `PRICC_SIGNATURE_INVALID`, + `DID_VERIFICATION_METHOD_INVALID`, `DID_RESOLUTION_FAILED`, + `DID_DOCUMENT_ID_MISMATCH`, `IDENTIFIER_PROOF_INVALID`. + content: + application/json: + schema: + $ref: '#/components/schemas/ErrorResponse' + + /ans/identities/{identityId}/revoke: + post: + tags: + - Verified Identities + summary: Revoke the identity + description: | + Transitions a VERIFIED identity to REVOKED and seals ONE + `IDENTITY_REVOKED` event. A POST (state change), never a + DELETE: an identity cannot be deleted — its history is + append-only in the Transparency Log. Propagation to every + linked agent's badge is the TL's read-time join, not a write + fan-out. + operationId: revokeIdentity + parameters: + - $ref: '#/components/parameters/IdentityIdPath' + responses: + '200': + description: Identity revoked, event sealed + content: + application/json: + schema: + $ref: '#/components/schemas/IdentityDetails' + '403': + description: Caller does not own this identity + content: + application/json: + schema: + $ref: '#/components/schemas/ErrorResponse' + '404': + description: Identity not found + content: + application/json: + schema: + $ref: '#/components/schemas/ErrorResponse' + '409': + description: Identity is not VERIFIED (nothing sealed to revoke) + content: + application/json: + schema: + $ref: '#/components/schemas/ErrorResponse' + + /ans/identities/{identityId}/links: + post: + tags: + - Verified Identities + summary: Link agents to the identity + description: | + Binds a batch of the caller's agents to the identity — a + single owner-gated call with no challenge and no signature: + the caller MUST own the identity AND every named agent (key + possession never authorizes a link). The whole batch seals as + ONE `IDENTITY_LINKED` event on the IDENTITY stream carrying + the agent ids; an agent's own stream and audit history are + never written by identity operations. Already-linked agents + are skipped idempotently; a call that links nothing new seals + nothing. Links attach only while the identity is VERIFIED. + operationId: linkIdentityAgents + parameters: + - $ref: '#/components/parameters/IdentityIdPath' + requestBody: + required: true + content: + application/json: + schema: + $ref: '#/components/schemas/IdentityLinkRequest' + responses: + '200': + description: Batch linked (count of newly-created links) + content: + application/json: + schema: + $ref: '#/components/schemas/IdentityLinkResponse' + '403': + description: Caller does not own this identity + content: + application/json: + schema: + $ref: '#/components/schemas/ErrorResponse' + '404': + description: Identity, or a named agent, not found / not the caller's + content: + application/json: + schema: + $ref: '#/components/schemas/ErrorResponse' + '409': + description: Identity is not VERIFIED (IDENTITY_NOT_VERIFIED) + content: + application/json: + schema: + $ref: '#/components/schemas/ErrorResponse' + '422': + description: Invalid link request (empty or oversized batch) + content: + application/json: + schema: + $ref: '#/components/schemas/ErrorResponse' + + /ans/identities/{identityId}/links/{agentId}: + delete: + tags: + - Verified Identities + summary: Unlink an agent + description: | + Ends one association and seals `IDENTITY_UNLINKED` on the + identity stream. The association's history persists in the + identity's audit chain and the raw log tiles; unlinked pairs + may be re-linked later. + operationId: unlinkIdentityAgent + parameters: + - $ref: '#/components/parameters/IdentityIdPath' + - $ref: '#/components/parameters/AgentIdPath' + responses: + '204': + description: Link removed, event sealed + '403': + description: Caller does not own this identity + content: + application/json: + schema: + $ref: '#/components/schemas/ErrorResponse' + '404': + description: Identity or live link not found + content: + application/json: + schema: + $ref: '#/components/schemas/ErrorResponse' + components: # ────────────────────────────────────────────────────────────────── @@ -923,6 +1295,14 @@ components: type: string format: uuid + IdentityIdPath: + name: identityId + in: path + description: Unique identifier of the verified identity (UUIDv7) + required: true + schema: + type: string + # ────────────────────────────────────────────────────────────────── # Schemas # ────────────────────────────────────────────────────────────────── @@ -1155,6 +1535,16 @@ components: type: array items: $ref: '#/components/schemas/Link' + identities: + type: array + description: | + Additive, optional, COMPUTED — the verified identities + currently linked to this agent, joined from the link rows + at read time. Never stored on the registration; identity + rotation/revocation is visible here immediately with zero + agent-side writes. + items: + $ref: '#/components/schemas/LinkedIdentity' required: - agentId - ansName @@ -1585,4 +1975,209 @@ components: found: type: string expected: - type: string \ No newline at end of file + type: string + + # ──────────────────────────────────────────────────────────────── + # Verified Identities + # ──────────────────────────────────────────────────────────────── + IdentityRegistrationRequest: + type: object + description: | + Registers (POST) or rotates (PUT) an identifier. The kind is + inferred from the value's lexical form — `did:web:` prefix, + `did:key:` prefix, or a 20-character LEI (recognized but + postponed) — never caller-asserted. + properties: + value: + type: string + description: The identifier to prove control of + example: did:web:identity.acme-corp.com + required: + - value + + IdentityChallengeResponse: + type: object + description: | + The 202 challenge round. Every entry shares the same + anti-replay nonce and the same signingInput — the input is + key-independent; entries enumerate the keys the resolver + could see in advance (a single unkeyed entry when it could + not — name keys via the JWS `kid` header at verify time). + properties: + identityId: + type: string + description: RA-assigned UUIDv7 — the TL stream key + kind: + type: string + enum: ['did:web', 'did:key', 'lei'] + value: + type: string + description: The canonical identifier this round proves + status: + $ref: '#/components/schemas/IdentityLifecycleStatus' + nonce: + type: string + description: Base64url 32-byte single-use anti-replay nonce + expiresAt: + type: string + format: date-time + challenges: + type: array + items: + $ref: '#/components/schemas/IdentityProofChallenge' + required: + - identityId + - kind + - value + - status + - nonce + - expiresAt + - challenges + + IdentityProofChallenge: + type: object + properties: + kid: + type: string + description: | + Verification-method id eligible to sign this round + (omitted when the resolver could not enumerate keys) + example: did:web:identity.acme-corp.com#key-1 + signingInput: + type: string + description: | + Base64url of the exact RFC 8785 (JCS) canonical + IdentityProofInput bytes — {identifier, identityId, + nonce, purpose:"ans:identity-proof:v1", raId, scheme}. A + compact JWS's payload segment MUST equal this string + verbatim; clients never canonicalize. + required: + - signingInput + + VerifyControlRequest: + type: object + description: | + One compact JWS per proven key. Supported algorithms match + what the verifier implements: EdDSA (Ed25519), ES256 + (ECDSA P-256), and RS256 (RSA >= 2048). Key-agreement keys + (X25519) and curves without a verifier (secp256k1, + P-384/521) are rejected with a precise error. + properties: + signedProofs: + type: array + minItems: 1 + maxItems: 16 + items: + type: string + description: | + Compact JWS over the served signingInput. The protected + header carries `kid` (the claimed verification method) + and MAY carry `jwk` (the signer's public key — required + by the quickstart noop resolver, ignored by the web + resolver, which always uses the resolved document). + required: + - signedProofs + + IdentityLifecycleStatus: + type: string + enum: [PENDING_CONTROL, VERIFIED, REVOKED] + description: | + PENDING_CONTROL → VERIFIED → REVOKED. Rotation keeps the row + VERIFIED (the staged replacement proves control before + anything changes). + + IdentityDetails: + type: object + properties: + identityId: + type: string + kind: + type: string + enum: ['did:web', 'did:key', 'lei'] + value: + type: string + status: + $ref: '#/components/schemas/IdentityLifecycleStatus' + proofMethod: + type: string + description: Control proof that verified this identity + enum: [did-web-sig, did-key-sig, lei-vlei-acdc] + pendingValue: + type: string + description: Staged rotation replacement (empty unless rotating) + verifiedAt: + type: string + format: date-time + createdAt: + type: string + format: date-time + linkedAgents: + type: array + description: Live links (detail responses only) + items: + type: object + properties: + agentId: + type: string + format: uuid + linkedAt: + type: string + format: date-time + required: + - agentId + required: + - identityId + - kind + - value + - status + - createdAt + + IdentityLinkRequest: + type: object + description: | + The batch of the caller's agents to bind — one owner-gated + call, no challenge, no signature; the whole batch seals as + ONE IDENTITY_LINKED event on the identity stream. + properties: + agentIds: + type: array + minItems: 1 + maxItems: 256 + items: + type: string + format: uuid + required: + - agentIds + + IdentityLinkResponse: + type: object + properties: + linked: + type: integer + description: Newly-created links (already-linked agents are skipped) + required: + - linked + + LinkedIdentity: + type: object + description: One computed identities[] entry on AgentDetails. + properties: + identityId: + type: string + kind: + type: string + enum: ['did:web', 'did:key', 'lei'] + value: + type: string + identityStatus: + type: string + enum: [VERIFIED, REVOKED] + description: The identity's CURRENT status — reflects its stream now + linkedAt: + type: string + format: date-time + required: + - identityId + - kind + - value + - identityStatus \ No newline at end of file From d32f1b3e7dd2214c25dcfb3f665a689580699a1c Mon Sep 17 00:00:00 2001 From: Connor Snitker Date: Wed, 10 Jun 2026 18:10:17 -0500 Subject: [PATCH 03/12] feat(demo): identity lifecycle demos + registrant-side signproof helper MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit scripts/demo/identity-lifecycle.sh exercises EVERY identity operation against the live stack: register → idempotent re-add → multi-key verify-control (one ES256 + one EdDSA proof over the same nonce, both verification methods sealed verbatim) → an Ed25519 did:key registered, proven, and linked alongside the did:web (one agent carrying two identities) → a two-agent batch link sealing ONE IDENTITY_LINKED with ansIds[2] → RA + TL computed views in both directions → rotation (proven set 2→1, visible on every linked badge from one sealed event) → identity SCITT receipt → per-pair unlink (agent keeps its other identity) → revocation propagating to the still-linked badge at read time while agents stay ACTIVE. run-lifecycle.sh gains steps 16-19: register the who, prove control, link the agent, and read the badge's identities[] join — one hop answers "who is behind this agent". scripts/demo/signproof is the registrant-side tool (keys minted and proofs signed locally; private keys never touch the RA): keygen emits did:key identifiers for P-256 or Ed25519 keypairs, sign emits the compact JWS with kid + embedded jwk headers, alg following the key type. The coverage gate now excludes scripts/* alongside cmd/* — demo tooling is exercised by the end-to-end lifecycle runs, not unit tests. Signed-off-by: Connor Snitker --- Makefile | 18 +- config/ra-local.yaml | 23 ++ scripts/demo/common.sh | 23 ++ scripts/demo/identity-lifecycle.sh | 368 +++++++++++++++++++++++++++++ scripts/demo/run-lifecycle.sh | 51 ++++ scripts/demo/signproof/main.go | 190 +++++++++++++++ scripts/demo/start.sh | 9 + 7 files changed, 674 insertions(+), 8 deletions(-) create mode 100755 scripts/demo/identity-lifecycle.sh create mode 100644 scripts/demo/signproof/main.go diff --git a/Makefile b/Makefile index e16bdb9..d93b801 100644 --- a/Makefile +++ b/Makefile @@ -63,14 +63,16 @@ test: test-cover: @echo "Running tests with coverage..." - @# Exclude cmd/* from the instrumented set. The three command - @# binaries (ans-ra, ans-tl, ans-verify) are thin glue: flag - @# parsing, config loading, dependency wiring, then hand off to - @# library code under internal/. We don't write unit tests for - @# main() — counting those ~30 unexercised statements toward the - @# 90% gate would only penalize real logic coverage. The library - @# packages under internal/ are where the gate has teeth. - @pkgs=$$(go list ./... | grep -v '/cmd/' | tr '\n' ',' | sed 's/,$$//'); \ + @# Exclude cmd/* and scripts/* from the instrumented set. The + @# command binaries (ans-ra, ans-tl, ans-verify) are thin glue: + @# flag parsing, config loading, dependency wiring, then hand off + @# to library code under internal/. scripts/* holds demo-side + @# tooling (e.g. the signproof helper) exercised end-to-end by + @# the scripts/demo lifecycle runs, not unit tests. Counting + @# either's unexercised main() statements toward the 90% gate + @# would only penalize real logic coverage. The library packages + @# under internal/ are where the gate has teeth. + @pkgs=$$(go list ./... | grep -v -e '/cmd/' -e '/scripts/' | tr '\n' ',' | sed 's/,$$//'); \ go test ./... -count=1 -coverpkg=$$pkgs -coverprofile=coverage.out -covermode=atomic @go tool cover -func=coverage.out @echo "" diff --git a/config/ra-local.yaml b/config/ra-local.yaml index f4e67b2..cef22ed 100644 --- a/config/ra-local.yaml +++ b/config/ra-local.yaml @@ -25,6 +25,29 @@ dns: type: noop # server: "127.0.0.1:15353" +identity: + # Verified identities — the "who" behind agents, proven through a + # challenge-bound key proof and sealed on their own TL stream. + # The did:web resolver mirrors the DNS verifier's noop/real split: + # "noop" — no I/O; the DID document is synthesized from the keys + # embedded in the submitted proofs' `jwk` headers. + # Signature verification still genuinely runs; only the + # "does the live did.json list this key" binding is + # waived. Quickstart only — NOT for production. + # "web" — hardened HTTPS fetch of the DID document: WebPKI + # validation, SSRF dialer guards, 5s timeout, bounded + # same-site redirects. + # did:key identities never resolve anything — the key decodes from + # the identifier itself, so they work fully in both modes. + resolver: + type: noop + # challenge-ttl bounds the verify-control nonce (default 1h; 5m is + # the recommended floor for high-assurance deployments). + challenge-ttl: 1h + # register-rate-limit caps per-owner register/rotate calls per + # minute (each can trigger an outbound did:web fetch). + register-rate-limit: 10 + keys: type: file file: diff --git a/scripts/demo/common.sh b/scripts/demo/common.sh index 4533604..63258cb 100755 --- a/scripts/demo/common.sh +++ b/scripts/demo/common.sh @@ -317,6 +317,29 @@ poll_tl_audit() { fail "TL not ready for $agent_id in ${timeout}s (records=${count:-0}, with merkle proof=${withproof:-0}; want $expected of each)" } +# poll_tl_identity_audit IDENTITY_ID EXPECTED_COUNT [TIMEOUT_SECONDS] +# +# Sibling of poll_tl_audit for the identity stream: polls +# /v1/identities/{identityId}/audit until it shows at least +# EXPECTED_COUNT events with Merkle proofs. +poll_tl_identity_audit() { + local identity_id="$1" expected="$2" timeout="${3:-30}" + local i=0 count withproof + while [ "$i" -lt "$timeout" ]; do + local resp + resp=$(curl -sSf -H "Authorization: Bearer $TL_API_KEY" \ + "$TL_URL/v1/identities/$identity_id/audit" 2>/dev/null || true) + count=$(printf '%s' "$resp" | jq -r '(.records | length) // 0') + withproof=$(printf '%s' "$resp" | jq -r '[.records[]? | select(.merkleProof)] | length // 0') + if [ "${count:-0}" -ge "$expected" ] && [ "${withproof:-0}" -ge "$expected" ]; then + return 0 + fi + sleep 1 + i=$((i + 1)) + done + fail "TL not ready for identity $identity_id in ${timeout}s (records=${count:-0}, with merkle proof=${withproof:-0}; want $expected of each)" +} + # wait_ready URL [TIMEOUT_SECONDS] # # Polls URL once a second until it returns 200, or TIMEOUT expires. diff --git a/scripts/demo/identity-lifecycle.sh b/scripts/demo/identity-lifecycle.sh new file mode 100755 index 0000000..faf01d2 --- /dev/null +++ b/scripts/demo/identity-lifecycle.sh @@ -0,0 +1,368 @@ +#!/usr/bin/env bash +# Exercise EVERY Verified Identity operation end-to-end, printing +# every request + response pair in color. The identity is the "who" +# behind an agent (the "what"): proven once through a control proof, +# sealed onto its own Transparency Log stream, and linked to any +# number of the owner's agents. +# +# 1. POST /v2/ans/identities register did:web → 202 + challenges +# 2. POST /v2/ans/identities idempotent re-add (same identityId, fresh nonce) +# 3. POST .../verify-control MULTI-KEY proof — one ES256 + one EdDSA JWS over the +# same nonce → VERIFIED, seals IDENTITY_VERIFIED with +# BOTH verification methods quoted verbatim +# 4. GET /v2/ans/identities list (mine) +# 5. GET /v2/ans/identities/{id} detail +# 6. POST /v2/ans/identities register did:key (zero-I/O kind) +# 7. POST .../verify-control did:key proof → VERIFIED +# 8. (register TWO fresh agents to ACTIVE — the fleet) +# 9. POST .../links did:web → BOTH agents in one call (ONE IDENTITY_LINKED, +# ansIds[2]); did:key → agent #1 (its own stream's event) +# — one agent now carries TWO identities +# 10. GET /v2/ans/agents/{agentId} RA detail now carries computed identities[] +# 11. TL /v1/identities/{id} + /audit + /agents both identity badges (did:web AND did:key Multikey +# seal), chains, reverse joins +# 12. TL /v1/agents/{agentId} agent-1 badge shows identities[2] (did:web + did:key); +# agent-2 shows the did:web +# 13. TL /v1/agents/{agentId}/identities{,/history} agent-side computed views +# 14. PUT /v2/ans/identities/{id} rotate → fresh challenges +# 15. POST .../verify-control new key proof → seals ONE IDENTITY_UPDATED +# (proven set goes 2 keys → 1 key) +# 16. TL both linked badges reflect the rotation — ONE event, zero agent-stream writes +# 17. TL /v1/identities/{id}/receipt SCITT COSE receipt for the identity leaf +# 18. DELETE .../links/{agentId} unlink the did:web from agent #1 ONLY → agent #1 +# keeps its did:key link; agent #2 keeps the did:web +# 19. POST /v2/ans/identities/{id}/revoke revoke the did:web → IDENTITY_REVOKED (one event) +# 20. TL did:web badge REVOKED — agent-2's join shows it at the next read; agent-1's did:key +# stays VERIFIED; both agents stay ACTIVE (the whats survive the who) +# +# The demo runs against the noop did:web resolver (the default — +# `identity.resolver.type: noop`): signature verification is real, +# only the "does the live did.json list this key" binding is waived. +# Point `identity.resolver.type: web` at a real deployment and the +# same flow works against a hosted did.json. did:key (step 6-7) uses +# real key decoding either way — zero I/O by construction. +# +# Usage: +# scripts/demo/identity-lifecycle.sh # random did:web value +# scripts/demo/identity-lifecycle.sh acme.test # did:web:acme.test + +set -euo pipefail +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +# shellcheck source=common.sh +source "$SCRIPT_DIR/common.sh" + +require_cmd jq +require_cmd openssl +require_cmd go + +DID_HOST="${1:-identity-$(openssl rand -hex 4).example.com}" +DID_WEB="did:web:${DID_HOST}" + +header "Demo target" +printf " identity %s\n" "$DID_WEB" >&2 +printf " resolver noop (quickstart) — real JWS verification, waived live-document binding\n" >&2 + +if ! curl -sSf "$RA_URL/v2/admin/ready" >/dev/null 2>&1; then + fail "ans-ra isn't reachable at $RA_URL — run scripts/demo/start.sh first" +fi + +KEY_DIR="$DATA/identity-keys" +rm -rf "$KEY_DIR" +mkdir -p "$KEY_DIR" + +# signproof is the registrant-side tool: keys are minted and proofs +# are signed locally; the private key never touches the RA. +signproof() { (cd "$ROOT" && go run ./scripts/demo/signproof "$@"); } + +# register_agent HOST — drive a fresh agent to ACTIVE and echo its +# agentId on stdout. The identity demo needs a small fleet to link. +register_agent() { + local host="$1" + local ans_name="ans://v1.0.0.${host}" + local csr_dir="$DATA/identity-agent-csr/$host" + rm -rf "$csr_dir" && mkdir -p "$csr_dir" + cat >"$csr_dir/openssl.cnf" </dev/null + openssl req -new -key "$csr_dir/key.pem" -config "$csr_dir/openssl.cnf" -out "$csr_dir/csr.pem" 2>/dev/null + cat >"$csr_dir/server.cnf" </dev/null + openssl req -new -key "$csr_dir/server.key" -config "$csr_dir/server.cnf" -out "$csr_dir/server.csr" 2>/dev/null + + local resp agent_id + resp=$(curl_json POST /v2/ans/agents "$(jq -n \ + --arg host "$host" \ + --arg csr "$(cat "$csr_dir/csr.pem")" \ + --arg srv "$(cat "$csr_dir/server.csr")" ' + { agentDisplayName: "identity-demo-agent", + version: "1.0.0", + agentHost: $host, + endpoints: [{agentUrl: ("https://" + $host + "/mcp"), protocol: "MCP", transports: ["SSE"]}], + identityCsrPEM: $csr, + serverCsrPEM: $srv }')") + agent_id=$(printf '%s' "$resp" | jq -r '.agentId // empty') + [ -n "$agent_id" ] || fail "agent registration failed for $host" + curl_json POST "/v2/ans/agents/$agent_id/verify-acme" >/dev/null + assert_2xx "verify-acme ($host)" + if [ -n "${ANS_DNS_ZONE:-}" ] && [ -x "$BIN/ans-dns" ]; then + "$BIN/ans-dns" install --zone "$ANS_DNS_ZONE" --api-key "$RA_API_KEY" "$RA_URL" "$agent_id" + fi + curl_json POST "/v2/ans/agents/$agent_id/verify-dns" >/dev/null + assert_2xx "verify-dns ($host)" + printf '%s' "$agent_id" +} + +header "0. Mint the operator's identity keypairs (locally — the RA never sees them)" +signproof keygen -out "$KEY_DIR/who-p256.pem" >/dev/null +signproof keygen -alg ed25519 -out "$KEY_DIR/who-ed25519.pem" >/dev/null +ok "two keys minted: P-256 (ES256 proofs) + Ed25519 (EdDSA proofs)" + +# ----- 1. Register the did:web identity ----- +header "1. POST /v2/ans/identities (register $DID_WEB → 202 + challenges)" +REG_RESP=$(curl_json POST /v2/ans/identities "$(jq -n --arg v "$DID_WEB" '{value: $v}')") +IDENTITY_ID=$(printf '%s' "$REG_RESP" | jq -r '.identityId // empty') +SIGNING_INPUT=$(printf '%s' "$REG_RESP" | jq -r '.challenges[0].signingInput') +NONCE_1=$(printf '%s' "$REG_RESP" | jq -r '.nonce') +[ -n "$IDENTITY_ID" ] || fail "no identityId in register response" +echo "$IDENTITY_ID" >"$DATA/last-identity-id" +ok "identityId=$IDENTITY_ID (PENDING_CONTROL)" + +# ----- 2. Idempotent re-add ----- +header "2. POST /v2/ans/identities (re-add while PENDING_CONTROL → same identityId, fresh nonce)" +READD_RESP=$(curl_json POST /v2/ans/identities "$(jq -n --arg v "$DID_WEB" '{value: $v}')") +READD_ID=$(printf '%s' "$READD_RESP" | jq -r '.identityId // empty') +NONCE_2=$(printf '%s' "$READD_RESP" | jq -r '.nonce') +[ "$READD_ID" = "$IDENTITY_ID" ] || fail "re-add returned a different identityId" +[ "$NONCE_1" != "$NONCE_2" ] || fail "re-add did not supersede the nonce" +SIGNING_INPUT=$(printf '%s' "$READD_RESP" | jq -r '.challenges[0].signingInput') +ok "same identityId, superseded nonce — the §4.2 idempotent re-add" + +# ----- 3. Verify control — MULTI-KEY ----- +header "3. POST /v2/ans/identities/$IDENTITY_ID/verify-control (TWO keys, one nonce → seals both verbatim)" +note "did:web supports multi-key attestation: one JWS per key over the SAME signingInput" +note " #key-1 → P-256 / ES256; #key-2 → Ed25519 / EdDSA" +PROOF_1=$(signproof sign -key "$KEY_DIR/who-p256.pem" -kid "${DID_WEB}#key-1" -input "$SIGNING_INPUT") +PROOF_2=$(signproof sign -key "$KEY_DIR/who-ed25519.pem" -kid "${DID_WEB}#key-2" -input "$SIGNING_INPUT") +curl_json POST "/v2/ans/identities/$IDENTITY_ID/verify-control" \ + "$(jq -n --arg p1 "$PROOF_1" --arg p2 "$PROOF_2" '{signedProofs: [$p1, $p2]}')" >/dev/null +assert_2xx "multi-key verify-control" +ok "BOTH keys proven against one nonce — every proof must pass (one bad proof fails the call closed)" + +# ----- 4. List ----- +header "4. GET /v2/ans/identities (list mine)" +curl_json GET /v2/ans/identities >/dev/null + +# ----- 5. Detail ----- +header "5. GET /v2/ans/identities/$IDENTITY_ID (detail)" +curl_json GET "/v2/ans/identities/$IDENTITY_ID" >/dev/null + +# ----- 6-7. did:key — the zero-I/O kind ----- +header "6. POST /v2/ans/identities (register an Ed25519 did:key — the key IS the identifier)" +DID_KEY=$(signproof keygen -alg ed25519 -out "$KEY_DIR/didkey.pem") +note "minted $DID_KEY (z6Mk… = ed25519-pub; proofs use EdDSA)" +DK_RESP=$(curl_json POST /v2/ans/identities "$(jq -n --arg v "$DID_KEY" '{value: $v}')") +DK_ID=$(printf '%s' "$DK_RESP" | jq -r '.identityId // empty') +[ -n "$DK_ID" ] || fail "did:key register failed" +DK_KID=$(printf '%s' "$DK_RESP" | jq -r '.challenges[0].kid') +DK_INPUT=$(printf '%s' "$DK_RESP" | jq -r '.challenges[0].signingInput') + +header "7. POST /v2/ans/identities/$DK_ID/verify-control (did:key proof — real crypto, zero I/O)" +DK_PROOF=$(signproof sign -key "$KEY_DIR/didkey.pem" -kid "$DK_KID" -input "$DK_INPUT") +curl_json POST "/v2/ans/identities/$DK_ID/verify-control" \ + "$(jq -n --arg p "$DK_PROOF" '{signedProofs: [$p]}')" >/dev/null +assert_2xx "did:key verify-control" +ok "did:key identity VERIFIED — the keyless-future test track" + +# ----- 8. Register a small fleet (the WHATs) ----- +header "8. Register TWO fresh agents to ACTIVE (the fleet to link)" +AGENT_1=$(register_agent "linked-a-$(openssl rand -hex 4).example.com") +AGENT_2=$(register_agent "linked-b-$(openssl rand -hex 4).example.com") +ok "fleet ready: $AGENT_1 + $AGENT_2" + +# ----- 9. Link the fleet — one owner-gated call per identity ----- +header "9a. POST /v2/ans/identities/$IDENTITY_ID/links (did:web → BOTH agents, one call)" +LINK_RESP=$(curl_json POST "/v2/ans/identities/$IDENTITY_ID/links" \ + "$(jq -n --arg a "$AGENT_1" --arg b "$AGENT_2" '{agentIds: [$a, $b]}')") +LINKED_COUNT=$(printf '%s' "$LINK_RESP" | jq -r '.linked // 0') +[ "$LINKED_COUNT" = "2" ] || fail "expected linked: 2, got $LINKED_COUNT" +ok "linked: 2 — ONE IDENTITY_LINKED carries the whole batch; fleet linking is O(1) sealed events" + +header "9b. POST /v2/ans/identities/$DK_ID/links (did:key → agent #1 — a second WHO on the same agent)" +note "an agent legitimately carries several identities; each link seals on ITS identity's stream" +DK_LINK=$(curl_json POST "/v2/ans/identities/$DK_ID/links" \ + "$(jq -n --arg a "$AGENT_1" '{agentIds: [$a]}')") +[ "$(printf '%s' "$DK_LINK" | jq -r '.linked // 0')" = "1" ] || fail "did:key link failed" +ok "agent-1 now carries TWO identities: the multi-key did:web and the Ed25519 did:key" + +# ----- 10. RA-side computed identities[] ----- +header "10. GET /v2/ans/agents/$AGENT_1 (RA detail — computed identities[], never stored on the agent)" +curl_json GET "/v2/ans/agents/$AGENT_1" >/dev/null + +# ----- 11. TL identity stream ----- +header "11. Wait for the outbox worker, then read the identity stream from the TL" +poll_tl_identity_audit "$IDENTITY_ID" 2 30 +ok "TL sealed the IDENTITY_VERIFIED + IDENTITY_LINKED leaves" + +header "11a. TL: GET /v1/identities/$IDENTITY_ID (identity badge — latest sealed event + proof + status)" +curl_tl GET "/v1/identities/$IDENTITY_ID" >/dev/null + +header "11b. TL: GET /v1/identities/$IDENTITY_ID/audit (the WHO's full chain — standard audit envelope)" +AUDIT=$(curl_tl GET "/v1/identities/$IDENTITY_ID/audit") +# The multi-key proof sealed BOTH verification methods verbatim, and +# the batch link sealed ONE event naming both agents. +SEALED_KEY_COUNT=$(printf '%s' "$AUDIT" | \ + jq -r '[.records[].payload.producer.event | select(.keys)][0].keys | length') +LINK_ANSIDS=$(printf '%s' "$AUDIT" | \ + jq -r '[.records[].payload.producer.event | select(.eventType == "IDENTITY_LINKED")][0].ansIds | length') +[ "$SEALED_KEY_COUNT" = "2" ] || fail "sealed proof should carry 2 keys, got $SEALED_KEY_COUNT" +[ "$LINK_ANSIDS" = "2" ] || fail "sealed link event should carry 2 ansIds, got $LINK_ANSIDS" +ok "sealed: IDENTITY_VERIFIED.keys[2] (ES256 + EdDSA, verbatim) and IDENTITY_LINKED.ansIds[2] (one event)" + +header "11c. TL: GET /v1/identities/$IDENTITY_ID/agents (reverse join: both linked agents)" +AGENTS_VIEW=$(curl_tl GET "/v1/identities/$IDENTITY_ID/agents") +AGENTS_COUNT=$(printf '%s' "$AGENTS_VIEW" | jq -r '.agents | length') +[ "$AGENTS_COUNT" = "2" ] || fail "reverse join should list 2 agents, got $AGENTS_COUNT" + +header "11d. TL: GET /v1/identities/$DK_ID (did:key badge — the sealed Multikey verification method)" +poll_tl_identity_audit "$DK_ID" 2 30 +DK_BADGE_VM=$(curl_tl GET "/v1/identities/$DK_ID/audit" | \ + jq -r '[.records[].payload.producer.event | select(.keys)][0].keys[0].verificationMethod') +DK_VM_TYPE=$(printf '%s' "$DK_BADGE_VM" | jq -r '.type // empty') +DK_VM_MB=$(printf '%s' "$DK_BADGE_VM" | jq -r '.publicKeyMultibase // empty') +[ "$DK_VM_TYPE" = "Multikey" ] || fail "did:key seal should be a Multikey method, got $DK_VM_TYPE" +[ "did:key:$DK_VM_MB" = "$DID_KEY" ] || fail "did:key seal's publicKeyMultibase must be the identifier's msid verbatim" +ok "did:key sealed verbatim: type=Multikey, publicKeyMultibase = the did:key msid itself" + +# ----- 12-13. Agent-side computed views (both badges) ----- +poll_tl_audit "$AGENT_1" 1 30 +poll_tl_audit "$AGENT_2" 1 30 +header "12. TL: GET /v1/agents/{both} (agent-1 carries BOTH whos; agent-2 carries the did:web)" +BADGE_1=$(curl_tl GET "/v1/agents/$AGENT_1") +IDS_1=$(printf '%s' "$BADGE_1" | jq -r '.identities | length') +[ "$IDS_1" = "2" ] || fail "agent-1 badge should show 2 identities, got $IDS_1" +KEYIDS_1=$(printf '%s' "$BADGE_1" | jq -r '.identities[] | select(.kind == "did:web") | .provenKeyIds | length') +[ "$KEYIDS_1" = "2" ] || fail "agent-1's did:web entry should show 2 provenKeyIds, got $KEYIDS_1" +DK_ON_1=$(printf '%s' "$BADGE_1" | jq -r '.identities[] | select(.kind == "did:key") | .value // empty') +[ "$DK_ON_1" = "$DID_KEY" ] || fail "agent-1's did:key entry missing" +BADGE_2=$(curl_tl GET "/v1/agents/$AGENT_2") +WHO_2=$(printf '%s' "$BADGE_2" | jq -r '.identities[0].value // empty') +[ "$WHO_2" = "$DID_WEB" ] || fail "agent-2 badge missing the identity join" +# Capture the sealed verbatim key material so the rotation step can +# show it change (read from the latest PROOF event on the stream). +SEALED_X_BEFORE=$(printf '%s' "$AUDIT" | \ + jq -r '[.records[].payload.producer.event | select(.keys)][0].keys[0].verificationMethod.publicKeyJwk.x // empty') +[ -n "$SEALED_X_BEFORE" ] || fail "sealed proof event is missing the verbatim verification method" +ok "agent-1: did:web (provenKeyIds[2]) + did:key (Ed25519) side by side; agent-2: $WHO_2" + +header "13. TL: GET /v1/agents/$AGENT_1/identities + /identities/history" +curl_tl GET "/v1/agents/$AGENT_1/identities" >/dev/null +curl_tl GET "/v1/agents/$AGENT_1/identities/history" >/dev/null + +# ----- 14-16. Rotation — ONE event, no fan-out ----- +header "14. PUT /v2/ans/identities/$IDENTITY_ID (rotate — stage a re-proof under ONE new key)" +signproof keygen -out "$KEY_DIR/who-rotated.pem" >/dev/null +ROT_RESP=$(curl_json PUT "/v2/ans/identities/$IDENTITY_ID" "$(jq -n --arg v "$DID_WEB" '{value: $v}')") +ROT_INPUT=$(printf '%s' "$ROT_RESP" | jq -r '.challenges[0].signingInput // empty') +[ -n "$ROT_INPUT" ] || fail "rotate did not return a fresh challenge" +note "until the new proof lands, the previously sealed state (2 keys) stands" + +header "15. POST /v2/ans/identities/$IDENTITY_ID/verify-control (new key → seals ONE IDENTITY_UPDATED)" +ROT_PROOF=$(signproof sign -key "$KEY_DIR/who-rotated.pem" -kid "${DID_WEB}#key-1" -input "$ROT_INPUT") +curl_json POST "/v2/ans/identities/$IDENTITY_ID/verify-control" \ + "$(jq -n --arg p "$ROT_PROOF" '{signedProofs: [$p]}')" >/dev/null +assert_2xx "rotation verify-control" + +header "16. TL: the rotation sealed ONE IDENTITY_UPDATED — proven set 2 keys → 1, key material changed" +poll_tl_identity_audit "$IDENTITY_ID" 3 30 +AUDIT=$(curl_tl GET "/v1/identities/$IDENTITY_ID/audit") +SEALED_X_AFTER=$(printf '%s' "$AUDIT" | \ + jq -r '[.records[].payload.producer.event | select(.keys)][0].keys[0].verificationMethod.publicKeyJwk.x // empty') +SEALED_KEYS_AFTER=$(printf '%s' "$AUDIT" | \ + jq -r '[.records[].payload.producer.event | select(.keys)][0].keys | length') +[ -n "$SEALED_X_AFTER" ] && [ "$SEALED_X_AFTER" != "$SEALED_X_BEFORE" ] || \ + fail "rotation not sealed (before=$SEALED_X_BEFORE after=$SEALED_X_AFTER)" +[ "$SEALED_KEYS_AFTER" = "1" ] || fail "rotated proven set should be 1 key, got $SEALED_KEYS_AFTER" +# Both linked badges reflect it immediately (read-time join) — with +# 10,000 linked agents this would still be ONE sealed event. +KEYIDS_1_AFTER=$(curl_tl GET "/v1/agents/$AGENT_1" | \ + jq -r '.identities[] | select(.kind == "did:web") | .provenKeyIds | length') +[ "$KEYIDS_1_AFTER" = "1" ] || fail "agent-1's did:web entry should now show 1 provenKeyId, got $KEYIDS_1_AFTER" +ok "sealed key material flipped (${SEALED_X_BEFORE:0:12}… → ${SEALED_X_AFTER:0:12}…), set 2→1 — ONE event, zero agent-stream writes" + +# ----- 17. Identity receipt ----- +header "17. TL: GET /v1/identities/$IDENTITY_ID/receipt (SCITT COSE_Sign1 for the identity leaf)" +IDENTITY_RECEIPT="$DATA/identity-receipt.cbor" +receipt_status=$(curl_tl_binary GET "/v1/identities/$IDENTITY_ID/receipt" "$IDENTITY_RECEIPT") +if [ "$receipt_status" = "200" ]; then + first_byte=$(od -An -tx1 -N1 "$IDENTITY_RECEIPT" | tr -d ' \n') + ok "identity receipt saved to $IDENTITY_RECEIPT (first byte=0x${first_byte}, want 0xd2 for COSE_Sign1)" +elif [ "$receipt_status" = "503" ]; then + warn "receipt 503 — checkpoint has not yet covered the leaf; retry in a few seconds" +else + fail "unexpected identity receipt status $receipt_status" +fi + +# ----- 18. Unlink ONE agent — the other stays linked ----- +header "18. DELETE /v2/ans/identities/$IDENTITY_ID/links/$AGENT_1 (unlink the did:web from agent #1 only)" +curl_json DELETE "/v2/ans/identities/$IDENTITY_ID/links/$AGENT_1" >/dev/null +assert_2xx "unlink" +poll_tl_identity_audit "$IDENTITY_ID" 4 30 +BADGE_1=$(curl_tl GET "/v1/agents/$AGENT_1") +REMAINING_1=$(printf '%s' "$BADGE_1" | jq -r '(.identities | length) // 0') +KIND_1=$(printf '%s' "$BADGE_1" | jq -r '.identities[0].kind // empty') +REMAINING_2=$(curl_tl GET "/v1/agents/$AGENT_2" | jq -r '(.identities | length) // 0') +[ "$REMAINING_1" = "1" ] && [ "$KIND_1" = "did:key" ] || \ + fail "agent-1 should keep exactly its did:key link (got $REMAINING_1 × $KIND_1)" +[ "$REMAINING_2" = "1" ] || fail "agent-2 badge lost its link — unlink must be per-pair" +ok "the did:web↔agent-1 pair ended; agent-1's did:key link and agent-2's did:web link are untouched" +curl_tl GET "/v1/agents/$AGENT_1/identities/history" >/dev/null + +# ----- 19-20. Revoke — the who dies, the whats survive ----- +header "19. POST /v2/ans/identities/$IDENTITY_ID/revoke (state change — an identity cannot be deleted)" +curl_json POST "/v2/ans/identities/$IDENTITY_ID/revoke" >/dev/null +assert_2xx "revoke" +poll_tl_identity_audit "$IDENTITY_ID" 5 30 + +header "20. TL: did:web REVOKED; agent-2's join shows it; agent-1's did:key stays VERIFIED" +ID_STATUS=$(curl_tl GET "/v1/identities/$IDENTITY_ID" | jq -r '.status') +[ "$ID_STATUS" = "REVOKED" ] || fail "identity badge status=$ID_STATUS, want REVOKED" +BADGE_2=$(curl_tl GET "/v1/agents/$AGENT_2") +WHO_STATUS_2=$(printf '%s' "$BADGE_2" | jq -r '.identities[0].identityStatus // empty') +AGENT_STATUS_2=$(printf '%s' "$BADGE_2" | jq -r '.status') +[ "$WHO_STATUS_2" = "REVOKED" ] || fail "agent-2's identities[] should show REVOKED, got $WHO_STATUS_2" +[ "$AGENT_STATUS_2" = "ACTIVE" ] || fail "agent-2 status=$AGENT_STATUS_2, want ACTIVE — identity ops must never touch the agent" +# Each identity has its own lifecycle: the did:key on agent-1 is +# unaffected by the did:web's revocation. +DK_STATUS_1=$(curl_tl GET "/v1/agents/$AGENT_1" | \ + jq -r '.identities[] | select(.kind == "did:key") | .identityStatus // empty') +[ "$DK_STATUS_1" = "VERIFIED" ] || fail "agent-1's did:key should stay VERIFIED, got $DK_STATUS_1" +ok "ONE IDENTITY_REVOKED propagated to agent-2's join at read time; agent-1's did:key untouched; agents ACTIVE" + +# ----- summary ----- +header "Identity lifecycle complete" +printf " did:web %s (VERIFIED w/ 2 keys → rotated to 1 → REVOKED)\n" "$IDENTITY_ID" >&2 +printf " did:key %s (Ed25519 — VERIFIED, linked to agent-1 throughout)\n" "$DK_ID" >&2 +printf " agent-1 %s (carried BOTH whos; kept the did:key after the did:web unlink)\n" "$AGENT_1" >&2 +printf " agent-2 %s (did:web-linked throughout; saw rotation + revocation at read time)\n" "$AGENT_2" >&2 +printf " receipt %s\n" "$IDENTITY_RECEIPT" >&2 +printf "\n" >&2 +printf " sealed: did:web stream — IDENTITY_VERIFIED (multi-key), IDENTITY_LINKED (ansIds[2]),\n" >&2 +printf " IDENTITY_UPDATED, IDENTITY_UNLINKED, IDENTITY_REVOKED; did:key stream —\n" >&2 +printf " IDENTITY_VERIFIED (Multikey, verbatim), IDENTITY_LINKED.\n" >&2 diff --git a/scripts/demo/run-lifecycle.sh b/scripts/demo/run-lifecycle.sh index f0a4437..355a6df 100755 --- a/scripts/demo/run-lifecycle.sh +++ b/scripts/demo/run-lifecycle.sh @@ -17,6 +17,15 @@ # 13. GET TL /v1/agents/{id}/receipt (SCITT COSE) # 14. go test ./internal/tl/receipt -run TestSmokeVerifyDemoReceipt (offline verify) # 15. GET TL /internal/v1/producer-keys/ra/{raId} (admin CRUD) +# 16. POST /v2/ans/identities (register the WHO — a did:web verified identity) +# 17. POST /v2/ans/identities/{id}/verify-control (control proof → VERIFIED, seals IDENTITY_VERIFIED) +# 18. POST /v2/ans/identities/{id}/links (link the agent — ONE IDENTITY_LINKED on the identity stream) +# 19. GET TL /v1/agents/{id} (badge now carries the computed identities[] join) +# +# Step 16-19 show the verified-identity surface in the standard +# lifecycle; scripts/demo/identity-lifecycle.sh exercises EVERY +# identity operation (re-add, did:key, rotation, unlink, revoke, +# receipts, history views). # # Usage: # scripts/demo/run-lifecycle.sh # random host, version 1.0.0 @@ -308,12 +317,54 @@ fi header "15. TL: GET /internal/v1/producer-keys/ra/ans-ra-local (admin)" curl_tl GET "/internal/v1/producer-keys/ra/ans-ra-local" >/dev/null +# ----- 16-19. Verified identity (the WHO behind this agent) ----- +# +# The agent registration above is the "what": one FQDN, its +# endpoints, its certificates. The verified identity is the "who" +# behind it — proven through a challenge-bound key proof, sealed on +# its own TL stream, and linked to the agent. Rotating or revoking +# the identity later is ONE sealed event regardless of how many +# agents are linked; every linked badge reflects it at read time. +header "16. POST /v2/ans/identities (register the WHO — did:web)" +require_cmd go +IDENTITY_DID="did:web:who-$(openssl rand -hex 4).example.com" +IDENTITY_KEY="$DATA/identity-who.pem" +(cd "$ROOT" && go run ./scripts/demo/signproof keygen -out "$IDENTITY_KEY") >/dev/null +ID_REG=$(curl_json POST /v2/ans/identities "$(jq -n --arg v "$IDENTITY_DID" '{value: $v}')") +IDENTITY_ID=$(printf '%s' "$ID_REG" | jq -r '.identityId // empty') +ID_INPUT=$(printf '%s' "$ID_REG" | jq -r '.challenges[0].signingInput // empty') +[ -n "$IDENTITY_ID" ] && [ -n "$ID_INPUT" ] || fail "identity register failed" +echo "$IDENTITY_ID" >"$DATA/last-identity-id" +ok "identityId=$IDENTITY_ID (PENDING_CONTROL — challenge issued)" + +header "17. POST /v2/ans/identities/$IDENTITY_ID/verify-control (→ VERIFIED)" +ID_PROOF=$(cd "$ROOT" && go run ./scripts/demo/signproof sign \ + -key "$IDENTITY_KEY" -kid "${IDENTITY_DID}#key-1" -input "$ID_INPUT") +curl_json POST "/v2/ans/identities/$IDENTITY_ID/verify-control" \ + "$(jq -n --arg p "$ID_PROOF" '{signedProofs: [$p]}')" >/dev/null +assert_2xx "identity verify-control" +ok "control proven — IDENTITY_VERIFIED seals on the identity's own TL stream" + +header "18. POST /v2/ans/identities/$IDENTITY_ID/links (link this agent — one call, one sealed event)" +curl_json POST "/v2/ans/identities/$IDENTITY_ID/links" \ + "$(jq -n --arg a "$AGENT_ID" '{agentIds: [$a]}')" >/dev/null +assert_2xx "identity link" + +header "19. TL: GET /v1/agents/$AGENT_ID (badge — computed identities[] join)" +poll_tl_identity_audit "$IDENTITY_ID" 2 30 +BADGE_WITH_WHO=$(curl_tl GET "/v1/agents/$AGENT_ID") +WHO_VALUE=$(printf '%s' "$BADGE_WITH_WHO" | jq -r '.identities[0].value // empty') +[ "$WHO_VALUE" = "$IDENTITY_DID" ] || fail "badge identities[] join missing (got: $WHO_VALUE)" +ok "one hop answers \"who is behind this agent\": $WHO_VALUE (VERIFIED)" + # ----- summary ----- header "Lifecycle complete (both sides)" printf " agentId %s\n" "$AGENT_ID" >&2 printf " ansName %s\n" "$ANS_NAME" >&2 +printf " identityId %s (%s)\n" "$IDENTITY_ID" "$IDENTITY_DID" >&2 printf " saved to %s\n" "$DATA/last-agent-id" >&2 printf " root-keys %s\n" "$ROOT_KEYS_FILE" >&2 printf " receipt %s\n" "$RECEIPT_FILE" >&2 printf "\n" >&2 printf " revoke: %s\n" "scripts/demo/revoke.sh" >&2 +printf " identities: %s\n" "scripts/demo/identity-lifecycle.sh (every identity operation)" >&2 diff --git a/scripts/demo/signproof/main.go b/scripts/demo/signproof/main.go new file mode 100644 index 0000000..5573114 --- /dev/null +++ b/scripts/demo/signproof/main.go @@ -0,0 +1,190 @@ +// signproof is the demo-side identity-proof tool: it mints keypairs +// and signs identity verify-control proofs (compact JWS over the +// RA-served signingInput). +// +// This is what a registrant's own tooling does in production — the +// private key never touches the RA. Run via `go run`: +// +// go run ./scripts/demo/signproof keygen -alg ed25519 -out key.pem +// → writes the key, prints the did:key identifier on stdout +// +// go run ./scripts/demo/signproof sign -key key.pem -kid KID -input SIGNING_INPUT +// → prints the compact JWS on stdout (alg auto-selected from +// the key type: Ed25519 → EdDSA, P-256 → ES256) +// +// The JWS protected header carries kid + the embedded public jwk. +// The jwk header is what lets the RA's noop resolver synthesize a +// DID document for local development; the hardened web resolver +// ignores it and uses the fetched did.json. +package main + +import ( + "crypto/ecdsa" + "crypto/ed25519" + "crypto/elliptic" + "crypto/rand" + "crypto/sha256" + "crypto/x509" + "encoding/base64" + "encoding/json" + "encoding/pem" + "errors" + "flag" + "fmt" + "os" + + anscrypto "github.com/godaddy/ans/internal/crypto" +) + +func main() { + if err := run(os.Args[1:]); err != nil { + fmt.Fprintln(os.Stderr, "signproof:", err) + os.Exit(1) + } +} + +func run(args []string) error { + if len(args) < 1 { + return errors.New("usage: signproof [flags]") + } + switch args[0] { + case "keygen": + return keygen(args[1:]) + case "sign": + return sign(args[1:]) + default: + return fmt.Errorf("unknown subcommand %q (want keygen or sign)", args[0]) + } +} + +// keygen mints a keypair, writes it as PKCS#8 PEM, and prints the +// matching did:key identifier — handy both for did:key registrations +// and as the kid fragment for did:web proofs. +func keygen(args []string) error { + fs := flag.NewFlagSet("keygen", flag.ContinueOnError) + out := fs.String("out", "", "path to write the private key PEM (required)") + alg := fs.String("alg", "p256", "key algorithm: p256 | ed25519") + if err := fs.Parse(args); err != nil { + return err + } + if *out == "" { + return errors.New("keygen: -out is required") + } + + var priv any + var pub any + switch *alg { + case "p256": + key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) + if err != nil { + return fmt.Errorf("generate p256 key: %w", err) + } + priv, pub = key, &key.PublicKey + case "ed25519": + pubKey, privKey, err := ed25519.GenerateKey(rand.Reader) + if err != nil { + return fmt.Errorf("generate ed25519 key: %w", err) + } + priv, pub = privKey, pubKey + default: + return fmt.Errorf("keygen: unsupported -alg %q (want p256 or ed25519)", *alg) + } + + der, err := x509.MarshalPKCS8PrivateKey(priv) + if err != nil { + return fmt.Errorf("marshal key: %w", err) + } + pemBytes := pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: der}) + if err := os.WriteFile(*out, pemBytes, 0o600); err != nil { + return fmt.Errorf("write key: %w", err) + } + + multibase, err := anscrypto.EncodeMultibase(pub) + if err != nil { + return fmt.Errorf("encode multibase: %w", err) + } + // stdout carries exactly the did:key identifier so shell callers + // can capture it: DID=$(go run ./scripts/demo/signproof keygen ...). + fmt.Fprintln(os.Stdout, "did:key:"+multibase) + return nil +} + +// sign produces the compact JWS the verify-control endpoint expects: +// the payload segment is the RA-served signingInput VERBATIM (clients +// never canonicalize — the RA checks payload equality first), and the +// algorithm follows the key type: P-256 → ES256 (SHA-256 prehash, +// P1363 signature), Ed25519 → EdDSA (raw signing input, RFC 8037). +func sign(args []string) error { + fs := flag.NewFlagSet("sign", flag.ContinueOnError) + keyPath := fs.String("key", "", "path to the private key PEM (required)") + kid := fs.String("kid", "", "verification method id to claim (required)") + input := fs.String("input", "", "the RA-served base64url signingInput (required)") + if err := fs.Parse(args); err != nil { + return err + } + if *keyPath == "" || *kid == "" || *input == "" { + return errors.New("sign: -key, -kid, and -input are all required") + } + + raw, err := os.ReadFile(*keyPath) + if err != nil { + return fmt.Errorf("read key: %w", err) + } + block, _ := pem.Decode(raw) + if block == nil { + return errors.New("key file is not PEM") + } + parsed, err := x509.ParsePKCS8PrivateKey(block.Bytes) + if err != nil { + return fmt.Errorf("parse key: %w", err) + } + + var alg string + var pub any + switch key := parsed.(type) { + case *ecdsa.PrivateKey: + if key.Curve != elliptic.P256() { + return errors.New("ECDSA key must be P-256") + } + alg, pub = "ES256", &key.PublicKey + case ed25519.PrivateKey: + alg, pub = "EdDSA", key.Public() + default: + return fmt.Errorf("unsupported key type %T (want P-256 or Ed25519)", parsed) + } + + jwk, err := anscrypto.PublicKeyToJWK(pub) + if err != nil { + return fmt.Errorf("encode jwk: %w", err) + } + headerJSON, err := json.Marshal(map[string]any{ + "alg": alg, + "kid": *kid, + "jwk": jwk, + }) + if err != nil { + return fmt.Errorf("marshal header: %w", err) + } + encodedHeader := base64.RawURLEncoding.EncodeToString(headerJSON) + toSign := encodedHeader + "." + *input + + var sig []byte + switch key := parsed.(type) { + case *ecdsa.PrivateKey: + digest := sha256.Sum256([]byte(toSign)) + derSig, err := ecdsa.SignASN1(rand.Reader, key, digest[:]) + if err != nil { + return fmt.Errorf("sign: %w", err) + } + sig, err = anscrypto.DERToP1363(derSig, 32) + if err != nil { + return fmt.Errorf("encode signature: %w", err) + } + case ed25519.PrivateKey: + // EdDSA signs the raw signing input — no prehash. + sig = ed25519.Sign(key, []byte(toSign)) + } + + fmt.Fprintln(os.Stdout, toSign+"."+base64.RawURLEncoding.EncodeToString(sig)) + return nil +} diff --git a/scripts/demo/start.sh b/scripts/demo/start.sh index 415a09e..979a3e9 100755 --- a/scripts/demo/start.sh +++ b/scripts/demo/start.sh @@ -120,6 +120,15 @@ dns: type: ${ANS_DNS_TYPE:-noop} server: "${ANS_DNS_SERVER:-}" +identity: + # Verified identities (the "who" behind agents). The "noop" + # resolver synthesizes did:web documents from the keys embedded in + # submitted proofs — real signature verification, no hosting + # needed; flip to "web" for the hardened HTTPS did.json fetch. + resolver: + type: ${ANS_IDENTITY_RESOLVER:-noop} + challenge-ttl: 1h + keys: type: file file: From 327c2e47993bfa7c959ce352470e64198942e8b2 Mon Sep 17 00:00:00 2001 From: Connor Snitker Date: Wed, 10 Jun 2026 18:10:17 -0500 Subject: [PATCH 04/12] docs: verified-identities implementation plan and as-built notes The repo-adapted implementation plan derived from the DESIGN-multi-identity-anchors rev-4 design of record, including the as-built deviations: the single-table TL read index, the multi-algorithm key allowlist, verbatim verification-method sealing, and the noop/web resolver split. Signed-off-by: Connor Snitker --- docs/PLAN-verified-identities.md | 544 +++++++++++++++++++++++++++++++ 1 file changed, 544 insertions(+) create mode 100644 docs/PLAN-verified-identities.md diff --git a/docs/PLAN-verified-identities.md b/docs/PLAN-verified-identities.md new file mode 100644 index 0000000..4e2f22a --- /dev/null +++ b/docs/PLAN-verified-identities.md @@ -0,0 +1,544 @@ +# Implementation Plan — Verified Identities (the "who" behind an agent) + +> **As-built notes (2026-06-10).** The implementation landed per this +> plan with three deliberate deviations, each truer to the design's +> own principles than the plan's first sketch: +> +> 1. **TL storage: one table, not two.** Instead of a parallel +> `tl_identity_events` table, `tl_events` gained a nullable +> `identity_id` column (+ partial index) and a +> `tl_identity_event_agents` fan-out table for the link read-join +> — the literal embodiment of "streams are read indexes over one +> log". One mirror, one badge builder, one receipt path, one +> Merkle-proof path; the identity envelope exposes its index keys +> through an optional capability interface so `event.View` stayed +> frozen for existing implementers. +> 2. **Key-type support: everything the JWS layer verifies.** +> Ed25519 (EdDSA), ECDSA P-256 (ES256), and RSA ≥ 2048 (RS256) +> are all accepted for identity proofs — the allowlist names +> exactly what jws.go implements. Keys that structurally cannot +> prove control are rejected with precise errors: X25519 is a +> key-agreement key; secp256k1 and P-384/521 have no verifier +> here (adding one is the gate for admitting them). +> 2a. **Seals quote the verification method verbatim.** Sealed +> `ProvenKey` = `{verificationMethod, signedProof}` where +> `verificationMethod` is the DID document's object exactly as +> served — member-for-member, values untouched (JCS signing +> preserves member values, so the quoted material survives +> intact). Nothing derived, re-encoded, or normalized enters a +> seal; thumbprints are compute-at-read conveniences. The badge +> join surfaces `provenKeyIds` (verification-method ids). did:key +> seals the method-spec-derived Multikey entry whose key material +> is the method-specific id verbatim from the identifier. The +> postponed lei kind remains the deliberate exception (subject +> AID + thumbprint only — no document to quote, ACDC is PII, the +> KEL is the authoritative key history). +> 3. **No localhost did:web host for development.** The hardened web +> resolver pins fetches to port 443, requires WebPKI, and its SSRF +> denylist rejects loopback — a local did.json server is +> structurally unusable with it, by design. Local development is +> the noop resolver (real JWS verification, waived live-document +> binding); the web resolver is covered by TLS-server tests at the +> parse seam plus dialer-level SSRF tests. + +| | | +|---|---| +| **Source design** | `DESIGN-multi-identity-anchors.md` rev 4 (2026-06-10, who/what pivot) — design of record from the ans-registry working sessions | +| **Target repo** | `godaddy/ans` (this repo), branched from `main` @ `d94d531` | +| **Scope** | Identity aggregate + proof-of-control gate (did:web, did:key), identity links, the five `IDENTITY_*` TL events on a dedicated ingest lane, TL identity read surface + computed badge join, offline verification. **lei postponed** (design §3.6/§10.7 retained as design of record). | +| **Pattern requirement** | Every outbound I/O dependency ships a **noop adapter for local/quickstart** and a **fully functional adapter selected by config** — exactly the `dns.type: noop \| lookup` precedent (`cmd/ans-ra/main.go` `selectDNSVerifier`, `internal/adapter/dns/{noop,lookup}.go`). | + +--- + +## 0. Design → this-repo mapping + +The design doc was written against the `ans-registry-poc` codebase. Every code +anchor it cites maps onto this repo as follows; the *wire shapes and rules* in +the design (§3.2 IdentityProofInput, §5.3 bodies, §5.5 events, §5.7 schema) +carry over verbatim. + +| Design reference | This repo | +|---|---| +| `AgentRegistration` aggregate, `agent.go:68/129/150/155` | `internal/domain/agent.go:54-179` (`AgentRegistration`, `NewRegistration`) | +| Status machine `status.go:62-71` | `internal/domain/status.go` (`ValidTransitions`) | +| `lifecycle.go` `VerifyACME` | `internal/ra/service/registration.go` + `lifecycle.go` (`VerifyACME`/`VerifyDNS`) | +| TL `event.go:92-102` `Type.IsValid()` closed switch | `internal/tl/event/event.go:92-102` — **stays frozen**; identity events get their own package (see §2.1) | +| V1 enum frozen (`internal/tl/event/v1/event.go:50-65`) | same path here — untouched | +| `migrations/001_initial.sql` (RA) | `internal/adapter/store/sqlite/migrations/` — latest is `005_agent_acme_challenge.sql`, so the identity migration is **006** | +| TL migrations | `internal/adapter/store/sqlitetl/migrations/` — latest is `002_producer_keys.sql`, so identity events migration is **003** | +| `spec/api-spec-v2.yaml`, `spec/api-spec-tl-v2.yaml` | same paths — the canonical contracts; every PR pastes its shape diff per CLAUDE.md | +| Producer lane / outbox | `internal/ra/service/registration.go` (`signAndMarshalPayload`, `enqueueTLEvent`), `internal/ra/outbox/worker.go`, `internal/adapter/tlclient/client.go` | +| `noop` vs real verification | `internal/adapter/dns/noop.go` / `lookup.go`, selected by `cfg.DNS.Type` in `cmd/ans-ra/main.go:410-421` | + +One deliberate divergence from the design text: design §5.5 says the V2 +`Type.IsValid()` switch "MUST widen." In this repo the cleaner equivalent is a +**parallel identity event package** with its own closed five-token enum — the +agent codec stays byte-frozen, and the cross-lane guard falls out of each +codec's `Validate()` (an `IDENTITY_*` token fails the agent enum **and** lacks +`ansId`; an `AGENT_*` token fails the identity enum and lacks `identityId` — +both 422 `INVALID_EVENT`). This satisfies the same normative requirement +(identity tokens accepted on the identity lane, V1 lane frozen) without +touching the existing agent contract. + +--- + +## 1. The noop/full pattern, applied per kind + +The single design invariant: **the RA seals an identity attestation only +after control is proven** — a challenge-bound signature verified against the +identifier's *authoritative* key. The only outbound I/O in Slices 1–3 is the +did:web `did.json` HTTPS fetch. That fetch is the port. + +| Kind | Outbound I/O | noop adapter (quickstart) | full adapter (configured) | +|---|---|---|---| +| `did:key` | **none** — key decodes from the DID string | not needed; real crypto runs locally out of the box | same code path | +| `did:web` | HTTPS GET of `did.json` (registrant-steered host) | `didresolver.Noop` — never dials; synthesizes the DID document from the submitted proofs' embedded keys (below) | `didresolver.Web` — hardened fetcher: WebPKI, SSRF dialer guards, 5 s timeout, size cap, ≤5 same-registrable-domain redirects (design §3.7) | +| `lei` *(postponed)* | GLEIF L1 GET + internal `vlei-verifier` | `Noop` variants of both clients | real HTTP clients behind `port.LEIControlVerifier` | + +**Noop semantics — mirror the DNS precedent precisely.** The noop DNS +verifier waives the *external-world binding* (does the zone really contain +the record?) while every pure-crypto check (CSR self-signature) still runs. +The did:web analog: the noop resolver waives "does the live `did.json` +really list this key?" while the JWS verification still genuinely runs. + +Port shape that makes both adapters uniform from the service's view: + +```go +// internal/port/didresolver.go +type DIDResolver interface { + // Resolve returns the DID document for did. Hints carry the + // kid → public-JWK pairs the caller extracted from the submitted + // proofs' protected headers; the web resolver ignores them + // (authoritative fetch), the noop resolver synthesizes a document + // from them so local flows run with zero hosting. + Resolve(ctx context.Context, did string, hints []KeyHint) (*DIDDocument, error) +} + +type KeyHint struct { + Kid string + PublicKeyJWK json.RawMessage // from the proof's `jwk` protected header +} + +type DIDDocument struct { + ID string + AssertionMethod []VerificationMethod +} + +type VerificationMethod struct { + ID string + Controller string + Type string + PublicKeyJwk json.RawMessage + PublicKeyMultibase string +} +``` + +Consequences, stated honestly: + +- In **noop mode** the registrant's JWS proofs must carry the `jwk` protected + header (standard JOSE) so the synthesized document has key material. The + 202 challenge list contains a single `{kid: "", signingInput}` entry (the + `signingInput` is key-independent — design §5.3 notes every per-key entry + carries the same bytes), and the registrant names keys via the JWS `kid` + + `jwk` headers. +- In **web mode** the register-time advisory fetch enumerates the document's + `assertionMethod` kids into the challenge list, and verify-control + **re-fetches authoritatively** (design §3.6); the `jwk` header, if present, + is ignored — the resolved document is always the key source. +- Sealed events remain **self-verifying in both modes**: the sealed + `signedProof` really verifies against the sealed `publicKeyJwk`. Noop mode + waives only the authoritative web binding — the documented quickstart + caveat, same as noop DNS ("accepts any state; NOT for production"). + +Config (mirrors `dns:`): + +```yaml +identity: + resolver: + type: noop # noop | web (default noop — quickstart parity) + challengeTTL: 1h # nonce TTL; design floor 5m for high-assurance +``` + +`cmd/ans-ra/main.go` gains `selectDIDResolver(cfg)` next to +`selectDNSVerifier`. `IdentityProofInput.raId` reuses the already-configured +producer identity (`cfg.Signer.RAID`) — one RA, one `raId`, no new key. + +--- + +## 2. PR slices + +Ordered so TL lands before anything emits to it, and so the event vocabulary +(append-only-forever) is settled in the first sealing-relevant PR — design +§5.5's sequencing note. Each PR: `make check` green, spec shape-diff pasted +into the PR description, DCO sign-off + GPG signature, no AI trailers. + +``` +PR1 (TL ingest) ──> PR2 (TL reads/join) ──> PR3 (RA identity + did:web) ──> PR4 (RA links + views + demo) + └────────────────> PR5 (did:key) ──> PR6 (ans-verify) +``` + +### PR 1 — `feat(tl): identity event family + ingest lane` + +**The vocabulary-freezing PR.** The five tokens and the identity event shape +land here and are forever after. + +New package `internal/tl/event/identity/` (import alias `identityevent`), +mirroring `internal/tl/event/event.go` structure exactly: + +- `Type` enum: `IDENTITY_VERIFIED`, `IDENTITY_UPDATED`, `IDENTITY_REVOKED`, + `IDENTITY_LINKED`, `IDENTITY_UNLINKED` + closed `IsValid()`. +- `Event` struct (design §5.5 shapes): + `identityId` (required, stream key), `kind`, `value`, `providerId`, + `proofMethod`, `keys[]` (`ProvenKey{verificationMethodId, keyThumbprint, + publicKeyJwk, signedProof}` — JWK/proof omitted for lei's thumbprint-only + tier), `ansIds[]` (LINKED/UNLINKED only), `previousValue` (UPDATED only), + `verifiedAt`, `revokedAt`, `raId`, `timestamp`. +- Own `Envelope`/`Payload`/`Producer` wrapper (same outer shape, typed inner + event) implementing `event.Signable`; `SchemaVersion = "V2"`; + identical JCS canonicalization + RFC 6962 `SHA-256(0x00 || leaf)` rules. +- `Validate()` per-type required-key matrix (design §5.6.1): proofs/rotation/ + revocation require `identityId`; link events additionally require non-empty + `ansIds[]`; `keys[]` required and non-empty on VERIFIED/UPDATED. + +TL service + handler: + +- `internal/tl/service/codec.go`: add `identityCodec` implementing + `envelopeCodec` (same `ParseAndBuild` contract, `RAID_MISMATCH` guard + included). +- `internal/tl/service/log.go`: the `append()` pipeline is already + version-agnostic except the SQLite mirror; introduce a small per-lane + bundle `{codec envelopeCodec; mirror eventMirror}` where `eventMirror` is + `{ExistsByEventHash; StoreEvent}` — the agent lanes keep the existing + store, the identity lane mirrors into the new tables. +- `internal/tl/handler/handler.go`: route `POST /v1/internal/identities/event` + (same producer-signature discipline, same 256 KiB cap, same response shape). + Producer-key verification is untouched — same trust store, same `kid` + lookup. + +Storage — `internal/adapter/store/sqlitetl/migrations/003_identity_events.sql`: + +```sql +CREATE TABLE tl_identity_events ( + id INTEGER PRIMARY KEY, + leaf_index INTEGER NOT NULL UNIQUE, + leaf_hash TEXT NOT NULL, + event_hash TEXT NOT NULL UNIQUE, -- dedup key, SHA-256(JCS inner) + log_id TEXT NOT NULL, + identity_id TEXT NOT NULL, + provider_id TEXT, + kind TEXT, + value TEXT, + event_type TEXT NOT NULL, + raw_event TEXT NOT NULL CHECK (json_valid(raw_event)), + created_at_ms INTEGER NOT NULL +); +CREATE INDEX idx_tl_identity_events_identity_leaf + ON tl_identity_events(identity_id, leaf_index DESC); + +-- the read-join index: one row per (link event, named agent) — design §5.6.1 +CREATE TABLE tl_identity_event_agents ( + id INTEGER PRIMARY KEY, + leaf_index INTEGER NOT NULL, + identity_id TEXT NOT NULL, + ans_id TEXT NOT NULL, + event_type TEXT NOT NULL, -- IDENTITY_LINKED | IDENTITY_UNLINKED + created_at_ms INTEGER NOT NULL +); +CREATE INDEX idx_tl_identity_event_agents_ans + ON tl_identity_event_agents(ans_id, leaf_index DESC); +``` + +Plus `internal/adapter/store/sqlitetl/identityevents.go` (StoreEvent — also +fans the `ansIds[]` into the agent-index table; GetLatestByIdentityID; +GetByIdentityID paginated; ExistsByEventHash; ListLinkEventsByAgent; +ListAgentLinkStateByIdentity). + +Spec: `spec/api-spec-tl-v2.yaml` — the ingest path + `IdentityProducerEvent` +schema + the five-token enum. + +Tests: codec round-trip + leaf-hash vectors; per-type validation matrix; +**cross-lane guards both directions** (V2 agent body on the identity route → +422 `INVALID_EVENT`; identity body on `/v1/` and `/v2/internal/agents/event` +→ 422; V1 lane rejects all `IDENTITY_*`); ingest handler tests with real +producer signatures; dedup-by-content-hash including ansIds-index idempotence +on duplicate. + +**Acceptance:** one Merkle tree — identity and agent leaves interleave in the +same tiles; checkpoints/witnesses/receipt machinery untouched; `/tile/*` +serves both. + +### PR 2 — `feat(tl): identity read surface + computed badge join` + +No new persistence beyond PR 1 — everything here is read-time computation +(design §5.6.3), the same pattern as the V2 badge's computed `status`. + +- `internal/tl/service/identitybadge.go` — `Get` (latest identity event + + Merkle proof + computed status `VERIFIED|REVOKED`), `Audit` (paginated, + **format-identical to the agent audit envelope** — no bespoke shape). +- Receipt support for identity leaves: `ReceiptService.ForIdentity` — reuse + the leaf-index receipt machinery; `tl_receipts` keying generalizes (add a + nullable `identity_id` column or a `subject_type` discriminator in the same + 003 migration — implementation detail, decide at code time). +- The **join**, both directions, computed in the service from PR 1's stores: + a link is *effective* iff latest link/unlink for `(identityId, ansId)` is + `LINKED` ∧ identity stream says `VERIFIED` ∧ agent badge status is live. +- Routes (`internal/tl/handler/handler.go`): + - `GET /v1/identities/{identityId}` — identity badge. + - `GET /v1/identities/{identityId}/audit` — full chain, audit envelope. + - `GET /v1/identities/{identityId}/receipt` — COSE receipt. + - `GET /v1/identities/{identityId}/agents` — reverse join (currently linked). + - `GET /v1/agents/{agentId}` — **gains computed `identities[]`** (design + §5.6.3 badge shape: identityId, kind, value, identityStatus, + provenKeyThumbprints, linkedAt, linkLogId, identityLogId). Covered by the + TL's response signature, never by the seal. + - `GET /v1/agents/{agentId}/identities` and `…/identities/history` — the + audit envelope filtered through the agent index. +- Spec: full read-surface delta to `spec/api-spec-tl-v2.yaml`. + +Tests: join truth-table (link → badge shows it; rotate → thumbprints flip on +every linked badge with **one** sealed event; revoke identity → all badges +show REVOKED; unlink → gone; agent revoked → link ineffective, identity +stream untouched); audit pagination; receipt round-trip; agent audit remains +pure `AGENT_*`. + +### PR 3 — `feat(ra): verified-identity aggregate + proof gate + did:web` + +The RA half: domain, storage, the generalized verify-control gate, sealing, +and the noop/web resolver pair. Internal milestone "2a" of the design. + +**Domain** (`internal/domain/identity.go`, 100% coverage): + +- `IdentifierKind` + lexical `InferKind(value)` (`did:web:` / `did:key:` / + `lei` — only did:web *enabled* this PR; others → + `IDENTIFIER_KIND_UNSUPPORTED`), canonicalization rules, did:web → + resolution-URL mapping (root → `/.well-known/did.json`, path-bearing → + `/{path}/did.json`, **reject port/userinfo** → `DID_BAD_FORMAT`). +- `VerifiedIdentity` aggregate (design §2.1): identityId (UUIDv7 — + `google/uuid` v1.6 has `NewV7`), providerId, kind, value, status + (`PENDING_CONTROL → VERIFIED → REVOKED`), proofMethod, staged + `pendingValue`, challenge (nonce/expiry/consumed); transitions + `IssueChallenge`, `MarkVerified`, `StageRotation`, `CompleteRotation`, + `Revoke`; **no public-key field** (ANS-0 §6.2). +- `IdentityLink` (status `LINKED|UNLINKED`) — used in PR 4, lands with the + aggregate. +- Domain events appended to `internal/domain/events.go` pattern. + +**Crypto** (`internal/crypto/`, ≥95–100%): + +- `jwk.go` — JWK → `crypto.PublicKey` (Ed25519 OKP, ECDSA P-256 allowlist), + RFC 7638 thumbprint, `publicKeyMultibase`/Multikey → JWK conversion (the + pinned thumbprint rule, design §3.6 semantic note). +- `proofinput.go` — `IdentityProofInput` builder: exactly the §3.2 JCS object + `{identifier, identityId, nonce, purpose:"ans:identity-proof:v1", raId, + scheme}` via the existing `Canonicalize`; the served `signingInput` is the + base64url of those bytes, and verify-control checks **payload-equality + before signature** (clients never canonicalize). +- JWS verification reuses `VerifyWithPublicKey` (go-jose v4 already supports + EdDSA + ES256); pin `alg` to the resolved key type. + +**Port + adapters:** + +- `internal/port/didresolver.go` — as §1 above. +- `internal/adapter/didresolver/web.go` — hardened fetcher (design §3.7): + custom dialer that re-resolves, **rejects RFC 1918 / loopback / link-local + / ULA / metadata at connect time**, pins the resolved IP per + verify-control call; WebPKI + hostname verification on every fetch; 5 s + timeout; response cap; ≤5 redirects within the same registrable domain + (needs `golang.org/x/net/publicsuffix`); error `detail` never echoes + resolved IPs/redirect chains. +- `internal/adapter/didresolver/noop.go` — synthesizes from hints (§1). +- `internal/adapter/store/sqlite/identity.go` (+ link store) implementing new + `port.IdentityStore` / `port.IdentityLinkStore` in `internal/port/store.go`. + Challenge consumption is a **conditional + `UPDATE … WHERE challenge_consumed_at_ms IS NULL`** inside the + verify-success `uow.Run` transaction — the TOCTOU guard (§3.2). + +**Storage** — `internal/adapter/store/sqlite/migrations/006_identities.sql`: +verbatim design §5.7 (`identities` + `identity_links`, partial unique +indexes `idx_identities_live` on `(provider_id, kind, value) WHERE status != +'REVOKED'` and `idx_identities_proven` on `(kind, value) WHERE status = +'VERIFIED'` — first-to-prove wins, no squatting). Plus +`007_outbox_identity_lane.sql`: SQLite cannot widen a CHECK in place, so +rebuild `outbox_events` (create-copy-drop-rename inside the migration tx) +with `schema_version IN ('V1','V2','IDENTITY')`. + +**Service** (`internal/ra/service/identity.go`): + +- `Register` — infer/canonicalize kind, advisory resolve (noop: empty doc), + idempotent re-add (same owner + same value while `PENDING_CONTROL` → same + identityId, fresh nonce; `IDENTIFIER_DUPLICATE` only for genuine conflicts + per §4.2/§9 Q1), 202 with `{identityId, nonce, expiresAt, challenges[]}`. +- `VerifyControl` — authoritative re-resolve; per-proof checks in design + §3.6 order (payload-equality; `kid ∈ assertionMethod`; `{DID}#fragment` + rule; `controller == DID`; key-type allowlist; verify **every** JWS, + one bad proof fails closed; nonce fresh, consumed once in-tx); flip to + `VERIFIED` (or swap staged rotation) and **seal in the same flow**: + build `identityevent.Event`, JCS + sign **once** via the existing + `signAndMarshalPayload` pattern, enqueue with lane `IDENTITY` — + the outbox-replay invariant applies unchanged. +- `Rotate` (PUT — stage `pending_value`, old state stands, fresh challenges), + `Revoke` (POST — seal `IDENTITY_REVOKED`), `List`, `Detail`. + +**Outbox/tlclient:** `tlclient.Client` URL map gains +`IDENTITY → /v1/internal/identities/event`; worker untouched (lane passes +through `Sender.Append`). + +**HTTP** (`internal/ra/handler/identity.go` + routes in `cmd/ans-ra/main.go`): + +``` +POST /v2/ans/identities 202 + challenges +POST /v2/ans/identities/{identityId}/verify-control 200 VERIFIED (seals) +PUT /v2/ans/identities/{identityId} 202 + fresh challenges +POST /v2/ans/identities/{identityId}/revoke 200 (seals; POST, never DELETE) +GET /v2/ans/identities list (mine) +GET /v2/ans/identities/{identityId} detail +``` + +New `internal/ra/middleware/identityownership.go` mirroring the agent +ownership middleware (read → 404 hides existence, write → 403). RFC 7807 +codes added to the handler error map: `IDENTIFIER_KIND_UNSUPPORTED`, +`IDENTIFIER_DUPLICATE`, `IDENTIFIER_CHALLENGE_EXPIRED`, +`PRICC_SIGNATURE_INVALID`, `PRICC_TOKEN_EXPIRED`, `PRICC_TOKEN_ALREADY_USED`, +`DID_BAD_FORMAT`, `DID_RESOLUTION_FAILED`, `DID_DOCUMENT_ID_MISMATCH`, +`DID_REDIRECT_DOMAIN_MISMATCH`, `DID_VERIFICATION_METHOD_INVALID`. + +**Config:** `Identity` block in `internal/config/config.go` (§1 above); +`selectDIDResolver` in main.go; `config/ra-local.yaml` gains the block with +`resolver.type: noop`. + +**Spec:** `spec/api-spec-v2.yaml` — six identity routes + request/response/ +challenge schemas (design §5.2/§5.3), shape diff pasted in the PR. + +Per CLAUDE.md "no placeholder routes": the link routes and the RA-side +computed `identities[]` do **not** register in this PR — they land +implemented in PR 4. + +Tests: domain 100% (state machine, kind inference, challenge lifecycle); +proof-gate table tests (good/bad kid, wrong controller, external reference, +alg confusion, expired/consumed nonce, multi-key one-bad-fails-closed, +concurrent verify double-consume race via `make test-race`); web resolver +against `httptest.NewTLSServer` + SSRF dialer unit tests (denylist matrix, +rebind pin); noop resolver; handler 202/422/403/404 paths; end-to-end +RA→outbox→TL integration sealing `IDENTITY_VERIFIED` against the PR 1 lane. + +### PR 4 — `feat(ra): identity links + computed agent views + demo` + +Internal milestone "2b": the link mechanism + read-side joins + quickstart. + +- Service `Link`/`Unlink`: **single owner-gated call, no signature** (§4.3 — + caller's principal must own the identity **and every** agent; identity must + be `VERIFIED`); batch upsert against `idx_identity_links_live`; seal **one** + `IDENTITY_LINKED` carrying `ansIds[]` (chunk very large batches to bound + leaf size); unlink seals `IDENTITY_UNLINKED`. Cascade rules §4.4: agent + revocation emits zero identity events and vice versa — enforced by tests. +- Routes: `POST /v2/ans/identities/{identityId}/links` (200 `{linked: N}`), + `DELETE /v2/ans/identities/{identityId}/links/{agentId}`. +- RA `AgentDetails` gains the additive computed `identities[]` (design §5.4) + in lifecycle `Detail` — computed from the link + identity tables, never + stored on the registration; `agent_registrations` untouched. +- Spec deltas for both files. +- **Demo** (`scripts/demo/`): `identity-lifecycle.sh` against the noop + resolver — register identity → sign challenge → verify-control → link to + the demo agent → show TL badge `identities[]` → rotate (one event) → + revoke. JWS signing from shell needs a helper: small + `scripts/demo/signproof/main.go` run via `go run` (mints an Ed25519 key, + emits the compact JWS with `kid`+`jwk` headers over the served + `signingInput`). README quickstart section updated. + +### PR 5 — `feat(ra): did:key` + +Reuses every PR 3 seam; **zero I/O — no noop needed**, the keyless test +track (§2.2). + +- `internal/crypto/didkey.go` — decode `did:key:z…`: multibase base58btc + + multicodec varint (`0xed01` Ed25519; optionally `0x1200` P-256). Hand-roll + base58btc (~40 lines, fully testable) rather than adding multiformats deps. +- Kind dispatch: challenges return exactly one entry + (`kid = {did}#{method-specific-id}` per did:key convention); verify against + the key decoded **from the DID string**; `alg` pinned. +- Seal `keys[]` with thumbprint + JWK + signedProof (self-verifying; key also + derivable from the DID itself). +- Demo extension + table tests with fixed vectors. + +### PR 6 — `feat(verify): offline verification of IDENTITY_* leaves` + +`ans-verify` learns the identity stream so third parties can check seals +offline (design D5 / §5.6.3 verifier walk): + +- Parse identity envelopes; verify TL attestation JWS, producer JWS, + inclusion proof to checkpoint — all existing machinery, new inner shape. +- **Self-verifying key proofs**: verify each sealed `signedProof` against its + sealed `publicKeyJwk`; decode the payload and confirm it is an + `IdentityProofInput` binding this `identityId` + `identifier` + `purpose` + — offline, without trusting the RA. +- CLI surface consistent with existing verify commands. + +### PR 7 *(postponed — do not schedule)* — `lei` + +Design §3.6/§10.7 retained as design of record. When resumed: +`port.LEIControlVerifier` (present / authorization / verify-signature) with +a **noop adapter** + a real adapter for the internal GLEIF `vlei-verifier`; +fixed-host GLEIF L1 client (noop + real); present-once at register / +prove-repeatably at verify-control; AID pinned at presentation; +thumbprint-only sealing (PII rule). Until then the kind returns +`IDENTIFIER_KIND_UNSUPPORTED`. + +--- + +## 3. Cross-cutting rules (gates on every PR) + +1. **Event vocabulary is frozen by PR 1.** Sealed shapes are + append-only-forever; nothing seals to a shared TL until the five tokens + + payload schemas in PR 1 are reviewed against design §5.5 and the spec + delta is merged (design §5.9 amendment rows A–F note the upstream ANS-spec + ratification — track it in the PR description). +2. **Outbox-replay invariant** extends verbatim to identity events: payload + JCS-canonicalized + signed exactly once at enqueue; retries replay bytes; + TL dedups on content hash. +3. **No placeholder routes.** Every route registers only in the PR that + implements it end-to-end; unsupported kinds are `IDENTIFIER_KIND_UNSUPPORTED` + at dispatch, never a stubbed seal. +4. **Streams never cross at write time.** Identity operations write zero + agent-stream events and vice versa; all propagation is read-time join. + Every PR 2/4 test suite asserts the negative. +5. **Minimal abstraction** (design §6): per-kind control logic starts as a + switch + functions inside the identity service; no `port.ControlVerifier` + until did:key is the second real caller (extract the seam in PR 5 only if + it pays for itself). No `LocationChallenger` interface — one implementer + (ACME) exists and it stays where it is. +6. **Coverage**: `internal/domain` 100%; `internal/crypto` 100% target + (≥95% with annotated SAFETY/NOTE exceptions); overall ≥90% via + `make test-cover`. `make check` before every commit; `make test-race` for + the nonce-consumption race. +7. **Commits**: conventional commits (`feat(tl):`, `feat(ra):` …), + `git commit -s` (DCO), GPG-signed, **no AI Co-Authored-By trailers**. + +--- + +## 4. Open decisions (flagged for review before/during PR 1 & 3) + +| # | Decision | Recommendation | Where it lands | +|---|---|---|---| +| 1 | Outbox lane mechanism: widen `schema_version` CHECK (table rebuild) vs dispatch on `IDENTITY_*` event-type prefix in the worker | **Widen the CHECK** — explicit lane column beats stringly dispatch; SQLite rebuild migration is routine | PR 3, migration 007 | +| 2 | Noop resolver semantics: synthesize from `jwk`-header hints (real JWS verification, waived web binding) vs accept-any | **Hint synthesis** — keeps sealed events self-verifying even from quickstart runs | PR 3, §1 above | +| 3 | `IdentityProofInput.raId` source | **Reuse `cfg.Signer.RAID`** — one RA identity everywhere; deployment docs say to set it to the external base URL per design §3.2 | PR 3 | +| 4 | did:key multibase: hand-rolled base58btc vs `go-multiformats` deps | **Hand-roll** (~40 lines + vectors) — two deps for one decode is poor trade | PR 5 | +| 5 | Key-type allowlist v1 | **Ed25519 + ECDSA P-256** (matches go-jose EdDSA/ES256 and the TL's own P-256 posture) | PR 3 | +| 6 | Must linked agents be `ACTIVE` at link time, or any live (non-terminal) status? | **Any live status** — effectiveness is computed at read time anyway (§5.6.3); blocking on ACTIVE adds a write-time race for no read-time gain | PR 4 | +| 7 | Identity envelope `schemaVersion` token | **`"V2"`** — same producer-lane generation; lane separation is the route + event family, not the version string | PR 1 | +| 8 | Receipt table generalization for identity leaves (`tl_receipts`) | Nullable `identity_id` column in migration 003 | PR 2 | + +--- + +## 5. What this plan explicitly does NOT do + +- No change to the agent registration path — `AgentRegistration`, + `agent_registrations`, the ACME/CSR/BYOC flows, and `AGENT_*` event shapes + are byte-for-byte untouched (design §2.1, §5.1). +- No keyless *primary* agents (§2.3) — the policy guard stands; did:key + exercises the keyless path as a linked identity only. +- No cross-owner links, no `EquivalenceLink`, no `clientCatalog` (§2.3). +- No AIM/monitoring emission to the TL (§4.6 — operator alerting concern, + out of scope for this repo today). +- V1 RA/TL lanes frozen — zero identity surface on `/v1/agents/*` RA routes + or the V1 ingest lane. From 654253a5b6fb64af2253a59b4e65eb855aa49dc4 Mon Sep 17 00:00:00 2001 From: Connor Snitker Date: Thu, 11 Jun 2026 18:29:03 -0500 Subject: [PATCH 05/12] feat(ra): seal-before-success for the identity lane + race guards MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Implements the 2026-06-11 design-review deltas on the RA side (design §5.6.1 item 6, §4.3, §3.2, §3.6): - Identity operations (verify-control, revoke, link, unlink) now seal SYNCHRONOUSLY and report success only after the TL acknowledges the seal; the row transition commits with the acknowledgment, so the RA row can never be ahead of the log. The IDENTITY lane leaves the outbox entirely: sign once, submit once — a failed delivery IS a failed operation (503 TL_UNAVAILABLE, retryable, nothing consumed). A nil/disabled TL client fails identity sealing closed. - A short-TTL provisional nonce claim (migration 008) serializes concurrent verify attempts across the seal round trip (VERIFICATION_IN_FLIGHT to the loser); failed attempts release it, preserving the failed-attempts-don't-consume rule. - Conditional commits replace every blind save a racing operation could clobber: MarkRevoked flips VERIFIED->REVOKED only while still VERIFIED; Link re-checks the identity in its commit tx (a revoke landing during the seal gains no live link rows); StageChallenge persists a re-add/rotate challenge conditional on the load-time status+nonce with no live claim, and never writes status. - Link liveness gate: every agent in the batch must be ACTIVE or DEPRECATED — terminal or pre-activation agents fail the whole batch with 422 AGENT_NOT_LINKABLE. New per-owner link/unlink rate limit (identity.link-rate-limit, default 60/min). - RA reads follow the visibility predicate: AgentDetails.identities[] is empty for a terminal agent; the identity detail's linked list drops links to terminal agents and surfaces (not swallows) agent lookup failures. - GET /v2/ans/identities adopts the v2 limit + opaque-cursor envelope; JWS proof decoding rejects crit headers (RFC 7515 $4.1.11); did:web path segments additionally reject '.', '..', and control bytes. Race-pinning tests simulate a concurrent revoke committing inside the seal round trip via a sealer hook; spec/api-spec-v2.yaml documents the new codes and seal-before-success semantics. Signed-off-by: Connor Snitker --- cmd/ans-ra/main.go | 18 +- internal/adapter/docsui/openapi/ra.yaml | 100 +++- internal/adapter/store/sqlite/identity.go | 197 ++++++- .../adapter/store/sqlite/identity_test.go | 188 +++++- .../migrations/008_identity_seal_claim.sql | 10 + internal/adapter/tlclient/client.go | 28 + internal/adapter/tlclient/client_test.go | 54 ++ internal/config/config.go | 8 + internal/config/defaults.go | 2 + internal/crypto/jws.go | 14 + internal/crypto/jws_crit_test.go | 26 + internal/domain/errors.go | 10 + internal/domain/identity.go | 15 + internal/domain/identity_test.go | 28 + internal/port/store.go | 40 +- internal/ra/handler/errors.go | 4 + internal/ra/handler/errors_internal_test.go | 38 ++ internal/ra/handler/identity.go | 27 +- internal/ra/handler/identity_handler_test.go | 84 ++- internal/ra/service/identity.go | 420 ++++++++++---- internal/ra/service/identity_test.go | 536 +++++++++++++++--- internal/ra/service/identityratelimit.go | 19 +- spec/api-spec-v2.yaml | 100 +++- 23 files changed, 1741 insertions(+), 225 deletions(-) create mode 100644 internal/adapter/store/sqlite/migrations/008_identity_seal_claim.sql create mode 100644 internal/crypto/jws_crit_test.go create mode 100644 internal/ra/handler/errors_internal_test.go diff --git a/cmd/ans-ra/main.go b/cmd/ans-ra/main.go index bc1c032..1464706 100644 --- a/cmd/ans-ra/main.go +++ b/cmd/ans-ra/main.go @@ -187,15 +187,27 @@ func run(cfgPath string) error { // Verified identities — the "who" behind the agents. Shares the // producer signer with the registration service: one RA, one - // producer identity on every TL lane. + // producer identity on every TL lane. Identity events seal + // SYNCHRONOUSLY (seal-before-success, design §5.6.1): they never + // ride the outbox, so the service gets the TL client directly. A + // disabled TL client means identity sealing operations fail + // closed with TL_UNAVAILABLE — there is no "seal later" mode. + var identitySealer service.IdentityEventSealer + if !cfg.TLClient.Disabled { + identitySealer = tlclient.New(cfg.TLClient.BaseURL, cfg.TLClient.APIKey, cfg.TLClient.Timeout) + } else { + logger.Warn().Msg("TL client disabled — identity verify/revoke/link operations will fail with TL_UNAVAILABLE (seal-before-success)") + } identitySvc := service.NewIdentityService( - identityStore, identityLinks, agents, didResolver, outbox, db, + identityStore, identityLinks, agents, didResolver, identitySealer, db, ).WithSigner(service.EventSigner{ KeyManager: km, KeyID: signerKeyID, RaID: cfg.Signer.RaID, }).WithChallengeTTL(cfg.Identity.ChallengeTTL). - WithRegisterRateLimit(cfg.Identity.RegisterRateLimit) + WithRegisterRateLimit(cfg.Identity.RegisterRateLimit). + WithLinkRateLimit(cfg.Identity.LinkRateLimit). + WithSealTimeout(cfg.Identity.SealTimeout) // HTTP. r := chi.NewRouter() diff --git a/internal/adapter/docsui/openapi/ra.yaml b/internal/adapter/docsui/openapi/ra.yaml index 98073c1..88eef90 100644 --- a/internal/adapter/docsui/openapi/ra.yaml +++ b/internal/adapter/docsui/openapi/ra.yaml @@ -966,20 +966,39 @@ paths: tags: - Verified Identities summary: List my identities - description: Returns every identity owned by the caller, newest first. + description: | + Returns one page of the caller's identities, newest first, in + the standard limit + opaque-cursor envelope (the agent-list + convention). Pass `nextCursor` back as `cursor` for the next + page; a null `nextCursor` is the last page. operationId: listIdentities + parameters: + - name: limit + in: query + required: false + schema: { type: integer, default: 20, minimum: 1, maximum: 100 } + - name: cursor + in: query + required: false + schema: { type: string } + description: Opaque cursor from the previous page's nextCursor responses: '200': - description: The caller's identities + description: One page of the caller's identities content: application/json: schema: type: object + required: [identities, nextCursor] properties: identities: type: array items: $ref: '#/components/schemas/IdentityDetails' + nextCursor: + type: string + nullable: true + description: Cursor for the next page; null on the last page '401': description: Authentication failed content: @@ -1081,11 +1100,19 @@ paths: closed. The nonce is consumed exactly once, inside the success transaction — a failed attempt does not consume it. - On success the identity flips to `VERIFIED` (or completes a - staged rotation) and the RA seals `IDENTITY_VERIFIED` / + JWS bearing unrecognized `crit` header parameters are + rejected (RFC 7515 §4.1.11) — this verifier implements no + critical extensions. + + Seal-before-success: the RA seals `IDENTITY_VERIFIED` / `IDENTITY_UPDATED` on the identity's own Transparency Log - stream, with every proven key sealed self-verifyingly - (public key + signed proof). + stream — every proven key sealed self-verifyingly (public + key + signed proof) — and reports success ONLY after the TL + acknowledges the seal; the row transition commits with that + acknowledgment, so anything this API reports as set up is + resolvable in the TL at that moment. If the TL is + unavailable the call fails 503 `TL_UNAVAILABLE`, retryable: + the nonce is NOT consumed and the prior state stands. operationId: verifyIdentityControl parameters: - $ref: '#/components/parameters/IdentityIdPath' @@ -1118,8 +1145,12 @@ paths: description: | Challenge state error — `PRICC_TOKEN_EXPIRED`, `PRICC_TOKEN_ALREADY_USED`, `IDENTIFIER_CHALLENGE_EXPIRED` - — or the identity is revoked. Recovery from an expired - nonce is the idempotent re-add (re-POST the same value). + — the identity is revoked, a concurrent verify attempt + holds the seal claim (`VERIFICATION_IN_FLIGHT` — retry + shortly; the claim expires within ~30s), or the + identifier is already verified by another owner + (`IDENTIFIER_DUPLICATE`). Recovery from an expired nonce + is the idempotent re-add (re-POST the same value). content: application/json: schema: @@ -1134,6 +1165,13 @@ paths: schema: $ref: '#/components/schemas/ErrorResponse' + '503': + description: Transparency Log unavailable (TL_UNAVAILABLE — retryable; the nonce is NOT consumed) + content: + application/json: + schema: + $ref: '#/components/schemas/ErrorResponse' + /ans/identities/{identityId}/revoke: post: tags: @@ -1146,6 +1184,10 @@ paths: append-only in the Transparency Log. Propagation to every linked agent's badge is the TL's read-time join, not a write fan-out. + + Seal-before-success: the row flips only after the TL + acknowledges the seal. TL unavailable → 503 `TL_UNAVAILABLE`, + retryable, the identity stays VERIFIED. operationId: revokeIdentity parameters: - $ref: '#/components/parameters/IdentityIdPath' @@ -1174,6 +1216,12 @@ paths: application/json: schema: $ref: '#/components/schemas/ErrorResponse' + '503': + description: Transparency Log unavailable (TL_UNAVAILABLE — retryable, nothing changed) + content: + application/json: + schema: + $ref: '#/components/schemas/ErrorResponse' /ans/identities/{identityId}/links: post: @@ -1189,7 +1237,19 @@ paths: the agent ids; an agent's own stream and audit history are never written by identity operations. Already-linked agents are skipped idempotently; a call that links nothing new seals - nothing. Links attach only while the identity is VERIFIED. + nothing. + + Liveness gate: links attach only while the identity is + VERIFIED, and every named agent must be live — ACTIVE or + DEPRECATED; a terminal or pre-activation agent fails the + whole batch with 422 `AGENT_NOT_LINKABLE` (all-or-nothing, + matching the one-event batch semantics). The route is + per-owner rate limited (429-equivalent `RATE_LIMITED`). + + Seal-before-success: success is reported only after the TL + acknowledges the `IDENTITY_LINKED` seal; link rows commit + with the acknowledgment. TL unavailable → 503 + `TL_UNAVAILABLE`, retryable, nothing linked. operationId: linkIdentityAgents parameters: - $ref: '#/components/parameters/IdentityIdPath' @@ -1225,7 +1285,14 @@ paths: schema: $ref: '#/components/schemas/ErrorResponse' '422': - description: Invalid link request (empty or oversized batch) + description: Invalid link request (empty or oversized batch — INVALID_LINK_REQUEST), or a named agent is not live (AGENT_NOT_LINKABLE — links require ACTIVE or DEPRECATED; rejected all-or-nothing) + content: + application/json: + schema: + $ref: '#/components/schemas/ErrorResponse' + + '503': + description: Transparency Log unavailable (TL_UNAVAILABLE — retryable, nothing linked) content: application/json: schema: @@ -1240,7 +1307,12 @@ paths: Ends one association and seals `IDENTITY_UNLINKED` on the identity stream. The association's history persists in the identity's audit chain and the raw log tiles; unlinked pairs - may be re-linked later. + may be re-linked later. Shares the link route's per-owner + rate limit. + + Seal-before-success: the link row flips only after the TL + acknowledges the seal. TL unavailable → 503 `TL_UNAVAILABLE`, + retryable, the link stands. operationId: unlinkIdentityAgent parameters: - $ref: '#/components/parameters/IdentityIdPath' @@ -1260,6 +1332,12 @@ paths: application/json: schema: $ref: '#/components/schemas/ErrorResponse' + '503': + description: Transparency Log unavailable (TL_UNAVAILABLE — retryable, nothing changed) + content: + application/json: + schema: + $ref: '#/components/schemas/ErrorResponse' components: diff --git a/internal/adapter/store/sqlite/identity.go b/internal/adapter/store/sqlite/identity.go index daf56af..ea35bc4 100644 --- a/internal/adapter/store/sqlite/identity.go +++ b/internal/adapter/store/sqlite/identity.go @@ -3,10 +3,12 @@ package sqlite import ( "context" "database/sql" + "encoding/base64" "errors" "time" "github.com/godaddy/ans/internal/domain" + "github.com/godaddy/ans/internal/port" ) // IdentityStore implements port.IdentityStore. @@ -94,6 +96,12 @@ func (s *IdentityStore) Save(ctx context.Context, v *domain.VerifiedIdentity) er challenge_nonce = excluded.challenge_nonce, challenge_expires_at_ms = excluded.challenge_expires_at_ms, challenge_consumed_at_ms = excluded.challenge_consumed_at_ms, + -- Every save resets the provisional seal claim: a save + -- either issues a new challenge (the claim must not leak + -- into the new nonce's epoch), commits a verified flip + -- (nonce consumed, claim moot), or revokes (challenge + -- moot). See ClaimChallenge. + challenge_claimed_at_ms = NULL, verified_at_ms = excluded.verified_at_ms, updated_at_ms = excluded.updated_at_ms` _, err := s.db.extx(ctx).ExecContext(ctx, q, @@ -155,21 +163,53 @@ func (s *IdentityStore) ExistsVerified(ctx context.Context, kind domain.Identifi // ListByOwner returns every identity owned by the principal, newest // first. -func (s *IdentityStore) ListByOwner(ctx context.Context, providerID string) ([]*domain.VerifiedIdentity, error) { +func (s *IdentityStore) ListByOwner(ctx context.Context, providerID string, limit int, cursor string) (*port.CursorPage[*domain.VerifiedIdentity], error) { var rows []identityRow - err := s.db.extx(ctx).SelectContext(ctx, &rows, - `SELECT `+identityCols+` + // identity_id is a UUIDv7 — lexicographic order IS creation + // order — so the id doubles as a stable cursor. + const defaultLimit, maxLimit = 20, 100 + if limit <= 0 { + limit = defaultLimit + } + if limit > maxLimit { + limit = maxLimit + } + q := `SELECT ` + identityCols + ` FROM identities - WHERE provider_id = ? - ORDER BY created_at_ms DESC, identity_id DESC`, providerID) + WHERE provider_id = ?` + args := []any{providerID} + if cursor != "" { + raw, err := base64.RawURLEncoding.DecodeString(cursor) + if err != nil { + return nil, domain.NewValidationError("INVALID_CURSOR", "malformed cursor") + } + q += ` AND identity_id < ?` + args = append(args, string(raw)) + } + q += ` ORDER BY identity_id DESC LIMIT ?` + args = append(args, limit+1) + + err := s.db.extx(ctx).SelectContext(ctx, &rows, q, args...) if err != nil { return nil, mapSQLErr(err) } + hasMore := len(rows) > limit + if hasMore { + rows = rows[:limit] + } out := make([]*domain.VerifiedIdentity, 0, len(rows)) for _, r := range rows { out = append(out, r.toDomain()) } - return out, nil + page := &port.CursorPage[*domain.VerifiedIdentity]{ + Items: out, + HasMore: hasMore, + ReturnedCount: len(out), + } + if hasMore && len(out) > 0 { + page.NextCursor = base64.RawURLEncoding.EncodeToString([]byte(out[len(out)-1].IdentityID)) + } + return page, nil } // ConsumeChallenge atomically consumes the live challenge nonce. The @@ -199,6 +239,151 @@ func (s *IdentityStore) ConsumeChallenge(ctx context.Context, identityID, nonce return nil } +// ClaimChallenge takes the short-TTL provisional claim that +// serializes concurrent verify-control attempts across the +// seal-before-success TL round trip (design §5.6.1). The conditional +// UPDATE succeeds only while the nonce is live, unconsumed, and not +// claimed by an attempt fresher than staleBefore — a crashed +// claimer's stale claim is reclaimable. A claim is NOT consumption. +func (s *IdentityStore) ClaimChallenge(ctx context.Context, identityID, nonce string, now, staleBefore time.Time) error { + res, err := s.db.extx(ctx).ExecContext(ctx, ` + UPDATE identities + SET challenge_claimed_at_ms = ?, updated_at_ms = ? + WHERE identity_id = ? + AND challenge_nonce = ? + AND challenge_consumed_at_ms IS NULL + AND challenge_expires_at_ms > ? + AND (challenge_claimed_at_ms IS NULL OR challenge_claimed_at_ms < ?)`, + now.UnixMilli(), now.UnixMilli(), identityID, nonce, + now.UnixMilli(), staleBefore.UnixMilli()) + if err != nil { + return mapSQLErr(err) + } + n, err := res.RowsAffected() + if err != nil { + return err + } + if n != 1 { + return domain.NewInvalidStateError("VERIFICATION_IN_FLIGHT", + "a verify-control attempt for this challenge is already in flight, or the nonce is consumed, expired, or superseded") + } + return nil +} + +// ReleaseChallenge releases a provisional claim after a failed +// attempt (failed attempts never consume — §3.2). Best-effort: a +// consumed or superseded nonce matches zero rows, which is fine. +func (s *IdentityStore) ReleaseChallenge(ctx context.Context, identityID, nonce string) error { + _, err := s.db.extx(ctx).ExecContext(ctx, ` + UPDATE identities + SET challenge_claimed_at_ms = NULL + WHERE identity_id = ? + AND challenge_nonce = ? + AND challenge_consumed_at_ms IS NULL`, + identityID, nonce) + if err != nil { + return mapSQLErr(err) + } + return nil +} + +// StageChallenge persists a freshly issued challenge onto an +// existing row, conditional on the load-time snapshot: the status +// must be unchanged, the nonce must still be the one observed (a +// concurrent verify consumes it; a concurrent re-add supersedes it), +// and no fresh seal claim may be live (an in-flight verify must not +// have its nonce yanked mid-seal). Status, value, and verified_at +// are deliberately NOT written — issuing a challenge is not a state +// transition, so a racing commit can never be clobbered. +func (s *IdentityStore) StageChallenge( + ctx context.Context, + v *domain.VerifiedIdentity, + expectedStatus domain.IdentityStatus, + expectedNonce string, + staleBefore time.Time, +) error { + if v.Challenge == nil { + return domain.NewInternalError("CHALLENGE_STAGE", "no challenge on the aggregate", errors.New("nil challenge")) + } + var expires sql.NullInt64 + if !v.Challenge.ExpiresAt.IsZero() { + expires = sql.NullInt64{Int64: v.Challenge.ExpiresAt.UnixMilli(), Valid: true} + } + res, err := s.db.extx(ctx).ExecContext(ctx, ` + UPDATE identities + SET challenge_nonce = ?, + challenge_expires_at_ms = ?, + challenge_consumed_at_ms = NULL, + challenge_claimed_at_ms = NULL, + pending_value = ?, + updated_at_ms = ? + WHERE identity_id = ? + AND status = ? + AND (challenge_nonce = ? OR (challenge_nonce IS NULL AND ? = '')) + AND (challenge_claimed_at_ms IS NULL OR challenge_claimed_at_ms < ?)`, + v.Challenge.Nonce, expires, v.PendingValue, v.UpdatedAt.UnixMilli(), + v.IdentityID, string(expectedStatus), expectedNonce, expectedNonce, + staleBefore.UnixMilli()) + if err != nil { + return mapSQLErr(err) + } + n, err := res.RowsAffected() + if err != nil { + return err + } + if n == 1 { + return nil + } + // Lost the race — re-read for the precise refusal. + current, rerr := s.FindByID(ctx, v.IdentityID) + if rerr != nil { + return rerr + } + switch { + case current.Status == domain.IdentityRevoked: + return domain.NewInvalidStateError("IDENTITY_REVOKED", "identity was revoked") + case current.Status == domain.IdentityVerified && expectedStatus != domain.IdentityVerified: + return domain.NewConflictError("IDENTIFIER_DUPLICATE", + "identifier was verified concurrently; rotate it with PUT instead") + default: + return domain.NewConflictError("VERIFICATION_IN_FLIGHT", + "a verify-control attempt is in flight or the challenge was superseded; retry shortly") + } +} + +// MarkRevoked is Revoke's conditional Phase C commit: flip VERIFIED +// → REVOKED, clearing the rotation stage and challenge state, only +// while the row is still VERIFIED — a verify/rotate that committed +// during the revoke's seal round trip is never overwritten with the +// revoker's stale snapshot. Zero rows → conflict (the caller's view +// was stale; the sealed IDENTITY_REVOKED is the benign residue the +// retry converges with). +func (s *IdentityStore) MarkRevoked(ctx context.Context, identityID string, now time.Time) error { + res, err := s.db.extx(ctx).ExecContext(ctx, ` + UPDATE identities + SET status = 'REVOKED', + pending_value = '', + challenge_nonce = NULL, + challenge_expires_at_ms = NULL, + challenge_consumed_at_ms = NULL, + challenge_claimed_at_ms = NULL, + updated_at_ms = ? + WHERE identity_id = ? AND status = 'VERIFIED'`, + now.UnixMilli(), identityID) + if err != nil { + return mapSQLErr(err) + } + n, err := res.RowsAffected() + if err != nil { + return err + } + if n != 1 { + return domain.NewConflictError("IDENTITY_CONCURRENTLY_MODIFIED", + "identity changed during the revoke; re-read and retry") + } + return nil +} + // IdentityLinkStore implements port.IdentityLinkStore. type IdentityLinkStore struct{ db *DB } diff --git a/internal/adapter/store/sqlite/identity_test.go b/internal/adapter/store/sqlite/identity_test.go index 7c90608..9644f62 100644 --- a/internal/adapter/store/sqlite/identity_test.go +++ b/internal/adapter/store/sqlite/identity_test.go @@ -191,13 +191,30 @@ func TestIdentityStore_ListByOwner(t *testing.T) { } } - got, err := store.ListByOwner(ctx, "owner-1") + page, err := store.ListByOwner(ctx, "owner-1", 0, "") if err != nil { t.Fatal(err) } + got := page.Items if len(got) != 2 || got[0].IdentityID != "id-2" || got[1].IdentityID != "id-1" { t.Fatalf("list wrong: %+v", got) } + if page.HasMore || page.NextCursor != "" { + t.Fatalf("two rows fit one default page: %+v", page) + } + + // Cursor pagination: limit 1 → first row + cursor → second row. + first1, err := store.ListByOwner(ctx, "owner-1", 1, "") + if err != nil || len(first1.Items) != 1 || first1.Items[0].IdentityID != "id-2" || !first1.HasMore { + t.Fatalf("page 1: %+v (%v)", first1, err) + } + page2, err := store.ListByOwner(ctx, "owner-1", 1, first1.NextCursor) + if err != nil || len(page2.Items) != 1 || page2.Items[0].IdentityID != "id-1" || page2.HasMore { + t.Fatalf("page 2: %+v (%v)", page2, err) + } + if _, err := store.ListByOwner(ctx, "owner-1", 1, "%%%not-base64%%%"); err == nil { + t.Fatal("malformed cursor must be rejected") + } } func TestIdentityStore_ConsumeChallenge(t *testing.T) { @@ -297,3 +314,172 @@ func TestIdentityLinkStore_Lifecycle(t *testing.T) { t.Fatalf("re-link after unlink: created=%v err=%v", created, err) } } + +func TestIdentityStore_ClaimAndReleaseChallenge(t *testing.T) { + db := newTestDB(t) + store := NewIdentityStore(db) + ctx := context.Background() + + v := newIdentityFixture(t, "id-claim", "owner-1", "did:web:claim.example.com") + if err := v.IssueChallenge("nonce-c", time.Hour, identityNow); err != nil { + t.Fatal(err) + } + if err := store.Save(ctx, v); err != nil { + t.Fatal(err) + } + + now := identityNow.Add(time.Minute) + staleBefore := now.Add(-30 * time.Second) + + // First claim wins; a second concurrent claim loses while the + // first is fresh. + if err := store.ClaimChallenge(ctx, "id-claim", "nonce-c", now, staleBefore); err != nil { + t.Fatalf("claim: %v", err) + } + err := store.ClaimChallenge(ctx, "id-claim", "nonce-c", now.Add(time.Second), now.Add(time.Second).Add(-30*time.Second)) + if err == nil || !strings.Contains(err.Error(), "VERIFICATION_IN_FLIGHT") { + t.Fatalf("second claim must lose: %v", err) + } + + // Release → claimable again (failed attempts never consume). + if err := store.ReleaseChallenge(ctx, "id-claim", "nonce-c"); err != nil { + t.Fatal(err) + } + if err := store.ClaimChallenge(ctx, "id-claim", "nonce-c", now.Add(2*time.Second), now.Add(2*time.Second).Add(-30*time.Second)); err != nil { + t.Fatalf("re-claim after release: %v", err) + } + + // A stale claim (crashed claimer) is reclaimable after the TTL. + later := now.Add(time.Minute) + if err := store.ClaimChallenge(ctx, "id-claim", "nonce-c", later, later.Add(-30*time.Second)); err != nil { + t.Fatalf("stale claim must be reclaimable: %v", err) + } + + // Consumption beats any claim; a consumed nonce is unclaimable. + if err := store.ConsumeChallenge(ctx, "id-claim", "nonce-c", later.Add(time.Second)); err != nil { + t.Fatalf("consume: %v", err) + } + if err := store.ClaimChallenge(ctx, "id-claim", "nonce-c", later.Add(2*time.Second), later.Add(2*time.Second)); err == nil { + t.Fatal("consumed nonce must not be claimable") + } + // Releasing a consumed nonce is a harmless no-op. + if err := store.ReleaseChallenge(ctx, "id-claim", "nonce-c"); err != nil { + t.Fatal(err) + } + + // A fresh challenge (Save) resets any prior claim epoch. + if err := v.IssueChallenge("nonce-d", time.Hour, later); err != nil { + t.Fatal(err) + } + if err := store.Save(ctx, v); err != nil { + t.Fatal(err) + } + fresh := later.Add(3 * time.Second) + if err := store.ClaimChallenge(ctx, "id-claim", "nonce-d", fresh, fresh.Add(-30*time.Second)); err != nil { + t.Fatalf("new nonce epoch must be claimable: %v", err) + } +} + +func TestIdentityStore_StageChallengeOptimisticConcurrency(t *testing.T) { + db := newTestDB(t) + store := NewIdentityStore(db) + ctx := context.Background() + + v := newIdentityFixture(t, "id-stage", "owner-1", "did:web:stage.example.com") + if err := v.IssueChallenge("nonce-1", time.Hour, identityNow); err != nil { + t.Fatal(err) + } + if err := store.Save(ctx, v); err != nil { + t.Fatal(err) + } + now := identityNow.Add(time.Minute) + stale := now.Add(-30 * time.Second) + + // Happy path: snapshot matches → fresh nonce persists, status + // untouched. + loaded, _ := store.FindByID(ctx, "id-stage") + if err := loaded.IssueChallenge("nonce-2", time.Hour, now); err != nil { + t.Fatal(err) + } + if err := store.StageChallenge(ctx, loaded, domain.IdentityPendingControl, "nonce-1", stale); err != nil { + t.Fatalf("stage: %v", err) + } + got, _ := store.FindByID(ctx, "id-stage") + if got.Challenge.Nonce != "nonce-2" || got.Status != domain.IdentityPendingControl { + t.Fatalf("staged state: %+v", got) + } + + // A stale snapshot (nonce superseded) is refused — never clobbers. + stale2 := loaded + if err := stale2.IssueChallenge("nonce-3", time.Hour, now); err != nil { + t.Fatal(err) + } + err := store.StageChallenge(ctx, stale2, domain.IdentityPendingControl, "nonce-1", stale) + if err == nil || !strings.Contains(err.Error(), "VERIFICATION_IN_FLIGHT") { + t.Fatalf("superseded snapshot must be refused: %v", err) + } + + // A live seal claim blocks re-challenge (it would yank the nonce + // out from under an in-flight verify). + if err := store.ClaimChallenge(ctx, "id-stage", "nonce-2", now, stale); err != nil { + t.Fatal(err) + } + fresh, _ := store.FindByID(ctx, "id-stage") + if err := fresh.IssueChallenge("nonce-4", time.Hour, now); err != nil { + t.Fatal(err) + } + err = store.StageChallenge(ctx, fresh, domain.IdentityPendingControl, "nonce-2", stale) + if err == nil || !strings.Contains(err.Error(), "VERIFICATION_IN_FLIGHT") { + t.Fatalf("live claim must block re-challenge: %v", err) + } + + // A concurrently committed VERIFIED flip maps to the precise + // duplicate refusal. + if err := store.ReleaseChallenge(ctx, "id-stage", "nonce-2"); err != nil { + t.Fatal(err) + } + committed, _ := store.FindByID(ctx, "id-stage") + committed.Status = domain.IdentityVerified + committed.VerifiedAt = now + if err := store.Save(ctx, committed); err != nil { + t.Fatal(err) + } + again, _ := store.FindByID(ctx, "id-stage") + again.Status = domain.IdentityPendingControl // simulate the stale loader's view + if err := again.IssueChallenge("nonce-5", time.Hour, now); err != nil { + t.Fatal(err) + } + err = store.StageChallenge(ctx, again, domain.IdentityPendingControl, "nonce-2", stale) + if err == nil || !strings.Contains(err.Error(), "IDENTIFIER_DUPLICATE") { + t.Fatalf("concurrent verify must map to IDENTIFIER_DUPLICATE: %v", err) + } +} + +func TestIdentityStore_MarkRevokedConditional(t *testing.T) { + db := newTestDB(t) + store := NewIdentityStore(db) + ctx := context.Background() + + v := newIdentityFixture(t, "id-mr", "owner-1", "did:web:mr.example.com") + v.Status = domain.IdentityVerified + v.VerifiedAt = identityNow + if err := store.Save(ctx, v); err != nil { + t.Fatal(err) + } + now := identityNow.Add(time.Minute) + + if err := store.MarkRevoked(ctx, "id-mr", now); err != nil { + t.Fatalf("revoke: %v", err) + } + got, _ := store.FindByID(ctx, "id-mr") + if got.Status != domain.IdentityRevoked || got.PendingValue != "" || got.Challenge != nil { + t.Fatalf("revoked state: %+v", got) + } + + // Second revoke (or revoke of a non-VERIFIED row): conflict, no + // clobber. + err := store.MarkRevoked(ctx, "id-mr", now.Add(time.Second)) + if err == nil || !strings.Contains(err.Error(), "IDENTITY_CONCURRENTLY_MODIFIED") { + t.Fatalf("conditional revoke must conflict on a non-VERIFIED row: %v", err) + } +} diff --git a/internal/adapter/store/sqlite/migrations/008_identity_seal_claim.sql b/internal/adapter/store/sqlite/migrations/008_identity_seal_claim.sql new file mode 100644 index 0000000..a263366 --- /dev/null +++ b/internal/adapter/store/sqlite/migrations/008_identity_seal_claim.sql @@ -0,0 +1,10 @@ +-- Seal-before-success (design §5.6.1 item 6): identity operations +-- return success only after the TL acknowledges the seal, so the +-- verify path now spans a network call. challenge_claimed_at_ms is a +-- short-TTL provisional claim taken BEFORE the seal — it serializes +-- concurrent verify-control attempts on one nonce so at most one +-- in-flight attempt can seal (the conditional consume remains the +-- authoritative guard at commit). A claim is NOT consumption: failed +-- attempts release it, and a stale claim (crashed claimer) is +-- reclaimable after the claim TTL. +ALTER TABLE identities ADD COLUMN challenge_claimed_at_ms INTEGER; diff --git a/internal/adapter/tlclient/client.go b/internal/adapter/tlclient/client.go index d898edc..ca21ab4 100644 --- a/internal/adapter/tlclient/client.go +++ b/internal/adapter/tlclient/client.go @@ -35,6 +35,8 @@ import ( "io" "net/http" "time" + + "github.com/godaddy/ans/internal/domain" ) // AppendResult is the parsed success response from the TL's ingest @@ -238,3 +240,29 @@ func IsPermanent(err error) bool { var p *PermanentError return errors.As(err, &p) } + +// SealIdentityEvent submits a producer-signed identity event on the +// IDENTITY lane and returns only after the TL acknowledges the seal — +// the client half of seal-before-success (design §5.6.1). Identity +// events never ride the outbox: delivery precedes success, and a +// failed delivery IS a failed operation, so errors are mapped to +// domain kinds the service layer can return directly: +// +// - transient (transport, 5xx, 429) → ErrUnavailable / 503 +// TL_UNAVAILABLE: retryable, the caller consumed nothing; +// - permanent (other 4xx) → ErrInternal / +// TL_REJECTED_EVENT: the RA produced an event the TL refuses — +// a pipeline bug, not weather; operators must see it. +func (c *Client) SealIdentityEvent(ctx context.Context, innerCanonical []byte, producerSig string) error { + _, err := c.Append(ctx, "IDENTITY", innerCanonical, producerSig) + switch { + case err == nil: + return nil + case IsTransient(err): + return domain.NewUnavailableError("TL_UNAVAILABLE", + "the transparency log did not confirm the seal; the operation is retryable and nothing was consumed") + default: + return domain.NewInternalError("TL_REJECTED_EVENT", + "the transparency log rejected the identity event", err) + } +} diff --git a/internal/adapter/tlclient/client_test.go b/internal/adapter/tlclient/client_test.go index 8e529f6..33b1ddb 100644 --- a/internal/adapter/tlclient/client_test.go +++ b/internal/adapter/tlclient/client_test.go @@ -10,6 +10,8 @@ import ( "time" "github.com/godaddy/ans/internal/adapter/tlclient" + + "github.com/godaddy/ans/internal/domain" ) func TestAppend_Created(t *testing.T) { @@ -273,3 +275,55 @@ func TestAppend_UnknownSchemaVersion(t *testing.T) { t.Fatal("expected error on unknown schemaVersion") } } + +// TestSealIdentityEvent_DomainErrorMapping pins the seal-before- +// success client wrapper: success on TL ack; transient failures map +// to ErrUnavailable (TL_UNAVAILABLE — retryable, nothing consumed); +// TL schema rejections map to an internal TL_REJECTED_EVENT. +func TestSealIdentityEvent_DomainErrorMapping(t *testing.T) { + t.Parallel() + + t.Run("acknowledged seal succeeds on the IDENTITY lane", func(t *testing.T) { + t.Parallel() + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path != "/v1/internal/identities/event" { + t.Errorf("wrong ingest path: %s", r.URL.Path) + } + w.WriteHeader(http.StatusOK) + _, _ = w.Write([]byte(`{"logId":"01900000-0000-7000-8000-000000000002","success":true,"leafIndex":1,"leafHashHex":"ab","treeSize":2}`)) + })) + defer srv.Close() + c := tlclient.New(srv.URL, "test-key", 5*time.Second) + if err := c.SealIdentityEvent(context.Background(), []byte(`{"eventType":"IDENTITY_VERIFIED"}`), "sig..jws"); err != nil { + t.Fatalf("seal: %v", err) + } + }) + + t.Run("5xx maps to ErrUnavailable", func(t *testing.T) { + t.Parallel() + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + w.WriteHeader(http.StatusBadGateway) + })) + defer srv.Close() + c := tlclient.New(srv.URL, "test-key", 5*time.Second) + err := c.SealIdentityEvent(context.Background(), []byte(`{}`), "sig..jws") + if !errors.Is(err, domain.ErrUnavailable) { + t.Fatalf("want ErrUnavailable, got %v", err) + } + }) + + t.Run("4xx maps to internal TL_REJECTED_EVENT", func(t *testing.T) { + t.Parallel() + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + w.WriteHeader(http.StatusUnprocessableEntity) + _, _ = w.Write([]byte(`{"detail":"INVALID_EVENT"}`)) + })) + defer srv.Close() + c := tlclient.New(srv.URL, "test-key", 5*time.Second) + err := c.SealIdentityEvent(context.Background(), []byte(`{}`), "sig..jws") + var de *domain.Error + if !errors.As(err, &de) || de.Code != "TL_REJECTED_EVENT" { + t.Fatalf("want TL_REJECTED_EVENT, got %v", err) + } + }) +} diff --git a/internal/config/config.go b/internal/config/config.go index e7c813e..0f025e2 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -120,6 +120,14 @@ type Identity struct { // minute (default 10) — each call can trigger an outbound // did:web fetch before any proof exists. RegisterRateLimit int `koanf:"register-rate-limit"` + // LinkRateLimit is the per-owner link/unlink budget per minute + // (default 60) — operational hardening on the link route + // (design §4.3). + LinkRateLimit int `koanf:"link-rate-limit"` + // SealTimeout bounds the inline TL seal call identity operations + // make before reporting success (seal-before-success, design + // §5.6.1). Default 5s. + SealTimeout time.Duration `koanf:"seal-timeout"` } // IdentityResolver selects the did:web resolver adapter. "noop" diff --git a/internal/config/defaults.go b/internal/config/defaults.go index 00ee8df..f12e9e7 100644 --- a/internal/config/defaults.go +++ b/internal/config/defaults.go @@ -24,6 +24,8 @@ func defaultRAConfig() *RAConfig { Resolver: IdentityResolver{Type: "noop"}, ChallengeTTL: time.Hour, RegisterRateLimit: 10, + LinkRateLimit: 60, + SealTimeout: 5 * time.Second, }, Keys: Keys{ Type: "file", diff --git a/internal/crypto/jws.go b/internal/crypto/jws.go index 14c1490..01d789d 100644 --- a/internal/crypto/jws.go +++ b/internal/crypto/jws.go @@ -70,6 +70,14 @@ type JWSProtectedHeader struct { // resolver ignores it — the authoritatively resolved document is // always the key source. Never set on producer/TL signatures. Jwk json.RawMessage `json:"jwk,omitempty"` + + // Crit (RFC 7515 §4.1.11) names header parameters the signer + // requires verifiers to understand. This implementation supports + // no critical extensions, so decode REJECTS any JWS bearing a + // non-empty crit — required behavior for a conforming verifier, + // and the design's third-party verification recipe (§5.5) states + // the same rule. Never emitted on the signing path. + Crit []string `json:"crit,omitempty"` } // SignDetachedJWS produces a detached JWS — compact-serialization @@ -454,6 +462,12 @@ func decodeHeader(encodedHeader string) (*JWSProtectedHeader, error) { if err := json.Unmarshal(headerJSON, &h); err != nil { return nil, fmt.Errorf("%w: parse header: %w", ErrJWSDecode, err) } + // RFC 7515 §4.1.11: a verifier MUST reject a JWS whose crit names + // extensions it does not implement — and this implementation + // implements none. + if len(h.Crit) > 0 { + return nil, fmt.Errorf("%w: unsupported critical header parameters %v", ErrJWSDecode, h.Crit) + } return &h, nil } diff --git a/internal/crypto/jws_crit_test.go b/internal/crypto/jws_crit_test.go new file mode 100644 index 0000000..6886bfb --- /dev/null +++ b/internal/crypto/jws_crit_test.go @@ -0,0 +1,26 @@ +package crypto_test + +import ( + "encoding/base64" + "strings" + "testing" + + anscrypto "github.com/godaddy/ans/internal/crypto" +) + +// TestDecodeStandardJWS_RejectsCrit pins RFC 7515 §4.1.11: this +// verifier implements no critical extensions, so any JWS bearing a +// crit header is rejected at decode — before key selection, before +// signature verification (design §5.5 third-party recipe). +func TestDecodeStandardJWS_RejectsCrit(t *testing.T) { + t.Parallel() + header := base64.RawURLEncoding.EncodeToString( + []byte(`{"alg":"ES256","kid":"did:web:a.com#k1","crit":["exp"],"exp":1}`)) + payload := base64.RawURLEncoding.EncodeToString([]byte(`{}`)) + jws := header + "." + payload + ".c2ln" + + _, _, err := anscrypto.DecodeStandardJWS(jws) + if err == nil || !strings.Contains(err.Error(), "critical header") { + t.Fatalf("crit must be rejected, got %v", err) + } +} diff --git a/internal/domain/errors.go b/internal/domain/errors.go index 66e977e..a89386f 100644 --- a/internal/domain/errors.go +++ b/internal/domain/errors.go @@ -16,6 +16,11 @@ var ( ErrUnauthorized = errors.New("unauthorized") ErrCertificate = errors.New("certificate") ErrInternal = errors.New("internal") + // ErrUnavailable marks a transient upstream failure: the request + // was valid but a dependency (the TL, the vlei-verifier) could + // not confirm it. Maps to 503; the operation is retryable and no + // state was consumed. + ErrUnavailable = errors.New("unavailable") ) // Error is the base error type for all domain errors. @@ -65,6 +70,11 @@ func NewInternalError(code, message string, cause error) *Error { return &Error{Code: code, Message: message, Cause: fmt.Errorf("%w: %w", ErrInternal, cause)} } +// NewUnavailableError creates a retryable upstream-unavailable error. +func NewUnavailableError(code, message string) *Error { + return &Error{Code: code, Message: message, Cause: ErrUnavailable} +} + // Error implements the error interface. func (e *Error) Error() string { if e.Cause != nil { diff --git a/internal/domain/identity.go b/internal/domain/identity.go index 6868259..85e0dc0 100644 --- a/internal/domain/identity.go +++ b/internal/domain/identity.go @@ -210,6 +210,21 @@ func canonicalizeDIDWeb(value string) (string, error) { if seg == "" { return "", NewValidationError("DID_BAD_FORMAT", "did:web has an empty path segment") } + // Dot segments would re-path the resolution URL; control + // bytes (NUL first among them) have no legitimate use in a + // path segment. The same parsed segments feed both the + // resolution URL and the SSRF dialer check (§3.6/§3.7), so + // the rejection happens exactly once, here. + if seg == "." || seg == ".." { + return "", NewValidationError("DID_BAD_FORMAT", + "did:web path segments must not be '.' or '..'") + } + for _, r := range seg { + if r < 0x20 || r == 0x7f { + return "", NewValidationError("DID_BAD_FORMAT", + "did:web path segment contains a control character") + } + } } canonical := "did:web:" + host if len(segments) > 1 { diff --git a/internal/domain/identity_test.go b/internal/domain/identity_test.go index ea0644f..88000f9 100644 --- a/internal/domain/identity_test.go +++ b/internal/domain/identity_test.go @@ -1,6 +1,7 @@ package domain import ( + "errors" "strings" "testing" "time" @@ -280,3 +281,30 @@ func TestEffectiveValueWithoutPending(t *testing.T) { t.Fatal("effective value should be the proven value when nothing is staged") } } + +// TestInferIdentifierKind_DIDWebSegmentRejections pins the §3.6 +// per-segment rejection list: '.', '..', and control characters are +// DID_BAD_FORMAT (empty segments, '%', '@', '/', and ports are pinned +// by the existing grammar tests). +func TestInferIdentifierKind_DIDWebSegmentRejections(t *testing.T) { + t.Parallel() + for _, value := range []string{ + "did:web:acme-corp.com:.", + "did:web:acme-corp.com:..", + "did:web:acme-corp.com:..:identity", + "did:web:acme-corp.com:iden\x00tity", + "did:web:acme-corp.com:iden\ttity", + } { + _, _, err := InferIdentifierKind(value) + var de *Error + if !errors.As(err, &de) || de.Code != "DID_BAD_FORMAT" { + t.Fatalf("%q: want DID_BAD_FORMAT, got %v", value, err) + } + } + + // Sane multi-segment forms still canonicalize. + kind, canonical, err := InferIdentifierKind("did:web:Acme-Corp.com:user:alice") + if err != nil || kind != KindDIDWeb || canonical != "did:web:acme-corp.com:user:alice" { + t.Fatalf("multi-segment: %v %v %v", kind, canonical, err) + } +} diff --git a/internal/port/store.go b/internal/port/store.go index b103b82..4c2ce43 100644 --- a/internal/port/store.go +++ b/internal/port/store.go @@ -175,9 +175,10 @@ type IdentityStore interface { // verify time. ExistsVerified(ctx context.Context, kind domain.IdentifierKind, value string) (bool, error) - // ListByOwner returns every identity owned by the principal, - // newest first. - ListByOwner(ctx context.Context, providerID string) ([]*domain.VerifiedIdentity, error) + // ListByOwner returns the principal's identities, newest first, + // cursor-paginated with the same opaque-cursor convention as the + // agent list (§5.6.1 pagination inherits, never invents). + ListByOwner(ctx context.Context, providerID string, limit int, cursor string) (*CursorPage[*domain.VerifiedIdentity], error) // ConsumeChallenge atomically consumes the live challenge nonce: // a conditional update that succeeds only while the stored nonce @@ -186,6 +187,39 @@ type IdentityStore interface { // the loser receives an invalid-state error. MUST be called // inside the verify-control success transaction. ConsumeChallenge(ctx context.Context, identityID, nonce string, now time.Time) error + + // ClaimChallenge takes the short-TTL provisional claim on the + // live nonce before the seal-before-success TL round trip + // (design §5.6.1): a conditional update that succeeds only while + // the nonce matches, is unconsumed, and is not already claimed + // by an attempt fresher than staleBefore. Serializes concurrent + // verify-control attempts so at most one can seal; the loser + // gets an invalid-state error. A claim is NOT consumption. + ClaimChallenge(ctx context.Context, identityID, nonce string, now, staleBefore time.Time) error + + // ReleaseChallenge releases a provisional claim after a failed + // attempt so the registrant can retry until the nonce expires + // (§3.2 failed-attempts-don't-consume). Best-effort: releasing + // an already-consumed or superseded nonce is a no-op. + ReleaseChallenge(ctx context.Context, identityID, nonce string) error + + // StageChallenge persists a freshly issued challenge (and any + // staged pending_value) onto an EXISTING row, conditionally on + // the status and nonce observed at load time and on no live seal + // claim — the optimistic-concurrency guard that stops a re-add / + // rotate from clobbering a concurrently committed verify or + // revoke (their commits race the resolver fetch between load and + // persist). Never writes status, value, or verified_at: issuing + // a challenge is not a state transition. A failed condition maps + // to a precise conflict error. + StageChallenge(ctx context.Context, identity *domain.VerifiedIdentity, expectedStatus domain.IdentityStatus, expectedNonce string, staleBefore time.Time) error + + // MarkRevoked flips a VERIFIED row to REVOKED conditionally — + // the seal-before-success Phase C commit for Revoke. The status + // condition (not a blind save) means a verify or rotate that + // committed during the revoke's TL round trip is never clobbered + // with stale column values. Zero rows → conflict. + MarkRevoked(ctx context.Context, identityID string, now time.Time) error } // IdentityLinkStore persists identity↔agent link rows. Rows are diff --git a/internal/ra/handler/errors.go b/internal/ra/handler/errors.go index f7fc75e..c3e2574 100644 --- a/internal/ra/handler/errors.go +++ b/internal/ra/handler/errors.go @@ -73,6 +73,8 @@ func statusForCause(cause error) int { return http.StatusUnprocessableEntity case errors.Is(cause, domain.ErrUnauthorized): return http.StatusForbidden + case errors.Is(cause, domain.ErrUnavailable): + return http.StatusServiceUnavailable default: return http.StatusInternalServerError } @@ -92,6 +94,8 @@ func titleForCause(cause error) string { return "Certificate Error" case errors.Is(cause, domain.ErrUnauthorized): return "Forbidden" + case errors.Is(cause, domain.ErrUnavailable): + return "Service Unavailable" default: return "Internal Server Error" } diff --git a/internal/ra/handler/errors_internal_test.go b/internal/ra/handler/errors_internal_test.go new file mode 100644 index 0000000..9ddc2a4 --- /dev/null +++ b/internal/ra/handler/errors_internal_test.go @@ -0,0 +1,38 @@ +package handler + +import ( + "errors" + "net/http" + "testing" + + "github.com/godaddy/ans/internal/domain" +) + +// TestStatusAndTitleForCause pins the sentinel→HTTP mapping table, +// including the seal-before-success 503 (ErrUnavailable: retryable, +// nothing consumed) and the unknown-error fallthrough. +func TestStatusAndTitleForCause(t *testing.T) { + t.Parallel() + cases := []struct { + cause error + status int + title string + }{ + {domain.ErrValidation, http.StatusUnprocessableEntity, "Validation Failed"}, + {domain.ErrNotFound, http.StatusNotFound, "Not Found"}, + {domain.ErrConflict, http.StatusConflict, "Conflict"}, + {domain.ErrInvalidState, http.StatusConflict, "Invalid State"}, + {domain.ErrCertificate, http.StatusUnprocessableEntity, "Certificate Error"}, + {domain.ErrUnauthorized, http.StatusForbidden, "Forbidden"}, + {domain.ErrUnavailable, http.StatusServiceUnavailable, "Service Unavailable"}, + {errors.New("mystery"), http.StatusInternalServerError, "Internal Server Error"}, + } + for _, tc := range cases { + if got := statusForCause(tc.cause); got != tc.status { + t.Errorf("status(%v) = %d, want %d", tc.cause, got, tc.status) + } + if got := titleForCause(tc.cause); got != tc.title { + t.Errorf("title(%v) = %q, want %q", tc.cause, got, tc.title) + } + } +} diff --git a/internal/ra/handler/identity.go b/internal/ra/handler/identity.go index bb9fb58..7dc1d8f 100644 --- a/internal/ra/handler/identity.go +++ b/internal/ra/handler/identity.go @@ -3,6 +3,7 @@ package handler import ( "encoding/json" "net/http" + "strconv" "time" "github.com/go-chi/chi/v5" @@ -176,22 +177,38 @@ func (h *IdentityHandler) Revoke(w http.ResponseWriter, r *http.Request) { WriteJSON(w, http.StatusOK, toDetailResponse(identity, nil)) } -// List handles GET /v2/ans/identities — the caller's identities. +// List handles GET /v2/ans/identities — one page of the caller's +// identities, in the v2 limit + opaque-cursor envelope (§5.6.1: +// pagination inherits the surface's convention). func (h *IdentityHandler) List(w http.ResponseWriter, r *http.Request) { providerID, ok := callerSubject(w, r) if !ok { return } - identities, err := h.svc.List(r.Context(), providerID) + q := r.URL.Query() + limit := 0 + if lv := q.Get("limit"); lv != "" { + n, err := strconv.Atoi(lv) + if err != nil || n < 1 || n > 100 { + WriteError(w, domain.NewValidationError("INVALID_LIMIT", "limit must be between 1 and 100")) + return + } + limit = n + } + page, err := h.svc.List(r.Context(), providerID, limit, q.Get("cursor")) if err != nil { WriteError(w, err) return } - out := make([]identityDetailResponse, 0, len(identities)) - for _, identity := range identities { + out := make([]identityDetailResponse, 0, len(page.Items)) + for _, identity := range page.Items { out = append(out, toDetailResponse(identity, nil)) } - WriteJSON(w, http.StatusOK, map[string]any{"identities": out}) + resp := map[string]any{"identities": out, "nextCursor": nil} + if page.NextCursor != "" { + resp["nextCursor"] = page.NextCursor + } + WriteJSON(w, http.StatusOK, resp) } // Detail handles GET /v2/ans/identities/{identityId} — the identity diff --git a/internal/ra/handler/identity_handler_test.go b/internal/ra/handler/identity_handler_test.go index e021ec5..a4f241e 100644 --- a/internal/ra/handler/identity_handler_test.go +++ b/internal/ra/handler/identity_handler_test.go @@ -28,6 +28,7 @@ import ( "github.com/godaddy/ans/internal/adapter/eventbus" "github.com/godaddy/ans/internal/adapter/store/sqlite" anscrypto "github.com/godaddy/ans/internal/crypto" + "github.com/godaddy/ans/internal/domain" "github.com/godaddy/ans/internal/port" "github.com/godaddy/ans/internal/ra/handler" ramiddleware "github.com/godaddy/ans/internal/ra/middleware" @@ -36,12 +37,20 @@ import ( // identityHTTPFixture wires the identity routes (plus the agent // register + detail routes the link tests need) over real SQLite and -// the noop resolver. No signer — covering the unsigned outbox branch; -// the signed path is pinned by the service tests. +// the noop resolver. No signer — the seal goes through an always-ok +// stub sealer with an unsigned payload; the signed seal path is +// pinned by the service tests. type identityHTTPFixture struct { router chi.Router + agents *sqlite.AgentStore } +// okSealer acknowledges every seal — the HTTP tests pin the wire +// contract, not the seal discipline (service tests own that). +type okSealer struct{} + +func (okSealer) SealIdentityEvent(context.Context, []byte, string) error { return nil } + func newIdentityHTTPFixture(t *testing.T) *identityHTTPFixture { t.Helper() dir := t.TempDir() @@ -77,7 +86,7 @@ func newIdentityHTTPFixture(t *testing.T) *identityHTTPFixture { sqlite.NewIdentityLinkStore(db), agents, didresolver.NewNoopResolver(), - outbox, + okSealer{}, db, ).WithChallengeTTL(30 * time.Minute) @@ -98,7 +107,7 @@ func newIdentityHTTPFixture(t *testing.T) *identityHTTPFixture { r.Post("/v2/ans/identities/{identityId}/links", idH.Link) r.Delete("/v2/ans/identities/{identityId}/links/{agentId}", idH.Unlink) - return &identityHTTPFixture{router: r} + return &identityHTTPFixture{router: r, agents: agents} } // do sends a request as the given owner ("" = unauthenticated) and @@ -410,5 +419,72 @@ func registerAgentForIdentity(t *testing.T, f *identityHTTPFixture, owner, host if err := json.Unmarshal(rec.Body.Bytes(), &resp); err != nil || resp.AgentID == "" { t.Fatalf("agent register response: %s (%v)", rec.Body, err) } + + // Drive the agent to ACTIVE directly through the store — the link + // liveness gate (§4.3) rejects pre-activation agents, and these + // tests pin the identity wire contract, not the ACME lifecycle. + reg, err := f.agents.FindByAgentID(context.Background(), resp.AgentID) + if err != nil { + t.Fatal(err) + } + reg.Status = domain.StatusActive + if err := f.agents.Save(context.Background(), reg); err != nil { + t.Fatal(err) + } return resp.AgentID } + +// TestIdentityHandler_ListPagination pins the v2 limit + opaque- +// cursor envelope on GET /v2/ans/identities. +func TestIdentityHandler_ListPagination(t *testing.T) { + t.Parallel() + f := newIdentityHTTPFixture(t) + owner := "owner-pages" + f.registerAndVerify(t, owner, "did:web:page-a.example.com") + f.registerAndVerify(t, owner, "did:web:page-b.example.com") + f.registerAndVerify(t, owner, "did:web:page-c.example.com") + + // Invalid limit → 422 INVALID_LIMIT. + rec := f.do(t, owner, http.MethodGet, "/v2/ans/identities?limit=0", nil) + if rec.Code != http.StatusUnprocessableEntity { + t.Fatalf("limit=0: %d %s", rec.Code, rec.Body) + } + + type page struct { + Identities []struct { + IdentityID string `json:"identityId"` + } `json:"identities"` + NextCursor *string `json:"nextCursor"` + } + var p1 page + rec = f.do(t, owner, http.MethodGet, "/v2/ans/identities?limit=2", nil) + if rec.Code != http.StatusOK { + t.Fatalf("page 1: %d %s", rec.Code, rec.Body) + } + if err := json.Unmarshal(rec.Body.Bytes(), &p1); err != nil { + t.Fatal(err) + } + if len(p1.Identities) != 2 || p1.NextCursor == nil { + t.Fatalf("page 1 shape: %+v", p1) + } + + var p2 page + rec = f.do(t, owner, http.MethodGet, "/v2/ans/identities?limit=2&cursor="+*p1.NextCursor, nil) + if rec.Code != http.StatusOK { + t.Fatalf("page 2: %d %s", rec.Code, rec.Body) + } + if err := json.Unmarshal(rec.Body.Bytes(), &p2); err != nil { + t.Fatal(err) + } + if len(p2.Identities) != 1 || p2.NextCursor != nil { + t.Fatalf("page 2 shape: %+v", p2) + } + // No overlap between pages. + seen := map[string]bool{} + for _, e := range append(p1.Identities, p2.Identities...) { + if seen[e.IdentityID] { + t.Fatalf("duplicate across pages: %s", e.IdentityID) + } + seen[e.IdentityID] = true + } +} diff --git a/internal/ra/service/identity.go b/internal/ra/service/identity.go index e9ec09f..f9c38a3 100644 --- a/internal/ra/service/identity.go +++ b/internal/ra/service/identity.go @@ -16,12 +16,6 @@ import ( identityevent "github.com/godaddy/ans/internal/tl/event/identity" ) -// identityLane is the outbox schema_version value routing identity -// events to the TL's `POST /v1/internal/identities/event` ingest -// lane. Same producer signature and replay-verbatim invariant as the -// V1/V2 agent lanes; different inner-event schema. -const identityLane = "IDENTITY" - // maxProofsPerVerify bounds the multi-key proof set on one // verify-control call. did:web legitimately proves several // assertionMethod keys; sixteen is far beyond any real document while @@ -34,6 +28,28 @@ const maxProofsPerVerify = 16 // every accepted call still seals exactly one event. const maxLinkBatch = 256 +// sealClaimTTL bounds the provisional verify-control claim taken +// across the seal-before-success TL round trip: a claim older than +// this (a crashed claimer) is reclaimable. Comfortably above the +// seal timeout, comfortably below the nonce TTL. +const sealClaimTTL = 30 * time.Second + +// defaultSealTimeout bounds the inline TL seal call (§5.6.1) — +// parity with the outbound-fetch budget (§3.7). +const defaultSealTimeout = 5 * time.Second + +// IdentityEventSealer submits one producer-signed identity event to +// the TL's IDENTITY ingest lane and returns only after the TL +// acknowledges the seal — the dependency seal-before-success (design +// §5.6.1) hangs on. Identity events never ride the outbox: delivery +// precedes success, a failed delivery IS a failed operation, and +// there is nothing for a background worker to retry (retrying would +// seal an event whose row transition never happened). Implementations +// map failures to domain error kinds (ErrUnavailable for transient). +type IdentityEventSealer interface { + SealIdentityEvent(ctx context.Context, innerCanonical []byte, producerSig string) error +} + // ProofChallenge is one entry of the 202 challenge list: a key the // registrant may prove, plus the exact base64url signing input a // compact JWS over it must carry as its payload segment. Every entry @@ -66,7 +82,7 @@ type IdentityService struct { identities port.IdentityStore links port.IdentityLinkStore agents port.AgentStore - outbox OutboxEnqueuer + sealer IdentityEventSealer uow port.UnitOfWork signer *EventSigner @@ -76,19 +92,23 @@ type IdentityService struct { verifiers map[domain.IdentifierKind]controlVerifier challengeTTL time.Duration + sealTimeout time.Duration limiter *ownerLimiter + linkLimiter *ownerLimiter clock func() time.Time newID func() (string, error) newNonce func() (string, error) } -// NewIdentityService constructs an IdentityService. +// NewIdentityService constructs an IdentityService. A nil sealer +// fails every sealing operation closed with TL_UNAVAILABLE — the +// seal-before-success rule (§5.6.1) admits no "seal later" mode. func NewIdentityService( identities port.IdentityStore, links port.IdentityLinkStore, agents port.AgentStore, resolver port.DIDResolver, - outbox OutboxEnqueuer, + sealer IdentityEventSealer, uow port.UnitOfWork, ) *IdentityService { return &IdentityService{ @@ -96,10 +116,12 @@ func NewIdentityService( links: links, agents: agents, verifiers: newControlVerifiers(resolver), - outbox: outbox, + sealer: sealer, uow: uow, challengeTTL: time.Hour, + sealTimeout: defaultSealTimeout, limiter: newOwnerLimiter(defaultRegisterPerMinute), + linkLimiter: newOwnerLimiter(defaultLinkPerMinute), clock: time.Now, newID: func() (string, error) { id, err := uuid.NewV7() @@ -146,6 +168,24 @@ func (s *IdentityService) WithRegisterRateLimit(perMinute int) *IdentityService return s } +// WithLinkRateLimit overrides the per-owner link/unlink rate limit +// (default 60/min — design §4.3 operational hardening: the bounds +// limit blast radius and TL noise, not the named risk). +func (s *IdentityService) WithLinkRateLimit(perMinute int) *IdentityService { + if perMinute > 0 { + s.linkLimiter = newOwnerLimiter(perMinute) + } + return s +} + +// WithSealTimeout overrides the inline TL seal budget (default 5s). +func (s *IdentityService) WithSealTimeout(timeout time.Duration) *IdentityService { + if timeout > 0 { + s.sealTimeout = timeout + } + return s +} + // WithClock overrides the time source (tests only). func (s *IdentityService) WithClock(fn func() time.Time) *IdentityService { s.clock = fn @@ -209,7 +249,7 @@ func (s *IdentityService) Register(ctx context.Context, providerID, rawValue str "identifier is already verified by this owner; rotate it with PUT instead") case err == nil: // PENDING_CONTROL → idempotent re-challenge on the same row. - return s.challenge(ctx, existing, now) + return s.challenge(ctx, existing, now, false) case errors.Is(err, domain.ErrNotFound): // fall through to creation default: @@ -234,7 +274,7 @@ func (s *IdentityService) Register(ctx context.Context, providerID, rawValue str if err != nil { return nil, err } - return s.challenge(ctx, identity, now) + return s.challenge(ctx, identity, now, true) } // Rotate stages a same-kind replacement (§4.2 PUT) and returns fresh @@ -254,13 +294,27 @@ func (s *IdentityService) Rotate(ctx context.Context, providerID, identityID, ra if err := identity.StageRotation(rawValue, now); err != nil { return nil, err } - return s.challenge(ctx, identity, now) + return s.challenge(ctx, identity, now, false) } // challenge mints a fresh nonce on the identity, runs the kind's // advisory resolution to seed the per-key challenge list, persists, // and assembles the 202 response. Shared by Register and Rotate. -func (s *IdentityService) challenge(ctx context.Context, identity *domain.VerifiedIdentity, now time.Time) (*IdentityChallengeResponse, error) { +// +// isNew selects the persist path: a brand-new identity INSERTs +// (fresh UUIDv7 — unracable); an existing row persists through the +// store's CONDITIONAL StageChallenge — the resolver fetch between +// load and persist spans seconds, and a blind upsert here could +// clobber a verify or revoke that committed in that window (status +// regression = a different owner could then take the identifier). +func (s *IdentityService) challenge(ctx context.Context, identity *domain.VerifiedIdentity, now time.Time, isNew bool) (*IdentityChallengeResponse, error) { + // Load-time snapshot for the conditional persist. + expectedStatus := identity.Status + expectedNonce := "" + if identity.Challenge != nil { + expectedNonce = identity.Challenge.Nonce + } + nonce, err := s.newNonce() if err != nil { return nil, domain.NewInternalError("CHALLENGE_GENERATION", "could not generate challenge nonce", err) @@ -291,8 +345,14 @@ func (s *IdentityService) challenge(ctx context.Context, identity *domain.Verifi return nil, err } - if err := s.identities.Save(ctx, identity); err != nil { - return nil, mapIdentitySaveErr(err) + if isNew { + if err := s.identities.Save(ctx, identity); err != nil { + return nil, mapIdentitySaveErr(err) + } + } else { + if err := s.identities.StageChallenge(ctx, identity, expectedStatus, expectedNonce, now.Add(-sealClaimTTL)); err != nil { + return nil, err + } } return &IdentityChallengeResponse{ Identity: identity, @@ -303,12 +363,15 @@ func (s *IdentityService) challenge(ctx context.Context, identity *domain.Verifi } // VerifyControl runs the identity's per-kind control proof over the -// submission and, when every proof passes, flips the identity to -// VERIFIED (or completes a staged rotation), consumes the nonce, and -// seals IDENTITY_VERIFIED / IDENTITY_UPDATED on the identity's TL -// stream — all in one transaction. One bad proof fails the call -// closed; a failed attempt does NOT consume the nonce. The per-kind -// logic lives entirely behind the controlVerifier seam +// submission and, when every proof passes, seals IDENTITY_VERIFIED / +// IDENTITY_UPDATED on the identity's TL stream and THEN flips the +// identity to VERIFIED (or completes a staged rotation), consuming +// the nonce in the commit transaction — seal-before-success (§5.6.1): +// success is reported only after the TL acknowledges the seal, and +// the RA row can never be ahead of the log. One bad proof fails the +// call closed; a failed attempt does NOT consume the nonce (the +// provisional claim taken across the seal round trip is released). +// The per-kind logic lives entirely behind the controlVerifier seam // (identitykinds.go); this method owns the kind-agnostic discipline. func (s *IdentityService) VerifyControl(ctx context.Context, providerID, identityID string, sub ProofSubmission) (*domain.VerifiedIdentity, error) { identity, err := s.ownedIdentityForWrite(ctx, providerID, identityID) @@ -345,16 +408,56 @@ func (s *IdentityService) VerifyControl(ctx context.Context, providerID, identit return nil, err } + // Advisory cross-owner duplicate check before sealing — narrows + // the window in which a competing owner's verify could leave a + // sealed event whose row transition loses the proven-uniqueness + // index race. The index at commit stays the authoritative guard; + // a sealed loser is a benign true fact (control WAS proven) whose + // row never flips and whose identity never becomes linkable. statusBefore := identity.Status - var sealed *domain.VerifiedIdentity - err = s.uow.Run(ctx, func(txCtx context.Context) error { - // Consume the nonce first — the conditional update is the - // TOCTOU guard; exactly one concurrent verify can win. - if err := s.identities.ConsumeChallenge(txCtx, identity.IdentityID, identity.Challenge.Nonce, now); err != nil { - return err + if statusBefore != domain.IdentityVerified { + if taken, terr := s.identities.ExistsVerified(ctx, identity.Kind, identity.EffectiveValue()); terr != nil { + return nil, terr + } else if taken { + return nil, domain.NewConflictError("IDENTIFIER_DUPLICATE", + "identifier is already verified by another owner") } - previousValue, err := identity.CompleteVerification(now) - if err != nil { + } + + // Phase A — claim. Serializes concurrent verify attempts on this + // nonce across the seal round trip: at most one in-flight attempt + // can seal. A claim is NOT consumption; every failure path below + // releases it. + nonce := identity.Challenge.Nonce + if err := s.identities.ClaimChallenge(ctx, identity.IdentityID, nonce, now, now.Add(-sealClaimTTL)); err != nil { + return nil, err + } + + previousValue, err := identity.CompleteVerification(now) + if err != nil { + s.releaseClaim(ctx, identity.IdentityID, nonce) + return nil, err + } + eventType := identityevent.TypeIdentityVerified + if statusBefore == domain.IdentityVerified { + eventType = identityevent.TypeIdentityUpdated + } + inner := s.buildIdentityEvent(identity, eventType, now) + inner.Keys = provenKeys + inner.PreviousValue = previousValue + inner.VerifiedAt = now.Format(time.RFC3339) + + // Phase B — seal. No success without the TL's acknowledgment. + if err := s.sealIdentityEvent(ctx, inner, now); err != nil { + s.releaseClaim(ctx, identity.IdentityID, nonce) + return nil, err + } + + // Phase C — commit with the ack: consume the nonce (the + // conditional update stays the authoritative TOCTOU guard) and + // flip the row. + err = s.uow.Run(ctx, func(txCtx context.Context) error { + if err := s.identities.ConsumeChallenge(txCtx, identity.IdentityID, nonce, now); err != nil { return err } consumed := now @@ -362,27 +465,25 @@ func (s *IdentityService) VerifyControl(ctx context.Context, providerID, identit if err := s.identities.Save(txCtx, identity); err != nil { return mapIdentitySaveErr(err) } - - eventType := identityevent.TypeIdentityVerified - if statusBefore == domain.IdentityVerified { - eventType = identityevent.TypeIdentityUpdated - } - inner := s.buildIdentityEvent(identity, eventType, now) - inner.Keys = provenKeys - inner.PreviousValue = previousValue - inner.VerifiedAt = now.Format(time.RFC3339) - return s.enqueueIdentityEvent(txCtx, inner, now) + return nil }) if err != nil { + s.releaseClaim(ctx, identity.IdentityID, nonce) return nil, err } - sealed = identity - return sealed, nil + return identity, nil +} + +// releaseClaim is the best-effort failure-path release of the +// verify-control seal claim — failed attempts never consume (§3.2). +func (s *IdentityService) releaseClaim(ctx context.Context, identityID, nonce string) { + _ = s.identities.ReleaseChallenge(ctx, identityID, nonce) } // Revoke transitions a VERIFIED identity to REVOKED and seals // IDENTITY_REVOKED — one event; propagation to every linked agent's -// badge is the TL's read-time join, never a write fan-out. +// badge is the TL's read-time join, never a write fan-out. Seal +// before success (§5.6.1): the row flips only after the TL ack. func (s *IdentityService) Revoke(ctx context.Context, providerID, identityID string) (*domain.VerifiedIdentity, error) { identity, err := s.ownedIdentityForWrite(ctx, providerID, identityID) if err != nil { @@ -392,23 +493,27 @@ func (s *IdentityService) Revoke(ctx context.Context, providerID, identityID str if err := identity.Revoke(now); err != nil { return nil, err } - err = s.uow.Run(ctx, func(txCtx context.Context) error { - if err := s.identities.Save(txCtx, identity); err != nil { - return mapIdentitySaveErr(err) - } - inner := s.buildIdentityEvent(identity, identityevent.TypeIdentityRevoked, now) - inner.RevokedAt = now.Format(time.RFC3339) - return s.enqueueIdentityEvent(txCtx, inner, now) - }) - if err != nil { + inner := s.buildIdentityEvent(identity, identityevent.TypeIdentityRevoked, now) + inner.RevokedAt = now.Format(time.RFC3339) + if err := s.sealIdentityEvent(ctx, inner, now); err != nil { + return nil, err + } + // Phase C — CONDITIONAL commit (re-read + compare, §plan W1): the + // seal round trip is a window a concurrent verify/rotate commit + // can land in; a blind save would overwrite it with this call's + // stale snapshot. On conflict the sealed IDENTITY_REVOKED is the + // benign residue (the TL's read-time status is terminal on ANY + // revocation leaf) and the caller retries against fresh state. + if err := s.identities.MarkRevoked(ctx, identity.IdentityID, now); err != nil { return nil, err } return identity, nil } -// List returns the owner's identities, newest first. -func (s *IdentityService) List(ctx context.Context, providerID string) ([]*domain.VerifiedIdentity, error) { - return s.identities.ListByOwner(ctx, providerID) +// List returns one page of the owner's identities, newest first +// (opaque-cursor pagination, the agent-list convention). +func (s *IdentityService) List(ctx context.Context, providerID string, limit int, cursor string) (*port.CursorPage[*domain.VerifiedIdentity], error) { + return s.identities.ListByOwner(ctx, providerID, limit, cursor) } // LinkedIdentitySummary is one entry of the RA-side computed @@ -425,10 +530,21 @@ type LinkedIdentitySummary struct { } // LinkedIdentitiesForAgent computes the identities currently linked -// to an agent. Callers reach this through the ownership-gated agent -// detail route; links are same-owner by construction, so no further -// gate applies here. +// to an agent under the §5.6.3 visibility predicate: link LINKED ∧ +// agent live — a terminal agent's view is empty (its links are no +// longer visible; history stays in the TL). REVOKED identities stay +// visible with their status: a reader must see the who behind a +// still-linked agent was revoked. Callers reach this through the +// ownership-gated agent detail route; links are same-owner by +// construction, so no further gate applies here. func (s *IdentityService) LinkedIdentitiesForAgent(ctx context.Context, agentID string) ([]LinkedIdentitySummary, error) { + reg, err := s.agents.FindByAgentID(ctx, agentID) + if err != nil { + return nil, err + } + if !agentLinkable(reg.Status) { + return []LinkedIdentitySummary{}, nil + } links, err := s.links.ListLiveByAgent(ctx, agentID) if err != nil { return nil, err @@ -450,7 +566,10 @@ func (s *IdentityService) LinkedIdentitiesForAgent(ctx context.Context, agentID return out, nil } -// Detail returns one identity plus its live links. +// Detail returns one identity plus its visible links — the §5.6.3 +// visibility predicate applies to the linked-agent list and count +// exactly as to every other "current" view: a link to a terminal +// agent drops out (its history stays in the TL). func (s *IdentityService) Detail(ctx context.Context, providerID, identityID string) (*domain.VerifiedIdentity, []*domain.IdentityLink, error) { identity, err := s.ownedIdentity(ctx, providerID, identityID) if err != nil { @@ -460,17 +579,39 @@ func (s *IdentityService) Detail(ctx context.Context, providerID, identityID str if err != nil { return nil, nil, err } - return identity, links, nil + visible := make([]*domain.IdentityLink, 0, len(links)) + for _, l := range links { + reg, err := s.agents.FindByAgentID(ctx, l.AgentID) + if err != nil { + if errors.Is(err, domain.ErrNotFound) { + continue // agent row gone (admin cleanup) — not visible + } + return nil, nil, err // infra failure must surface, never under-count + } + if !agentLinkable(reg.Status) { + continue + } + visible = append(visible, l) + } + return identity, visible, nil } // Link binds a batch of the owner's agents to the identity — a // single owner-gated call, no challenge, no signature (§4.3): the // caller must own the identity AND every named agent; key possession -// never authorizes a link. The whole batch seals as ONE -// IDENTITY_LINKED event on the identity stream; agent streams are -// never written. Already-linked agents are skipped idempotently; a -// call that links nothing new seals nothing. +// never authorizes a link. Liveness gate (§4.3): the identity must +// be VERIFIED and every agent live (ACTIVE or DEPRECATED — a +// deprecated agent still serves during migration and the who stays +// true); terminal or pre-activation agents are AGENT_NOT_LINKABLE. +// The whole batch seals as ONE IDENTITY_LINKED event on the identity +// stream — before success is reported (§5.6.1) — and agent streams +// are never written. Already-linked agents are skipped idempotently; +// a call that links nothing new seals nothing. func (s *IdentityService) Link(ctx context.Context, providerID, identityID string, agentIDs []string) (int, error) { + if !s.linkLimiter.Allow(providerID, s.clock()) { + return 0, domain.NewValidationError("RATE_LIMITED", + "too many link/unlink calls; retry later") + } identity, err := s.ownedIdentityForWrite(ctx, providerID, identityID) if err != nil { return 0, err @@ -500,7 +641,9 @@ func (s *IdentityService) Link(ctx context.Context, providerID, identityID strin // Owner gate, both sides: every agent must exist and belong to // the caller. A non-owned agent is reported as not-found — the - // caller learns nothing about other owners' agents. + // caller learns nothing about other owners' agents. Then the + // liveness gate: rejected atomically, all-or-nothing, matching + // the one-event batch semantics. for _, agentID := range deduped { reg, err := s.agents.FindByAgentID(ctx, agentID) if err != nil { @@ -511,50 +654,124 @@ func (s *IdentityService) Link(ctx context.Context, providerID, identityID strin return 0, domain.NewNotFoundError("AGENT_NOT_FOUND", fmt.Sprintf("agent %q not found", agentID)) } + if !agentLinkable(reg.Status) { + return 0, domain.NewValidationError("AGENT_NOT_LINKABLE", + fmt.Sprintf("agent %q is %s — links require a live agent (ACTIVE or DEPRECATED)", agentID, reg.Status)) + } + } + + // Compute the batch BEFORE sealing: the sealed ansIds[] must be + // exactly the pairs this call creates. + existingLinks, err := s.links.ListLiveByIdentity(ctx, identityID) + if err != nil { + return 0, err + } + alreadyLinked := make(map[string]bool, len(existingLinks)) + for _, l := range existingLinks { + alreadyLinked[l.AgentID] = true + } + newlyLinked := make([]string, 0, len(deduped)) + for _, agentID := range deduped { + if !alreadyLinked[agentID] { + newlyLinked = append(newlyLinked, agentID) + } + } + if len(newlyLinked) == 0 { + return 0, nil // fully idempotent — nothing to seal } now := s.clock().UTC() - linked := 0 + inner := s.buildIdentityEvent(identity, identityevent.TypeIdentityLinked, now) + inner.AnsIDs = newlyLinked + + // Seal before success (§5.6.1), then commit the rows with the + // ack. A concurrent call winning a pair's row in between is + // benign: both sealed events assert LINKED, the row upsert is + // idempotent, and latest-event-wins reads are unaffected. + if err := s.sealIdentityEvent(ctx, inner, now); err != nil { + return 0, err + } err = s.uow.Run(ctx, func(txCtx context.Context) error { - newlyLinked := make([]string, 0, len(deduped)) - for _, agentID := range deduped { - created, err := s.links.Link(txCtx, identityID, agentID, now) - if err != nil { + // Re-read + compare (§plan W1): a revoke that committed + // during the seal round trip must not gain live link rows — + // the §4.3 VERIFIED gate holds at commit, not just at entry. + current, err := s.identities.FindByID(txCtx, identityID) + if err != nil { + return err + } + if current.Status != domain.IdentityVerified { + return domain.NewInvalidStateError("IDENTITY_NOT_VERIFIED", + "identity was revoked while the link was sealing; the sealed link event is inert") + } + for _, agentID := range newlyLinked { + if _, err := s.links.Link(txCtx, identityID, agentID, now); err != nil { return err } - if created { - newlyLinked = append(newlyLinked, agentID) - } } - linked = len(newlyLinked) - if linked == 0 { - return nil // fully idempotent — nothing to seal - } - inner := s.buildIdentityEvent(identity, identityevent.TypeIdentityLinked, now) - inner.AnsIDs = newlyLinked - return s.enqueueIdentityEvent(txCtx, inner, now) + return nil }) if err != nil { return 0, err } - return linked, nil + return len(newlyLinked), nil +} + +// agentLinkable is the link liveness gate (§4.3): live states only. +// DEPRECATED is deliberately linkable; terminal and pre-activation +// states are not (a terminal link is dead on arrival under the +// visibility predicate, and a pre-activation agent has no sealed TL +// presence to join). +func agentLinkable(status domain.RegistrationStatus) bool { + return status == domain.StatusActive || status == domain.StatusDeprecated } // Unlink ends one association and seals IDENTITY_UNLINKED on the -// identity stream. The association's history persists in the log. +// identity stream — before success is reported (§5.6.1). The +// association's history persists in the log. func (s *IdentityService) Unlink(ctx context.Context, providerID, identityID, agentID string) error { + if !s.linkLimiter.Allow(providerID, s.clock()) { + return domain.NewValidationError("RATE_LIMITED", + "too many link/unlink calls; retry later") + } identity, err := s.ownedIdentityForWrite(ctx, providerID, identityID) if err != nil { return err } + + // The live link must exist before anything seals. + existingLinks, err := s.links.ListLiveByIdentity(ctx, identityID) + if err != nil { + return err + } + live := false + for _, l := range existingLinks { + if l.AgentID == agentID { + live = true + break + } + } + if !live { + return domain.NewNotFoundError("LINK_NOT_FOUND", + "no live link exists for this identity and agent") + } + now := s.clock().UTC() + inner := s.buildIdentityEvent(identity, identityevent.TypeIdentityUnlinked, now) + inner.AnsIDs = []string{agentID} + if err := s.sealIdentityEvent(ctx, inner, now); err != nil { + return err + } return s.uow.Run(ctx, func(txCtx context.Context) error { if err := s.links.Unlink(txCtx, identityID, agentID, now); err != nil { + // A concurrent unlink winning the row after our liveness + // read is benign: the association ended and both sealed + // events say so. + if errors.Is(err, domain.ErrNotFound) { + return nil + } return err } - inner := s.buildIdentityEvent(identity, identityevent.TypeIdentityUnlinked, now) - inner.AnsIDs = []string{agentID} - return s.enqueueIdentityEvent(txCtx, inner, now) + return nil }) } @@ -618,13 +835,18 @@ func (s *IdentityService) buildIdentityEvent( } } -// enqueueIdentityEvent JCS-canonicalizes the inner event, signs it -// once with the producer key, and writes the outbox row on the -// IDENTITY lane. Same replay-verbatim invariant as the agent lanes: -// the worker must POST these exact bytes on every retry. -func (s *IdentityService) enqueueIdentityEvent(ctx context.Context, inner *identityevent.Event, now time.Time) error { - if s.outbox == nil { - return nil +// sealIdentityEvent JCS-canonicalizes the inner event, signs it once +// with the producer key, and submits it inline to the TL's IDENTITY +// ingest lane, returning only on the TL's acknowledgment — +// seal-before-success (§5.6.1). Identity events never ride the +// outbox: sign once, submit once; a failed submission is a failed +// operation, surfaced retryable (TL_UNAVAILABLE) with nothing +// consumed. A nil sealer fails closed for the same reason — there is +// no "seal later" mode. +func (s *IdentityService) sealIdentityEvent(ctx context.Context, inner *identityevent.Event, now time.Time) error { + if s.sealer == nil { + return domain.NewUnavailableError("TL_UNAVAILABLE", + "identity sealing is not configured; identity operations cannot report success without a sealed event") } innerCanonical, err := identityevent.CanonicalizeEvent(inner) if err != nil { @@ -645,17 +867,9 @@ func (s *IdentityService) enqueueIdentityEvent(ctx context.Context, inner *ident return fmt.Errorf("sign identity event: %w", err) } } - payload, err := marshalOutboxPayload(innerCanonical, producerSig) - if err != nil { - return err - } - // The outbox row's subject column carries the identityId — the - // stream key for identity events, exactly as agent rows carry - // the agentId. - if _, err := s.outbox.Enqueue(ctx, string(inner.EventType), inner.IdentityID, identityLane, payload, now); err != nil { - return err - } - return nil + sealCtx, cancel := context.WithTimeout(ctx, s.sealTimeout) + defer cancel() + return s.sealer.SealIdentityEvent(sealCtx, innerCanonical, producerSig) } // mapIdentitySaveErr converts the storage layer's generic conflict diff --git a/internal/ra/service/identity_test.go b/internal/ra/service/identity_test.go index c9e2ebd..bdc2f10 100644 --- a/internal/ra/service/identity_test.go +++ b/internal/ra/service/identity_test.go @@ -3,9 +3,10 @@ package service_test // IdentityService tests: the proof gate (payload equality, kid // selection, signature verification, nonce discipline), the lifecycle // (register → verify → rotate → revoke), the owner-gated links, and -// the sealed-event emission on the outbox IDENTITY lane. Real SQLite -// stores + real crypto; the resolver is the noop adapter (hint -// synthesis) or a canned-document fake for the did:web rules. +// the synchronous seal-before-success emission (§5.6.1) through a +// recording sealer. Real SQLite stores + real crypto; the resolver is +// the noop adapter (hint synthesis) or a canned-document fake for the +// did:web rules. import ( "context" @@ -18,6 +19,7 @@ import ( "encoding/json" "errors" "strings" + "sync" "testing" "time" @@ -45,13 +47,57 @@ func (f *fakeResolver) Resolve(context.Context, string, []port.KeyHint) (*port.D type identityFixture struct { svc *service.IdentityService db *sqlite.DB - outbox *sqlite.OutboxStore + sealer *recordingSealer agents port.AgentStore signerPub any clock *fakeClock providerID string } +// recordingSealer is the test IdentityEventSealer: it records every +// sealed (innerCanonical, producerSig) pair the service submitted — +// each entry is one TL-acknowledged seal — and can be primed to +// fail, exercising the seal-before-success failure paths. +type recordingSealer struct { + mu sync.Mutex + events []sealedEvent + err error + // hook runs inside SealIdentityEvent before recording — the test + // stand-in for "something committed during the TL round trip", + // which is exactly the window the Phase C conditional commits + // must survive. + hook func() +} + +type sealedEvent struct { + Inner []byte + Sig string +} + +func (r *recordingSealer) SealIdentityEvent(_ context.Context, innerCanonical []byte, producerSig string) error { + r.mu.Lock() + defer r.mu.Unlock() + if r.hook != nil { + r.hook() + } + if r.err != nil { + return r.err + } + r.events = append(r.events, sealedEvent{ + Inner: append([]byte(nil), innerCanonical...), + Sig: producerSig, + }) + return nil +} + +// fail primes the sealer to reject every seal with err (nil restores +// normal operation). +func (r *recordingSealer) fail(err error) { + r.mu.Lock() + defer r.mu.Unlock() + r.err = err +} + type fakeClock struct{ now time.Time } func (c *fakeClock) Now() time.Time { return c.now } @@ -82,12 +128,13 @@ func newIdentityFixture(t *testing.T, resolver port.DIDResolver) *identityFixtur resolver = didresolver.NewNoopResolver() } clock := &fakeClock{now: time.Date(2026, 6, 10, 15, 0, 0, 0, time.UTC)} + sealer := &recordingSealer{} svc := service.NewIdentityService( sqlite.NewIdentityStore(db), sqlite.NewIdentityLinkStore(db), sqlite.NewAgentStore(db), resolver, - sqlite.NewOutboxStore(db), + sealer, db, ).WithSigner(service.EventSigner{ KeyManager: km, @@ -98,7 +145,7 @@ func newIdentityFixture(t *testing.T, resolver port.DIDResolver) *identityFixtur return &identityFixture{ svc: svc, db: db, - outbox: sqlite.NewOutboxStore(db), + sealer: sealer, agents: sqlite.NewAgentStore(db), signerPub: pub, clock: clock, @@ -108,6 +155,13 @@ func newIdentityFixture(t *testing.T, resolver port.DIDResolver) *identityFixtur // saveAgent persists a minimal ACTIVE agent owned by `owner`. func (fx *identityFixture) saveAgent(t *testing.T, agentID, owner, host string) { + t.Helper() + fx.saveAgentWithStatus(t, agentID, owner, host, domain.StatusActive) +} + +// saveAgentWithStatus persists a minimal agent in the given lifecycle +// state — the link liveness-gate tests need every state. +func (fx *identityFixture) saveAgentWithStatus(t *testing.T, agentID, owner, host string, status domain.RegistrationStatus) { t.Helper() v, err := domain.NewSemVer(1, 0, 0) if err != nil { @@ -121,7 +175,7 @@ func (fx *identityFixture) saveAgent(t *testing.T, agentID, owner, host string) AgentID: agentID, OwnerID: owner, AnsName: ansName, - Status: domain.StatusActive, + Status: status, Details: domain.RegistrationDetails{ RegistrationTimestamp: fx.clock.now, DisplayName: "agent " + agentID, @@ -173,38 +227,27 @@ func genKey(t *testing.T) *ecdsa.PrivateKey { return priv } -// drainOutbox claims and returns all pending outbox rows. -func (fx *identityFixture) drainOutbox(t *testing.T) []sqlite.OutboxEvent { +// drainSealed returns (and clears) everything the recording sealer +// accepted — the events the TL acknowledged, in seal order. +func (fx *identityFixture) drainSealed(t *testing.T) []sealedEvent { t.Helper() - rows, err := fx.outbox.Claim(context.Background(), 100) - if err != nil { - t.Fatal(err) - } - for _, row := range rows { - if err := fx.outbox.MarkSent(context.Background(), row.ID); err != nil { - t.Fatal(err) - } - } - return rows + fx.sealer.mu.Lock() + defer fx.sealer.mu.Unlock() + out := fx.sealer.events + fx.sealer.events = nil + return out } -// decodeOutboxEvent parses one outbox row's payload, verifies the -// producer signature against the fixture's signer key, and returns -// the inner identity event. -func (fx *identityFixture) decodeOutboxEvent(t *testing.T, row sqlite.OutboxEvent) *identityevent.Event { +// decodeSealed verifies one sealed record's producer signature +// against the fixture's signer key and returns the inner identity +// event. +func (fx *identityFixture) decodeSealed(t *testing.T, rec sealedEvent) *identityevent.Event { t.Helper() - var payload struct { - InnerEventCanonical json.RawMessage `json:"innerEventCanonical"` - ProducerSignature string `json:"producerSignature"` - } - if err := json.Unmarshal(row.PayloadJSON, &payload); err != nil { - t.Fatalf("payload: %v", err) - } - if _, err := anscrypto.VerifyWithPublicKey(fx.signerPub, payload.ProducerSignature, payload.InnerEventCanonical); err != nil { + if _, err := anscrypto.VerifyWithPublicKey(fx.signerPub, rec.Sig, rec.Inner); err != nil { t.Fatalf("producer signature: %v", err) } var inner identityevent.Event - if err := json.Unmarshal(payload.InnerEventCanonical, &inner); err != nil { + if err := json.Unmarshal(rec.Inner, &inner); err != nil { t.Fatalf("inner event: %v", err) } if err := inner.Validate(); err != nil { @@ -263,7 +306,7 @@ func TestIdentityRegister_DIDWebNoop(t *testing.T) { } // Register seals nothing — only proven control reaches the TL. - if rows := fx.drainOutbox(t); len(rows) != 0 { + if rows := fx.drainSealed(t); len(rows) != 0 { t.Fatalf("register must not emit, got %d rows", len(rows)) } } @@ -343,11 +386,11 @@ func TestIdentityVerifyControl_DIDWebNoop(t *testing.T) { t.Fatalf("verified state: %+v", identity) } - rows := fx.drainOutbox(t) - if len(rows) != 1 || rows[0].SchemaVersion != "IDENTITY" { - t.Fatalf("outbox rows: %+v", rows) + rows := fx.drainSealed(t) + if len(rows) != 1 { + t.Fatalf("sealed events: %d", len(rows)) } - inner := fx.decodeOutboxEvent(t, rows[0]) + inner := fx.decodeSealed(t, rows[0]) if inner.EventType != identityevent.TypeIdentityVerified || inner.IdentityID != identity.IdentityID || inner.ProviderID != fx.providerID || @@ -398,8 +441,8 @@ func TestIdentityVerifyControl_MultiKey(t *testing.T) { if _, err := fx.svc.VerifyControl(ctx, fx.providerID, res.Identity.IdentityID, service.ProofSubmission{SignedProofs: []string{jws1, jws2}}); err != nil { t.Fatalf("multi-key verify: %v", err) } - rows := fx.drainOutbox(t) - inner := fx.decodeOutboxEvent(t, rows[0]) + rows := fx.drainSealed(t) + inner := fx.decodeSealed(t, rows[0]) if len(inner.Keys) != 2 { t.Fatalf("sealed keys: %d", len(inner.Keys)) } @@ -666,8 +709,8 @@ func TestIdentityLifecycle_DIDKey(t *testing.T) { if identity.Status != domain.IdentityVerified || identity.ProofMethod != "did-key-sig" { t.Fatalf("did:key verified state: %+v", identity) } - rows := fx.drainOutbox(t) - inner := fx.decodeOutboxEvent(t, rows[0]) + rows := fx.drainSealed(t) + inner := fx.decodeSealed(t, rows[0]) if inner.Kind != "did:key" || len(inner.Keys) != 1 { t.Fatalf("did:key sealed event: %+v", inner) } @@ -718,8 +761,8 @@ func TestIdentityLifecycle_Ed25519(t *testing.T) { // The seal quotes the did:key Multikey method — its key material // is the method-specific id verbatim from the identifier. - rows := fx.drainOutbox(t) - inner := fx.decodeOutboxEvent(t, rows[0]) + rows := fx.drainSealed(t) + inner := fx.decodeSealed(t, rows[0]) var vm struct { Type string `json:"type"` PublicKeyMultibase string `json:"publicKeyMultibase"` @@ -770,7 +813,7 @@ func TestIdentityRotation_SealsUpdated(t *testing.T) { ctx := context.Background() identity, _ := verifyDIDWeb(t, fx, fx.providerID, "did:web:a.com") - fx.drainOutbox(t) + fx.drainSealed(t) rot, err := fx.svc.Rotate(ctx, fx.providerID, identity.IdentityID, "did:web:b.com") if err != nil { @@ -781,7 +824,7 @@ func TestIdentityRotation_SealsUpdated(t *testing.T) { } // Until the proof lands, the previously sealed state stands — // nothing emitted by the PUT itself. - if rows := fx.drainOutbox(t); len(rows) != 0 { + if rows := fx.drainSealed(t); len(rows) != 0 { t.Fatalf("PUT must not seal, got %d rows", len(rows)) } @@ -795,11 +838,11 @@ func TestIdentityRotation_SealsUpdated(t *testing.T) { t.Fatalf("rotated state: %+v", rotated) } - rows := fx.drainOutbox(t) + rows := fx.drainSealed(t) if len(rows) != 1 { t.Fatalf("rotation rows: %d", len(rows)) } - inner := fx.decodeOutboxEvent(t, rows[0]) + inner := fx.decodeSealed(t, rows[0]) if inner.EventType != identityevent.TypeIdentityUpdated || inner.Value != "did:web:b.com" || inner.PreviousValue != "did:web:a.com" { t.Fatalf("IDENTITY_UPDATED event: %+v", inner) @@ -812,7 +855,7 @@ func TestIdentityRevoke(t *testing.T) { ctx := context.Background() identity, _ := verifyDIDWeb(t, fx, fx.providerID, "did:web:a.com") - fx.drainOutbox(t) + fx.drainSealed(t) revoked, err := fx.svc.Revoke(ctx, fx.providerID, identity.IdentityID) if err != nil { @@ -821,8 +864,8 @@ func TestIdentityRevoke(t *testing.T) { if revoked.Status != domain.IdentityRevoked { t.Fatalf("status: %s", revoked.Status) } - rows := fx.drainOutbox(t) - inner := fx.decodeOutboxEvent(t, rows[0]) + rows := fx.drainSealed(t) + inner := fx.decodeSealed(t, rows[0]) if inner.EventType != identityevent.TypeIdentityRevoked || inner.RevokedAt == "" { t.Fatalf("IDENTITY_REVOKED event: %+v", inner) } @@ -892,7 +935,7 @@ func TestIdentityProvenUniquenessRace(t *testing.T) { } // The losing transaction rolled back whole — including the nonce // consumption — and only the winner's event sealed. - rows := fx.drainOutbox(t) + rows := fx.drainSealed(t) if len(rows) != 1 { t.Fatalf("sealed events after race: %d", len(rows)) } @@ -918,14 +961,14 @@ func TestIdentityOwnerGates(t *testing.T) { if _, err := fx.svc.Rotate(ctx, "owner-2", identity.IdentityID, "did:web:b.com"); !errors.Is(err, domain.ErrUnauthorized) { t.Fatalf("cross-owner rotate: %v", err) } - // List is owner-scoped. - mine, err := fx.svc.List(ctx, fx.providerID) - if err != nil || len(mine) != 1 { - t.Fatalf("list mine: %d %v", len(mine), err) + // List is owner-scoped (one page, default limit). + mine, err := fx.svc.List(ctx, fx.providerID, 0, "") + if err != nil || len(mine.Items) != 1 { + t.Fatalf("list mine: %+v %v", mine, err) } - theirs, err := fx.svc.List(ctx, "owner-2") - if err != nil || len(theirs) != 0 { - t.Fatalf("list theirs: %d %v", len(theirs), err) + theirs, err := fx.svc.List(ctx, "owner-2", 0, "") + if err != nil || len(theirs.Items) != 0 { + t.Fatalf("list theirs: %+v %v", theirs, err) } } @@ -937,7 +980,7 @@ func TestIdentityLinks(t *testing.T) { ctx := context.Background() identity, _ := verifyDIDWeb(t, fx, fx.providerID, "did:web:a.com") - fx.drainOutbox(t) + fx.drainSealed(t) fx.saveAgent(t, "agent-1", fx.providerID, "one.example.com") fx.saveAgent(t, "agent-2", fx.providerID, "two.example.com") fx.saveAgent(t, "agent-x", "owner-2", "theirs.example.com") @@ -948,7 +991,7 @@ func TestIdentityLinks(t *testing.T) { if !errors.Is(err, domain.ErrNotFound) { t.Fatalf("cross-owner agent in batch: %v", err) } - if rows := fx.drainOutbox(t); len(rows) != 0 { + if rows := fx.drainSealed(t); len(rows) != 0 { t.Fatal("failed batch must seal nothing") } @@ -957,11 +1000,11 @@ func TestIdentityLinks(t *testing.T) { if err != nil || linked != 2 { t.Fatalf("link batch: %d %v", linked, err) } - rows := fx.drainOutbox(t) + rows := fx.drainSealed(t) if len(rows) != 1 { t.Fatalf("link batch rows: %d", len(rows)) } - inner := fx.decodeOutboxEvent(t, rows[0]) + inner := fx.decodeSealed(t, rows[0]) if inner.EventType != identityevent.TypeIdentityLinked || len(inner.AnsIDs) != 2 { t.Fatalf("IDENTITY_LINKED event: %+v", inner) } @@ -971,7 +1014,7 @@ func TestIdentityLinks(t *testing.T) { if err != nil || linked != 0 { t.Fatalf("idempotent link: %d %v", linked, err) } - if rows := fx.drainOutbox(t); len(rows) != 0 { + if rows := fx.drainSealed(t); len(rows) != 0 { t.Fatal("idempotent link must seal nothing") } @@ -985,8 +1028,8 @@ func TestIdentityLinks(t *testing.T) { if err := fx.svc.Unlink(ctx, fx.providerID, identity.IdentityID, "agent-1"); err != nil { t.Fatalf("unlink: %v", err) } - rows = fx.drainOutbox(t) - inner = fx.decodeOutboxEvent(t, rows[0]) + rows = fx.drainSealed(t) + inner = fx.decodeSealed(t, rows[0]) if inner.EventType != identityevent.TypeIdentityUnlinked || len(inner.AnsIDs) != 1 || inner.AnsIDs[0] != "agent-1" { t.Fatalf("IDENTITY_UNLINKED event: %+v", inner) @@ -995,7 +1038,7 @@ func TestIdentityLinks(t *testing.T) { if err := fx.svc.Unlink(ctx, fx.providerID, identity.IdentityID, "agent-1"); !errors.Is(err, domain.ErrNotFound) { t.Fatalf("double unlink: %v", err) } - if rows := fx.drainOutbox(t); len(rows) != 0 { + if rows := fx.drainSealed(t); len(rows) != 0 { t.Fatal("failed unlink must seal nothing") } } @@ -1034,3 +1077,364 @@ func TestIdentityLinkGuards(t *testing.T) { t.Errorf("oversized batch: %v", err) } } + +// TestVerifyControl_SealFailureIsRetryable pins seal-before-success +// (§5.6.1): a TL failure surfaces retryable, consumes nothing — +// including the provisional claim — and the SAME proof succeeds once +// the TL returns. +func TestVerifyControl_SealFailureIsRetryable(t *testing.T) { + t.Parallel() + fx := newIdentityFixture(t, nil) + ctx := context.Background() + + res, err := fx.svc.Register(ctx, fx.providerID, "did:web:seal-fail.example.com") + if err != nil { + t.Fatal(err) + } + priv := genKey(t) + jws := signProof(t, priv, res.Identity.Value+"#key-1", res.Challenges[0].SigningInput, true) + sub := service.ProofSubmission{SignedProofs: []string{jws}} + + fx.sealer.fail(domain.NewUnavailableError("TL_UNAVAILABLE", "down")) + if _, err := fx.svc.VerifyControl(ctx, fx.providerID, res.Identity.IdentityID, sub); !errors.Is(err, domain.ErrUnavailable) { + t.Fatalf("want ErrUnavailable, got %v", err) + } + // Nothing sealed, row untouched, nonce unconsumed. + if rows := fx.drainSealed(t); len(rows) != 0 { + t.Fatalf("failed seal must record nothing, got %d", len(rows)) + } + identity, _, err := fx.svc.Detail(ctx, fx.providerID, res.Identity.IdentityID) + if err != nil { + t.Fatal(err) + } + if identity.Status != domain.IdentityPendingControl { + t.Fatalf("row must stand on seal failure, got %s", identity.Status) + } + + // TL back: the same proof (same nonce) succeeds — the claim was + // released, the nonce never consumed. + fx.sealer.fail(nil) + verified, err := fx.svc.VerifyControl(ctx, fx.providerID, res.Identity.IdentityID, sub) + if err != nil { + t.Fatalf("retry after TL recovery: %v", err) + } + if verified.Status != domain.IdentityVerified { + t.Fatalf("status: %s", verified.Status) + } + if rows := fx.drainSealed(t); len(rows) != 1 { + t.Fatalf("exactly one seal after recovery, got %d", len(rows)) + } +} + +// TestLink_LivenessGate pins §4.3: terminal and pre-activation agents +// are AGENT_NOT_LINKABLE (atomically — one bad agent fails the whole +// batch); DEPRECATED is deliberately linkable. +func TestLink_LivenessGate(t *testing.T) { + t.Parallel() + fx := newIdentityFixture(t, nil) + ctx := context.Background() + identity, _ := verifyDIDWeb(t, fx, fx.providerID, "did:web:liveness.example.com") + fx.drainSealed(t) + + fx.saveAgentWithStatus(t, "agent-active", fx.providerID, "a.example.com", domain.StatusActive) + fx.saveAgentWithStatus(t, "agent-deprecated", fx.providerID, "d.example.com", domain.StatusDeprecated) + fx.saveAgentWithStatus(t, "agent-revoked", fx.providerID, "r.example.com", domain.StatusRevoked) + fx.saveAgentWithStatus(t, "agent-pending", fx.providerID, "p.example.com", domain.StatusPendingValidation) + + for _, tc := range []struct { + name string + agentID string + }{ + {"terminal", "agent-revoked"}, + {"pre-activation", "agent-pending"}, + {"mixed batch is atomic", "agent-revoked"}, + } { + batch := []string{tc.agentID} + if tc.name == "mixed batch is atomic" { + batch = []string{"agent-active", tc.agentID} + } + _, err := fx.svc.Link(ctx, fx.providerID, identity.IdentityID, batch) + var de *domain.Error + if !errors.As(err, &de) || de.Code != "AGENT_NOT_LINKABLE" { + t.Fatalf("%s: want AGENT_NOT_LINKABLE, got %v", tc.name, err) + } + } + // The atomic rejection linked nothing. + if rows := fx.drainSealed(t); len(rows) != 0 { + t.Fatalf("rejected batches must seal nothing, got %d", len(rows)) + } + + // ACTIVE and DEPRECATED both link. + linked, err := fx.svc.Link(ctx, fx.providerID, identity.IdentityID, []string{"agent-active", "agent-deprecated"}) + if err != nil || linked != 2 { + t.Fatalf("live link: %d %v", linked, err) + } +} + +// TestLink_SealFailureLeavesNoRows pins seal-before-success on the +// link path: a failed seal writes no link rows and the retry links +// the full batch. +func TestLink_SealFailureLeavesNoRows(t *testing.T) { + t.Parallel() + fx := newIdentityFixture(t, nil) + ctx := context.Background() + identity, _ := verifyDIDWeb(t, fx, fx.providerID, "did:web:link-seal.example.com") + fx.drainSealed(t) + fx.saveAgent(t, "agent-ls", fx.providerID, "ls.example.com") + + fx.sealer.fail(domain.NewUnavailableError("TL_UNAVAILABLE", "down")) + if _, err := fx.svc.Link(ctx, fx.providerID, identity.IdentityID, []string{"agent-ls"}); !errors.Is(err, domain.ErrUnavailable) { + t.Fatalf("want ErrUnavailable, got %v", err) + } + if _, links, _ := fx.svc.Detail(ctx, fx.providerID, identity.IdentityID); len(links) != 0 { + t.Fatalf("failed seal must write no rows, got %d", len(links)) + } + + fx.sealer.fail(nil) + if linked, err := fx.svc.Link(ctx, fx.providerID, identity.IdentityID, []string{"agent-ls"}); err != nil || linked != 1 { + t.Fatalf("retry: %d %v", linked, err) + } +} + +// TestLink_RateLimited pins the §4.3 link-route limiter. +func TestLink_RateLimited(t *testing.T) { + t.Parallel() + fx := newIdentityFixture(t, nil) + fx.svc.WithLinkRateLimit(1) + ctx := context.Background() + identity, _ := verifyDIDWeb(t, fx, fx.providerID, "did:web:ratelimit.example.com") + fx.saveAgent(t, "agent-rl-1", fx.providerID, "rl1.example.com") + fx.saveAgent(t, "agent-rl-2", fx.providerID, "rl2.example.com") + + if _, err := fx.svc.Link(ctx, fx.providerID, identity.IdentityID, []string{"agent-rl-1"}); err != nil { + t.Fatalf("first link: %v", err) + } + _, err := fx.svc.Link(ctx, fx.providerID, identity.IdentityID, []string{"agent-rl-2"}) + var de *domain.Error + if !errors.As(err, &de) || de.Code != "RATE_LIMITED" { + t.Fatalf("want RATE_LIMITED, got %v", err) + } +} + +// TestUnlink_Discipline pins the unlink guards: rate limit shared +// with link, LINK_NOT_FOUND before anything seals, seal failure +// leaves the link standing, and a clean unlink seals exactly one +// IDENTITY_UNLINKED. +func TestUnlink_Discipline(t *testing.T) { + t.Parallel() + fx := newIdentityFixture(t, nil) + fx.svc.WithSealTimeout(2 * time.Second) + ctx := context.Background() + identity, _ := verifyDIDWeb(t, fx, fx.providerID, "did:web:unlink.example.com") + fx.saveAgent(t, "agent-ud", fx.providerID, "ud.example.com") + + // Unlink before any link: LINK_NOT_FOUND, nothing sealed. + err := fx.svc.Unlink(ctx, fx.providerID, identity.IdentityID, "agent-ud") + var de *domain.Error + if !errors.As(err, &de) || de.Code != "LINK_NOT_FOUND" { + t.Fatalf("want LINK_NOT_FOUND, got %v", err) + } + + if _, err := fx.svc.Link(ctx, fx.providerID, identity.IdentityID, []string{"agent-ud"}); err != nil { + t.Fatal(err) + } + fx.drainSealed(t) + + // Seal failure: the link stands, retry works. + fx.sealer.fail(domain.NewUnavailableError("TL_UNAVAILABLE", "down")) + if err := fx.svc.Unlink(ctx, fx.providerID, identity.IdentityID, "agent-ud"); !errors.Is(err, domain.ErrUnavailable) { + t.Fatalf("want ErrUnavailable, got %v", err) + } + if _, links, _ := fx.svc.Detail(ctx, fx.providerID, identity.IdentityID); len(links) != 1 { + t.Fatalf("failed unlink seal must leave the link, got %d", len(links)) + } + fx.sealer.fail(nil) + if err := fx.svc.Unlink(ctx, fx.providerID, identity.IdentityID, "agent-ud"); err != nil { + t.Fatalf("retry unlink: %v", err) + } + rows := fx.drainSealed(t) + if len(rows) != 1 { + t.Fatalf("unlink seals exactly one event, got %d", len(rows)) + } + if inner := fx.decodeSealed(t, rows[0]); inner.EventType != identityevent.TypeIdentityUnlinked { + t.Fatalf("event type: %s", inner.EventType) + } +} + +// TestVisibilityPredicate_RASide pins §5.6.3 on the management +// plane: a terminal agent's AgentDetails identities[] is empty, and +// the identity detail's linked list drops links to terminal agents — +// while the link rows (history) stay in place. +func TestVisibilityPredicate_RASide(t *testing.T) { + t.Parallel() + fx := newIdentityFixture(t, nil) + ctx := context.Background() + identity, _ := verifyDIDWeb(t, fx, fx.providerID, "did:web:predicate.example.com") + fx.saveAgent(t, "agent-vp-1", fx.providerID, "vp1.example.com") + fx.saveAgent(t, "agent-vp-2", fx.providerID, "vp2.example.com") + if _, err := fx.svc.Link(ctx, fx.providerID, identity.IdentityID, []string{"agent-vp-1", "agent-vp-2"}); err != nil { + t.Fatal(err) + } + + // Both live: both views full. + if got, err := fx.svc.LinkedIdentitiesForAgent(ctx, "agent-vp-1"); err != nil || len(got) != 1 { + t.Fatalf("live agent view: %v %v", got, err) + } + if _, links, _ := fx.svc.Detail(ctx, fx.providerID, identity.IdentityID); len(links) != 2 { + t.Fatalf("want 2 visible links, got %d", len(links)) + } + + // Terminal agent: drops from every current view. Flip the status + // on the EXISTING row (a fresh save would collide on ans_name). + reg, err := fx.agents.FindByAgentID(ctx, "agent-vp-2") + if err != nil { + t.Fatal(err) + } + reg.Status = domain.StatusRevoked + if err := fx.agents.Save(ctx, reg); err != nil { + t.Fatal(err) + } + if got, err := fx.svc.LinkedIdentitiesForAgent(ctx, "agent-vp-2"); err != nil || len(got) != 0 { + t.Fatalf("terminal agent view must be empty: %v %v", got, err) + } + if _, links, _ := fx.svc.Detail(ctx, fx.providerID, identity.IdentityID); len(links) != 1 { + t.Fatalf("terminal agent's link must drop from the count, got %d", len(links)) + } +} + +// TestVerifyControl_ClaimSerializesAttempts pins the seal claim: a +// held claim rejects a second attempt (VERIFICATION_IN_FLIGHT) +// without consuming anything. +func TestVerifyControl_ClaimSerializesAttempts(t *testing.T) { + t.Parallel() + fx := newIdentityFixture(t, nil) + ctx := context.Background() + res, err := fx.svc.Register(ctx, fx.providerID, "did:web:claimrace.example.com") + if err != nil { + t.Fatal(err) + } + priv := genKey(t) + jws := signProof(t, priv, res.Identity.Value+"#key-1", res.Challenges[0].SigningInput, true) + sub := service.ProofSubmission{SignedProofs: []string{jws}} + + // Simulate a concurrent in-flight attempt holding the claim. + store := sqlite.NewIdentityStore(fx.db) + now := fx.clock.now + if err := store.ClaimChallenge(ctx, res.Identity.IdentityID, res.Nonce, now, now.Add(-30*time.Second)); err != nil { + t.Fatal(err) + } + _, err = fx.svc.VerifyControl(ctx, fx.providerID, res.Identity.IdentityID, sub) + var de *domain.Error + if !errors.As(err, &de) || de.Code != "VERIFICATION_IN_FLIGHT" { + t.Fatalf("want VERIFICATION_IN_FLIGHT, got %v", err) + } + + // The holder releases (failed attempt) — the next attempt wins. + if err := store.ReleaseChallenge(ctx, res.Identity.IdentityID, res.Nonce); err != nil { + t.Fatal(err) + } + if _, err := fx.svc.VerifyControl(ctx, fx.providerID, res.Identity.IdentityID, sub); err != nil { + t.Fatalf("verify after release: %v", err) + } +} + +// TestLink_RevokeDuringSealRoundTrip pins the Phase C re-read: a +// revoke committing while the IDENTITY_LINKED seal is in flight must +// not gain live link rows — the §4.3 VERIFIED gate holds at commit, +// and the sealed link event is inert (the TL's read-time status is +// terminal on any revocation leaf). +func TestLink_RevokeDuringSealRoundTrip(t *testing.T) { + t.Parallel() + fx := newIdentityFixture(t, nil) + ctx := context.Background() + identity, _ := verifyDIDWeb(t, fx, fx.providerID, "did:web:race-link.example.com") + fx.saveAgent(t, "agent-race", fx.providerID, "race.example.com") + fx.drainSealed(t) + + store := sqlite.NewIdentityStore(fx.db) + fx.sealer.hook = func() { + if err := store.MarkRevoked(ctx, identity.IdentityID, fx.clock.now); err != nil { + t.Errorf("hook revoke: %v", err) + } + } + _, err := fx.svc.Link(ctx, fx.providerID, identity.IdentityID, []string{"agent-race"}) + var de *domain.Error + if !errors.As(err, &de) || de.Code != "IDENTITY_NOT_VERIFIED" { + t.Fatalf("want IDENTITY_NOT_VERIFIED at commit, got %v", err) + } + fx.sealer.hook = nil + if _, links, _ := fx.svc.Detail(ctx, fx.providerID, identity.IdentityID); len(links) != 0 { + t.Fatalf("revoked identity must gain no live links, got %d", len(links)) + } +} + +// TestVerifyControl_RevokeDuringSealRoundTrip pins the other side of +// the race: a revoke committing while a rotation's IDENTITY_UPDATED +// seal is in flight clears the nonce, so the verifier's conditional +// consume fails closed — the row stays REVOKED, never resurrected. +func TestVerifyControl_RevokeDuringSealRoundTrip(t *testing.T) { + t.Parallel() + fx := newIdentityFixture(t, nil) + ctx := context.Background() + identity, _ := verifyDIDWeb(t, fx, fx.providerID, "did:web:race-verify.example.com") + fx.drainSealed(t) + + // Stage a rotation (same value — key rotation) → fresh nonce. + res, err := fx.svc.Rotate(ctx, fx.providerID, identity.IdentityID, identity.Value) + if err != nil { + t.Fatal(err) + } + priv := genKey(t) + jws := signProof(t, priv, identity.Value+"#key-1", res.Challenges[0].SigningInput, true) + + store := sqlite.NewIdentityStore(fx.db) + fx.sealer.hook = func() { + if err := store.MarkRevoked(ctx, identity.IdentityID, fx.clock.now); err != nil { + t.Errorf("hook revoke: %v", err) + } + } + _, err = fx.svc.VerifyControl(ctx, fx.providerID, identity.IdentityID, service.ProofSubmission{SignedProofs: []string{jws}}) + if err == nil { + t.Fatal("verify racing a committed revoke must fail closed") + } + fx.sealer.hook = nil + got, _, gerr := fx.svc.Detail(ctx, fx.providerID, identity.IdentityID) + if gerr != nil || got.Status != domain.IdentityRevoked { + t.Fatalf("row must stay REVOKED, got %v (%v)", got.Status, gerr) + } +} + +// TestNilSealerFailsClosed pins seal-before-success's no-"seal +// later" rule: without a configured sealer every sealing operation +// refuses with TL_UNAVAILABLE and consumes nothing. +func TestNilSealerFailsClosed(t *testing.T) { + t.Parallel() + db, err := sqlite.Open(context.Background(), ":memory:") + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { _ = db.Close() }) + svc := service.NewIdentityService( + sqlite.NewIdentityStore(db), + sqlite.NewIdentityLinkStore(db), + sqlite.NewAgentStore(db), + didresolver.NewNoopResolver(), + nil, // no sealer + db, + ) + ctx := context.Background() + res, err := svc.Register(ctx, "owner-ns", "did:web:nosealer.example.com") + if err != nil { + t.Fatalf("register (no seal needed): %v", err) + } + priv := genKey(t) + jws := signProof(t, priv, res.Identity.Value+"#key-1", res.Challenges[0].SigningInput, true) + _, err = svc.VerifyControl(ctx, "owner-ns", res.Identity.IdentityID, service.ProofSubmission{SignedProofs: []string{jws}}) + if !errors.Is(err, domain.ErrUnavailable) { + t.Fatalf("nil sealer must fail closed with ErrUnavailable, got %v", err) + } + got, _, gerr := svc.Detail(ctx, "owner-ns", res.Identity.IdentityID) + if gerr != nil || got.Status != domain.IdentityPendingControl { + t.Fatalf("row must stand: %v (%v)", got.Status, gerr) + } +} diff --git a/internal/ra/service/identityratelimit.go b/internal/ra/service/identityratelimit.go index 129a3dd..33f947a 100644 --- a/internal/ra/service/identityratelimit.go +++ b/internal/ra/service/identityratelimit.go @@ -1,7 +1,6 @@ package service import ( - "encoding/json" "sync" "time" ) @@ -13,6 +12,13 @@ import ( // RA into a fetch proxy (design §3.7 "bounded fetch"). const defaultRegisterPerMinute = 10 +// defaultLinkPerMinute is the default per-owner budget for the link +// route (design §4.3): operational/DoS hardening — one link suffices +// for the named stolen-credential risk, so the bound limits blast +// radius and TL noise, not the risk itself. Links seal synchronously +// (§5.6.1), so the limiter also shields the TL ingest lane. +const defaultLinkPerMinute = 60 + // ownerLimiter is a fixed-window per-owner rate limiter. In-process // and intentionally simple: the window is a minute, the state is one // counter per owner, and stale owners are pruned opportunistically. @@ -67,14 +73,3 @@ func (l *ownerLimiter) prune(now time.Time) { } } } - -// marshalOutboxPayload renders the {innerEventCanonical, -// producerSignature} outbox payload — the bytes the worker replays -// verbatim. Shared by every event family; the inner canonical bytes -// are family-specific, the payload wrapper is not. -func marshalOutboxPayload(innerCanonical []byte, producerSig string) ([]byte, error) { - return json.Marshal(OutboxPayload{ - InnerEventCanonical: json.RawMessage(innerCanonical), - ProducerSignature: producerSig, - }) -} diff --git a/spec/api-spec-v2.yaml b/spec/api-spec-v2.yaml index 98073c1..88eef90 100644 --- a/spec/api-spec-v2.yaml +++ b/spec/api-spec-v2.yaml @@ -966,20 +966,39 @@ paths: tags: - Verified Identities summary: List my identities - description: Returns every identity owned by the caller, newest first. + description: | + Returns one page of the caller's identities, newest first, in + the standard limit + opaque-cursor envelope (the agent-list + convention). Pass `nextCursor` back as `cursor` for the next + page; a null `nextCursor` is the last page. operationId: listIdentities + parameters: + - name: limit + in: query + required: false + schema: { type: integer, default: 20, minimum: 1, maximum: 100 } + - name: cursor + in: query + required: false + schema: { type: string } + description: Opaque cursor from the previous page's nextCursor responses: '200': - description: The caller's identities + description: One page of the caller's identities content: application/json: schema: type: object + required: [identities, nextCursor] properties: identities: type: array items: $ref: '#/components/schemas/IdentityDetails' + nextCursor: + type: string + nullable: true + description: Cursor for the next page; null on the last page '401': description: Authentication failed content: @@ -1081,11 +1100,19 @@ paths: closed. The nonce is consumed exactly once, inside the success transaction — a failed attempt does not consume it. - On success the identity flips to `VERIFIED` (or completes a - staged rotation) and the RA seals `IDENTITY_VERIFIED` / + JWS bearing unrecognized `crit` header parameters are + rejected (RFC 7515 §4.1.11) — this verifier implements no + critical extensions. + + Seal-before-success: the RA seals `IDENTITY_VERIFIED` / `IDENTITY_UPDATED` on the identity's own Transparency Log - stream, with every proven key sealed self-verifyingly - (public key + signed proof). + stream — every proven key sealed self-verifyingly (public + key + signed proof) — and reports success ONLY after the TL + acknowledges the seal; the row transition commits with that + acknowledgment, so anything this API reports as set up is + resolvable in the TL at that moment. If the TL is + unavailable the call fails 503 `TL_UNAVAILABLE`, retryable: + the nonce is NOT consumed and the prior state stands. operationId: verifyIdentityControl parameters: - $ref: '#/components/parameters/IdentityIdPath' @@ -1118,8 +1145,12 @@ paths: description: | Challenge state error — `PRICC_TOKEN_EXPIRED`, `PRICC_TOKEN_ALREADY_USED`, `IDENTIFIER_CHALLENGE_EXPIRED` - — or the identity is revoked. Recovery from an expired - nonce is the idempotent re-add (re-POST the same value). + — the identity is revoked, a concurrent verify attempt + holds the seal claim (`VERIFICATION_IN_FLIGHT` — retry + shortly; the claim expires within ~30s), or the + identifier is already verified by another owner + (`IDENTIFIER_DUPLICATE`). Recovery from an expired nonce + is the idempotent re-add (re-POST the same value). content: application/json: schema: @@ -1134,6 +1165,13 @@ paths: schema: $ref: '#/components/schemas/ErrorResponse' + '503': + description: Transparency Log unavailable (TL_UNAVAILABLE — retryable; the nonce is NOT consumed) + content: + application/json: + schema: + $ref: '#/components/schemas/ErrorResponse' + /ans/identities/{identityId}/revoke: post: tags: @@ -1146,6 +1184,10 @@ paths: append-only in the Transparency Log. Propagation to every linked agent's badge is the TL's read-time join, not a write fan-out. + + Seal-before-success: the row flips only after the TL + acknowledges the seal. TL unavailable → 503 `TL_UNAVAILABLE`, + retryable, the identity stays VERIFIED. operationId: revokeIdentity parameters: - $ref: '#/components/parameters/IdentityIdPath' @@ -1174,6 +1216,12 @@ paths: application/json: schema: $ref: '#/components/schemas/ErrorResponse' + '503': + description: Transparency Log unavailable (TL_UNAVAILABLE — retryable, nothing changed) + content: + application/json: + schema: + $ref: '#/components/schemas/ErrorResponse' /ans/identities/{identityId}/links: post: @@ -1189,7 +1237,19 @@ paths: the agent ids; an agent's own stream and audit history are never written by identity operations. Already-linked agents are skipped idempotently; a call that links nothing new seals - nothing. Links attach only while the identity is VERIFIED. + nothing. + + Liveness gate: links attach only while the identity is + VERIFIED, and every named agent must be live — ACTIVE or + DEPRECATED; a terminal or pre-activation agent fails the + whole batch with 422 `AGENT_NOT_LINKABLE` (all-or-nothing, + matching the one-event batch semantics). The route is + per-owner rate limited (429-equivalent `RATE_LIMITED`). + + Seal-before-success: success is reported only after the TL + acknowledges the `IDENTITY_LINKED` seal; link rows commit + with the acknowledgment. TL unavailable → 503 + `TL_UNAVAILABLE`, retryable, nothing linked. operationId: linkIdentityAgents parameters: - $ref: '#/components/parameters/IdentityIdPath' @@ -1225,7 +1285,14 @@ paths: schema: $ref: '#/components/schemas/ErrorResponse' '422': - description: Invalid link request (empty or oversized batch) + description: Invalid link request (empty or oversized batch — INVALID_LINK_REQUEST), or a named agent is not live (AGENT_NOT_LINKABLE — links require ACTIVE or DEPRECATED; rejected all-or-nothing) + content: + application/json: + schema: + $ref: '#/components/schemas/ErrorResponse' + + '503': + description: Transparency Log unavailable (TL_UNAVAILABLE — retryable, nothing linked) content: application/json: schema: @@ -1240,7 +1307,12 @@ paths: Ends one association and seals `IDENTITY_UNLINKED` on the identity stream. The association's history persists in the identity's audit chain and the raw log tiles; unlinked pairs - may be re-linked later. + may be re-linked later. Shares the link route's per-owner + rate limit. + + Seal-before-success: the link row flips only after the TL + acknowledges the seal. TL unavailable → 503 `TL_UNAVAILABLE`, + retryable, the link stands. operationId: unlinkIdentityAgent parameters: - $ref: '#/components/parameters/IdentityIdPath' @@ -1260,6 +1332,12 @@ paths: application/json: schema: $ref: '#/components/schemas/ErrorResponse' + '503': + description: Transparency Log unavailable (TL_UNAVAILABLE — retryable, nothing changed) + content: + application/json: + schema: + $ref: '#/components/schemas/ErrorResponse' components: From 073a16f61846fc988a9689661893349939be84b8 Mon Sep 17 00:00:00 2001 From: Connor Snitker Date: Thu, 11 Jun 2026 18:29:31 -0500 Subject: [PATCH 06/12] feat(tl): visibility predicate, verbatim keys[] views, terminal revocation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The TL half of the 2026-06-11 review-pass deltas (design §5.6.3): - Computed views carry the keys: the badge identities[] join and the identity badge quote the CURRENT proven key set verbatim from the latest sealed proof event (json.RawMessage end to end), with keysLogId pointing at that seal — a verifier checks operator signatures from the badge alone. provenKeyIds/identityLogId are replaced. writeJSON disables HTML escaping so the quoted sealed bytes survive byte-verbatim. - One visibility predicate on every current view: an entry appears while the link is LINKED and the agent is live (ACTIVE/DEPRECATED/ WARNING). REVOKED identities STAY VISIBLE with identityStatus REVOKED and keys withheld — a verifier must see the who behind a still-linked agent was revoked. Terminal agents drop out of the badge join and the reverse join; history routes are untouched. - Revocation is terminal at read time: status derives REVOKED from ANY IDENTITY_REVOKED leaf on the stream, never tail-only — a racing operation's leaf landing after the revocation can never resurrect the identity on the public surface. - Join failure is explicit, never silent: the badge serves the agent material with identitiesUnavailable: true when the join cannot be computed; the reverse join propagates non-not-found failures instead of shrinking the answer. - Badge identities[] carries a small safety cap with identitiesTotal; the standalone per-agent route and the reverse join are paginated (TL limit/offset convention) as the overflow targets. Signed-off-by: Connor Snitker --- internal/adapter/docsui/openapi/tl.yaml | 146 ++++++++++++--- internal/adapter/store/sqlitetl/events.go | 22 +++ internal/tl/handler/handler.go | 69 ++++++- internal/tl/handler/handler_identity_test.go | 171 +++++++++++++++-- internal/tl/handler/paginate_internal_test.go | 26 +++ internal/tl/service/badge.go | 23 +++ internal/tl/service/identitybadge.go | 174 +++++++++++++----- internal/tl/service/log.go | 7 + spec/api-spec-tl-v2.yaml | 146 ++++++++++++--- 9 files changed, 660 insertions(+), 124 deletions(-) create mode 100644 internal/tl/handler/paginate_internal_test.go diff --git a/internal/adapter/docsui/openapi/tl.yaml b/internal/adapter/docsui/openapi/tl.yaml index fa98ebc..4664a78 100644 --- a/internal/adapter/docsui/openapi/tl.yaml +++ b/internal/adapter/docsui/openapi/tl.yaml @@ -332,11 +332,21 @@ paths: summary: Identity badge description: | The latest sealed identity event + inclusion proof + computed - status (`VERIFIED` | `REVOKED`). The proof events seal every - proven key self-verifyingly: any third party reads the key - out of the sealed verbatim `verificationMethod`, verifies the - sealed `signedProof` against it, and confirms the payload - binds this identityId — offline, without trusting the RA. + `{status, keys[], keysLogId}`: the latest entry may be a + link/unlink/revocation carrying no key material, so the badge + quotes the CURRENT proven key set verbatim from the latest + sealed proof event, with `keysLogId` pointing at that seal + (omitted when REVOKED — the keys are no longer attested). + + Third-party verification is byte-recompute, not + parse-and-inspect: verify the sealed `signedProof` JWS + against the key inside the sealed `verificationMethod`; take + the nonce from the decoded payload; reconstruct the + IdentityProofInput from the sealed event plus that nonce; + JCS-serialize; and require the JWS payload segment to equal + those bytes verbatim. Verifiers MUST reject a JWS bearing + unrecognized `crit` headers (RFC 7515 §4.1.11). All offline, + without trusting the RA. operationId: getIdentityBadge parameters: - $ref: '#/components/parameters/IdentityIdPath' @@ -425,13 +435,23 @@ paths: tags: [Transparency Log] summary: Agents currently linked to the identity (reverse join) description: | - Computed at query time from the link events' agent index: - every agent whose latest link/unlink fact naming it is - LINKED, each decorated with its own computed badge status so - a reader checks both ends of the link in one response. + Computed at query time from the link events' agent index + under the visibility predicate: every agent whose latest + link/unlink fact naming it is LINKED and whose own computed + status is live, decorated with that status. Paginated — the + reverse join is the genuinely unbounded read (an identity + links to unlimited agents). operationId: getIdentityAgents parameters: - $ref: '#/components/parameters/IdentityIdPath' + - name: limit + in: query + required: false + schema: { type: integer, default: 50, minimum: 1, maximum: 200 } + - name: offset + in: query + required: false + schema: { type: integer, default: 0, minimum: 0 } responses: '200': description: Currently-linked agents. @@ -439,12 +459,15 @@ paths: application/json: schema: type: object - required: [agents] + required: [agents, total] properties: agents: type: array items: $ref: '#/components/schemas/LinkedAgentView' + total: + type: integer + description: Full visible-link count across all pages '404': description: No events for this identity. content: @@ -458,11 +481,22 @@ paths: summary: Identities currently linked to the agent (computed) description: | The same entries as the agent badge's `identities[]` field, - served alone. Computed at read time — link live ∧ identity - stream state — never stored on, or sealed into, the agent. + served alone and paginated — the overflow target for the + badge's embedded safety cap. Computed at read time under the + visibility predicate (link LINKED ∧ agent live; a REVOKED + identity stays visible with no keys) — never stored on, or + sealed into, the agent. operationId: getAgentIdentities parameters: - $ref: '#/components/parameters/AgentIdPath' + - name: limit + in: query + required: false + schema: { type: integer, default: 50, minimum: 1, maximum: 200 } + - name: offset + in: query + required: false + schema: { type: integer, default: 0, minimum: 0 } responses: '200': description: Currently-linked identities. @@ -470,12 +504,15 @@ paths: application/json: schema: type: object - required: [identities] + required: [identities, total] properties: identities: type: array items: $ref: '#/components/schemas/LinkedIdentityView' + total: + type: integer + description: Full visible-link count across all pages /v1/agents/{agentId}/identities/history: get: @@ -1030,12 +1067,49 @@ components: type: array description: | Agent badges only — the COMPUTED read-time join of the - agent's currently-linked verified identities. Covered by - the TL's response signature, never by any seal; identity - rotation/revocation is visible here immediately with - zero agent-stream writes. + agent's currently-linked verified identities, under the + visibility predicate (link LINKED AND agent live). A + REVOKED identity stays visible with identityStatus + REVOKED and no keys[] — a verifier must see the who was + revoked. Covered by the TL's response signature, never by + any seal; identity rotation/revocation is visible here + immediately with zero agent-stream writes. Embeds at most + a small safety cap of entries; identitiesTotal carries + the full count and /v1/agents/{agentId}/identities is the + paginated overflow target. items: $ref: '#/components/schemas/LinkedIdentityView' + identitiesTotal: + type: integer + description: | + Agent badges only — the full count of visible links when + identities[] is present (the embedded array is capped). + identitiesUnavailable: + type: boolean + description: | + Agent badges only — set when the identities join could + not be computed. Join failure is explicit, never silent: + an absent or empty identities[] always means "no visible + links", never "the join failed". + keys: + type: array + description: | + Identity badges only — the CURRENT proven key set quoted + VERBATIM from the latest sealed proof event + (IDENTITY_VERIFIED / IDENTITY_UPDATED): the latest stream + entry may be a link/unlink/revocation carrying no key + material, so the badge answers "what are the attested + keys" directly. Verification methods only — the + signedProof evidence lives in the seal at keysLogId, one + hop away. Omitted when the identity is REVOKED. + items: + type: object + description: A sealed verification method, member-for-member verbatim + keysLogId: + type: string + description: | + Identity badges only — the sealed proof event keys[] is + quoted from. Omitted when the identity is REVOKED. TransparencyLogAudit: type: object @@ -1151,7 +1225,12 @@ components: LinkedIdentityView: type: object - description: One computed identities[] entry on the agent badge. + description: | + One computed identities[] entry on the agent badge, under the + visibility predicate: the entry appears while the link is + LINKED and the agent is live; identityStatus reports the + identity's current stream state (a REVOKED identity stays + visible — the attestation rule withholds only its keys). required: [identityId, kind, value, identityStatus] properties: identityId: { type: string } @@ -1161,20 +1240,26 @@ components: type: string enum: [VERIFIED, REVOKED] description: Reflects the identity stream NOW - provenKeyIds: + keys: type: array description: | - Verification-method ids of the current proven key set - (post-rotation). The full verbatim methods live in the - sealed proof event. - items: { type: string } + The CURRENT proven key set quoted VERBATIM from the + latest sealed proof event — verification methods only, + member-for-member as sealed, so a verifier checks + operator signatures from the badge alone (no audit + walk). The signedProof evidence lives in the seal at + keysLogId. Omitted when identityStatus is REVOKED: the + keys are no longer attested. + items: + type: object + description: A sealed verification method, verbatim + keysLogId: + type: string + description: The sealed proof event keys[] is quoted from — fetch for signedProofs / offline evidence. Omitted when REVOKED. linkedAt: { type: string, format: date-time } linkLogId: type: string description: The sealed IDENTITY_LINKED entry on the identity stream — fetch for link evidence - identityLogId: - type: string - description: Latest identity-stream entry — fetch for the identity evidence/history LinkedAgentView: type: object @@ -1185,8 +1270,13 @@ components: linkedAt: { type: string, format: date-time } agentStatus: type: string - enum: [ACTIVE, REVOKED, DEPRECATED, EXPIRED, WARNING] - description: The linked agent's own computed badge status — a link is effective only while both ends are live + enum: [ACTIVE, DEPRECATED, WARNING] + description: | + The linked agent's own computed badge status. The reverse + join applies the visibility predicate — only live agents + (ACTIVE / DEPRECATED / WARNING) appear; terminal agents' + links drop out of every "current" view (history stays in + the audit chain). # ── Admin: producer keys ─────────────────────────────────── ProducerKeyRequest: diff --git a/internal/adapter/store/sqlitetl/events.go b/internal/adapter/store/sqlitetl/events.go index 84aa033..3ca7283 100644 --- a/internal/adapter/store/sqlitetl/events.go +++ b/internal/adapter/store/sqlitetl/events.go @@ -323,6 +323,28 @@ func (s *EventStore) GetLatestProofByIdentityID(ctx context.Context, identityID return &r, nil } +// HasIdentityRevoked reports whether the identity's stream contains +// an IDENTITY_REVOKED event. Revocation is terminal at READ time +// regardless of stream tail order: a racing operation's event can +// land after the revocation leaf (the RA's seal spans a network +// round trip), and a late leaf must never resurrect a revoked +// identity on the public surface. +func (s *EventStore) HasIdentityRevoked(ctx context.Context, identityID string) (bool, error) { + var one int + err := s.db.db.GetContext(ctx, &one, ` + SELECT 1 FROM tl_events + WHERE identity_id = ? AND event_type = 'IDENTITY_REVOKED' + LIMIT 1`, identityID) + switch { + case err == nil: + return true, nil + case errors.Is(err, sql.ErrNoRows): + return false, nil + default: + return false, err + } +} + // LinkStatesByAgent returns, for one agent, the latest link/unlink // fact per identity that ever named it — the badge-join input. Rows // where Linked() is true are the agent's live links. diff --git a/internal/tl/handler/handler.go b/internal/tl/handler/handler.go index 88b4566..928a263 100644 --- a/internal/tl/handler/handler.go +++ b/internal/tl/handler/handler.go @@ -262,32 +262,71 @@ func (h *Handlers) GetBadge(w http.ResponseWriter, r *http.Request) { return } if h.identityBadge != nil { - identities, jerr := h.identityBadge.LinkedIdentitiesForAgent(r.Context(), agentID) + identities, jerr := h.identityBadge.LinkedIdentitiesForAgent(r.Context(), agentID, tl.Status) if jerr != nil { - writeError(w, jerr) - return + // Join failure is explicit, never silent (§5.6.3): the + // badge still serves the agent's sealed material; the + // flag says the identities view could not be computed — + // an empty identities[] always means "no visible links". + tl.IdentitiesUnavailable = true + } else { + tl.IdentitiesTotal = len(identities) + if len(identities) > badgeIdentitiesCap { + identities = identities[:badgeIdentitiesCap] + } + tl.Identities = identities } - tl.Identities = identities } writeJSON(w, http.StatusOK, tl) } +// badgeIdentitiesCap bounds the badge's embedded identities[] — +// identities-per-agent is realistically tiny, and the standalone +// paginated /v1/agents/{agentId}/identities route is the overflow +// target (§5.6.1). identitiesTotal always carries the full count. +const badgeIdentitiesCap = 25 + // GetAgentIdentities handles GET /v1/agents/{agentId}/identities — // the computed list of identities currently linked to the agent. // Identical entries to the badge's identities[] field, served alone -// for callers who don't need the full badge. +// and paginated (TL limit/offset convention) — the overflow target +// for the badge's embedded cap. func (h *Handlers) GetAgentIdentities(w http.ResponseWriter, r *http.Request) { agentID := chi.URLParam(r, "agentId") if agentID == "" { writeError(w, domain.NewValidationError("MISSING_AGENT_ID", "agentId is required")) return } - identities, err := h.identityBadge.LinkedIdentitiesForAgent(r.Context(), agentID) + // The agent's computed status is the liveness conjunct of the + // visibility predicate — and resolving it 404s unknown agents, + // parity with the badge route. + agentTL, err := h.badge.Get(r.Context(), agentID) + if err != nil { + writeError(w, err) + return + } + identities, err := h.identityBadge.LinkedIdentitiesForAgent(r.Context(), agentID, agentTL.Status) if err != nil { writeError(w, err) return } - writeJSON(w, http.StatusOK, map[string]any{"identities": identities}) + limit, offset := parsePagination(r) + total := len(identities) + identities = paginateIdentityViews(identities, limit, offset) + writeJSON(w, http.StatusOK, map[string]any{"identities": identities, "total": total}) +} + +// paginateIdentityViews applies the TL limit/offset convention to a +// computed (already fully materialized) view slice. +func paginateIdentityViews[T any](views []T, limit, offset int) []T { + if offset >= len(views) { + return []T{} + } + views = views[offset:] + if limit > 0 && limit < len(views) { + views = views[:limit] + } + return views } // GetAgentIdentityHistory handles @@ -395,7 +434,12 @@ func (h *Handlers) GetIdentityAgents(w http.ResponseWriter, r *http.Request) { writeError(w, err) return } - writeJSON(w, http.StatusOK, map[string]any{"agents": agents}) + // Paginated (TL limit/offset convention): the reverse join is the + // genuinely unbounded read — an identity links to unlimited agents. + limit, offset := parsePagination(r) + total := len(agents) + agents = paginateIdentityViews(agents, limit, offset) + writeJSON(w, http.StatusOK, map[string]any{"agents": agents, "total": total}) } // GetAudit handles GET /v1/agents/{agentId}/audit. Matches the @@ -650,7 +694,14 @@ func writeJSON(w http.ResponseWriter, status int, body any) { w.Header().Set("Content-Type", "application/json") w.WriteHeader(status) if body != nil { - _ = json.NewEncoder(w).Encode(body) + enc := json.NewEncoder(w) + // The TL serves SEALED bytes quoted verbatim (keys[], the + // echoed payload): Go's default HTML escaping would rewrite + // &, <, > inside json.RawMessage, breaking the byte-verbatim + // contract a verifier byte-compares against the seal. This + // is a pure-JSON API — HTML escaping protects nothing here. + enc.SetEscapeHTML(false) + _ = enc.Encode(body) } } diff --git a/internal/tl/handler/handler_identity_test.go b/internal/tl/handler/handler_identity_test.go index 18b79ce..04877b7 100644 --- a/internal/tl/handler/handler_identity_test.go +++ b/internal/tl/handler/handler_identity_test.go @@ -22,7 +22,7 @@ const testIdentityID = "01HXKQTESTIDENTITY0000000A" // keyed to the testbed's identity fixture. timestamps vary per call // site so dedup never collides across events in one test; proof // events name their verification method by the given kid so the -// read-join's provenKeyIds visibly flips on rotation. +// read-join's quoted keys[] visibly flip on rotation. func identityInner(typ identityevent.Type, ts string, ansIDs []string, kid string) identityevent.Event { ev := identityevent.Event{ EventType: typ, @@ -79,18 +79,31 @@ type badgeView struct { SchemaVersion string `json:"schemaVersion"` Status string `json:"status"` Signature string `json:"signature"` - Identities []struct { - IdentityID string `json:"identityId"` - Kind string `json:"kind"` - Value string `json:"value"` - IdentityStatus string `json:"identityStatus"` - ProvenKeyIDs []string `json:"provenKeyIds"` - LinkedAt string `json:"linkedAt"` - LinkLogID string `json:"linkLogId"` - IdentityLogID string `json:"identityLogId"` + + // Identity badge only: the computed current attestation. + Keys []verificationMethodView `json:"keys"` + KeysLogID string `json:"keysLogId"` + + IdentitiesTotal int `json:"identitiesTotal"` + IdentitiesUnavailable bool `json:"identitiesUnavailable"` + Identities []struct { + IdentityID string `json:"identityId"` + Kind string `json:"kind"` + Value string `json:"value"` + IdentityStatus string `json:"identityStatus"` + Keys []verificationMethodView `json:"keys"` + KeysLogID string `json:"keysLogId"` + LinkedAt string `json:"linkedAt"` + LinkLogID string `json:"linkLogId"` } `json:"identities"` } +// verificationMethodView decodes the id member of a quoted verbatim +// verification method — enough to assert which key is attested. +type verificationMethodView struct { + ID string `json:"id"` +} + // auditView decodes the subset of audit responses the stages assert // on. type auditView struct { @@ -141,6 +154,11 @@ func stageVerify(t *testing.T, tb *tlTestbed, agentID string) { if idBadge.SchemaVersion != "V2" || idBadge.Signature == "" { t.Fatalf("identity badge missing schema/attestation: %+v", idBadge) } + // Computed current attestation (§5.6.3): the badge quotes the + // proven keys + the seal they came from. + if len(idBadge.Keys) != 1 || idBadge.Keys[0].ID != "did:web:identity.acme-corp.com#key-1" || idBadge.KeysLogID == "" { + t.Fatalf("identity badge keys quote: %+v", idBadge) + } var agentBadge badgeView if code := getJSON(t, tb, "/v1/agents/"+agentID, &agentBadge); code != http.StatusOK { @@ -170,22 +188,33 @@ func stageLink(t *testing.T, tb *tlTestbed, agentID string) { got.Kind != "did:web" || got.Value != "did:web:identity.acme-corp.com" { t.Fatalf("join entry wrong: %+v", got) } - if len(got.ProvenKeyIDs) != 1 || got.ProvenKeyIDs[0] != "did:web:identity.acme-corp.com#key-1" { - t.Fatalf("provenKeyIds = %v", got.ProvenKeyIDs) + if len(got.Keys) != 1 || got.Keys[0].ID != "did:web:identity.acme-corp.com#key-1" || got.KeysLogID == "" { + t.Fatalf("quoted keys = %+v", got) } - if got.LinkedAt != "2026-06-10T11:00:00Z" || got.LinkLogID == "" || got.IdentityLogID == "" { + if got.LinkedAt != "2026-06-10T11:00:00Z" || got.LinkLogID == "" { t.Fatalf("link evidence fields missing: %+v", got) } + if agentBadge.IdentitiesTotal != 1 { + t.Fatalf("identitiesTotal = %d, want 1", agentBadge.IdentitiesTotal) + } - // The standalone computed view matches the badge join. + // The standalone computed view matches the badge join — and is + // paginated with a total (the badge-cap overflow target). var identitiesResp struct { Identities []json.RawMessage `json:"identities"` + Total int `json:"total"` } if code := getJSON(t, tb, "/v1/agents/"+agentID+"/identities", &identitiesResp); code != http.StatusOK { t.Fatalf("agent identities view: %d", code) } - if len(identitiesResp.Identities) != 1 { - t.Fatalf("agent identities view count = %d", len(identitiesResp.Identities)) + if len(identitiesResp.Identities) != 1 || identitiesResp.Total != 1 { + t.Fatalf("agent identities view: %d of %d", len(identitiesResp.Identities), identitiesResp.Total) + } + if code := getJSON(t, tb, "/v1/agents/"+agentID+"/identities?limit=1&offset=1", &identitiesResp); code != http.StatusOK { + t.Fatalf("paged identities view: %d", code) + } + if len(identitiesResp.Identities) != 0 || identitiesResp.Total != 1 { + t.Fatalf("offset page must be empty with total intact: %d of %d", len(identitiesResp.Identities), identitiesResp.Total) } // Reverse join: identity → agents, with the agent's own status. @@ -203,6 +232,13 @@ func stageLink(t *testing.T, tb *tlTestbed, agentID string) { agentsResp.Agents[0].AgentStatus != "ACTIVE" { t.Fatalf("reverse join: %+v", agentsResp.Agents) } + // Reverse join is paginated too (the genuinely unbounded read). + if code := getJSON(t, tb, "/v1/identities/"+testIdentityID+"/agents?limit=1&offset=1", &agentsResp); code != http.StatusOK { + t.Fatalf("paged reverse join: %d", code) + } + if len(agentsResp.Agents) != 0 { + t.Fatalf("offset page of reverse join must be empty: %+v", agentsResp.Agents) + } } // stageRotate seals IDENTITY_UPDATED and checks the proven-key flip @@ -216,8 +252,8 @@ func stageRotate(t *testing.T, tb *tlTestbed, agentID string) { if code := getJSON(t, tb, "/v1/agents/"+agentID, &agentBadge); code != http.StatusOK { t.Fatalf("agent badge after rotation: %d", code) } - if ids := agentBadge.Identities[0].ProvenKeyIDs; len(ids) != 1 || ids[0] != "did:web:identity.acme-corp.com#key-2" { - t.Fatalf("rotation not visible on linked badge: %v", ids) + if keys := agentBadge.Identities[0].Keys; len(keys) != 1 || keys[0].ID != "did:web:identity.acme-corp.com#key-2" { + t.Fatalf("rotation not visible on linked badge: %+v", keys) } // The agent's own audit history stays purely AGENT_* — identity @@ -240,6 +276,14 @@ func stageRotate(t *testing.T, tb *tlTestbed, agentID string) { if audit.Records[0].Payload.Producer.Event.EventType != "IDENTITY_UPDATED" { t.Fatalf("identity audit order: %+v", audit.Records[0]) } + // Pagination clamps apply on the audit too (newest-first: offset 1 + // of the 3-event chain is the IDENTITY_LINKED leaf). + if code := getJSON(t, tb, "/v1/identities/"+testIdentityID+"/audit?limit=1&offset=1", &audit); code != http.StatusOK { + t.Fatalf("paged identity audit: %d", code) + } + if len(audit.Records) != 1 || audit.Records[0].Payload.Producer.Event.EventType != "IDENTITY_LINKED" { + t.Fatalf("paged identity audit: %+v", audit.Records) + } // Association history for the agent: the standard audit envelope // filtered to link events naming it. @@ -272,6 +316,14 @@ func stageRevoke(t *testing.T, tb *tlTestbed, agentID string) { if len(agentBadge.Identities) != 1 || agentBadge.Identities[0].IdentityStatus != "REVOKED" { t.Fatalf("revocation not visible on linked badge: %+v", agentBadge.Identities) } + // Visibility ≠ attestation (§5.6.3): the entry stays — a verifier + // must SEE the revocation — but the keys are withheld. + if len(agentBadge.Identities[0].Keys) != 0 || agentBadge.Identities[0].KeysLogID != "" { + t.Fatalf("revoked identity must carry no attested keys: %+v", agentBadge.Identities[0]) + } + if len(idBadge.Keys) != 0 || idBadge.KeysLogID != "" { + t.Fatalf("revoked identity badge must carry no keys quote: %+v", idBadge) + } // The agent itself is untouched (the what survives the who's // revocation, and vice versa). if agentBadge.Status != "ACTIVE" { @@ -293,6 +345,20 @@ func stageUnlink(t *testing.T, tb *tlTestbed, agentID string) { if len(unlinkedBadge.Identities) != 0 { t.Fatalf("identities after unlink: %+v", unlinkedBadge.Identities) } + // REVOKED is terminal at read time: this unlink leaf landed AFTER + // the revocation, and the identity badge must NOT flip back to + // VERIFIED on latest-event-wins — a late leaf can never resurrect + // a revoked identity (nor re-attach its keys). + var idBadge badgeView + if code := getJSON(t, tb, "/v1/identities/"+testIdentityID, &idBadge); code != http.StatusOK { + t.Fatalf("identity badge after post-revoke leaf: %d", code) + } + if idBadge.Status != "REVOKED" { + t.Fatalf("post-revoke leaf resurrected the identity: status=%q", idBadge.Status) + } + if len(idBadge.Keys) != 0 || idBadge.KeysLogID != "" { + t.Fatalf("post-revoke leaf re-attached keys: %+v", idBadge) + } var audit auditView if code := getJSON(t, tb, "/v1/agents/"+agentID+"/identities/history", &audit); code != http.StatusOK { t.Fatalf("identity history after unlink: %d", code) @@ -405,3 +471,72 @@ func TestIdentityReads_NotFound(t *testing.T) { t.Errorf("unknown identity audit: code=%d records=%d", code, len(audit.Records)) } } + +// TestAgentLivenessConjunct pins the §5.6.3 visibility predicate's +// agent leg on the TL: once the agent's stream goes terminal +// (AGENT_REVOKED), its identities[] views empty out — the link +// history stays recoverable — while the identity itself is untouched. +func TestAgentLivenessConjunct(t *testing.T) { + tb := newTLTestbed(t) + + agentBody := []byte(mustJSON(t, tb.inner)) + tb.postEvent(t, agentBody, tb.signWithProducer(t, agentBody)) + agentID := tb.inner.AnsID + + postIdentityEvent(t, tb, + identityInner(identityevent.TypeIdentityVerified, "2026-06-10T10:00:00Z", nil, "did:web:identity.acme-corp.com#key-1")) + postIdentityEvent(t, tb, + identityInner(identityevent.TypeIdentityLinked, "2026-06-10T11:00:00Z", []string{agentID}, "")) + + var badge badgeView + if code := getJSON(t, tb, "/v1/agents/"+agentID, &badge); code != http.StatusOK { + t.Fatalf("badge: %d", code) + } + if len(badge.Identities) != 1 { + t.Fatalf("pre-revoke identities: %+v", badge.Identities) + } + + // Terminal agent event → the agent leg of the predicate fails. + revoked := tb.inner + revoked.EventType = "AGENT_REVOKED" + revoked.IssuedAt = "2026-06-10T12:00:00Z" + revBody := []byte(mustJSON(t, revoked)) + tb.postEvent(t, revBody, tb.signWithProducer(t, revBody)) + + // Fresh struct: omitted fields (identities, identitiesTotal) must + // not inherit the pre-revoke read's values. + var afterBadge badgeView + if code := getJSON(t, tb, "/v1/agents/"+agentID, &afterBadge); code != http.StatusOK { + t.Fatalf("badge after agent revoke: %d", code) + } + if afterBadge.Status != "REVOKED" || len(afterBadge.Identities) != 0 || afterBadge.IdentitiesTotal != 0 { + t.Fatalf("terminal agent must show no identities: status=%s %+v total=%d", + afterBadge.Status, afterBadge.Identities, afterBadge.IdentitiesTotal) + } + // Reverse join drops the terminal agent too. + var agentsResp struct { + Agents []json.RawMessage `json:"agents"` + } + if code := getJSON(t, tb, "/v1/identities/"+testIdentityID+"/agents", &agentsResp); code != http.StatusOK { + t.Fatalf("reverse join after agent revoke: %d", code) + } + if len(agentsResp.Agents) != 0 { + t.Fatalf("terminal agent must drop from the reverse join: %+v", agentsResp.Agents) + } + // The identity itself is untouched (the who survives the what). + var idBadge badgeView + if code := getJSON(t, tb, "/v1/identities/"+testIdentityID, &idBadge); code != http.StatusOK { + t.Fatalf("identity badge: %d", code) + } + if idBadge.Status != "VERIFIED" || len(idBadge.Keys) != 1 { + t.Fatalf("identity must stay VERIFIED with keys: %+v", idBadge) + } + // History keeps the link evidence. + var audit auditView + if code := getJSON(t, tb, "/v1/agents/"+agentID+"/identities/history", &audit); code != http.StatusOK { + t.Fatalf("history: %d", code) + } + if len(audit.Records) != 1 { + t.Fatalf("history must keep the link: %+v", audit.Records) + } +} diff --git a/internal/tl/handler/paginate_internal_test.go b/internal/tl/handler/paginate_internal_test.go new file mode 100644 index 0000000..9bad2f8 --- /dev/null +++ b/internal/tl/handler/paginate_internal_test.go @@ -0,0 +1,26 @@ +package handler + +import "testing" + +// paginateIdentityViews backs both computed-view routes; pin its +// edges (offset past the end, zero limit = all, limit clamps). +func TestPaginateIdentityViews(t *testing.T) { + t.Parallel() + views := []int{1, 2, 3, 4, 5} + + if got := paginateIdentityViews(views, 0, 0); len(got) != 5 { + t.Fatalf("no bounds: %v", got) + } + if got := paginateIdentityViews(views, 2, 0); len(got) != 2 || got[0] != 1 { + t.Fatalf("limit only: %v", got) + } + if got := paginateIdentityViews(views, 2, 4); len(got) != 1 || got[0] != 5 { + t.Fatalf("tail page: %v", got) + } + if got := paginateIdentityViews(views, 2, 9); len(got) != 0 { + t.Fatalf("offset past end: %v", got) + } + if got := paginateIdentityViews(views, 99, 1); len(got) != 4 { + t.Fatalf("oversized limit: %v", got) + } +} diff --git a/internal/tl/service/badge.go b/internal/tl/service/badge.go index 6d6e331..39ff897 100644 --- a/internal/tl/service/badge.go +++ b/internal/tl/service/badge.go @@ -2,6 +2,7 @@ package service import ( "context" + "encoding/json" "errors" "time" @@ -51,6 +52,28 @@ type TransparencyLog struct { // single-purpose. Covered by the TL's response signature, not by // any seal: link facts live on the identity stream. Identities []*LinkedIdentityView `json:"identities,omitempty"` + + // IdentitiesTotal counts every visible link when Identities is + // present — the badge embeds at most a small safety cap of + // entries (§5.6.1); the standalone, paginated + // /v1/agents/{agentId}/identities route is the overflow target. + IdentitiesTotal int `json:"identitiesTotal,omitempty"` + + // IdentitiesUnavailable is set when the identities join could not + // be computed (design §5.6.3: join failure is explicit, never + // silent) — an absent/empty identities[] always means "no visible + // links", never "the join failed". + IdentitiesUnavailable bool `json:"identitiesUnavailable,omitempty"` + + // Keys / KeysLogID are the identity badge's computed quote of the + // CURRENT proven key set (design §5.6.3 "computed views carry the + // keys"): the verification methods verbatim from the latest + // sealed proof event, with KeysLogID pointing at that seal (fetch + // it for the signedProof evidence). Populated on the identity + // badge only — never on audit entries — and omitted when the + // identity is REVOKED (the keys are no longer attested). + Keys []json.RawMessage `json:"keys,omitempty"` + KeysLogID string `json:"keysLogId,omitempty"` } // BadgeService computes the badge from the latest mirrored event diff --git a/internal/tl/service/identitybadge.go b/internal/tl/service/identitybadge.go index f1816c0..5ff52bb 100644 --- a/internal/tl/service/identitybadge.go +++ b/internal/tl/service/identitybadge.go @@ -6,6 +6,7 @@ import ( "errors" sqlitetl "github.com/godaddy/ans/internal/adapter/store/sqlitetl" + "github.com/godaddy/ans/internal/domain" ) // Identity badge statuses. Identities have a two-state read-time @@ -31,12 +32,21 @@ type LinkedIdentityView struct { Value string `json:"value"` IdentityStatus string `json:"identityStatus"` // VERIFIED | REVOKED — reflects the identity stream NOW - // ProvenKeyIDs names the identity's current proven key set — the - // verification-method ids from the latest proof event - // (post-rotation). The full verbatim verification methods live in - // the sealed event; thumbprints are compute-at-read conveniences - // derivable from that sealed source. - ProvenKeyIDs []string `json:"provenKeyIds,omitempty"` + // Keys quotes the identity's CURRENT proven key set verbatim + // from the latest sealed proof event (IDENTITY_VERIFIED / + // IDENTITY_UPDATED) — the verification methods exactly as sealed, + // member-for-member (§5.6.3 "computed views carry the keys"): a + // verifier checks operator signatures from the badge alone, no + // audit walk. Methods only — the signedProof evidence lives in + // the seal at KeysLogID, one hop away. Omitted when the identity + // is REVOKED: the keys are no longer attested (the entry itself + // stays visible with identityStatus REVOKED while the link and + // agent are live). + Keys []json.RawMessage `json:"keys,omitempty"` + + // KeysLogID points at the sealed proof event Keys is quoted from + // — fetch it for the signedProofs / offline evidence. + KeysLogID string `json:"keysLogId,omitempty"` // LinkedAt is the producer timestamp of the sealed // IDENTITY_LINKED event that bound this agent. @@ -45,10 +55,6 @@ type LinkedIdentityView struct { // LinkLogID points at the sealed IDENTITY_LINKED entry on the // identity stream — fetch it for link evidence. LinkLogID string `json:"linkLogId,omitempty"` - - // IdentityLogID points at the latest identity-stream entry — - // fetch it (or the audit) for the identity evidence/history. - IdentityLogID string `json:"identityLogId,omitempty"` } // LinkedAgentView is one entry of the reverse join — the agents an @@ -79,13 +85,60 @@ func NewIdentityBadgeService(log *LogService, badge *BadgeService) *IdentityBadg } // Get returns the TransparencyLog view of an identity's most recent -// event — the identity badge. +// event — the identity badge — plus the computed current attestation +// (§5.6.3 "latest entry ≠ current attestation"): the latest entry may +// be a link/unlink/revocation carrying no key material, so the badge +// quotes the current proven key set verbatim from the latest sealed +// proof event, with keysLogId pointing at that seal. Keys are omitted +// when the identity is REVOKED — no longer attested. func (s *IdentityBadgeService) Get(ctx context.Context, identityID string) (*TransparencyLog, error) { rec, err := s.log.LatestEventByIdentity(ctx, identityID) if err != nil { return nil, err } - return s.buildTransparencyLog(ctx, rec) + tl, err := s.buildTransparencyLog(ctx, rec) + if err != nil { + return nil, err + } + status, err := s.identityStatus(ctx, identityID, rec.EventType) + if err != nil { + return nil, err + } + tl.Status = status + if tl.Status == BadgeVerified { + proof, err := s.log.LatestProofByIdentity(ctx, identityID) + if err != nil { + return nil, err + } + _, _, keys := proofSummary(proof.RawEvent) + tl.Keys = keys + tl.KeysLogID = proof.LogID + } + return tl, nil +} + +// identityStatus derives the identity's read-time status with the +// TERMINAL rule: REVOKED iff ANY IDENTITY_REVOKED exists on the +// stream, not merely when it is the tail. The RA's seal spans a +// network round trip, so a racing operation's event can land after +// the revocation leaf — a late leaf must never flip a revoked +// identity's public answer back to VERIFIED. Sound because no +// legitimate flow appends to a revoked identity's stream (every RA +// write op gates on the REVOKED row; re-registration mints a new +// identityId), so the fast path (latest event already REVOKED) +// covers the common case and the EXISTS query the race. +func (s *IdentityBadgeService) identityStatus(ctx context.Context, identityID, latestEventType string) (BadgeStatus, error) { + if identityStatusFromEventType(latestEventType) == BadgeIdentityRevoked { + return BadgeIdentityRevoked, nil + } + revoked, err := s.log.IdentityRevoked(ctx, identityID) + if err != nil { + return "", err + } + if revoked { + return BadgeIdentityRevoked, nil + } + return BadgeVerified, nil } // Audit returns the identity's full event chain, paginated, in the @@ -147,12 +200,18 @@ func identityStatusFromEventType(eventType string) BadgeStatus { const sqlitetlIdentityRevoked = "IDENTITY_REVOKED" // LinkedIdentitiesForAgent computes the identities[] join for an -// agent badge: every identity whose latest link/unlink fact naming -// this agent is LINKED, decorated with that identity's current -// stream state. Revoked identities stay in the list with -// identityStatus REVOKED — the rotation/revocation visibility on -// every linked badge is the point of the read-time join. -func (s *IdentityBadgeService) LinkedIdentitiesForAgent(ctx context.Context, ansID string) ([]*LinkedIdentityView, error) { +// agent badge under the §5.6.3 visibility predicate — link LINKED ∧ +// agent live — given the agent's already-computed badge status (the +// caller has it; recomputing here would double the Merkle work). +// A terminal agent's view is empty: its links are no longer visible. +// Revoked IDENTITIES, by contrast, stay in the list with +// identityStatus REVOKED and no keys — a verifier must see that the +// who behind a still-linked agent was revoked, not have the fact +// silently vanish (the attestation rule withholds only the keys). +func (s *IdentityBadgeService) LinkedIdentitiesForAgent(ctx context.Context, ansID string, agentStatus BadgeStatus) ([]*LinkedIdentityView, error) { + if !agentLive(agentStatus) { + return []*LinkedIdentityView{}, nil + } states, err := s.log.LinkStatesByAgent(ctx, ansID) if err != nil { return nil, err @@ -171,10 +230,24 @@ func (s *IdentityBadgeService) LinkedIdentitiesForAgent(ctx context.Context, ans return out, nil } +// agentLive is the agent conjunct of the §5.6.3 visibility predicate: +// ACTIVE and DEPRECATED are live (a deprecated agent still serves +// during migration); WARNING is live too — it is the ACTIVE overlay +// for an expiring attested cert, not a lifecycle exit. REVOKED, +// EXPIRED, and UNKNOWN are not live. +func agentLive(status BadgeStatus) bool { + switch status { + case BadgeActive, BadgeDeprecated, BadgeWarning: + return true + default: + return false + } +} + // linkedIdentityView decorates one live link with the identity's -// current state: latest event (status + identityLogId), latest proof -// (kind/value/thumbprints), and the sealed link event (linkedAt + -// linkLogId). +// current state: latest event (status), latest proof (kind/value + +// the verbatim keys[] + keysLogId — withheld when REVOKED), and the +// sealed link event (linkedAt + linkLogId). func (s *IdentityBadgeService) linkedIdentityView(ctx context.Context, st *sqlitetl.LinkState) (*LinkedIdentityView, error) { view := &LinkedIdentityView{IdentityID: st.IdentityID} @@ -189,17 +262,23 @@ func (s *IdentityBadgeService) linkedIdentityView(ctx context.Context, st *sqlit if err != nil { return nil, err } - view.IdentityLogID = latest.LogID - view.IdentityStatus = string(identityStatusFromEventType(latest.EventType)) + status, err := s.identityStatus(ctx, st.IdentityID, latest.EventType) + if err != nil { + return nil, err + } + view.IdentityStatus = string(status) proof, err := s.log.LatestProofByIdentity(ctx, st.IdentityID) if err != nil { return nil, err } - kind, value, keyIDs := proofSummary(proof.RawEvent) + kind, value, keys := proofSummary(proof.RawEvent) view.Kind = kind view.Value = value - view.ProvenKeyIDs = keyIDs + if view.IdentityStatus == string(BadgeVerified) { + view.Keys = keys + view.KeysLogID = proof.LogID + } return view, nil } @@ -221,13 +300,26 @@ func (s *IdentityBadgeService) LinkedAgentsForIdentity(ctx context.Context, iden if !st.Linked() { continue } - view := &LinkedAgentView{AnsID: st.AnsID} + // Visibility predicate (§5.6.3): only live agents appear in + // any "current" view — a terminal agent's link history stays + // recoverable from the audit chain. Not-found is a liveness + // answer (the agent lane still seals via the async outbox, so + // a just-activated agent's leaf can lag); any OTHER failure + // propagates — join failure is explicit, never silent. + agentTL, err := s.badge.Get(ctx, st.AnsID) + if err != nil { + if errors.Is(err, domain.ErrNotFound) { + continue + } + return nil, err + } + if !agentLive(agentTL.Status) { + continue + } + view := &LinkedAgentView{AnsID: st.AnsID, AgentStatus: agentTL.Status} if linkRec, err := s.log.EventByLeafIndex(ctx, st.LeafIndex); err == nil { view.LinkedAt = innerTimestamp(linkRec.RawEvent) } - if agentTL, err := s.badge.Get(ctx, st.AnsID); err == nil { - view.AgentStatus = agentTL.Status - } out = append(out, view) } return out, nil @@ -272,11 +364,13 @@ func innerTimestamp(rawEvent string) string { return w.Payload.Producer.Event.Timestamp } -// proofSummary drills kind, value, and the proven verification-method -// ids out of a stored proof event (IDENTITY_VERIFIED / -// IDENTITY_UPDATED). The ids come from the sealed verbatim -// verification methods. -func proofSummary(rawEvent string) (string, string, []string) { +// proofSummary drills kind, value, and the proven verification +// methods out of a stored proof event (IDENTITY_VERIFIED / +// IDENTITY_UPDATED). The methods are returned as the RAW sealed +// bytes, untouched — the computed views quote sealed material +// verbatim, never re-encode it (the seal-verbatim rule extends to +// the quote). The signedProof member stays behind in the seal. +func proofSummary(rawEvent string) (string, string, []json.RawMessage) { var w struct { Payload struct { Producer struct { @@ -284,9 +378,7 @@ func proofSummary(rawEvent string) (string, string, []string) { Kind string `json:"kind"` Value string `json:"value"` Keys []struct { - VerificationMethod struct { - ID string `json:"id"` - } `json:"verificationMethod"` + VerificationMethod json.RawMessage `json:"verificationMethod"` } `json:"keys"` } `json:"event"` } `json:"producer"` @@ -296,11 +388,11 @@ func proofSummary(rawEvent string) (string, string, []string) { return "", "", nil } ev := w.Payload.Producer.Event - ids := make([]string, 0, len(ev.Keys)) + methods := make([]json.RawMessage, 0, len(ev.Keys)) for _, k := range ev.Keys { - if k.VerificationMethod.ID != "" { - ids = append(ids, k.VerificationMethod.ID) + if len(k.VerificationMethod) > 0 { + methods = append(methods, k.VerificationMethod) } } - return ev.Kind, ev.Value, ids + return ev.Kind, ev.Value, methods } diff --git a/internal/tl/service/log.go b/internal/tl/service/log.go index 3b95e55..b973954 100644 --- a/internal/tl/service/log.go +++ b/internal/tl/service/log.go @@ -311,6 +311,13 @@ func (s *LogService) LatestProofByIdentity(ctx context.Context, identityID strin return s.events.GetLatestProofByIdentityID(ctx, identityID) } +// IdentityRevoked reports whether the identity's stream contains an +// IDENTITY_REVOKED event — the terminal read-time rule (§5.6.3): +// once revoked, no later leaf changes the answer. +func (s *LogService) IdentityRevoked(ctx context.Context, identityID string) (bool, error) { + return s.events.HasIdentityRevoked(ctx, identityID) +} + // LinkStatesByAgent returns the latest link/unlink fact per identity // that ever named this agent. func (s *LogService) LinkStatesByAgent(ctx context.Context, ansID string) ([]*sqlitetl.LinkState, error) { diff --git a/spec/api-spec-tl-v2.yaml b/spec/api-spec-tl-v2.yaml index fa98ebc..4664a78 100644 --- a/spec/api-spec-tl-v2.yaml +++ b/spec/api-spec-tl-v2.yaml @@ -332,11 +332,21 @@ paths: summary: Identity badge description: | The latest sealed identity event + inclusion proof + computed - status (`VERIFIED` | `REVOKED`). The proof events seal every - proven key self-verifyingly: any third party reads the key - out of the sealed verbatim `verificationMethod`, verifies the - sealed `signedProof` against it, and confirms the payload - binds this identityId — offline, without trusting the RA. + `{status, keys[], keysLogId}`: the latest entry may be a + link/unlink/revocation carrying no key material, so the badge + quotes the CURRENT proven key set verbatim from the latest + sealed proof event, with `keysLogId` pointing at that seal + (omitted when REVOKED — the keys are no longer attested). + + Third-party verification is byte-recompute, not + parse-and-inspect: verify the sealed `signedProof` JWS + against the key inside the sealed `verificationMethod`; take + the nonce from the decoded payload; reconstruct the + IdentityProofInput from the sealed event plus that nonce; + JCS-serialize; and require the JWS payload segment to equal + those bytes verbatim. Verifiers MUST reject a JWS bearing + unrecognized `crit` headers (RFC 7515 §4.1.11). All offline, + without trusting the RA. operationId: getIdentityBadge parameters: - $ref: '#/components/parameters/IdentityIdPath' @@ -425,13 +435,23 @@ paths: tags: [Transparency Log] summary: Agents currently linked to the identity (reverse join) description: | - Computed at query time from the link events' agent index: - every agent whose latest link/unlink fact naming it is - LINKED, each decorated with its own computed badge status so - a reader checks both ends of the link in one response. + Computed at query time from the link events' agent index + under the visibility predicate: every agent whose latest + link/unlink fact naming it is LINKED and whose own computed + status is live, decorated with that status. Paginated — the + reverse join is the genuinely unbounded read (an identity + links to unlimited agents). operationId: getIdentityAgents parameters: - $ref: '#/components/parameters/IdentityIdPath' + - name: limit + in: query + required: false + schema: { type: integer, default: 50, minimum: 1, maximum: 200 } + - name: offset + in: query + required: false + schema: { type: integer, default: 0, minimum: 0 } responses: '200': description: Currently-linked agents. @@ -439,12 +459,15 @@ paths: application/json: schema: type: object - required: [agents] + required: [agents, total] properties: agents: type: array items: $ref: '#/components/schemas/LinkedAgentView' + total: + type: integer + description: Full visible-link count across all pages '404': description: No events for this identity. content: @@ -458,11 +481,22 @@ paths: summary: Identities currently linked to the agent (computed) description: | The same entries as the agent badge's `identities[]` field, - served alone. Computed at read time — link live ∧ identity - stream state — never stored on, or sealed into, the agent. + served alone and paginated — the overflow target for the + badge's embedded safety cap. Computed at read time under the + visibility predicate (link LINKED ∧ agent live; a REVOKED + identity stays visible with no keys) — never stored on, or + sealed into, the agent. operationId: getAgentIdentities parameters: - $ref: '#/components/parameters/AgentIdPath' + - name: limit + in: query + required: false + schema: { type: integer, default: 50, minimum: 1, maximum: 200 } + - name: offset + in: query + required: false + schema: { type: integer, default: 0, minimum: 0 } responses: '200': description: Currently-linked identities. @@ -470,12 +504,15 @@ paths: application/json: schema: type: object - required: [identities] + required: [identities, total] properties: identities: type: array items: $ref: '#/components/schemas/LinkedIdentityView' + total: + type: integer + description: Full visible-link count across all pages /v1/agents/{agentId}/identities/history: get: @@ -1030,12 +1067,49 @@ components: type: array description: | Agent badges only — the COMPUTED read-time join of the - agent's currently-linked verified identities. Covered by - the TL's response signature, never by any seal; identity - rotation/revocation is visible here immediately with - zero agent-stream writes. + agent's currently-linked verified identities, under the + visibility predicate (link LINKED AND agent live). A + REVOKED identity stays visible with identityStatus + REVOKED and no keys[] — a verifier must see the who was + revoked. Covered by the TL's response signature, never by + any seal; identity rotation/revocation is visible here + immediately with zero agent-stream writes. Embeds at most + a small safety cap of entries; identitiesTotal carries + the full count and /v1/agents/{agentId}/identities is the + paginated overflow target. items: $ref: '#/components/schemas/LinkedIdentityView' + identitiesTotal: + type: integer + description: | + Agent badges only — the full count of visible links when + identities[] is present (the embedded array is capped). + identitiesUnavailable: + type: boolean + description: | + Agent badges only — set when the identities join could + not be computed. Join failure is explicit, never silent: + an absent or empty identities[] always means "no visible + links", never "the join failed". + keys: + type: array + description: | + Identity badges only — the CURRENT proven key set quoted + VERBATIM from the latest sealed proof event + (IDENTITY_VERIFIED / IDENTITY_UPDATED): the latest stream + entry may be a link/unlink/revocation carrying no key + material, so the badge answers "what are the attested + keys" directly. Verification methods only — the + signedProof evidence lives in the seal at keysLogId, one + hop away. Omitted when the identity is REVOKED. + items: + type: object + description: A sealed verification method, member-for-member verbatim + keysLogId: + type: string + description: | + Identity badges only — the sealed proof event keys[] is + quoted from. Omitted when the identity is REVOKED. TransparencyLogAudit: type: object @@ -1151,7 +1225,12 @@ components: LinkedIdentityView: type: object - description: One computed identities[] entry on the agent badge. + description: | + One computed identities[] entry on the agent badge, under the + visibility predicate: the entry appears while the link is + LINKED and the agent is live; identityStatus reports the + identity's current stream state (a REVOKED identity stays + visible — the attestation rule withholds only its keys). required: [identityId, kind, value, identityStatus] properties: identityId: { type: string } @@ -1161,20 +1240,26 @@ components: type: string enum: [VERIFIED, REVOKED] description: Reflects the identity stream NOW - provenKeyIds: + keys: type: array description: | - Verification-method ids of the current proven key set - (post-rotation). The full verbatim methods live in the - sealed proof event. - items: { type: string } + The CURRENT proven key set quoted VERBATIM from the + latest sealed proof event — verification methods only, + member-for-member as sealed, so a verifier checks + operator signatures from the badge alone (no audit + walk). The signedProof evidence lives in the seal at + keysLogId. Omitted when identityStatus is REVOKED: the + keys are no longer attested. + items: + type: object + description: A sealed verification method, verbatim + keysLogId: + type: string + description: The sealed proof event keys[] is quoted from — fetch for signedProofs / offline evidence. Omitted when REVOKED. linkedAt: { type: string, format: date-time } linkLogId: type: string description: The sealed IDENTITY_LINKED entry on the identity stream — fetch for link evidence - identityLogId: - type: string - description: Latest identity-stream entry — fetch for the identity evidence/history LinkedAgentView: type: object @@ -1185,8 +1270,13 @@ components: linkedAt: { type: string, format: date-time } agentStatus: type: string - enum: [ACTIVE, REVOKED, DEPRECATED, EXPIRED, WARNING] - description: The linked agent's own computed badge status — a link is effective only while both ends are live + enum: [ACTIVE, DEPRECATED, WARNING] + description: | + The linked agent's own computed badge status. The reverse + join applies the visibility predicate — only live agents + (ACTIVE / DEPRECATED / WARNING) appear; terminal agents' + links drop out of every "current" view (history stays in + the audit chain). # ── Admin: producer keys ─────────────────────────────────── ProducerKeyRequest: From a599ea8ac23c3958953f3281837c0b669b588606 Mon Sep 17 00:00:00 2001 From: Connor Snitker Date: Thu, 11 Jun 2026 18:29:31 -0500 Subject: [PATCH 07/12] feat(demo): assert seal-before-success, liveness gate, verbatim keys views - TL reads after identity operations no longer poll for records: the 200 already guarantees the seal (assert_tl_identity_audit fails immediately on a missing record as a seal-before-success regression; only Merkle-proof coverage may briefly wait on the checkpoint cadence). - New negative step: linking a REVOKED agent fails 422 AGENT_NOT_LINKABLE all-or-nothing and seals nothing. - Badge asserts move to the new shape: verbatim keys[] lengths + keysLogId presence, rotation visible as the quoted set flipping 2 -> 1, and the revoked did:web staying VISIBLE on the still-linked agent with identityStatus REVOKED and no quoted keys. - Agents' TL presence is polled right after activation: the AGENT lane still seals via the async outbox (flagged in the design as a bug to fix separately), and the identity joins need the AGENT_REGISTERED leaves present. Signed-off-by: Connor Snitker --- scripts/demo/common.sh | 39 ++++++++++++------ scripts/demo/identity-lifecycle.sh | 66 ++++++++++++++++++++++-------- scripts/demo/run-lifecycle.sh | 4 +- 3 files changed, 79 insertions(+), 30 deletions(-) diff --git a/scripts/demo/common.sh b/scripts/demo/common.sh index 63258cb..eb6208e 100755 --- a/scripts/demo/common.sh +++ b/scripts/demo/common.sh @@ -317,27 +317,40 @@ poll_tl_audit() { fail "TL not ready for $agent_id in ${timeout}s (records=${count:-0}, with merkle proof=${withproof:-0}; want $expected of each)" } -# poll_tl_identity_audit IDENTITY_ID EXPECTED_COUNT [TIMEOUT_SECONDS] +# assert_tl_identity_audit IDENTITY_ID EXPECTED_COUNT [TIMEOUT_SECONDS] # -# Sibling of poll_tl_audit for the identity stream: polls -# /v1/identities/{identityId}/audit until it shows at least -# EXPECTED_COUNT events with Merkle proofs. -poll_tl_identity_audit() { - local identity_id="$1" expected="$2" timeout="${3:-30}" - local i=0 count withproof +# Sibling of poll_tl_audit for the identity stream — with ONE crucial +# difference: the RECORD COUNT is asserted on the very first read, no +# polling. Identity operations are seal-before-success (design +# §5.6.1): the RA reports success only after the TL acknowledges the +# seal, so the events MUST already be in the log the moment the API +# returned — a missing record here is a seal-before-success +# regression, not a timing flake. Merkle INCLUSION PROOFS are the one +# thing allowed to lag: proofs are built against the latest published +# checkpoint, and checkpoint publication is cadence-based (one root, +# one cadence — unchanged by this design), so proof coverage alone is +# polled briefly. +assert_tl_identity_audit() { + local identity_id="$1" expected="$2" timeout="${3:-15}" + local resp count withproof + resp=$(curl -sSf -H "Authorization: Bearer $TL_API_KEY" \ + "$TL_URL/v1/identities/$identity_id/audit" 2>/dev/null || true) + count=$(printf '%s' "$resp" | jq -r '(.records | length) // 0') + if [ "${count:-0}" -lt "$expected" ]; then + fail "seal-before-success violated for identity $identity_id: audit shows ${count:-0} records immediately after the API returned; want $expected" + fi + local i=0 while [ "$i" -lt "$timeout" ]; do - local resp - resp=$(curl -sSf -H "Authorization: Bearer $TL_API_KEY" \ - "$TL_URL/v1/identities/$identity_id/audit" 2>/dev/null || true) - count=$(printf '%s' "$resp" | jq -r '(.records | length) // 0') withproof=$(printf '%s' "$resp" | jq -r '[.records[]? | select(.merkleProof)] | length // 0') - if [ "${count:-0}" -ge "$expected" ] && [ "${withproof:-0}" -ge "$expected" ]; then + if [ "${withproof:-0}" -ge "$expected" ]; then return 0 fi sleep 1 i=$((i + 1)) + resp=$(curl -sSf -H "Authorization: Bearer $TL_API_KEY" \ + "$TL_URL/v1/identities/$identity_id/audit" 2>/dev/null || true) done - fail "TL not ready for identity $identity_id in ${timeout}s (records=${count:-0}, with merkle proof=${withproof:-0}; want $expected of each)" + fail "checkpoint never covered identity $identity_id's leaves in ${timeout}s (records=${count}, with proof=${withproof:-0}; want $expected)" } # wait_ready URL [TIMEOUT_SECONDS] diff --git a/scripts/demo/identity-lifecycle.sh b/scripts/demo/identity-lifecycle.sh index faf01d2..cce0b86 100755 --- a/scripts/demo/identity-lifecycle.sh +++ b/scripts/demo/identity-lifecycle.sh @@ -31,9 +31,17 @@ # 17. TL /v1/identities/{id}/receipt SCITT COSE receipt for the identity leaf # 18. DELETE .../links/{agentId} unlink the did:web from agent #1 ONLY → agent #1 # keeps its did:key link; agent #2 keeps the did:web +# 18b. POST .../links → 422 AGENT_NOT_LINKABLE liveness gate: a REVOKED agent rejects the whole +# batch and seals NOTHING # 19. POST /v2/ans/identities/{id}/revoke revoke the did:web → IDENTITY_REVOKED (one event) # 20. TL did:web badge REVOKED — agent-2's join shows it at the next read; agent-1's did:key -# stays VERIFIED; both agents stay ACTIVE (the whats survive the who) +# stays VERIFIED with no keys quoted; both agents stay ACTIVE (the whats survive the who) +# +# Every sealing operation above is SEAL-BEFORE-SUCCESS (§5.6.1): the +# RA reports success only after the TL acknowledges the seal, so the +# TL reads in this script run immediately after each call — no +# polling anywhere. Badge views quote the current proven keys[] +# VERBATIM from the latest sealed proof event (+ keysLogId to it). # # The demo runs against the noop did:web resolver (the default — # `identity.resolver.type: noop`): signature verification is real, @@ -194,6 +202,13 @@ ok "did:key identity VERIFIED — the keyless-future test track" header "8. Register TWO fresh agents to ACTIVE (the fleet to link)" AGENT_1=$(register_agent "linked-a-$(openssl rand -hex 4).example.com") AGENT_2=$(register_agent "linked-b-$(openssl rand -hex 4).example.com") +# The AGENT lane still seals via the async outbox (flagged 2026-06-11 +# as a bug in the design doc §5.6.1 — agents should also wait for seal +# confirmation; tracked separately). Until that lands, wait for both +# agents' TL streams here: the identity-side reads below join against +# agent TL status, which needs the AGENT_REGISTERED leaves present. +poll_tl_audit "$AGENT_1" 1 30 +poll_tl_audit "$AGENT_2" 1 30 ok "fleet ready: $AGENT_1 + $AGENT_2" # ----- 9. Link the fleet — one owner-gated call per identity ----- @@ -216,9 +231,9 @@ header "10. GET /v2/ans/agents/$AGENT_1 (RA detail — computed identities[], n curl_json GET "/v2/ans/agents/$AGENT_1" >/dev/null # ----- 11. TL identity stream ----- -header "11. Wait for the outbox worker, then read the identity stream from the TL" -poll_tl_identity_audit "$IDENTITY_ID" 2 30 -ok "TL sealed the IDENTITY_VERIFIED + IDENTITY_LINKED leaves" +header "11. Read the identity stream from the TL — NO polling: identity ops are seal-before-success (§5.6.1)" +assert_tl_identity_audit "$IDENTITY_ID" 2 +ok "IDENTITY_VERIFIED + IDENTITY_LINKED were already sealed when the API calls returned" header "11a. TL: GET /v1/identities/$IDENTITY_ID (identity badge — latest sealed event + proof + status)" curl_tl GET "/v1/identities/$IDENTITY_ID" >/dev/null @@ -241,7 +256,7 @@ AGENTS_COUNT=$(printf '%s' "$AGENTS_VIEW" | jq -r '.agents | length') [ "$AGENTS_COUNT" = "2" ] || fail "reverse join should list 2 agents, got $AGENTS_COUNT" header "11d. TL: GET /v1/identities/$DK_ID (did:key badge — the sealed Multikey verification method)" -poll_tl_identity_audit "$DK_ID" 2 30 +assert_tl_identity_audit "$DK_ID" 2 DK_BADGE_VM=$(curl_tl GET "/v1/identities/$DK_ID/audit" | \ jq -r '[.records[].payload.producer.event | select(.keys)][0].keys[0].verificationMethod') DK_VM_TYPE=$(printf '%s' "$DK_BADGE_VM" | jq -r '.type // empty') @@ -251,14 +266,14 @@ DK_VM_MB=$(printf '%s' "$DK_BADGE_VM" | jq -r '.publicKeyMultibase // empty') ok "did:key sealed verbatim: type=Multikey, publicKeyMultibase = the did:key msid itself" # ----- 12-13. Agent-side computed views (both badges) ----- -poll_tl_audit "$AGENT_1" 1 30 -poll_tl_audit "$AGENT_2" 1 30 header "12. TL: GET /v1/agents/{both} (agent-1 carries BOTH whos; agent-2 carries the did:web)" BADGE_1=$(curl_tl GET "/v1/agents/$AGENT_1") IDS_1=$(printf '%s' "$BADGE_1" | jq -r '.identities | length') [ "$IDS_1" = "2" ] || fail "agent-1 badge should show 2 identities, got $IDS_1" -KEYIDS_1=$(printf '%s' "$BADGE_1" | jq -r '.identities[] | select(.kind == "did:web") | .provenKeyIds | length') -[ "$KEYIDS_1" = "2" ] || fail "agent-1's did:web entry should show 2 provenKeyIds, got $KEYIDS_1" +KEYS_1=$(printf '%s' "$BADGE_1" | jq -r '.identities[] | select(.kind == "did:web") | .keys | length') +[ "$KEYS_1" = "2" ] || fail "agent-1's did:web entry should quote 2 verbatim keys, got $KEYS_1" +KEYSLOG_1=$(printf '%s' "$BADGE_1" | jq -r '.identities[] | select(.kind == "did:web") | .keysLogId // empty') +[ -n "$KEYSLOG_1" ] || fail "agent-1's did:web entry is missing keysLogId (the seal the keys are quoted from)" DK_ON_1=$(printf '%s' "$BADGE_1" | jq -r '.identities[] | select(.kind == "did:key") | .value // empty') [ "$DK_ON_1" = "$DID_KEY" ] || fail "agent-1's did:key entry missing" BADGE_2=$(curl_tl GET "/v1/agents/$AGENT_2") @@ -269,7 +284,7 @@ WHO_2=$(printf '%s' "$BADGE_2" | jq -r '.identities[0].value // empty') SEALED_X_BEFORE=$(printf '%s' "$AUDIT" | \ jq -r '[.records[].payload.producer.event | select(.keys)][0].keys[0].verificationMethod.publicKeyJwk.x // empty') [ -n "$SEALED_X_BEFORE" ] || fail "sealed proof event is missing the verbatim verification method" -ok "agent-1: did:web (provenKeyIds[2]) + did:key (Ed25519) side by side; agent-2: $WHO_2" +ok "agent-1: did:web (keys[2] quoted verbatim + keysLogId) + did:key (Ed25519) side by side; agent-2: $WHO_2" header "13. TL: GET /v1/agents/$AGENT_1/identities + /identities/history" curl_tl GET "/v1/agents/$AGENT_1/identities" >/dev/null @@ -290,7 +305,7 @@ curl_json POST "/v2/ans/identities/$IDENTITY_ID/verify-control" \ assert_2xx "rotation verify-control" header "16. TL: the rotation sealed ONE IDENTITY_UPDATED — proven set 2 keys → 1, key material changed" -poll_tl_identity_audit "$IDENTITY_ID" 3 30 +assert_tl_identity_audit "$IDENTITY_ID" 3 AUDIT=$(curl_tl GET "/v1/identities/$IDENTITY_ID/audit") SEALED_X_AFTER=$(printf '%s' "$AUDIT" | \ jq -r '[.records[].payload.producer.event | select(.keys)][0].keys[0].verificationMethod.publicKeyJwk.x // empty') @@ -301,9 +316,9 @@ SEALED_KEYS_AFTER=$(printf '%s' "$AUDIT" | \ [ "$SEALED_KEYS_AFTER" = "1" ] || fail "rotated proven set should be 1 key, got $SEALED_KEYS_AFTER" # Both linked badges reflect it immediately (read-time join) — with # 10,000 linked agents this would still be ONE sealed event. -KEYIDS_1_AFTER=$(curl_tl GET "/v1/agents/$AGENT_1" | \ - jq -r '.identities[] | select(.kind == "did:web") | .provenKeyIds | length') -[ "$KEYIDS_1_AFTER" = "1" ] || fail "agent-1's did:web entry should now show 1 provenKeyId, got $KEYIDS_1_AFTER" +KEYS_1_AFTER=$(curl_tl GET "/v1/agents/$AGENT_1" | \ + jq -r '.identities[] | select(.kind == "did:web") | .keys | length') +[ "$KEYS_1_AFTER" = "1" ] || fail "agent-1's did:web entry should now quote 1 key, got $KEYS_1_AFTER" ok "sealed key material flipped (${SEALED_X_BEFORE:0:12}… → ${SEALED_X_AFTER:0:12}…), set 2→1 — ONE event, zero agent-stream writes" # ----- 17. Identity receipt ----- @@ -323,7 +338,7 @@ fi header "18. DELETE /v2/ans/identities/$IDENTITY_ID/links/$AGENT_1 (unlink the did:web from agent #1 only)" curl_json DELETE "/v2/ans/identities/$IDENTITY_ID/links/$AGENT_1" >/dev/null assert_2xx "unlink" -poll_tl_identity_audit "$IDENTITY_ID" 4 30 +assert_tl_identity_audit "$IDENTITY_ID" 4 BADGE_1=$(curl_tl GET "/v1/agents/$AGENT_1") REMAINING_1=$(printf '%s' "$BADGE_1" | jq -r '(.identities | length) // 0') KIND_1=$(printf '%s' "$BADGE_1" | jq -r '.identities[0].kind // empty') @@ -334,11 +349,26 @@ REMAINING_2=$(curl_tl GET "/v1/agents/$AGENT_2" | jq -r '(.identities | length) ok "the did:web↔agent-1 pair ended; agent-1's did:key link and agent-2's did:web link are untouched" curl_tl GET "/v1/agents/$AGENT_1/identities/history" >/dev/null +# ----- 18b. Link liveness gate — terminal agents are not linkable ----- +header "18b. Liveness gate: linking to a REVOKED agent fails 422 AGENT_NOT_LINKABLE (nothing seals)" +AGENT_3=$(register_agent "dead-$(openssl rand -hex 3).example.com") +curl_json POST "/v2/ans/agents/$AGENT_3/revoke" \ + "$(jq -n '{reason: "CESSATION_OF_OPERATION", comments: "liveness-gate demo"}')" >/dev/null +assert_2xx "revoke agent-3" +GATE_RESP=$(curl_json POST "/v2/ans/identities/$DK_ID/links" \ + "$(jq -n --arg a "$AGENT_3" '{agentIds: [$a]}')" || true) +GATE_CODE=$(printf '%s' "$GATE_RESP" | jq -r '.code // empty') +[ "$GATE_CODE" = "AGENT_NOT_LINKABLE" ] || \ + fail "linking a revoked agent must fail AGENT_NOT_LINKABLE, got: $GATE_RESP" +# Nothing sealed: the did:key stream still has exactly its 2 events. +assert_tl_identity_audit "$DK_ID" 2 +ok "terminal agent rejected all-or-nothing; the did:key stream sealed nothing" + # ----- 19-20. Revoke — the who dies, the whats survive ----- header "19. POST /v2/ans/identities/$IDENTITY_ID/revoke (state change — an identity cannot be deleted)" curl_json POST "/v2/ans/identities/$IDENTITY_ID/revoke" >/dev/null assert_2xx "revoke" -poll_tl_identity_audit "$IDENTITY_ID" 5 30 +assert_tl_identity_audit "$IDENTITY_ID" 5 header "20. TL: did:web REVOKED; agent-2's join shows it; agent-1's did:key stays VERIFIED" ID_STATUS=$(curl_tl GET "/v1/identities/$IDENTITY_ID" | jq -r '.status') @@ -347,6 +377,10 @@ BADGE_2=$(curl_tl GET "/v1/agents/$AGENT_2") WHO_STATUS_2=$(printf '%s' "$BADGE_2" | jq -r '.identities[0].identityStatus // empty') AGENT_STATUS_2=$(printf '%s' "$BADGE_2" | jq -r '.status') [ "$WHO_STATUS_2" = "REVOKED" ] || fail "agent-2's identities[] should show REVOKED, got $WHO_STATUS_2" +# Visibility ≠ attestation: the revoked entry stays on the badge — +# a verifier must SEE the who was revoked — but its keys[] are gone. +REVOKED_KEYS_2=$(printf '%s' "$BADGE_2" | jq -r '(.identities[0].keys | length) // 0') +[ "$REVOKED_KEYS_2" = "0" ] || fail "revoked identity must quote no keys, got $REVOKED_KEYS_2" [ "$AGENT_STATUS_2" = "ACTIVE" ] || fail "agent-2 status=$AGENT_STATUS_2, want ACTIVE — identity ops must never touch the agent" # Each identity has its own lifecycle: the did:key on agent-1 is # unaffected by the did:web's revocation. diff --git a/scripts/demo/run-lifecycle.sh b/scripts/demo/run-lifecycle.sh index 355a6df..67b4a2a 100755 --- a/scripts/demo/run-lifecycle.sh +++ b/scripts/demo/run-lifecycle.sh @@ -351,7 +351,9 @@ curl_json POST "/v2/ans/identities/$IDENTITY_ID/links" \ assert_2xx "identity link" header "19. TL: GET /v1/agents/$AGENT_ID (badge — computed identities[] join)" -poll_tl_identity_audit "$IDENTITY_ID" 2 30 +# No polling: identity ops are seal-before-success — the seals are +# already in the log the moment the link call returned. +assert_tl_identity_audit "$IDENTITY_ID" 2 BADGE_WITH_WHO=$(curl_tl GET "/v1/agents/$AGENT_ID") WHO_VALUE=$(printf '%s' "$BADGE_WITH_WHO" | jq -r '.identities[0].value // empty') [ "$WHO_VALUE" = "$IDENTITY_DID" ] || fail "badge identities[] join missing (got: $WHO_VALUE)" From 551738f9faa5b535faa8a1ba5765da30db3cdd23 Mon Sep 17 00:00:00 2001 From: Connor Snitker Date: Mon, 15 Jun 2026 14:15:12 -0500 Subject: [PATCH 08/12] fix(tl): serve /v1/identities anonymously; drop reverse-join proof waste MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Addresses two PR #41 review blockers on the TL read surface. - /v1/identities/* was absent from both the static and OIDC anonymous-path allowlists, so under the shipped public-read default a third-party verifier resolving the who behind an agent got 401 — the feature's own offline-evidence hop was unreachable. Add /v1/identities/ to both blocks. A new package-main table test drives the real buildAuth wiring (handler tests mount a bare router with no auth, which is why nothing caught this), asserting the public-read families are reachable without a credential while /v1/internal/* still requires the bearer key. - LinkedAgentsForIdentity called badge.Get per linked agent, which builds a full Merkle inclusion proof (checkpoint read + tile walk) that the reverse-join view discards. Add BadgeService.StatusOf — latest-event read plus the cheap envelope parse, no proof — and use it for the agent-liveness conjunct. Pagination stays at the handler because the liveness filter needs each agent's latest event (absent from the link index), so a SQL LIMIT couldn't preserve an accurate total or full pages; the per-agent cost is now one indexed read instead of a proof build. Signed-off-by: Connor Snitker --- cmd/ans-tl/main.go | 2 + cmd/ans-tl/main_test.go | 65 ++++++++++++++++++++++++++++ internal/tl/service/badge.go | 18 ++++++++ internal/tl/service/identitybadge.go | 15 +++++-- 4 files changed, 97 insertions(+), 3 deletions(-) create mode 100644 cmd/ans-tl/main_test.go diff --git a/cmd/ans-tl/main.go b/cmd/ans-tl/main.go index 3a202bc..15eb59a 100644 --- a/cmd/ans-tl/main.go +++ b/cmd/ans-tl/main.go @@ -401,6 +401,7 @@ func buildAuth(ctx context.Context, cfg *config.TLConfig) (authMiddlewareProvide // covered here — that route requires the Bearer key. opts = append(opts, auth.WithAnonymousPath("/v1/agents/"), + auth.WithAnonymousPath("/v1/identities/"), auth.WithAnonymousPath("/v1/log/"), auth.WithAnonymousPath("/checkpoint"), auth.WithAnonymousPath("/root-keys"), @@ -421,6 +422,7 @@ func buildAuth(ctx context.Context, cfg *config.TLConfig) (authMiddlewareProvide auth.WithOIDCAnonymousPath("/v2/admin/ready"), auth.WithOIDCAnonymousPath("/docs"), auth.WithOIDCAnonymousPath("/v1/agents/"), + auth.WithOIDCAnonymousPath("/v1/identities/"), auth.WithOIDCAnonymousPath("/v1/log/"), auth.WithOIDCAnonymousPath("/checkpoint"), auth.WithOIDCAnonymousPath("/root-keys"), diff --git a/cmd/ans-tl/main_test.go b/cmd/ans-tl/main_test.go new file mode 100644 index 0000000..abd25bb --- /dev/null +++ b/cmd/ans-tl/main_test.go @@ -0,0 +1,65 @@ +package main + +import ( + "context" + "net/http" + "net/http/httptest" + "testing" + + "github.com/godaddy/ans/internal/config" +) + +// TestBuildAuth_PublicReadAnonymousPaths pins the TL's anonymous-path +// wiring through the REAL provider construction (buildAuth), not a +// bare router. Regression guard for the verified-identity read +// surface: under the shipped public-read default, a third-party +// verifier must reach /v1/identities/* with no credential — the +// feature's offline-evidence hop — while producer ingest under +// /v1/internal/* still requires the bearer key. Handler tests mount a +// bare chi.Router with no auth, so only a test at this layer catches +// a missing WithAnonymousPath entry. +func TestBuildAuth_PublicReadAnonymousPaths(t *testing.T) { + t.Parallel() + cfg := &config.TLConfig{} + cfg.Auth.Type = "static" + cfg.Auth.PublicRead = true + cfg.Auth.Static = &config.AuthStatic{APIKey: "test-key"} + + provider, err := buildAuth(context.Background(), cfg) + if err != nil { + t.Fatalf("buildAuth: %v", err) + } + // A handler that 200s — so an anonymous pass-through yields 200, + // and only the middleware's own 401 distinguishes a gated path. + h := provider.Middleware()(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + w.WriteHeader(http.StatusOK) + })) + + cases := []struct { + path string + anon bool // true = reachable with no Authorization header + comment string + }{ + {"/v1/agents/01HX/badge", true, "agent badge — reference parity"}, + {"/v1/identities/01HX", true, "identity badge — the verifier's who-hop"}, + {"/v1/identities/01HX/audit", true, "identity audit"}, + {"/v1/identities/01HX/receipt", true, "identity receipt"}, + {"/v1/identities/01HX/agents", true, "reverse join"}, + {"/v1/log/checkpoint", true, "log read"}, + {"/checkpoint", true, "raw checkpoint"}, + {"/root-keys", true, "verification keys"}, + {"/tile/0/0", true, "tlog tiles"}, + {"/v1/internal/identities/event", false, "identity ingest — bearer required"}, + {"/v1/internal/agents/event", false, "agent ingest — bearer required"}, + } + for _, tc := range cases { + req := httptest.NewRequest(http.MethodGet, tc.path, nil) // no Authorization header + rec := httptest.NewRecorder() + h.ServeHTTP(rec, req) + anon := rec.Code != http.StatusUnauthorized + if anon != tc.anon { + t.Errorf("%s (%s): anonymous=%v (status %d), want anonymous=%v", + tc.path, tc.comment, anon, rec.Code, tc.anon) + } + } +} diff --git a/internal/tl/service/badge.go b/internal/tl/service/badge.go index 39ff897..f2ece11 100644 --- a/internal/tl/service/badge.go +++ b/internal/tl/service/badge.go @@ -101,6 +101,24 @@ func (s *BadgeService) Get(ctx context.Context, agentID string) (*TransparencyLo return s.buildTransparencyLog(ctx, rec) } +// StatusOf returns only the agent's read-time badge status — the +// latest-event read plus the cheap envelope parse, WITHOUT building +// a Merkle inclusion proof (no checkpoint read, no tile walk). The +// identity reverse join needs the agent-liveness conjunct of the +// §5.6.3 visibility predicate but discards the proof, so calling the +// full Get() per linked agent was O(total) wasted proof builds. +func (s *BadgeService) StatusOf(ctx context.Context, agentID string) (BadgeStatus, error) { + rec, err := s.log.LatestEventByAgent(ctx, agentID) + if err != nil { + return "", err + } + wrapper, err := parseEnvelopeWrapper(rec.RawEvent) + if err != nil { + return "", err + } + return s.statusFromRecord(rec, wrapper.certExpiresAt()), nil +} + // buildTransparencyLog assembles a TransparencyLog from a stored // event record. Used by both Get (latest event) and Audit (each // record in the page). diff --git a/internal/tl/service/identitybadge.go b/internal/tl/service/identitybadge.go index 5ff52bb..96ec558 100644 --- a/internal/tl/service/identitybadge.go +++ b/internal/tl/service/identitybadge.go @@ -306,17 +306,26 @@ func (s *IdentityBadgeService) LinkedAgentsForIdentity(ctx context.Context, iden // answer (the agent lane still seals via the async outbox, so // a just-activated agent's leaf can lag); any OTHER failure // propagates — join failure is explicit, never silent. - agentTL, err := s.badge.Get(ctx, st.AnsID) + // + // StatusOf (not badge.Get): this view carries only + // ansId/linkedAt/agentStatus, so building a Merkle proof per + // agent here would be discarded work. Pagination stays at the + // handler after this walk because the agent-liveness filter + // needs each agent's latest event (not in the link index), so + // a SQL LIMIT couldn't preserve an accurate total or full + // pages; the per-agent cost is now one indexed read, not a + // checkpoint read + tile walk. + agentStatus, err := s.badge.StatusOf(ctx, st.AnsID) if err != nil { if errors.Is(err, domain.ErrNotFound) { continue } return nil, err } - if !agentLive(agentTL.Status) { + if !agentLive(agentStatus) { continue } - view := &LinkedAgentView{AnsID: st.AnsID, AgentStatus: agentTL.Status} + view := &LinkedAgentView{AnsID: st.AnsID, AgentStatus: agentStatus} if linkRec, err := s.log.EventByLeafIndex(ctx, st.LeafIndex); err == nil { view.LinkedAt = innerTimestamp(linkRec.RawEvent) } From 6be307755c7f4ace462083d38668964482091516 Mon Sep 17 00:00:00 2001 From: Connor Snitker Date: Mon, 15 Jun 2026 14:15:25 -0500 Subject: [PATCH 09/12] fix(ra): remove the dead IDENTITY outbox lane MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Identity events seal SYNCHRONOUSLY (seal-before-success, design §5.6.1) and never ride the outbox, yet migration 007 rebuilt outbox_events solely to widen its CHECK to accept 'IDENTITY', and Enqueue accepted that value — dead code that contradicted the stated invariant and invited a future async-seal regression. - Drop the 'IDENTITY' case from OutboxStore.Enqueue (and its doc comment); the agent lanes are V1/V2 only. - Delete migration 007_outbox_identity_lane.sql; the seal-claim migration becomes 007 (renamed from 008). Both migrations are new in this PR, so the history main receives never carried the dead lane. Removing 007 restores the correct ('V1','V2') CHECK that migration 003 set. - tlclient.ingestPathForVersion keeps its 'IDENTITY' arm — the synchronous SealIdentityEvent path uses it. Signed-off-by: Connor Snitker --- ..._claim.sql => 007_identity_seal_claim.sql} | 0 .../migrations/007_outbox_identity_lane.sql | 41 ------------------- internal/adapter/store/sqlite/outbox.go | 13 +++--- 3 files changed, 7 insertions(+), 47 deletions(-) rename internal/adapter/store/sqlite/migrations/{008_identity_seal_claim.sql => 007_identity_seal_claim.sql} (100%) delete mode 100644 internal/adapter/store/sqlite/migrations/007_outbox_identity_lane.sql diff --git a/internal/adapter/store/sqlite/migrations/008_identity_seal_claim.sql b/internal/adapter/store/sqlite/migrations/007_identity_seal_claim.sql similarity index 100% rename from internal/adapter/store/sqlite/migrations/008_identity_seal_claim.sql rename to internal/adapter/store/sqlite/migrations/007_identity_seal_claim.sql diff --git a/internal/adapter/store/sqlite/migrations/007_outbox_identity_lane.sql b/internal/adapter/store/sqlite/migrations/007_outbox_identity_lane.sql deleted file mode 100644 index 670bee0..0000000 --- a/internal/adapter/store/sqlite/migrations/007_outbox_identity_lane.sql +++ /dev/null @@ -1,41 +0,0 @@ --- Third outbox lane: identity events. --- --- The schema_version column routes each outbox row to its TL ingest --- lane ('V1' → /v1/internal/agents/event, 'V2' → --- /v2/internal/agents/event). Identity events ride a third lane, --- 'IDENTITY' → /v1/internal/identities/event — same producer --- signature, same replay-verbatim invariant, different inner-event --- schema (keyed by identityId). --- --- SQLite cannot widen a column CHECK in place, so this rebuilds the --- table with the widened constraint (the standard --- create-copy-drop-rename dance; the migration runner wraps it in one --- transaction). Index is recreated to match 001's definition. -CREATE TABLE outbox_events_new ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - event_type TEXT NOT NULL, - agent_id TEXT NOT NULL, - schema_version TEXT NOT NULL DEFAULT 'V2' - CHECK (schema_version IN ('V1', 'V2', 'IDENTITY')), - payload_json TEXT NOT NULL CHECK (json_valid(payload_json)), - attempts INTEGER NOT NULL DEFAULT 0, - last_error TEXT, - next_attempt_at_ms INTEGER NOT NULL, - sent_at_ms INTEGER, - created_at_ms INTEGER NOT NULL -); - -INSERT INTO outbox_events_new ( - id, event_type, agent_id, schema_version, payload_json, - attempts, last_error, next_attempt_at_ms, sent_at_ms, created_at_ms -) -SELECT id, event_type, agent_id, schema_version, payload_json, - attempts, last_error, next_attempt_at_ms, sent_at_ms, created_at_ms -FROM outbox_events; - -DROP TABLE outbox_events; - -ALTER TABLE outbox_events_new RENAME TO outbox_events; - -CREATE INDEX IF NOT EXISTS idx_outbox_next - ON outbox_events(sent_at_ms, next_attempt_at_ms); diff --git a/internal/adapter/store/sqlite/outbox.go b/internal/adapter/store/sqlite/outbox.go index 6e59de6..ec039f4 100644 --- a/internal/adapter/store/sqlite/outbox.go +++ b/internal/adapter/store/sqlite/outbox.go @@ -43,10 +43,11 @@ func NewOutboxStore(db *DB) *OutboxStore { return &OutboxStore{db: db} } // preceded it — that's how we guarantee at-least-once delivery // without a dual-write window. // -// schemaVersion must be "V1", "V2", or "IDENTITY"; the worker reads -// this value to pick the matching TL ingest lane. An empty value is -// rejected. For identity events the agentID parameter carries the -// identityId — the row's subject, whatever the event family. +// schemaVersion must be "V1" or "V2"; the worker reads this value to +// pick the matching TL ingest lane. An empty value is rejected. +// Identity events do NOT ride the outbox — they seal synchronously +// (design §5.6.1: seal-before-success), so there is no "IDENTITY" +// lane here. func (s *OutboxStore) Enqueue( ctx context.Context, eventType, agentID, schemaVersion string, @@ -57,9 +58,9 @@ func (s *OutboxStore) Enqueue( return 0, errors.New("sqlite/outbox: payload is empty") } switch schemaVersion { - case "V1", "V2", "IDENTITY": + case "V1", "V2": default: - return 0, fmt.Errorf("sqlite/outbox: invalid schemaVersion %q (want V1, V2, or IDENTITY)", schemaVersion) + return 0, fmt.Errorf("sqlite/outbox: invalid schemaVersion %q (want V1 or V2)", schemaVersion) } const q = ` INSERT INTO outbox_events(event_type, agent_id, schema_version, payload_json, From 826ca9673c92e99c60c18d17e17c23c6eb47c2e7 Mon Sep 17 00:00:00 2001 From: Connor Snitker Date: Mon, 15 Jun 2026 14:15:42 -0500 Subject: [PATCH 10/12] fix(ra): observability for the identity seal boundary and did:web resolver MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The synchronous identity lane and the did:web resolver emitted zero logs — including the TL_REJECTED_EVENT path whose own comment says operators must see it — leaving on-call with no signal (the agent lane's outbox worker logs the same failure class loudly). - Inject a zerolog logger into IdentityService (default no-op): ERROR on TL_REJECTED_EVENT, WARN on transient TL_UNAVAILABLE, INFO on seal success (identityId + eventType). The previously swallowed ReleaseChallenge error is logged at WARN — best-effort by contract, but a leaked claim blocks re-verify until its TTL lapses, so make it visible. - Inject a logger into the did:web resolver (default no-op): on a fetch failure, log the category (the underlying err — denylist rejection, DNS-rebind block, TLS, timeout, refused), the DID, and elapsed time SERVER-SIDE; the wire error stays deliberately coarse (no SSRF oracle). Set DisableKeepAlives on the per-call transport so an idle connection to a registrant-steered host doesn't linger. - Wire both loggers from cmd/ans-ra/main.go. Signed-off-by: Connor Snitker --- cmd/ans-ra/main.go | 9 ++++--- internal/adapter/didresolver/web.go | 34 +++++++++++++++++++++-- internal/ra/service/identity.go | 42 +++++++++++++++++++++++++++-- 3 files changed, 77 insertions(+), 8 deletions(-) diff --git a/cmd/ans-ra/main.go b/cmd/ans-ra/main.go index 1464706..324057a 100644 --- a/cmd/ans-ra/main.go +++ b/cmd/ans-ra/main.go @@ -154,7 +154,7 @@ func run(cfgPath string) error { // did:web resolver — noop (quickstart) or hardened web fetch, // the identity surface's analog of the DNS verifier selection. - didResolver := selectDIDResolver(cfg) + didResolver := selectDIDResolver(cfg, logger) logger.Info(). Str("resolver", cfg.Identity.Resolver.Type). Dur("challengeTTL", cfg.Identity.ChallengeTTL). @@ -207,7 +207,8 @@ func run(cfgPath string) error { }).WithChallengeTTL(cfg.Identity.ChallengeTTL). WithRegisterRateLimit(cfg.Identity.RegisterRateLimit). WithLinkRateLimit(cfg.Identity.LinkRateLimit). - WithSealTimeout(cfg.Identity.SealTimeout) + WithSealTimeout(cfg.Identity.SealTimeout). + WithLogger(logger) // HTTP. r := chi.NewRouter() @@ -475,10 +476,10 @@ func selectDNSVerifier(cfg *config.RAConfig) port.DNSVerifier { // the hardened HTTPS fetch (WebPKI + SSRF dialer guards); the default // "noop" synthesizes documents from the submitted proofs' embedded // keys for self-contained local development. -func selectDIDResolver(cfg *config.RAConfig) port.DIDResolver { +func selectDIDResolver(cfg *config.RAConfig, logger zerolog.Logger) port.DIDResolver { switch cfg.Identity.Resolver.Type { case "web": - return didresolver.NewWebResolver() + return didresolver.NewWebResolver(didresolver.WithLogger(logger)) default: return didresolver.NewNoopResolver() } diff --git a/internal/adapter/didresolver/web.go b/internal/adapter/didresolver/web.go index d37a017..37752e1 100644 --- a/internal/adapter/didresolver/web.go +++ b/internal/adapter/didresolver/web.go @@ -14,6 +14,7 @@ import ( "sync" "time" + "github.com/rs/zerolog" "golang.org/x/net/publicsuffix" "github.com/godaddy/ans/internal/domain" @@ -46,6 +47,7 @@ type Web struct { maxBodyBytes int64 rootCAs *x509.CertPool allowPrivate bool + logger zerolog.Logger } // WebOption customizes the Web resolver. @@ -84,11 +86,25 @@ func WithAllowPrivateNetworks() WebOption { return func(w *Web) { w.allowPrivate = true } } +// WithLogger attaches a server-side logger (default: no-op). It +// records the failure CATEGORY (the underlying error) plus the DID +// and elapsed time so an on-call engineer can distinguish a +// denylist rejection from a DNS-rebind block, a TLS failure, a +// timeout, or a refused connection — none of which ever reach the +// API caller (the wire error stays deliberately coarse, no SSRF +// oracle). +func WithLogger(logger zerolog.Logger) WebOption { + return func(w *Web) { + w.logger = logger.With().Str("component", "did-web-resolver").Logger() + } +} + // NewWebResolver constructs the production resolver. func NewWebResolver(opts ...WebOption) *Web { w := &Web{ timeout: 5 * time.Second, maxBodyBytes: 1 << 20, // 1 MiB + logger: zerolog.Nop(), } for _, opt := range opts { opt(w) @@ -121,10 +137,19 @@ func (w *Web) Resolve(ctx context.Context, did string, _ []port.KeyHint) (*port. } req.Header.Set("Accept", "application/did+json, application/json") + start := time.Now() resp, err := client.Do(req) if err != nil { - // Deliberately coarse: no resolved IPs, ports, or redirect - // targets in the detail (no SSRF oracle). + // The wire error stays deliberately coarse — no resolved IPs, + // ports, or redirect targets (no SSRF oracle). The underlying + // err carries the diagnosable category (denylist rejection, + // DNS-rebind block, TLS failure, timeout, refused) and goes + // only to the server-side log. + w.logger.Info(). + Err(err). + Str("did", did). + Int64("elapsedMs", time.Since(start).Milliseconds()). + Msg("did:web resolution fetch failed") return nil, domain.NewValidationError("DID_RESOLUTION_FAILED", fmt.Sprintf("could not fetch the DID document for %s", did)) } @@ -171,6 +196,11 @@ func (w *Web) newClient(originHost string) (*http.Client, error) { DialContext: pinned.DialContext, ForceAttemptHTTP2: true, TLSClientConfig: &tls.Config{RootCAs: w.rootCAs, MinVersion: tls.VersionTLS12}, + // This transport is built per Resolve call against one + // registrant-steered host; keeping idle connections open + // would let a connection linger ~90s after the fetch with no + // reuse benefit. Close them when the request completes. + DisableKeepAlives: true, } originDomain, err := registrableDomain(originHost) if err != nil { diff --git a/internal/ra/service/identity.go b/internal/ra/service/identity.go index f9c38a3..e83d2c8 100644 --- a/internal/ra/service/identity.go +++ b/internal/ra/service/identity.go @@ -9,6 +9,7 @@ import ( "time" "github.com/google/uuid" + "github.com/rs/zerolog" anscrypto "github.com/godaddy/ans/internal/crypto" "github.com/godaddy/ans/internal/domain" @@ -95,6 +96,7 @@ type IdentityService struct { sealTimeout time.Duration limiter *ownerLimiter linkLimiter *ownerLimiter + logger zerolog.Logger clock func() time.Time newID func() (string, error) newNonce func() (string, error) @@ -122,6 +124,7 @@ func NewIdentityService( sealTimeout: defaultSealTimeout, limiter: newOwnerLimiter(defaultRegisterPerMinute), linkLimiter: newOwnerLimiter(defaultLinkPerMinute), + logger: zerolog.Nop(), clock: time.Now, newID: func() (string, error) { id, err := uuid.NewV7() @@ -140,6 +143,17 @@ func NewIdentityService( } } +// WithLogger attaches the structured logger used at the seal +// boundary (default: a no-op logger). The identity lane seals +// synchronously, so — unlike the agent lane's outbox worker — there +// is no background component to log TL failures; this is where the +// operator's signal for TL_REJECTED_EVENT / TL_UNAVAILABLE comes +// from. +func (s *IdentityService) WithLogger(logger zerolog.Logger) *IdentityService { + s.logger = logger.With().Str("component", "identity-service").Logger() + return s +} + // WithSigner attaches the producer event signer (same KeyManager + // keyID + raID tuple the registration service signs with — one RA, // one producer identity). @@ -477,7 +491,14 @@ func (s *IdentityService) VerifyControl(ctx context.Context, providerID, identit // releaseClaim is the best-effort failure-path release of the // verify-control seal claim — failed attempts never consume (§3.2). func (s *IdentityService) releaseClaim(ctx context.Context, identityID, nonce string) { - _ = s.identities.ReleaseChallenge(ctx, identityID, nonce) + if err := s.identities.ReleaseChallenge(ctx, identityID, nonce); err != nil { + // Best-effort by contract, but a leaked claim blocks re-verify + // until its TTL lapses (~30s) — make that visible rather than + // silent. + s.logger.Warn().Err(err). + Str("identityId", identityID). + Msg("failed to release verify-control seal claim; it will self-heal at the claim TTL") + } } // Revoke transitions a VERIFIED identity to REVOKED and seals @@ -869,7 +890,24 @@ func (s *IdentityService) sealIdentityEvent(ctx context.Context, inner *identity } sealCtx, cancel := context.WithTimeout(ctx, s.sealTimeout) defer cancel() - return s.sealer.SealIdentityEvent(sealCtx, innerCanonical, producerSig) + if err := s.sealer.SealIdentityEvent(sealCtx, innerCanonical, producerSig); err != nil { + ev := s.logger.Error() + if errors.Is(err, domain.ErrUnavailable) { + // Transient — the TL didn't confirm; the operation fails + // retryable and nothing was consumed. WARN, not ERROR. + ev = s.logger.Warn() + } + ev.Err(err). + Str("identityId", inner.IdentityID). + Str("eventType", string(inner.EventType)). + Msg("identity event seal failed") + return err + } + s.logger.Info(). + Str("identityId", inner.IdentityID). + Str("eventType", string(inner.EventType)). + Msg("identity event sealed") + return nil } // mapIdentitySaveErr converts the storage layer's generic conflict From 8eaa0d75ce524cd38b0b938239627a6101973a21 Mon Sep 17 00:00:00 2001 From: Connor Snitker Date: Mon, 15 Jun 2026 14:15:42 -0500 Subject: [PATCH 11/12] fix(ra): identity list envelope mirrors AgentListResponse MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit GET /v2/ans/identities returned {identities, nextCursor} while its own description claimed the agent-list convention — which AgentListResponse documents as items (+ returnedCount/limit/ hasMore). Three different collection envelopes across sibling list endpoints on an append-only public contract. Return the AgentListResponse-shaped envelope: items + returnedCount + limit + nextCursor + hasMore. Add a named IdentityListResponse schema mirroring AgentListResponse, reference it from the 200, and sync the docsui copy via make docs-sync. The AgentDetails computed identities[] join (§5.4) is a different field and stays unchanged. Signed-off-by: Connor Snitker --- internal/adapter/docsui/openapi/ra.yaml | 56 +++++++++++++----- internal/ra/handler/identity.go | 34 +++++++++-- internal/ra/handler/identity_handler_test.go | 62 ++++++++++++-------- spec/api-spec-v2.yaml | 56 +++++++++++++----- 4 files changed, 153 insertions(+), 55 deletions(-) diff --git a/internal/adapter/docsui/openapi/ra.yaml b/internal/adapter/docsui/openapi/ra.yaml index 88eef90..d8bcf0a 100644 --- a/internal/adapter/docsui/openapi/ra.yaml +++ b/internal/adapter/docsui/openapi/ra.yaml @@ -968,9 +968,12 @@ paths: summary: List my identities description: | Returns one page of the caller's identities, newest first, in - the standard limit + opaque-cursor envelope (the agent-list - convention). Pass `nextCursor` back as `cursor` for the next - page; a null `nextCursor` is the last page. + the same limit + opaque-cursor envelope as + `GET /v2/ans/agents` (`AgentListResponse`): the collection + array is named `items`, alongside `returnedCount`, `limit`, + `nextCursor`, and `hasMore`. Pass `nextCursor` back as + `cursor` for the next page; a null `nextCursor` is the last + page. operationId: listIdentities parameters: - name: limit @@ -988,17 +991,7 @@ paths: content: application/json: schema: - type: object - required: [identities, nextCursor] - properties: - identities: - type: array - items: - $ref: '#/components/schemas/IdentityDetails' - nextCursor: - type: string - nullable: true - description: Cursor for the next page; null on the last page + $ref: '#/components/schemas/IdentityListResponse' '401': description: Authentication failed content: @@ -2164,6 +2157,41 @@ components: VERIFIED (the staged replacement proves control before anything changes). + IdentityListResponse: + type: object + description: | + Ownership-scoped, cursor-paginated list of the caller's + verified identities. Mirrors `AgentListResponse`: the + collection array is named `items` per the standard REST + collection-response convention shared across the v2 surface. + properties: + items: + type: array + description: List of identities owned by the caller + items: + $ref: '#/components/schemas/IdentityDetails' + returnedCount: + type: integer + description: Number of identities in this response + limit: + type: integer + description: Limit that was applied + nextCursor: + type: string + nullable: true + description: | + Opaque cursor for the next page. Pass as the `cursor` query + parameter to retrieve the next page. Null when there are no + more results. + hasMore: + type: boolean + description: Whether more results exist beyond this page + required: + - items + - returnedCount + - limit + - hasMore + IdentityDetails: type: object properties: diff --git a/internal/ra/handler/identity.go b/internal/ra/handler/identity.go index 7dc1d8f..7f451ff 100644 --- a/internal/ra/handler/identity.go +++ b/internal/ra/handler/identity.go @@ -177,9 +177,26 @@ func (h *IdentityHandler) Revoke(w http.ResponseWriter, r *http.Request) { WriteJSON(w, http.StatusOK, toDetailResponse(identity, nil)) } +// defaultIdentityListLimit mirrors the store's default page size so +// the reported `limit` is accurate when the caller omits one. +const defaultIdentityListLimit = 20 + +// identityListResponse is the cursor-paginated list envelope, named +// to match AgentListResponse (`items` + returnedCount/limit/ +// nextCursor/hasMore) — one collection-response convention across +// the v2 surface. +type identityListResponse struct { + Items []identityDetailResponse `json:"items"` + ReturnedCount int `json:"returnedCount"` + Limit int `json:"limit"` + NextCursor *string `json:"nextCursor"` + HasMore bool `json:"hasMore"` +} + // List handles GET /v2/ans/identities — one page of the caller's -// identities, in the v2 limit + opaque-cursor envelope (§5.6.1: -// pagination inherits the surface's convention). +// identities, in the v2 limit + opaque-cursor envelope mirroring +// AgentListResponse (`items` array): one collection convention +// across the v2 surface. func (h *IdentityHandler) List(w http.ResponseWriter, r *http.Request) { providerID, ok := callerSubject(w, r) if !ok { @@ -204,9 +221,18 @@ func (h *IdentityHandler) List(w http.ResponseWriter, r *http.Request) { for _, identity := range page.Items { out = append(out, toDetailResponse(identity, nil)) } - resp := map[string]any{"identities": out, "nextCursor": nil} + effectiveLimit := limit + if effectiveLimit == 0 { + effectiveLimit = defaultIdentityListLimit + } + resp := identityListResponse{ + Items: out, + ReturnedCount: len(out), + Limit: effectiveLimit, + HasMore: page.HasMore, + } if page.NextCursor != "" { - resp["nextCursor"] = page.NextCursor + resp.NextCursor = &page.NextCursor } WriteJSON(w, http.StatusOK, resp) } diff --git a/internal/ra/handler/identity_handler_test.go b/internal/ra/handler/identity_handler_test.go index a4f241e..1de1164 100644 --- a/internal/ra/handler/identity_handler_test.go +++ b/internal/ra/handler/identity_handler_test.go @@ -211,6 +211,37 @@ func (f *identityHTTPFixture) registerAndVerify(t *testing.T, owner, didValue st return ch.IdentityID } +// assertSingletonVerifiedList pins GET /v2/ans/identities returning a +// single VERIFIED identity in the AgentListResponse-shaped envelope — +// `items` array plus returnedCount/limit/hasMore (review #6). Lives +// here so its branches stay out of the big lifecycle test's +// cyclomatic budget. +func assertSingletonVerifiedList(t *testing.T, f *identityHTTPFixture, owner string) { + t.Helper() + rec := f.do(t, owner, http.MethodGet, "/v2/ans/identities", nil) + if rec.Code != http.StatusOK { + t.Fatalf("list: %d", rec.Code) + } + var list struct { + Items []struct { + IdentityID string `json:"identityId"` + Status string `json:"status"` + } `json:"items"` + ReturnedCount int `json:"returnedCount"` + Limit int `json:"limit"` + HasMore bool `json:"hasMore"` + } + if err := json.Unmarshal(rec.Body.Bytes(), &list); err != nil { + t.Fatal(err) + } + if len(list.Items) != 1 || list.Items[0].Status != "VERIFIED" { + t.Fatalf("list shape: %+v", list) + } + if list.ReturnedCount != 1 || list.Limit != 20 || list.HasMore { + t.Fatalf("list envelope (items/returnedCount/limit/hasMore): %+v", list) + } +} + func TestIdentityHandler_RegisterShape(t *testing.T) { t.Parallel() f := newIdentityHTTPFixture(t) @@ -281,29 +312,14 @@ func TestIdentityHandler_FullLifecycleOverHTTP(t *testing.T) { identityID := f.registerAndVerify(t, owner, "did:web:identity.acme-corp.com") - // List. - rec := f.do(t, owner, http.MethodGet, "/v2/ans/identities", nil) - if rec.Code != http.StatusOK { - t.Fatalf("list: %d", rec.Code) - } - var list struct { - Identities []struct { - IdentityID string `json:"identityId"` - Status string `json:"status"` - } `json:"identities"` - } - if err := json.Unmarshal(rec.Body.Bytes(), &list); err != nil { - t.Fatal(err) - } - if len(list.Identities) != 1 || list.Identities[0].Status != "VERIFIED" { - t.Fatalf("list shape: %+v", list) - } + // List — the AgentListResponse-shaped envelope (items + counts). + assertSingletonVerifiedList(t, f, owner) // Register the agent to link. agentID := registerAgentForIdentity(t, f, owner, "linked.example.com") // Link → 200 {linked:1}. - rec = f.do(t, owner, http.MethodPost, "/v2/ans/identities/"+identityID+"/links", + rec := f.do(t, owner, http.MethodPost, "/v2/ans/identities/"+identityID+"/links", map[string]any{"agentIds": []string{agentID}}) if rec.Code != http.StatusOK { t.Fatalf("link: %d %s", rec.Code, rec.Body) @@ -451,9 +467,9 @@ func TestIdentityHandler_ListPagination(t *testing.T) { } type page struct { - Identities []struct { + Items []struct { IdentityID string `json:"identityId"` - } `json:"identities"` + } `json:"items"` NextCursor *string `json:"nextCursor"` } var p1 page @@ -464,7 +480,7 @@ func TestIdentityHandler_ListPagination(t *testing.T) { if err := json.Unmarshal(rec.Body.Bytes(), &p1); err != nil { t.Fatal(err) } - if len(p1.Identities) != 2 || p1.NextCursor == nil { + if len(p1.Items) != 2 || p1.NextCursor == nil { t.Fatalf("page 1 shape: %+v", p1) } @@ -476,12 +492,12 @@ func TestIdentityHandler_ListPagination(t *testing.T) { if err := json.Unmarshal(rec.Body.Bytes(), &p2); err != nil { t.Fatal(err) } - if len(p2.Identities) != 1 || p2.NextCursor != nil { + if len(p2.Items) != 1 || p2.NextCursor != nil { t.Fatalf("page 2 shape: %+v", p2) } // No overlap between pages. seen := map[string]bool{} - for _, e := range append(p1.Identities, p2.Identities...) { + for _, e := range append(p1.Items, p2.Items...) { if seen[e.IdentityID] { t.Fatalf("duplicate across pages: %s", e.IdentityID) } diff --git a/spec/api-spec-v2.yaml b/spec/api-spec-v2.yaml index 88eef90..d8bcf0a 100644 --- a/spec/api-spec-v2.yaml +++ b/spec/api-spec-v2.yaml @@ -968,9 +968,12 @@ paths: summary: List my identities description: | Returns one page of the caller's identities, newest first, in - the standard limit + opaque-cursor envelope (the agent-list - convention). Pass `nextCursor` back as `cursor` for the next - page; a null `nextCursor` is the last page. + the same limit + opaque-cursor envelope as + `GET /v2/ans/agents` (`AgentListResponse`): the collection + array is named `items`, alongside `returnedCount`, `limit`, + `nextCursor`, and `hasMore`. Pass `nextCursor` back as + `cursor` for the next page; a null `nextCursor` is the last + page. operationId: listIdentities parameters: - name: limit @@ -988,17 +991,7 @@ paths: content: application/json: schema: - type: object - required: [identities, nextCursor] - properties: - identities: - type: array - items: - $ref: '#/components/schemas/IdentityDetails' - nextCursor: - type: string - nullable: true - description: Cursor for the next page; null on the last page + $ref: '#/components/schemas/IdentityListResponse' '401': description: Authentication failed content: @@ -2164,6 +2157,41 @@ components: VERIFIED (the staged replacement proves control before anything changes). + IdentityListResponse: + type: object + description: | + Ownership-scoped, cursor-paginated list of the caller's + verified identities. Mirrors `AgentListResponse`: the + collection array is named `items` per the standard REST + collection-response convention shared across the v2 surface. + properties: + items: + type: array + description: List of identities owned by the caller + items: + $ref: '#/components/schemas/IdentityDetails' + returnedCount: + type: integer + description: Number of identities in this response + limit: + type: integer + description: Limit that was applied + nextCursor: + type: string + nullable: true + description: | + Opaque cursor for the next page. Pass as the `cursor` query + parameter to retrieve the next page. Null when there are no + more results. + hasMore: + type: boolean + description: Whether more results exist beyond this page + required: + - items + - returnedCount + - limit + - hasMore + IdentityDetails: type: object properties: From 684b49b114ebc4758fdd28bc3151c2f515bc9ea5 Mon Sep 17 00:00:00 2001 From: Connor Snitker Date: Mon, 15 Jun 2026 14:42:20 -0500 Subject: [PATCH 12/12] test: restore coverage margin over the review-fix code MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The review fixes added internal/ lines (the seal-boundary logging branches, the resolver failure-category log, the WithLogger options, the new list envelope) faster than their tests, leaving total statement coverage sitting on the 90.0% rounding boundary — a run could round to 89.9% and trip the gate. Add tests exercising the new and adjacent uncovered paths to carry real margin (90.2%): - did:web resolver: a fetch-failure case with a logger attached asserts the diagnosable category reaches the SERVER-SIDE log while the wire error stays coarse (covers WithLogger + the log arm). - identity service: the recording fixture now attaches a logger; a TL_REJECTED_EVENT (non-transient) seal failure covers the ERROR branch and fail-closed behavior; a wrapper that fails ReleaseChallenge covers the leaked-claim WARN path (best-effort release does not change the retryable outcome). - identity handlers: service-error pass-throughs (valid body, unknown identity → 404/403) and empty-collection validation (empty signedProofs / agentIds) across the mutating routes. No production code changes — tests only. Signed-off-by: Connor Snitker --- internal/adapter/didresolver/resolver_test.go | 14 +++ internal/ra/handler/identity_handler_test.go | 62 +++++++++++ internal/ra/service/identity_test.go | 102 +++++++++++++++++- 3 files changed, 177 insertions(+), 1 deletion(-) diff --git a/internal/adapter/didresolver/resolver_test.go b/internal/adapter/didresolver/resolver_test.go index 221555e..38c6247 100644 --- a/internal/adapter/didresolver/resolver_test.go +++ b/internal/adapter/didresolver/resolver_test.go @@ -1,6 +1,7 @@ package didresolver import ( + "bytes" "context" "encoding/json" "net" @@ -11,6 +12,8 @@ import ( "testing" "time" + "github.com/rs/zerolog" + "github.com/godaddy/ans/internal/domain" "github.com/godaddy/ans/internal/port" ) @@ -342,10 +345,16 @@ func TestParseDIDDocument_Tolerance(t *testing.T) { // against a host that cannot resolve: the fetch-failure branch // returns the coarse DID_RESOLUTION_FAILED (no SSRF oracle detail). func TestWebResolver_FullResolveFetchFailure(t *testing.T) { + // A logger is attached so the server-side failure-category log + // path (and WithLogger) is exercised; the wire error must stay + // coarse regardless. The log goes to a buffer we assert on — the + // category lives server-side, never in the returned error. + var logbuf bytes.Buffer w := NewWebResolver( WithTimeout(2*time.Second), WithRootCAs(nil), WithAllowPrivateNetworks(), + WithLogger(zerolog.New(&logbuf)), ) _, err := w.Resolve(context.Background(), "did:web:no-such-host-ans-test.invalid", nil) if err == nil || !strings.Contains(err.Error(), "DID_RESOLUTION_FAILED") { @@ -354,6 +363,11 @@ func TestWebResolver_FullResolveFetchFailure(t *testing.T) { if strings.Contains(err.Error(), "127.") { t.Fatalf("error detail leaks addresses: %v", err) } + // The diagnosable category reached the server-side log, not the + // caller. + if !strings.Contains(logbuf.String(), "did:web resolution fetch failed") { + t.Fatalf("expected a server-side failure log, got %q", logbuf.String()) + } } // resolveTimeoutGuard pins that the context deadline propagates: a diff --git a/internal/ra/handler/identity_handler_test.go b/internal/ra/handler/identity_handler_test.go index 1de1164..28c0294 100644 --- a/internal/ra/handler/identity_handler_test.go +++ b/internal/ra/handler/identity_handler_test.go @@ -504,3 +504,65 @@ func TestIdentityHandler_ListPagination(t *testing.T) { seen[e.IdentityID] = true } } + +// TestIdentityHandler_ServiceErrorsPropagate exercises the +// service-error pass-through arms across the mutating identity +// handlers: a well-formed request against an identity the caller +// does not own / that does not exist surfaces the service's domain +// error (404/403), not a 200. Complements the malformed-body and +// auth tests, which never reach the service call. +func TestIdentityHandler_ServiceErrorsPropagate(t *testing.T) { + t.Parallel() + f := newIdentityHTTPFixture(t) + owner := "owner-svc-err" + missing := "01HXMISSINGIDENTITY0000000" + + // Valid bodies, unknown identity → not-found from the owner gate. + cases := []struct { + method, path string + body any + }{ + {http.MethodGet, "/v2/ans/identities/" + missing, nil}, + {http.MethodPut, "/v2/ans/identities/" + missing, map[string]string{"value": "did:web:x.example.com"}}, + {http.MethodPost, "/v2/ans/identities/" + missing + "/verify-control", map[string]any{"signedProofs": []string{"a.b.c"}}}, + {http.MethodPost, "/v2/ans/identities/" + missing + "/revoke", nil}, + {http.MethodPost, "/v2/ans/identities/" + missing + "/links", map[string]any{"agentIds": []string{"some-agent"}}}, + {http.MethodDelete, "/v2/ans/identities/" + missing + "/links/some-agent", nil}, + } + for _, tc := range cases { + rec := f.do(t, owner, tc.method, tc.path, tc.body) + if rec.Code != http.StatusNotFound && rec.Code != http.StatusForbidden { + t.Fatalf("%s %s: got %d, want 404/403 (service error must propagate)", tc.method, tc.path, rec.Code) + } + } +} + +// TestIdentityHandler_EmptyCollectionsRejected pins the +// missing-required-field validation arms that a syntactically valid +// but empty body reaches: verify-control with no proofs and links +// with no agentIds are 4xx, not 200. +func TestIdentityHandler_EmptyCollectionsRejected(t *testing.T) { + t.Parallel() + f := newIdentityHTTPFixture(t) + owner := "owner-empty" + id := f.registerAndVerify(t, owner, "did:web:empty-coll.example.com") + agentID := registerAgentForIdentity(t, f, owner, "empty-coll-agent.example.com") + + // Empty signedProofs on a verified identity → rejected (no proof + // to verify), not a 200. + if rec := f.do(t, owner, http.MethodPost, "/v2/ans/identities/"+id+"/verify-control", + map[string]any{"signedProofs": []string{}}); rec.Code < 400 { + t.Fatalf("empty signedProofs: got %d, want 4xx", rec.Code) + } + // Empty agentIds on a link call → INVALID_LINK_REQUEST. + if rec := f.do(t, owner, http.MethodPost, "/v2/ans/identities/"+id+"/links", + map[string]any{"agentIds": []string{}}); rec.Code != http.StatusUnprocessableEntity { + t.Fatalf("empty agentIds: got %d, want 422", rec.Code) + } + // A valid single-agent link still works (sanity — the fixture's + // okSealer acknowledges). + if rec := f.do(t, owner, http.MethodPost, "/v2/ans/identities/"+id+"/links", + map[string]any{"agentIds": []string{agentID}}); rec.Code != http.StatusOK { + t.Fatalf("valid link after empty-batch reject: %d %s", rec.Code, rec.Body) + } +} diff --git a/internal/ra/service/identity_test.go b/internal/ra/service/identity_test.go index bdc2f10..e9ad0a1 100644 --- a/internal/ra/service/identity_test.go +++ b/internal/ra/service/identity_test.go @@ -18,6 +18,7 @@ import ( "encoding/base64" "encoding/json" "errors" + "io" "strings" "sync" "testing" @@ -31,6 +32,7 @@ import ( "github.com/godaddy/ans/internal/port" "github.com/godaddy/ans/internal/ra/service" identityevent "github.com/godaddy/ans/internal/tl/event/identity" + "github.com/rs/zerolog" ) // fakeResolver returns a canned document — the did:web rule tests @@ -140,7 +142,7 @@ func newIdentityFixture(t *testing.T, resolver port.DIDResolver) *identityFixtur KeyManager: km, KeyID: "ra-signer", RaID: "ra-test", - }).WithClock(clock.Now) + }).WithClock(clock.Now).WithLogger(zerolog.New(io.Discard)) return &identityFixture{ svc: svc, @@ -1438,3 +1440,101 @@ func TestNilSealerFailsClosed(t *testing.T) { t.Fatalf("row must stand: %v (%v)", got.Status, gerr) } } + +// TestVerifyControl_TLRejectedEventFailsClosed pins the ERROR seal +// branch: a NON-transient TL rejection (TL_REJECTED_EVENT — the RA +// produced an event the TL refuses, a pipeline bug) fails the +// operation closed, consumes nothing, and is distinct from the +// retryable TL_UNAVAILABLE path. +func TestVerifyControl_TLRejectedEventFailsClosed(t *testing.T) { + t.Parallel() + fx := newIdentityFixture(t, nil) + ctx := context.Background() + res, err := fx.svc.Register(ctx, fx.providerID, "did:web:rejected.example.com") + if err != nil { + t.Fatal(err) + } + priv := genKey(t) + jws := signProof(t, priv, res.Identity.Value+"#key-1", res.Challenges[0].SigningInput, true) + sub := service.ProofSubmission{SignedProofs: []string{jws}} + + // A schema rejection is ErrInternal (TL_REJECTED_EVENT), not + // ErrUnavailable — the seal boundary logs it at ERROR and the + // operation fails. + fx.sealer.fail(domain.NewInternalError("TL_REJECTED_EVENT", "the transparency log rejected the identity event", errors.New("422"))) + _, err = fx.svc.VerifyControl(ctx, fx.providerID, res.Identity.IdentityID, sub) + if err == nil || errors.Is(err, domain.ErrUnavailable) { + t.Fatalf("want a non-unavailable rejection error, got %v", err) + } + if rows := fx.drainSealed(t); len(rows) != 0 { + t.Fatalf("rejected seal must record nothing, got %d", len(rows)) + } + identity, _, derr := fx.svc.Detail(ctx, fx.providerID, res.Identity.IdentityID) + if derr != nil || identity.Status != domain.IdentityPendingControl { + t.Fatalf("row must stand on rejection: %v (%v)", identity.Status, derr) + } + + // The claim was released, so a retry once the pipeline is fixed + // succeeds with the same proof. + fx.sealer.fail(nil) + if _, err := fx.svc.VerifyControl(ctx, fx.providerID, res.Identity.IdentityID, sub); err != nil { + t.Fatalf("retry after rejection cleared: %v", err) + } +} + +// releaseFailStore wraps the real identity store but fails +// ReleaseChallenge, so the leaked-claim WARN path in releaseClaim is +// exercised — the operation must still fail (best-effort release does +// not change the outcome) without panicking. +type releaseFailStore struct { + port.IdentityStore +} + +func (releaseFailStore) ReleaseChallenge(context.Context, string, string) error { + return errors.New("release failed (simulated)") +} + +// TestReleaseClaim_LeakedClaimIsLoggedNotSwallowed pins that a failed +// claim release is surfaced (logged), not silently dropped, and does +// not change the operation's failure. +func TestReleaseClaim_LeakedClaimIsLoggedNotSwallowed(t *testing.T) { + t.Parallel() + db, err := sqlite.Open(context.Background(), ":memory:") + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { _ = db.Close() }) + km, err := keymanager.NewFileKeyManager(t.TempDir() + "/keys") + if err != nil { + t.Fatal(err) + } + if _, err := km.EnsureKey(context.Background(), "ra-signer", port.AlgorithmECDSAP256); err != nil { + t.Fatal(err) + } + clock := &fakeClock{now: time.Date(2026, 6, 10, 15, 0, 0, 0, time.UTC)} + sealer := &recordingSealer{err: domain.NewUnavailableError("TL_UNAVAILABLE", "down")} + svc := service.NewIdentityService( + releaseFailStore{IdentityStore: sqlite.NewIdentityStore(db)}, + sqlite.NewIdentityLinkStore(db), + sqlite.NewAgentStore(db), + didresolver.NewNoopResolver(), + sealer, + db, + ).WithSigner(service.EventSigner{KeyManager: km, KeyID: "ra-signer", RaID: "ra-test"}). + WithClock(clock.Now).WithLogger(zerolog.New(io.Discard)) + + ctx := context.Background() + res, err := svc.Register(ctx, "owner-rl", "did:web:leak.example.com") + if err != nil { + t.Fatal(err) + } + priv := genKey(t) + jws := signProof(t, priv, res.Identity.Value+"#key-1", res.Challenges[0].SigningInput, true) + // Seal fails (TL down) → the success path releases the claim; the + // release itself fails → WARN-logged, but the verify still fails + // retryable as expected (not a panic, not a different error). + _, err = svc.VerifyControl(ctx, "owner-rl", res.Identity.IdentityID, service.ProofSubmission{SignedProofs: []string{jws}}) + if !errors.Is(err, domain.ErrUnavailable) { + t.Fatalf("want ErrUnavailable despite the release failure, got %v", err) + } +}