Skip to main content

Keycloak IAM Admin Pydantic Models

Project description

Keycloak Admin Model Representation Pydantic v2 Bindings

This project contains ONLY the pydantic v2 bindings for Keycloak model representations for use in Python based projects utilizing Keycloak admin APIs.

For APIs, I recommend Mantelo. Since this library itself doesn't offer any model representation bindings, projects can use both these libraries together for a complete pydantic compliant experience.

Caveats

  1. Transform model representations to python dictionaries using pydantic model_dump(). Mantelo client expects python dictionaries for write operations. Keycloak API semantics expect that all unset attributes are excluded.

    class TenantAdapter:
        @classmethod
        def model_new(cls, schema: TenantReqSchema) -> OrganizationRepresentation:
            """Convert `TenantReqSchema` to `OrganizationRepresentation`."""
            return OrganizationRepresentation(
                name=schema.name,
                alias=schema.name.replace(" ", "_"),
                domains=[OrganizationDomainRepresentation(name=f"{schema.name.replace(' ', '-').lower()}.org", verified=False)],
                enabled=schema.active,
                attributes={"tax_id": [schema.tax_id], "address": [schema.address]},
            ).model_dump(exclude_unset=True)
    
  2. Models use snake-case attribute names for applications; but for Keycloak APIs, users should aliases to ensure actual Keyclock API attribute names are used.

    class TenantAdapter:
        @classmethod
        def model_new(cls, schema: TenantReqSchema) -> Any:
            """Convert `TenantReqSchema` to `Serializable`."""
            return OrganizationRepresentation(
                name=schema.name,
                alias=schema.name.replace(" ", "_"),
                domains=[OrganizationDomainRepresentation(name=f"{schema.name.replace(' ', '-').lower()}.org", verified=False)],
                enabled=schema.active,
                attributes={"tax_id": [schema.tax_id], "address": [schema.address]},
            ).model_dump(exclude_unset=True, use_alias=True)
    
  3. Transform python dictionaries to model representations using python dictionary unpacking operator **. Mantelo client returns python dictionaries for read operations.

    class TenantAdapter:
        @classmethod
        def schema(cls, data: Any | None) -> TenantResSchema | None:
            """Convert `Dict` to `TenantResSchema`."""
            if data is None:
                return None
    
            # Coerse data dict to `OrganizationRepresentation`
            #
            representation = OrganizationRepresentation(**data)
            return TenantResSchema(
                id=representation.id,
                name=representation.name,
                address=representation.attributes["address"][0],
                tax_id=representation.attributes["tax_id"][0],
                active=representation.enabled,
            )
    

Versioning

I intend to keep the library versioning synced with the latest stable Keycloak release.

Installing

  • pip install keycloak-admin-client
  • poetry add keycloak-admin-client

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

keycloak_admin_client-26.2.4.tar.gz (11.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

keycloak_admin_client-26.2.4-py3-none-any.whl (10.9 kB view details)

Uploaded Python 3

File details

Details for the file keycloak_admin_client-26.2.4.tar.gz.

File metadata

  • Download URL: keycloak_admin_client-26.2.4.tar.gz
  • Upload date:
  • Size: 11.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.12.3 Linux/5.15.167.4-microsoft-standard-WSL2

File hashes

Hashes for keycloak_admin_client-26.2.4.tar.gz
Algorithm Hash digest
SHA256 74b51cb28dcde2a3f490ab9b88102d7b3161b6bdf36e8a5c36fbc722ea246217
MD5 cd73f8b0a99704bbd1b1fef3dcc65692
BLAKE2b-256 58cabb08b2b5272d2f395eaa6b65943b1e5e21f1726b4653a84e9b1dbdad7615

See more details on using hashes here.

File details

Details for the file keycloak_admin_client-26.2.4-py3-none-any.whl.

File metadata

  • Download URL: keycloak_admin_client-26.2.4-py3-none-any.whl
  • Upload date:
  • Size: 10.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.12.3 Linux/5.15.167.4-microsoft-standard-WSL2

File hashes

Hashes for keycloak_admin_client-26.2.4-py3-none-any.whl
Algorithm Hash digest
SHA256 63ee76a2bb6f55450beaf2936ae5d06f633e3a0c29c64c5b116409ad1b8fbcb8
MD5 f82cedcfa98e4d97aea2eaa12971282b
BLAKE2b-256 da51d3ab203e70c08a6d17530cdcedecd1c4e7bc54f4eec97086d3ba221f67fa

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page