I am trying to understand how to implement an OAuth 2 Authorization Code flow when having both a single page JS app and a REST API. The aim is to secure access to the REST API by offloading authentication to the OAuth provider.
Currently my flow looks like:
1)
+--------------------+ +----------------+
| JS Single Page App | - redirect -> | OAuth Provider | - user enters credentials
+--------------------+ +----------------+
2)
+----------------+ +----------+
| OAuth Provider | - redirect with temporary code -> | REST API |
+----------------+ +----------+
3)
+----------+ +----------------+
| REST API | - request access token using code -> | OAuth Provider |
+----------+ <- return access token ------------- +----------------+
What should I do now? My current understanding is that I should redirect the user upon receiving the access token to a page that will load the JS single page app again. But should I share the access token with the single page app and use the presence of it to authenticate any request hitting my REST API, or is better to create a separate identifier and maintain a server-side mapping between new-identifier->access_token
? Either way what would be the best way of transferring whichever identifier to the client? I would prefer not maintain any session and not have the identifier turn up in the URL bar of the redirected page. The only thing I can currently think of would be to create a temporary cookie that the single page app would read and then erase, but somehow that feels a bit clunky.