Login & Auth Workflows

Single-Page Application OAuth Login Using Implicit Grant With JWTs In Cookies

By Brian Pontarelli

This workflow is used by single-page applications using the FusionAuth OAuth login interface. The single-page application navigates away from its interface and over to FusionAuth’s OAuth interface. Once the user completes their login, FusionAuth redirects back to the single-page application. This requires that the single-page application re-initialize itself, but the browser should cache the application files and be able to restart it quickly. Below is a diagram that describes the primary components of this workflow and how they interact. Keep in mind that not every interaction is covered here, just the primary login interactions. At the bottom of the diagram is a discussion of the key steps.

For all of our examples, we use a store and a forum for the same company. The store requires a user to login to view their shopping cart and the forum requires the user to login to view forum posts. We also provide a couple of example attack vectors that hackers could use if portions of the system are compromised. These cases might be theoretical or based on known exploits such as XSS (cross-site scripting).

Diagram

Legend

() --> request/response bodies
{} --> request parameters
[] --> cookies
BrowserStoreForumsFusionAuthHackerInitializeLogin (browser navigates away from SPA)Cookie dropShopping cart loadJWT expiresShopping cart loadAutomatic re-login (since session exists)Cookie dropShopping cart loadSSO login to forumsLogin (browser navigates away from SPA but auto-logs-in since the session exists)Cookie dropForums loadAttack vectorsStolen JWTGET /1(SPA HTML, CSS & JavaScript)2AJAX GET /api/user[No cookies]3404 Missing4GET /oauth2/authorize {response_type=token}5(Login form HTML)6POST /oauth2/authorize (response_type=token)7302 Location: {redirect_uri w/ JWT in redirect URI and no refresh token}[SessionId HttpOnly w/ domain: example.fusionauth.io]8GET {redirect_uri w/o JWT in URI}9(SPA HTML, CSS & JavaScript)10AJAX POST /api/cookie-drop(JWT from redirect_uri)11[JWT HttpOnly w/ domain: store.example.com]12AJAX GET /api/load-shopping-cart[JWT HttpOnly w/ domain: store.example.com]13(Shopping cart contents)14AJAX GET /api/load-shopping-cart[JWT HttpOnly w/ domain: store.example.com]15401 Not Authorized16GET /oauth2/authorize {response_type=token}[SessionId HttpOnly w/ domain: example.fusionauth.io]17302 Location: {redirect_uri w/ JWT in redirect URI and no refresh token}[SessionId HttpOnly w/ domain: example.fusionauth.io]18GET {redirect_uri w/o JWT in URI}19(SPA HTML, CSS & JavaScript)20AJAX POST /api/cookie-drop(JWT from redirect_uri)21[JWT HttpOnly w/ domain: store.example.com]22AJAX GET /api/load-shopping-cart[JWT HttpOnly w/ domain: store.example.com]23(Shopping cart contents)24GET /[No cookies]25(SPA HTML, CSS & JavaScript)26AJAX GET /api/user[No cookies]27404 Missing28GET /oauth2/authorize {response_type=token}[SessionId HttpOnly w/ domain: example.fusionauth.io]29302 Location: {redirect_uri w/ JWT in redirect URI and no refresh token}[SessionId HttpOnly w/ domain: example.fusionauth.io]30GET {redirect_uri w/o JWT in URI}31(SPA HTML, CSS & JavaScript)32AJAX POST /api/cookie-drop(JWT from redirect_uri)33[JWT HttpOnly w/ domain: forums.example.com]34AJAX GET /api/load-load-posts[JWT HttpOnly w/ domain: forums.example.com]35(Forum posts)36GET /api/load-shopping-cart[JWT HttpOnly w/ domain: store.example.com]37(Shopping cart contents)38BrowserStoreForumsFusionAuthHacker

Explanation

  1. The browser requests the shopping cart single-page application from the application backend
  2. The application backend responds with the HTML, CSS & JavaScript of the application
  3. The browser loads the application and as part of the initialization process, it makes a request to the application backend to see if the user is logged in
  4. The application backend responds with a 404 indicating the user is not logged in
  5. The user clicks the login link and the browser navigates away from the single-page application to FusionAuth's OAuth 2 interface. The browser requests the OAuth 2 login page from FusionAuth with a response_type of token indicating that it is using the implicit grant
  6. FusionAuth responds with the HTML, CSS & JavaScript of the login page (including the form)
  7. The user inputs their credentials and clicks the submit button. The browser POSTs the form data to FusionAuth
  8. FusionAuth returns a redirect to the application backend's OAuth 2 redirect_uri. This redirect includes the access token (in our case a JWT) from FusionAuth. Also, this response includes a session id for the FusionAuth OAuth 2 interface as an HTTP cookie. This cookie is HttpOnly, which prevents JavaScript from accessing it, making it less vulnerable to theft
  9. The browser requests the application backend's OAuth redirect_uri. This request does not include the JWT because it is after the # in the URL, which means the browser will not send it in the HTTP request to the application backend
  10. The application backend responds with the HTML, CSS & JavaScript of the application. During this step, the browser will initialize the single-page application. As part of the initialization of the application, the JWT will be pulled from the current URL of the browser
  11. The application calls the cookie drop API in the application backend and sends it the JWT in the request body
  12. The application backend responds with a 200 and an HttpOnly cookie that contains the JWT
  13. The browser requests the user's shopping cart via AJAX from the application backend and includes the JWT
  14. The application backend verifies the JWT and then uses the JWT to identify the user. Once the user is identified, the backend looks up the user's shopping cart from the database (or similar location). Finally, the application backend returns the user's shopping cart contents (usually as JSON)
  15. A while later, the user's JWT expires and the user clicks on their shopping cart again. The browser requests the user's shopping cart via AJAX from the application backend and includes the JWT cookie
  16. The application backend responds with a 401, indicating that the JWT has expired
  17. The application might automatically redirect the browser back to FusionAuth's OAuth 2 interface or wait until the user clicks the login link. Regardless, the browser requests the OAuth 2 login page from FusionAuth with a response_type of token indicating that it is using the implicit grant
  18. FusionAuth realizes that the user already has a session and is already logged in. Therefore, it returns a redirect to the application backend's OAuth 2 redirect_uri. This redirect includes a new JWT from FusionAuth in the URI
  19. The browser requests the application backend's OAuth redirect_uri. This request does not include the JWT because it is after the # in the URL, which means the browser will not send it in the HTTP request to the application backend
  20. The application backend responds with the HTML, CSS & JavaScript of the application. During this step, the browser will initialize the single-page application. As part of the initialization of the application, the JWT will be pulled from the current URL of the browser
  21. The application calls the cookie drop API in the application backend and sends it the JWT in the request body
  22. The application backend responds with a 200 and an HttpOnly cookie that contains the JWT
  23. The browser requests the user's shopping cart via AJAX from the application backend and includes the JWT
  24. The application backend verifies the JWT and then uses the JWT to identify the user. Once the user is identified, the backend looks up the user's shopping cart from the database (or similar location). Finally, the application backend returns the user's shopping cart contents (usually as JSON)
  25. The browser requests the forums single-page application from the application backend. This is a standard SSO login that is fully supported by FusionAuth
  26. The application backend responds with the HTML, CSS & JavaScript of the application
  27. The browser loads the application and as part of the initialization process, it makes a request to the application backend to see if the user is logged in
  28. The application backend responds with a 404 indicating the user is not logged in
  29. The application might automatically redirect the browser back to FusionAuth's OAuth 2 interface or wait until the user clicks the login link. Regardless, the browser requests the OAuth 2 login page from FusionAuth with a response_type of token indicating that it is using the implicit grant
  30. FusionAuth realizes that the user already has a session and is already logged in. Therefore, it returns a redirect to the application backend's OAuth 2 redirect_uri. This redirect includes a new JWT from FusionAuth in the URI
  31. The browser requests the application backend's OAuth redirect_uri. This request does not include the JWT because it is after the # in the URL, which means the browser will not send it in the HTTP request to the application backend
  32. The application backend responds with the HTML, CSS & JavaScript of the application. During this step, the browser will initialize the single-page application. As part of the initialization of the application, the JWT will be pulled from the current URL of the browser
  33. The application calls the cookie drop API in the application backend and sends it the JWT in the request body
  34. The application backend responds with a 200 and an HttpOnly cookie that contains the JWT
  35. The browser requests the user's forum posts from the application backend via AJAX and includes the JWT cookie
  36. The application backend verifies the JWT and then uses the JWT to identify the user. Once the user is identified, the backend looks up the user's forum posts from the database (or similar location). Finally, the application backend returns the user's forum posts that the browser renders (usually as JSON)
  37. This is an attack vector where the attacker has stolen the user's JWT. Here, the attacker requests the user's shopping cart with the stolen JWT
  38. The application backend verifies the JWT and then uses the JWT to identify the user. Once the user is identified, the backend looks up the user's shopping cart from the database (or similar location). Finally, the application backend returns the user's shopping cart to the attacker (usually as JSON)

Security considerations

This workflow is less secure than other workflows because the JWT is available to JavaScript during the cookie drop process. While this is a small window of time, it is still possible that malicious JavaScript running in the application could gain access to the JWT. If an attacker can inject JavaScript into the page, they can begin stealing user’s JWTs. The attacker might introduce JavaScript into an open source project through obfuscated code or through a backend exploit of some kind. Many platforms like Wordpress also allow plugins to add JavaScript includes to websites as well. Therefore, ensuring that your JavaScript is secure can be extremely difficult.

This workflow might still be a good solution for some applications. Developers should just weigh the risks associated with JWTs accessible to JavaScript versus the other workflows we have documented.

Additionally, since this workflow does not use refresh tokens (and cannot use refresh tokens according to the specification). Therefore, when the user’s session expires, they will need to log into the application again. This could be an automatic login, but it still requires the browser to take the user to the FusionAuth OAuth interface.

APIs used

Here are the FusionAuth APIs used in this example: