This project has been archived by its maintainers, and is no longer receiving any updates.
Keycloak OIDC
Keycloak OIDC is a simple Django app that wraps the mozilla_django_oidc app and implements Keycloak authentication the way we use it at Datapunt.
It creates and updates users and sets their email, username and first- and lastname based on the info provided by keycloak, and manages group membership based on keycloak roles.
Quick start
-
Install using pip
pip install datapunt_keycloak_oidc
-
Add "keycloak-oidc" to your INSTALLED_APPS (make sure to load after auth!):
INSTALLED_APPS = [ ... 'django.contrib.auth', 'keycloak_oidc', # load after auth! ]
-
Add the mozilla_django_oidc.SessionRefreshMiddleware to your MIDDLEWARE (middleware involving session and authentication must come first!):
MIDDLEWARE = [ ... 'django.contrib.sessions.middleware.SessionMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'mozilla_django_oidc.middleware.SessionRefresh', ]
-
Add the OIDCAuthenticationBackend to the AUTHENTICATION_BACKENDS:
AUTHENTICATION_BACKENDS = [ 'keycloak_oidc.auth.OIDCAuthenticationBackend', ... ]
-
In settings.py import the default OIDC settings. These defaults will work in most situations.
# Import from keycloak_oidc settings and use the defaults from keycloak_oidc.default_settings import *
-
Set the OIDC_RP_CLIENT_ID and OIDC_RP_CLIENT_SECRET in settings.py. Note that these should be kept secret. Therefore these should preferable be set in the OS ENV. Obtain these from the keycloak provider.
OIDC_RP_CLIENT_ID = os.environ['OIDC_RP_CLIENT_ID'] OIDC_RP_CLIENT_SECRET = os.environ['OIDC_RP_CLIENT_SECRET']
Keycloak only talks to urls that are whitelisted. Therefore, make sure that the app url for production is added to keycloak. To make local development possible, also make sure localhost:8080 (or any other port) is added.
-
Add the OIDC provider URLs to settings.py, and set the proper OS env. This default will fall back to the acceptance keycloak urls.
OIDC_OP_AUTHORIZATION_ENDPOINT = os.getenv('OIDC_OP_AUTHORIZATION_ENDPOINT', 'https://iam.amsterdam.nl/auth/realms/datapunt-acc/protocol/openid-connect/auth') OIDC_OP_TOKEN_ENDPOINT = os.getenv('OIDC_OP_TOKEN_ENDPOINT', 'https://iam.amsterdam.nl/auth/realms/datapunt-acc/protocol/openid-connect/token') OIDC_OP_USER_ENDPOINT = os.getenv('OIDC_OP_USER_ENDPOINT', 'https://iam.amsterdam.nl/auth/realms/datapunt-acc/protocol/openid-connect/userinfo') OIDC_OP_JWKS_ENDPOINT = os.getenv('OIDC_OP_JWKS_ENDPOINT', 'https://iam.amsterdam.nl/auth/realms/datapunt-acc/protocol/openid-connect/certs') OIDC_OP_LOGOUT_ENDPOINT = os.getenv('OIDC_OP_LOGOUT_ENDPOINT', 'https://iam.amsterdam.nl/auth/realms/datapunt-acc/protocol/openid-connect/logout')
-
When using Django-rest-framework, add the mozilla_django_oidc OIDCAuthentication to the default authentication classes (and make sure the DRF SessionAuthentication has been added):
REST_FRAMEWORK = dict( ... DEFAULT_AUTHENTICATION_CLASSES=( 'mozilla_django_oidc.contrib.drf.OIDCAuthentication', 'rest_framework.authentication.SessionAuthentication' ... ) )
-
When using Django-rest-framework, override the InAuthGroup permission class to implement role based access control:
from keycloak_oidc.drf.permissions import InAuthGroup class InTestAuthGroup(InAuthGroup): """ A permission to allow access if and only if a user is logged in, and is a member of the 'test' role inside keycloak. """ allowed_group_names = ['test']
-
Include the keycloak-oidc URLconf in your project urls.py:
url(r'^oidc/', include('keycloak_oidc.urls')),
-
IMPORTANT: Make sure to read through the Mozilla Django OIDC docs: https://mozilla-django-oidc.readthedocs.io/en/stable/installation.html
All settings that can be configured are documented there.
Metadata
Release files for datapunt-keycloak-oidc 0.5.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| datapunt-keycloak-oidc-0.5.1.tar.gz | 13.0 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| datapunt_keycloak_oidc-0.5.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 28.4 kB
Release files / datapunt-keycloak-oidc-0.5.1.tar.gz
| Download URL | datapunt-keycloak-oidc-0.5.1.tar.gz |
|---|---|
| Size | 13.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
0c90357c4747441335ec7b3dd4573906e26d33861e22d78c0b7f8db993706432
|
|
BLAKE2b-256 checksum How to use checksums |
81f5308f55eddbf22088f5f8ecc93553ddd1e3f7a2a6629b1edc6955425c843f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.1 importlib_metadata/3.10.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.2
|
Release files / datapunt_keycloak_oidc-0.5.1-py3-none-any.whl
| Download URL | datapunt_keycloak_oidc-0.5.1-py3-none-any.whl |
|---|---|
| Size | 15.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
4a0d1f84985f61edc1a13276e98edbf83498e51462f7e9be2cf6dc130738985e
|
|
BLAKE2b-256 checksum How to use checksums |
bccf620fd46c7e73cb18b2dee903d604b8b5867a14fa9689d50134db4267356c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.1 importlib_metadata/3.10.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.2
|
Security for datapunt-keycloak-oidc 0.5.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.