# openapi/coordinator.yaml

```yaml
# This is the source of truth for Coordinator endpoints we have decided are public.
# It is NOT auto-generated; update it when endpoint contracts change.
openapi: 3.0.0
info:
  title: Arcade Coordinator API
  description: Public API surface of the Arcade Coordinator service.
  contact:
    name: Arcade
    url: https://arcade.dev
    email: contact@arcade.dev
  license:
    name: Proprietary
    url: https://arcade.dev/license
  version: 0.1.0

servers:
  - url: https://cloud.arcade.dev

tags:
  - name: Audit Logs
    description: Query audit log events for organizations and projects.
  - name: Projects
    description: Create and manage projects within an organization.
  - name: User Sources
    description: Create and manage project-bound OIDC user sources.
  - name: RBAC
    description: |
      Role-based access control. Browse the roles catalog, read the caller's
      effective permissions, list org/project members, and assign, change, or
      revoke org-level and project-level roles.

      These routes are gated by the per-organization `operator-authz` feature
      flag. While the flag is OFF for an organization, every route below except
      `GET /capabilities` is absent and returns `404`.

paths:
  /api/v1/orgs/{organization_id}/audit_logs:
    get:
      security:
        - Bearer: []
      tags:
        - Audit Logs
      summary: List organization audit logs
      operationId: audit-logs-list-org
      description: |
        Query audit logs for an organization, newest first, paginated by offset.

        Optionally filter by action, source, resource type, resource ID, and time
        range. The reported total counts the entries the filters select, not the page.
      parameters:
        - $ref: '#/components/parameters/OrganizationId'
        - $ref: '#/components/parameters/ActionFilter'
        - $ref: '#/components/parameters/SourceFilter'
        - $ref: '#/components/parameters/ResourceTypeFilter'
        - $ref: '#/components/parameters/ResourceIdFilter'
        - $ref: '#/components/parameters/StartTime'
        - $ref: '#/components/parameters/EndTime'
        - $ref: '#/components/parameters/Offset'
        - $ref: '#/components/parameters/Limit'
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/AuditLogListEnvelope'
        '400':
          description: Bad Request
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '401':
          description: Unauthorized
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '403':
          description: Forbidden -- caller is not a member of the organization
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '429':
          description: Rate limit exceeded (100 req / 60 s)
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'

  /api/v1/orgs/{organization_id}/projects/{project_id}/audit_logs:
    get:
      security:
        - Bearer: []
      tags:
        - Audit Logs
      summary: List project audit logs
      operationId: audit-logs-list-project
      description: |
        Query audit logs for a specific project, newest first, paginated by offset.

        Optionally filter by action, source, resource type, resource ID, and time
        range. The reported total counts the entries the filters select, not the page.
      parameters:
        - $ref: '#/components/parameters/OrganizationId'
        - $ref: '#/components/parameters/ProjectId'
        - $ref: '#/components/parameters/ActionFilter'
        - $ref: '#/components/parameters/SourceFilter'
        - $ref: '#/components/parameters/ResourceTypeFilter'
        - $ref: '#/components/parameters/ResourceIdFilter'
        - $ref: '#/components/parameters/StartTime'
        - $ref: '#/components/parameters/EndTime'
        - $ref: '#/components/parameters/Offset'
        - $ref: '#/components/parameters/Limit'
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/AuditLogListEnvelope'
        '400':
          description: Bad Request
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '401':
          description: Unauthorized
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '403':
          description: Forbidden -- caller is not a member of the organization or project
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '429':
          description: Rate limit exceeded (100 req / 60 s)
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'

  # ── Projects ────────────────────────────────────────────────────────────────
  /api/v1/orgs/{organization_id}/projects:
    get:
      security:
        - Bearer: []
      tags:
        - Projects
      summary: List all projects
      operationId: projects-list
      description: |
        Get all projects the caller has access to within an organization.
      parameters:
        - $ref: '#/components/parameters/OrganizationId'
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ListProjectsResponse'
        '400':
          description: Bad Request
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '401':
          description: Unauthorized
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '403':
          description: Forbidden -- caller is not a member of the organization
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'

    post:
      security:
        - Bearer: []
      tags:
        - Projects
      summary: Create a new project
      operationId: projects-create
      description: |
        Create a new project within an organization.
      parameters:
        - $ref: '#/components/parameters/OrganizationId'
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/CreateProjectInput'
      responses:
        '200':
          description: Created
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ProjectResponseEnvelope'
        '400':
          description: Bad Request
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '401':
          description: Unauthorized
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '403':
          description: Forbidden -- caller is not an organization owner
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'

  /api/v1/orgs/{organization_id}/projects/{project_id}:
    get:
      security:
        - Bearer: []
      tags:
        - Projects
      summary: Get project details
      operationId: projects-get
      description: |
        Get details for a specific project.
      parameters:
        - $ref: '#/components/parameters/OrganizationId'
        - $ref: '#/components/parameters/ProjectId'
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ProjectResponseEnvelope'
        '400':
          description: Bad Request
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '401':
          description: Unauthorized
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '403':
          description: Forbidden -- caller is not a member of the project
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '404':
          description: Not Found -- project not found
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'

    put:
      security:
        - Bearer: []
      tags:
        - Projects
      summary: Update project
      operationId: projects-update
      description: |
        Update project details (name, description, image, color).
      parameters:
        - $ref: '#/components/parameters/OrganizationId'
        - $ref: '#/components/parameters/ProjectId'
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/UpdateProjectInput'
      responses:
        '200':
          description: Updated
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ProjectResponseEnvelope'
        '400':
          description: Bad Request
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '401':
          description: Unauthorized
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '403':
          description: Forbidden -- caller is not a member of the project
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '404':
          description: Not Found -- project not found
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'

    delete:
      security:
        - Bearer: []
      tags:
        - Projects
      summary: Delete project
      operationId: projects-delete
      description: |
        Delete a project from the organization.
      parameters:
        - $ref: '#/components/parameters/OrganizationId'
        - $ref: '#/components/parameters/ProjectId'
      responses:
        '200':
          description: Deleted
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/SuccessEnvelope'
        '400':
          description: Bad Request
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '401':
          description: Unauthorized
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '403':
          description: Forbidden -- caller is not an organization owner
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '404':
          description: Not Found -- project not found
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'

  /api/v1/orgs/{organization_id}/projects/{project_id}/members:
    get:
      security:
        - Bearer: []
      tags:
        - Projects
      summary: List project members
      operationId: projects-members-list
      description: |
        List all members of a project.
      parameters:
        - $ref: '#/components/parameters/OrganizationId'
        - $ref: '#/components/parameters/ProjectId'
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ListProjectMembersResponse'
        '400':
          description: Bad Request
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '401':
          description: Unauthorized
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '403':
          description: Forbidden -- caller is not a member of the project
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'

    post:
      security:
        - Bearer: []
      tags:
        - RBAC
      summary: Assign an org member to this project at a role
      operationId: rbac-project-members-assign
      description: |
        Directly assign an existing organization member to this project at a
        project-level role. Requires the `members_update` project permission.
      parameters:
        - $ref: '#/components/parameters/OrganizationId'
        - $ref: '#/components/parameters/ProjectId'
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/MemberAssignRequest'
      responses:
        '201':
          description: Created -- the member was assigned to the project.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/MemberRowEnvelope'
        '401':
          description: Unauthorized
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '403':
          description: Forbidden -- caller lacks `members_update` on the project
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '404':
          description: Not Found -- the `operator-authz` flag is OFF, or the org/project is not visible to the caller
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '422':
          description: Unprocessable Entity -- the role is not a project-level role, or `account_id` is malformed
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '503':
          description: Service Unavailable -- the policy store could not be reached
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'

  /api/v1/orgs/{organization_id}/projects/{project_id}/members/{account_id}:
    put:
      security:
        - Bearer: []
      tags:
        - RBAC
      summary: Change a project member's role
      operationId: rbac-project-members-change-role
      description: |
        Change the project-level role of an existing project member. Requires
        the `members_update` project permission. Re-assigning the role the member
        already holds is an idempotent success.

        Note: organization admins inherit administration over every child project
        and hold no per-project role binding, so their project role cannot be
        changed through this endpoint.
      parameters:
        - $ref: '#/components/parameters/OrganizationId'
        - $ref: '#/components/parameters/ProjectId'
        - $ref: '#/components/parameters/AccountId'
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/MemberRoleUpdateRequest'
      responses:
        '200':
          description: OK -- the member's role was changed (or already matched).
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/MemberRowEnvelope'
        '401':
          description: Unauthorized
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '403':
          description: Forbidden -- caller lacks `members_update` on the project
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '404':
          description: Not Found -- the `operator-authz` flag is OFF, or the org/project/member is not visible to the caller
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '422':
          description: Unprocessable Entity -- the role is not a project-level role
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '503':
          description: Service Unavailable -- the policy store (OpenFGA) could not be reached
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'

  /api/v1/orgs/{organization_id}/projects/{project_id}/assignable-members:
    get:
      security:
        - Bearer: []
      tags:
        - RBAC
      summary: List org members assignable to this project
      operationId: rbac-project-assignable-members-list
      description: |
        List organization members who are eligible to be assigned to this
        project. Requires the `members_update` project permission.
      parameters:
        - $ref: '#/components/parameters/OrganizationId'
        - $ref: '#/components/parameters/ProjectId'
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/AssignableAccountsResponseEnvelope'
        '401':
          description: Unauthorized
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '403':
          description: Forbidden -- caller lacks `members_update` on the project
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '404':
          description: Not Found -- the `operator-authz` flag is OFF, or the org/project is not visible to the caller
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'

  /api/v1/orgs/{organization_id}/projects/{project_id}/members/{member_account_id}:
    delete:
      security:
        - Bearer: []
      tags:
        - Projects
      summary: Remove a member from a project
      operationId: projects-members-remove
      description: |
        Remove a member from a project. Organization owners can remove members
        even if they are not currently members of the project.
      parameters:
        - $ref: '#/components/parameters/OrganizationId'
        - $ref: '#/components/parameters/ProjectId'
        - in: path
          name: member_account_id
          required: true
          description: Account ID of the member to remove.
          schema:
            type: string
            format: uuid
      responses:
        '204':
          description: No Content
        '400':
          description: Bad Request
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '401':
          description: Unauthorized
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '403':
          description: Forbidden -- caller cannot remove this member
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'

  # ── User Sources ────────────────────────────────────────────────────────────
  /api/v1/orgs/{organization_id}/projects/{project_id}/user_sources:
    get:
      security:
        - Bearer: []
      tags:
        - User Sources
      summary: List user sources
      operationId: user-sources-list
      description: |
        List user sources visible in this project, newest first, paginated by
        offset. `total_count` is the full matching set before pagination.

        `client_secret` is never returned.

        Requires the `user_sources_read` project permission.
      parameters:
        - $ref: '#/components/parameters/OrganizationId'
        - $ref: '#/components/parameters/ProjectId'
        - name: limit
          in: query
          required: false
          description: Results per page.
          schema:
            type: integer
            default: 100
            minimum: 1
            maximum: 500
        - name: offset
          in: query
          required: false
          description: Rows to skip before the page.
          schema:
            type: integer
            default: 0
            minimum: 0
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/UserSourceListEnvelope'
        '401':
          description: Unauthorized
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '403':
          description: Forbidden -- caller lacks `user_sources_read` on the project
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '404':
          description: Not Found -- organization or project is not visible to the caller
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '422':
          description: Unprocessable Entity -- `limit` or `offset` is out of range
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '503':
          description: Service Unavailable -- the policy store (OpenFGA) could not be reached
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'

    post:
      security:
        - Bearer: []
      tags:
        - User Sources
      summary: Create a user source
      operationId: user-sources-create
      description: |
        Create a project-bound user source. The new record always has
        `binding_type: project`. Its `id` is a `us_`-prefixed KSUID; pass that
        id to Engine gateway create/update as `user_source_id` when
        `auth_type=user_source`.

        The Coordinator probes the issuer's OIDC discovery document and JWKS
        before persisting. A malformed, unreachable, or self-inconsistent
        issuer is rejected with `422`. The stored issuer string is the
        caller's exact input (no trailing-slash normalization) so it can be
        exact-matched against the `iss` claim on upstream ID tokens.

        `scopes` must include `openid`. Duplicates are collapsed on write
        while preserving first occurrence. `client_secret` is stored
        encrypted and is never returned.

        Requires the `user_sources_create` project permission.
      parameters:
        - $ref: '#/components/parameters/OrganizationId'
        - $ref: '#/components/parameters/ProjectId'
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/UserSourceCreateRequest'
      responses:
        '201':
          description: Created
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/UserSourceEnvelope'
        '400':
          description: Bad Request
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '401':
          description: Unauthorized
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '403':
          description: Forbidden -- caller lacks `user_sources_create` on the project
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '404':
          description: Not Found -- organization or project is not visible to the caller
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '422':
          description: |
            Unprocessable Entity -- the body failed validation, `scopes` omitted
            `openid`, or the issuer failed OIDC discovery / JWKS checks.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '503':
          description: Service Unavailable -- the policy store (OpenFGA) could not be reached
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'

  /api/v1/orgs/{organization_id}/projects/{project_id}/user_sources/{user_source_id}:
    get:
      security:
        - Bearer: []
      tags:
        - User Sources
      summary: Get a user source
      operationId: user-sources-get
      description: |
        Return one project-bound user source. `client_secret` is never returned.

        Organization-bound records (`binding_type: organization`) return `422`,
        not `404`, with guidance to use the organization scope. Platform-internal
        (`static`) records return `404`. Unknown ids return `404`. A malformed
        id (missing or wrong `us_` prefix, or not a KSUID) returns `422`.

        Requires the `user_sources_read` project permission.
      parameters:
        - $ref: '#/components/parameters/OrganizationId'
        - $ref: '#/components/parameters/ProjectId'
        - $ref: '#/components/parameters/UserSourceId'
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/UserSourceEnvelope'
        '401':
          description: Unauthorized
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '403':
          description: Forbidden -- caller lacks `user_sources_read` on the project
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '404':
          description: Not Found -- user source is unknown, static-bound, or not visible
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '422':
          description: |
            Unprocessable Entity -- `user_source_id` is malformed, or the record
            is organization-bound (use the organization scope).
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '503':
          description: Service Unavailable -- the policy store (OpenFGA) could not be reached
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'

    put:
      security:
        - Bearer: []
      tags:
        - User Sources
      summary: Replace a user source
      operationId: user-sources-replace
      description: |
        Full replace of a project-bound user source. `client_secret` is
        required. The Coordinator re-validates the issuer's OIDC discovery
        document and JWKS. `client_secret` is never returned.

        Organization-bound records return `422`. Static records return `404`.

        Requires the `user_sources_update` project permission.
      parameters:
        - $ref: '#/components/parameters/OrganizationId'
        - $ref: '#/components/parameters/ProjectId'
        - $ref: '#/components/parameters/UserSourceId'
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/UserSourceReplaceRequest'
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/UserSourceEnvelope'
        '400':
          description: Bad Request
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '401':
          description: Unauthorized
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '403':
          description: Forbidden -- caller lacks `user_sources_update` on the project
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '404':
          description: Not Found -- user source is unknown, static-bound, or not visible
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '422':
          description: |
            Unprocessable Entity -- the body failed validation, `client_secret`
            is missing, the issuer failed OIDC checks, `user_source_id` is
            malformed, or the record is organization-bound.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '503':
          description: Service Unavailable -- the policy store (OpenFGA) could not be reached
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'

    patch:
      security:
        - Bearer: []
      tags:
        - User Sources
      summary: Patch a user source
      operationId: user-sources-patch
      description: |
        Partial update of a project-bound user source. Omitted fields keep
        their stored values.

        `client_secret`: omit to keep the stored secret; send a new value to
        rotate; send explicit `null` to clear. `issuer` must not be `null`.
        `description` sent as `null` is stored as an empty string.

        The Coordinator re-validates OIDC discovery and JWKS only when
        `issuer` or `scopes` actually change, so a transient IdP outage does
        not block a status flip. `client_secret` is never returned.

        Organization-bound records return `422`. Static records return `404`.

        Requires the `user_sources_update` project permission.
      parameters:
        - $ref: '#/components/parameters/OrganizationId'
        - $ref: '#/components/parameters/ProjectId'
        - $ref: '#/components/parameters/UserSourceId'
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/UserSourcePatchRequest'
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/UserSourceEnvelope'
        '400':
          description: Bad Request -- `issuer` was sent as `null`
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '401':
          description: Unauthorized
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '403':
          description: Forbidden -- caller lacks `user_sources_update` on the project
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '404':
          description: Not Found -- user source is unknown, static-bound, or not visible
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '422':
          description: |
            Unprocessable Entity -- the body failed validation, the issuer
            failed OIDC checks, `user_source_id` is malformed, or the record
            is organization-bound.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '503':
          description: Service Unavailable -- the policy store (OpenFGA) could not be reached
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'

    delete:
      security:
        - Bearer: []
      tags:
        - User Sources
      summary: Delete a user source
      operationId: user-sources-delete
      description: |
        Permanently delete a project-bound user source. Returns `204` with no
        body on success.

        Organization-bound records return `422`. Static records return `404`.

        Requires the `user_sources_delete` project permission.
      parameters:
        - $ref: '#/components/parameters/OrganizationId'
        - $ref: '#/components/parameters/ProjectId'
        - $ref: '#/components/parameters/UserSourceId'
      responses:
        '204':
          description: No Content -- the user source was deleted.
        '401':
          description: Unauthorized
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '403':
          description: Forbidden -- caller lacks `user_sources_delete` on the project
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '404':
          description: Not Found -- user source is unknown, static-bound, or not visible
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '422':
          description: |
            Unprocessable Entity -- `user_source_id` is malformed, or the
            record is organization-bound.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '503':
          description: Service Unavailable -- the policy store (OpenFGA) could not be reached
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'

  # ── RBAC: capabilities, roles catalog, org membership & roles ─────────────────
  /api/v1/orgs/{organization_id}/capabilities:
    get:
      security:
        - Bearer: []
      tags:
        - RBAC
      summary: Get the caller's org-level capabilities
      operationId: rbac-org-capabilities
      description: |
        Report the caller's effective org-level permissions plus the billing
        capability block. Mounted regardless of the `operator-authz` flag: when
        the flag is OFF the response carries `authz.enabled = false` with no
        permissions, so a client can detect the flag state without inferring it
        from a missing route.
      parameters:
        - $ref: '#/components/parameters/OrganizationId'
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/OrgCapabilities'
        '401':
          description: Unauthorized
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '403':
          description: Forbidden -- caller is not a member of the organization
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '404':
          description: Not Found -- caller is not a member of the organization (flag-OFF)
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'

  /api/v1/orgs/{organization_id}/authz/roles:
    get:
      security:
        - Bearer: []
      tags:
        - RBAC
      summary: List roles available in the organization
      operationId: rbac-roles-list
      description: |
        Return the roles catalog for the organization -- every role key with its
        display name, level, and the permission bundle it grants. Use a role key
        from this catalog as the `role` field when assigning or changing a role.
      parameters:
        - $ref: '#/components/parameters/OrganizationId'
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/RolesResponseEnvelope'
        '401':
          description: Unauthorized
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '403':
          description: Forbidden -- caller is not a member of the organization
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '404':
          description: Not Found -- the `operator-authz` flag is OFF for this organization
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'

  /api/v1/orgs/{organization_id}/members:
    get:
      security:
        - Bearer: []
      tags:
        - RBAC
      summary: List organization members with their org-level role
      operationId: rbac-org-members-list
      description: |
        The organization member directory: every account in the org with its
        org-level role (or none). Requires the `members_read` org permission.
        Results are limit/offset paginated; `total_count` reports the org-wide
        member count regardless of the page requested.
      parameters:
        - $ref: '#/components/parameters/OrganizationId'
        - name: limit
          in: query
          required: false
          description: Results per page.
          schema:
            type: integer
            default: 100
            minimum: 1
            maximum: 500
        - name: offset
          in: query
          required: false
          description: Rows to skip before the page.
          schema:
            type: integer
            default: 0
            minimum: 0
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/OrgMembersResponseEnvelope'
        '401':
          description: Unauthorized
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '403':
          description: Forbidden -- caller lacks `members_read` on the organization
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '404':
          description: Not Found -- the `operator-authz` flag is OFF, or the caller is not a member of the organization
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '422':
          description: Unprocessable Entity -- `limit` or `offset` is out of range
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'

    post:
      security:
        - Bearer: []
      tags:
        - RBAC
      summary: Assign an org-level role
      operationId: rbac-org-members-assign
      description: |
        Assign an org-level role (e.g. `org_admin`) to an organization member.
        Requires the `members_update` org permission.
      parameters:
        - $ref: '#/components/parameters/OrganizationId'
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/MemberAssignRequest'
      responses:
        '201':
          description: Created -- the org-level role was assigned.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/OrgMemberRowEnvelope'
        '401':
          description: Unauthorized
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '403':
          description: Forbidden -- caller lacks `members_update` on the organization
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '404':
          description: Not Found -- the `operator-authz` flag is OFF, or the caller is not a member of the organization
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '422':
          description: Unprocessable Entity -- the role is not an org-level role, or `account_id` is malformed
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '503':
          description: Service Unavailable -- the policy store (OpenFGA) could not be reached
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'

  /api/v1/orgs/{organization_id}/members/{account_id}:
    put:
      security:
        - Bearer: []
      tags:
        - RBAC
      summary: Change an org-level role
      operationId: rbac-org-members-change-role
      description: |
        Change the org-level role of an organization member. Requires the
        `members_update` org permission. Re-assigning the role the member already
        holds is an idempotent success.
      parameters:
        - $ref: '#/components/parameters/OrganizationId'
        - $ref: '#/components/parameters/AccountId'
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/MemberRoleUpdateRequest'
      responses:
        '200':
          description: OK -- the member's org-level role was changed (or already matched).
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/OrgMemberRowEnvelope'
        '401':
          description: Unauthorized
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '403':
          description: Forbidden -- caller lacks `members_update` on the organization
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '404':
          description: Not Found -- the `operator-authz` flag is OFF, or the org/member is not visible to the caller
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '422':
          description: Unprocessable Entity -- the role is not an org-level role
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '503':
          description: Service Unavailable -- the policy store (OpenFGA) could not be reached
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'

    delete:
      security:
        - Bearer: []
      tags:
        - RBAC
      summary: Revoke an org-level role binding
      operationId: rbac-org-members-remove
      description: |
        Revoke the caller-specified account's org-level role binding. Requires
        the `members_delete` org permission. This removes the org-level role only
        and does not affect legacy organization membership.

        Removal is fail-closed: if the policy store cannot be reached the binding
        is left intact and the request fails with `503` rather than reporting a
        removal that did not happen.
      parameters:
        - $ref: '#/components/parameters/OrganizationId'
        - $ref: '#/components/parameters/AccountId'
      responses:
        '204':
          description: No Content -- the org-level role binding was revoked.
        '401':
          description: Unauthorized
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '403':
          description: Forbidden -- caller lacks `members_delete` on the organization
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '404':
          description: Not Found -- the `operator-authz` flag is OFF, or the org/member is not visible to the caller
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '503':
          description: Service Unavailable -- the policy store (OpenFGA) could not be reached
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'

components:
  securitySchemes:
    Bearer:
      description: 'Enter your API key or API token in the format: Bearer <token>'
      type: apiKey
      name: Authorization
      in: header

  parameters:
    OrganizationId:
      name: organization_id
      in: path
      required: true
      description: Organization ID (UUID).
      schema:
        type: string
        format: uuid

    ProjectId:
      name: project_id
      in: path
      required: true
      description: Project ID (UUID).
      schema:
        type: string
        format: uuid

    AccountId:
      name: account_id
      in: path
      required: true
      description: Account ID (UUID) of the member whose role is being changed or revoked.
      schema:
        type: string
        format: uuid

    UserSourceId:
      name: user_source_id
      in: path
      required: true
      description: |
        User source id. A `us_`-prefixed KSUID (for example
        `us_0000GfkSkVjL1s3EM2wUfhjxv7B`). The Engine validates the same
        prefix when the id is passed as `user_source_id` on a gateway.
      schema:
        type: string
        pattern: '^us_[0-9A-Za-z]{27}$'
        example: us_0000GfkSkVjL1s3EM2wUfhjxv7B

    ActionFilter:
      name: action
      in: query
      required: false
      description: |
        Filter by audit action.
        Known values: `AUDIT_ACTION_CREATED`, `AUDIT_ACTION_UPDATED`,
        `AUDIT_ACTION_DELETED`, `AUDIT_ACTION_DISABLED`.
      schema:
        type: string

    SourceFilter:
      name: source
      in: query
      required: false
      description: |
        Filter by audit source.
        Known values: `AUDIT_SOURCE_API`, `AUDIT_SOURCE_DASHBOARD`,
        `AUDIT_SOURCE_CLI`, `AUDIT_SOURCE_SDK`, `AUDIT_SOURCE_SYSTEM`.
      schema:
        type: string

    ResourceTypeFilter:
      name: resource_type
      in: query
      required: false
      description: |
        Filter by resource type.
        Examples: `gateway`, `secret`, `worker`, `api_key`, `project`, etc.
      schema:
        type: string

    ResourceIdFilter:
      name: resource_id
      in: query
      required: false
      description: Filter by the resource's unique identifier.
      schema:
        type: string

    StartTime:
      name: start_time
      in: query
      required: false
      description: Inclusive lower bound for event timestamp (ISO 8601).
      schema:
        type: string
        format: date-time

    EndTime:
      name: end_time
      in: query
      required: false
      description: Exclusive upper bound for event timestamp (ISO 8601).
      schema:
        type: string
        format: date-time

    Offset:
      name: offset
      in: query
      required: false
      description: How many matching entries to skip before this page.
      schema:
        type: integer
        minimum: 0
        default: 0

    Limit:
      name: limit
      in: query
      required: false
      description: Number of results per page (1--100).
      schema:
        type: integer
        default: 50
        minimum: 1
        maximum: 100

  schemas:
    # ── Response envelope ────────────────────────────────────────────────
    AuditLogListEnvelope:
      type: object
      required:
        - code
        - msg
        - data
      properties:
        code:
          type: integer
          example: 200
        msg:
          type: string
          example: Request successful
        data:
          $ref: '#/components/schemas/AuditLogListResponse'

    ErrorEnvelope:
      type: object
      required:
        - code
        - msg
      properties:
        code:
          type: integer
          example: 400
        msg:
          type: string
          example: Bad Request
        data:
          description: Optional error details.

    # ── Payload schemas ──────────────────────────────────────────────────
    AuditLogListResponse:
      type: object
      required:
        - items
        - total_count
        - limit
        - offset
      properties:
        items:
          type: array
          items:
            $ref: '#/components/schemas/AuditLogResponse'
        total_count:
          type: integer
          description: >-
            Total entries matching the filters, across every page. Add the page's
            limit to its offset until that reaches this value to walk the set.
        limit:
          type: integer
          description: Page size the request asked for.
        offset:
          type: integer
          description: Offset the request asked for.

    AuditLogResponse:
      type: object
      required:
        - id
        - event_id
        - timestamp
        - action
        - source
        - display_name
        - organization_id
        - principal_id
        - principal_type
        - created_time
      properties:
        id:
          type: string
          format: uuid
          description: Internal audit log record ID.
        event_id:
          type: string
          format: uuid
          description: Unique event ID assigned at emission time.
        timestamp:
          type: string
          format: date-time
          description: When the audited action occurred.
        client_ip:
          type: string
          nullable: true
          description: IP address of the caller, if available.
        action:
          type: string
          description: 'Audit action (e.g. `AUDIT_ACTION_CREATED`).'
        source:
          type: string
          description: 'Origin of the action (e.g. `AUDIT_SOURCE_API`).'
        display_name:
          type: string
          description: Human-readable name of the caller.
        customer_id:
          type: string
          format: uuid
          nullable: true
          description: Customer ID, if applicable.
        organization_id:
          type: string
          format: uuid
          description: Organization the event belongs to.
        project_id:
          type: string
          format: uuid
          nullable: true
          description: Project the event belongs to, if scoped to a project.
        principal_id:
          type: string
          description: Account ID or API key ID of the caller.
        user_id:
          type: string
          nullable: true
          description: User ID for user-account callers.
        principal_type:
          type: string
          description: 'Type of principal (e.g. `ACCOUNT`, `API_KEY`).'
        resource_type:
          type: string
          nullable: true
          description: 'Type of the affected resource (e.g. `gateway`, `secret`).'
        resource_id:
          type: string
          nullable: true
          description: Identifier of the affected resource.
        resource_display:
          type: string
          nullable: true
          description: Human-readable name of the affected resource.
        data:
          type: object
          nullable: true
          description: |
            Structured event payload. Shape depends on the action:
            - **created**: `{"action_type": "created", "data": {...}}`
            - **updated**: `{"action_type": "updated", "original": {...}, "updated": {...}}`
            - **deleted**: `{"action_type": "deleted", "data": {...}}`

            Sensitive fields (secrets, tokens) are masked.
        created_time:
          type: string
          format: date-time
          description: When the audit log record was persisted.

    # ── Project schemas ─────────────────────────────────────────────────────
    SuccessEnvelope:
      type: object
      required:
        - code
        - msg
      properties:
        code:
          type: integer
          example: 200
        msg:
          type: string
          example: Request successful
        data:
          type: object
          nullable: true
          description: Payload representing the result of an operation. May be empty for delete/success responses.

    ProjectResponse:
      type: object
      required:
        - project_id
        - organization_id
        - name
        - is_default
        - created_at
      properties:
        project_id:
          type: string
          format: uuid
          description: Unique project identifier.
        organization_id:
          type: string
          format: uuid
          description: Organization the project belongs to.
        name:
          type: string
          description: Project name.
        description:
          type: string
          nullable: true
          description: Project description.
        image_url:
          type: string
          format: uri
          nullable: true
          description: Project image URL.
        color:
          type: string
          nullable: true
          description: Project color (hex, 6 chars).
        is_default:
          type: boolean
          description: Whether this is the caller's default project.
        created_at:
          type: string
          format: date-time
          description: When the project was created.

    ProjectResponseEnvelope:
      type: object
      required:
        - code
        - msg
        - data
      properties:
        code:
          type: integer
          example: 200
        msg:
          type: string
          example: Request successful
        data:
          $ref: '#/components/schemas/ProjectResponse'

    ListProjectsResponse:
      type: object
      required:
        - items
        - count
      properties:
        items:
          type: array
          items:
            $ref: '#/components/schemas/ProjectResponse'
        count:
          type: integer
          description: Total number of items.
        total_count:
          type: integer
          description: Total number of items.

    CreateProjectInput:
      type: object
      required:
        - name
      properties:
        name:
          type: string
          minLength: 1
          maxLength: 255
          description: Project name.
        description:
          type: string
          maxLength: 1000
          nullable: true
          description: Project description.
        image_url:
          type: string
          format: uri
          nullable: true
          description: Project image URL.
        color:
          type: string
          nullable: true
          description: Project color (hex, 6 characters).

    UpdateProjectInput:
      type: object
      properties:
        name:
          type: string
          minLength: 1
          maxLength: 255
          nullable: true
          description: Project name.
        description:
          type: string
          maxLength: 1000
          nullable: true
          description: Project description.
        image_url:
          type: string
          format: uri
          nullable: true
          description: Project image URL.
        color:
          type: string
          nullable: true
          description: Project color (hex, 6 characters).

    ProjectMemberResponse:
      type: object
      required:
        - account_id
        - email
        - full_name
        - actions
      properties:
        account_id:
          type: string
          format: uuid
          description: Account ID of the member.
        email:
          type: string
          format: email
          description: Email address of the member.
        full_name:
          type: string
          description: Full name of the member.
        preferred_name:
          type: string
          nullable: true
          description: Preferred name of the member.
        actions:
          type: object
          properties:
            can_remove:
              type: boolean
              description: Whether the caller can remove this member.
            cannot_remove_reason:
              type: string
              nullable: true
              description: Reason why the member cannot be removed.

    ProjectMembersViewerResponse:
      type: object
      properties:
        can_leave:
          type: boolean
          description: Whether the caller can leave the project.
        cannot_leave_reason:
          type: string
          nullable: true
          description: Reason why the caller cannot leave.
        can_remove_members:
          type: boolean
          description: Whether the caller can remove other members.

    ListProjectMembersResponse:
      type: object
      required:
        - items
        - count
        - viewer
      properties:
        items:
          type: array
          items:
            $ref: '#/components/schemas/ProjectMemberResponse'
        count:
          type: integer
          description: Total number of members.
        total_count:
          type: integer
          description: Total number of items.
        viewer:
          $ref: '#/components/schemas/ProjectMembersViewerResponse'

    # ── RBAC schemas ─────────────────────────────────────────────────────────
    RoleLevel:
      type: string
      enum:
        - org
        - project
      description: The object level a role applies to.

    ContainerRef:
      type: object
      required:
        - type
        - id
      properties:
        type:
          $ref: '#/components/schemas/RoleLevel'
        id:
          type: string
          description: ID of the org or project the role is held in.
        name:
          type: string
          nullable: true
          description: Display name of the container, if available.

    RoleRef:
      type: object
      required:
        - key
        - display_name
        - level
        - builtin
        - container
      properties:
        key:
          type: string
          description: 'Opaque role key (e.g. `org_admin`, `project_admin`, `project_member`).'
        display_name:
          type: string
          description: Human-readable role name.
        level:
          $ref: '#/components/schemas/RoleLevel'
        builtin:
          type: boolean
          description: Whether this is a built-in role (not a custom org role).
        container:
          $ref: '#/components/schemas/ContainerRef'

    ScopedPermission:
      type: object
      required:
        - token
        - level
      properties:
        token:
          type: string
          description: |
            Permission token held by the role, e.g. `members_update`,
            `audit_logs_read`, `tools_execute`. The same token can exist at both
            levels, so `level` disambiguates.
        level:
          $ref: '#/components/schemas/RoleLevel'

    RoleCatalogEntry:
      type: object
      required:
        - key
        - display_name
        - level
        - builtin
        - editable
        - invitable
        - permissions
      properties:
        key:
          type: string
          description: Opaque role key.
        display_name:
          type: string
          description: Human-readable role name.
        level:
          $ref: '#/components/schemas/RoleLevel'
        builtin:
          type: boolean
          description: Whether this is a built-in role.
        editable:
          type: boolean
          description: Whether the role's permission bundle can be edited.
        invitable:
          type: boolean
          description: Whether members can be invited directly at this role.
        permissions:
          type: array
          items:
            $ref: '#/components/schemas/ScopedPermission'

    RolesResponse:
      type: object
      required:
        - roles
      properties:
        roles:
          type: array
          items:
            $ref: '#/components/schemas/RoleCatalogEntry'

    RolesResponseEnvelope:
      type: object
      required:
        - code
        - msg
        - data
      properties:
        code:
          type: integer
          example: 200
        msg:
          type: string
          example: Request successful
        data:
          $ref: '#/components/schemas/RolesResponse'

    MemberActions:
      type: object
      required:
        - can_remove
        - can_change_role
      properties:
        can_remove:
          type: boolean
          description: Whether the caller can remove this member.
        cannot_remove_reason:
          type: string
          nullable: true
          description: Reason why the member cannot be removed, if any.
        can_change_role:
          type: boolean
          description: Whether the caller can change this member's role.

    MemberRow:
      type: object
      required:
        - account_id
        - email
        - role
        - actions
      properties:
        account_id:
          type: string
          format: uuid
          description: Account ID of the member.
        email:
          type: string
          format: email
          description: Email address of the member.
        full_name:
          type: string
          nullable: true
          description: Full name of the member.
        preferred_name:
          type: string
          nullable: true
          description: Preferred name of the member.
        role:
          $ref: '#/components/schemas/RoleRef'
        actions:
          $ref: '#/components/schemas/MemberActions'

    MemberRowEnvelope:
      type: object
      required:
        - code
        - msg
        - data
      properties:
        code:
          type: integer
          example: 200
        msg:
          type: string
          example: Request successful
        data:
          $ref: '#/components/schemas/MemberRow'

    OrgMemberRow:
      type: object
      required:
        - account_id
        - email
      properties:
        account_id:
          type: string
          format: uuid
          description: Account ID of the member.
        email:
          type: string
          format: email
          description: Email address of the member.
        name:
          type: string
          nullable: true
          description: Full name of the member.
        org_role:
          allOf:
            - $ref: '#/components/schemas/RoleRef'
          nullable: true
          description: The member's org-level role, or null if they hold none.
        projects:
          type: array
          items:
            $ref: '#/components/schemas/MemberProject'
          description: >-
            Projects the member effectively covers in this org. An Org Admin covers
            every project; everyone else covers only projects they hold a direct
            binding on. Populated on the directory read.

    MemberProject:
      type: object
      required:
        - project_id
        - name
      properties:
        project_id:
          type: string
          format: uuid
          description: ID of the covered project.
        name:
          type: string
          description: Display name of the covered project.

    OrgMemberRowEnvelope:
      type: object
      required:
        - code
        - msg
        - data
      properties:
        code:
          type: integer
          example: 200
        msg:
          type: string
          example: Request successful
        data:
          $ref: '#/components/schemas/OrgMemberRow'

    OrgMembersResponse:
      type: object
      required:
        - items
        - total_count
        - limit
        - offset
        - can_invite
      properties:
        items:
          type: array
          items:
            $ref: '#/components/schemas/OrgMemberRow'
        total_count:
          type: integer
          description: Total number of members in the organization.
        limit:
          type: integer
          description: Page size the request asked for.
        offset:
          type: integer
          description: Offset the request asked for.
        can_invite:
          type: boolean
          description: >-
            Whether the caller may add a member to the organization (mirrors the
            POST /orgs/{organization_id}/members gate).

    OrgMembersResponseEnvelope:
      type: object
      required:
        - code
        - msg
        - data
      properties:
        code:
          type: integer
          example: 200
        msg:
          type: string
          example: Request successful
        data:
          $ref: '#/components/schemas/OrgMembersResponse'

    AssignableAccount:
      type: object
      required:
        - account_id
        - email
      properties:
        account_id:
          type: string
          format: uuid
          description: Account ID of the assignable member.
        email:
          type: string
          format: email
          description: Email address of the member.
        name:
          type: string
          nullable: true
          description: Full name of the member.

    AssignableAccountsResponse:
      type: object
      required:
        - items
        - count
        - total_count
      properties:
        items:
          type: array
          items:
            $ref: '#/components/schemas/AssignableAccount'
        count:
          type: integer
          description: Number of members in this response.
        total_count:
          type: integer
          description: Total number of assignable members.

    AssignableAccountsResponseEnvelope:
      type: object
      required:
        - code
        - msg
        - data
      properties:
        code:
          type: integer
          example: 200
        msg:
          type: string
          example: Request successful
        data:
          $ref: '#/components/schemas/AssignableAccountsResponse'

    MemberAssignRequest:
      type: object
      required:
        - account_id
        - role
      properties:
        account_id:
          type: string
          format: uuid
          description: Account ID of the org member to assign.
        role:
          type: string
          description: |
            Role key to assign. Must be a role whose catalog level matches the
            endpoint (org-level key for org endpoints, project-level key for
            project endpoints), else the request is rejected with 422.

    MemberRoleUpdateRequest:
      type: object
      required:
        - role
      properties:
        role:
          type: string
          description: |
            New role key. Must match the endpoint's level (org or project), else
            the request is rejected with 422.

    OrgAuthzCapabilities:
      type: object
      required:
        - enabled
        - permissions
      properties:
        enabled:
          type: boolean
          description: Whether operator RBAC is enabled for this organization.
        permissions:
          type: array
          description: Org-level permission tokens the caller holds.
          items:
            type: string

    BillingCapabilities:
      type: object
      required:
        - enabled
      properties:
        enabled:
          type: boolean
          description: Whether billing capabilities are enabled for the caller.

    OrgCapabilities:
      type: object
      required:
        - authz
        - billing
      properties:
        authz:
          $ref: '#/components/schemas/OrgAuthzCapabilities'
        billing:
          $ref: '#/components/schemas/BillingCapabilities'

    # ── User Source schemas ─────────────────────────────────────────────────
    UserSourceBindingType:
      type: string
      enum:
        - project
        - organization
      description: |
        Whether the record is project-scoped (mutable on this route) or
        organization-scoped (inherited; GET/PUT/PATCH/DELETE through the
        project path return `422`). Platform-internal `static` bindings
        never appear on the wire.

    UserSourceStatus:
      type: string
      enum:
        - active
        - inactive
      description: Whether the user source is active.

    UserSourceProtocol:
      type: string
      enum:
        - oidc
      description: Authentication protocol. Only OIDC is supported.

    UserSource:
      type: object
      required:
        - id
        - organization_id
        - binding_type
        - name
        - description
        - protocol
        - issuer
        - client_id
        - subject_claim
        - scopes
        - status
        - created_at
        - updated_at
      properties:
        id:
          type: string
          description: User source id (`us_`-prefixed KSUID).
          pattern: '^us_[0-9A-Za-z]{27}$'
          example: us_0000GfkSkVjL1s3EM2wUfhjxv7B
        organization_id:
          type: string
          format: uuid
          description: Owning organization.
        project_id:
          type: string
          format: uuid
          nullable: true
          description: Owning project. `null` for organization-bound records.
        binding_type:
          $ref: '#/components/schemas/UserSourceBindingType'
        name:
          type: string
          description: Human-readable name.
        description:
          type: string
          description: Freeform description. Never shown to end users.
        protocol:
          $ref: '#/components/schemas/UserSourceProtocol'
        issuer:
          type: string
          description: |
            OIDC issuer URL, stored exactly as submitted (no trailing-slash
            normalization) so it can be exact-matched against the `iss` claim.
        client_id:
          type: string
          description: OIDC client ID.
        subject_claim:
          type: string
          description: OIDC claim used as the stable subject identifier.
          example: sub
        scopes:
          type: array
          items:
            type: string
          description: OIDC scopes requested from the upstream IdP.
          example:
            - openid
            - profile
            - email
        status:
          $ref: '#/components/schemas/UserSourceStatus'
        created_at:
          type: string
          format: date-time
          description: Creation timestamp.
        updated_at:
          type: string
          format: date-time
          description: Last-updated timestamp.

    UserSourceList:
      type: object
      required:
        - items
        - total_count
        - limit
        - offset
      properties:
        items:
          type: array
          items:
            $ref: '#/components/schemas/UserSource'
        total_count:
          type: integer
          description: Total matching items across every page.
        limit:
          type: integer
          description: Page size the request asked for.
        offset:
          type: integer
          description: Offset the request asked for.

    UserSourceEnvelope:
      type: object
      required:
        - code
        - msg
        - data
      properties:
        code:
          type: integer
          example: 200
        msg:
          type: string
          example: Request successful
        data:
          $ref: '#/components/schemas/UserSource'

    UserSourceListEnvelope:
      type: object
      required:
        - code
        - msg
        - data
      properties:
        code:
          type: integer
          example: 200
        msg:
          type: string
          example: Request successful
        data:
          $ref: '#/components/schemas/UserSourceList'

    UserSourceCreateRequest:
      type: object
      required:
        - name
        - issuer
        - client_id
        - client_secret
      properties:
        name:
          type: string
          minLength: 1
          maxLength: 255
          description: Human-readable name.
        description:
          type: string
          maxLength: 4000
          default: ''
          description: Freeform description. Visible to project members, never to end users.
        protocol:
          allOf:
            - $ref: '#/components/schemas/UserSourceProtocol'
          default: oidc
        issuer:
          type: string
          minLength: 1
          maxLength: 2048
          format: uri
          description: |
            OIDC issuer URL. Stored verbatim. Must be reachable and serve a
            self-consistent discovery document and JWKS at create time.
        client_id:
          type: string
          minLength: 1
          maxLength: 2048
          description: OIDC client ID.
        client_secret:
          type: string
          minLength: 1
          maxLength: 4096
          description: |
            OIDC client secret (plaintext; stored encrypted). Never returned
            in responses.
        subject_claim:
          type: string
          minLength: 1
          maxLength: 2048
          default: sub
          description: OIDC claim used as the stable subject identifier.
        scopes:
          type: array
          minItems: 1
          maxItems: 64
          default:
            - openid
            - profile
            - email
          items:
            type: string
            minLength: 1
            maxLength: 1024
          description: |
            OIDC scopes requested from the upstream IdP. Must include
            `openid`. Tokens must not contain whitespace. Duplicates are
            collapsed on write while preserving first occurrence.
        status:
          allOf:
            - $ref: '#/components/schemas/UserSourceStatus'
          default: active

    UserSourceReplaceRequest:
      type: object
      required:
        - name
        - issuer
        - client_id
        - client_secret
      properties:
        name:
          type: string
          minLength: 1
          maxLength: 255
          description: Human-readable name.
        description:
          type: string
          maxLength: 4000
          default: ''
          description: Freeform description.
        protocol:
          allOf:
            - $ref: '#/components/schemas/UserSourceProtocol'
          default: oidc
        issuer:
          type: string
          minLength: 1
          maxLength: 2048
          format: uri
          description: OIDC issuer URL. Stored verbatim. Re-validated on replace.
        client_id:
          type: string
          minLength: 1
          maxLength: 2048
          description: OIDC client ID.
        client_secret:
          type: string
          minLength: 1
          maxLength: 4096
          description: |
            OIDC client secret (plaintext; stored encrypted). Required on
            replace. Never returned in responses.
        subject_claim:
          type: string
          minLength: 1
          maxLength: 2048
          default: sub
          description: OIDC claim used as the stable subject identifier.
        scopes:
          type: array
          minItems: 1
          maxItems: 64
          default:
            - openid
            - profile
            - email
          items:
            type: string
            minLength: 1
            maxLength: 1024
          description: |
            OIDC scopes requested from the upstream IdP. Must include
            `openid`.
        status:
          allOf:
            - $ref: '#/components/schemas/UserSourceStatus'
          default: active

    UserSourcePatchRequest:
      type: object
      properties:
        name:
          type: string
          minLength: 1
          maxLength: 255
          description: Human-readable name. Omit to keep the stored value.
        description:
          type: string
          maxLength: 4000
          description: |
            Freeform description. Omit to keep the stored value. Explicit
            `null` is stored as an empty string.
        protocol:
          $ref: '#/components/schemas/UserSourceProtocol'
        issuer:
          type: string
          minLength: 1
          maxLength: 2048
          format: uri
          nullable: true
          description: |
            OIDC issuer URL. Omit to keep the stored value. Must not be
            `null`. Re-validated only when the string actually changes.
        client_id:
          type: string
          minLength: 1
          maxLength: 2048
          description: OIDC client ID. Omit to keep the stored value.
        client_secret:
          type: string
          minLength: 1
          maxLength: 4096
          nullable: true
          description: |
            OIDC client secret. Omit to keep the stored secret. Send a new
            value to rotate. Explicit `null` clears the secret. Never
            returned in responses.
        subject_claim:
          type: string
          minLength: 1
          maxLength: 2048
          description: OIDC claim used as the stable subject identifier.
        scopes:
          type: array
          minItems: 1
          maxItems: 64
          items:
            type: string
            minLength: 1
            maxLength: 1024
          description: |
            OIDC scopes requested from the upstream IdP. Must include
            `openid` when sent. Re-validated only when the set actually
            changes.
        status:
          $ref: '#/components/schemas/UserSourceStatus'
```
