Simple extension that provides Basic and Digest HTTP authentication for Flask routes
npx @tessl/cli install tessl/pypi-flask-httpauth@4.8.0A simple extension that provides Basic and Digest HTTP authentication for Flask routes. Flask-HTTPAuth enables developers to easily secure Flask endpoints with various authentication methods including HTTP Basic, HTTP Digest, token-based authentication, and flexible multi-authentication schemes.
pip install Flask-HTTPAuthfrom flask_httpauth import HTTPBasicAuth, HTTPDigestAuth, HTTPTokenAuth, MultiAuthfrom flask import Flask
from flask_httpauth import HTTPBasicAuth
from werkzeug.security import generate_password_hash, check_password_hash
app = Flask(__name__)
auth = HTTPBasicAuth()
users = {
"john": generate_password_hash("hello"),
"susan": generate_password_hash("bye")
}
@auth.verify_password
def verify_password(username, password):
if username in users and check_password_hash(users.get(username), password):
return username
@app.route('/')
@auth.login_required
def index():
return f"Hello, {auth.current_user()}"
if __name__ == '__main__':
app.run()Flask-HTTPAuth follows a class-based architecture with inheritance:
All authentication classes provide decorator-based callback registration, automatic error handling, and seamless Flask integration through request/response processing.
HTTP Basic authentication with secure password verification, supporting both plain password comparison and hashed password storage with custom verification callbacks.
class HTTPBasicAuth:
def __init__(self, scheme=None, realm=None): ...
def verify_password(self, f): ...
def hash_password(self, f): ...
def login_required(self, f=None, role=None, optional=None): ...
def current_user(self): ...HTTP Digest authentication providing enhanced security over Basic auth through challenge-response mechanisms, supporting MD5 and MD5-Sess algorithms with customizable nonce and opaque value generation.
class HTTPDigestAuth:
def __init__(self, scheme=None, realm=None, use_ha1_pw=False, qop='auth', algorithm='MD5'): ...
def generate_nonce(self, f): ...
def verify_nonce(self, f): ...
def generate_opaque(self, f): ...
def verify_opaque(self, f): ...
def login_required(self, f=None, role=None, optional=None): ...Token-based authentication supporting Bearer tokens and custom authentication schemes, with flexible token verification and custom header support for API authentication patterns.
class HTTPTokenAuth:
def __init__(self, scheme='Bearer', realm=None, header=None): ...
def verify_token(self, f): ...
def login_required(self, f=None, role=None, optional=None): ...
def current_user(self): ...Combines multiple authentication methods, automatically selecting the appropriate authentication handler based on request headers, enabling flexible endpoint protection with fallback authentication schemes.
class MultiAuth:
def __init__(self, main_auth, *args): ...
def login_required(self, f=None, role=None, optional=None): ...
def current_user(self): ...Role-based access control system that works across all authentication methods, supporting simple roles, multiple roles per user, and complex role hierarchies with flexible authorization callbacks.
# Available on HTTPAuth base class and all subclasses
def get_user_roles(self, f): ...
def login_required(self, f=None, role=None, optional=None): ...# Flask imports used throughout
from flask import request, make_response, session, g
from werkzeug.datastructures import Authorization
# Base authentication class inherited by all auth types
class HTTPAuth:
def __init__(self, scheme=None, realm=None, header=None): ...
def get_password(self, f): ...
def get_user_roles(self, f): ...
def error_handler(self, f): ...
def login_required(self, f=None, role=None, optional=None): ...
def username(self): ...
def current_user(self): ...