CORS and the OAuth 2 authorization code flow


I have a back end application that is protected with the OAuth 2 authorization code flow. The front end (javascript in browser) hits an authorization endpoint on the back end, the back end redirects the browser to an authorization code server, the user authenticates and then the authorization server redirects the browser back to the back end with an authorization code which the back end redeems for a token to access some services.

The problem is that these redirects all happen in succession and CORS in the browser is preventing the exchange. What do the servers need to do as far as CORS to make this flow work?

browser                                 -> POST
307 <-
browser                                 -> POST (with authorization header)
307         <-
browser                                 -> POST

Is roughly how it is supposed to go.

EDIT: Browser says

XMLHttpRequest cannot load The request was redirected to '', which is disallowed for cross-origin requests that require preflight.

EDIT2: So the redirect works fine without the Authorization header. Guess that data is going in the body for now.

asked on Stack Overflow Sep 19, 2015 by kag0 • edited May 22, 2018 by kag0

1 Answer


The thing that seems incorrect to me here is that you're trying to use a redirection protocol flow from JavaScript.

Normally, your browser gets redirected to the authorization server and upon successful authentication, the browser is redirected back to the application with an auth-code or access token (depending on which flow is used).

In that case, you are not talking to the authorization server from JavaScript, so cross origin considerations do not come into play.

If you want to use OAuth2 from a JavaScript client, I suggest you look at the implicit grant, which is a redirection flow designed for untrusted clients like JavaScript applications.

answered on Stack Overflow Sep 20, 2015 by MvdD

User contributions licensed under CC BY-SA 3.0