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

Conversation

tfrommen
Copy link
Contributor

@tfrommen tfrommen commented Jul 1, 2017

Hi again,

@rmccue didn't comment on my What's Left list in #8, but since this was no big deal, I just created this pull request with a proposed validation for grant type( handler)s.

The result of the grant type filter, oauth2.grant_types, will be validated, and only WP\OAuth2\Types\Type implementations make it to the consumer.

Comments?

Cheers,
Thorsten

@rmccue
Copy link
Member

rmccue commented Jul 1, 2017

Whoops, let me go back and answer :)

@rmccue rmccue mentioned this pull request Jul 1, 2017
Copy link
Member

@rmccue rmccue left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we instead just specify function ( Type $type ) and have PHP validate for us? 🤔

plugin.php Outdated
$grant_types = apply_filters( 'oauth2.grant_types', array() );

return array_filter( $grant_types, function ( $type ) {

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for the blank line here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed (aka fixed).

@tfrommen
Copy link
Contributor Author

tfrommen commented Jul 1, 2017

Could we instead just specify function ( Type $type ) and have PHP validate for us? 🤔

No, not really. PHP would not validate (and discard invalid) types, like the code that I suggested, but rather expect correct implementations, and fatal error if encountered anything else.
If this is the desired behavior, then sure, we could add a type hint. But the array_filter() would then get pretty awkward to look at, in my opinion. We do not want to do anything to valid types, so the code could look something like this:

return array_filter( $types, function ( Type $type )  {
    return true;
} );

So, in fact, we wouldn't really need the callback('s body), but just PHP that kicks in for checking (and fatal erroring because of) the type.

To be honest, I would rather keep my code (or something along the lines). But like I said, the bahavior would be discard invalid types. If you want PHP to jump the user into their face, we can do that. 😀

Does this make sense? 🤔

@rmccue
Copy link
Member

rmccue commented Jul 2, 2017

I'd rather not silently drop objects; it makes debugging a pain for developers. If not the fatal error, we should at least fire a _doing_it_wrong when we drop it.

@tfrommen
Copy link
Contributor Author

tfrommen commented Jul 2, 2017

Makes sense. Should I adapt the closure accordingly?

@rmccue
Copy link
Member

rmccue commented Jul 2, 2017

That'd be great, thanks :)

Inform user when they are doing it wrong.
Since it is PHP 5.6, we cannot use array_filter() with the ARRAY_FILTER_USE_BOTH flag, can we?
Thus, use a regular foreach loop.
@tfrommen
Copy link
Contributor Author

tfrommen commented Jul 2, 2017

I just updated the validation code, again.

If only we could use PHP 5.6+... :)
I decided to use a regular foreach loop now, because then I'm able to include the type in the message. If this is not required, I can refactor this to use array_filter(). Another option would be to use array_walk() in combination with another variable, or array_filter() - which both might not be faster than the foreach.

Comments?

Copy link
Member

@rmccue rmccue left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me 👍

@rmccue rmccue merged commit 635cfca into WP-API:master Jul 3, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants