Package: django-oauth-client
OAuth Login with DjangoThis README.md provides instructions on implementing OAuth login within your Django application. OAuth integration allows you to replace local authentication with a remote Identity Provider (IdP) while enabling role-based access control for specific endpoints.It is the result of a client project ofSchroeck IT Consulting.
The goal was a simple Django integration with Keycloak without much manual interference needed. Hence, the code is very much focused on Keycloak.However, it should work for most other OAuth IdP's. If you require RBAC (Role-based access), make sure to name the claim containing the roles,roles.FeaturesGet an API token for your Django app using your favorite IdP via OAuth. This library simplifies the process by giving you the required endpoints and the login mechanismProtect your endpoints by just adding a dependency to the endpoint functionsSupport for role-based access control for endpointsContactThis library was originally published bySchroeck IT-Consulting.Email:hello@schroeck-consulting.deInstallationSimply pull the package from Pypi:bash pip install django_oauth_clientHow to Secure Your EndpointsSecuring your endpoints using OAuth is a straightforward process:Configure Application Environment: First, set up the necessary environment variables you received from your IdP administrator. Replace<client-id>and<client-secret>with the provided values. TheOAUTH_SCOPEvariable specifies the required access scopes, andOAUTH_METADATA_URLshould point to your IdP's OpenID configuration.exportOAUTH_CLIENT_ID="<client-id>"exportOAUTH_CLIENT_SECRET="<client-secret>"exportOAUTH_SCOPE="profile openid email"exportOAUTH_METADATA_URL="http://127.0.0.1:8100/realms/master/.well-known/openid-configuration"Add OAuth Views to URLs: In your Djangourls.py, import and include thelogin,auth, andlogoutviews provided by this package. These views are responsible for handling OAuth authentication.fromdjango.urlsimportpathfromdjango_oauth_client.oauthimportlogin,auth,logouturlpatterns=[...# other URLspath('login/',login),path('auth',auth),path('logout/',logout),]With these URL patterns, you'll have endpoints for initiating the OAuth login (/login/), handling authentication callbacks (/auth), and logging out (/logout/).Protect Endpoints with Roles: To restrict access to specific endpoints based on roles, use the@protecteddecorator provided by this package in yourviews.pyfile. In this example, we protect theprotected_endpointview, allowing only users with the "admin" role to access it.fromdjango_oauth_client.oauthimportprotected@protected(roles=["admin"])defprotected_endpoint(request):"""A protected endpoint that only users with the "admin" role can access."""return"OK"By applying the@protecteddecorator to your views, you can enforce role-based access control on your Django application's endpoints, ensuring that only authorized users can access protected resources.** How to retrieve a token for API Access**: Simply access theloginendpoint with an additional?token=Truefrom your browser once to get a token from Keycloak that you can use asBearertoken in theAuthorizationheader.By following these steps, you can seamlessly integrate OAuth authentication into your Django application, enhancing security and access control for your users.Contact:hello@schroeck-consulting.de