跳到內容

OpenID Connect Discovery

OpenID Connect Discovery 1.0 定義了 Relying Party(RP)自動取得 OpenID Provider(OP)設定資訊的標準機制,於 2014 年發布。此規範是 RFC 8414(OAuth 2.0 Authorization Server Metadata) 的前身——RFC 8414 是在此基礎上泛化而來的。

要串接一個 OP,RP 需要知道很多資訊:Authorization Endpoint 在哪裡、Token Endpoint 在哪裡、支援哪些 scope、用什麼演算法簽署 ID Token 等。如果每次串接都要手動查文件、寫死設定,會有幾個問題:

  • 維護困難——OP 更新端點或設定時,所有 RP 都要跟著改
  • 無法自動化——新的 RP 無法在執行期間自動取得串接資訊
  • 容易出錯——手動設定容易遺漏或打錯

Discovery 讓 RP 只要知道 OP 的 Issuer URL,就能透過標準化的端點自動取得所有設定。

RP 向 OP 的 well-known 端點發送 GET 請求:

GET /.well-known/openid-configuration HTTP/1.1
Host: accounts.google.com

OP 回傳一個 JSON 物件,稱為 OpenID Provider Metadata

{
"issuer": "https://accounts.google.com",
"authorization_endpoint": "https://accounts.google.com/o/oauth2/v2/auth",
"token_endpoint": "https://oauth2.googleapis.com/token",
"userinfo_endpoint": "https://openidconnect.googleapis.com/v1/userinfo",
"jwks_uri": "https://www.googleapis.com/oauth2/v3/certs",
"scopes_supported": ["openid", "profile", "email"],
"response_types_supported": ["code", "id_token", "code id_token"],
"grant_types_supported": ["authorization_code", "implicit"],
"id_token_signing_alg_values_supported": ["RS256"]
}
欄位說明
issuerOP 的識別 URL,與 ID Token 的 iss 值一致
authorization_endpoint啟動身分驗證的端點
jwks_uri驗證 ID Token 簽章用的公鑰端點(JWK Set
response_types_supported支援的 response_type
subject_types_supported支援的 Subject Identifier 類型(如 publicpairwise
id_token_signing_alg_values_supported支援的 ID Token 簽章演算法
欄位必要說明
token_endpointREQUIRED / OPTIONALToken 端點(僅使用 Implicit Flow 時為 OPTIONAL)
userinfo_endpointRECOMMENDED取得使用者資訊的端點
scopes_supportedRECOMMENDED支援的 scope 值
response_modes_supportedOPTIONAL支援的 response_mode
grant_types_supportedOPTIONAL支援的 grant_type
registration_endpointOPTIONALDynamic Client Registration 的端點
claims_supportedRECOMMENDED支援的 Claims 名稱

實務上,RP 的套件在串接 OIDC 時,通常會自動透過 Discovery 取得這些資訊,開發者只需要提供 Issuer URL 即可。

ProviderDiscovery URL
Googlehttps://accounts.google.com/.well-known/openid-configuration
Microsofthttps://login.microsoftonline.com/common/v2.0/.well-known/openid-configuration
LINEhttps://access.line.me/.well-known/openid-configuration

與 OAuth 2.0 Authorization Server Metadata 的關係

Section titled “與 OAuth 2.0 Authorization Server Metadata 的關係”

OIDC Discovery 是原始規範(2014),RFC 8414 是從它泛化而來的(2018):

OIDC DiscoveryRFC 8414
Well-known 路徑/.well-known/openid-configuration/.well-known/oauth-authorization-server
OIDC 欄位有(userinfo_endpointid_token_signing_alg_values_supported 等)無,只有通用的 OAuth 2.0 欄位
發布時間20142018

支援 OIDC 的 OP 通常同時提供兩個端點。RFC 8414 也建議,如果 /.well-known/oauth-authorization-server 取不到,可以 fallback 到 OIDC 的 /.well-known/openid-configuration