Giter VIP home page Giter VIP logo

django-keycloak-auth's Introduction

[WIP] Django Keycloak Authorization

Middleware to allow authorization using Keycloak and Django for DRF and Graphene based projects. This package can only be used for projects started from scratch since they override the users management.

Installation

  1. Add django_keycloak to the Django INSTALLED_APPS

  2. Add django_keycloak.middleware.KeycloakMiddleware to the Django `MIDDLEWARE

  3. Change Django AUTHENTICATION_BACKENDS to:

    AUTHENTICATION_BACKENDS = ('django_keycloak.backends.KeycloakAuthenticationBackend',)
  4. Add the following configuration to Django settings and replace the values by your own values:

    KEYCLOAK_CONFIG = {
        'BASE_PATH': 'auth/', # default path, but something keycloak doesn't have it in url
        'SERVER_URL': '<PUBLIC_SERVER_URL>',
        'INTERNAL_URL': <INTERNAL_SERVER_URL>'',
        'REALM': '<REALM_NAME>',
        'CLIENT_ID': '<CLIENT_ID>',
        'CLIENT_SECRET_KEY': '<CLIENT_SECRET_KEY>',
        'CLIENT_SUPERUSER_ROLE': '<client role in Keycloak which means superuser in Django>',
        'REALM_SUPERUSER_ROLE': '<realm role in Keycloak which means superuser in Django>',
        'CLIENT_STAFF_ROLE': '<client role in Keycloak which means staff user in Django>', 
        'REALM_STAFF_ROLE': '<realm role in Keycloak which means staff in Django>', 
        'EXEMPT_URIS': [],  # URIS to be ignored by the package
        'GRAPHQL_ENDPOINT': 'graphql/'  # Default graphQL endpoint
    }
    
  5. Override the Django user model on settings:

    AUTH_USER_MODEL = "django_keycloak.KeycloakUserAutoId"
  6. If using graphene add the GRAPHQL_ENDPOINT to settings and KeycloakGrapheneMiddleware to the graphene MIDDLEWARE.

  7. Configure Django Rest Framework authentication classes with django_keycloak.authentication.KeycloakAuthentication:

    REST_FRAMEWORK = {
        'DEFAULT_AUTHENTICATION_CLASSES': [
            'django_keycloak.authentication.KeycloakAuthentication'
        ],
        'DEFAULT_RENDERER_CLASSES': [
            'rest_framework.renderers.JSONRenderer',
        ],
        'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.LimitOffsetPagination',
        'PAGE_SIZE': 100,  # Default to 20
        'PAGINATE_BY_PARAM': 'page_size',
        # Allow client to override, using `?page_size=xxx`.
        'MAX_PAGINATE_BY': 100,
        # Maximum limit allowed when using `?page_size=xxx`.
        'TEST_REQUEST_DEFAULT_FORMAT': 'json'
    }
    

DRY Permissions

The permissions must be set like in other projects. You must set the permissions configuration for each model. Example:

@staticmethod
@authenticated_users
def has_read_permission(request):
    roles = request.remote_user.get('client_roles')

    return True if 'ADMIN' in roles else False

Keycloak users synchronization

The management command sync_keycloak_users must be ran periodically. In order to remove from the local users the ones that are no longer available at keycloak. This command can be called using the task named sync_users_with_keycloak, using celery. Fot that you just need to:

  • Add the task to the CELERY_BEAT_SCHEDULE ìns Django settings:

    CELERY_BEAT_SCHEDULE = {
        'sync_users_with_keycloak': {
            'task': 'django_keycloak.tasks.sync_users_with_keycloak',
            'schedule': timedelta(hours=24),
            'options': {'queue': 'sync_users'}
        },
    }
  • Add the sync_users queue to the docker-compose celery service:

    command: celery worker -A citibrain_base -B -E -l info -Q backup,celery,sync_users --autoscale=4,1

Attention: This task is only responsible to delete users from local storage. The creation of new users, that are on keycloak, is done when they try to login.

Notes

Support for celery 5: from version 0.7.4 on we should use celery 5 for the user sync. This implies running celery with celery -A app worker ... instead of celery worker -A app ...

django-keycloak-auth's People

Contributors

ftcardoso avatar jmggarcia avatar moritz89 avatar nickspring avatar pcoelho-ubi avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.