Login & Auth Workflows
Mobile Login To FusionAuth With JWTs And Refresh Tokens - Recommended
By Brian Pontarelli
This workflow is used by mobile applications using a native login form inside the app. This login form
For the mobile application examples, we only use a store app and do not provide an SSO workflow. The store requires a user to login to view their shopping cart. 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 hacked devices.
Diagram
Legend
() --> request/response bodies
{} --> request parameters
[] --> cookies
Explanation
- The user opens the application. The application initializes itself and then renders its login form
- The user inputs their credentials and clicks the submit button. The application
POST
s the form data directly to FusionAuth - FusionAuth returns a 200 status code stating that the credentials were okay. It also returns the User object, a JWT and a refresh token in JSON
- The application requests the user's shopping cart from the application backend and includes the JWT and refresh token
- 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
- A while later, the user's JWT expires and the user clicks on their shopping cart again. The application requests the shopping cart from the application backend and sends the JWT and refresh token to the application backend
- 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
- FusionAuth looks up the refresh token and returns a new JWT
- The application backend responds with the user's shopping cart contents. It also includes the new JWT that replaces the old JWT
- A while later, the user's refresh token expires and the user clicks on their shopping cart again. The application requests the shopping cart from the application backend and sends the JWT and refresh token to the application backend
- The application backend verifies the JWT and realizes it is expired. Since the application also sent across the refresh token, the application backend calls the JWT refresh API in FusionAuth with the refresh token
- Since the refresh token has expired, FusionAuth returns a 404 status code
- Since FusionAuth returned a 404 status code, the application backend returns a 401 that indicates the user is no longer logged in
- At this point, the application can allow the user to log in the same way they did above
- 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
- The application backend verifies the JWT 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
- FusionAuth looks up the refresh token and returns a new JWT
- 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. It also includes the new JWT that attacker can now use
- 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
- 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
Security considerations
This workflow is one of the more secure methods of authenticating users. It uses a JWT and refresh token that are securely store on the mobile device. It has the benefit that passwords are only provided directly to FusionAuth.
APIs used
Here are the FusionAuth APIs used in this example: