Reputation: 3686
Some users of my service wish to be able to access our REST API (which requires user credentials) via javascript in a browser. I will not allow this generally due to all of the vulnerabilities associated with violating the same origin policy. But if a specific user needs it and understands what is going on... I wish to add him to a "can make cross origin request" whitelist.
However, I cannot figure out how to selectively return a Access-Control-Allow-Origin header based on http authorization information.
If I do this in javascript:
$.ajax({'url':'url', 'beforeSend': function (xhr) { xhr.setRequestHeader("Authorization", "Basic " + 'base64encodedcreds' ) } })
The browser first sends an ORIGIN METHOD request without the Authorization header. Consequently, there is no way for me to know if I should include Access-Control-Allow-Origin in the response.
Is there some setting in the xmlhttprequest to get it to send an ORIGIN with the authorization? Or is there another way to selectively grant cross-origin access?
Upvotes: 1
Views: 282
Reputation: 3686
Based on duskwuff's answer that this was impossible I took this close route to not have to create new urls:
(Very easy to pull off with django middleware)
Upvotes: 1
Reputation:
There's no way to bypass this behavior -- passing an authorization header is one of the bits of behavior that AAAO is trying to restrict. But you can sidestep it:
Access-Control-Allow-Origin
that permits access. (Or whatever.)Upvotes: 1