{
  "openapi": "3.1.0",
  "info": {
    "title": "Getriebe HTTP session contract",
    "version": "1.0",
    "description": "Implemented session endpoints and selected account operations affecting access in the experimental Getriebe MVP."
  },
  "servers": [
    {
      "url": "/api/v1"
    }
  ],
  "security": [
    {
      "sessionCookie": []
    }
  ],
  "paths": {
    "/session/csrf": {
      "get": {
        "summary": "Obtain a CSRF token",
        "operationId": "getCsrfToken",
        "security": [],
        "responses": {
          "200": {
            "description": "Token for the request session",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/CsrfToken"
                }
              }
            },
            "headers": {
              "Cache-Control": {
                "description": "The token must not be cached.",
                "schema": {
                  "const": "no-store"
                }
              },
              "Set-Cookie": {
                "description": "Session cookie when establishing or updating the session",
                "schema": {
                  "type": "string"
                }
              }
            }
          },
          "429": {
            "$ref": "#/components/responses/Problem"
          },
          "500": {
            "$ref": "#/components/responses/Problem"
          }
        }
      }
    },
    "/login": {
      "post": {
        "summary": "Sign in",
        "operationId": "login",
        "security": [
          {
            "sessionCookie": [],
            "csrfToken": []
          }
        ],
        "responses": {
          "200": {
            "description": "Current authenticated profile",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Profile"
                }
              }
            },
            "headers": {
              "Set-Cookie": {
                "description": "Rotated signed session cookie",
                "schema": {
                  "type": "string"
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/Problem"
          },
          "401": {
            "$ref": "#/components/responses/Problem"
          },
          "403": {
            "$ref": "#/components/responses/Problem"
          },
          "409": {
            "$ref": "#/components/responses/Problem"
          },
          "413": {
            "$ref": "#/components/responses/Problem"
          },
          "415": {
            "$ref": "#/components/responses/Problem"
          },
          "429": {
            "$ref": "#/components/responses/Problem"
          },
          "500": {
            "$ref": "#/components/responses/Problem"
          },
          "503": {
            "$ref": "#/components/responses/Problem"
          }
        },
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/Login"
              }
            }
          }
        }
      }
    },
    "/profile": {
      "get": {
        "summary": "Read the current profile",
        "operationId": "getProfile",
        "security": [
          {
            "sessionCookie": []
          }
        ],
        "responses": {
          "200": {
            "description": "Current authenticated profile",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Profile"
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/Problem"
          },
          "409": {
            "$ref": "#/components/responses/Problem"
          },
          "500": {
            "$ref": "#/components/responses/Problem"
          },
          "503": {
            "$ref": "#/components/responses/Problem"
          }
        }
      }
    },
    "/logout": {
      "post": {
        "summary": "Sign out",
        "operationId": "logout",
        "security": [
          {
            "sessionCookie": [],
            "csrfToken": []
          }
        ],
        "responses": {
          "204": {
            "description": "Session revoked"
          },
          "401": {
            "$ref": "#/components/responses/Problem"
          },
          "403": {
            "$ref": "#/components/responses/Problem"
          },
          "409": {
            "$ref": "#/components/responses/Problem"
          },
          "500": {
            "$ref": "#/components/responses/Problem"
          },
          "503": {
            "$ref": "#/components/responses/Problem"
          }
        }
      }
    },
    "/users": {
      "post": {
        "summary": "Create an account",
        "operationId": "createUser",
        "security": [
          {
            "sessionCookie": [],
            "csrfToken": []
          }
        ],
        "responses": {
          "201": {
            "description": "Account created",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/User"
                }
              }
            },
            "headers": {
              "Location": {
                "description": "Created account URI",
                "schema": {
                  "type": "string"
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/Problem"
          },
          "401": {
            "$ref": "#/components/responses/Problem"
          },
          "403": {
            "$ref": "#/components/responses/Problem"
          },
          "404": {
            "$ref": "#/components/responses/Problem"
          },
          "409": {
            "$ref": "#/components/responses/Problem"
          },
          "413": {
            "$ref": "#/components/responses/Problem"
          },
          "415": {
            "$ref": "#/components/responses/Problem"
          },
          "500": {
            "$ref": "#/components/responses/Problem"
          },
          "503": {
            "$ref": "#/components/responses/Problem"
          }
        },
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/CreateUser"
              }
            }
          }
        }
      }
    },
    "/users/{userId}": {
      "parameters": [
        {
          "in": "path",
          "name": "userId",
          "required": true,
          "schema": {
            "type": "string"
          }
        }
      ],
      "get": {
        "summary": "View the account",
        "operationId": "getUser",
        "security": [
          {
            "sessionCookie": []
          }
        ],
        "responses": {
          "200": {
            "description": "Current account representation",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/User"
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/Problem"
          },
          "403": {
            "$ref": "#/components/responses/Problem"
          },
          "404": {
            "$ref": "#/components/responses/Problem"
          },
          "409": {
            "$ref": "#/components/responses/Problem"
          },
          "500": {
            "$ref": "#/components/responses/Problem"
          },
          "503": {
            "$ref": "#/components/responses/Problem"
          }
        }
      },
      "patch": {
        "summary": "Change account access or display name",
        "operationId": "updateUser",
        "security": [
          {
            "sessionCookie": [],
            "csrfToken": []
          }
        ],
        "responses": {
          "200": {
            "description": "Current account representation",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/User"
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/Problem"
          },
          "401": {
            "$ref": "#/components/responses/Problem"
          },
          "403": {
            "$ref": "#/components/responses/Problem"
          },
          "404": {
            "$ref": "#/components/responses/Problem"
          },
          "409": {
            "$ref": "#/components/responses/Problem"
          },
          "413": {
            "$ref": "#/components/responses/Problem"
          },
          "415": {
            "$ref": "#/components/responses/Problem"
          },
          "500": {
            "$ref": "#/components/responses/Problem"
          },
          "503": {
            "$ref": "#/components/responses/Problem"
          }
        },
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/UpdateUser"
              }
            }
          }
        }
      }
    },
    "/profile/password": {
      "post": {
        "summary": "Change the signed-in user's password",
        "operationId": "changePassword",
        "security": [
          {
            "sessionCookie": [],
            "csrfToken": []
          }
        ],
        "responses": {
          "204": {
            "description": "Password changed and previous sessions revoked"
          },
          "400": {
            "$ref": "#/components/responses/Problem"
          },
          "401": {
            "$ref": "#/components/responses/Problem"
          },
          "403": {
            "$ref": "#/components/responses/Problem"
          },
          "409": {
            "$ref": "#/components/responses/Problem"
          },
          "413": {
            "$ref": "#/components/responses/Problem"
          },
          "415": {
            "$ref": "#/components/responses/Problem"
          },
          "500": {
            "$ref": "#/components/responses/Problem"
          },
          "503": {
            "$ref": "#/components/responses/Problem"
          }
        },
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/ChangePassword"
              }
            }
          }
        }
      }
    },
    "/users/{userId}/password": {
      "parameters": [
        {
          "in": "path",
          "name": "userId",
          "required": true,
          "schema": {
            "type": "string"
          }
        }
      ],
      "post": {
        "summary": "Reset an account's password",
        "operationId": "resetPassword",
        "security": [
          {
            "sessionCookie": [],
            "csrfToken": []
          }
        ],
        "responses": {
          "204": {
            "description": "Password reset and previous sessions revoked"
          },
          "400": {
            "$ref": "#/components/responses/Problem"
          },
          "401": {
            "$ref": "#/components/responses/Problem"
          },
          "403": {
            "$ref": "#/components/responses/Problem"
          },
          "404": {
            "$ref": "#/components/responses/Problem"
          },
          "409": {
            "$ref": "#/components/responses/Problem"
          },
          "413": {
            "$ref": "#/components/responses/Problem"
          },
          "415": {
            "$ref": "#/components/responses/Problem"
          },
          "500": {
            "$ref": "#/components/responses/Problem"
          },
          "503": {
            "$ref": "#/components/responses/Problem"
          }
        },
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/ResetPassword"
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "sessionCookie": {
        "type": "apiKey",
        "in": "cookie",
        "name": "getriebe_session",
        "description": "Host-only signed session cookie. Path is /api/v1. HttpOnly and SameSite=Strict. Secure in HTTPS mode."
      },
      "csrfToken": {
        "type": "apiKey",
        "in": "header",
        "name": "X-CSRF-Token",
        "description": "Token returned by GET /session/csrf for this cookie. Obtain a fresh token after signing in."
      }
    },
    "schemas": {
      "CsrfToken": {
        "type": "object",
        "required": [
          "csrfToken"
        ],
        "properties": {
          "csrfToken": {
            "type": "string",
            "minLength": 1
          }
        }
      },
      "Login": {
        "type": "object",
        "required": [
          "username",
          "password"
        ],
        "properties": {
          "username": {
            "type": "string",
            "minLength": 1,
            "maxLength": 128
          },
          "password": {
            "type": "string",
            "minLength": 1,
            "maxLength": 256,
            "writeOnly": true
          }
        },
        "additionalProperties": false
      },
      "Profile": {
        "type": "object",
        "required": [
          "user",
          "roles",
          "permissions"
        ],
        "properties": {
          "user": {
            "type": "object",
            "required": [
              "id",
              "username",
              "displayName"
            ],
            "properties": {
              "id": {
                "type": "string"
              },
              "username": {
                "type": "string"
              },
              "displayName": {
                "type": "string"
              }
            }
          },
          "roles": {
            "type": "array",
            "items": {
              "type": "string"
            }
          },
          "permissions": {
            "type": "array",
            "items": {
              "enum": [
                "admin",
                "model_manage",
                "process_start"
              ]
            }
          }
        }
      },
      "Problem": {
        "type": "object",
        "required": [
          "type",
          "title",
          "status"
        ],
        "properties": {
          "type": {
            "type": "string",
            "format": "uri-reference"
          },
          "title": {
            "type": "string"
          },
          "status": {
            "type": "integer"
          }
        }
      },
      "CreateUser": {
        "type": "object",
        "properties": {
          "displayName": {
            "type": "string",
            "minLength": 1,
            "maxLength": 128
          },
          "administrator": {
            "type": "boolean"
          },
          "permissions": {
            "type": "array",
            "maxItems": 2,
            "uniqueItems": true,
            "items": {
              "type": "string",
              "enum": [
                "model_manage",
                "process_start"
              ]
            }
          },
          "username": {
            "type": "string",
            "minLength": 1,
            "maxLength": 128
          },
          "password": {
            "type": "string",
            "minLength": 1,
            "maxLength": 256,
            "writeOnly": true
          },
          "roles": {
            "type": "array",
            "maxItems": 100,
            "uniqueItems": true,
            "items": {
              "type": "string",
              "minLength": 1,
              "maxLength": 128,
              "pattern": "^[A-Za-z_][A-Za-z0-9._-]*$"
            }
          }
        },
        "additionalProperties": false,
        "required": [
          "username",
          "password"
        ],
        "minProperties": 1
      },
      "UpdateUser": {
        "type": "object",
        "properties": {
          "displayName": {
            "type": "string",
            "minLength": 1,
            "maxLength": 128
          },
          "administrator": {
            "type": "boolean"
          },
          "permissions": {
            "type": "array",
            "maxItems": 2,
            "uniqueItems": true,
            "items": {
              "type": "string",
              "enum": [
                "model_manage",
                "process_start"
              ]
            }
          },
          "active": {
            "type": "boolean"
          }
        },
        "additionalProperties": false,
        "minProperties": 1
      },
      "User": {
        "type": "object",
        "required": [
          "id",
          "username",
          "displayName",
          "active",
          "roles",
          "administrator",
          "permissions"
        ],
        "properties": {
          "id": {
            "type": "string"
          },
          "username": {
            "type": "string"
          },
          "displayName": {
            "type": "string"
          },
          "active": {
            "type": "boolean"
          },
          "roles": {
            "type": "array",
            "items": {
              "type": "string"
            }
          },
          "administrator": {
            "type": "boolean"
          },
          "permissions": {
            "type": "array",
            "items": {
              "type": "string",
              "enum": [
                "admin",
                "model_manage",
                "process_start"
              ]
            }
          }
        }
      },
      "ChangePassword": {
        "type": "object",
        "properties": {
          "currentPassword": {
            "type": "string",
            "minLength": 1,
            "maxLength": 256,
            "writeOnly": true
          },
          "newPassword": {
            "type": "string",
            "minLength": 1,
            "maxLength": 256,
            "writeOnly": true
          }
        },
        "additionalProperties": false,
        "required": [
          "currentPassword",
          "newPassword"
        ],
        "minProperties": 1
      },
      "ResetPassword": {
        "type": "object",
        "properties": {
          "newPassword": {
            "type": "string",
            "minLength": 1,
            "maxLength": 256,
            "writeOnly": true
          }
        },
        "additionalProperties": false,
        "required": [
          "newPassword"
        ],
        "minProperties": 1
      }
    },
    "responses": {
      "Problem": {
        "description": "Machine-readable error",
        "content": {
          "application/problem+json": {
            "schema": {
              "$ref": "#/components/schemas/Problem"
            }
          }
        }
      }
    }
  }
}
