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

[CORS] Access-Control-Allow-Origin header missing #83

Closed
shprink opened this issue Sep 5, 2015 · 7 comments
Closed

[CORS] Access-Control-Allow-Origin header missing #83

shprink opened this issue Sep 5, 2015 · 7 comments

Comments

@shprink
Copy link

shprink commented Sep 5, 2015

From a JS client I get the following message on oauth1/request:

No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access. The response had HTTP status code 400.

You might want to copy this function from the WP-API:

function rest_send_cors_headers( $value ) {
    $origin = get_http_origin();

    if ( $origin ) {
        header( 'Access-Control-Allow-Origin: ' . esc_url_raw( $origin ) );
        header( 'Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE' );
        header( 'Access-Control-Allow-Credentials: true' );
    }

    return $value;
}
@romuloctba
Copy link

https://gist.github.com/Shelob9/87f9474df0f541e07383

2015-09-05 2:09 GMT-03:00 Julien Renaux notifications@github.com:

From a JS client I get the following message:

No 'Access-Control-Allow-Origin' header is present on the requested
resource. Origin 'http://localhost:8080' is therefore not allowed access.
The response had HTTP status code 400.

You might want to copy this function from the WP-API:

function rest_send_cors_headers( $value ) {
$origin = get_http_origin();

if ( $origin ) {
    header( 'Access-Control-Allow-Origin: ' . esc_url_raw( $origin ) );
    header( 'Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE' );
    header( 'Access-Control-Allow-Credentials: true' );
}

return $value;

}


Reply to this email directly or view it on GitHub
#83.

@shprink
Copy link
Author

shprink commented Sep 5, 2015

@romuloctba Don't you want to create a PR so everybody can benefit from this changes (if it works).

@romuloctba
Copy link

Well, my friend, those are not changes, they are 3 diff hooks that will provide a header with the required Access-controll-allow-origin and etc for you. You should put in your functions.php or (better) wrap it into a plugin.

Btw is not my code also. You could install wp-api cors available @ WordPress plugin directory, but you should not use it in production, since it allows all with *.

I don't think the oAuth plugin should have anything related to CORS, since it should be used with wp-api, that should.

Well, this is untested thou, so I can only hope it helps you with your problem.

@itsananderson
Copy link

That Gist doesn't work for Oauth1 because the "rest_pre_serve_request" action doesn't get triggered for Oauth1 requests (since it's not technically a WP-API route).

I wrote a plugin that tackles the CORS issues with Oauth, by plugging into template_redirect, but it's a little hacky. https://github.com/itsananderson/wp-api-cors/blob/09d0e5e13343b492fb0ab060b59bde836505220d/wp-api-cors.php#L25

The other issue you run into after fixing the CORS issues is that the Oauth1 plugin uses wp_http_validate_url to validate the callback URL, which prevents localhost apps from authenticating using Oauth1.

@coderkevin
Copy link

If you're thinking of using CORS to send your Client ID and Client Secret to your WP site, I suggest you reconsider and use a server call or a server proxy. Holding a Client Secret securely in client-side code is impossible. For a longer explanation, look here: http://alexbilbie.com/2014/11/oauth-and-javascript/

@rmccue
Copy link
Member

rmccue commented Nov 23, 2015

Per @coderkevin's note, you're not meant to access these directly via the client side, which is why they don't have the CORS headers.

@rmccue rmccue closed this as completed Nov 23, 2015
@larssn
Copy link

larssn commented Mar 7, 2018

That depends on the auth flow you've built. In my case, only the first few steps (until the WP login step) is handled in the JS client. After that the rest of the flow is effectively handed off to a server (cloud), which then handles the remaining steps behind the scenes. This server then securely communicates with the resource and exchanges data, and the client doesn't need access to the OAuth1 tokens, keys etc, but can still benefit from the data exchange.

The client thus only needs the key and secret for a short time.

However this is currently not possible due to the reasons mentioned above.

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

No branches or pull requests

6 participants