Skip to main content

No project description provided

Project description

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")),
]

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

django_cognito_saml-0.1.8.tar.gz (40.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_cognito_saml-0.1.8-py3-none-any.whl (11.3 kB view details)

Uploaded Python 3

File details

Details for the file django_cognito_saml-0.1.8.tar.gz.

File metadata

  • Download URL: django_cognito_saml-0.1.8.tar.gz
  • Upload date:
  • Size: 40.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.1 CPython/3.9.24 Linux/6.11.0-1018-azure

File hashes

Hashes for django_cognito_saml-0.1.8.tar.gz
Algorithm Hash digest
SHA256 c3b6972cda7495d2ecb8ace6661943364945db6d8a02cca9ef0730f14d17d3c9
MD5 7b323001fbeb7beb571f95b0f0fefaf6
BLAKE2b-256 65a05b31281545f7fc002d5d52d62417fea98f842f9ca2132f28e4ad673b9ffe

See more details on using hashes here.

File details

Details for the file django_cognito_saml-0.1.8-py3-none-any.whl.

File metadata

  • Download URL: django_cognito_saml-0.1.8-py3-none-any.whl
  • Upload date:
  • Size: 11.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.1 CPython/3.9.24 Linux/6.11.0-1018-azure

File hashes

Hashes for django_cognito_saml-0.1.8-py3-none-any.whl
Algorithm Hash digest
SHA256 4a22165a6ff6be3a8699a85eb8888be1a6a94a3c44c819ece929b02660f6fb8d
MD5 13ffdd35209d2c4feceb70ce7bc62baa
BLAKE2b-256 046845a015e6dd648bf5cdf2370e41b0701a25d3beb297bf08a1556f6669b192

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page