Skip to main content

django-cognito-saml

Library to implement django authentication using cognito (via pyjwt).

Assumptions made:

  • Using authorization code flow. Implicit grant is insecure as the access token is transferred over in the request parameters without encryption.

Settings

The following settings should be set in your settings file against a COGNITO_CONFIG dictionary.

Setting Description
ENDPOINT Either the hosted domain or custom domain for your cognito app
CLIENT_ID CLIENT_ID of your application in your user pool
CLIENT_SECRET CLIENT_SECRET of your application in your user pool
JWKS_URI The JWKS URI of your user pool. Used to verify the JWT.
REDIRECT_URI OPTIONAL It is possible to share one cognito app with multiple websites via a proxy.
RESPONSE_HOOK OPTIONAL Post authentication hook to modify the response (perhaps to add headers). Specify it as a django import_string.
REQUIRED_GROUPS OPTIONAL Specify when using SuperUserBackend to restrict the ability to login to saml users with custom:groups containing all `REQUIRED_GROUPS.

Installation

  1. Add the above settings to your settings.
COGNITO_CONFIG = {
    "ENDPOINT": "",
    "CLIENT_ID": "",
    "CLIENT_SECRET": "",
    "JWKS_URI": "",
    "REDIRECT_URI": "",
    "RESPONSE_HOOK": ""
    "REQUIRED_GROUPS": []
}
  1. Define your authentication backend. Subclass off django_cognito_saml.backends.CognitoUserBackend.

    Define the username field of your user by customizing the authenticate method. If you wish to add additional fields to the user or modify the user's permissions, override the configure_user method. The configure_user method has access to self.cognito_jwt which contains the decoded jwt token with the cognito saml assertions.

    Set create_unknown_user = False if you want to disable automatic creation of users.

class CustomCognitoBackend(CognitoUserBackend):
    # Change this to False if you do not want to create a remote user.
    create_unknown_user = True

    def authenticate(  # type: ignore[override]
        self, request: HttpRequest, cognito_jwt: dict[str, Any], **kwargs: Any
    ) -> Optional[AbstractBaseUser]:
        # Customizing the username field used to create the user
        remote_user = cognito_jwt["username"]
        user = super().authenticate(request, remote_user=remote_user, **kwargs)
        return user

    def configure_user(  # type: ignore[override]
        self, request: HttpRequest, user: AbstractBaseUser, created: bool = True
    ) -> AbstractBaseUser:
        # Configuring the user post login
        if created:
            user.name = self.cognito_jwt["name"]
            user.save()
        return user
  1. Add SuperUserBackend to your authentication backends.
AUTHENTICATION_BACKENDS = (
    ...
    "django_cognito_saml.backends.SuperUserBackend",
    ...
)
  1. Add the cognito saml urls to your urls.py
urls = [
    ...
    path("/", include("django_cognito_saml.urls")),
]

Metadata

Release files for django-cognito-saml 0.1.9

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-cognito-saml 0.1.9
File Size Uploaded
django_cognito_saml-0.1.9.tar.gz 43.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-cognito-saml 0.1.9
File Interpreter ABI Platform
django_cognito_saml-0.1.9-py3-none-any.whl Python 3 none any Details

Total release size: 54.3 kB

Release files / django_cognito_saml-0.1.9.tar.gz

Download URL django_cognito_saml-0.1.9.tar.gz
Size 43.1 kB
Tags Source
SHA-256 checksum
How to use checksums
f5f7153641b79076c11e4bb07b0e9aab37d419012a6c2a4e84e023ca73f7f2b3
BLAKE2b-256 checksum
How to use checksums
a78b60ef7d5e45cd624e6fa25855c377e3a7d3d021594bbd2d8e256271681a8d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.2.1 CPython/3.9.25 Linux/6.14.0-1017-azure

Release files / django_cognito_saml-0.1.9-py3-none-any.whl

Download URL django_cognito_saml-0.1.9-py3-none-any.whl
Size 11.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ff8586c001361bf2d486b911b5d770ece2515e3fae58ca846418915c8a41d159
BLAKE2b-256 checksum
How to use checksums
f8c8c617eb9b2c8f317a57b3bf41e6bbba3ce77d6f73c5ea855614bc1db5e662
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.2.1 CPython/3.9.25 Linux/6.14.0-1017-azure

Release history Release notifications | RSS feed

This release

0.1.9 This release

2 release files

0.1.8

2 release files

0.1.7

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page