Skip to main content

A Keycloak Middleware

Project description

##KCMiddleware

A keycloak middleware


Quick start

  1. Add the middleware to your MIDDLEWARE setting below the XFrameOptionsMiddleware:
    MIDDLEWARE = [
        ...
        'KCMiddleware.KC_middleware.KeycloakMiddleware',
    ]
  1. Add the following to your setting in order to obtain the needed variables:
    import os
    
    BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    CONFIG_DIR = os.path.join(os.path.dirname(__file__), os.pardir)

    KEYCLOAK_REALM_KEY = f"""-----BEGIN PUBLIC KEY-----
    {os.environ.get('KEYCLOAK_PUBLIC_KEY')}
    -----END PUBLIC KEY-----"""

    KEYCLOAK_CONFIG = {
        'KEYCLOAK_REALM': os.environ.get('KEYCLOAK_REALM_NAME'),
        'KEYCLOAK_CLIENT_AUTHENTICATION_ID': os.environ.get('KEYCLOAK_CLIENT_AUTHENTICATION_ID'),
        'KEYCLOAK_CLIENT_AUTHENTICATION_SECRET': os.environ.get('KEYCLOAK_CLIENT_AUTHENTICATION_SECRET'),
        'KEYCLOAK_CLIENT_AUTHENTICATION_INTERNAL_ID': os.environ.get('KEYCLOAK_CLIENT_AUTHENTICATION_INTERNAL_ID'),
        'KEYCLOAK_DEFAULT_ACCESS': 'ALLOW',  # DENY or ALLOW
        'KEYCLOAK_AUTHORIZATION_CONFIG': os.path.join(BASE_DIR, 'authorization-config-keycloak.json'),
        'KEYCLOAK_METHOD_VALIDATE_TOKEN': 'DECODE',
        'KEYCLOAK_SERVER_URL': os.environ.get('KEYCLOAK_URL', default='http://localhost') + 'auth/',
        'KEYCLOAK_REALM_KEY': KEYCLOAK_REALM_KEY,
        'KEYCLOAK_ADMIN_USERNAME': os.environ.get('KEYCLOAK_ADMIN_USERNAME'),
        'KEYCLOAK_ADMIN_PASSWORD': os.environ.get('KEYCLOAK_ADMIN_PASSWORD'),
        'KEYCLOAK_APPLICATION_ID': env.str('KEYCLOAK_APPLICATION_ID'),
        'KEYCLOAK_INTROSPECT_OFFLINE': os.environ.get('KEYCLOAK_INTROSPECT_OFFLINE')
    }
  1. Add your Keycloak Authorization config to the root of your project and rename it to authorization-config-keycloak.json

    You can find the configuration by logging into your keycloak application -> Clients -> "Your client name" -> Authorization -> Export Setting

  2. To be able to authenticate a view, you have to add the keycloak_scopes into your view before all the functions:

    class SomeRandomViewSet(viewsets.ModelViewSet):

        keycloak_scopes = {
            'GET': 'somerandom_scope:view',
            'POST': 'somerandom_scope:create',
            'PUT': 'somerandom_scope:update',
            'DELETE': 'somerandom_scope:delete',
        }

        def retrieve(self, request, *args, **kwargs):
            ...
  1. If you need to check for client scopes (or service scopes), use the following:
    keycloak_scopes = {
        'GET': {'scope': 'somerandom_scope:view',
                'service_scope': 'somerandom_client_scope:view'},
        'POST': {...},
        'PUT': {...},
        'DELETE': {...},
    }

Project details


Release history Release notifications | RSS feed

This version

0.5

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

KCMiddleware-0.5.tar.gz (9.1 kB view details)

Uploaded Source

File details

Details for the file KCMiddleware-0.5.tar.gz.

File metadata

  • Download URL: KCMiddleware-0.5.tar.gz
  • Upload date:
  • Size: 9.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.5.0 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.0

File hashes

Hashes for KCMiddleware-0.5.tar.gz
Algorithm Hash digest
SHA256 a5f24d34666cdc578c454209675f051b7f92a41d0adeb809ca914fcb59d2d9c4
MD5 19f75ed1d3e843c3751fd818aa50598f
BLAKE2b-256 04142cbd45d6b3a10f90507b8b66a3e08c8d363b8347c9e13684f187f769015a

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