-
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
Validating redirect_uri according to rfc6749 4.1.3 #45
Open
almirbi
wants to merge
3
commits into
main
Choose a base branch
from
validate-args-bug
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 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 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 |
---|---|---|
|
@@ -14,4 +14,7 @@ _book | |
*.epub | ||
*.mobi | ||
.idea | ||
.idea | ||
.vscode | ||
vendor | ||
composer.lock |
This file contains 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 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 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 |
---|---|---|
|
@@ -108,6 +108,47 @@ public function get_expiration() { | |
return (int) $value['expiration']; | ||
} | ||
|
||
private function validate_redirect_uri( $args ) { | ||
$value = $this->get_value(); | ||
|
||
if ( ! empty( $args['redirect_uri'] ) ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we invert this check instead to return early? |
||
if ( empty( $value['redirect_uri'] ) ) { | ||
return new WP_Error( | ||
'oauth2.tokens.authorization_code.redirect_uri.wrong', | ||
__( 'Missing redirect_uri.', 'oauth2' ), | ||
[ | ||
'status' => WP_Http::BAD_REQUEST, | ||
'expiration' => $expiration, | ||
'time' => $now, | ||
] | ||
); | ||
} | ||
|
||
if ( $value['redirect_uri'] !== $args['redirect_uri'] ) { | ||
return new WP_Error( | ||
'oauth2.tokens.authorization_code.redirect_uri.mismatch', | ||
__( 'redirect_uri does not match the one in the initial request.', 'oauth2' ), | ||
[ | ||
'status' => WP_Http::BAD_REQUEST, | ||
] | ||
); | ||
} | ||
} | ||
if ( empty( $value['redirect_uri'] ) && ! empty( $args['redirect_uri'] ) ) { | ||
return new WP_Error( | ||
'oauth2.tokens.authorization_code.redirect_uri.mismatch', | ||
__( 'redirect_uri does not match the one in the initial request.', 'oauth2' ), | ||
[ | ||
'status' => WP_Http::BAD_REQUEST, | ||
'expiration' => $expiration, | ||
'time' => $now, | ||
] | ||
); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* Validate the code for use. | ||
* | ||
|
@@ -129,6 +170,13 @@ public function validate( $args = [] ) { | |
); | ||
} | ||
|
||
$redirect_uri = $this->validate_redirect_uri( [ | ||
'redirect_uri' => $args['redirect_uri'], | ||
] ); | ||
if ( is_wp_error( $redirect_uri ) ) { | ||
return $redirect_uri; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
|
@@ -183,12 +231,13 @@ public static function get_by_code( Client $client, $code ) { | |
* | ||
* @return Authorization_Code|WP_Error Authorization code instance, or error on failure. | ||
*/ | ||
public static function create( Client $client, WP_User $user ) { | ||
public static function create( Client $client, WP_User $user, $redirect_uri = '' ) { | ||
$code = wp_generate_password( static::KEY_LENGTH, false ); | ||
$meta_key = static::KEY_PREFIX . $code; | ||
$data = [ | ||
'user' => (int) $user->ID, | ||
'expiration' => time() + static::MAX_AGE, | ||
'user' => (int) $user->ID, | ||
'expiration' => time() + static::MAX_AGE, | ||
'redirect_uri' => $redirect_uri, | ||
]; | ||
$result = add_post_meta( $client->get_post_id(), wp_slash( $meta_key ), wp_slash( $data ), true ); | ||
if ( ! $result ) { | ||
|
This file contains 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 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 |
---|---|---|
|
@@ -52,9 +52,12 @@ public function handle_authorisation() { | |
} | ||
|
||
// Validate the redirection URI. | ||
$redirect_uri = $this->validate_redirect_uri( $client, $redirect_uri ); | ||
if ( is_wp_error( $redirect_uri ) ) { | ||
return $redirect_uri; | ||
if ( ! empty( $redirect_uri ) ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems like this should always pass the |
||
$redirect_uri = $this->validate_redirect_uri( $client, $redirect_uri ); | ||
|
||
if ( is_wp_error( $redirect_uri ) ) { | ||
return $redirect_uri; | ||
} | ||
} | ||
|
||
// Valid parameters, ensure the user is logged in. | ||
|
@@ -69,8 +72,7 @@ public function handle_authorisation() { | |
} | ||
|
||
// Check nonce. | ||
$nonce_action = $this->get_nonce_action( $client ); | ||
if ( ! wp_verify_nonce( wp_unslash( $_POST['_wpnonce'] ), $none_action ) ) { | ||
if ( ! wp_verify_nonce( wp_unslash( $_POST['_wpnonce'] ), $this->get_nonce_action( $client ) ) ) { | ||
return new WP_Error( | ||
'oauth2.types.authorization_code.handle_authorisation.invalid_nonce', | ||
__( 'Invalid nonce.', 'oauth2' ) | ||
|
@@ -106,16 +108,10 @@ public function handle_authorisation() { | |
*/ | ||
protected function validate_redirect_uri( Client $client, $redirect_uri = null ) { | ||
if ( empty( $redirect_uri ) ) { | ||
$registered = $client->get_redirect_uris(); | ||
if ( count( $registered ) !== 1 ) { | ||
// Either none registered, or more than one, so error. | ||
return new WP_Error( | ||
'oauth2.types.authorization_code.handle_authorisation.missing_redirect_uri', | ||
__( 'Redirect URI was required, but not found.', 'oauth2' ) | ||
); | ||
} | ||
|
||
$redirect_uri = $registered[0]; | ||
return new WP_Error( | ||
'oauth2.types.authorization_code.handle_authorisation.missing_redirect_uri', | ||
__( 'Redirect URI was required, but not found.', 'oauth2' ) | ||
); | ||
} else { | ||
if ( ! $client->check_redirect_uri( $redirect_uri ) ) { | ||
return new WP_Error( | ||
|
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.
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
instead, and should have a phpDoc block.