Skip to main content

A simple Django app to use keycloak over OIDC

Project description

Keycloak OIDC

Keycloak OIDC is a simple Django app that wraps the mozilla_django_oidc app and implements Keycloak authentication the way we use it at Datapunt.

It creates and updates users and sets their email, username and first- and lastname based on the info provided by keycloak, and manages group membership based on keycloak roles.

Quick start

  1. Install using pip

    pip install datapunt_keycloak_oidc
    
  2. Add "keycloak-oidc" to your INSTALLED_APPS (make sure to load after auth!):

    INSTALLED_APPS = [
        ...
        'django.contrib.auth',
        'keycloak_oidc',  # load after auth!
    ]
    
  3. Add the mozilla_django_oidc.SessionRefreshMiddleware to your MIDDLEWARE (middleware involving session and authentication must come first!):

    MIDDLEWARE = [
        ...
        'django.contrib.sessions.middleware.SessionMiddleware',
        'django.contrib.auth.middleware.AuthenticationMiddleware',
        'mozilla_django_oidc.middleware.SessionRefresh',
    ]
    
  4. Add the OIDCAuthenticationBackend to the AUTHENTICATION_BACKENDS:

    AUTHENTICATION_BACKENDS = [
        'keycloak_oidc.auth.OIDCAuthenticationBackend',
        ...
    ]
    
  5. Set the OIDC_RP_CLIENT_ID and OIDC_RP_CLIENT_SECRET in settings.py. Note that these should be kept secret. Therefore these should preferable be set in the OS ENV. Obtain these from the keycloak provider.

    OIDC_RP_CLIENT_ID = os.environ['OIDC_RP_CLIENT_ID']
    OIDC_RP_CLIENT_SECRET = os.environ['OIDC_RP_CLIENT_SECRET']
    

    Keycloak only talks to urls that are whitelisted. Therefore, make sure that the app url for production is added to keycloak. To make local development possible, also make sure localhost:8080 (or any other port) is added.

  6. Add the OIDC provider URLs to settings.py, and set the proper OS env. This default will fall back to the acceptance keycloak urls.

    OIDC_OP_AUTHORIZATION_ENDPOINT = os.getenv('OIDC_OP_AUTHORIZATION_ENDPOINT',
        'https://iam.amsterdam.nl/auth/realms/datapunt-acc/protocol/openid-connect/auth')
    OIDC_OP_TOKEN_ENDPOINT = os.getenv('OIDC_OP_TOKEN_ENDPOINT',
        'https://iam.amsterdam.nl/auth/realms/datapunt-acc/protocol/openid-connect/token')
    OIDC_OP_USER_ENDPOINT = os.getenv('OIDC_OP_USER_ENDPOINT',
        'https://iam.amsterdam.nl/auth/realms/datapunt-acc/protocol/openid-connect/userinfo')
    OIDC_OP_JWKS_ENDPOINT = os.getenv('OIDC_OP_JWKS_ENDPOINT',
        'https://iam.amsterdam.nl/auth/realms/datapunt-acc/protocol/openid-connect/certs')
    OIDC_OP_LOGOUT_ENDPOINT = os.getenv('OIDC_OP_LOGOUT_ENDPOINT',
        'https://iam.amsterdam.nl/auth/realms/datapunt-acc/protocol/openid-connect/logout')
    
  7. When using Django-rest-framework, add the mozilla_django_oidc OIDCAuthentication to the default authentication classes (and make sure the DRF SessionAuthentication has been added):

    REST_FRAMEWORK = dict(
        ...
        DEFAULT_AUTHENTICATION_CLASSES=(
            'mozilla_django_oidc.contrib.drf.OIDCAuthentication',
            'rest_framework.authentication.SessionAuthentication'
            ...
        )
    )
    
  8. When using Django-rest-framework, override the InAuthGroup permission class to implement role based access control:

    from keycloak_oidc.drf.permissions import InAuthGroup
    
    class InTestAuthGroup(InAuthGroup):
        """
        A permission to allow access if and only if a user is logged in,
        and is a member of the 'test' role inside keycloak.
        """
        allowed_group_names = ['test']
    
  9. Include the keycloak-oidc URLconf in your project urls.py:

    url(r'^oidc/', include('keycloak_oidc.urls')),
    
  10. IMPORTANT: Make sure to read through the Mozilla Django OIDC docs: https://mozilla-django-oidc.readthedocs.io/en/stable/installation.html

All settings that can be configured are documented there.

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

datapunt-keycloak-oidc-0.4.1.tar.gz (12.7 kB view details)

Uploaded Source

Built Distribution

datapunt_keycloak_oidc-0.4.1-py3-none-any.whl (16.4 kB view details)

Uploaded Python 3

File details

Details for the file datapunt-keycloak-oidc-0.4.1.tar.gz.

File metadata

  • Download URL: datapunt-keycloak-oidc-0.4.1.tar.gz
  • Upload date:
  • Size: 12.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.6.8

File hashes

Hashes for datapunt-keycloak-oidc-0.4.1.tar.gz
Algorithm Hash digest
SHA256 83729967568e5fabcf6cce538d2ff4babc33e3099bae0d372fb1351faaab68c2
MD5 fc325c3ca731b0f66d0cd5a7c4b32e4a
BLAKE2b-256 29566aec04f6d46b533207dbd06c2830a09c8773b8217388903c300ff6aae127

See more details on using hashes here.

File details

Details for the file datapunt_keycloak_oidc-0.4.1-py3-none-any.whl.

File metadata

  • Download URL: datapunt_keycloak_oidc-0.4.1-py3-none-any.whl
  • Upload date:
  • Size: 16.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.6.8

File hashes

Hashes for datapunt_keycloak_oidc-0.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d1b5330e3c64d9c7a698b79f1374897707bf1c26910da03022f23d4d82006709
MD5 758569fb9acd679519f63fd9da6bfbb9
BLAKE2b-256 e98717d3826c930381f3e2b892eadc05f389c1b698dd12a8e87d492399489b9d

See more details on using hashes here.

Supported by

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