-
Notifications
You must be signed in to change notification settings - Fork 329
Description
Description
We're using python-keycloak in a Flask API application that needs to support multiple dynamic Keycloak realms. Currently, we can't simply modify the global realm_name attribute of the Keycloak client instance because this would cause race conditions in our multi-threaded environment.
After reviewing the code, I noticed that:
- The library appears to be thread-safe
- The realm name is only used in request payloads (e.g., {realm_name: self.realm_name})
- The realm name isn't part of the server URL or underlying connection
Proposed Solution
Allow the realm name to be overridden on a per-request basis while maintaining the default realm name that was set during object construction. This would:
- Preserve backward compatibility
- Support dynamic realm use cases
- Avoid race conditions since each request could specify its own realm
Use Case
In our multi-tenant application, different requests may need to authenticate against different Keycloak realms. Currently, we have to create new Keycloak client instances for each realm, which is inefficient.
Suggested Implementation
Add an optional realm_name parameter to relevant methods that would temporarily override the instance's default realm name for that specific request.