A middleware to secure your flask application routes
Project description
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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Close
Hashes for flask_auth_middleware-0.3.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5a50a5ef73810d2d9888ac05a6889fe8a6066edf4a280b98f7be96ca4595de18 |
|
MD5 | 670cab3bff1bcd38a36ebeb0027cded2 |
|
BLAKE2b-256 | 3f59453b175dfc08a8a4625d016ed556dd525f0e2e2a78f0552641e66d36a7b6 |
Close
Hashes for flask_auth_middleware-0.3.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 47e37cde795b0cf5ff6df39b168cf0b4dc955e472df356ed463ca1fdc45197dc |
|
MD5 | 3c00d428edeb3aeb26fcfbe99e853d35 |
|
BLAKE2b-256 | 18d2c952be5fa731adeddff9d759c6e9305cdfb124b93b329b6a06875a4ea36f |