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.

Installation

  1. Add the above settings to your settings.
COGNITO_CONFIG = {
    "ENDPOINT": "",
    "CLIENT_ID": "",
    "CLIENT_SECRET": "",
    "JWKS_URI": "",
    "REDIRECT_URI": "",
    "RESPONSE_HOOK": ""
}
  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 CustomCognitoBackend to your authentication backends.
AUTHENTICATION_BACKENDS = (
    ...
    "apps.backends.CustomCognitoBackend",
    ...
)
  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.2.tar.gz (7.3 kB view details)

Uploaded Source

Built Distribution

django_cognito_saml-0.1.2-py3-none-any.whl (9.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: django_cognito_saml-0.1.2.tar.gz
  • Upload date:
  • Size: 7.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.4.0 CPython/3.9.16 Linux/5.15.0-1034-azure

File hashes

Hashes for django_cognito_saml-0.1.2.tar.gz
Algorithm Hash digest
SHA256 98cabdc9ac78c02440783a061c16916b5bf60ea9a460289a3f5369cac133f38b
MD5 fe5b336ba89eee00e6ef661c2625d1ce
BLAKE2b-256 f50f24c82cadf56514c5ecd72d51891d2c40668d555910c43926684da8edc548

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for django_cognito_saml-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 5d21245ba04d3e3eb64f07ff5993830fb504404638947b822302acd4a027d6e9
MD5 448bc6dcaecbb8cf9d134b7640634355
BLAKE2b-256 f3ce9fbe664ede9ae1005cf05ef88bda86ea966a7440582dd71dd8340072b82e

See more details on using hashes here.

Supported by

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