requirements.txt
# https://pypi.org/project/Flask-HTTPAuth/
Flask-HTTPAuth==3.3.0
main.py
from flask_httpauth import HTTPBasicAuthauth = HTTPBasicAuth()users = { "username": "password"}@auth.get_passworddef get_password(username): return users.get(username)@auth.login_requireddef test_basic_auth(request): return f"{auth.username()}, you may pass!"
or using a more versatile verification method
from werkzeug.security import generate_password_hash, check_password_hashusers = { "username": generate_password_hash("password"), "another": generate_password_hash("secret")}@auth.verify_passworddef verify_password(username, password): if username in users: return check_password_hash(users.get(username), password) return False
Test
curl -u username:password [URL]/test_basic_auth
References: