コンテンツにスキップ

OAuth 2.0

Authrimは最新のセキュリティベストプラクティスに従ったOAuth 2.0を実装しており、OAuth 2.1の推奨事項もサポートしています。

概要

対応グラントタイプ

認可コードグラント

すべてのクライアントに推奨されるグラント:

POST /token HTTP/1.1
Content-Type: application/x-www-form-urlencoded
grant_type=authorization_code
&code=auth_code_value
&redirect_uri=https://app.example.com/callback
&client_id=client_id
&code_verifier=pkce_verifier

リフレッシュトークングラント

リフレッシュトークンを新しいトークンと交換:

POST /token HTTP/1.1
Content-Type: application/x-www-form-urlencoded
Authorization: Basic <base64(client_id:client_secret)>
grant_type=refresh_token
&refresh_token=refresh_token_value

クライアントクレデンシャルグラント

マシン間認証用:

POST /token HTTP/1.1
Content-Type: application/x-www-form-urlencoded
Authorization: Basic <base64(client_id:client_secret)>
grant_type=client_credentials
&scope=api.read api.write

デバイス認可グラント

入力制約のあるデバイス用(デバイスフローを参照):

POST /token HTTP/1.1
Content-Type: application/x-www-form-urlencoded
grant_type=urn:ietf:params:oauth:grant-type:device_code
&device_code=device_code_value
&client_id=client_id

JWTベアラーグラント

信頼されたアサーション発行者との識別連携用:

POST /token HTTP/1.1
Content-Type: application/x-www-form-urlencoded
grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer
&assertion=jwt_assertion
&client_id=client_id

エンドポイント

エンドポイントメソッド説明
/authorizeGET/POST認可エンドポイント
/tokenPOSTトークンエンドポイント
/revokePOSTトークン失効
/introspectPOSTトークンイントロスペクション

トークンタイプ

アクセストークン

以下を含むJWT形式のアクセストークン:

{
"iss": "https://auth.example.com",
"sub": "user-id-123",
"aud": "https://api.example.com",
"exp": 1699880000,
"iat": 1699876400,
"jti": "unique-token-id",
"scope": "read write",
"client_id": "my_client_id"
}

リフレッシュトークン

新しいアクセストークンを取得するための長寿命トークン:

  • ローテーション: リフレッシュトークンは使用ごとにローテーション
  • 有効期限: 設定可能な有効期間(デフォルト:30日)
  • 失効: /revokeエンドポイントで失効可能

クライアント認証

client_secret_basic

クライアント資格情報によるHTTP Basic認証:

Authorization: Basic <base64(client_id:client_secret)>

client_secret_post

リクエストボディでクライアント資格情報を送信:

POST /token
Content-Type: application/x-www-form-urlencoded
client_id=my_client_id
&client_secret=my_client_secret
&grant_type=...

private_key_jwt

クライアントの秘密鍵で署名されたJWTクライアントアサーション:

POST /token
Content-Type: application/x-www-form-urlencoded
client_assertion_type=urn:ietf:params:oauth:client-assertion-type:jwt-bearer
&client_assertion=eyJhbGciOiJSUzI1NiJ9...
&grant_type=...

none

パブリッククライアント用(PKCEが必須):

POST /token
Content-Type: application/x-www-form-urlencoded
client_id=public_client_id
&grant_type=authorization_code
&code=...
&code_verifier=...

トークンイントロスペクション

トークンがアクティブかどうかを確認し、メタデータを取得:

POST /introspect HTTP/1.1
Content-Type: application/x-www-form-urlencoded
Authorization: Basic <base64(client_id:client_secret)>
token=access_token_value
&token_type_hint=access_token

レスポンス:

{
"active": true,
"scope": "read write",
"client_id": "my_client_id",
"username": "[email protected]",
"token_type": "Bearer",
"exp": 1699880000,
"iat": 1699876400,
"sub": "user-id-123",
"aud": "https://api.example.com",
"iss": "https://auth.example.com"
}

トークン失効

アクセストークンまたはリフレッシュトークンを失効:

POST /revoke HTTP/1.1
Content-Type: application/x-www-form-urlencoded
Authorization: Basic <base64(client_id:client_secret)>
token=token_value
&token_type_hint=refresh_token

レスポンス: 200 OK(RFC 7009に従い常に返す)

レスポンスモード

query(codeのデフォルト)

クエリ文字列に認可コード:

https://app.example.com/callback?code=abc123&state=xyz

fragment(implicitのデフォルト)

URLフラグメントにトークン:

https://app.example.com/callback#access_token=abc123&token_type=Bearer

form_post

POSTで認可レスポンスを送信:

<form method="post" action="https://app.example.com/callback">
<input type="hidden" name="code" value="abc123">
<input type="hidden" name="state" value="xyz">
</form>

エラーレスポンス

標準的なOAuth 2.0エラー形式:

{
"error": "invalid_request",
"error_description": "リクエストに必須パラメータがありません"
}

エラーコード:

エラー説明
invalid_requestパラメータが不足または無効
unauthorized_clientクライアントがグラントタイプに対して認可されていない
access_deniedユーザーが認可を拒否
unsupported_response_typeレスポンスタイプがサポートされていない
invalid_scopeリクエストされたスコープが無効
server_errorサーバーでエラーが発生
temporarily_unavailableサーバーが一時的に利用不可
invalid_clientクライアント認証に失敗
invalid_grantグラントが無効または期限切れ
unsupported_grant_typeグラントタイプがサポートされていない

OAuth 2.1準拠

AuthrimはOAuth 2.1の推奨事項に従います:

  • PKCEが必須 - 認可コードフローに対して
  • リフレッシュトークンローテーション - デフォルトで有効
  • インプリシットグラントなし - 新規アプリケーションに対して
  • パスワードグラントなし - 削除済み
  • 完全一致リダイレクトURI - 正確なマッチングのみ

セキュリティベストプラクティス

  1. PKCEを使用 - すべての認可コードフローに必須
  2. stateパラメータを使用 - CSRF攻撃を防止
  3. リダイレクトURIを検証 - 完全一致のみ
  4. 短寿命アクセストークンを使用 - 1時間以下
  5. リフレッシュトークンをローテーション - 使い捨て
  6. トークンバインディングを実装 - 高セキュリティにはDPoPを検討

参照