keycloakflask-oidc

Flask-OIDC | How to call a specific function after the user logged in


I built a login system using Flask OIDC and Keycloak. In my system, there is some endpoints decorated with oidc.require_login() that calls the Keycloak login page.

My goal is, after the user successfully logged in, my system checks if the user name exists in a specific database.

How can I set a function to be called every time someone successfully logged in with Keycloak and do this verification at the database?


Solution

  • According to your needs there are several ways to create the user in the backend.

    The last two examples create a JAR (which is explained in the links). That JAR with the SPI must be deployed in keycloaks standalone/deployments folder. The EventListener should be active by default, the UserStorage SPI must be activated per realm.

    But - be aware - Keycloak/SSO/JWT - should not be used by creating users in multiple backends. Syncing the users between all backends in a SSO environment is maybe the wrong way. Most information is located in the JWT or can be called by a backend from one central user identity management. Do not store a user more then once. If you need the user reference in your backend - link just to the username or userid (string) instead of a complete entity.