Skip to main content

Keycloak admin and REST auth.

Project description

Django Keycloak

A simple remote authentication module for use with Django and a Keycloak auth server.

Loosely based on Peter Slump's unmaintained https://github.com/Peter-Slump/django-keycloak. This updated version works with Keycloak v21.0, Django 5.0.0 and python-keycloak 4.0.0.

Capabilities

It supports:

  • OpenID authentication for logging into Django's admin interface
  • Authentication for REST requests, using django-rest-framework

TODO: Support resource & role permissions

Quickstart

  1. Install via pip:
pip install django-keycloak-admin
  1. Configure settings: In your application's settings, add the following lines:
# your-project/settings.py
INSTALLED_APPS = [
    ...
    'django_keycloak'
]
# For admin site authentication
AUTHENTICATION_BACKENDS = [
    ...
    'django_keycloak.backends.KeycloakAuthorizationCodeBackend',
]
KEYCLOAK_CLIENTS = {
    "DEFAULT": {
        "URL": ...,
        "REALM": "example-realm",
        "CLIENT_ID": "example-backend-client",
        "CLIENT_SECRET": "*************************",
    },
    # If you're using django's REST framework
    "API": {
        "URL": "http://localhost:8001",
        "REALM": "example-realm",
        "CLIENT_ID": "example-frontend-client",
        "CLIENT_SECRET": None,  # Typically a public client
    },
}

REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': [
        'django_keycloak.authentication.KeycloakDRFAuthentication',
    ]
}
  1. Include URLs Open your app's urls.py file, ad add the following:
from django.contrib import admin
from django.urls import path, include
from django_keycloak.views import admin_login

urlpatterns = [
    # This will override the default django login page
    path("admin/", include("django_keycloak.urls")),
    path("admin/", admin.site.urls),
    ...
]
  1. Migrate changes:
python manage.py migrate

Roles and Permissions

Roles assigned on keycloak are represented as Django groups, so a user with an 'example' role on the keycloak server will be added to an 'example' group in the Django app. An administrator can configure permissions for the group in the Django admin site.

There is also a special 'superuser' role defined by KEYCLOAK_ADMIN_ROLE in settings (and defaulting to 'admin'). If users have this role, they are classified a Django superuser, with all permissions automatically assigned.

Examples

An example application demonstrating this setup is included in the examples folder.

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_keycloak_admin-0.0.4.tar.gz (10.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_keycloak_admin-0.0.4-py3-none-any.whl (11.6 kB view details)

Uploaded Python 3

File details

Details for the file django_keycloak_admin-0.0.4.tar.gz.

File metadata

  • Download URL: django_keycloak_admin-0.0.4.tar.gz
  • Upload date:
  • Size: 10.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.12

File hashes

Hashes for django_keycloak_admin-0.0.4.tar.gz
Algorithm Hash digest
SHA256 74f5a2952d5d9b93e7b6b9d47ff50eb670e5352991c03d43c998cb3409d84e55
MD5 57ba82b19489b6a2e218a8576949d072
BLAKE2b-256 98b4db260dfdedd307d45077f769ab4378a5e6864a788c06149c9093036f2755

See more details on using hashes here.

File details

Details for the file django_keycloak_admin-0.0.4-py3-none-any.whl.

File metadata

File hashes

Hashes for django_keycloak_admin-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 ba0b23618fc6cd054e85f3fcc231ac5c1b6ca3811562ddaec8b932c8ffcd10a4
MD5 44c4f206a0568091d52c4942f218b00b
BLAKE2b-256 335fa10694233a9ff3b2326f68193024edfda44150f9c5489ade9110227cfe68

See more details on using hashes here.

Supported by

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