Django application integrate with keycloak

Hi,
I have Django application which is using a djangorestframework to create restful api. I would like to integrate user system for authentication with keycloak. I am not getting anywhere, can anybody please give me advise, how to integrate authenticate system with keycloak.

Thanks in advance

hello @rhussain did you figure out a way to do this please??

The Mozilla Django OIDC https://mozilla-django-oidc.readthedocs.io/ library can facilitate this.

@xgp thank you for you reply, but i’m wondering if you have an exemple or repository which uses this library to integrate kc

https://www.google.com/search?q=django+oidc+keycloak+example

i’m looking for a complete project with django bcz all the project are not comlted or not working @xgp

Please share what you have tried and what is not working. Perhaps we can help you debug.

thank you @xgp i tried to use this repo

it has some docs which i followed step by step:

i followed the steps evrything was okey, but when i tried to login via /login it gives me a redirect errors like this screenshoots:


The redirect_uri you set when you set up the Client in Keycloak must match the one the browser is setting.

@xgp what di you lean by the browser is settings,knowing that for now i’m using the templates of django

It’s probably the URL of the page before you click on “Sign In”. Open the Developer Tools in your browser and choose the Network tab. After you click on “Sign In”, look for a request to the Keycloak server that contains a request with a redirect_uri parameter in the request. That will tell you what the browser is setting it to. Then you have to make sure that is in the “Valid redirect URIs” section of the Client you set up in Keycloak.

1 Like

Hello! We are implementing Keycloak on our Django application, over Django REST Framework (DRF).

There are a few things I’d like to point out:

  • If you are going to work with Session and want to integrate your Django users with Keycloak (KC), mozilla-django-oidc will work and it is the easiest and better one from all the libraries we configured.

  • If you are going to work with APIs and but not with Django users, only authenticating in KC, you should use Python Keycloak. Python KC implements most of the Keycloak REST APIs, which will save you from doing a lot of hand work to put up methods and functions.

Their documentation aren’t that extensive, but you can figure out most things reading their code and cross referencing with KC REST docs 1.

  • If you are going to work both with Browser Flow (sessions, I believe) and API authentication/authorization via tokens, I cannot help much because we are only integrating it with tokens. But I believe Python KC can suffice this too.

I’m not sure if Python KC has everything from the new API implemented, so double check both 21 1 and 20 2 versions. We are based in KC 20.

Cheers!

2 Likes