PKCE OIDC Flow

rclimer

web

431 Words … ⏲ Reading Time: 1 Minute, 57 Seconds

2025-01-11 14:18 +0000


PKCE OIDC flow

PKCE → Proof Key for Code Exchange by OAuth Public Client

  • PKCE 是為了要解決假的client 冒充合法的client 取得user token

  • OIDC 可能被攻擊的方式:
    偷到 client id, client secretauthorization code. 然後冒充 client 打 request 到 authorization server 換到 user token

pkce-1.png

Authorization Code Interception Attack

Notes

  • 因為一般的 oidc flow,authorization code 有可能會被攔劫,這樣就有可能可以代替 user 去打給 authorization server 換 access token

  • 所以需要在 client 在(1)就先 generate code_challengestate

  • 在 (2) 時,帶著 state, code_challengecode_challenge_method 到 authorization server. Authorization server 會把 code_challengecode_challenge_method 和對應發出去的 authorization_code都記下來

  • state 是在 (4) 的時候在 client 做verify

  • (6)的時候,client 會把在(1)產生的code_challenge加密後,放在 code_verifier 當參數帶在 request 上

  • authorization server 在 (6) 把 client 當初帶來的 code_challenge hash, 跟這次帶來的 code_challenge 比對是否相同,就可以確認是否這個request 一樣來自合法的client

  • code_verifier 是一個generate 出來的 random string.

    • 在 plain, 也就是沒有加密的情況下
      code_challenge = code_verifier.

    • 而在 S256 的情況下
      code_challenge = BASE64URL-ENCODE(SHA256(ASCII(code_verifier)))

pkce-2.png

Abstract Protocol Flow

Ref: