Skip to content

Commit 7a70055

Browse files
committed
Make the wp-login page work with ?action=oauth2
1 parent b105925 commit 7a70055

File tree

5 files changed

+22
-78
lines changed

5 files changed

+22
-78
lines changed

inc/endpoints/class-authorization.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,8 @@ public function handle_request() {
4242
wp_die( $result->get_error_message() );
4343
}
4444
}
45+
46+
public function render_page_fields() {
47+
wp_nonce_field( 'json_oauth2_authorize' );
48+
}
4549
}

inc/types/class-authorization-code.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use WP_Http;
66
use WP\OAuth2\Client;
77

8-
class Authorization_Code extends Base {
8+
class AuthorizationCode extends Base {
99
protected function handle_authorization_submission( $submit, Client $client, $data ) {
1010
$redirect_uri = $data['redirect_uri'];
1111

inc/types/class-base.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ abstract class Base implements Type {
1010
* Handle authorisation page.
1111
*/
1212
public function handle_authorisation() {
13+
1314
if ( empty( $_GET['client_id'] ) ) {
1415
return new WP_Error(
1516
'oauth2.types.authorization_code.handle_authorisation.missing_client_id',
@@ -45,6 +46,7 @@ public function handle_authorisation() {
4546
return $this->render_form( $client );
4647
}
4748

49+
4850
// Check nonce.
4951
$nonce = wp_unslash( $_POST['_wpnonce'] );
5052
if ( ! wp_verify_nonce( $nonce, $this->get_nonce_action( $client ) ) ) {
@@ -54,6 +56,8 @@ public function handle_authorisation() {
5456
);
5557
}
5658

59+
60+
5761
$submit = wp_unslash( $_POST['wp-submit'] );
5862
if ( empty( $submit ) ) {
5963
return new WP_Error();
@@ -99,10 +103,10 @@ protected function validate_redirect_uri( Client $client, $redirect_uri = null )
99103
*
100104
* @param Client $client Client being authorised.
101105
*/
102-
protected function render_form( Client $client ) {
103-
$file = locate_template( 'oauth1-authorize.php' );
106+
public function render_form( Client $client ) {
107+
$file = locate_template( 'oauth2-authorize.php' );
104108
if ( empty( $file ) ) {
105-
$file = dirname( dirname( __DIR__ ) ) . '/theme/oauth1-authorize.php';
109+
$file = dirname( dirname( __DIR__ ) ) . '/theme/oauth2-authorize.php';
106110
}
107111

108112
include $file;
@@ -114,6 +118,6 @@ protected function render_form( Client $client ) {
114118
* @param Client $client Client to generate nonce for.
115119
*/
116120
protected function get_nonce_action( Client $client ) {
117-
return sprintf( 'oauth2_authorize:%s', $client->get_key() );
121+
return sprintf( 'oauth2_authorize:%s', $client->get_post_id() );
118122
}
119123
}

lib/class-wp-rest-oauth2-ui.php

Lines changed: 6 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,9 @@ public function handle_request() {
4343
exit;
4444
}
4545

46-
$response = $this->render_page();
47-
if ( is_wp_error( $response ) ) {
48-
$this->display_error( $response );
49-
}
46+
$auth_code = new \WP\OAuth2\Types\AuthorizationCode();
47+
48+
$auth_code->handle_authorisation();
5049
exit;
5150
}
5251

@@ -56,69 +55,8 @@ public function handle_request() {
5655
* @return null|WP_Error Null on success, error otherwise
5756
*/
5857
public function render_page() {
59-
// Check required fields
60-
/*if ( empty( $_REQUEST['response_type'] ) ) {
61-
return new WP_Error( 'json_oauth2_missing_param', sprintf( __( 'Missing parameter %s', 'rest_oauth2' ), 'response_type' ), array( 'status' => 400 ) );
62-
}
63-
64-
if ( empty( $_REQUEST['client_id'] ) ) {
65-
return new WP_Error( 'json_oauth2_missing_param', sprintf( __( 'Missing parameter %s', 'rest_oauth2' ), 'client_id' ), array( 'status' => 400 ) );
66-
}*/
67-
68-
/*// Set up fields
69-
$token_key = wp_unslash( $_REQUEST['oauth_token'] );
70-
$scope = '*';
71-
if ( ! empty( $_REQUEST['wp_scope'] ) ) {
72-
$scope = wp_unslash( $_REQUEST['wp_scope'] );
73-
}*/
74-
75-
// $authenticator = new WP_REST_OAuth1();
76-
// $errors = array();
77-
// $this->token = $authenticator->get_request_token( $token_key );
78-
/*if ( is_wp_error( $this->token ) ) {
79-
return $this->token;
80-
}
81-
82-
if ( ! empty( $_REQUEST['oauth_callback'] ) ) {
83-
$resp = $authenticator->set_request_token_callback( $this->token['key'], $_REQUEST['oauth_callback'] );
84-
if ( is_wp_error( $resp ) ) {
85-
return $resp;
86-
}
87-
}
88-
89-
if ( $this->token['authorized'] === true ) {
90-
return $this->handle_callback_redirect( $this->token['verifier'] );
91-
}
92-
93-
// Fetch consumer
94-
$this->consumer = $consumer = get_post( $this->token['consumer'] );*/
95-
96-
/*if ( ! empty( $_POST['wp-submit'] ) ) {
97-
check_admin_referer( 'json_oauth2_authorize' );
98-
99-
switch ( $_POST['wp-submit'] ) {
100-
case 'authorize':
101-
$verifier = $authenticator->authorize_request_token( $this->token['key'] );
102-
if ( is_wp_error( $verifier ) ) {
103-
return $verifier;
104-
}
105-
106-
return $this->handle_callback_redirect( $verifier );
107-
108-
case 'cancel':
109-
exit;
110-
111-
default:
112-
return new WP_Error( 'json_oauth1_invalid_action', __( 'Invalid authorization action', 'rest_oauth1' ), array( 'status' => 400 ) );
113-
}
114-
}*/
115-
116-
$file = locate_template( 'oauth2-authorize.php' );
117-
if ( empty( $file ) ) {
118-
$file = dirname( dirname( __FILE__ ) ) . '/theme/oauth2-authorize.php';
119-
}
120-
121-
include $file;
58+
$auth_code = new \WP\OAuth2\Types\AuthorizationCode();
59+
$auth_code->handle_authorisation();
12260
}
12361

12462
/**
@@ -128,9 +66,7 @@ public function render_page() {
12866
* nonce field.
12967
*/
13068
public function page_fields() {
131-
echo '<input type="hidden" name="consumer" value="' . absint( $this->consumer->ID ) . '" />';
132-
echo '<input type="hidden" name="oauth_token" value="' . esc_attr( $this->token['key'] ) . '" />';
133-
wp_nonce_field( 'json_oauth2_authorize' );
69+
wp_nonce_field( sprintf( 'oauth2_authorize:%s', $this->client->get_post_id() ) );
13470
}
13571

13672
/**

theme/oauth2-authorize.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454

5555
<form name="oauth1_authorize_form" id="oauth1_authorize_form" action="<?php echo esc_url( $url ); ?>" method="post">
5656

57-
<h2 class="login-title"><?php echo esc_html( sprintf( __('Connect %1$s'), "Awesome Client" ) ) ?></h2>
57+
<h2 class="login-title"><?php echo esc_html( sprintf( __('Connect %1$s'), $client->get_name() ) ) ?></h2>
5858

5959
<div class="login-info">
6060

@@ -64,7 +64,7 @@
6464
printf(
6565
__( 'Howdy <strong>%1$s</strong>,<br/> "%2$s" would like to connect to %3$s.', 'oauth2' ),
6666
$current_user->user_login,
67-
"Awesome Client",
67+
$client->get_name(),
6868
get_bloginfo( 'name' )
6969
)
7070
?></p>
@@ -76,7 +76,7 @@
7676
* Fires inside the lostpassword <form> tags.
7777
*/
7878
do_action( 'oauth2_authorize_form', $client );
79-
wp_nonce_field( sprintf( 'oauth2_authorize:%s', "Aajaskdjalskdjkl" ) );
79+
wp_nonce_field( 'oauth2_authorize' );
8080
?>
8181

8282
<p class="submit">

0 commit comments

Comments
 (0)