{
  "openapi": "3.0.0",
  "info": {
    "title": "OAuth API Reference",
    "description": "OAuth 2.0 authorization, token management, and dynamic client registration (RFC 7591/7592)",
    "version": "1.0.0",
    "contact": {
      "name": "iClosed.io Support",
      "url": "https://app.iclosed.io/support"
    },
    "license": {
      "name": "Proprietary",
      "url": "https://app.iclosed.io/terms"
    }
  },
  "servers": [
    {
      "url": "https://public.api.iclosed.io",
      "description": "Production"
    }
  ],
  "paths": {
    "/v1/oauth/authorize": {
      "get": {
        "operationId": "oauthAuthorize",
        "summary": "OAuth authorization endpoint (authorization code)",
        "description": "Backend step after the iClosed consent UI (`https://app.iclosed.io/oauth/authorize`): validates client, redirect URI, and scopes, requires an authenticated resource owner session (Bearer session JWT from login — not an API key or OAuth access token), then redirects to `redirect_uri` with `code` and optional `state`. Third-party OAuth clients start the flow in the browser at the consent URL; they do not call this endpoint directly.",
        "security": [],
        "parameters": [
          {
            "name": "response_type",
            "in": "query",
            "schema": {
              "type": "string",
              "enum": [
                "code"
              ]
            },
            "required": true,
            "description": "OAuth response type. Must be \"code\"."
          },
          {
            "name": "client_id",
            "in": "query",
            "schema": {
              "description": "OAuth client identifier.",
              "type": "string",
              "minLength": 1,
              "maxLength": 255
            },
            "required": true,
            "description": "OAuth client identifier."
          },
          {
            "name": "redirect_uri",
            "in": "query",
            "schema": {
              "description": "Redirect URI registered for the client. Must use https://, or http:// on localhost.",
              "type": "string",
              "maxLength": 2048,
              "format": "uri"
            },
            "required": true,
            "description": "Redirect URI registered for the client. Must use https://, or http:// on localhost."
          },
          {
            "name": "scope",
            "in": "query",
            "schema": {
              "description": "Space-delimited scopes requested by the client. Defaults to the full client-allowed scope set.",
              "type": "string",
              "maxLength": 2000
            },
            "description": "Space-delimited scopes requested by the client. Defaults to the full client-allowed scope set."
          },
          {
            "name": "state",
            "in": "query",
            "schema": {
              "description": "Opaque value returned unchanged to prevent CSRF in client-side state handling.",
              "type": "string",
              "maxLength": 2048
            },
            "description": "Opaque value returned unchanged to prevent CSRF in client-side state handling."
          },
          {
            "name": "code_challenge",
            "in": "query",
            "schema": {
              "description": "PKCE code challenge. Required together with code_challenge_method for public clients and for every client registered through anonymous dynamic client registration; optional for account-owned confidential clients.",
              "type": "string",
              "minLength": 43,
              "maxLength": 128
            },
            "description": "PKCE code challenge. Required together with code_challenge_method for public clients and for every client registered through anonymous dynamic client registration; optional for account-owned confidential clients."
          },
          {
            "name": "code_challenge_method",
            "in": "query",
            "schema": {
              "type": "string",
              "enum": [
                "S256"
              ]
            },
            "description": "PKCE method; required when code_challenge is present. Only S256 is supported."
          }
        ],
        "responses": {
          "302": {
            "description": "Redirect to the validated redirect_uri with code/state query params"
          },
          "400": {
            "description": "OAuth request validation failed",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string"
                    },
                    "error_description": {
                      "type": "string"
                    }
                  },
                  "required": [
                    "error"
                  ],
                  "additionalProperties": false
                },
                "example": {
                  "error": "invalid_grant",
                  "error_description": "Authorization code has already been used"
                }
              }
            }
          },
          "401": {
            "description": "Resource owner not authenticated or invalid OAuth client credentials",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string"
                    },
                    "error_description": {
                      "type": "string"
                    }
                  },
                  "required": [
                    "error"
                  ],
                  "additionalProperties": false
                }
              }
            }
          },
          "429": {
            "description": "Account rate limit exceeded"
          }
        }
      }
    },
    "/v1/oauth/token": {
      "post": {
        "operationId": "oauthToken",
        "summary": "OAuth token endpoint",
        "description": "Exchanges an authorization code for access/refresh tokens (with `code_verifier` when the code was issued with PKCE), or rotates refresh tokens. Supports `authorization_code` and `refresh_token` grants. Referenced by `components.securitySchemes.oauth2` for MCP and OpenAPI OAuth clients.",
        "security": [],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "grant_type": {
                    "type": "string",
                    "enum": [
                      "authorization_code",
                      "refresh_token"
                    ]
                  },
                  "code": {
                    "type": "string",
                    "minLength": 1,
                    "maxLength": 255
                  },
                  "redirect_uri": {
                    "type": "string",
                    "maxLength": 2048,
                    "format": "uri"
                  },
                  "code_verifier": {
                    "type": "string",
                    "minLength": 43,
                    "maxLength": 128
                  },
                  "refresh_token": {
                    "type": "string",
                    "minLength": 1,
                    "maxLength": 255
                  },
                  "scope": {
                    "type": "string",
                    "maxLength": 2000
                  },
                  "client_id": {
                    "type": "string",
                    "minLength": 1,
                    "maxLength": 255
                  },
                  "client_secret": {
                    "type": "string",
                    "minLength": 1,
                    "maxLength": 255
                  }
                },
                "required": [
                  "grant_type"
                ],
                "additionalProperties": false
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "OAuth access token response",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "access_token": {
                      "type": "string"
                    },
                    "token_type": {
                      "type": "string",
                      "enum": [
                        "Bearer"
                      ]
                    },
                    "expires_in": {
                      "type": "integer",
                      "exclusiveMinimum": true,
                      "minimum": 0
                    },
                    "refresh_token": {
                      "type": "string"
                    },
                    "scope": {
                      "type": "string"
                    }
                  },
                  "required": [
                    "access_token",
                    "token_type",
                    "expires_in",
                    "refresh_token",
                    "scope"
                  ],
                  "additionalProperties": false
                },
                "example": {
                  "access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
                  "token_type": "Bearer",
                  "expires_in": 3600,
                  "refresh_token": "kN9b-4wFQxQXoXr3Jra6D2xSAn8YvXSVF0yP9fvQX1A",
                  "scope": "contacts:read contacts:write"
                }
              }
            }
          },
          "400": {
            "description": "OAuth token request failed (invalid_grant, invalid_scope, unsupported_grant_type)",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string"
                    },
                    "error_description": {
                      "type": "string"
                    }
                  },
                  "required": [
                    "error"
                  ],
                  "additionalProperties": false
                },
                "example": {
                  "error": "invalid_grant",
                  "error_description": "Authorization code has already been used"
                }
              }
            }
          },
          "401": {
            "description": "Client authentication failed",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string"
                    },
                    "error_description": {
                      "type": "string"
                    }
                  },
                  "required": [
                    "error"
                  ],
                  "additionalProperties": false
                },
                "example": {
                  "error": "invalid_client",
                  "error_description": "client authentication failed"
                }
              }
            }
          },
          "429": {
            "description": "IP rate limit exceeded"
          }
        }
      }
    },
    "/v1/oauth/revoke": {
      "post": {
        "operationId": "oauthRevoke",
        "summary": "OAuth token revocation endpoint",
        "description": "Revokes an access token or refresh token. Endpoint is idempotent and always returns 200 for known clients.",
        "security": [],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "token": {
                    "type": "string",
                    "minLength": 1,
                    "maxLength": 2048
                  },
                  "token_type_hint": {
                    "type": "string",
                    "enum": [
                      "access_token",
                      "refresh_token"
                    ]
                  },
                  "client_id": {
                    "type": "string",
                    "minLength": 1,
                    "maxLength": 255
                  },
                  "client_secret": {
                    "type": "string",
                    "minLength": 1,
                    "maxLength": 255
                  }
                },
                "required": [
                  "token"
                ],
                "additionalProperties": false
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Token revoked (or token not found)",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "additionalProperties": false
                },
                "example": {}
              }
            }
          },
          "401": {
            "description": "Client authentication failed",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string"
                    },
                    "error_description": {
                      "type": "string"
                    }
                  },
                  "required": [
                    "error"
                  ],
                  "additionalProperties": false
                }
              }
            }
          },
          "429": {
            "description": "IP rate limit exceeded"
          }
        }
      }
    }
  },
  "components": {}
}