Skip to content

feat(auth/oidc): enable PKCE by default and add WithAuthCodeOptions - #25

Merged
mackee merged 1 commit into
mainfrom
feat/oidc-pkce-auth-code-options
Oct 8, 2026
Merged

mackee merged 1 commit into
mainfrom
feat/oidc-pkce-auth-code-options

Conversation

@mackee

@mackee mackee commented Oct 8, 2026

Copy link
Copy Markdown
Owner

Summary

auth/oidc の認可リクエストに、PKCE と任意のパラメータ(prompt・hd など)を付けられるようにしました。これまでは Redirect が AuthCodeURL(state, oidc.Nonce(nonce)) に固定されていて、どちらも付けられませんでした。

Changes

PKCE をデフォルトで有効化(挙動の変更)

  • Redirect で code verifier を生成してセッションに保存し、認可 URL に S256 の code_challenge を付けます。
  • Callback はセッションから verifier を取り出して削除し、トークン交換に code_verifier として渡します。
  • セッションに verifier が無い場合は 400 を返します。PKCE を使わない交換にフォールバックすると、PKCE 導入前のセッションを使って PKCE を回避できてしまうためです。
  • WithoutPKCE() で無効にできます。

PKCE に対応していない IdP でも、RFC 6749 §3.1 により知らないパラメータは無視されるため、通常は影響ありません。

WithAuthCodeOptions(opts ...oauth2.AuthCodeOption) の追加

oidc.WithAuthCodeOptions[*Registry](
    oauth2.SetAuthURLParam("prompt", "select_account"),
    oauth2.SetAuthURLParam("hd", "example.com"),
)

Redirect が設定する state / nonce / PKCE のパラメータより先に適用するので、これらを上書きすることはできません。

既存の利用者への影響

PKCE のデフォルト有効化で失敗するのは、次のケースに限られます。

ケース 起きること 対処
デプロイ中にログイン途中だった人 コールバックで 1 回だけ 400 再ログインで回復
ローリングデプロイで新旧が混在する間 400、または IdP がトークン交換を拒否 ロールアウト完了で解消
認可 URL を自前で作り、Callback だけ使っている 毎回 400 WithoutPKCE()
Redirect だけ使い、トークン交換は自前 IdP が code_verifier 不足で拒否 WithoutPKCE()
PKCE を拒否する IdP トークン交換の失敗 WithoutPKCE()

Redirect と Callback を別々の Handlers で組み立てていても、セッションを共有していて PKCE の設定が揃っていれば問題ありません。README に PKCE と WithAuthCodeOptions の節を追加しました。

Test

  • ログインフローのテスト用ヘルパーを flow_test.go に切り出しました。偽 IdP は、実際の IdP と同様に code_verifier を検証します(challenge が無いのに verifier が来た場合も拒否)。
  • pkce_test.go で次の挙動を確認しています。
    • デフォルトで S256 が付き、ログインが成功する
    • WithoutPKCE では code_challenge を送らない
    • verifier が一致しないと失敗する
    • PKCE なしで始めたセッションは 400
    • 2 回目のコールバックは 400(verifier は使い捨て)
    • WithAuthCodeOptions のパラメータが付き、nonce / code_challenge は上書きされない
  • go test ./... が通ることを確認しました。

🤖 Generated with Claude Code

Redirect now generates a PKCE code verifier, stores it in the session and
sends the S256 code challenge. Callback requires the verifier and passes
it to the token exchange. A session without the verifier is rejected
with 400 instead of falling back to the exchange without PKCE.
WithoutPKCE disables it for providers that reject PKCE, or for apps that
build the authorization URL or exchange the code themselves.

WithAuthCodeOptions adds parameters such as prompt and hd to the
authorization request. They are applied before state, nonce and the PKCE
parameters so that they cannot override them.

The test helpers for the login flow are moved to flow_test.go, and the
fake provider now verifies the code verifier like real providers.
@mackee
mackee force-pushed the feat/oidc-pkce-auth-code-options branch from e7726cf to e9fbefc Compare October 8, 2026 05:54
@mackee
mackee merged commit 827f2f0 into main Oct 8, 2026
6 checks passed
@mackee
mackee deleted the feat/oidc-pkce-auth-code-options branch October 8, 2026 06:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant