Middleware to allow authorization using Keycloak and Django
Project description
[WIP] Django Keycloak Authorization
Middleware to allow authorization using Keycloak and Django for DRF and Graphene based projects. This package can only be used for projects started from scratch since they override the users management.
Installation
-
Add
django_keycloak
to the DjangoINSTALLED_APPS
-
Add
django_keycloak.middleware.KeycloakMiddleware
to the Django `MIDDLEWARE -
Change Django
AUTHENTICATION_BACKENDS
to:AUTHENTICATION_BACKENDS = ('django_keycloak.backends.KeycloakAuthenticationBackend',)
-
Add the following configuration to Django settings and replace the values by your own values:
KEYCLOAK_CONFIG = { 'SERVER_URL': '<PUBLIC_SERVER_URL>', 'INTERNAL_URL': <INTERNAL_SERVER_URL>'', 'REALM': '<REALM_NAME>', 'CLIENT_ID': '<CLIENT_ID>', 'CLIENT_SECRET_KEY': '<CLIENT_SECRET_KEY>', 'CLIENT_ADMIN_ROLE': '<CLIENT_ADMIN_ROLE>', 'REALM_ADMIN_ROLE': '<REALM_ADMIN_ROLE>', 'EXEMPT_URIS': [], # URIS to be ignored by the package 'GRAPHQL_ENDPOINT': 'graphql/' # Default graphQL endpoint }
-
Override the Django user model on settings:
AUTH_USER_MODEL = "django_keycloak.KeycloakUserAutoId"
-
If using graphene add the
GRAPHQL_ENDPOINT
to settings andKeycloakGrapheneMiddleware
to the grapheneMIDDLEWARE
. -
Configure Django Rest Framework authentication classes with
django_keycloak.authentication.KeycloakAuthentication
:REST_FRAMEWORK = { 'DEFAULT_AUTHENTICATION_CLASSES': [ 'django_keycloak.authentication.KeycloakAuthentication' ], 'DEFAULT_RENDERER_CLASSES': [ 'rest_framework.renderers.JSONRenderer', ], 'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.LimitOffsetPagination', 'PAGE_SIZE': 100, # Default to 20 'PAGINATE_BY_PARAM': 'page_size', # Allow client to override, using `?page_size=xxx`. 'MAX_PAGINATE_BY': 100, # Maximum limit allowed when using `?page_size=xxx`. 'TEST_REQUEST_DEFAULT_FORMAT': 'json' }
DRY Permissions
The permissions must be set like in other projects. You must set the permissions configuration for each model. Example:
@staticmethod
@authenticated_users
def has_read_permission(request):
roles = request.remote_user.get('client_roles')
return True if 'ADMIN' in roles else False
Keycloak users synchronization
The management command sync_keycloak_users
must be ran periodically. In
order to remove from the local users the ones that are no longer available at
keycloak. This command can be called using the task named sync_users_with_keycloak
,
using celery. Fot that you just need to:
-
Add the task to the
CELERY_BEAT_SCHEDULE
ìns Django settings:CELERY_BEAT_SCHEDULE = { 'sync_users_with_keycloak': { 'task': 'django_keycloak.tasks.sync_users_with_keycloak', 'schedule': timedelta(hours=24), 'options': {'queue': 'sync_users'} }, }
-
Add the
sync_users
queue to the docker-compose celery service:command: celery worker -A citibrain_base -B -E -l info -Q backup,celery,sync_users --autoscale=4,1
Attention: This task is only responsible to delete users from local storage. The creation of new users, that are on keycloak, is done when they try to login.
Notes
Support for celery 5: from version 0.7.4 on we should use celery 5 for the user sync. This implies running celery with celery -A app worker ... instead of celery worker -A app ...
Project details
Release history Release notifications | RSS feed
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_uw_keycloak-1.1.tar.gz
.
File metadata
- Download URL: django_uw_keycloak-1.1.tar.gz
- Upload date:
- Size: 17.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.0 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.11
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 62433bc3d885ef822dc92e13422833cf0b2ff1298d136cfc7788cf2bf973734d |
|
MD5 | 0d1af8f63836f74a2d3a1c7baf2ade31 |
|
BLAKE2b-256 | 062317ba48c269fde6595363ca334c6926f0097d85ad27f70550086974ed030e |
File details
Details for the file django_uw_keycloak-1.1-py3-none-any.whl
.
File metadata
- Download URL: django_uw_keycloak-1.1-py3-none-any.whl
- Upload date:
- Size: 23.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.0 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.11
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 253472d2e6efdfc4fc8ca450cda2d21d22e5e113b6a19b6cac1f88decae3dd17 |
|
MD5 | b80f55f31edadfe80772fc693eacb979 |
|
BLAKE2b-256 | f6786789f5f18aa619d896b69458a4e3c2a49e8938c97d89f33468e19818703a |