Skip to main content

Keycloak Manager

PyPI version Python Versions License

A simple Python package to work with Keycloak users and login systems. Makes it easy to create and manage users, handle logins, and work with user tokens all through clean and easy-to-use Python code.

Features

  • 🔐 User Management (Create, Update, Delete)
  • 🎫 Token Generation and Validation
  • 👤 User Authentication
  • 🔑 Password Management
  • 📝 Custom Attributes Support
  • 🔄 Realm Role Management
  • 📊 Token Decoding and Verification

Installation

# Using Poetry (recommended)
poetry add keycloak-manager

# Using pip
pip install keycloak-manager

Quick Start

from keycloak_manager import KeycloakManager

# Initialize the manager
manager = KeycloakManager(
    base_url="http://localhost:8080",
    realm="YourRealm",
    admin_username="admin",
    admin_password="admin",
    client_id="your-client",
    client_secret="your-secret"
)

# Create a new user
user_id = manager.create_user(
    username="abdullah",
    email="abdullah@anqorithm.com",
    first_name="John",
    last_name="Doe",
    enabled=True,
    email_verified=True,
    realm_roles=["user"]
)

# Set user password
manager.set_user_password(user_id, "secure_password")

# Get user token
token_response = manager.get_user_token(
    username="abdullah",
    password="secure_password"
)

Configuration

The KeycloakManager requires the following configuration:

  • base_url: Keycloak server URL
  • realm: Keycloak realm name
  • admin_username: Admin username
  • admin_password: Admin password
  • client_id: Client ID
  • client_secret: Client secret (optional)

Development Setup

  1. Clone the repository:
git clone https://github.com/anqorithm/keycloak-manager.git
cd keycloak-manager
  1. Install dependencies:
poetry install
  1. Run tests:
poetry run pytest

Testing

The package includes comprehensive tests. Run them using:

poetry run pytest -v

For development, you can use the example script:

python app.py

Project Structure

keycloak_manager/
├── keycloak_manager/
│   ├── __init__.py
│   ├── manager.py     # Main manager class
│   └── models.py      # Pydantic models
├── tests/
│   └── test_manager.py
├── app.py            # Example usage
├── pyproject.toml
└── README.md

Models

UserCreate

class UserCreate(BaseModel):
    username: str
    email: EmailStr
    first_name: str
    last_name: str
    enabled: bool = True
    email_verified: bool = False
    realm_roles: List[str]
    attributes: Dict[str, List[str]] = Field(default_factory=dict)

TokenResponse

class TokenResponse(BaseModel):
    access_token: str
    expires_in: int
    refresh_expires_in: int
    refresh_token: str
    token_type: str
    session_state: str
    scope: str

Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

License

This project is licensed under the MIT License - see the LICENSE file for details.

Author

Abdullah Alqahtani
Email: abdullah@anqorithm.com
GitHub: @anqorithm

Acknowledgments

  • Keycloak Team for their excellent authentication server
  • Pydantic for data validation
  • FastAPI community for inspiration

Support

For support, please open an issue in the GitHub repository or contact the maintainers at abdullah@anqorithm.com

Metadata

Release files for keycloak-manager 0.1.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for keycloak-manager 0.1.3
File Size Uploaded Provenance Actions
keycloak_manager-0.1.3.tar.gz 5.5 kB Without provenance Details for keycloak_manager-0.1.3.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for keycloak-manager 0.1.3
File Interpreter ABI Platform Actions
keycloak_manager-0.1.3-py3-none-any.whl Python 3 none any Details for keycloak_manager-0.1.3-py3-none-any.whl

Total release size: 12.0 kB

Release files / keycloak_manager-0.1.3.tar.gz

Download URL keycloak_manager-0.1.3.tar.gz
Size 5.5 kB
Tags Source
SHA-256 checksum
How to use checksums
5d3a5c9e4eea74e970793b8f674cd717e7e952c25cb39b32739f2729b7943242
BLAKE2b-256 checksum
How to use checksums
45216582b35c11a272cc726177639bcbc2a6d8cd0723786c409c5f85f1ddf60e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.2 CPython/3.12.0 Linux/5.15.167.4-microsoft-standard-WSL2

Release files / keycloak_manager-0.1.3-py3-none-any.whl

Download URL keycloak_manager-0.1.3-py3-none-any.whl
Size 6.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3e25f9b606c89f33cc5ee0cb7ffae396a1cac32d6d1739b0e36f3d42b7f3af49
BLAKE2b-256 checksum
How to use checksums
cd023194c4706760c7a184f630540df7311d31299cf80b4d20d2ae366bb782d5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.2 CPython/3.12.0 Linux/5.15.167.4-microsoft-standard-WSL2

Security for keycloak-manager 0.1.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page