django-rest-framework-keycloak

django-rest-framework-keycloak package provides Keycloak support.


Keywords
django, rest, framework, keycloak, openid
License
GPL-3.0
Install
pip install django-rest-framework-keycloak==0.2.0

Documentation

Documentation Status

Django REST Framework Keycloak

django-rest-framework-keycloak package provides Keycloak support.

Installation

Via Pypi Package:

$ pip install django-rest-framework-keycloak

Manually

$ python setup.py install

Dependencies

django-rest-framework-keycloak depends on:

Tests Dependencies

  • unittest

Bug reports

Please report bugs and feature requests at https://bitbucket.org/agriness/django-rest-framework-keycloak/issues

Documentation

The documentation for django-rest-framework-keycloak is available on readthedocs.

Contributors

Usage

  • Add "django_keycloak" to your INSTALLED_APPS setting like this::
    INSTALLED_APPS = [
        ...
        'django_keycloak',
    ]
  • Add "keycloak_django.middleware.KeycloakMiddleware" to your MIDDLEWARE setting like this::
   MIDDLEWARE = [
       ...
       'keycloak_django.middleware.KeycloakMiddleware'
       ...
   ]
  • Add configure Keycloak::
   KEYCLOAK_CONFIG = {
       'KEYCLOAK_SERVER_URL': 'http://localhost/auth/',
       'KEYCLOAK_REALM': 'your_realm',
       'KEYCLOAK_CLIENT_ID': 'your_client',
       'KEYCLOAK_CLIENT_SECRET_KEY': 'secret_key',
       'KEYCLOAK_CLIENT_PUBLIC_KEY': 'public_key',
       'KEYCLOAK_DEFAULT_ACCESS': 'DENY', # DENY or ALLOW (Default is DENY)
       'KEYCLOAK_AUTHORIZATION_CONFIG': os.path.join(BASE_DIR,  'your-client-authz-config.json'),
       'KEYCLOAK_METHOD_VALIDATE_TOKEN': 'INTROSPECT', # INTROSPECT OR DECODE (Default is INTROSPECT)
   }
  • Map the scopes of the APIView::

from django.http.response import JsonResponse
from rest_framework.views import APIView

class AdminView(APIView):
    keycloak_scopes = {'GET': 'read-only-admin-view',
                       'POST': 'edit-admin-view'}

    def get(self, request, **kwargs):
        return JsonResponse({"page": "Admin Resource"})

    def post(self, request, format=None):
        return JsonResponse({"page": "Edit Admin Resource"})