コンテンツにスキップ

OpenID Connect (OIDC)

AuthrimはOpenID Connect Core 1.0に完全準拠しており、アプリケーションに安全な認証を提供します。

概要

  • 標準: OpenID Connect Core 1.0
  • 適合性: OpenID認定適合性テストスイート対応
  • ディスカバリー: 完全なOIDCディスカバリーサポート

対応フロー

認可コードフロー + PKCE(推奨)

すべてのクライアントタイプに最も安全なフロー:

クライアント → 認可エンドポイント → ユーザーログイン → 認可コード
クライアント → トークンエンドポイント (+ PKCE) → アクセストークン + IDトークン

ハイブリッドフロー

即時のID検証とバックエンドトークン交換の両方が必要なアプリケーション向け:

  • code id_token
  • code token
  • code id_token token

インプリシットフロー

レガシーアプリケーション専用(新規実装には非推奨):

  • id_token
  • id_token token

ディスカバリーエンドポイント

OpenIDプロバイダー設定を取得:

GET /.well-known/openid-configuration

レスポンス:

{
"issuer": "https://auth.example.com",
"authorization_endpoint": "https://auth.example.com/authorize",
"token_endpoint": "https://auth.example.com/token",
"userinfo_endpoint": "https://auth.example.com/userinfo",
"jwks_uri": "https://auth.example.com/.well-known/jwks.json",
"registration_endpoint": "https://auth.example.com/register",
"scopes_supported": ["openid", "profile", "email", "address", "phone", "offline_access"],
"response_types_supported": ["code", "id_token", "id_token token", "code id_token", "code token", "code id_token token"],
"response_modes_supported": ["query", "fragment", "form_post"],
"grant_types_supported": ["authorization_code", "refresh_token", "urn:ietf:params:oauth:grant-type:device_code"],
"subject_types_supported": ["public", "pairwise"],
"id_token_signing_alg_values_supported": ["RS256", "ES256"],
"token_endpoint_auth_methods_supported": ["client_secret_basic", "client_secret_post", "private_key_jwt"],
"code_challenge_methods_supported": ["S256"]
}

JWKSエンドポイント

IDトークン検証用のJSON Web Key Setを取得:

GET /.well-known/jwks.json

レスポンス:

{
"keys": [
{
"kty": "RSA",
"kid": "key-id-1",
"use": "sig",
"alg": "RS256",
"n": "...",
"e": "AQAB"
}
]
}

認可フロー

1. 認可リクエスト

GET /authorize
?response_type=code
&client_id=my_client_id
&redirect_uri=https://myapp.example.com/callback
&scope=openid+profile+email
&state=random_state_value
&nonce=random_nonce_value
&code_challenge=...
&code_challenge_method=S256
Host: auth.example.com

パラメータ:

パラメータ必須説明
response_typeはい認可コードフローの場合はcode
client_idはいクライアント識別子
redirect_uriはい登録済みリダイレクトURI
scopeはいopenidを含む必要あり
state推奨CSRF保護
nonceOIDC必須IDトークンバインディング
code_challenge推奨PKCEコードチャレンジ
code_challenge_method推奨S256である必要あり

2. ユーザー認証

ユーザーはログインページにリダイレクトされ、認証を行います。

3. 認可レスポンス

HTTP/1.1 302 Found
Location: https://myapp.example.com/callback
?code=authorization_code_value
&state=random_state_value

4. トークンリクエスト

POST /token HTTP/1.1
Content-Type: application/x-www-form-urlencoded
Authorization: Basic <base64(client_id:client_secret)>
grant_type=authorization_code
&code=authorization_code_value
&redirect_uri=https://myapp.example.com/callback
&code_verifier=pkce_code_verifier

5. トークンレスポンス

{
"access_token": "eyJhbGciOiJSUzI1NiJ9...",
"token_type": "Bearer",
"expires_in": 3600,
"id_token": "eyJhbGciOiJSUzI1NiJ9...",
"refresh_token": "eyJhbGciOiJSUzI1NiJ9...",
"scope": "openid profile email"
}

IDトークン

IDトークンはユーザーIDクレームを含むJSON Web Token (JWT)です:

{
"iss": "https://auth.example.com",
"sub": "user-id-123",
"aud": "my_client_id",
"exp": 1699880000,
"iat": 1699876400,
"nonce": "random_nonce_value",
"auth_time": 1699876400,
"acr": "urn:mace:incommon:iap:silver",
"at_hash": "...",
"name": "John Doe",
"email": "[email protected]",
"email_verified": true
}

標準クレーム

クレーム説明
iss発行者識別子
subサブジェクト識別子(ユーザーID)
audオーディエンス(クライアントID)
exp有効期限
iat発行時刻
nonce認可リクエストからのnonce
auth_time認証時刻
acr認証コンテキストクラス参照
amr認証方法参照
at_hashアクセストークンハッシュ
c_hashコードハッシュ(ハイブリッドフロー)

プロファイルスコープクレーム

クレーム説明
nameフルネーム
given_name
family_name
nicknameニックネーム
preferred_username優先ユーザー名
pictureプロファイル画像URL
localeロケール
zoneinfoタイムゾーン
updated_at最終更新時刻

メールスコープクレーム

クレーム説明
emailメールアドレス
email_verifiedメール検証状態

UserInfoエンドポイント

ユーザープロファイル情報を取得:

GET /userinfo HTTP/1.1
Authorization: Bearer <access_token>

レスポンス:

{
"sub": "user-id-123",
"name": "John Doe",
"given_name": "John",
"family_name": "Doe",
"email": "[email protected]",
"email_verified": true,
"picture": "https://example.com/photo.jpg"
}

クライアント登録

動的クライアント登録

POST /register HTTP/1.1
Content-Type: application/json
{
"client_name": "My Application",
"redirect_uris": ["https://myapp.example.com/callback"],
"grant_types": ["authorization_code", "refresh_token"],
"response_types": ["code"],
"token_endpoint_auth_method": "client_secret_basic"
}

レスポンス:

{
"client_id": "generated_client_id",
"client_secret": "generated_client_secret",
"client_id_issued_at": 1699876400,
"client_secret_expires_at": 0,
"redirect_uris": ["https://myapp.example.com/callback"],
"grant_types": ["authorization_code", "refresh_token"],
"response_types": ["code"]
}

スコープ

スコープ説明
openidOIDC必須、subクレームを返す
profileプロファイルクレームを返す(name、picture等)
emailemailとemail_verifiedクレームを返す
addressaddressクレームを返す
phonephone_numberとphone_number_verifiedを返す
offline_accessリフレッシュトークンを返す

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

  1. 常にPKCEを使用 - 認可コードの傍受を防止
  2. stateパラメータを使用 - CSRF攻撃を防止
  3. nonceパラメータを使用 - IDトークンのリプレイを防止
  4. IDトークンを検証 - 署名、発行者、オーディエンス、有効期限を検証
  5. 短寿命トークンを使用 - トークン盗難の影響を最小化
  6. 適切なログアウトを実装 - ログアウト時にトークンを失効

参照