Flask JWT Persistency
This extension was developed to add additional features to JWT like revoke a single token or all tokens issued to a specific user. These features are implemented by persisting the tokens in a database, so JWT looses its stateless property.
Installing
Install and update using pip:
$ pip install flask-jwt-persistency
Full working example (also available at examples folder)
from flask import Flask, request, jsonify
from flask_sqlalchemy import SQLAlchemy
from flask_jwt_extended import JWTManager, create_access_token, jwt_required, get_jti, get_jwt, get_jwt_identity
from flask_jwt_persistency import JWTPersistency
app = Flask(__name__)
app.config["JWT_SECRET_KEY"] = "super-secret" # Change this!
app.config["JWTP_DATABASE_URL"] = "sqlite:///jwtptokens.db" # DB to store the tokens
db = SQLAlchemy(app)
jwt = JWTManager(app)
jwtp = JWTPersistency(app, jwt, db)
@app.route("/auth/login", methods=["POST"])
def login():
username = request.json.get("username", None)
password = request.json.get("password", None)
if username != "test" or password != "test":
return jsonify({"msg": "Bad username or password"}), 401
access_token = create_access_token(identity=username)
jti = get_jti(encoded_token=access_token)
# Persist the jti in the database
jwtp.new_token(jti, username)
return jsonify(access_token=access_token)
# Access a protected route
@app.route("/protected", methods=["GET"])
@jwt_required()
def protected():
return jsonify({"msg": "You got access to the protected route"})
# Revoke token used in the request
@app.route("/auth/logout", methods=["DELETE"])
@jwt_required()
def logout():
"""Revoke access token"""
jti = get_jwt()['jti']
jwtp.revoke_token(jti)
# Revoke all tokens that have been issued to the user that is making the request
@app.route("/auth/logout-from-all-devices", methods=["DELETE"])
@jwt_required()
def logout_from_all_devices():
"""Revoke all tokens"""
username = get_jwt_identity()
jwtp.revoke_all_tokens(username)
if __name__ == "__main__":
app.run(debug=True)
Metadata
Release files for flask-jwt-persistency 0.0.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| flask-jwt-persistency-0.0.3.tar.gz | 4.3 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| flask_jwt_persistency-0.0.3-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 8.8 kB
Release files / flask-jwt-persistency-0.0.3.tar.gz
| Download URL | flask-jwt-persistency-0.0.3.tar.gz |
|---|---|
| Size | 4.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
3e00ea3927b564dc6c9bf6b60d72f0b9f1078e6abdf369316f50fb2d04b4b8c9
|
|
BLAKE2b-256 checksum How to use checksums |
a8c0bf88ddb5312d4319ee9089c10a5d94521ae1f1c14fb82c731220945ed7d7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.2 importlib_metadata/4.6.4 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.1 CPython/3.9.6
|
Release files / flask_jwt_persistency-0.0.3-py3-none-any.whl
| Download URL | flask_jwt_persistency-0.0.3-py3-none-any.whl |
|---|---|
| Size | 4.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
a153100e398ec47b7f47d7b878acc23a20fce1cf23529a120f945ee10e533d89
|
|
BLAKE2b-256 checksum How to use checksums |
d069cd162ba56783cd6efae19116c5f391536d0a0a5a9446f824444d92118be8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.2 importlib_metadata/4.6.4 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.1 CPython/3.9.6
|