-
Notifications
You must be signed in to change notification settings - Fork 42
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
Added PKCE support #46
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Phew, massive review, but looking really good overall. Thanks so much for this PR!
Mostly minor things to fix up here; if you don't have time for them, I can follow them up instead.
Haven't tested this for functionality or against the spec yet, but will do so soonish.
README.md
Outdated
|
||
code_verifier = 052edd3941bb8040ecac75d2359d7cd1abe2518911b<br> | ||
code_challenge = base64( sha256( code_verifier ) ) = MmNmZTJlNGZhYmNmYzQ3YTI4MmRhY2Q1NGEwZDUzZTFiZGFhNTNlODI4MGY3NjM0YWUwNjA1YjYzMmQwNDMxNQ==<br> | ||
code_challenge_method = s256 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should probably be wrapped in a code block. (Actually, we should eventually move into the proper docs, but that can happen later.)
inc/endpoints/class-token.php
Outdated
@@ -71,7 +76,7 @@ public function exchange_token( WP_REST_Request $request ) { | |||
return $auth_code; | |||
} | |||
|
|||
$is_valid = $auth_code->validate(); | |||
$is_valid = $auth_code->validate( $request ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd rather pass the args in separately here to avoid having the validate()
method depend on the request parameter names.
@@ -108,6 +108,36 @@ public function get_expiration() { | |||
return (int) $value['expiration']; | |||
} | |||
|
|||
private function validate_code_verifier( $args ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be protected
, not private
$is_valid = $decoded === $value['code_challenge']; | ||
break; | ||
case 'plain': | ||
$is_valid = $code_verifier === $value['code_challenge']; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Both this equality check and the one above should use hash_equals()
to ensure constant-time string comparison (to avoid timing attacks).
|
||
switch ( strtolower( $value['code_challenge_method'] ) ) { | ||
case 's256': | ||
$decoded = base64_encode( hash( 'sha256', $code_verifier ) ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be $encoded
instead?
} else { | ||
$is_strong_crypto = true; | ||
$random_seed = \bin2hex( \openssl_random_pseudo_bytes( $length / 2 + $length % 2, $is_strong_crypto ) ); | ||
$random_seed = \substr( $random_seed, 0, $length ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The \
s here are unnecessary.
], | ||
]; | ||
|
||
\WP_CLI\Utils\format_items( 'table', $items, [ 'code_verifier', 'code_challenge = base64( sha256( code_verifier ) )' ] ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use WP_CLI\Utils
here too.
} | ||
} | ||
|
||
$code_challenge = \base64_encode( hash( 'sha256', $random_seed ) ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unnecessary \
$items = [ | ||
[ | ||
'code_verifier' => $random_seed, | ||
'code_challenge = base64( sha256( code_verifier ) )' => $code_challenge, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should keep the title a little shorter, but not sure what this actually looks like in practice.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The keys are longer anyways
inc/namespace.php
Outdated
|
||
// WP-Cli | ||
if ( class_exists( __NAMESPACE__ . '\\Utilities\\Oauth2_Wp_Cli' ) ) { | ||
\WP_CLI::add_command( 'oauth2', __NAMESPACE__ . '\\Utilities\\Oauth2_Wp_Cli' ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
WP_CLI
should be use
d at the top of the file instead of an absolute reference.
#18