Skip to main content

JSON Web Token authenticator backend for Starlette's authentication system.

Project description


Basic JWT authentication integration for Starlette. All it does is validate and sign JWTs and set their contents on the request's auth and user attributes. See the Starlette documentation for more details.


Create a JWTBackend instance and install Starlette's AuthenticationMiddleware using it. Then in your login and logout routes, wrap your responses in backend.set_login_cookie(repsonse, sub) and backend.logout(response). For a general guide to Starlette's authentication system see the here.

By default, the user is a SimpleUser with username set to the sub value of the JWT, and the scopes are empty. To change this behaviour, you can subclass JWTBackend and override the get_user method to, for example, get the user in the database.


from starlette.applications import Starlette
from starlette.middleware.authentication import AuthenticationMiddleware
from starlette.responses import PlainTextResponse
from starlette_jwt import JWTBackend

app = Starlette()
backend = JWTBackend(...)
app.add_middleware(AuthenticationMiddleware, backend=backend)

async def login():
    # do login
    return backend.set_login_cookie(PlainTextResponse("ok"), "username")

async def logout():
    # do logout
    return backend.logout(PlainTextResponse("ok"))


Starlette JWT requires Starlette, PyJWT and Python 3.8 or higher (why?).


Starlette JWT is licensed under the AGPL 3.0.

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 star-jwt, version 0.4.0
Filename, size File type Python version Upload date Hashes
Filename, size star_jwt-0.4.0-py3-none-any.whl (16.9 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size star_jwt-0.4.0.tar.gz (4.8 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page