-
Notifications
You must be signed in to change notification settings - Fork 112
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
get_parameters() is ignoring data params sent as application/json #95
Comments
This is annoying, but we can't simply read |
Ahhh. I see. It caters for the deprecation of |
Hmm, so, thinking over this again: I don't think we're meant to mix in any of the JSON data at all. The OAuth RFC explicitly states:
Based on this, I don't think we should be parsing out any of the data at all, so the current behaviour seems correct; although we're lacking the Content-Type header check, I think this is handled by PHP when parsing out |
Yeah, I quoted that bit above. ;) I see the WP_REST_Server I've built apps against some APIs which allow for sending extra params in either GET POST or via json/application payloads. I've also seen some which only allow oauth_... params in the Auth header. OAuth can be a mess for some (and also why an author since walked away from it), so why not err on the side of flexibility? :) Unless there's a gaping security hazard which I haven't seen yet? Go JSON, I say! Especially since the WPI-API is all about the JSON, eh? ;) |
Indeed, it makes sense to sign the body given that's where most of the data actually is, but we're best off sticking to the spec.
Of course, I forgot about the OAuth meta parameters themselves, that makes sense. That said, it's a bit of a bad idea to send those in the JSON data, as it might interfere with the actual payload itself. There's no cases in the core REST API where the payload is used wholesale, but it's possible that could happen; this could lead to your OAuth parameters accidentally getting saved to the database as part of an object. As far as I know, there's no standard libraries which send the OAuth params in JSON data in the body, so I think we're fine not supporting that. Either GET parameters or the Authorization header (with a preference for the latter) are the best spots to send the data.
The parameters are used for two things: getting the OAuth meta parameters, and building the signature base string. We can't do one without the other, and accepting JSON here would make our implementation non-conforming per the first point. Going to close this out as wontfix in light of the above; if anyone needs the ability to send OAuth parameters in JSON, please note here why and we can reconsider this in the future. :) @kosso Thanks for filing the issue! 🍍 |
OK. I guess I'll just have to live with that. ;) I just irks me a little that we can't send pure JSON data (not the oauth_.. headers) to a JSON based API server. Naturally, multi-depth structured JSON might be problematic. But as long as a client and server both parse each param/property, and format, order and concatenate the base string the same way, it could work ;) Is it something a developer could write a plugin/hook to override if they wanted to? I haven't looked too deeply yet into how I could 'enhance' the server (plugin, for now) with my own plugin if I wanted to. eg: I might want my consumer apps to have more fields or extra functionality in the wp-admin forms, etc. |
The OAuth stuff is built almost entirely separately from the API itself, so there's only a loose binding between the two.
Realistically, the OAuth meta-parameters belong in the Authorization header; with those being sent there, you should be able to send entirely JSON requests, unless I've missed something?
I suspect it's possible if you need to :) |
Possibly. That's the thing, neither $_POST nor $HTTP_RAW_POST_DATA can read data from an incoming application/json POST without using php://input. eg: Say I want to create a new post: (sorry, I go on a bit here - just for my own clarity ;) ) I need to make a signature for a Let's say this is the JSON I want to send:
I then get my .. breathes.. ;) .. then the app will build the Authorization header to use, using just the oauth_ meta-parameters, along with the freshly generated Currently, this fails for me. Without the additional php://input I suggested, the server never sees the json payload parameters to assist in rebuilding the same base string to then sign itself and compare signatures to authenticate. The current system can't accept an Authorized POST of It's quite possible though, that OAuth has fried my brain and I'm vastly over-thinking this. ;) |
Right, so per the OAuth spec, you shouldn't be adding the parameters from the payload unless they're URL-encoded. The spec also notes this:
The payload parameters are only included when the Content-Type is application/x-www-form-urlencoded, not for anything else. |
That is: if you have the following request:
Then the signature pieces are:
And the signature base string is
Note: no title parameter in there :) |
get_parameters()
is still ignoring extra post data parameters sent as application/json.Is this on purpose?
When merging all $_GET and $_POST parameters, along with Authorization headers, since PHP 5.6 data sent as application/json is ignored in $_POST, so we need to also collect parameters from posted JSON data using php://input
After line 93 of class-wp-json-authentication-oauth1.php
This fixed my
Missing OAuth parameter oauth_verifier
error.Now, I can finally connect a client to my server and get an access_token for its user, since my OAuth client was sending the
oauth_verifier
as application/json. (And likely to interact with the API in this way for all other POST requests).I have also successfully tested creating a new post with my newly acquired access_token/secret after making these changes, when sending the data as application/json.
One thing, however... the OAuth1.0a signing requests spec does state:
note: "(with a content-type of application/x-www-form-urlencoded)"
but this ignores the PHP5.6+ issue when sending applicaton/json formatted data.
also note: When I tested uploading a media file, I found I needed to use : multipart/form-data (in a PHP cUrl request)
The text was updated successfully, but these errors were encountered: