Skip to main content

Django app to map Keycloak roles and groups to Django user permissions and groups using OIDC

Project description

django-keyclock-oidc

This project depends on mozilla-django-oidc and map keycloak roles and groups to django user permissions and groups.

Features

  • Automatic mapping of Keycloak roles and groups to Django user permissions and groups
  • Django admin login integration with Keycloak
  • OIDC authentication with Keycloak

Installation

  1. You can install the package via your python package manager, example:
pip install mozilla-django-oidc
pip install django-keycloak-oidc
# or
poetry add mozilla-django-oidc
poetry add django-keycloak-oidc
# or
uv add mozilla-django-oidc
uv add django-keycloak-oidc
  1. Add django_keycloak_oidc and mozilla_django_oidc to your INSTALLED_APPS in settings.py:
INSTALLED_APPS = [
    "django_keycloak_oidc",  # top of admin app
    "django.contrib.admin",
    "django.contrib.auth",
    "mozilla_django_oidc",  # bottom of auth app
    ...
]
  1. Add the authentication backend to your AUTHENTICATION_BACKENDS in settings.py:
AUTHENTICATION_BACKENDS = [
    "django_keycloak_oidc.auth.KeyCloakAuthenticationBackend",  # here
    "django.contrib.auth.backends.ModelBackend",  # django default (need it too)
    ...,  # and other
]
  1. Add mozilla_django_oidc urls to your urls.py:
urlpatterns = [
    ...,
    path("oidc/", include("mozilla_django_oidc.urls")),
    ...,
]

Important Note: If you changed your admin root path, make sure that the oidc/ and admin/ paths are in same root.

for example:

urlpatterns = [
    ...,
    path(
        "root/",  # your root (if you did it)
        include(
            [
                ...,
                path("admin/", admin.site.urls),
                path("oidc/", include("mozilla_django_oidc.urls")),
                ...,
            ]
        )
    ),
    ...,
]
  1. Add the settings_context to your context_processors in settings.py:
TEMPLATES = [
    {
        ...,
        "OPTIONS": {
            "context_processors": [
                ...,
                "django_keycloak_oidc.context_processor.settings_context",  # here
            ],
        },
    },
]
  1. Run migrations (Done):
python manage.py migrate

Configuration

You can see configuration of the original project here.

My sample configuration(settings.py) is as below:

OIDC_RP_CLIENT_ID = "<client-id>"
OIDC_RP_CLIENT_SECRET = "<client-secret>"
OIDC_RP_SIGN_ALGO = "RS256"

OIDC_VERIFY_SSL = False

OIDC_OP_AUTHORIZATION_ENDPOINT = "http://<keycloak-host>/realms/<realm>/protocol/openid-connect/auth"
OIDC_OP_TOKEN_ENDPOINT = "http://<keycloak-host>/realms/<realm>/protocol/openid-connect/token"
OIDC_OP_USER_ENDPOINT = "http://<keycloak-host>/realms/<realm>/protocol/openid-connect/userinfo"
OIDC_OP_JWKS_ENDPOINT = "http://<keycloak-host>/realms/<realm>/protocol/openid-connect/certs"

LOGIN_URL = "/oidc/authenticate/"
LOGIN_REDIRECT_URL = "/leasing/admin/"
LOGIN_REDIRECT_URL_FAILURE = "/leasing/admin/"
LOGOUT_REDIRECT_URL = "/leasing/admin/login/"

# (django-keycloak-oidc) settings for customizing the login button in django admin login page(make sure you did step 5 in installation):
KEYCLOAK_DJANGO_ADMIN_LOGIN_VISIBLE = True
KEYCLOAK_DJANGO_ADMIN_LOGIN_DIRECTION = "ltr"
KEYCLOAK_DJANGO_ADMIN_LOGIN_TEXT = "Login with"
KEYCLOAK_DJANGO_ADMIN_LOGIN_LOGO = "https://karnameh.com/assets/logos/karnameh-logo.svg"

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_oidc-0.1.0.tar.gz (7.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_oidc-0.1.0-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

Details for the file django_keycloak_oidc-0.1.0.tar.gz.

File metadata

  • Download URL: django_keycloak_oidc-0.1.0.tar.gz
  • Upload date:
  • Size: 7.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for django_keycloak_oidc-0.1.0.tar.gz
Algorithm Hash digest
SHA256 f25d2ff1e6eaac5b8b3efd8fd4e387cf3bd74b8516ffd78ec084a259a4b0587f
MD5 04f3d3cf3fed705d97c1817436fbb5d0
BLAKE2b-256 1b356249a0028d64469df05febc811035a450c20071d839edfcd0fe549d19cb7

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_keycloak_oidc-0.1.0.tar.gz:

Publisher: publish.yml on karnameh-tech/django-keycloak-oidc

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file django_keycloak_oidc-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_keycloak_oidc-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a82d4780c8ec55f8a43c7a07adbd19bd54949385a7668ed13a63a7419252c49f
MD5 76f6c13659f0160fafeeb04c0805a416
BLAKE2b-256 0c5d33ed40832f47ffa73f89432133cad23195afffa516801385199120a1125d

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_keycloak_oidc-0.1.0-py3-none-any.whl:

Publisher: publish.yml on karnameh-tech/django-keycloak-oidc

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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