Sanic-HTTPAuth
This a fork of Flask-HTTPAuth for Sanic. It is a simple extension that provides Basic and Digest HTTP authentication for Sanic routes.
Still a work in progress, contributions are welcome.
Installation
The easiest way to install this is through pip.
pip install Sanic-HTTPAuth
Basic authentication example
import hashlib
from sanic import Sanic, response
from sanic_httpauth import HTTPBasicAuth
app = Sanic(__name__)
auth = HTTPBasicAuth()
def hash_password(salt, password):
salted = password + salt
return hashlib.sha512(salted.encode("utf8")).hexdigest()
app_salt = "APP_SECRET - don't do this in production"
users = {
"john": hash_password(app_salt, "hello"),
"susan": hash_password(app_salt, "bye"),
}
@auth.verify_password
def verify_password(username, password):
if username in users:
return users.get(username) == hash_password(app_salt, password)
return False
@app.route("/")
@auth.login_required
def index(request):
return response.text(f"Hello, {auth.username(request)}!")
if __name__ == "__main__":
app.run()
Note: See the Flask-HTTPAuth documentation for more complex examples that involve password hashing and custom verification callbacks.
Digest authentication example
from sanic import Sanic, response
from sanic_httpauth import HTTPDigestAuth
from sanic_session import Session
app = Sanic(__name__)
app.config["SECRET_KEY"] = "secret key here"
auth = HTTPDigestAuth()
Session(app)
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(request):
return response.text(f"Hello, {auth.username(request)}!")
if __name__ == "__main__":
app.run()
Resources
Metadata
Release files for Sanic-HTTPAuth 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| Sanic-HTTPAuth-0.2.0.tar.gz | 36.9 kB | Without provenance | Details for Sanic-HTTPAuth-0.2.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| Sanic_HTTPAuth-0.2.0-py3-none-any.whl | Python 3 | none | any | Details for Sanic_HTTPAuth-0.2.0-py3-none-any.whl |
Total release size: 46.5 kB
Release files / Sanic-HTTPAuth-0.2.0.tar.gz
| Download URL | Sanic-HTTPAuth-0.2.0.tar.gz |
|---|---|
| Size | 36.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a4f9fb15165a470d53ba1bd42acb8b0d9f579acb1c0af2c9752d1ed38e41cb89
|
|
BLAKE2b-256 checksum How to use checksums |
128938a2116129df5afb926c8eb4625fd12fe66cdc1133a989397c9fd3d73e66
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.2.0 pkginfo/1.6.0 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.8.5
|
Release files / Sanic_HTTPAuth-0.2.0-py3-none-any.whl
| Download URL | Sanic_HTTPAuth-0.2.0-py3-none-any.whl |
|---|---|
| Size | 9.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
25c7d63ac49e18d0c60baabb02c62f0036f02ec4a54fa5cff9b0582fd920d67f
|
|
BLAKE2b-256 checksum How to use checksums |
83a4f0c41404c87c03e75e5740067b551fdc6072c4581d689c6450d2c5cfa562
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.2.0 pkginfo/1.6.0 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.8.5
|
Security for Sanic-HTTPAuth 0.2.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.