Skip to content

Commit 9d274c0

Browse files
committed
House cleaning.
1 parent 3ca28b9 commit 9d274c0

20 files changed

+214
-62
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ This is in extremely early beta, and does not work yet. Please help us out and c
1616

1717
This plugin is licensed under the GNU General Public License v2 or later:
1818

19-
> Copyright 2016 by the contributors.
19+
> Copyright 2017 by the contributors.
2020
>
2121
> This program is free software; you can redistribute it and/or modify
2222
> it under the terms of the GNU General Public License as published by

bin/readme.txt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
=== WordPress REST API - OAuth 1.0a Server ===
1+
=== WordPress REST API - OAuth 2 Server ===
22
Contributors: rmccue, rachelbaker, danielbachhuber, joehoyle
33
Tags: json, rest, api, rest-api
4-
Requires at least: 4.4
5-
Tested up to: 4.7-alpha
4+
Requires at least: 4.8
5+
Tested up to: 4.8
66
Stable tag: {{TAG}}
77
License: GPLv2 or later
88
License URI: http://www.gnu.org/licenses/gpl-2.0.html
99

1010
== Description ==
1111
Connect applications to your WordPress site without ever giving away your password.
1212

13-
This plugin uses the OAuth 1.0a protocol to allow delegated authorization; that is, to allow applications to access a site using a set of secondary credentials. This allows server administrators to control which applications can access the site, as well as allowing users to control which applications have access to their data.
13+
This plugin uses the OAuth 2 protocol to allow delegated authorization; that is, to allow applications to access a site using a set of secondary credentials. This allows server administrators to control which applications can access the site, as well as allowing users to control which applications have access to their data.
1414

15-
This plugin only supports WordPress >= 4.4.
15+
This plugin only supports WordPress >= 4.8.

bin/release.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
TAG=$1
66

7-
PLUGIN="rest-api-oauth1"
8-
TMPDIR=/tmp/rest-api-oauth1-release-svn
7+
PLUGIN="rest-api-oauth2"
8+
TMPDIR=/tmp/rest-api-oauth2-release-svn
99
PLUGINDIR="$PWD"
1010
PLUGINSVN="https://plugins.svn.wordpress.org/$PLUGIN"
1111

book.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
"plugins": ["edit-link", "github"],
77
"pluginsConfig": {
88
"edit-link": {
9-
"base": "https://github.com/WP-API/OAuth1/tree/master",
9+
"base": "https://github.com/WP-API/OAuth2/tree/master",
1010
"label": "Edit This Page"
1111
},
1212
"github": {
13-
"url": "https://github.com/WP-API/OAuth1/"
13+
"url": "https://github.com/WP-API/OAuth2/"
1414
}
1515
}
1616
}

composer.json

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
{
2-
"name": "wp-api/oauth1",
3-
"description": "OAuth 1.0a Server for WordPress",
4-
"type": "wordpress-plugin",
5-
"license": "GPL2+",
6-
"authors": [
7-
{
8-
"name": "WP-API Team",
9-
"homepage": "http://wp-api.org/"
10-
}
11-
],
12-
"require": {}
2+
"name": "wp-api/oauth2",
3+
"description": "OAuth 2 Server for WordPress",
4+
"type": "wordpress-plugin",
5+
"license": "GPL2+",
6+
"authors": [
7+
{
8+
"name": "WP-API Team",
9+
"homepage": "http://wp-api.org/"
10+
}
11+
]
1312
}

inc/admin/class-admin.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace WP\OAuth2\Admin;
44

55
use WP\OAuth2\Client;
6-
use WP\OAuth2\Types;
76
use WP_Error;
87

98
class Admin {
@@ -136,6 +135,12 @@ class="add-new-h2"><?php echo esc_html_x( 'Add New', 'application', 'rest_oauth2
136135
<?php
137136
}
138137

138+
/**
139+
* Validates given parameters.
140+
*
141+
* @param array $params RAW parameters.
142+
* @return array|WP_Error Validated parameters, or error on failure.
143+
*/
139144
protected static function validate_parameters( $params ) {
140145
$valid = [];
141146

@@ -167,11 +172,11 @@ protected static function validate_parameters( $params ) {
167172
/**
168173
* Handle submission of the add page
169174
*
170-
* @param $consumer
175+
* @param Client $consumer
171176
*
172177
* @return array|null List of errors. Issues a redirect and exits on success.
173178
*/
174-
protected static function handle_edit_submit( $consumer ) {
179+
protected static function handle_edit_submit( Client $consumer = null ) {
175180
$messages = [];
176181
if ( empty( $consumer ) ) {
177182
$did_action = 'add';
@@ -243,8 +248,9 @@ public static function render_edit_page() {
243248
}
244249

245250
// Are we editing?
246-
$consumer = null;
247-
$form_action = self::get_url( 'action=add' );
251+
$consumer = null;
252+
$form_action = self::get_url( 'action=add' );
253+
$regenerate_action = '';
248254
if ( ! empty( $_REQUEST['id'] ) ) {
249255
$id = absint( $_REQUEST['id'] );
250256
$consumer = Client::get_by_post_id( $id );

inc/admin/class-listtable.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ public function get_columns() {
5656
return $c;
5757
}
5858

59+
/**
60+
* @param \WP_Post $item Post object.
61+
*/
5962
public function column_cb( $item ) {
6063
?>
6164
<label class="screen-reader-text"
@@ -67,6 +70,10 @@ public function column_cb( $item ) {
6770
<?php
6871
}
6972

73+
/**
74+
* @param \WP_Post $item Post object.
75+
* @return string Name of the column.
76+
*/
7077
protected function column_name( $item ) {
7178
$title = get_the_title( $item->ID );
7279
if ( empty( $title ) ) {
@@ -100,6 +107,10 @@ protected function column_name( $item ) {
100107
return $title . ' ' . $action_html;
101108
}
102109

110+
/**
111+
* @param \WP_Post $item Post object.
112+
* @return string Content of the column.
113+
*/
103114
protected function column_description( $item ) {
104115
return $item->post_content;
105116
}

inc/authentication/namespace.php

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace WP\OAuth2\Authentication;
44

5+
use WP_Error;
6+
use WP_User;
57
use WP\OAuth2\Tokens;
68

79
/**
@@ -33,6 +35,11 @@ function get_authorization_header() {
3335
return null;
3436
}
3537

38+
/**
39+
* Extracts the token from the authorization header or the current request.
40+
*
41+
* @return string|null Token on success, null on failure.
42+
*/
3643
function get_provided_token() {
3744
$header = get_authorization_header();
3845
if ( $header ) {
@@ -47,6 +54,13 @@ function get_provided_token() {
4754
return null;
4855
}
4956

57+
/**
58+
* Extracts the token from the given authorization header.
59+
*
60+
* @param string $header Authorization header.
61+
*
62+
* @return string|null Token on succes, null on failure.
63+
*/
5064
function get_token_from_bearer_header( $header ) {
5165
if ( is_string( $header ) && preg_match( '/Bearer ([a-zA-Z0-9\-._~\+\/=]+)/', trim( $header ), $matches ) ) {
5266
return $matches[1];
@@ -55,6 +69,11 @@ function get_token_from_bearer_header( $header ) {
5569
return null;
5670
}
5771

72+
/**
73+
* Extracts the token from the current request.
74+
*
75+
* @return string|null Token on succes, null on failure.
76+
*/
5877
function get_token_from_request() {
5978
if ( empty( $_GET['access_token'] ) ) {
6079
return null;
@@ -74,9 +93,9 @@ function get_token_from_request() {
7493
/**
7594
* Try to authenticate if possible.
7695
*
77-
* @param \WP_User|null $user Existing authenticated user.
96+
* @param WP_User|null $user Existing authenticated user.
7897
*
79-
* @return \WP_User|int|\WP_Error
98+
* @return WP_User|int|WP_Error
8099
*/
81100
function attempt_authentication( $user = null ) {
82101
// Lock against infinite loops when querying the token itself.
@@ -115,6 +134,8 @@ function attempt_authentication( $user = null ) {
115134
* Attached to the rest_authentication_errors filter. Passes through existing
116135
* errors registered on the filter.
117136
*
137+
* @param WP_Error|null Current error, or null.
138+
*
118139
* @return WP_Error|null Error if one is set, otherwise null.
119140
*/
120141
function maybe_report_errors( $error = null ) {
@@ -126,8 +147,15 @@ function maybe_report_errors( $error = null ) {
126147
return $oauth2_error;
127148
}
128149

150+
/**
151+
* Creates an error object for the given invalid token.
152+
*
153+
* @param mixed $token Invalid token.
154+
*
155+
* @return WP_Error
156+
*/
129157
function create_invalid_token_error( $token ) {
130-
return new \WP_Error(
158+
return new WP_Error(
131159
'oauth2.authentication.attempt_authentication.invalid_token',
132160
__( 'Supplied token is invalid.', 'oauth2' ),
133161
array(

inc/class-client.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use WP\OAuth2\Tokens\Access_Token;
66
use WP\OAuth2\Tokens\Authorization_Code;
77
use WP_Error;
8-
use WP_Http;
98
use WP_Post;
109
use WP_Query;
1110
use WP_User;
@@ -224,17 +223,17 @@ public function check_redirect_uri( $uri ) {
224223
$valid = apply_filters( 'rest_oauth.check_callback', $valid, $uri, $registered_uri, $this );
225224
if ( $valid ) {
226225
// Stop checking, we have a match.
227-
break;
226+
return true;
228227
}
229228
}
230229

231-
return $valid;
230+
return false;
232231
}
233232

234233
/**
235234
* @param WP_User $user
236235
*
237-
* @return string|WP_Error
236+
* @return Authorization_Code|WP_Error
238237
*/
239238
public function generate_authorization_code( WP_User $user ) {
240239
return Authorization_Code::create( $this, $user );
@@ -244,7 +243,7 @@ public function generate_authorization_code( WP_User $user ) {
244243
* Get data stored for an authorization code.
245244
*
246245
* @param string $code Authorization code to fetch.
247-
* @return array|WP_Error Data if available, error if invalid code.
246+
* @return Authorization_Code|WP_Error Data if available, error if invalid code.
248247
*/
249248
public function get_authorization_code( $code ) {
250249
return Authorization_Code::get_by_code( $this, $code );
@@ -266,7 +265,7 @@ public function regenerate_secret() {
266265
* Issue token for a user.
267266
*
268267
* @param \WP_User $user
269-
*
268+
*
270269
* @return Access_Token
271270
*/
272271
public function issue_token( WP_User $user ) {

inc/endpoints/class-authorization.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
use WP_Error;
66
use WP\OAuth2;
7-
use WP\OAuth2\Client;
8-
use WP\OAuth2\Types;
97

108
class Authorization {
119
const LOGIN_ACTION = 'oauth2_authorize';
@@ -27,19 +25,21 @@ public function handle_request() {
2725

2826
// Match type to a handler.
2927
$grant_types = OAuth2\get_grant_types();
30-
foreach ( $grant_types as $type_handler ) {
31-
if ( $type_handler->get_response_type_code() === $type ) {
32-
$handler = $type_handler;
28+
if ( $grant_types ) {
29+
foreach ( array_reverse( $grant_types ) as $type_handler ) {
30+
if ( $type_handler->get_response_type_code() === $type ) {
31+
$handler = $type_handler;
32+
break;
33+
}
3334
}
3435
}
36+
3537
if ( empty( $handler ) ) {
3638
$result = new WP_Error(
3739
'oauth2.endpoints.authorization.handle_request.invalid_type',
3840
__( 'Invalid response type specified.', 'oauth2' )
3941
);
40-
}
41-
42-
if ( empty( $result ) ) {
42+
} else {
4343
$result = $handler->handle_authorisation();
4444
}
4545

0 commit comments

Comments
 (0)