Skip to main content

Authentication backends and helpers for Starlette-based ASGI apps and frameworks

Project description


travis pypi python black

Authentication backends and helpers for Starlette-based ASGI apps and frameworks.

Note: documentation is in progress. In the meantime, feel free to read the source code.


  • Database-agnostic.
  • User model-agnostic.
  • Password hashing and hash migration support.
  • Built-in support for common authentication flows, including Basic and Token authentication.
  • Support for multiple authentication backends.
  • Easy integration with orm.



pip install starlette-auth-toolkit


import typing

from starlette.applications import Starlette
from starlette.authentication import requires
from starlette.middleware.authentication import AuthenticationMiddleware
from starlette.responses import JSONResponse, PlainTextResponse

from starlette_auth_toolkit.base.backends import BaseBasicAuth
from starlette_auth_toolkit.cryptography import PBKDF2Hasher

# Password hasher
hasher = PBKDF2Hasher()

# Example user model
class User(typing.NamedTuple):
    username: str
    password: str

# Fake user storage
    "alice": User(username="alice", password=hasher.make_sync("alicepwd")),
    "bob": User(username="bob", password=hasher.make_sync("bobpwd")),

# Authentication backend
class BasicAuth(BaseBasicAuth):
    async def find_user(self, username: str):
        return USERS.get(username)

    async def verify_password(self, user: User, password: str):
        return await hasher.verify(password, user.password)

# Application

app = Starlette()

    on_error=lambda _, exc: PlainTextResponse(str(exc), status_code=401),

async def protected(request):
    return JSONResponse({"message": f"Hello, {request.user.username}!"})

Save this file as Then, assuming you have uvicorn installed, run $ uvicorn app:app and make requests:

  • Anonymous request:
curl -i http://localhost:8000/protected
HTTP/1.1 403 Forbidden
date: Tue, 23 Jul 2019 20:44:52 GMT
server: uvicorn
content-length: 9
content-type: text/plain; charset=utf-8

  • Authenticated request:
curl -i -u alice:alicepwd http://localhost:8000/protected
HTTP/1.1 200 OK
date: Tue, 23 Jul 2019 20:45:24 GMT
server: uvicorn
content-length: 27
content-type: application/json

{"message":"Hello, alice!"}

For a real-world example, see here.


Like Starlette, starlette-auth-toolkit does not have any hard dependencies, but you can optionally install the following:

  • passlib - Required if you want to use password hashers.

Base backends

Base backends implement an authentication flow, but the exact implementation of credentials verification is left up to you. This means you can choose to perform a database query, use environment variables or private files, etc.

These backends grant a set of scopes when authentication succeeds.

Although base backends are user model agnostic, we recommend you implement the interface specified by starlette.authentication.BaseUser (see also Starlette authentication).

They are available at starlette_auth_toolkit.base.backends.


Base implementation of the Basic authentication scheme.

Request header format

Authorization: Basic {credentials}

where {credentials} refers to the base64 encoding of {username}:{password}.


# myapp/
from starlette.authentication import SimpleUser  # or a custom user model
from starlette_auth_toolkit.base.backends import BaseBasicAuth

class BasicAuth(BaseBasicAuth):
    async def verify(self, username: str, password: str):
        # In practice, request the database to find the user associated
        # to `username`, and validate that its password hash matches the
        # given password.
        if (username, password) != ("bob", "s3kr3t"):
            return None
        return SimpleUser(username)

Abstract methods

  • async .verify(self, username: str, password: str) -> Optional[BaseUser]

    If username and password are valid, return the corresponding user. Otherwise, return None.


  • authenticated


Base implementation of token authentication, a simplified version of the Bearer authentication scheme.

Request header format

Authorization: Token {token}


# myapp/
from starlette.authentication import SimpleUser  # or a custom user model
from starlette_auth_toolkit.base.backends import BaseTokenAuth

class TokenAuth(BaseTokenAuth):
    async def verify(self, token: str):
        # In practice, request the database to find the token object
        # associated to `token`, and return its associated user.
        if token != "abcd":
            return None
        return SimpleUser("bob")

Abstract methods

  • async .verify(self, token: str) -> Optional[BaseUser]

    If token refers to a valid token, return the corresponding user. Otherwise, return None.


  • authenticated


Authentication backends listed here are ready-to-use implementations and are available in the backends module, unless specified otherwise.


A ready-to-use implementation of BaseBasicAuth using an orm user model.

Note: orm must be installed to use this backend.


from starlette.applications import Starlette
from starlette_auth_toolkit.contrib.orm import ModelBasicAuth
from starlette_auth_toolkit.cryptography import PBKDF2Hasher

from myproject.models import User  # DIY

hasher = PBKDF2Hasher()

app = Starlette()
    backend=ModelBasicAuth(User, hasher=hasher)


  • model (orm.Model or () -> orm.Model): the user model (or a callable for lazy loading).
  • hasher (BaseHasher): a password hasher — the same one used to hash user passwords.
  • password_field (str, optional): field where password hashes are stored on user objects. Defaults to "password".


  • authenticated


This backend allows you to support multiple authentication methods in your application. MultiAuth attempts authenticating using the given backends in order until one succeeds (or all fail).

Note: if any backend fails with an AuthenticationError (e.g. because some credentials were provided but they were invalid), MultiAuth will propagate the exception and no further attempts will be made — even if a later backend would have succeeded.


from starlette_auth_toolkit.backends import MultiAuth

from myproject.auth import TokenAuth, BasicAuth  # TODO

# Allow to authenticate using either a token or username/password credentials.
backend = MultiAuth([TokenAuth(), BasicAuth()])


  • backends (List[AuthBackend]): a list of authentication backends, which determines which authentication methods clients can use to authenticate.


  • authenticated

Password hashers

This package provides password hashing utilities built on top of PassLib.


  • Asynchronous: await .make() / await .verify() (hashing and verification occurs in the threadpool)
import asyncio
from starlette_auth_toolkit.cryptography import PBKDF2Hasher

async def main():
    # Instanciate a hasher:
    hasher = PBKDF2Hasher()

    # Hash a password:
    pwd = await hasher.make("hello")

    # Verify a password against a known hash:
    assert await hasher.verify("hello", pwd)

# Python 3.7+
  • Blocking: .make_sync() / .verify_sync()
from starlette_auth_toolkit.cryptography import PBKDF2Hasher

# Instanciate a hasher:
hasher = PBKDF2Hasher()

# Hash a password
pwd = hasher.make_sync("hello")

# Verify a password against a known hash:
assert hasher.verify_sync("hello", pwd)

Hash migration (Advanced)

If you need to change the hash algorithm (say from PBKDF2 to Argon2), you will typically want to keep support for existing hashes, but rehash them with the new algorithm as soon as possible.

MultiHasher was designed to solve this problem:

from starlette_auth_toolkit.cryptography import Argon2Hasher, PBKDF2Hasher, MultiHasher

hasher = MultiHasher([Argon2Hasher(), PBKDF2Hasher()])

The above hasher will use Argon2 when hashing new passwords, but will be able to verify hashes created using either Argon2 or PBKDF2.

To detect whether a hash needs rehashing, use .needs_update():

valid = await hasher.verify(pwd, pwd_hash)

if hasher.needs_update(pwd_hash):
    new_hash = await hasher.make(pwd)
    # TODO: store new hash

# ...

Note: calling .needs_update() at anytime other than just after calling .verify() will raise a RuntimeError.

Available hashers

Name Requires PassLib algorithm
PBKDF2Hasher pbkdf2_sha256
CryptHasher sha256_crypt
BCryptHasher bcrypt bcrypt
Argon2Hasher argon2-cffi argon2
MultiHasher N/A

For advanced use cases, use Hasher and pass one of the algorithms listed in passlib.hash:

from starlette_auth_toolkit.cryptography import Hasher

hasher = Hasher(algorithm="pbkdf2_sha512")

Authenticating in views

If you need to authenticate a user inside a view, i.e. exchange a pair of username and password for the actual user, use your BasicAuth backend:

auth = MyBasicAuth()

async def logs_user_in(request):
    data = await request.json()
    username = data["username"]
    password = data["password"]
    user = await auth.verify(username, password)
    # ...


Want to contribute? Awesome! Be sure to read our Contributing guidelines.





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

starlette-auth-toolkit-0.5.0.tar.gz (13.3 kB view hashes)

Uploaded source

Built Distribution

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