Simple extension that provides Basic, Digest and Token HTTP authentication for Flask routes
Flask-HTTPAuth ==============
Simple extension that provides Basic, Digest and Token HTTP authentication for Flask routes.
Installation
The easiest way to install this is through pip.
pip install Flask-HTTPAuth
Basic authentication example
from flask import Flask
from flask_httpauth import HTTPBasicAuth
from werkzeug.security import generatepasswordhash, checkpasswordhash
app = Flask(name) auth = HTTPBasicAuth()
users = { "john": generatepasswordhash("hello"), "susan": generatepasswordhash("bye") }
@auth.verify_password def verify_password(username, password): if username in users and \ checkpasswordhash(users.get(username), password): return username
@app.route('/') @auth.login_required def index(): return "Hello, %s!" % auth.current_user()
if name == 'main': app.run()
Note: See the documentation for more complex examples that involve password hashing and custom verification callbacks.
Digest authentication example
from flask import Flask
from flask_httpauth import HTTPDigestAuth
app = Flask(name) app.config['SECRET_KEY'] = 'secret key here' auth = HTTPDigestAuth()
users = { "john": "hello", "susan": "bye" }
@auth.get_password def get_pw(username): if username in users: return users.get(username) return None
@app.route('/') @auth.login_required def index(): return "Hello, %s!" % auth.username()
if name == 'main': app.run()
Resources