Coder Social home page Coder Social logo

casdoor / django-casdoor-auth Goto Github PK

View Code? Open in Web Editor NEW
20.0 3.0 8.0 18 KB

Django auth middleware based on Casdoor

Home Page: https://pypi.org/project/django-casdoor-auth

License: Apache License 2.0

Python 100.00%
auth cas casdoor django go iam middleware oauth oidc python sso

django-casdoor-auth's Introduction

django-casdoor-auth

GitHub Action Version PyPI - Wheel Pyversions Discord

Casdoor's SDK for Django will allow you to easily connect your application to the Casdoor authentication system without having to implement it from scratch.

Step1. install app

django-casdoor-auth is available on PyPI:

pip install django-casdoor-auth

casdoor-auth is simple to use. We will show you the steps below.

Step2. Config

setting.py

Add "casdoor_auth" in INSTALLED_APPS

INSTALLED_APPS = [
    "django.contrib.admin",
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.sessions",
    "django.contrib.messages",
    "django.contrib.staticfiles",
    "casdoor_auth"
]

Initialization requires 6 parameters, which are all str type:

Name (in order) Must Description
endpoint Yes Casdoor Server Url, such as http://localhost:8000
client_id Yes Application.client_id
client_secret Yes Application.client_secret
certificate Yes The public key for the Casdoor application's cert
org_name Yes Application.organization
application_name Yes Application.name
CASDOOR_CONFIG = {
    'endpoint': 'http://localhost:8000',
    'client_id': '<client-id>',
    'client_secret': '<client-secret>',
    'certificate': '''<certificate>''',
    'org_name': 'built-in',
    'application_name': 'app-built-in'
}

The redirect url, is the URL that your APP is configured to listen to the response from Casdoor.

REDIRECT_URI = 'http://127.0.0.1:8000/casdoor/callback/'

The login redirect url, after login successfully, you will jump to this page.

LOGIN_REDIRECT_URL = '/'

Step3. router

urls.py

urlpatterns = [
    ...
    path('casdoor/', include('casdoor_auth.urls')),
    ...
]

The casdoor_auth provider two functions for using Casdoor.

urlpatterns = [
    path('login/', views.toLogin, name='casdoor_sso'),
    path('callback/', views.callback, name='callback'),
]

To add a button for using the Casdoor login, for example:

<button><a href="{% url 'casdoor_sso' %}">casdoor</a></button>`

django-casdoor-auth's People

Contributors

anyidea avatar hopomi avatar hsluoyz avatar leo220yuyaodog avatar selflocking avatar semantic-release-bot avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

django-casdoor-auth's Issues

/casdoor/callback/ Error

translate:
This error is returned because the interface actually returns a dict, and the tokenn string needs to be extracted. The error is as follows.

Internal Server Error: /casdoor/callback/
Traceback (most recent call last):
File "C:\Users\BCY\AppData\Roaming\Python\Python310\site-packages\django\core\handlers\exception.py", line 55, in inner
response = get_response(request)
File "C:\Users\BCY\AppData\Roaming\Python\Python310\site-packages\django\core\handlers\base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "D:\Users\BCY\anaconda3\lib\site-packages\casdoor_auth\views.py", line 45, in callback
user = sdk.parse_jwt_token(token)
File "C:\Users\BCY\AppData\Roaming\Python\Python310\site-packages\casdoor\main.py", line 239, in parse_jwt_token
return_json = jwt.decode(
File "C:\Users\BCY\AppData\Roaming\Python\Python310\site-packages\jwt\api_jwt.py", line 84, in decode
payload, _, _, _ = self._load(jwt)
File "C:\Users\BCY\AppData\Roaming\Python\Python310\site-packages\jwt\api_jws.py", line 176, in _load
raise DecodeError("Invalid token type. Token must be a {0}".format(
jwt.exceptions.DecodeError: Invalid token type. Token must be a <class 'bytes'>

Adding a new judgment on line 43 of views.py can solve the problem.

if isinstance(token, dict): token = token['access_token']

Make a SSO auth middleware to integrate Casdoor with Django

Now we already have the Python SDK: https://github.com/casdoor/casdoor-python-sdk and the plugin for a popular Python open source software, Odoo: https://github.com/casdoor/odoo-casdoor-oauth

Now we need to develop the plugins for Django. This plugin ideally will be based on casdoor-python-sdk and will provide friendly interface for Django users, like a Django app can easily integrate with Casdoor login with only one or a few lines of code.

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.