Login & Auth Workflows

WebApp OAuth Login Using Resource Owner Password Credentials Grant With JWTs And Refresh Tokens

By Brian Pontarelli

This workflow is used by web applications using the FusionAuth OAuth login interface. The web application navigates over to FusionAuth and then FusionAuth redirects back to the web application at the end of the OAuth workflow. 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 WebApp)Shopping cart loadJWT expiresShopping cart loadRefresh tokenexpiresRe-loginSSO login to forums - not provided by FusionAuth for this workflowInitializeLogin (inside WebApp)Forum loadAttack vectorsStolen refresh tokenStolen JWTGET /1(HTML, CSS & JavaScript - with login link)2GET /login3(Login form HTML)4POST /login5POST /oauth2/token(grant_type=password)6(Refresh Token and JWT)7302 Location: /shopping-cart[Refresh Token and JWT HttpOnly w/ domain: store.example.com]8GET /shopping-cart[Refresh token and JWT HttpOnly w/ domain: store.example.com]9(Shopping cart HTML)10GET /shopping-cart[Refresh token and JWT HttpOnly w/ domain: store.example.com]11POST /oauth2/token or POST /api/jwt/refresh(grant_type=refresh and refresh token)12(JWT)13(Shopping cart HTML)[New JWT HttpOnly w/ domain: store.example.com]14GET /shopping-cart[Refresh token and JWT HttpOnly w/ domain: store.example.com]15POST /oauth2/token or POST /api/jwt/refresh(grant_type=refresh and refresh token)16404 Missing17302 Location: /login18Login same as above19GET /[No cookies]20(HTML, CSS & JavaScript - with login link)21GET /login22(Login form HTML)23POST /login24POST /oauth2/token(grant_type=password)25(Refresh Token and JWT)26302 Location: /posts[Refresh Token and JWT HttpOnly w/ domain: forums.example.com]27GET /posts[Refresh token and JWT HttpOnly w/ domain: forums.example.com]28(Forum posts HTML)29GET /shopping-cart[Refresh token and bad JWT HttpOnly w/ domain: store.example.com]30POST /oauth2/token or POST /api/jwt/refresh(grant_type=refresh and refresh token)31(JWT)32(Shopping cart HTML)[New JWT HttpOnly w/ domain: store.example.com]33GET /shopping-cart[JWT HttpOnly w/ domain: store.example.com]34(Shopping cart HTML)35BrowserStoreForumsFusionAuthHacker

Explanation

  1. The browser requests the shopping cart webapp's homepage from the application backend
  2. The application backend responds with the HTML, CSS & JavaScript of the homepage
  3. The user clicks the login link and the browser requests the login page from the application backend
  4. The application backend responds with the HTML, CSS & JavaScript of the login page (including the form)
  5. The user inputs their credentials and clicks the submit button. The browser POSTs the form data to the application backend
  6. 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
  7. FusionAuth returns a 200 status code stating that the credentials were okay. It also returns a JWT and a refresh token in JSON
  8. The application backend receives the 200 from FusionAuth. It returns a redirect to the browser instructing it to navigate to the user's shopping cart. The JWT and refresh token from FusionAuth are written back to the browser in HTTP cookies. 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 use them
  9. The browser requests the user's shopping cart from the application backend and includes the JWT and refresh token cookies
  10. 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 as HTML, CSS & JavaScript that the browser renders
  11. A while later, the user's JWT expires and the user clicks on their shopping cart again. The browser requests the shopping cart from the application backend and sends the JWT and refresh token to the application backend
  12. The application backend verifies the JWT 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.
  13. FusionAuth looks up the refresh token and returns a new JWT
  14. The application backend responds with the user's shopping cart HTML, CSS & JavaScript that the browser renders. It also includes the new JWT as a cookie that replaces the old JWT in the browser
  15. A while later, the user's refresh token expires and the user clicks on their shopping cart again. The browser requests the shopping cart from the application backend and sends the JWT and refresh token to the application backend
  16. The application backend verifies the JWT 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
  17. Since the refresh token has expired, FusionAuth returns a 404 status code
  18. Since FusionAuth returned a 404 status code, the application backend returns a redirect to the browser that sends the user to the login page
  19. The user can log in the same way they did above
  20. The browser requests the forum webapp's homepage from the application backend. This is a standard SSO login, but because of the way this workflow manages cookies and identities, FusionAuth does not provide SSO capabilities automatically
  21. The application backend responds with the HTML, CSS & JavaScript of the homepage
  22. The user clicks the login link and the browser requests the login page from the application backend
  23. The application backend responds with the HTML, CSS & JavaScript of the login page (including the form)
  24. The user inputs their credentials and clicks the submit button. The browser POSTs the form data to the application backend
  25. 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
  26. FusionAuth returns a 200 status code stating that the credentials were okay. It also returns a JWT and a refresh token in JSON
  27. The application backend receives the 200 from FusionAuth. It returns a redirect to the browser instructing it to navigate to the user's forum posts. The JWT and refresh token from FusionAuth are written back to the browser in HTTP cookies. 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 use them
  28. The browser requests the user's forum posts from the application backend and includes the JWT and refresh token cookies
  29. 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 as HTML, CSS & JavaScript that the browser renders
  30. 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 JWT
  31. The application backend verifies the JWT and realizes it is invalid. Since the browser also sent across the refresh token, the application backend calls the JWT refresh API in FusionAuth with the refresh token
  32. FusionAuth looks up the refresh token and returns a new JWT
  33. 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 HTML, CSS & JavaScript. It also includes the new JWT as a cookie that attacker can now use
  34. This is an attack vector where the attacker has stolen the user's JWT. Here, the attack requests the user's shopping cart with the stolen JWT
  35. 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 as HTML, CSS & JavaScript to the attacker

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: