HTTP authentication for Flask routes
Project description
Flask-HTTPAuth
Simple extension that provides Basic and Digest 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 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 "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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Flask-HTTPAuth-4.7.0.tar.gz
(230.3 kB
view details)
Built Distribution
File details
Details for the file Flask-HTTPAuth-4.7.0.tar.gz
.
File metadata
- Download URL: Flask-HTTPAuth-4.7.0.tar.gz
- Upload date:
- Size: 230.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.0 CPython/3.9.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f7199e7bad20d5b68b3f0b62bddfca7637c55087e9d02f605ae26e0de479fd94 |
|
MD5 | f63f10deff8310318c38f8e0c3626380 |
|
BLAKE2b-256 | de281681359136718332e97e763c0eec2e9da03e801f9349216f2cf1fb0a6ab4 |
File details
Details for the file Flask_HTTPAuth-4.7.0-py3-none-any.whl
.
File metadata
- Download URL: Flask_HTTPAuth-4.7.0-py3-none-any.whl
- Upload date:
- Size: 6.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.0 CPython/3.9.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a237e4c8ec1d339298a0eb88e5af2ca36117949b621631649462800e57e1ba37 |
|
MD5 | fe87e8db2714bb34bf26a65e44aaa34c |
|
BLAKE2b-256 | 09a4e9b3b4bbf6456578e0b457b46f1e70bf8b65e4b59cf830fa56938a587347 |