-
Couldn't load subscription status.
- Fork 43
Validating redirect_uri according to rfc6749 4.1.3 #45
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
base: main
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,4 +14,7 @@ _book | |
| *.epub | ||
| *.mobi | ||
| .idea | ||
| .idea | ||
| .vscode | ||
| vendor | ||
| composer.lock | ||
| 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'] ) ) { | ||
|
||
| 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 ) { | ||
|
|
||
| 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( | ||
|
|
||
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
protectedinstead, and should have a phpDoc block.