跳到主內容
UniKit

OAuth2 流程建構

產生 OAuth 2.0 授權 URL:response_type、client_id、redirect_uri、scope、state、nonce 與 PKCE(code_challenge / S256)一應俱全,附授權碼、隱式、用戶端憑證三種流程說明與範例程式碼。

瀏覽器本機執行所有計算都在你的瀏覽器裡完成,資料不會離開本機。

授權流程

只在本地拼接參數與範例程式碼,不會發起任何授權或 token 請求;SHA-256 用瀏覽器的 WebCrypto 計算,verifier 不會離開頁面。

授權 URL

拼好的授權連結
https://auth.example.com/authorize?response_type=code&client_id=unikit-demo&redirect_uri=https%3A%2F%2Funikit.cc%2Foauth%2Fcallback&scope=openid%20profile%20email&state=f8a1c2d3e4b5a6978877665544332211&nonce=n-0S6_WzA2Mj&code_challenge=E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM&code_challenge_method=S256
查詢參數
response_typecode
client_idunikit-demo
redirect_urihttps://unikit.cc/oauth/callback
scopeopenid profile email
statef8a1c2d3e4b5a6978877665544332211
noncen-0S6_WzA2Mj
code_challengeE9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM
code_challenge_methodS256

流程說明

  1. 用戶端產生 code_verifier 與 code_challenge,把瀏覽器跳到授權端點(response_type=code)。
  2. 使用者在授權伺服器登入並同意授權,瀏覽器帶著 code 回到 redirect_uri。
  3. 伺服器端用 code + code_verifier 呼叫 token 端點,換回 access_token 與 refresh_token。
  4. access_token 放進 Authorization: Bearer 標頭存取資源,refresh_token 用於續期。

參數檢查

參數齊全,PKCE 與 state 都符合規範。

範例程式碼

// 授权码 + PKCE:verifier 只保存在本地,challenge 才发给授权服务器
const verifier = 'YOUR_CODE_VERIFIER';
const digest = await crypto.subtle.digest('SHA-256', new TextEncoder().encode(verifier));
const codeChallenge = base64UrlEncode(new Uint8Array(digest));
// code_challenge=E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM (S256)
window.location.href = 'https://auth.example.com/authorize?response_type=code&client_id=unikit-demo&redirect_uri=https%3A%2F%2Funikit.cc%2Foauth%2Fcallback&scope=openid%20profile%20email&state=f8a1c2d3e4b5a6978877665544332211&nonce=n-0S6_WzA2Mj&code_challenge=E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM&code_challenge_method=S256';
// 回调拿到 code 后,在服务端用 code + verifier 换 token
// POST https://auth.example.com/token  grant_type=authorization_code
換取 token 的 curl
curl -X POST 'https://auth.example.com/token' \ \
  -H 'content-type: application/x-www-form-urlencoded' \ \
  -d 'grant_type=authorization_code&code=$CODE&redirect_uri=https://unikit.cc/oauth/callback' \ \
  -d 'client_id=unikit-demo' \
  -d 'code_verifier=$VERIFIER'

這個工具能做什麼

  • 接第三方登入時先生成一條授權連結,確認 response_type、scope、state 與 redirect_uri 都寫對了再貼進程式碼。
  • 要上 PKCE:點一下生成 code_verifier 與 S256 的 code_challenge,verifier 留在本地,challenge 才發給授權伺服器。
  • 對比三種流程:授權碼(服務端換 token)、隱式(已不推薦)、客戶端憑證(無使用者參與),每種都給出步驟說明與 curl 示例。

範例

輸入

流程 授權碼;授權端點 https://auth.example.com/authorize;client_id unikit-demo;redirect_uri https://unikit.cc/oauth/callback;scope openid profile email;state f8a1c2d3e4b5a6978877665544332211;nonce n-0S6_WzA2Mj;code_challenge_method S256

輸出

https://auth.example.com/authorize?response_type=code&client_id=unikit-demo&redirect_uri=https%3A%2F%2Funikit.cc%2Foauth%2Fcallback&scope=openid%20profile%20email&state=f8a1c2d3e4b5a6978877665544332211&nonce=n-0S6_WzA2Mj&code_challenge=...&code_challenge_method=S256

引數順序固定為 response_type → client_id → redirect_uri → scope → state → nonce → code_challenge,空格編成 %20。

常見問題

S256 的 code_challenge 是怎麼算出來的?

先生成 43–128 個 unreserved 字元的 code_verifier,再做 base64url(SHA-256(verifier))。工具用瀏覽器的 WebCrypto 算摘要,純邏輯部分只做 base64url 編碼,因此單測裡可以用 RFC 7636 附錄 B 的公開向量(verifier dBjftJeZ… → challenge E9Melhoa2Ow…)交叉驗證。

state 到底防的是什麼?

防 CSRF:把隨機 state 存進會話,回撥時比對,攻擊者偽造的授權響應就會因為 state 不匹配被拒。建議 16 位以上隨機字元,不要用可預測的時間戳或使用者 ID。

隱式流程還能用嗎?

能跑,但不建議。token 直接出現在 URL 片段裡,容易洩漏到瀏覽器歷史與日誌,且沒有 refresh_token;OAuth 2.1 已移除該流程,新專案請用授權碼 + PKCE。

客戶端憑證流程為什麼沒有授權 URL?

因為它沒有使用者參與授權,客戶端直接用自己的憑據向 token 端點換取 token。client_secret 只能在服務端儲存,任何出現在前端程式碼裡的 secret 都等同於公開。

工具會去請求授權伺服器嗎?

不會。工具只拼引數、生成示例程式碼並做規則檢查,不發起任何授權或 token 請求,也不會把你填的 client_id 傳送到任何地方。

關鍵字:OAuth2 流程授权链接生成PKCE 生成授权码模式oauth2oauth2 flowauthorization urlpkcecode challengeauthorization codeclient credentialsstate nonce

同類工具