Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate grant types #14

Merged
merged 4 commits into from
Jul 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions inc/endpoints/class-authorization.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use WP_Error;
use WP\OAuth2;
use WP\OAuth2\Types\Type;

class Authorization {
const LOGIN_ACTION = 'oauth2_authorize';
Expand All @@ -26,6 +27,7 @@ public function handle_request() {
// Match type to a handler.
$grant_types = OAuth2\get_grant_types();
if ( $grant_types ) {
/** @var Type $type_handler */
foreach ( array_reverse( $grant_types ) as $type_handler ) {
if ( $type_handler->get_response_type_code() === $type ) {
$handler = $type_handler;
Expand Down
17 changes: 14 additions & 3 deletions plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace WP\OAuth2;

use WP\OAuth2\Types\Type;
use WP_REST_Response;

bootstrap();
Expand Down Expand Up @@ -66,7 +67,7 @@ function rest_oauth2_load_authorize_page() {
/**
* Get valid grant types.
*
* @return array Map of grant type to handler object.
* @return Type[] Map of grant type to handler object.
*/
function get_grant_types() {
/**
Expand All @@ -76,9 +77,19 @@ function get_grant_types() {
* Note that additional grant types must follow the extension policy in the
* OAuth 2 specification.
*
* @param array $grant_types Map of grant type to handler object.
* @param Type[] $grant_types Map of grant type to handler object.
*/
return apply_filters( 'oauth2.grant_types', array() );
$grant_types = apply_filters( 'oauth2.grant_types', array() );
foreach ( $grant_types as $type => $handler ) {
if ( ! $handler instanceof Type ) {
/* translators: 1: Grant type name, 2: Grant type interface */
$message = __( 'Skipping invalid grant type "%s". Required interface "%s" not implemented.', 'oauth2' );
_doing_it_wrong( __FUNCTION__, sprintf( $message, $type, 'WP\\OAuth2\\Types\\Type' ), '0.1.0' );
unset( $grant_types[ $type ] );
}
}

return $grant_types;
}

/**
Expand Down