Skip to content

Commit 2fc7587

Browse files
authored
Merge pull request #5 from almirbi/master
Make the login page work with action=oauth2_authorize
2 parents ee789aa + 30a66f6 commit 2fc7587

File tree

9 files changed

+126
-14
lines changed

9 files changed

+126
-14
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ _book
1313
# eBook build output
1414
*.epub
1515
*.mobi
16-
*.pdf
16+
*.pdf
17+
.idea

inc/class-client.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,33 @@ public static function get_by_id( $id ) {
246246
return new static( $post );
247247
}
248248

249+
/**
250+
* Get a client by Client ID.
251+
*
252+
* @param int $id Client ID of the app.
253+
* @return static|null Client instance on success, null if invalid/not found.
254+
*/
255+
public static function get_by_client_id( $id ) {
256+
$args = array(
257+
'meta_query' => array(
258+
array(
259+
'key' => '_oauth2_client_id',
260+
'value' => $id,
261+
'compare' => '=',
262+
)
263+
),
264+
'post_type' => 'oauth2_client',
265+
'post_status' => 'any'
266+
);
267+
268+
$client_ids = get_posts( $args );
269+
if ( count( $client_ids ) !== 1 ) {
270+
return null;
271+
}
272+
273+
return new static( $client_ids[0] );
274+
}
275+
249276
/**
250277
* Create a new client.
251278
*

inc/endpoints/class-authorization.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ class Authorization {
1313
*/
1414
public function register_hooks() {
1515
add_action( 'login_form_' . static::LOGIN_ACTION, array( $this, 'handle_request' ) );
16-
add_action( 'oauth2_authorize_form', array( $this, 'render_page_fields' ) );
1716
}
1817

1918
public function handle_request() {
@@ -22,7 +21,7 @@ public function handle_request() {
2221

2322
switch ( $type ) {
2423
case 'code':
25-
$handler = new Types\Authorization_Code();
24+
$handler = new Types\AuthorizationCode();
2625
break;
2726

2827
case 'token':

inc/tokens/class-token.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,5 @@ public function is_valid() {
2929
public function get_meta_key() {
3030
return static::get_meta_prefix() . $this->get_key();
3131
}
32-
public function to_meta_value();
32+
public abstract function to_meta_value();
3333
}

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 & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace WP\OAuth2\Types;
44

5+
use WP_Http;
56
use WP_Error;
67
use WP\OAuth2\Client;
78

@@ -10,6 +11,7 @@ abstract class Base implements Type {
1011
* Handle authorisation page.
1112
*/
1213
public function handle_authorisation() {
14+
1315
if ( empty( $_GET['client_id'] ) ) {
1416
return new WP_Error(
1517
'oauth2.types.authorization_code.handle_authorisation.missing_client_id',
@@ -23,7 +25,7 @@ public function handle_authorisation() {
2325
$scope = isset( $_GET['scope'] ) ? wp_unslash( $_GET['scope'] ) : null;
2426
$state = isset( $_GET['state'] ) ? wp_unslash( $_GET['state'] ) : null;
2527

26-
$client = Client::get_by_id( $client_id );
28+
$client = Client::get_by_client_id( $client_id );
2729
if ( empty( $client ) ) {
2830
return new WP_Error(
2931
'oauth2.types.authorization_code.handle_authorisation.invalid_client_id',
@@ -99,10 +101,10 @@ protected function validate_redirect_uri( Client $client, $redirect_uri = null )
99101
*
100102
* @param Client $client Client being authorised.
101103
*/
102-
protected function render_form( Client $client ) {
103-
$file = locate_template( 'oauth1-authorize.php' );
104+
public function render_form( Client $client ) {
105+
$file = locate_template( 'oauth2-authorize.php' );
104106
if ( empty( $file ) ) {
105-
$file = dirname( dirname( __DIR__ ) ) . '/theme/oauth1-authorize.php';
107+
$file = dirname( dirname( __DIR__ ) ) . '/theme/oauth2-authorize.php';
106108
}
107109

108110
include $file;
@@ -114,6 +116,7 @@ protected function render_form( Client $client ) {
114116
* @param Client $client Client to generate nonce for.
115117
*/
116118
protected function get_nonce_action( Client $client ) {
117-
return sprintf( 'oauth2_authorize:%s', $client->get_key() );
119+
// return sprintf( 'oauth2_authorize:%s', $client->get_post_id() );
120+
return 'json_oauth2_authorize';
118121
}
119122
}

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

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
/**
3+
* Authorization page handler
4+
*
5+
* Takes care of UI and related elements for the authorization step of OAuth.
6+
*
7+
* @package WordPress
8+
* @subpackage JSON API
9+
*/
10+
11+
class WP_REST_OAuth2_UI {
12+
/**
13+
* Request token for the current authorization request
14+
*
15+
* @var array
16+
*/
17+
protected $token;
18+
19+
/**
20+
* Consumer post object for the current authorization request
21+
*
22+
* @var WP_Post
23+
*/
24+
protected $consumer;
25+
26+
/**
27+
* Register required actions and filters
28+
*/
29+
public function register_hooks() {
30+
add_action( 'login_form_oauth2_authorize', array( $this, 'handle_request' ) );
31+
}
32+
33+
/**
34+
* Handle request to authorization page
35+
*
36+
* Handles response from {@see render_page}, then exits to avoid output from
37+
* default wp-login handlers.
38+
*/
39+
public function handle_request() {
40+
if ( ! is_user_logged_in() ) {
41+
wp_safe_redirect( wp_login_url( $_SERVER['REQUEST_URI'] ) );
42+
exit;
43+
}
44+
45+
$auth_code = new \WP\OAuth2\Types\AuthorizationCode();
46+
47+
$auth_code->handle_authorisation();
48+
exit;
49+
}
50+
51+
/**
52+
* Render authorization page
53+
*
54+
* @return null|WP_Error Null on success, error otherwise
55+
*/
56+
public function render_page() {
57+
$auth_code = new \WP\OAuth2\Types\AuthorizationCode();
58+
$auth_code->handle_authorisation();
59+
}
60+
61+
/**
62+
* Display an error using login page wrapper
63+
*
64+
* @param WP_Error $error Error object
65+
*/
66+
public function display_error( WP_Error $error ) {
67+
login_header( __( 'Error', 'rest_oauth2' ), '', $error );
68+
login_footer();
69+
}
70+
}

plugin.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ function bootstrap() {
1919
/** @todo Implement this :) */
2020
// add_filter( 'determine_current_user', __NAMESPACE__ . '\\attempt_authentication' );
2121
add_filter( 'oauth2.grant_types', __NAMESPACE__ . '\\register_grant_types', 0 );
22-
22+
add_action( 'init', __NAMESPACE__ . '\\rest_oauth2_load_authorize_page' );
2323
add_action( 'admin_menu', array( __NAMESPACE__ . '\\admin\\Admin', 'register' ) );
2424
}
2525

@@ -31,6 +31,18 @@ function load() {
3131
require __DIR__ . '/inc/types/class-authorization-code.php';
3232
require __DIR__ . '/inc/types/class-implicit.php';
3333
require __DIR__ . '/inc/admin/class-admin.php';
34+
require __DIR__ . '/lib/class-wp-rest-oauth2-ui.php';
35+
}
36+
37+
/**
38+
* Register the authorization page
39+
*
40+
* Alas, login_init is too late to register pages, as the action is already
41+
* sanitized before this.
42+
*/
43+
function rest_oauth2_load_authorize_page() {
44+
$authorizer = new \WP_REST_OAuth2_UI();
45+
$authorizer->register_hooks();
3446
}
3547

3648
/**
@@ -60,7 +72,7 @@ function get_grant_types() {
6072
* @return array Grant types with additional types registered.
6173
*/
6274
function register_grant_types( $types ) {
63-
$types['authorization_code'] = new Types\Authorization_Code();
75+
$types['authorization_code'] = new Types\AuthorizationCode();
6476
$types['implicit'] = new Types\Implicit();
6577

6678
return $types;

theme/oauth1-authorize.php renamed to theme/oauth2-authorize.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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-
$client->get_name(),
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', $client->get_key() ) );
79+
wp_nonce_field( 'json_oauth2_authorize' );
8080
?>
8181

8282
<p class="submit">

0 commit comments

Comments
 (0)