Skip to content

Commit 635cfca

Browse files
authored
Merge pull request #14 from tfrommen/validate-grant-types
Validate grant types
2 parents 7e791ea + ce53f67 commit 635cfca

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

inc/endpoints/class-authorization.php

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

55
use WP_Error;
66
use WP\OAuth2;
7+
use WP\OAuth2\Types\Type;
78

89
class Authorization {
910
const LOGIN_ACTION = 'oauth2_authorize';
@@ -26,6 +27,7 @@ public function handle_request() {
2627
// Match type to a handler.
2728
$grant_types = OAuth2\get_grant_types();
2829
if ( $grant_types ) {
30+
/** @var Type $type_handler */
2931
foreach ( array_reverse( $grant_types ) as $type_handler ) {
3032
if ( $type_handler->get_response_type_code() === $type ) {
3133
$handler = $type_handler;

plugin.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
namespace WP\OAuth2;
1212

13+
use WP\OAuth2\Types\Type;
1314
use WP_REST_Response;
1415

1516
bootstrap();
@@ -67,7 +68,7 @@ function rest_oauth2_load_authorize_page() {
6768
/**
6869
* Get valid grant types.
6970
*
70-
* @return array Map of grant type to handler object.
71+
* @return Type[] Map of grant type to handler object.
7172
*/
7273
function get_grant_types() {
7374
/**
@@ -77,9 +78,19 @@ function get_grant_types() {
7778
* Note that additional grant types must follow the extension policy in the
7879
* OAuth 2 specification.
7980
*
80-
* @param array $grant_types Map of grant type to handler object.
81+
* @param Type[] $grant_types Map of grant type to handler object.
8182
*/
82-
return apply_filters( 'oauth2.grant_types', array() );
83+
$grant_types = apply_filters( 'oauth2.grant_types', array() );
84+
foreach ( $grant_types as $type => $handler ) {
85+
if ( ! $handler instanceof Type ) {
86+
/* translators: 1: Grant type name, 2: Grant type interface */
87+
$message = __( 'Skipping invalid grant type "%s". Required interface "%s" not implemented.', 'oauth2' );
88+
_doing_it_wrong( __FUNCTION__, sprintf( $message, $type, 'WP\\OAuth2\\Types\\Type' ), '0.1.0' );
89+
unset( $grant_types[ $type ] );
90+
}
91+
}
92+
93+
return $grant_types;
8394
}
8495

8596
/**

0 commit comments

Comments
 (0)