Flask-BasicAuth is a Flask extension that provides a straightforward way to add HTTP basic access authentication to specific views or an entire Flask application. The current version is 0.2.0, released in June 2013, indicating a very slow release cadence and a largely unmaintained status.
pip install Flask-BasicAuthVerified import paths — ran on the pinned version, not inferred.
This quickstart initializes a Flask application with Flask-BasicAuth. It demonstrates protecting a single route (`/secret`) using the `@basic_auth.required` decorator. Credentials are loaded from environment variables for security, defaulting to 'admin' and 'secret'. The example also notes how to protect the entire application using `BASIC_AUTH_FORCE = True`.
Always deploy Flask applications using Flask-BasicAuth behind an HTTPS/TLS-enabled server (e.g., Nginx, Apache) in production environments.
Configure your reverse proxy to pass the `Authorization` header to the backend. For Apache/mod_wsgi, add `WSGIPassAuthorization On` to your configuration. For Nginx, ensure `proxy_set_header Authorization $http_authorization;` is set.
If experiencing issues with `BASIC_AUTH_FORCE`, consider explicitly decorating each protected view with `@basic_auth.required` or explore alternative Flask authentication extensions like `Flask-HTTPAuth`.
For production applications requiring secure user management and password storage, consider using more robust Flask extensions like `Flask-Login` combined with `Flask-Bcrypt`, or `Flask-HTTPAuth` which supports secure password hashing. Flask-BasicAuth is best suited for simple, low-security scenarios or protecting staging environments.
For new projects or applications requiring ongoing maintenance and modern features, consider using actively developed alternatives such as `Flask-HTTPAuth` (for various HTTP auth schemes including Basic with hashed passwords) or `Flask-Login` (for session-based user management).
Install the package using pip: `pip install Flask-BasicAuth`
Update the import statement to `from flask_basicauth import BasicAuth`
Ensure `app.config['BASIC_AUTH_USERNAME']` and `app.config['BASIC_AUTH_PASSWORD']` are correctly set in your Flask application, and that the client is sending the correct credentials. If using a proxy (like Nginx or Apache), ensure it's configured to pass authorization headers (e.g., `proxy_pass_header Authorization;` in Nginx or `WSGIPassAuthorization On` in Apache/mod_wsgi).
Ensure the `BasicAuth` object is correctly instantiated and associated with your Flask app. Either pass the app directly: `basic_auth = BasicAuth(app)`, or initialize it separately: `basic_auth = BasicAuth(); basic_auth.init_app(app)`.