Package: keycloak-utils
keycloak auth utilsInstallation1. Django/DRFpipinstallkeycloak-utils[django]2. FastAPIpipinstallkeycloak-utils[fastapi]Usage1. Django/DRF# authentication.pyfromdjango.contrib.authimportget_user_modelfromkeycloak_utils.authentication.rest_frameworkimportBaseDRFKCAuthenticationUser=get_user_model()classKeycloakDRFAuthentication(BaseDRFKCAuthentication):kc_host="http://localhost:8080"kc_realm="your-realm-nae"kc_algorithms=["RS256"]kc_audience="account"auth_scheme="Bearer"defget_or_create_user(self,claims:dict):# override this method to get or create user# return User.objects.get_or_create(email=claims["email"])returnuser_instance# views.pyfromrest_framework.viewsimportAPIViewclassTestView(APIView):authentication_classes=[KeycloakDRFAuthentication]# Add authentication class heredefget(self,request):returnResponse({"message":"Hello, world!"})2. FastAPI# middlewares.pyimporttypingfromfastapiimportRequestfromkeycloak_utils.authentication.fastapiimportBaseFastAPIKCAuthenticationfromkeycloak_utils.backend.fastapiimportFastAPIKeycloakAuthBackendclassBearerAuthBackend(FastAPIKeycloakAuthBackend):kc_host="http://localhost:8080"kc_realm="test"kc_algorithms=["RS256"]kc_audience="account"auth_scheme="Bearer"classAuthenticationMiddleware(BaseFastAPIKCAuthentication):backends=[BearerAuthBackend]defpost_process_claims(self,claims:typing.Optional[dict],request:Request,)->Request:# do something with `claims` herereturnrequest# main.pyfromfastapiimportFastAPIapp=FastAPI()app.add_middleware(AuthenticationMiddleware)# Add middleware here@app.get("/")defread_root():return{"Hello":"World"}Example cURL requestcurl--location'http://localhost:8080/path/to/resource/'\--header'Authorization: <AUTH_SCHEME> <JWT_ACCESS_TOKEN>'Replace theJWT_ACCESS_TOKENwith the actual access token.AUTH_SCHEMEwith the actual auth scheme. For example,BearerorTokenor anything you defined withauth_schemeclass attribute.Usage Advanced1. Support for multiple authentication classes/backendsDjango/DRF# authentication.pyfromdjango.contrib.authimportget_user_modelfromkeycloak_utils.authentication.rest_frameworkimportBaseDRFKCAuthenticationUser=get_user_model()classKCBearerAuth(BaseDRFKCAuthentication):kc_host="http://localhost:8080"kc_realm="your-realm-nae"kc_algorithms=["RS256"]kc_audience="account"auth_scheme="Bearer"defget_or_create_user(self,claims:dict):# override this method to get or create user# return User.objects.get_or_create(email=claims["email"])returnuser_instanceclassKCRandomAuth(BaseDRFKCAuthentication):kc_host="http://localhost:1234"# using a different KeyCloak hostkc_realm="realm-2"# using a different realmkc_algorithms=["RS256"]kc_audience="account"auth_scheme="Random"# This should be unique across all the authentication classesdefget_or_create_user(self,claims:dict):# override this method to get or create user# return User.objects.get_or_create(email=claims["email"])returnuser_instance# views.pyfromrest_framework.viewsimportAPIViewclassTestView(APIView):authentication_classes=[KCBearerAuth,KCRandomAuth]# Add authentication class heredefget(self,request):returnResponse({"message":"Hello, world!"})FastAPI# middlewares.pyimporttypingfromfastapiimportRequestfromkeycloak_utils.authentication.fastapiimportBaseFastAPIKCAuthenticationfromkeycloak_utils.backend.fastapiimportFastAPIKeycloakAuthBackendclassBearerAuthBackend(FastAPIKeycloakAuthBackend):kc_host="http://localhost:8080"kc_realm="test"kc_algorithms=["RS256"]kc_audience="account"auth_scheme="Bearer"classRandomAuthBackend(FastAPIKeycloakAuthBackend):kc_host="http://localhost:1234"# using a different KeyCloak hostkc_realm="realm-2"# using a different realmkc_algorithms=["RS256"]kc_audience="account"auth_scheme="Random"classAuthenticationMiddleware(BaseFastAPIKCAuthentication):backends=[BearerAuthBackend,RandomAuthBackend]defpost_process_claims(self,claims:typing.Optional[dict],request:Request,)->Request:# do something with `claims` herereturnrequest# main.pyfromfastapiimportFastAPIapp=FastAPI()app.add_middleware(AuthenticationMiddleware)# Add middleware here@app.get("/")defread_root():return{"Hello":"World"}Test# Install the dependenciespipinstall.[test]# Run testspython-mpytestRelease# do a dry-run first -
bump2version --dry-run --verbose [major|minor|patch]

# if everything looks good, run the following command to release
bump2version --verbose [major|minor|patch]

# push the changes to remote
git push origin master --tags