Skip to main content

Basic, Digest and Bearer token authentication for Sanic routes

Project description

Sanic-HTTPAuth

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.

Installation

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"),
}


@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 "Hello, %s!" % auth.username()


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
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 "Hello, %s!" % auth.username()


if __name__ == "__main__":
    app.run()

Resources

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

Sanic-HTTPAuth-0.1.1.tar.gz (35.5 kB view details)

Uploaded Source

File details

Details for the file Sanic-HTTPAuth-0.1.1.tar.gz.

File metadata

  • Download URL: Sanic-HTTPAuth-0.1.1.tar.gz
  • Upload date:
  • Size: 35.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.1

File hashes

Hashes for Sanic-HTTPAuth-0.1.1.tar.gz
Algorithm Hash digest
SHA256 2f55a07cabc6ccb229fa5b6c0ae4e0830d779b955d26bbd03d1d72350a120815
MD5 364833d40b66a270037be3ea03bd1a6d
BLAKE2b-256 a0640a73fa5e45800be70742802bb2a476ca2412fb826e4e5f36c7ab93e53d89

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page