{
  "openapi": "3.0.0",
  "info": {
    "title": "Authentication API",
    "version": "1.0.0",
    "description": "API for authentication and authorization"
  },
  "tags": [
    {
      "name": "Authentication",
      "description": "Authentication operations"
    }
  ],
  "paths": {
    "/v1/authenticate": {
      "post": {
        "summary": "Login",
        "description": "Authenticate user and get tokens. If MFA is enabled for the user, returns MFA challenge instead of tokens.",
        "tags": ["Authentication"],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/AuthCredentials"
              },
              "examples": {
                "basic": {
                  "summary": "Basic login",
                  "value": {
                    "username": "user@example.com",
                    "password": "secure_password"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Authentication successful or MFA required",
            "content": {
              "application/json": {
                "schema": {
                  "oneOf": [
                    {
                      "$ref": "#/components/schemas/TokenResponse"
                    },
                    {
                      "$ref": "#/components/schemas/MFAResponse"
                    }
                  ]
                },
                "examples": {
                  "tokens": {
                    "summary": "Direct authentication success",
                    "value": {
                      "access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
                      "expires_in": 900,
                      "user_id": "123e4567-e89b-12d3-a456-426614174000"
                    }
                  },
                  "mfa_required": {
                    "summary": "MFA verification required",
                    "value": {
                      "credential_type": "phone",
                      "user_id": "123e4567-e89b-12d3-a456-426614174000",
                      "message": "An OTP has been sent to your phone successfully."
                    }
                  }
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/InvalidCredentials"
          },
          "429": {
            "$ref": "#/components/responses/AccountLocked"
          }
        }
      }
    },
    "/v1/verify-2fa": {
      "post": {
        "summary": "Verify 2FA",
        "description": "Verify 2FA code and complete authentication",
        "tags": ["Authentication"],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/VerifyOTPRequest"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "2FA verification successful",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/TokenResponse"
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/InvalidOTP"
          }
        }
      }
    },
    "/v1/refresh-token": {
      "post": {
        "summary": "Refresh token",
        "description": "Get new access token using refresh token",
        "tags": ["Authentication"],
        "responses": {
          "200": {
            "description": "Token refresh successful",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/TokenResponse"
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/InvalidToken"
          }
        }
      }
    },
    "/v1/logout": {
      "post": {
        "summary": "Logout",
        "description": "Invalidate tokens and end session",
        "tags": ["Authentication"],
        "responses": {
          "200": {
            "description": "Logout successful",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/SuccessResponse"
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "AuthCredentials": {
        "type": "object",
        "required": ["username", "password"],
        "properties": {
          "username": {
            "type": "string",
            "example": "user@example.com"
          },
          "password": {
            "type": "string",
            "format": "password",
            "example": "secure_password"
          }
        }
      },
      "TokenResponse": {
        "type": "object",
        "required": ["access_token", "expires_in", "user_id"],
        "properties": {
          "access_token": {
            "type": "string"
          },
          "expires_in": {
            "type": "integer",
            "description": "Token expiration in seconds"
          },
          "user_id": {
            "type": "string",
            "format": "uuid"
          }
        }
      },
      "MFAResponse": {
        "type": "object",
        "required": ["credential_type", "user_id", "message"],
        "properties": {
          "credential_type": {
            "type": "string",
            "enum": ["email", "phone", "totp"],
            "description": "Type of MFA credential to be verified"
          },
          "user_id": {
            "type": "string",
            "format": "uuid",
            "description": "User ID needed for the subsequent `Verify 2FA` call"
          },
          "message": {
            "type": "string",
            "description": "Human-readable message explaining what action has been completed",
            "example": "An OTP has been sent to your phone successfully."
          }
        }
      },
      "VerifyOTPRequest": {
        "type": "object",
        "required": ["user_id", "otp"],
        "properties": {
          "user_id": {
            "type": "string",
            "format": "uuid"
          },
          "otp": {
            "type": "string",
            "minLength": 6,
            "maxLength": 6
          }
        }
      },
      "SuccessResponse": {
        "type": "object",
        "required": ["message"],
        "properties": {
          "message": {
            "type": "string"
          }
        }
      },
      "Error": {
        "type": "object",
        "required": ["code", "message"],
        "properties": {
          "code": {
            "type": "string"
          },
          "message": {
            "type": "string"
          }
        }
      }
    },
    "responses": {
      "InvalidCredentials": {
        "description": "Invalid credentials",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/Error"
            },
            "example": {
              "code": "auth.unauthorized",
              "message": "Invalid credentials"
            }
          }
        }
      },
      "AccountLocked": {
        "description": "Account locked",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/Error"
            },
            "example": {
              "code": "auth.account_locked",
              "message": "Account locked for 15 minutes"
            }
          }
        }
      },
      "InvalidToken": {
        "description": "Invalid token",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/Error"
            },
            "example": {
              "code": "auth.invalid_token",
              "message": "Invalid or expired token"
            }
          }
        }
      },
      "InvalidOTP": {
        "description": "Invalid OTP",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/Error"
            },
            "example": {
              "code": "auth.invalid_otp",
              "message": "Invalid or expired OTP"
            }
          }
        }
      }
    },
    "securitySchemes": {
      "bearerAuth": {
        "type": "http",
        "scheme": "bearer",
        "bearerFormat": "JWT"
      },
      "apiKeyAuth": {
        "type": "apiKey",
        "in": "header",
        "name": "X-API-Key"
      }
    }
  },
  "security": [
    {
      "bearerAuth": []
    },
    {
      "apiKeyAuth": []
    }
  ]
}