Hi all,
I have a Google Cloud Run service running without anonymous access (Authentication set to Authenticated under service details). On my container, I have CORS configured with the appropriate Access-Control-Allow-Origin header defined on all responses.
In a web app under Chrome, I'm attempting to authenticate a user via Firebase, get an ID token, and then make authenticated calls to the GCR service:
firebase.auth().currentUser.getIdToken().then(function(idToken) {
let req = new XMLHttpRequest();
req.open('GET', 'https://<GCR Service URL>/example-path', false);
req.setRequestHeader('Authorization', 'Bearer ' + idToken);
req.addEventListener("load", function () {
console.log('request_status load', this.responseText);
});
req.send(null);
});
Everything appears to work in the browser up until the preflight request goes out. It appears GCR is expecting the Authorization header (as mentioned under
https://cloud.google.com/run/docs/securing/authenticating#end-users) on an OPTIONS request (see below). From my research, it appears that adding custom headers to a preflight request isn't possible within a browser.
GCR log
OPTIONS 403 0 B 0 ms Chrome 73 /example-path
Console errors
OPTIONS https://<GCR Service URL>/example-path 403
Access to XMLHttpRequest at 'https://<GCR Service URL>/example-path' from origin 'http://localhost:8080' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
Uncaught DOMException: Failed to execute 'send' on 'XMLHttpRequest': Failed to load 'https://<GCR Service URL>/example-path'.
Other than changing the service to allow unauthenticated end-user requests (which is a last resort), how can I get past the OPTIONS call to GCR requiring an authorization token?
Thanks!
- Derek