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',
]
AUTH_USER_MODEL = "django_keycloak.KeycloakUser"  # Optional
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

Syncing permissions with Keycloak isn't currently supported, but django_keycloak is able to recognise a special 'superuser' role defined by KEYCLOAK_ADMIN_ROLE defined in settings (and defaulting to 'admin'). If users have this role, they are classified a django superuser. Without this role, they may not be able to access the admin site.

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.2.tar.gz (10.8 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.2-py3-none-any.whl (11.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: django_keycloak_admin-0.0.2.tar.gz
  • Upload date:
  • Size: 10.8 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.2.tar.gz
Algorithm Hash digest
SHA256 fedac033935ca88557768a30061c02b7cc2e7bcf2e37fdbeccbae874a366e9a0
MD5 7d70a807bf70344888ca9f74425c3470
BLAKE2b-256 556df9772f5d75affd9cddde38c13be65d412ebfc9d18340ddbd5669a0f8d1dd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for django_keycloak_admin-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 8b16302b8e337622f3b37a45c65f587dad098f879c4e11e75e00ff0c33a5fb4a
MD5 3c4a5df50abd5d9a7c238ff55826a78b
BLAKE2b-256 78faa8a96f67a8576ab1e8f6c84e278cdf38c5b288f83ede1ecacb9df7730704

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