Login & Auth Workflows

Single-Page Application OAuth Login Using Resource Owner Password Credentials Grant With Sessions And Refresh Tokens

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 (inside SPA)Shopping cart loadSession expiresRefresh token still validShopping cart loadSession expiresRefresh token expiresRe-loginSSO login to forums - not provided by FusionAuth for this workflowInitializeLogin (inside SPA)Forum loadAttack vectorsStolen refresh tokenStolen session idGET /1(SPA HTML, CSS & JavaScript)2AJAX GET /api/user[No cookies]3404 Missing4Render login form5AJAX POST /api/login6POST /oauth2/token(grant_type=password)7(Refresh Token and JWT - ignored)8Create session andstore User in it9(User object)[SessionId and Refresh token HttpOnly w/ domain: store.example.com]10AJAX GET /api/load-shopping-cart[SessionId and Refresh token HttpOnly w/ domain: store.example.com]11Session extended12(Shopping cart contents)13AJAX GET /api/load-shopping-cart[SessionId and Refresh token HttpOnly w/ domain: store.example.com]14POST /oauth2/token or POST /api/jwt/refresh(grant_type=refresh and refresh token)15(JWT)16Create new session17(Shopping cart contents)[New SessionId HttpOnly w/ domain: store.example.com]18AJAX GET /api/load-shopping-cart[SessionId and Refresh token HttpOnly w/ domain: store.example.com]19POST /oauth2/token or POST /api/jwt/refresh(grant_type=refresh and refresh token)20404 Missing21401 Not Authorized22Login same as above23GET /[No cookies]24(SPA HTML, CSS & JavaScript)25GET /api/user[No cookies]26404 Missing27Render login form28AJAX POST /api/login29POST /oauth2/token(grant_type=password)30(Refresh Token and JWT - ignored)31Create session andstore User in it32(User object)[Refresh Token and JWT HttpOnly w/ domain: forums.example.com]33AJAX GET /api/load-posts[SessionId and Refresh token HttpOnly w/ domain: forums.example.com]34Session extended35(Forum posts)36GET /shopping-cart[Refresh token and bad session id HttpOnly w/ domain: store.example.com]37POST /oauth2/token or POST /api/jwt/refresh(grant_type=refresh and refresh token)38(JWT)39Create session andstore User in40(Shopping cart contents)[New session id HttpOnly w/ domain: store.example.com]41GET /api/load-shopping-cart[SessionId HttpOnly w/ domain: store.example.com]42Session extended43(Shopping cart contents)44BrowserStoreForumsFusionAuthHacker

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 application renders the login form
  6. The user inputs their credentials and clicks the submit button. The browser AJAX POSTs the form data to the application backend
  7. The application backend calls the OAuth token endpoint in FusionAuth by passing in the credentials it received plus a grant_type of password, which indicates it is using the resource owner password credentials grant in FusionAuth's OAuth 2 backend
  8. FusionAuth returns a 200 status code stating that the credentials were okay. It also returns a JWT and a refresh token in JSON
  9. The application backend receives the 200 from FusionAuth and creates a server-side session and stores the User object (or JWT) in it
  10. The application backend might return the User object to the browser if it needs it. The id for the server-side session is written back to the browser in an HTTP cookie. The refresh token from FusionAuth is also written back to the browser in an HTTP cookie. These cookies are HttpOnly, which prevents JavaScript from accessing them, making them less vulnerable to theft. Additionally, all requests from the browser to the application backend will include these cookies so that the backend can retrieve the User object from the server-side session and refresh their session if it expires
  11. The browser requests the user's shopping cart from the application backend via AJAX and includes the session and refresh token cookies
  12. The application backend looks up the server-side session associated with the session cookie and extends the expiration date
  13. The application backend loads the User object (or JWT) from the server-side session. The backend then 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)
  14. A while later, the user's session expires and the user clicks on their shopping cart again. The browser requests the shopping cart from the application backend via AJAX and sends the session id and refresh token cookies to the application backend
  15. The application backend attempts to load the server-side session associated with session cookie and realizes it is expired. Since the browser also sent across the refresh token, the application backend calls the JWT refresh API in FusionAuth with the refresh token
  16. FusionAuth looks up the refresh token and returns a new JWT
  17. The application backend receives the 200 from FusionAuth and creates a server-side session and stores the User object (or JWT) in it
  18. The application backend responds with the user's shopping cart contents (usually as JSON). It also includes the new session id as a cookie that replaces the old session id in the browser
  19. A while later, the user's server-side session and refresh token both expire and the user clicks on their shopping cart again. The browser requests the shopping cart from the application backend via AJAX and sends the session and refresh token cookies to the application backend
  20. The application backend attempts to load the server-side session associated with session cookie and realizes it is expired. Since the browser also sent across the refresh token, the application backend calls the JWT refresh API in FusionAuth with the refresh token
  21. Since the refresh token has expired, FusionAuth returns a 404 status code
  22. Since FusionAuth returned a 404 status code, the application backend returns a 401 indicating that the user is no longer logged in
  23. At this point, the application can allow the user to log in the same way they did above
  24. The browser requests the forums single-page application from the application backend. This is a standard SSO login that is fully supported by FusionAuth
  25. The application backend responds with the HTML, CSS & JavaScript of the application
  26. 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
  27. The application backend responds with a 404 indicating the user is not logged in
  28. The application renders the login form
  29. The user inputs their credentials and clicks the submit button. The browser AJAX POSTs the form data to the application backend
  30. The application backend calls the OAuth token endpoint in FusionAuth by passing in the credentials it received plus a grant_type of password, which indicates it is using the resource owner password credentials grant in FusionAuth's OAuth 2 backend
  31. FusionAuth returns a 200 status code stating that the credentials were okay. It also returns a JWT and a refresh token in JSON
  32. The application backend receives the 200 from FusionAuth and creates a server-side session and stores the User object (or JWT) in it
  33. The application backend might return the User object to the browser if it needs it. The id for the server-side session is written back to the browser in an HTTP cookie. The refresh token from FusionAuth is also written back to the browser in an HTTP cookie. These cookies are HttpOnly, which prevents JavaScript from accessing them, making them less vulnerable to theft. Additionally, all requests from the browser to the application backend will include these cookies so that the backend can retrieve the User object from the server-side session and refresh their session if it expires
  34. The browser requests the user's forum posts from the application backend via AJAX and includes the session and refresh token cookies
  35. The application backend looks up the server-side session associated with the session cookie and extends the expiration date
  36. The application backend loads the User object (or JWT) from the session associated with the session cookie. The backend then looks up the user's forum posts from the database (or similar location). Finally, the application backend returns the user's forum posts (usually as JSON)
  37. This is an attack vector where the attacker has stolen the user's refresh token. Here, the attacker requests the user's shopping cart with the stolen refresh token and an invalid session id
  38. The application backend verifies the session id and realizes it is invalid. Since the attacker also sent across the refresh token, the application backend calls the JWT refresh API in FusionAuth with the refresh token
  39. FusionAuth looks up the refresh token and returns a new JWT
  40. The application backend receives the 200 from FusionAuth and creates a server-side session and stores the User object (or JWT) in it
  41. The application backend uses the JWT to look up the user's shopping cart. It responds to the attacker with the user's shopping cart contents (usually as JSON). It also includes the new session id as a cookie that attacker can now use
  42. This is an attack vector where the attacker has stolen the user's session cookie. Here, the attacker requests the user's shopping cart with the stolen session cookie
  43. The application backend looks up the server-side session associated with the session cookie and extends the expiration date
  44. The application backend uses the session to look up the user's shopping cart. It responds to the attacker with the user's shopping cart contents (usually as JSON)

Security considerations

This workflow is one of the more secure methods of authenticating users. One downside is that the application backend receives passwords from the browser. While this isn’t an issue if TLS is used and the passwords are not stored by the application backend, developers that do not want to be part of the password chain of responsibility should consider other workflows.

APIs used

Here are the FusionAuth APIs used in this example: