Middleware to allow authorization using Keycloak and Django
Project description
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 to Django settings:
KEYCLOAK_CONFIG = { 'SERVER_URL': 'https://keycloak.staging.ubiwhere.com', 'INTERNAL_URL': 'https://keycloak.staging.ubiwhere.com', 'REALM': 'django', 'CLIENT_ID': 'api', 'CLIENT_SECRET_KEY': '0414b857-8430-4fbb-b86a-62bc398f37ea', 'CLIENT_ADMIN_ROLE': 'admin', 'REALM_ADMIN_ROLE': 'admin', 'EXEMPT_URIS': [], 'GRAPHQL_ENDPOINT': 'graphql/' }
-
Override the Django user model on settings:
AUTH_USER_MODEL = "django_keycloak.KeycloakUser"
-
If using graphene add the
GRAPHQL_ENDPOINT
to settings and ``KeycloakGrapheneMiddlewareto the graphene
MIDDLEWARE`
Django Admin
The Django superuser that can be used for the Django Admin login, must
created with the normal management command python manage.py createsuperuser
. But first you must create this user on keycloak and set a
client admin role and realm admin role like the CLIENT_ADMIN_ROLE
and
REALM_ADMIN_ROLE
that were added on settings previously.
Django Rest Framework
In the Django settings the the Rest Framework settings can't have any Authorization values (used in other projects). Example:
```json
# Rest framework settings
REST_FRAMEWORK = {
'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 the the permissions configuration for each model. Example:
```json
@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.
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-0.7.7.tar.gz
.
File metadata
- Download URL: django_uw_keycloak-0.7.7.tar.gz
- Upload date:
- Size: 13.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.8.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1e770a4341a71fb1ac7a5a1f94807332442ab0c7a80b98839bd164853cb9188d |
|
MD5 | 3b88dcca51a7fac252e119c52c45be9d |
|
BLAKE2b-256 | f50d1088305b4659f23aa3ea81a4ede33dd903b891c85e224c094cbfefce8c4a |
File details
Details for the file django_uw_keycloak-0.7.7-py3-none-any.whl
.
File metadata
- Download URL: django_uw_keycloak-0.7.7-py3-none-any.whl
- Upload date:
- Size: 17.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.8.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 65598c1786fd471d474f569f7cf4fa6ccd6978a76f721a33fab7ad429af148dc |
|
MD5 | e4faec096a5a75dd8e23ecd5fae148df |
|
BLAKE2b-256 | 6242f2646d0ba1ade8b29321c2fbef59c79421173fc63b5ce2f29b7ace637460 |