Authentication backends and helpers for Starlette-based apps and frameworks
Project description
starlette-auth-toolkit
Authentication backends and helpers for Starlette-based apps and frameworks.
Note: documentation is in progress. In the meantime, feel free to read the source code.
Features
- Database-agnostic.
- User model-agnostic.
- Built-in password hashing powered by PassLib.
- Hash migration support.
- Built-in support for common authentication flows, including Basic and Bearer authentication.
- Support for multiple authentication backends.
- Easy integration with
orm
.
Contents
Installation
pip install starlette-auth-toolkit
Quickstart
import typing
from starlette.applications import Starlette
from starlette.authentication import requires
from starlette.middleware.authentication import AuthenticationMiddleware
from starlette.responses import JSONResponse
from starlette_auth_toolkit.base.backends import BasicAuthBackend
from starlette_auth_toolkit.base.helpers import BaseAuthenticate
from starlette_auth_toolkit.cryptography import PBKDF2Hasher
# User model
class User(typing.NamedTuple):
username: str
password: str
# Password hasher
hasher = PBKDF2Hasher()
# Fake storage
USERS = {
"alice": User(username="alice", password=hasher.make_sync("alicepwd")),
"bob": User(username="bob", password=hasher.make_sync("bobpwd")),
}
# Authentication helper
class Authenticate(BaseAuthenticate):
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)
authenticate = Authenticate()
# Authentication backend
class BasicAuth(BasicAuthBackend):
async def verify(
self, username: str, password: str
) -> typing.Optional[User]:
return await authenticate(username, password)
# Application
app = Starlette()
app.add_middleware(AuthenticationMiddleware, backend=BasicAuth())
@app.route("/")
@requires("authenticated")
async def home(request):
"""Example protected route."""
return JSONResponse({"message": f"Hello, {request.user.username}!"})
Save this file as app.py
. Then, assuming you have uvicorn installed, run $ uvicorn app:app
and make requests:
- Anonymous request:
curl http://localhost:8000 -i
HTTP/1.1 401 Unauthorized
date: Sun, 21 Jul 2019 17:54:00 GMT
server: uvicorn
content-length: 52
content-type: text/plain; charset=utf-8
Could not authenticate with the provided credentials
- Authenticated request:
curl -u alice:alicepwd http://localhost:8000
HTTP/1.1 200 OK
date: Sun, 21 Jul 2019 17:54:28 GMT
server: uvicorn
content-length: 27
content-type: application/json
{"message":"Hello, alice!"}
For a real-world example, see here.
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
.
BasicAuthBackend
Implementation of the Basic authentication scheme.
Request header format
Authorization: Basic {credentials}
where {credentials}
refers to the base64 encoding of {username}:{password}
.
Example
# myapp/auth.py
from starlette.authentication import SimpleUser # or a custom user model
from starlette_auth_toolkit.base import backends
class BasicAuthBackend(backends.BasicAuthBackend):
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
andpassword
are valid, return the corresponding user. Otherwise, returnNone
.
Scopes
authenticated
BearerAuthBackend
Implementation of the Bearer authentication scheme, also known as Token authentication.
Request header format
Authorization: Bearer {token}
Example
# myapp/auth.py
from starlette.authentication import SimpleUser # or a custom user model
from starlette_auth_toolkit.base import backends
class BearerAuthBackend(backends.BearerAuthBackend):
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, returnNone
.
Scopes
authenticated
Password hashers
This package provides password hashing utilities built on top of PassLib.
Usage
- 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+
asyncio.run(main())
- 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 aRuntimeError
.
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")
Authentication helpers
Web applications often need to exchange user credentials (username and password) against the actual user.
Such an exchange is typically implemented as an authenticate
utility function:
user = await authenticate(username, password)
Helpers listed here make it easier to build a secure authenticate
utility function, which you can then reuse when building your authentication backends.
base.helpers.BaseAuthenticate
Base class for authentication helpers.
Abstract methods
-
async
.find_user(self, username: str) -> Optional[BaseUser]
Return the user associated to
username
, orNone
if none exist. -
async
.verify_password(self, user: BaseUser, password: str) -> bool
Given a user, check that the given
password
is valid. For example, compare the givenpassword
against the user's password hash.
Methods
-
async
.__call__(self, username: str, password: str) -> Optional[BaseUser]
Authenticate a user using the following algorithm:
- Find a
user
using.find_user()
- Verify the password using
.verify_password()
- Return
user
if it exists and the password is valid, andNone
otherwise.
- Find a
contrib.orm.ModelAuthenticate
A ready-to-use implementation of BaseAuthenticate
using an orm
user model.
Note: orm
must be installed to use this helper.
Example
from starlette.applications import Starlette
from starlette_auth_toolkit.contrib.orm import ModelAuthenticate
from starlette_auth_toolkit.cryptography import PBKDF2Hasher
from myproject.models import User # DIY
hasher = PBKDF2Hasher()
authenticate = ModelAuthenticate(User, hasher=hasher)
app = Starlette()
@app.route("/")
async def home(request):
data = await request.json()
username, password = data["username"], data["password"]
user: User = await authenticate(username, password)
# ...
Parameters
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"
.
Contributing
Want to contribute? Awesome! Be sure to read our Contributing guidelines.
Changelog
See CHANGELOG.md.
License
MIT
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 starlette-auth-toolkit-0.4.0.tar.gz
.
File metadata
- Download URL: starlette-auth-toolkit-0.4.0.tar.gz
- Upload date:
- Size: 13.3 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
Algorithm | Hash digest | |
---|---|---|
SHA256 | 31e75776ba49e1caa808458554ba362f65d8619b73567c9be83ca8bebb8416c6 |
|
MD5 | 33e1cfd90e3b669de3cefd504e10d34b |
|
BLAKE2b-256 | 734f27108516a782439f0870526f4b178d5046eb53d5ac523ad34401bf335314 |
File details
Details for the file starlette_auth_toolkit-0.4.0-py3-none-any.whl
.
File metadata
- Download URL: starlette_auth_toolkit-0.4.0-py3-none-any.whl
- Upload date:
- Size: 11.2 kB
- Tags: Python 3
- 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
Algorithm | Hash digest | |
---|---|---|
SHA256 | c06966d337a05f8f27049c898d6e1da05c3f57a874f23820c817919808f527db |
|
MD5 | a5f0c22847e66eabf4071102beacd034 |
|
BLAKE2b-256 | 8f798b5388b5bd0871227a2da5982e246dccf2caa2534754680bca136183facd |