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": ...,
        "REALM": "example-realm",
        "CLIENT_ID": "example-frontend-client",
        "CLIENT_SECRET": None,  # Typically a public client
    },
    "ADMIN": {
        "USERNAME": "admin",
        "PASSWORD": ...,
    }
}

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.5.tar.gz (11.1 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.5-py3-none-any.whl (12.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: django_keycloak_admin-0.0.5.tar.gz
  • Upload date:
  • Size: 11.1 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.5.tar.gz
Algorithm Hash digest
SHA256 a49031ce371886e2a01dfa8b2e78fb99e594e8c0cbcf42e399e5b82c7019b50e
MD5 e583d90f7935a349ba8b25b7ab8d38df
BLAKE2b-256 0ddba7e8f349e5222a06ca84fdcec28935ad9ddf85e00cf48571cfeed8e37f3c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for django_keycloak_admin-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 86e2fb0c17949134d67be22896d400ab7a1d2381ee09f05ce16c801ba3346a34
MD5 d57224f5c43d3afc0591b4921e872fee
BLAKE2b-256 d46d7f2c6aae6115b042443452cd4233befe6b5e242687983aaa528e163fb708

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