openapi: 3.1.0
info:
  title: OHN-07 Network Organization and Membership Registry
  version: 1.0.0
  description: |
    Authoritative registry of network organizations, hierarchy, member agreements and endpoints.
    All paths are jurisdiction-scoped to the bearer principal. Every POST requires `Idempotency-Key`
    (successful responses are replayed; key reuse with another body = 409). Version-checked writes take
    `expected_version` and return 409 `concurrent_mutation` on a stale version. Errors: 401 unauthenticated,
    403 denied_by_policy / separation_of_duties, 404 not_found, 409 concurrent_mutation / invalid_transition /
    duplicate / membership_not_active / challenge_expired, 422 domain_validation_failed (zod `details`) /
    untrusted_assessment / untrusted_certificate / challenge_mismatch / policy_refused, 429 rate_limited,
    503 not_configured (names the OHN07_* setting) / dependency_unavailable / server_unavailable.
servers: [{ url: /api/ohn/v1/network-registry }]
security: [{ bearer: [] }]
components:
  securitySchemes:
    bearer: { type: http, scheme: bearer, bearerFormat: JWT, description: 'HS256, iss ohn-07-registry, aud ohn-07-api; claims sub, role, jurisdiction, organization_id, purpose.' }
  parameters:
    Idem: { name: Idempotency-Key, in: header, required: true, schema: { type: string, maxLength: 255 } }
    Id: { name: id, in: path, required: true, schema: { type: string, format: uuid } }
    Limit: { name: limit, in: query, schema: { type: integer, minimum: 1, maximum: 100, default: 25 } }
    Offset: { name: offset, in: query, schema: { type: integer, minimum: 0, default: 0 } }
  schemas:
    Uuid: { type: string, format: uuid }
    Receipt:
      type: object
      required: [receipt_id, committed_at, aggregate_type, aggregate_id, version, status]
      properties: { receipt_id: { $ref: '#/components/schemas/Uuid' }, committed_at: { type: string, format: date-time }, aggregate_type: { type: string }, aggregate_id: { type: string }, version: { type: integer }, status: { enum: [committed, pending] } }
    Error: { type: object, required: [error], properties: { error: { type: string }, message: { type: string }, details: { type: object }, setting: { type: string } } }
    MembershipState: { enum: [applied, documents_verified, trust_assessed, agreement_signed, active, suspension_review, revoked, downstream_notified] }
    EndpointState: { enum: [registered, challenge_sent, ownership_proved, policy_checked, enabled, disabled] }
    OrganizationApplication:
      type: object
      additionalProperties: false
      required: [legal_name, jurisdiction_id, organization_type, registry_ref, accountable_officer, terms_version]
      properties:
        legal_name: { type: string, minLength: 1, maxLength: 512 }
        jurisdiction_id: { $ref: '#/components/schemas/Uuid' }
        organization_type: { type: string, description: 'Member of OHN07_ORGANIZATION_TYPES.' }
        registry_ref: { type: string, maxLength: 512, description: 'Must resolve in OHN-05.' }
        accountable_officer: { $ref: '#/components/schemas/Uuid' }
        terms_version: { type: string, maxLength: 512 }
        parent_organization_id: { oneOf: [{ $ref: '#/components/schemas/Uuid' }, { type: 'null' }] }
    OrganizationAmend:
      type: object
      additionalProperties: false
      required: [expected_version, reason]
      properties: { legal_name: { type: string }, organization_type: { type: string }, registry_ref: { type: string }, accountable_officer: { $ref: '#/components/schemas/Uuid' }, terms_version: { type: string }, parent_organization_id: { oneOf: [{ $ref: '#/components/schemas/Uuid' }, { type: 'null' }] }, expected_version: { type: integer, minimum: 1 }, reason: { type: string, minLength: 10, maxLength: 4096 } }
    MembershipApproval:
      type: object
      additionalProperties: false
      required: [organization_id, agreement_ref, trust_assessment_id, approved_scope, reviewer, expected_version]
      properties: { organization_id: { $ref: '#/components/schemas/Uuid' }, agreement_ref: { $ref: '#/components/schemas/Uuid' }, trust_assessment_id: { $ref: '#/components/schemas/Uuid' }, approved_scope: { type: string, maxLength: 8192 }, reviewer: { $ref: '#/components/schemas/Uuid', description: 'Must equal the bearer principal.' }, expected_version: { type: integer } }
    Transition:
      type: object
      additionalProperties: false
      required: [to_state, expected_version, reason]
      properties: { to_state: { enum: [documents_verified, trust_assessed, active] }, expected_version: { type: integer }, reason: { type: string, minLength: 10 }, trust_assessment_id: { $ref: '#/components/schemas/Uuid' } }
    Suspension:
      type: object
      additionalProperties: false
      required: [organization_id, suspension_reason, effective_at, affected_endpoints, expected_version]
      properties: { organization_id: { $ref: '#/components/schemas/Uuid' }, suspension_reason: { type: string, minLength: 10, maxLength: 4096 }, effective_at: { type: string, format: date-time }, affected_endpoints: { type: string, maxLength: 8192 }, expected_version: { type: integer } }
    Decision: { type: object, additionalProperties: false, required: [expected_version, reason], properties: { expected_version: { type: integer }, reason: { type: string, minLength: 10 } } }
    EndpointRegistration:
      type: object
      additionalProperties: false
      required: [organization_id, endpoint_url, service_type, environment, certificate_ref, jurisdiction_id]
      properties: { organization_id: { $ref: '#/components/schemas/Uuid' }, endpoint_url: { type: string, format: uri, pattern: '^https://', maxLength: 2048 }, service_type: { type: string }, environment: { type: string }, certificate_ref: { $ref: '#/components/schemas/Uuid' }, jurisdiction_id: { $ref: '#/components/schemas/Uuid' } }
    Step: { type: object, additionalProperties: false, required: [expected_version], properties: { expected_version: { type: integer } } }
    Standing:
      type: object
      description: 'Routing decision contract v1.0.0 for clinical gateways (OHN-20 etc.). Never cache beyond cache_ttl_seconds (0).'
      required: [contract_version, organization_id, membership_state, routable, reason, enabled_endpoints, evaluated_at, cache_ttl_seconds]
      properties:
        contract_version: { const: '1.0.0' }
        organization_id: { $ref: '#/components/schemas/Uuid' }
        jurisdiction_id: { $ref: '#/components/schemas/Uuid' }
        legal_name: { type: string }
        membership_state: { oneOf: [{ $ref: '#/components/schemas/MembershipState' }, { const: none }] }
        trust_tier: { type: [string, 'null'] }
        membership_active: { type: boolean }
        routable: { type: boolean, description: 'true only if membership active AND at least one enabled endpoint with unexpired certificate.' }
        reason: { enum: [active_member, no_enabled_endpoint, not_a_member, unknown_endpoint, membership_applied, membership_documents_verified, membership_trust_assessed, membership_agreement_signed, membership_suspension_review, membership_revoked, membership_downstream_notified, endpoint_registered, endpoint_challenge_sent, endpoint_ownership_proved, endpoint_policy_checked, endpoint_disabled] }
        enabled_endpoints: { type: array, items: { type: object, properties: { id: { type: string }, endpoint_url: { type: string }, service_type: { type: string }, environment: { type: string }, certificate_expires_at: { type: [string, 'null'] } } } }
        evaluated_at: { type: string, format: date-time }
        cache_ttl_seconds: { const: 0 }
paths:
  /auth/token:
    post: { summary: 'Synthetic sign-in (404 unless OHN07_SYNTHETIC_AUTH=true and NODE_ENV!=production)', security: [], responses: { '200': { description: token }, '404': { description: disabled }, '422': { description: invalid } } }
  /config:
    get: { summary: Policy configuration, own permissions and dependency mode, responses: { '200': { description: ok } } }
  /dashboard:
    get: { summary: 'P001 KPIs, status distribution, 14-day trend, exceptions', responses: { '200': { description: ok } } }
  /organizations:
    get: { summary: List organizations, parameters: [{ $ref: '#/components/parameters/Limit' }, { $ref: '#/components/parameters/Offset' }, { name: q, in: query, schema: { type: string } }, { name: organization_type, in: query, schema: { type: string } }, { name: state, in: query, schema: { $ref: '#/components/schemas/MembershipState' } }], responses: { '200': { description: ok } } }
    post:
      operationId: OHN-07-API-001
      summary: F001 organization application (creates organization + membership in state applied; emits organization.updated)
      parameters: [{ $ref: '#/components/parameters/Idem' }]
      requestBody: { required: true, content: { application/json: { schema: { $ref: '#/components/schemas/OrganizationApplication' } } } }
      responses: { '201': { description: created }, '403': { description: other jurisdiction }, '409': { description: duplicate registry_ref }, '422': { description: validation }, '503': { description: dependency or policy unavailable } }
  /organizations/tree:
    get: { summary: P002 hierarchy (roots with nested children), responses: { '200': { description: ok } } }
  /organizations/{id}:
    get: { summary: Organization detail with ancestors, children, agreements, endpoints, suspensions, history, provenance, parameters: [{ $ref: '#/components/parameters/Id' }], responses: { '200': { description: ok }, '404': { description: not in scope } } }
  /organizations/{id}/amend:
    post: { summary: Amend organization (cycle-safe parent change; emits organization.updated), parameters: [{ $ref: '#/components/parameters/Id' }, { $ref: '#/components/parameters/Idem' }], requestBody: { content: { application/json: { schema: { $ref: '#/components/schemas/OrganizationAmend' } } } }, responses: { '200': { description: ok }, '409': { description: stale }, '422': { description: cycle or validation } } }
  /organizations/{id}/certificates:
    get: { summary: "Certificate picker source: the organization's certificates held by OHN-39 (GET /api/ohn/v1/trust/certificates, organization-scoped). certificate_ref is the value POST /endpoints checks; usable = active, current/rollover/overlap and not expired. Audited (certificate.list).", parameters: [{ $ref: '#/components/parameters/Id' }], responses: { '200': { description: ok }, '404': { description: not in scope }, '503': { description: OHN-39 unavailable } } }
  /organizations/{id}/standing:
    get: { summary: Routing decision for an organization (roles network_service, officer, reviewer), parameters: [{ $ref: '#/components/parameters/Id' }], responses: { '200': { description: ok, content: { application/json: { schema: { $ref: '#/components/schemas/Standing' } } } } } }
  /routing-decisions:
    get: { summary: Routing decision by organization_id or endpoint_url, parameters: [{ name: organization_id, in: query, schema: { type: string, format: uuid } }, { name: endpoint_url, in: query, schema: { type: string } }], responses: { '200': { description: ok, content: { application/json: { schema: { $ref: '#/components/schemas/Standing' } } } } } }
  /memberships:
    get: { summary: List memberships, parameters: [{ name: state, in: query, schema: { $ref: '#/components/schemas/MembershipState' } }, { $ref: '#/components/parameters/Limit' }, { $ref: '#/components/parameters/Offset' }], responses: { '200': { description: ok } } }
    post:
      operationId: OHN-07-API-002
      summary: F002 membership approval — signs agreement version n+1 (trust_assessed → agreement_signed, or amendment while active)
      parameters: [{ $ref: '#/components/parameters/Idem' }]
      requestBody: { required: true, content: { application/json: { schema: { $ref: '#/components/schemas/MembershipApproval' } } } }
      responses: { '201': { description: signed }, '403': { description: role or separation_of_duties }, '409': { description: state or version }, '422': { description: untrusted_assessment or validation } }
  /memberships/{id}:
    get: { operationId: OHN-07-API-003, summary: Membership with organization, agreements, suspensions, endpoints, events, history and downstream status, parameters: [{ $ref: '#/components/parameters/Id' }], responses: { '200': { description: ok }, '404': { description: not in scope } } }
  /memberships/{id}/transitions:
    post: { summary: 'W001 steps: documents_verified (officer), trust_assessed (reviewer, OHN-39 check), active (officer; emits membership.approved)', parameters: [{ $ref: '#/components/parameters/Id' }, { $ref: '#/components/parameters/Idem' }], requestBody: { content: { application/json: { schema: { $ref: '#/components/schemas/Transition' } } } }, responses: { '200': { description: ok }, '403': { description: wrong role }, '409': { description: invalid transition }, '422': { description: untrusted } } }
  /memberships/{id}/suspensions:
    post: { summary: 'F004 open suspension review (officer; active → suspension_review; routing blocked immediately)', parameters: [{ $ref: '#/components/parameters/Id' }, { $ref: '#/components/parameters/Idem' }], requestBody: { content: { application/json: { schema: { $ref: '#/components/schemas/Suspension' } } } }, responses: { '201': { description: ok }, '409': { description: not active } } }
  /memberships/{id}/revoke:
    post: { operationId: OHN-07-API-004, summary: 'Revoke (reviewer ≠ requester): disables endpoints, terminates agreements, emits membership.revoked; 200 when downstream confirmed, 202 when pending', parameters: [{ $ref: '#/components/parameters/Id' }, { $ref: '#/components/parameters/Idem' }], requestBody: { content: { application/json: { schema: { $ref: '#/components/schemas/Decision' } } } }, responses: { '200': { description: downstream_notified }, '202': { description: revoked, downstream pending }, '403': { description: separation_of_duties } } }
  /memberships/{id}/reinstate:
    post: { summary: Reinstate after review (reviewer ≠ requester), parameters: [{ $ref: '#/components/parameters/Id' }, { $ref: '#/components/parameters/Idem' }], requestBody: { content: { application/json: { schema: { $ref: '#/components/schemas/Decision' } } } }, responses: { '200': { description: active again } } }
  /agreements:
    get: { summary: List agreements, parameters: [{ name: organization_id, in: query, schema: { type: string } }, { name: status, in: query, schema: { enum: [approved, terminated] } }], responses: { '200': { description: ok } } }
  /agreements/diff:
    get: { summary: P007 field diff between two agreement versions, parameters: [{ name: organization_id, in: query, required: true, schema: { type: string } }, { name: from_version, in: query, schema: { type: integer } }, { name: to_version, in: query, schema: { type: integer } }], responses: { '200': { description: ok } } }
  /endpoints:
    get: { summary: 'P006 server-paginated endpoint list (filters state, service_type, environment, organization_id, q; sort endpoint_url|state|certificate_expires_at|created_at, dir)', responses: { '200': { description: ok } } }
    post: { summary: 'F003 register endpoint (active member, OHN-39-trusted certificate, https)', parameters: [{ $ref: '#/components/parameters/Idem' }], requestBody: { content: { application/json: { schema: { $ref: '#/components/schemas/EndpointRegistration' } } } }, responses: { '201': { description: registered }, '409': { description: membership_not_active }, '422': { description: untrusted_certificate } } }
  /endpoints/{id}:
    get: { summary: Endpoint detail with probes and history, parameters: [{ $ref: '#/components/parameters/Id' }], responses: { '200': { description: ok } } }
  /endpoints/{id}/challenge:
    post: { summary: 'W002 issue ownership challenge (officer); returns one-time token and publish_at', parameters: [{ $ref: '#/components/parameters/Id' }, { $ref: '#/components/parameters/Idem' }], requestBody: { content: { application/json: { schema: { $ref: '#/components/schemas/Step' } } } }, responses: { '200': { description: challenge_sent } } }
  /endpoints/{id}/prove:
    post: { summary: 'W002 submit challenge_response (officer)', parameters: [{ $ref: '#/components/parameters/Id' }, { $ref: '#/components/parameters/Idem' }], responses: { '200': { description: ownership_proved }, '409': { description: challenge_expired }, '422': { description: challenge_mismatch } } }
  /endpoints/{id}/policy-check:
    post: { summary: 'W002 attestation checks (reviewer): OHN-39 certificate trusted and unexpired, environment permitted, https', parameters: [{ $ref: '#/components/parameters/Id' }, { $ref: '#/components/parameters/Idem' }], responses: { '200': { description: policy_checked }, '422': { description: policy_refused with checks } } }
  /endpoints/{id}/enable:
    post: { summary: 'W002 enable (reviewer)', parameters: [{ $ref: '#/components/parameters/Id' }, { $ref: '#/components/parameters/Idem' }], responses: { '200': { description: enabled } } }
  /endpoints/{id}/disable:
    post: { summary: Disable endpoint with reason, parameters: [{ $ref: '#/components/parameters/Id' }, { $ref: '#/components/parameters/Idem' }], responses: { '200': { description: disabled } } }
  /endpoints/{id}/probes:
    post: { summary: Record a reachability probe for an enabled endpoint (officer), parameters: [{ $ref: '#/components/parameters/Id' }, { $ref: '#/components/parameters/Idem' }], responses: { '201': { description: recorded } } }
  /audit:
    get: { summary: Audit receipts (filter resource_id), responses: { '200': { description: ok } } }
  /outbox:
    get: { summary: 'Pending outbox events (status=all for all)', responses: { '200': { description: ok } } }
  /outbox/dispatch:
    post: { summary: Retry delivery of pending events (reviewer), parameters: [{ $ref: '#/components/parameters/Idem' }], responses: { '200': { description: '{published, pending}' } } }
  /reports/{id}:
    get: { summary: 'R001/R002/R003 with metrics and provenance', parameters: [{ name: id, in: path, required: true, schema: { enum: [OHN-07-R001, OHN-07-R002, OHN-07-R003] } }], responses: { '200': { description: ok }, '404': { description: unknown } } }
  /reports/{id}/export:
    post: { summary: 'Signed CSV/PDF export (HMAC-SHA256, OHN07_EXPORT_SIGNING_KEY); returns single-use 5-minute retrieval path', parameters: [{ name: id, in: path, required: true, schema: { type: string } }, { $ref: '#/components/parameters/Idem' }], responses: { '201': { description: ok }, '503': { description: not_configured } } }
  /exports/{token}:
    get: { summary: 'Download once (same principal); X-OHN-Export-Signature header', parameters: [{ name: token, in: path, required: true, schema: { type: string } }], responses: { '200': { description: file }, '404': { description: expired or used } } }
