/Flask-JWT

A demo of Flask using JWT to protect routes with MongoDB backend, all of this Dockerized

Primary LanguagePython

Certainly, here's the cleaned-up version of your Markdown content. It includes proper formatting, corrections to the image URLs, and minor text improvements for clarity.


Flask API JWT Access Control: An Example with Pydantic, Flasgger, and MongoDB

Flask JWT Flasgger Gunicorn MongoDB Pydantic OpenAPI Docker Docker-Compose

Source: GitHub Repository

This project has evolved to demonstrate several functionalities:

  • tokenreq decorator function for route security.
  • Signup route for registering a username, email, and password in MongoDB.
  • Login route for obtaining access and refresh JWT tokens.
  • Refresh route for renewing the access token when it expires.
  • Unprotected and protected routes to demonstrate JWT-based security measures.

Security Techniques

Several security techniques have been added. The application uses an unprotected MongoDB backend running on localhost:27017.

Configuration Files

  • .env: Set environment variables. Use .env.example as a template.
  • pyproject.toml: Lists prerequisites. Run poetry install to install dependencies.

Gunicorn Support

Install Gunicorn within the poetry shell environment. To verify the installation, run which gunicorn.

VSCode Debugging

You'll need a launch.json file as shown below to run the app in debugging mode.

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Python: Flask",
      "type": "python",
      "request": "launch",
      "program": "/Users/bob/code/Rest_API_Flask/.venv/bin/gunicorn",
      "gevent": true,
      "args": ["app.run:app", "--bind=127.0.0.1:8000", "--reload", "-w", "4"]
    }
  ]
}

Note: Using the simple RUN command in VSCode will launch Werkzeug on port 5000, not Gunicorn.

Running the Code

For shell execution, use the provided runme.sh script. In this case, Gunicorn is launched as a Python module, not as an installed binary.


Let me know if this revision meets your requirements.