What is the proper way of combining session-based authentication with stateless, token-based authentication for a REST API?
Use case:
- User logs-in in the standard, traditional, session-based way. Most of the website uses this login flow.
- On one of the pages, a JavaScript script wants to request some data from a RESTful stateless API on behalf of the user
I expect the JavaScript script will need to be given something in the page (e.g. in meta tags) as the page loads to be able to authenticate with the API. The API uses OAuth2.
What should it be and how best to give it to JavaScript?
- The code in the Authorization Code grant? This would mean JavaScript will have to make another authorization request to get the access token.
- The access token itself? This feels insecure, outputting the access token into the HTML of the page.
- Something else?
How to give it to JavaScript?
- Meta tag?
- Headers?
- Something else?
The OAuth documentation / tutorials are really unclear about this.