{
  "openapi": "3.0.3",
  "info": {
    "title": "IP Central Auth API",
    "version": "1.0.0",
    "description": "Application-scoped login verification and administrator provisioning for the IP authentication gateway. Use the Authorize button to enter an application key for login verification or an administrator key for provisioning."
  },
  "servers": [
    {
      "url": "/",
      "description": "Current host"
    }
  ],
  "tags": [
    { "name": "Status", "description": "Service availability" },
    { "name": "Authentication", "description": "Application-scoped credential verification" },
    { "name": "Users", "description": "Administrator user provisioning" },
    { "name": "Applications", "description": "Administrator application provisioning" },
    { "name": "Assignments", "description": "User access assignments" }
  ],
  "paths": {
    "/health": {
      "get": {
        "tags": ["Status"],
        "summary": "Check service health",
        "operationId": "getHealth",
        "responses": {
          "200": {
            "description": "The service is available.",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/HealthResponse" },
                "example": { "status": "ok" }
              }
            }
          }
        }
      }
    },
    "/api/authenticate": {
      "post": {
        "tags": ["Authentication"],
        "summary": "Verify a user's credentials for this application",
        "operationId": "authenticateUser",
        "security": [{ "ApplicationKey": [] }],
        "description": "Requires the application's API key as a bearer token. Invalid credentials, an inactive or unassigned account, and an invalid key share the same 401 response. If MFA is enabled for the user, password verification returns 202; no MFA challenge is implemented yet and this is not a successful login.",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": { "$ref": "#/components/schemas/AuthenticateRequest" },
              "example": {
                "username": "alice",
                "password": "correct horse battery",
                "include": ["id", "role", "level"]
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Credentials accepted.",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/AuthenticateSuccess" },
                "example": {
                  "ack": true,
                  "user": { "id": 123, "role": "admin", "level": 4 },
                  "method": "password",
                  "mfa_required": false
                }
              }
            }
          },
          "202": {
            "description": "Password verified, but the account requires an MFA step that is not yet available.",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/MfaRequiredResponse" },
                "example": {
                  "ack": false,
                  "mfa_required": true,
                  "message": "Additional authentication required"
                }
              }
            }
          },
          "400": {
            "description": "The request body or requested include fields are invalid.",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/ErrorResponse" },
                "example": { "error": "Username and password are required strings." }
              }
            }
          },
          "401": {
            "description": "Authentication failed.",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/AuthenticationFailure" },
                "example": { "ack": false, "message": "Authentication failed" }
              }
            }
          },
          "500": {
            "description": "The request could not be completed.",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/ErrorResponse" },
                "example": { "error": "The request could not be completed." }
              }
            }
          }
        }
      }
    },
    "/api/admin/users": {
      "get": {
        "tags": ["Users"],
        "summary": "List users",
        "operationId": "listUsers",
        "security": [{ "AdminKey": [] }],
        "responses": {
          "200": {
            "description": "Users without password hashes.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "required": ["users"],
                  "properties": {
                    "users": {
                      "type": "array",
                      "items": { "$ref": "#/components/schemas/UserSummary" }
                    }
                  }
                },
                "example": {
                  "users": [
                    {
                      "id": 123,
                      "username": "alice",
                      "display_name": "Alice",
                      "email": "alice@example.com",
                      "is_active": 1,
                      "created_at": "2026-11-14 06:51:00"
                    }
                  ]
                }
              }
            }
          },
          "401": { "$ref": "#/components/responses/AdminUnauthorized" },
          "500": { "$ref": "#/components/responses/ServerError" }
        }
      },
      "post": {
        "tags": ["Users"],
        "summary": "Create a user",
        "operationId": "createUser",
        "security": [{ "AdminKey": [] }],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": { "$ref": "#/components/schemas/CreateUserRequest" },
              "example": {
                "username": "alice",
                "password": "correct horse battery",
                "display_name": "Alice Example",
                "email": "alice@example.com"
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": "User created.",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/CreatedUser" },
                "example": { "id": 123, "username": "alice" }
              }
            }
          },
          "400": { "$ref": "#/components/responses/InvalidInput" },
          "401": { "$ref": "#/components/responses/AdminUnauthorized" },
          "409": { "$ref": "#/components/responses/Conflict" },
          "500": { "$ref": "#/components/responses/ServerError" }
        }
      }
    },
    "/api/admin/applications": {
      "get": {
        "tags": ["Applications"],
        "summary": "List applications",
        "operationId": "listApplications",
        "security": [{ "AdminKey": [] }],
        "responses": {
          "200": {
            "description": "Applications without API key hashes.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "required": ["applications"],
                  "properties": {
                    "applications": {
                      "type": "array",
                      "items": { "$ref": "#/components/schemas/ApplicationSummary" }
                    }
                  }
                },
                "example": {
                  "applications": [
                    {
                      "id": 31,
                      "slug": "control",
                      "name": "Control Panel",
                      "is_active": 1,
                      "created_at": "2026-11-14 06:51:00"
                    }
                  ]
                }
              }
            }
          },
          "401": { "$ref": "#/components/responses/AdminUnauthorized" },
          "500": { "$ref": "#/components/responses/ServerError" }
        }
      },
      "post": {
        "tags": ["Applications"],
        "summary": "Create an application and generate its API key",
        "operationId": "createApplication",
        "security": [{ "AdminKey": [] }],
        "description": "The generated api_key is returned only once. Save it securely; the service stores only its SHA-256 hash.",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": { "$ref": "#/components/schemas/CreateApplicationRequest" },
              "example": { "slug": "control", "name": "Control Panel" }
            }
          }
        },
        "responses": {
          "201": {
            "description": "Application created; retain the API key because it cannot be retrieved again.",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/CreatedApplication" },
                "example": {
                  "id": 31,
                  "slug": "control",
                  "name": "Control Panel",
                  "api_key": "GENERATED_SECRET_RETURNED_ONCE"
                }
              }
            }
          },
          "400": { "$ref": "#/components/responses/InvalidInput" },
          "401": { "$ref": "#/components/responses/AdminUnauthorized" },
          "409": { "$ref": "#/components/responses/Conflict" },
          "500": { "$ref": "#/components/responses/ServerError" }
        }
      }
    },
    "/api/admin/assignments": {
      "post": {
        "tags": ["Assignments"],
        "summary": "Assign a user to an application",
        "operationId": "assignUserToApplication",
        "security": [{ "AdminKey": [] }],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": { "$ref": "#/components/schemas/AssignmentRequest" },
              "example": {
                "user_id": 123,
                "application_id": 31,
                "role": "admin",
                "level": 4,
                "metadata": { "region": "eu" }
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": "Assignment created or updated.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "required": ["assigned"],
                  "properties": { "assigned": { "type": "boolean", "example": true } }
                },
                "example": { "assigned": true }
              }
            }
          },
          "400": { "$ref": "#/components/responses/InvalidInput" },
          "401": { "$ref": "#/components/responses/AdminUnauthorized" },
          "409": { "$ref": "#/components/responses/Conflict" },
          "500": { "$ref": "#/components/responses/ServerError" }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "ApplicationKey": {
        "type": "http",
        "scheme": "bearer",
        "bearerFormat": "Application API key",
        "description": "Use the key generated when the application was created."
      },
      "AdminKey": {
        "type": "http",
        "scheme": "bearer",
        "bearerFormat": "Administrator API key",
        "description": "Use ADMIN_API_KEY configured on the auth service."
      }
    },
    "schemas": {
      "HealthResponse": {
        "type": "object",
        "required": ["status"],
        "properties": { "status": { "type": "string", "enum": ["ok"] } }
      },
      "AuthenticateRequest": {
        "type": "object",
        "required": ["username", "password"],
        "properties": {
          "username": { "type": "string", "minLength": 1, "example": "alice" },
          "password": { "type": "string", "minLength": 1, "writeOnly": true, "example": "correct horse battery" },
          "include": {
            "type": "array",
            "description": "Identity fields to return. If omitted, defaults to id, username, role, and level.",
            "items": {
              "type": "string",
              "enum": ["id", "username", "display_name", "email", "role", "level", "metadata"]
            },
            "uniqueItems": true,
            "example": ["id", "role", "level"]
          }
        }
      },
      "AuthenticateSuccess": {
        "type": "object",
        "required": ["ack", "user", "method", "mfa_required"],
        "properties": {
          "ack": { "type": "boolean", "enum": [true] },
          "user": {
            "type": "object",
            "description": "Contains only the requested allowlisted identity fields.",
            "properties": {
              "id": { "type": "integer", "format": "int64" },
              "username": { "type": "string" },
              "display_name": { "type": "string", "nullable": true },
              "email": { "type": "string", "format": "email", "nullable": true },
              "role": { "type": "string" },
              "level": { "type": "integer", "format": "int32", "nullable": true },
              "metadata": { "type": "object", "additionalProperties": true }
            },
            "additionalProperties": false
          },
          "method": { "type": "string", "enum": ["password"] },
          "mfa_required": { "type": "boolean", "enum": [false] }
        }
      },
      "MfaRequiredResponse": {
        "type": "object",
        "required": ["ack", "mfa_required", "message"],
        "properties": {
          "ack": { "type": "boolean", "enum": [false] },
          "mfa_required": { "type": "boolean", "enum": [true] },
          "message": { "type": "string", "example": "Additional authentication required" }
        }
      },
      "AuthenticationFailure": {
        "type": "object",
        "required": ["ack", "message"],
        "properties": {
          "ack": { "type": "boolean", "enum": [false] },
          "message": { "type": "string", "example": "Authentication failed" }
        }
      },
      "CreateUserRequest": {
        "type": "object",
        "required": ["username", "password"],
        "properties": {
          "username": { "type": "string", "minLength": 1, "maxLength": 128 },
          "password": { "type": "string", "minLength": 12, "maxLength": 4096, "writeOnly": true },
          "display_name": { "type": "string", "maxLength": 255, "nullable": true },
          "email": { "type": "string", "format": "email", "maxLength": 254, "nullable": true }
        }
      },
      "CreatedUser": {
        "type": "object",
        "required": ["id", "username"],
        "properties": {
          "id": { "type": "integer", "format": "int64" },
          "username": { "type": "string" }
        }
      },
      "UserSummary": {
        "type": "object",
        "required": ["id", "username", "display_name", "email", "is_active", "created_at"],
        "properties": {
          "id": { "type": "integer", "format": "int64" },
          "username": { "type": "string" },
          "display_name": { "type": "string", "nullable": true },
          "email": { "type": "string", "format": "email", "nullable": true },
          "is_active": { "type": "integer", "enum": [0, 1] },
          "created_at": { "type": "string", "description": "Database timestamp in YYYY-MM-DD HH:MM:SS format.", "example": "2026-11-14 06:51:00" }
        }
      },
      "CreateApplicationRequest": {
        "type": "object",
        "required": ["slug", "name"],
        "properties": {
          "slug": {
            "type": "string",
            "minLength": 1,
            "maxLength": 63,
            "pattern": "^[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$",
            "description": "Letters and numbers with optional internal hyphens; normalized to lowercase."
          },
          "name": { "type": "string", "minLength": 1, "maxLength": 255 }
        }
      },
      "CreatedApplication": {
        "type": "object",
        "required": ["id", "slug", "name", "api_key"],
        "properties": {
          "id": { "type": "integer", "format": "int64" },
          "slug": { "type": "string" },
          "name": { "type": "string" },
          "api_key": {
            "type": "string",
            "description": "Generated secret; returned only at creation time.",
            "writeOnly": true
          }
        }
      },
      "ApplicationSummary": {
        "type": "object",
        "required": ["id", "slug", "name", "is_active", "created_at"],
        "properties": {
          "id": { "type": "integer", "format": "int64" },
          "slug": { "type": "string" },
          "name": { "type": "string" },
          "is_active": { "type": "integer", "enum": [0, 1] },
          "created_at": { "type": "string", "description": "Database timestamp in YYYY-MM-DD HH:MM:SS format.", "example": "2026-11-14 06:51:00" }
        }
      },
      "AssignmentRequest": {
        "type": "object",
        "required": ["user_id", "application_id", "role"],
        "properties": {
          "user_id": { "type": "integer", "minimum": 1 },
          "application_id": { "type": "integer", "minimum": 1 },
          "role": { "type": "string", "minLength": 1, "maxLength": 100 },
          "level": { "type": "integer", "minimum": 0, "nullable": true },
          "metadata": {
            "description": "Optional JSON object returned as identity metadata when requested. Empty metadata is currently serialized as an empty array.",
            "oneOf": [
              { "type": "object", "additionalProperties": true },
              { "type": "array", "maxItems": 0 }
            ],
            "default": {}
          }
        }
      },
      "ErrorResponse": {
        "type": "object",
        "required": ["error"],
        "properties": { "error": { "type": "string" } }
      }
    },
    "responses": {
      "InvalidInput": {
        "description": "The request body or field values are invalid.",
        "content": {
          "application/json": {
            "schema": { "$ref": "#/components/schemas/ErrorResponse" },
            "example": { "error": "Password must be between 12 and 4096 characters." }
          }
        }
      },
      "AdminUnauthorized": {
        "description": "Administrator bearer key is missing or invalid.",
        "content": {
          "application/json": {
            "schema": { "$ref": "#/components/schemas/ErrorResponse" },
            "example": { "error": "Administrator authentication required" }
          }
        }
      },
      "Conflict": {
        "description": "A unique value or referenced resource conflicts with existing data.",
        "content": {
          "application/json": {
            "schema": { "$ref": "#/components/schemas/ErrorResponse" },
            "example": { "error": "The requested resource conflicts with existing data or references." }
          }
        }
      },
      "ServerError": {
        "description": "The request could not be completed.",
        "content": {
          "application/json": {
            "schema": { "$ref": "#/components/schemas/ErrorResponse" },
            "example": { "error": "The request could not be completed." }
          }
        }
      }
    }
  }
}