Tesseral for Django
Tesseral’s Django SDK lets you add authentication to your Python backend using Django.
The Tesseral Django SDK is open-source and available on GitHub.
When you’re using the Tesseral Django SDK, you’ll need to use a client-side Tesseral SDK, such as the Tesseral React SDK, on your frontend code.
Getting Started
Install the Tesseral Django SDK by running:
Then, in your Django settings, add tesseral_django.middleware.AuthMiddleware
to your MIDDLEWARE
setting by editing your settings.py
file:
Replace publishable_key_...
with your project’s Publishable Key. You can find
it in the API Keys
Settings of the
Tesseral Console.
The middleware handles parsing authentication tokens but does not enforce
authentication by default. To require authentication on specific views, use the
@require_auth
decorator:
Views decorated with @require_auth
will return a 401 Unauthorized
response
for inauthentic requests.
Accessing details about the authenticated request
The Tesseral SDK makes information about the current authenticated request available through simple helper functions.
The Tesseral Django SDK stores request-local state on the Django request object. The helper methods described in this section will throw an error if used outside the context of a Django request.
Getting the current Organization
To find out what Organization the request is for, use organization_id(request)
:
This is the most common identifier you’ll use in a B2B multitenant application.
Getting the request’s authenticated credentials
If your architecture forwards requests between internal services that need to
re-authenticate, use credentials(request)
:
Do not log or expose this value. You usually don’t need to use this unless you’re building internal service-to-service calls.
Getting details about the current User
To access more information about the authenticated User, use
access_token_claims(request)
:
access_token_claims
returns an
AccessTokenClaims
,
which contains details about the current Session ID, User, and Organization.
If the request if from an API Key, then
access_token_claims
will throw a NotAnAccessTokenError
.
We recommend that you mostly use organization_id(request)
in the vast majority of
your code; that is almost always the correct piece of information for most B2B
SaaS code should pay attention to. For more details, see B2B
Multitenancy.