Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enable CORS with Google IAP

I'm are trying make an ajax call to my web api server from a third party JavaScript integration. However after enabling IAP on my API server, I'm unable to make the calls to my web apis from my integration server. I have added my integration server as Authorized JavaScript origins in the Client ID for Web application.

Below is the error.

Access to XMLHttpRequest at 'https://webapiserver.com/apiendpoint1' from origin 'https://myintegration.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

Please let me know if there is other configuration missing at my end. Thanks.

like image 798
Feroz Shaikh Avatar asked Nov 05 '18 23:11

Feroz Shaikh


People also ask

Does Google support CORS?

You cannot manage CORS using the Google Cloud console.

What is CORS policy Google it and read about it?

Cross-origin resource sharing (CORS) is a standard mechanism that allows JavaScript XMLHttpRequest (XHR) calls executed in a web page to interact with resources from non-origin domains. CORS is a commonly implemented solution to the same-origin policy that is enforced by all browsers.

What is identity-aware proxy in GCP?

Identity-Aware Proxy (IAP) is a Google Cloud Platform service that intercepts web requests sent to your application, authenticates the user making the request using the Google Identity Service, and only lets the requests through if they come from a user you authorize.


1 Answers

CORS requires unauthenticated OPTIONS request. Your API server needs to allow it, to do it, you need:

  1. Add code to your app that responds to the OPTIONS requests.
  2. Change the setting access_settings.cors_settings.allow_http_options to true so that IAP passes OPTIONS requests through to your application.

More information about customizing IAP in the official Google documentation.

This feature was not previously available, and proposed workaround existed:

In the same GCP project create App Engine services:

  • A UI server
  • An API server

The UI server makes calls to the API server. To get around the CORS problem, use Routing with a dispatch file: https://cloud.google.com/appengine/docs/standard/python/how-requests-are-routed#routing_with_a_dispatch_file

Normally, when the UI server wants to call API server, it calls api-dot-MY_PROJECT.appspot.com

In this workaround, the UI server calls itself with a specified path ("/api"): MY_PROJECT.appspot.com/api

Since the UI server is calling itself, CORS does not apply.

like image 196
5 revs, 2 users 78% Avatar answered Oct 12 '22 11:10

5 revs, 2 users 78%