-
Notifications
You must be signed in to change notification settings - Fork 45
Add PKCE support (RFC 7636) #85
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
roborourke
wants to merge
18
commits into
WP-API:main
Choose a base branch
from
humanmade:roborourke/pkce-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
303e2c4
Add PKCE helper class
roborourke f5249aa
Bind PKCE challenge to authorization codes
roborourke beb6992
Add per-client PKCE requirement
roborourke bf81aa4
Validate PKCE parameters at the authorisation endpoint
roborourke fdb4c39
Verify code_verifier at the token endpoint
roborourke ca1ec3c
Add PKCE settings to the client admin screen
roborourke f032204
Advertise supported PKCE methods in the REST index
roborourke 078552b
Add WP-CLI command to generate PKCE pairs
roborourke f966cec
Document PKCE support
roborourke a94f613
Trigger CI checks
roborourke 0c481d6
Fix CI failures after merging main
roborourke 0257a45
Fix PKCE error redirects silently landing on wp-admin instead of the …
roborourke c827fba
Advertise PKCE methods in the RFC 8414 metadata document
roborourke 0b19589
Apply review feedback on PKCE
roborourke e52fa65
Merge upstream main into roborourke/pkce-support
roborourke 40696fe
Apply Copilot review feedback on PKCE error handling
roborourke 6cd4069
Name the supported PKCE methods in the unsupported-method error
roborourke 3d64684
Link PKCE tests to the RFC sections they cover, and fill gaps
roborourke File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,159 @@ | ||
| <?php | ||
| /** | ||
| * | ||
| * @package WordPress | ||
| * @subpackage JSON API | ||
| */ | ||
|
|
||
| namespace WP\OAuth2; | ||
|
|
||
| /** | ||
| * Proof Key for Code Exchange (PKCE) helpers, per RFC 7636. | ||
| * | ||
| * Kept as a single set of static methods so the transform is written once, | ||
| * rather than duplicated between the authorisation endpoint, the token | ||
| * endpoint, and the WP-CLI helper command. | ||
| */ | ||
| class PKCE { | ||
| const METHOD_S256 = 'S256'; | ||
| const METHOD_PLAIN = 'plain'; | ||
|
|
||
| const VERIFIER_MIN_LENGTH = 43; | ||
| const VERIFIER_MAX_LENGTH = 128; | ||
|
|
||
| /** | ||
| * Get the challenge methods this site supports. | ||
| * | ||
| * @return string[] Supported `code_challenge_method` values. | ||
| */ | ||
| public static function supported_methods() { | ||
| /** | ||
| * Filter the PKCE code challenge methods this site accepts. | ||
| * | ||
| * Only the built-in `S256` and `plain` methods can be enabled; any | ||
| * other value is dropped, since no challenge can be derived for it. | ||
| * Comparisons against these values are case-sensitive, per RFC 7636 | ||
| * section 4.3. | ||
| * | ||
| * @param string[] $methods Supported `code_challenge_method` values. | ||
| */ | ||
| $methods = apply_filters( 'oauth2.pkce.supported_methods', [ static::METHOD_S256, static::METHOD_PLAIN ] ); | ||
|
|
||
| return array_values( array_intersect( (array) $methods, [ static::METHOD_S256, static::METHOD_PLAIN ] ) ); | ||
| } | ||
|
|
||
| /** | ||
| * Derive a code challenge from a verifier, for a given transform method. | ||
| * | ||
| * @param string $verifier Code verifier. | ||
| * @param string $method Challenge method, `S256` or `plain`. Case-sensitive. | ||
| * | ||
| * @return string|null Derived challenge, or null if the method is not recognised. | ||
| */ | ||
| public static function derive_challenge( $verifier, $method ) { | ||
| if ( ! is_string( $verifier ) ) { | ||
| return null; | ||
| } | ||
|
|
||
| switch ( $method ) { | ||
| case static::METHOD_S256: | ||
| return rtrim( strtr( base64_encode( hash( 'sha256', $verifier, true ) ), '+/', '-_' ), '=' ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode | ||
|
|
||
| case static::METHOD_PLAIN: | ||
| return $verifier; | ||
|
|
||
| default: | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Verify a code verifier against a stored code challenge. | ||
| * | ||
| * Fails closed: an unsupported method, or a non-string input, returns | ||
| * false rather than raising a warning or a TypeError from hash_equals(). | ||
| * | ||
| * @param string $verifier Code verifier supplied at the token endpoint. | ||
| * @param string $challenge Code challenge stored at authorization time. | ||
| * @param string $method Challenge method the challenge was derived with. | ||
| * | ||
| * @return bool Whether the verifier produces the given challenge. | ||
| */ | ||
| public static function verify( $verifier, $challenge, $method ) { | ||
| if ( ! is_string( $verifier ) || ! is_string( $challenge ) ) { | ||
| return false; | ||
| } | ||
|
|
||
| $derived = static::derive_challenge( $verifier, $method ); | ||
| if ( null === $derived ) { | ||
| return false; | ||
| } | ||
|
|
||
| return hash_equals( $challenge, $derived ); | ||
| } | ||
|
|
||
| /** | ||
| * Check whether a string is a valid PKCE code verifier. | ||
| * | ||
| * Per RFC 7636 section 4.1: 43-128 characters from the unreserved URI | ||
| * character set [A-Z] / [a-z] / [0-9] / "-" / "." / "_" / "~". | ||
| * | ||
| * @param mixed $verifier Value to check. | ||
| * | ||
| * @return bool | ||
| */ | ||
| public static function is_valid_verifier( $verifier ) { | ||
| if ( ! is_string( $verifier ) ) { | ||
| return false; | ||
| } | ||
|
|
||
| return (bool) preg_match( '/^[A-Za-z0-9\-._~]{' . static::VERIFIER_MIN_LENGTH . ',' . static::VERIFIER_MAX_LENGTH . '}\z/', $verifier ); | ||
| } | ||
|
|
||
| /** | ||
| * Check whether a string is a valid code challenge for the given method. | ||
| * | ||
| * `S256` challenges are base64url (unpadded) of a 32-byte SHA-256 digest, | ||
| * which is always exactly 43 characters from a narrower alphabet than the | ||
| * verifier's. `plain` challenges are just verifiers, so the verifier's | ||
| * character set and length range apply directly. | ||
| * | ||
| * @param mixed $challenge Value to check. | ||
| * @param string $method Challenge method, `S256` or `plain`. Case-sensitive. | ||
| * | ||
| * @return bool | ||
| */ | ||
| public static function is_valid_challenge( $challenge, $method ) { | ||
| if ( ! is_string( $challenge ) ) { | ||
| return false; | ||
| } | ||
|
|
||
| if ( static::METHOD_S256 === $method ) { | ||
| return (bool) preg_match( '/^[A-Za-z0-9\-_]{43}\z/', $challenge ); | ||
| } | ||
|
|
||
| if ( static::METHOD_PLAIN === $method ) { | ||
| return static::is_valid_verifier( $challenge ); | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| /** | ||
| * Generate a random code verifier. | ||
| * | ||
| * @param int $length Desired length, 43-128. Default 64. | ||
| * | ||
| * @return string Randomly generated code verifier. | ||
| */ | ||
| public static function generate_verifier( $length = 64 ) { | ||
| $length = max( static::VERIFIER_MIN_LENGTH, min( static::VERIFIER_MAX_LENGTH, (int) $length ) ); | ||
|
|
||
| // Base64url-encode more bytes than needed, then trim to length, so the | ||
| // result is uniformly distributed over the unreserved character set. | ||
| $bytes = random_bytes( $length ); | ||
| $verifier = rtrim( strtr( base64_encode( $bytes ), '+/', '-_' ), '=' ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode | ||
|
|
||
| return substr( $verifier, 0, $length ); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.