Search code examples
flaskaws-lambdajwtflask-jwt-extended

Storing JWT tokens externally in Flask-JWT-Extended


I have a Flask app running in AWS using Flask-JWT-Extended. It is serving REST API calls to a web app.

As I understand from the documentation, the tokens are generated and stored in memory. I am considering storing them external to the Flask app in either a database or Redis. The reason for this is to support load balancing:

  • I presume that sticky-sessions would be required to make sure that the client's token can be properly decoded and analyzed for validity
  • I am considering putting the app in AWS Lambda, which would probably wipe out the JWT list once the request was served.

My questions are:

  • Is there any reason this scheme would not generally work?
  • If the tokens are stored outside the Flask app, it is not clear how to override the local token storage and access an external storage medium. Can this be done?

Solution

  • The tokens are not stored in memory. JWT works by creating tokens that can be verified to have not been tampered with without having to keep any state on the server (just make sure to keep the JWT_SECRET_KEY really and truly secret). Here is an article about stateless authentication you might want to read up on: https://blog.imaginea.com/stateless-authentication-using-jwt-2/