Skip to content
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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

Added PKCE support #46

wants to merge 3 commits into from

Conversation

almirbi
Copy link
Collaborator

@almirbi almirbi commented Sep 5, 2017

#18

@almirbi almirbi requested a review from rmccue September 5, 2017 20:34
Copy link
Member

@rmccue rmccue left a 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
Copy link
Member

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.)

@@ -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 );
Copy link
Member

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 ) {
Copy link
Member

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'];
Copy link
Member

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 ) );
Copy link
Member

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 );
Copy link
Member

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 ) )' ] );
Copy link
Member

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 ) );
Copy link
Member

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,
Copy link
Member

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.

Copy link
Collaborator Author

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


// WP-Cli
if ( class_exists( __NAMESPACE__ . '\\Utilities\\Oauth2_Wp_Cli' ) ) {
\WP_CLI::add_command( 'oauth2', __NAMESPACE__ . '\\Utilities\\Oauth2_Wp_Cli' );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WP_CLI should be used at the top of the file instead of an absolute reference.

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.

2 participants