Skip to main content

Basic, Digest and Bearer token authentication for Sanic routes

Project description


Build Status

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.


The easiest way to install this is through pip.

pip install Sanic-HTTPAuth

Basic authentication example

import hashlib
from sanic import Sanic
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"),

def verify_password(username, password):
    if username in users:
        return users.get(username) == hash_password(app_salt, password)
    return False

def index(request):
    return "Hello, %s!" % auth.username()

if __name__ == "__main__":

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
from sanic_httpauth import HTTPDigestAuth
from sanic_session import Session

app = Sanic(__name__)
app.config["SECRET_KEY"] = "secret key here"
auth = HTTPDigestAuth()

users = {"john": "hello", "susan": "bye"}

def get_pw(username):
    if username in users:
        return users.get(username)
    return None

def index(request):
    return "Hello, %s!" % auth.username()

if __name__ == "__main__":


Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for Sanic-HTTPAuth, version 0.1.2
Filename, size File type Python version Upload date Hashes
Filename, size Sanic-HTTPAuth-0.1.2.tar.gz (35.5 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page