Search code examples
pythonpostgresqlauthenticationkeycloakkeycloak-services

Keycloak UserStorageProvider Python Implementation


As per Keycloak documentation to connect to an existing external database with user information we will need to implement the UserStorageProvider interface:

https://www.keycloak.org/docs/latest/server_development/index.html#_user-storage-spi

I couldn't find the same within the latest version of python keycloak package:

https://pypi.org/project/python-keycloak/

How can i connect to an external database like postgres from my python application if the UserStorageProvider class is not present in the supported keycloak library for python?.

Couldn't find this information on any other forum so any help regarding this would be much appreciated


Solution

  • How can i connect to an external database like postgres from my python application if the UserStorageProvider class is not present in the supported keycloak library for python?.

    This library:

    https://pypi.org/project/python-keycloak/
    

    is just a library for the client side to communicate with the Keycloak server via Rest API calls. From that project you can read

    python-keycloak is a Python package providing access to the Keycloak API.

    It is not officially maintained by the Keycloak project; but even if that was the case, it is just a wrap around the Keycloak Rest API. You would not be able to implement your custom UserStorageProvider using it anyway.

    When the Keycloak documentation states:

    You can use the User Storage SPI to write extensions to Keycloak to connect to external user databases and credential stores.

    is referring to extending the code of the Keycloak server, which is writing in Java not in python. You have to look at this documentation to understand the interfaces provided. To extend the Keycloak server code via Service Provider Interfaces (SPI) check the official documentation. It will tell you how to create your custom user storage provider using SPI.