@@ -67,7 +67,16 @@ public function render_page() {
67
67
68
68
switch ( $ _POST ['wp-submit ' ] ) {
69
69
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
+ }
71
80
exit ;
72
81
73
82
case 'cancel ' :
@@ -102,6 +111,47 @@ public function page_fields() {
102
111
wp_nonce_field ( 'json_oauth1_authorize ' );
103
112
}
104
113
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
+
105
155
/**
106
156
* Display an error using login page wrapper
107
157
*
0 commit comments