OpenAPI: JWT認証ログインエンドポイント

OpenAPI 3.0でJWTベースの認証フローを文書化。ログイン、トークンリフレッシュ、ログアウトエンドポイント、Bearerトークンセキュリティスキーム、トークンレスポンススキーマを含みます。

Authentication

詳細な説明

OpenAPIでのJWT認証の文書化

JWT認証はステートレスAPI認証の最も一般的なパターンです。OpenAPI仕様では、エンドポイントとセキュリティスキームの両方を定義できます。

セキュリティスキーム定義

components:
  securitySchemes:
    BearerAuth:
      type: http
      scheme: bearer
      bearerFormat: JWT
      description: JWT access token obtained from /auth/login

ログインエンドポイント

paths:
  /auth/login:
    post:
      summary: Authenticate user and obtain JWT
      tags:
        - Authentication
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required:
                - email
                - password
              properties:
                email:
                  type: string
                password:
                  type: string
      responses:
        '200':
          description: Login successful
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/TokenResponse'
        '401':
          description: Invalid credentials

トークンレスポンススキーマ

components:
  schemas:
    TokenResponse:
      type: object
      required:
        - accessToken
        - tokenType
        - expiresIn
      properties:
        accessToken:
          type: string
          description: JWT access token
        refreshToken:
          type: string
          description: Token for obtaining new access tokens
        tokenType:
          type: string
          description: Always "Bearer"
        expiresIn:
          type: integer
          description: Token lifetime in seconds

トークンリフレッシュエンドポイント

リフレッシュエンドポイントはリフレッシュトークンを受け入れ、認証情報を再度要求せずに新しいアクセストークンを返します。これはアクセストークンの短い有効期間(通常15-30分)を超えてセッションを維持する必要があるモバイルアプリやSPAに不可欠です。

グローバルなセキュリティ適用

トップレベルのsecurityフィールドを使用してすべてのエンドポイントに認証を要求し、ログインや登録などのパブリックエンドポイントには空の配列でオーバーライドします。

ユースケース

モバイルアプリ、SPA、またはサードパーティクライアントがメール/パスワードで認証し、後続のAPI呼び出し用にJWTトークンを受け取るバックエンド認証システムの文書化に使用。

試してみる — API Documentation Generator

フルツールを開く