Basic authentication support via htpasswd files in flask applications
Project description
Flask extension for providing basic digest and token authentication via apache htpasswd files. So largely it fits between Flask-Security which has additional dependencies and Flask-BasicAuth which only allows you to have one user (and also puts the plain text password into the configuration).
Sample usage is to first create an htpasswd file with the apache tool:
htpasswd -c /path/to/.htpasswd my_username
Additional users can be added, or have their passwords changed, by running:
htpasswd /path/to/.htpasswd new_user
htpasswd /path/to/.htpasswd user_I_want_to_change_passwords_for
Then you just need to setup and configure your flask application, with something like:
import flask
from flask_htpasswd import HtPasswdAuth
app = flask.Flask(__name__)
app.config['FLASK_HTPASSWD_PATH'] = '/path/to/.htpasswd'
app.config['FLASK_SECRET'] = 'Hey Hey Kids, secure me!'
htpasswd = HtPasswdAuth(app)
@app.route('/')
@htpasswd.required
def index(user):
return 'Hello {user}'.format(user=user)
app.run(debug=True)
And that view should now prompt for a username and password (and accept tokens).
If you would like to protect all of your views, that is easy too, just add a little config. By setting app.config['FLASK_AUTH_ALL']=True before initializing the extension, an @app.before_request is added that will require auth for all pages, and it will add the user as flask.g.user.
One last small feature, is that you can also set the authentication realm. The default is ‘Login Required’, but it can be set with app.config['FLASK_AUTH_REALM'] before initialization.
Using Tokens
Tokens are based on the username and password, and thus invalid whenever the user’s password is changed. To get a user password, you can serve it out to the user with something like
import flask
from flask_htpasswd import HtPasswdAuth
app = flask.Flask(__name__)
app.config['FLASK_HTPASSWD_PATH'] = '/path/to/.htpasswd'
app.config['FLASK_SECRET'] = 'Hey Hey Kids, secure me!'
htpasswd = HtPasswdAuth(app)
@app.route('/')
@htpasswd.required
def index(user):
return flask.jsonify({'token': htpasswd.generate_token(user)})
app.run(debug=True)
It can then be used by the user by adding it to the header of their requests, something like:
import requests
requests.get('http://localhost:5000/', headers={'Authorization': 'token <token>'})
Release Notes
0.5.0
Switch from itsdangerous to pyjwt
Renamed FLASK_SECRET into FLASK_SECRET
0.4.0
Updated for newer language and Flask versions
Corrected deprecated passlib API call
0.3.0
Added function to reload user database
Added user to flask.g with FLASK_AUTH_ALL=True
0.2.0
Python 3 compatibility
Acknowledgements
This is largely based on a combination of:
Links
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file flask-htpasswd-0.5.0.tar.gz
.
File metadata
- Download URL: flask-htpasswd-0.5.0.tar.gz
- Upload date:
- Size: 7.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.42.1 CPython/3.6.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5d903ce4a37526a14d6109e86dd16d1e54ba4090c1396dd70294da6233c159a5 |
|
MD5 | 25098ababc41f88a7347f78c551ee283 |
|
BLAKE2b-256 | 1223a48ba432293903a161c674a19c791f84f9b98e6c7310d0190fb92555c655 |
File details
Details for the file flask_htpasswd-0.5.0-py3-none-any.whl
.
File metadata
- Download URL: flask_htpasswd-0.5.0-py3-none-any.whl
- Upload date:
- Size: 6.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.42.1 CPython/3.6.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 089d269acab128d955a0d88937e30498e22a6cb9f668f400fcf4d2e115bef654 |
|
MD5 | 6636f8aeee6a9c8a269a7f64c753a966 |
|
BLAKE2b-256 | 28e645a04f47e0ecaaa3314a39f483d1280150380a29bce9011efbcf4a669527 |