openapi: 3.1.0
info:
  title: OHN-38 Authentication and Identity Federation
  version: 1.1.0
  description: >
    Reconciled with the observed behaviour of the TypeScript reference (backend/modules/ohn-38), see
    docs/program/java/ohn-38-contract-changes.md. Administration routes need a gateway token with role
    security_administrator or identity_admin (default deny); login, token exchange, introspection and OIDC discovery are public.
jsonSchemaDialect: https://json-schema.org/draft/2020-12/schema
servers: [{ url: / }]
paths:
  /.well-known/openid-configuration:
    get:
      operationId: ohn38_discovery
      x-inventory-id: OHN-38-API-001
      tags: [ohn38]
      security: []
      description: Static OIDC metadata. Answers 503 not_configured when the issuer is not configured. Never contains keys or secrets.
      responses:
        '200': { description: 'OIDC metadata', content: { application/json: { schema: { type: object } } } }
        '503': { $ref: '#/components/responses/Unavailable' }
  /health/ohn-38:
    get:
      operationId: ohn38_health
      tags: [ohn38]
      security: []
      description: Public liveness probe (Java only; the reference has none). 200 {status, module} when the database answers, 503 otherwise.
      responses:
        '200': { description: 'ok', content: { application/json: { schema: { type: object } } } }
        '503': { description: 'unavailable', content: { application/json: { schema: { type: object } } } }
  /api/ohn/v1/authenticators/enroll:
    post:
      operationId: ohn38_enrollAuthenticator
      tags: [ohn38]
      parameters: [{ $ref: '#/components/parameters/IdempotencyKey' }]
      requestBody: { required: true, content: { application/json: { schema: { $ref: '#/components/schemas/EnrollmentCommand' } } } }
      description: Creates an authenticator and returns its provisioning secret ONCE (never readable afterwards). A re-enrolment supersedes every earlier TOTP secret of the principal.
      responses:
        '201': { description: 'Enrolled (id, principal_id, factor_type, provisioning_secret, otpauth_url, receipt)', content: { application/json: { schema: { type: object } } } }
        '401': { $ref: '#/components/responses/Unauthorized' }
        '403': { $ref: '#/components/responses/Denied' }
        '422': { $ref: '#/components/responses/Validation' }
  /api/ohn/v1/auth/login:
    post:
      operationId: ohn38_login
      tags: [ohn38]
      security: []
      parameters: [{ $ref: '#/components/parameters/IdempotencyKey' }]
      requestBody: { required: true, content: { application/json: { schema: { $ref: '#/components/schemas/LoginCommand' } } } }
      description: >
        Public. TOTP sign-in. A code is accepted once. After the configured number of failed codes inside the window the principal is locked (423, Retry-After) and even a correct code is refused.
        A malformed lockout configuration answers 503.
      responses:
        '200': { description: 'MFA-backed session (access_token, token_type, session_id, expires_in)', content: { application/json: { schema: { type: object } } } }
        '401': { description: 'authentication_failed with risk_ref (no credential enumeration)', content: { application/json: { schema: { type: object } } } }
        '422': { $ref: '#/components/responses/Validation' }
        '423': { description: 'account_locked (Retry-After, retry_after_seconds, risk_ref)', content: { application/json: { schema: { type: object } } } }
        '429': { $ref: '#/components/responses/RateLimited' }
        '503': { $ref: '#/components/responses/Unavailable' }
  /api/ohn/v1/sessions/introspect:
    post:
      operationId: ohn38_introspectSession
      tags: [ohn38]
      security: []
      parameters:
        - { name: X-Device-Attestation, in: header, required: false, description: The device the session is bound to (UUID). A missing or different value answers 403 device_binding_mismatch., schema: { type: string } }
      requestBody: { required: true, content: { application/json: { schema: { $ref: '#/components/schemas/IntrospectionCommand' } } } }
      description: Public. The signature, issuer, audience and expiry are checked, then the stored session must exist with the same token hash, unrevoked and unexpired.
      responses:
        '200': { description: 'Active session (active, session_id, principal_id, expires_at)', content: { application/json: { schema: { type: object } } } }
        '401': { description: '{active:false}', content: { application/json: { schema: { type: object } } } }
        '403': { description: '{active:false, reason:device_binding_mismatch}', content: { application/json: { schema: { type: object } } } }
        '422': { $ref: '#/components/responses/Validation' }
  /api/ohn/v1/sessions:
    get:
      operationId: ohn38_listIdentityState
      tags: [ohn38]
      description: Sessions, enrollments (no secrets), trusts, clients (no secrets), role bindings, revocations and workflow history of the caller's jurisdiction and organization, with a summary and provenance.
      responses:
        '200': { description: 'Scoped identity state', content: { application/json: { schema: { type: object } } } }
        '403': { $ref: '#/components/responses/Denied' }
  /api/ohn/v1/sessions/revoke:
    post:
      operationId: ohn38_revokeSessions
      x-inventory-id: OHN-38-API-002
      tags: [ohn38]
      parameters: [{ $ref: '#/components/parameters/IdempotencyKey' }]
      requestBody: { required: true, content: { application/json: { schema: { $ref: '#/components/schemas/SessionRevocationCommand' } } } }
      description: Revokes the listed sessions of the caller's jurisdiction and organization immediately and opens the privilege_revocation workflow at token_invalidated (version 2).
      responses:
        '200': { description: 'Revoked immediately (id, workflow_id, state, revoked_count, receipt)', content: { application/json: { schema: { type: object } } } }
        '409': { $ref: '#/components/responses/Conflict' }
        '422': { $ref: '#/components/responses/Validation' }
  /api/ohn/v1/identity-federation/trusts:
    post:
      operationId: ohn38_configureTrust
      x-inventory-id: OHN-38-API-003
      tags: [ohn38]
      parameters: [{ $ref: '#/components/parameters/IdempotencyKey' }]
      requestBody: { required: true, content: { application/json: { schema: { $ref: '#/components/schemas/FederationTrustCommand' } } } }
      description: Records an HTTPS issuer trust for the caller's own jurisdiction (a different jurisdiction_id is 403). A second trust for the same issuer is 409 conflict.
      responses:
        '201': { description: 'Trust configured (id, issuer_url, status, key_rotation_at, receipt)', content: { application/json: { schema: { type: object } } } }
        '403': { $ref: '#/components/responses/Denied' }
        '409': { $ref: '#/components/responses/Conflict' }
        '422': { $ref: '#/components/responses/Validation' }
  /api/ohn/v1/privileged-role-bindings:
    post:
      operationId: ohn38_bindRole
      tags: [ohn38]
      parameters: [{ $ref: '#/components/parameters/IdempotencyKey' }]
      requestBody: { required: true, content: { application/json: { schema: { $ref: '#/components/schemas/RoleBindingCommand' } } } }
      responses:
        '201': { description: 'Expiring role binding created (id, role, expires_at, receipt)', content: { application/json: { schema: { type: object } } } }
        '403': { $ref: '#/components/responses/Denied' }
        '422': { $ref: '#/components/responses/Validation' }
  /api/ohn/v1/oauth/clients:
    post:
      operationId: ohn38_onboardClient
      tags: [ohn38]
      parameters: [{ $ref: '#/components/parameters/IdempotencyKey' }]
      requestBody: { required: true, content: { application/json: { schema: { $ref: '#/components/schemas/OAuthClientCommand' } } } }
      responses:
        '201': { description: 'Client attested (id, client_id, state, version, receipt)', content: { application/json: { schema: { type: object } } } }
        '422': { $ref: '#/components/responses/Validation' }
  /api/ohn/v1/oauth/clients/{id}/transitions:
    post:
      operationId: ohn38_transitionClient
      tags: [ohn38]
      parameters: [{ $ref: '#/components/parameters/Id' }, { $ref: '#/components/parameters/IdempotencyKey' }]
      requestBody: { required: true, content: { application/json: { schema: { $ref: '#/components/schemas/WorkflowCommand' } } } }
      description: Same handler as /workflows/{id}/transitions (the reference accepts either route for a client or a revocation).
      responses:
        '200': { description: 'Guarded transition committed (id, state, version, client_secret ONCE when credential_issued, receipt)', content: { application/json: { schema: { type: object } } } }
        '404': { $ref: '#/components/responses/NotFound' }
        '409': { $ref: '#/components/responses/Conflict' }
        '422': { $ref: '#/components/responses/Validation' }
  /api/ohn/v1/workflows/{id}/transitions:
    post:
      operationId: ohn38_transitionWorkflow
      tags: [ohn38]
      x-inventory-id: OHN-38-API-004
      parameters: [{ $ref: '#/components/parameters/Id' }, { $ref: '#/components/parameters/IdempotencyKey' }]
      requestBody: { required: true, content: { application/json: { schema: { $ref: '#/components/schemas/WorkflowCommand' } } } }
      description: >
        Workflow service_principal_onboarding: client_attested, workflow_identity_bound, scope_approved, credential_issued (the secret is returned once).
        Workflow privilege_revocation: token_invalidated, sessions_terminated, audit_recorded. Only the next state is allowed; expected_version must match (409 stale_version, 422 invalid_transition).
      responses:
        '200': { description: 'Guarded transition committed', content: { application/json: { schema: { type: object } } } }
        '404': { $ref: '#/components/responses/NotFound' }
        '409': { $ref: '#/components/responses/Conflict' }
        '422': { $ref: '#/components/responses/Validation' }
  /api/ohn/v1/oauth/token:
    post:
      operationId: ohn38_exchangeToken
      tags: [ohn38]
      security: []
      parameters: [{ $ref: '#/components/parameters/IdempotencyKey' }]
      requestBody: { required: true, content: { application/json: { schema: { $ref: '#/components/schemas/TokenExchangeCommand' } } } }
      description: Public. client_credentials exchange (JSON body). grant_type is optional but any value other than client_credentials is 422. Every failure is 401 invalid_client.
      responses:
        '200': { description: 'Service session (access_token, token_type, expires_in, scope, session_id)', content: { application/json: { schema: { type: object } } } }
        '401': { $ref: '#/components/responses/Unauthorized' }
        '422': { $ref: '#/components/responses/Validation' }
        '429': { $ref: '#/components/responses/RateLimited' }
        '503': { $ref: '#/components/responses/Unavailable' }
  /api/ohn/v1/reports/{id}:
    get:
      operationId: ohn38_getReport
      tags: [ohn38]
      parameters: [{ name: id, in: path, required: true, description: 'OHN-38-R001, OHN-38-R002 or OHN-38-R003; any other value is 404', schema: { type: string } }]
      responses:
        '200': { description: 'Scoped report with metrics and provenance', content: { application/json: { schema: { type: object } } } }
        '404': { $ref: '#/components/responses/NotFound' }
  /api/ohn/v1/reports/{id}/export:
    post:
      operationId: ohn38_exportReport
      tags: [ohn38]
      parameters: [{ name: id, in: path, required: true, schema: { type: string } }, { $ref: '#/components/parameters/IdempotencyKey' }]
      requestBody: { required: true, content: { application/json: { schema: { $ref: '#/components/schemas/ExportCommand' } } } }
      description: HMAC-SHA256 signed content (base64) with five-minute expiry metadata and an audit receipt. Answers 503 not_configured when the export signing key is not configured.
      responses:
        '201': { description: 'Signed content', content: { application/json: { schema: { type: object } } } }
        '403': { $ref: '#/components/responses/Denied' }
        '404': { $ref: '#/components/responses/NotFound' }
        '422': { $ref: '#/components/responses/Validation' }
        '503': { $ref: '#/components/responses/Unavailable' }
components:
  securitySchemes: { bearerAuth: { type: http, scheme: bearer, bearerFormat: JWT } }
  parameters:
    IdempotencyKey: { name: Idempotency-Key, in: header, required: true, schema: { type: string, minLength: 1, maxLength: 255 } }
    Id: { name: id, in: path, required: true, description: UUID of the client or revocation; anything else is 404, schema: { type: string } }
  responses:
    Unauthorized: { description: Authentication failed without credential enumeration }
    Denied: { description: Default-deny policy refusal }
    NotFound: { description: Non-disclosing not found }
    Conflict: { description: Idempotency or optimistic concurrency conflict }
    Validation: { description: Field/domain validation failed (domain_validation_failed) }
    RateLimited: { description: Retry after the rate-limit window }
    Unavailable: { description: Not configured or dependency unavailable; nothing was done }
  schemas:
    EnrollmentCommand:
      type: object
      additionalProperties: false
      required: [principal_id, factor_type, verification_proof, recovery_method]
      properties:
        principal_id: { type: string, format: uuid }
        factor_type: { type: string, enum: [totp, passkey], description: passkey needs device_attestation; only totp can sign in }
        device_attestation: { type: string, format: uuid }
        verification_proof: { type: string, format: uuid }
        recovery_method: { type: string, enum: [identity_proofing, dual_control, recovery_codes] }
    SessionRevocationCommand:
      type: object
      additionalProperties: false
      required: [principal_id, session_ids, reason, effective_at]
      properties:
        principal_id: { type: string, format: uuid }
        session_ids: { type: string, minLength: 36, maxLength: 8192, description: One session UUID per line }
        reason: { type: string, minLength: 10, maxLength: 4096 }
        effective_at: { type: string, format: date-time }
    FederationTrustCommand:
      type: object
      additionalProperties: false
      required: [issuer_url, client_registration_ref, signing_key_ref, allowed_audiences, jurisdiction_id]
      properties:
        issuer_url: { type: string, maxLength: 2048, description: An https URL }
        client_registration_ref: { type: string, format: uuid }
        signing_key_ref: { type: string, format: uuid }
        allowed_audiences: { type: string, minLength: 1, maxLength: 8192, description: 'One audience per line, unique' }
        jurisdiction_id: { type: string, format: uuid }
    RoleBindingCommand:
      type: object
      additionalProperties: false
      required: [principal_id, role, organization_id, approval_ref, expires_at]
      properties:
        principal_id: { type: string, format: uuid }
        role: { type: string, enum: [security_administrator, identity_admin, clinician, patient, technical_client, pharmacist, lab_technologist, radiologist, claims_adjudicator, partner_staff, integration_developer] }
        organization_id: { type: string, format: uuid }
        approval_ref: { type: string, format: uuid }
        expires_at: { type: string, format: date-time, description: Must be in the future }
    LoginCommand:
      type: object
      additionalProperties: false
      required: [principal_id, otp, device_attestation]
      properties:
        principal_id: { type: string, format: uuid }
        otp: { type: string, pattern: '^\d{6}$' }
        device_attestation: { type: string, format: uuid }
    IntrospectionCommand:
      type: object
      additionalProperties: false
      required: [token]
      properties: { token: { type: string, minLength: 20 } }
    OAuthClientCommand:
      type: object
      additionalProperties: false
      required: [attestation_ref, allowed_scopes]
      properties:
        attestation_ref: { type: string, format: uuid }
        allowed_scopes: { type: string, minLength: 1, maxLength: 2048, description: One scope per line }
    WorkflowCommand:
      type: object
      additionalProperties: false
      required: [expected_version, to_state, evidence_ref, reason]
      properties:
        expected_version: { type: integer, minimum: 1 }
        to_state: { type: string }
        evidence_ref: { type: string, format: uuid }
        reason: { type: string, minLength: 10, maxLength: 4096 }
    TokenExchangeCommand:
      type: object
      additionalProperties: false
      required: [client_id, client_secret, scope]
      properties:
        grant_type: { type: string, description: Optional; only client_credentials is accepted }
        client_id: { type: string, format: uuid }
        client_secret: { type: string, minLength: 32, writeOnly: true }
        scope: { type: string, minLength: 1, description: 'Space separated scopes; all must be allowed for the client' }
    ExportCommand:
      type: object
      additionalProperties: false
      required: [format]
      properties: { format: { type: string, enum: [csv, pdf] } }
security: [{ bearerAuth: [] }]
