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
- Add the above settings to your settings.
COGNITO_CONFIG = {
"ENDPOINT": "",
"CLIENT_ID": "",
"CLIENT_SECRET": "",
"JWKS_URI": "",
"REDIRECT_URI": "",
"RESPONSE_HOOK": ""
"REQUIRED_GROUPS": []
}
-
Define your authentication backend. Subclass off
django_cognito_saml.backends.CognitoUserBackend
.Define the
username
field of your user by customizing theauthenticate
method. If you wish to add additional fields to the user or modify the user's permissions, override theconfigure_user
method. Theconfigure_user
method has access toself.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
- Add
SuperUserBackend
to your authentication backends.
AUTHENTICATION_BACKENDS = (
...
"django_cognito_saml.backends.SuperUserBackend",
...
)
- 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
Built Distribution
File details
Details for the file django_cognito_saml-0.1.7.tar.gz
.
File metadata
- Download URL: django_cognito_saml-0.1.7.tar.gz
- Upload date:
- Size: 8.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.4 CPython/3.9.20 Linux/6.5.0-1025-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a236984f98750bee5df56302b57d34846ffcf66dcb2c11b8cd9b0763c0ac27bb |
|
MD5 | 0af54ca440e280032e717557f58e7af7 |
|
BLAKE2b-256 | 62dd2e7fd0f962938dba4b6570e0cda753564edcf0d8ca582c7dd4515a31ed79 |
File details
Details for the file django_cognito_saml-0.1.7-py3-none-any.whl
.
File metadata
- Download URL: django_cognito_saml-0.1.7-py3-none-any.whl
- Upload date:
- Size: 11.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.4 CPython/3.9.20 Linux/6.5.0-1025-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d198bd2e6256c37888c430b0b28fddd188fb8de5102db9e6e1dcdda5572915d8 |
|
MD5 | 329c24bddda9efbe537a547a7fab7f7f |
|
BLAKE2b-256 | 7c700a82dc6733766687a63d599e65a43045867cfaf6d84461a23e332bf54be0 |