PKCE¶
Proof Key for Code Exchange (RFC 7636) authorization-server primitives:
constant-time verification of a code_verifier against the bound
code_challenge, plus input validation for verifiers, challenges, and the
challenge-method allowlist (S256, plain).
pkce
¶
PKCE (RFC 7636) verification and validation helpers.
Authorization-server-side primitives for Proof Key for Code Exchange:
- :func:
verify_pkce— constant-time check of acode_verifieragainst thecode_challengeoriginally bound to the authorization code. - :func:
validate_code_verifier/ :func:validate_code_challenge— input sanitization per RFC 7636 §4.1/§4.2 (length 43-128, unreserved charset). - :func:
validate_code_challenge_method— method allowlist (S256,plain).
OAuth 2.1 and RFC 9700 deprecate plain (it offers no protection when the
authorization request is observed). Both :func:verify_pkce and
:func:validate_code_challenge_method accept allow_plain=False to enforce
an S256-only policy; servers SHOULD pass it unless a legacy client genuinely
cannot compute S256.
Client-side code_verifier/code_challenge generation is intentionally
out of scope for this module; mcp-authflow is an authorization-server
framework.
validate_code_challenge_method
¶
Return True if method is an allowed PKCE method.
Per RFC 7636 the registered methods are plain and S256, but OAuth
2.1 and RFC 9700 deprecate plain. Pass allow_plain=False to
enforce an S256-only policy at the /authorize step; the default keeps
the full RFC 7636 allowlist for backward compatibility.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
method
|
str | None
|
The |
required |
allow_plain
|
bool
|
Whether |
True
|
Source code in mcp_authflow/pkce.py
validate_code_verifier
¶
Return True if code_verifier conforms to RFC 7636 §4.1.
Length 43-128, characters from the unreserved set
[A-Z] / [a-z] / [0-9] / "-" / "." / "_" / "~".
Source code in mcp_authflow/pkce.py
validate_code_challenge
¶
Return True if code_challenge conforms to RFC 7636 §4.2.
Same length/charset rules as the verifier. For S256 challenges the value is BASE64URL(SHA256(verifier)) with padding stripped — always 43 chars and always within the unreserved set.
Source code in mcp_authflow/pkce.py
verify_pkce
¶
verify_pkce(
code_verifier: str,
code_challenge: str,
method: str,
*,
allow_plain: bool = True,
) -> bool
Verify a PKCE code_verifier against the stored code_challenge.
Comparison is constant-time. Returns False for any unknown method,
so callers can use this as a single decision point without first checking
the method allowlist.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
code_verifier
|
str
|
The verifier presented at the token endpoint. |
required |
code_challenge
|
str
|
The challenge that was bound to the authorization
code at the |
required |
method
|
str
|
|
required |
allow_plain
|
bool
|
Whether |
True
|