百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
D

django-keycloak-auth

> 编程语言
开源

Django Keycloak 身份验证

100 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

Django Keycloak 身份验证

Django Keycloak Auth

  • Django Keycloak Auth
    • What is it?
    • Buy me a coffee
    • Installation
      • Via Pypi Package:
      • Manually
    • Dependencies
    • Test dependences
    • How to contribute
    • Licence
    • Credits
    • Usage
    • How to apply on your Views
      • Class-based Views
        • ModelViewSet
        • ViewSet
        • APIView
      • Function Based Views
    • Local development
      • Run tests for this lib
      • Upload this package to Pypi
      • Run local API test using this library

What is it?

Django Keycloak Auth is a simple library that authorizes your application's resources using Django Rest Framework.

This package is used to perform authorization by keycloak roles from JWT token. Both realm roles and client roles are supported.

For example, the following token indicates that the user has the realm role "manager" and the client roles "director" and "employer" :

...

  "realm_access": {
    "roles": [
      "manager"
    ]
  },
  "resource_access": {
    "first-api": {
      "roles": [
        "director",
        "employer",
      ]
    }
  },
  ...

For review see https://github.com/marcelo225/django-keycloak-auth

Package link: https://pypi.org/project/django-keycloak-auth/

Buy me a coffee

If you have recognized my effort in this initiative, please buy me a coffee when possible.

Installation

Via Pypi Package:

$ pip install django-keycloak-auth

Manually

$ python setup.py install

Dependencies

  • Python 3
  • requests
  • Django
  • Django Rest Framework

Test dependences

  • unittest

How to contribute

Please report bugs and feature requests at https://github.com/marcelo225/django-keycloak-auth/issues

Licence

The MIT License (MIT)

Copyright (c) 2020 Marcelo Vinícius

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Credits

Lead Developer - Marcelo Vinicius

Co-authored-by:

  • chmoder

Usage

  1. In your application settings.py file, add following Middleware:
…

How to apply on your Views

Class-based Views

ModelViewSet

…

ViewSet

python

class CarViewSet(viewsets.ViewSet):
    """
    Car endpoint
    This endpoint has not configured keycloak roles.
    That means all methods will be allowed to access.
    """
    def list(self, request):
        return JsonResponse({"detail": "success"}, status=status.HTTP_200_OK)

APIView

…

When you don't put keycloak_roles attribute in the Views that means all methods authorizations will be allowed.

Function Based Views

When if you use api_view decorator, you would write a very simple @keycloak_roles decorator like this:

python
from django_keycloak_auth.decorators import keycloak_roles
...

@keycloak_roles(['director', 'judge', 'employee'])
@api_view(['GET'])
def loans(request):
    """
    List loan endpoint
    This endpoint has configured keycloak roles only
    especific GET method will be accepted in api_view.
    """
    return JsonResponse({"message": request.roles})

Local development

Run tests for this lib

Before everything, you must install VirtualEnv.

bash
# Install venv in root project folder
$ python3 -m venv env && source env/bin/activate

# Install dependencies
$ pip install -r requirements.txt

# Run tests
$ python manage.py test

Upload this package to Pypi

Warning: before you update this package, certifies if you'll change the version in setup.py file.

If you interested contribute to developing this project, it was prepared a tiny tutorial to install the environment before you begin:

bash
# Install venv in root project folder
$ python3 -m venv env && source env/bin/activate

# Update packages for development
$ python -m pip install --upgrade -r requirements.txt

# Generate distribution -> it's on me for while ;)
$ python setup.py sdist

# Checks if the package has no errors
$ twine check dist/*

# Upload package -> it's on me for while ;)
$ twine upload --repository-url https://upload.pypi.org/legacy/ dist/*

Run local API test using this library

  1. Run following command on terminal to up Keycloak docker container:
bash
# in root project folder
$ docker-compose up
  1. Open http://localhost:8080/ in your web browser

  2. Create the following steps:

    1. realm, client (as confidential) and your client secret according the settings.py file
    2. Client Roles: director, judge, employee
    3. Create a new user account
    4. Vinculate Client Roles into above user account
  3. Run following command on another terminal:

bash
# Install venv in root project folder
$ python3 -m venv env && source env/bin/activate

# Install dependencies for this library
$ python -m pip install --upgrade -r requirements.txt

# Generate a local distribution for django-keyclaok-auth
# Change the version of this library if necessary
$ python setup.py sdist

# Generate a local dist (verify version)
$ pip install dist/*

# Create migrations, fixtures and run django server
$ python manage.py makemigrations && \
  python manage.py migrate && \
  python manage.py loaddata banks.json && \
  python manage.py runserver
  1. Starting development server at http://127.0.0.1:8000/
  2. Use Insonmina or Postman to test API's endpoints using Oauth2 as authentication mode.

Issues· 4 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Python

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言