Skip to content

Commit 4b05d5e

Browse files
committed
Redirect the user after authorization
1 parent be91856 commit 4b05d5e

File tree

1 file changed

+51
-1
lines changed

1 file changed

+51
-1
lines changed

lib/class-wp-json-authentication-oauth1-authorize.php

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,16 @@ public function render_page() {
6767

6868
switch ( $_POST['wp-submit'] ) {
6969
case 'authorize':
70-
$authenticator->authorize_request_token( $this->token['key'] );
70+
$verifier = $authenticator->authorize_request_token( $this->token['key'] );
71+
if ( is_wp_error( $verifier ) ) {
72+
$this->display_error( $error );
73+
exit;
74+
}
75+
76+
$error = $this->handle_callback_redirect( $verifier );
77+
if ( is_wp_error( $error ) ) {
78+
$this->display_error( $error );
79+
}
7180
exit;
7281

7382
case 'cancel':
@@ -102,6 +111,47 @@ public function page_fields() {
102111
wp_nonce_field( 'json_oauth1_authorize' );
103112
}
104113

114+
/**
115+
* Handle redirecting the user after authorization
116+
*
117+
* @param string $verifier Verification code
118+
* @return null|WP_Error Null on success, error otherwise
119+
*/
120+
public function handle_callback_redirect( $verifier ) {
121+
$callback = $this->token['callback'];
122+
if ( $callback === 'oob' || empty( $callback ) ) {
123+
return apply_filters( 'json_oauth1_handle_callback', null, $this->token );
124+
}
125+
126+
if ( empty( $callback ) ) {
127+
// No callback registered, display verification code to the user
128+
login_header( __( 'Access Token' ) );
129+
echo '<p>' . sprintf( __( 'Your access token is <code>%s</code>' ), $verifier ) . '</p>';
130+
login_footer();
131+
132+
return null;
133+
}
134+
135+
// Ensure the URL is safe to access
136+
$callback = wp_http_validate_url( $callback );
137+
if ( empty( $callback ) ) {
138+
return new WP_Error( 'json_oauth1_invalid_callback', __( 'The callback URL is invalid' ), array( 'status' => 400 ) );
139+
}
140+
141+
$args = array(
142+
'oauth_token' => $this->token['key'],
143+
'oauth_verifier' => $verifier,
144+
'wp_scope' => '*',
145+
);
146+
$args = apply_filters( 'json_oauth1_callback_args', $args, $token );
147+
$args = urlencode_deep( $args );
148+
$callback = add_query_arg( $args, $callback );
149+
150+
wp_redirect( $callback );
151+
152+
return null;
153+
}
154+
105155
/**
106156
* Display an error using login page wrapper
107157
*

0 commit comments

Comments
 (0)