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.6.0.tar.gz
(230.2 kB
view details)
Built Distribution
File details
Details for the file Flask-HTTPAuth-4.6.0.tar.gz
.
File metadata
- Download URL: Flask-HTTPAuth-4.6.0.tar.gz
- Upload date:
- Size: 230.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.0 CPython/3.9.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2076cf86e84c6aa442ee03344bff751eae133d35a1a48931e6f99f147161b55b |
|
MD5 | 2935240e00e2f0fb37e23fd70a81a9b1 |
|
BLAKE2b-256 | 0b922e0ea6a6193dbd36925ee296ede67bc861f8219a722a33d8ec7227c7c419 |
Provenance
File details
Details for the file Flask_HTTPAuth-4.6.0-py3-none-any.whl
.
File metadata
- Download URL: Flask_HTTPAuth-4.6.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.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 29191747bd6a6e81980fd9c415ec1279612b06ee4a7cad8463856f2ed571ec77 |
|
MD5 | 92ed3dc8613212f35bf75c5ad256a6f6 |
|
BLAKE2b-256 | 17ebec42029ee772d1506a2cb8a493622fffa7235524d8858f2ce8c2da73ffef |