openapi: 3.1.0
info:
  title: Fan identity API
  version: 1.0.0
  description: >-
    The routes of the Studio identity layer, described as they exist in the site. Every value in every request and reply is synthetic:
    no real person, sign-in or email is involved, and every schema here carries synthetic true semantics.
servers:
  - url: https://rankings.citizenofnowhere.org
    description: Production site. Synthetic data only.
paths:
  /api/studio/signin-bridge:
    post:
      operationId: signinBridge
      summary: Resolve and link a fan to a signed-in subject
      description: >-
        Verifies an OpenID Connect ID token for real (signature, issuer, audience, time, nonce) and then records the outcome and the
        fan ID link. Synthetic data only: the fan, the issuer and the token come from a demonstration session.
      x-rate-limit: 30 requests per 60 seconds per client address; the 31st gets 429 with a retry-after header.
      x-trust-boundary: >-
        The verify step is real. The write is session-scoped. When the server holds a service role key the write goes through a
        server-only function and the route hashes nothing itself; without that key it keeps the demonstration path, which anon can
        call like every Studio demo function. A production bridge takes the issuer keys from the club's own identity provider, never from a visitor.
        The route never logs, stores or echoes the token, the raw subject or the raw email, and never fetches a URL a visitor supplied.
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/SigninRequest"
      responses:
        "200":
          description: The token was checked and the outcome recorded. A refused token is still a 200 with verdict rejected.
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/SigninReply"
        "400":
          description: The body is not a JSON object, or a field is missing or malformed.
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Error"
        "413":
          description: The body is over 16 KB.
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Error"
        "429":
          description: Rate limit reached. Wait the number of seconds in the retry-after header.
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Error"
        "502":
          description: A database call failed. The error line names the function.
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Error"
  /api/studio/export/open:
    post:
      operationId: openExport
      summary: Take fans, identity links and consent history out in a documented format
      description: >-
        Returns the fans who passed the consent rule for the purpose and have a usable identity, their identity links without any key
        value, and their full consent history, as three NDJSON files plus a manifest with a SHA-256 per file. Fans left out are
        counted and never listed. Synthetic data only: every fan in the export is a generated record.
      x-rate-limit: 30 requests per 60 seconds per client address; the 31st gets 429 with a retry-after header.
      x-trust-boundary: >-
        The database function behind it is session-scoped and callable by anon, like every Studio demo function, so this proves the
        format and the consent rule, not the access control of a production export. A production export would sit behind the club's
        own sign-in. Identity key values are never selected, so they cannot leave. The route never logs a body.
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/OpenExportRequest"
      responses:
        "200":
          description: The export was built.
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/OpenExportReply"
        "400":
          description: The body is not a JSON object, the session is not a uuid, or the purpose is not a short lower-case name.
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Error"
        "413":
          description: The body is over 16 KB.
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Error"
        "429":
          description: Rate limit reached. Wait the number of seconds in the retry-after header.
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Error"
        "502":
          description: The database call failed or returned an unexpected shape.
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Error"
x-not-yet-http:
  note: >-
    Consent has no plain HTTP route. The pages call these Supabase RPC functions directly, with the anon key and a session id.
    They are listed so an integrator knows the names; they are not part of this HTTP contract.
  rpc:
    - name: studio_set_consent
      kind: write
      arguments: p_session uuid, p_fan text, p_purpose text, p_state boolean
    - name: studio_current_consent
      kind: read
      arguments: p_session uuid, p_fan text
components:
  schemas:
    Error:
      type: object
      description: Every error reply. Synthetic data only.
      required: [ok, error]
      properties:
        ok:
          type: boolean
          const: false
        error:
          type: string
    SigninRequest:
      type: object
      description: Synthetic data only. The session and the issuer are demonstration records.
      required: [session, issuer_id, id_token]
      properties:
        session:
          type: string
          format: uuid
        issuer_id:
          type: string
          description: The id of an issuer registered for this session. Up to 64 characters are used.
        id_token:
          type: string
          description: An OpenID Connect ID token, up to 16 KB for the whole body.
        nonce:
          type: string
          maxLength: 256
    OpenExportRequest:
      type: object
      description: Synthetic data only.
      required: [session, purpose]
      properties:
        session:
          type: string
          format: uuid
        purpose:
          type: string
          pattern: "^[a-z_]{1,40}$"
    OpenExportReply:
      type: object
      description: >-
        Synthetic data only. Each file is NDJSON text, one JSON object per line, a newline after every line. The manifest is
        described by the JSON Schema at /studio/schemas/fan-export-manifest-1.0.0.json, and each row type by its own schema file
        in the same folder.
      required: [ok, files, manifest]
      properties:
        ok:
          type: boolean
          const: true
        files:
          type: object
          required: [fans.ndjson, identity_links.ndjson, consent_records.ndjson]
          properties:
            fans.ndjson:
              type: string
            identity_links.ndjson:
              type: string
            consent_records.ndjson:
              type: string
        manifest:
          type: object
          description: Matches fan-export-manifest-1.0.0.json. Carries synthetic true.
          required: [schema_version, exported_at, purpose, row_counts, sha256, counts, synthetic]
          properties:
            schema_version:
              type: string
              const: 1.0.0
            exported_at:
              type: string
              format: date-time
            purpose:
              type: string
            row_counts:
              type: object
            sha256:
              type: object
            counts:
              type: object
            synthetic:
              type: boolean
              const: true
    TraceStep:
      type: object
      required: [step, detail]
      properties:
        step:
          type: string
        detail:
          type: string
    SigninReply:
      type: object
      description: Synthetic data only. The outcome of one sign-in check.
      required: [ok, verdict, write_path, salt_version, trace]
      properties:
        ok:
          type: boolean
          const: true
        verdict:
          type: string
          enum: [accepted, rejected]
        reason:
          type: string
          description: Present when the verdict is rejected.
        fan_id:
          type: [string, "null"]
        link_action:
          type: [string, "null"]
        method:
          type: [string, "null"]
        match_confidence:
          type: [number, string, "null"]
        tier_before:
          type: [string, "null"]
        tier_after:
          type: [string, "null"]
        row_id:
          type: [string, "null"]
        write_path:
          type: string
          enum: [server_role, demo_anon]
          description: Which write ran. server_role means a server-only function hashed the subject with the club's own salt; demo_anon is the demonstration path.
        salt_version:
          type: integer
          minimum: 0
          description: The salt version used for the subject hash. 0 on the demonstration path.
        trace:
          type: array
          items:
            $ref: "#/components/schemas/TraceStep"
