Skip to main content

Flask Auth Middleware

Flask Auth Middlewawre is a middleware for securing flask application. It provides a convenient way to secure your Flask routes and endpoints using jwt tokens.

Features

  • Seamless integration with Flask applications.
  • Easily protect routes and endpoints with JWT authentication.
  • Lightweight and designed for simplicity.

Requirements

Python 3

Installation

$ pip install flask_auth_middleware

Example

Create it

  • Create a file app.py with:
from flask import Flask
from flask_auth_middleware import JWTAuthMiddleware

app = Flask(__name__)

app.wsgi_app = JWTAuthMiddleware(app, "your_secret_key")


@app.get("/")
def hello():
    return "Hello, World!"

@app.get("/namaste")
def hello():
    return "Namaste, World!"

Here all the routes of flask application will be protected by JWTAuthMiddleware.

If you want to secure selective routes. Then here you go:

from flask import Flask, jsonify, request

from flask_auth_middleware import login_required

app = Flask(__name__)


secret_key = "your_secret_key"


@app.route("/public")
def public_data():
    return jsonify({"message": "This is public data"})


@app.route("/protected")
@login_required(secret_key=secret_key, algorithms=["HS256"])
def protected_route(decoded_payload):
    # your logic with decoded payload
    return jsonify(
        {"message": "This is public data.", "decoded_payload": decoded_payload}
    )


if __name__ == "__main__":
    app.run(debug=True)

Run it

  • Run the server with:
$ flask run
    * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

Contributing

Feel free to contribute to this project.

License

This project is licensed under the terms of the MIT license.

Metadata

Release files for flask-auth-middleware 0.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for flask-auth-middleware 0.3.0
File Size Uploaded
flask_auth_middleware-0.3.0.tar.gz 5.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for flask-auth-middleware 0.3.0
File Interpreter ABI Platform
flask_auth_middleware-0.3.0-py3-none-any.whl Python 3 none any Details

Total release size: 10.6 kB

Release files / flask_auth_middleware-0.3.0.tar.gz

Download URL flask_auth_middleware-0.3.0.tar.gz
Size 5.1 kB
Tags Source
SHA-256 checksum
How to use checksums
5a50a5ef73810d2d9888ac05a6889fe8a6066edf4a280b98f7be96ca4595de18
BLAKE2b-256 checksum
How to use checksums
3f59453b175dfc08a8a4625d016ed556dd525f0e2e2a78f0552641e66d36a7b6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.4

Release files / flask_auth_middleware-0.3.0-py3-none-any.whl

Download URL flask_auth_middleware-0.3.0-py3-none-any.whl
Size 5.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
47e37cde795b0cf5ff6df39b168cf0b4dc955e472df356ed463ca1fdc45197dc
BLAKE2b-256 checksum
How to use checksums
18d2c952be5fa731adeddff9d759c6e9305cdfb124b93b329b6a06875a4ea36f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.4

Release history Release notifications | RSS feed

This release

0.3.0 This release

2 release files

0.2.0

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page