Skip to content

Feature Request: Support Dynamic Realm Names per Request #652

@AmmarRabie

Description

@AmmarRabie

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions