A simple Python package to work with Keycloak users and login systems
Project description
Keycloak Manager
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 URLrealm: Keycloak realm nameadmin_username: Admin usernameadmin_password: Admin passwordclient_id: Client IDclient_secret: Client secret (optional)
Development Setup
- Clone the repository:
git clone https://github.com/anqorithm/keycloak-manager.git
cd keycloak-manager
- Install dependencies:
poetry install
- 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
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - 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
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file keycloak_manager-0.1.1.tar.gz.
File metadata
- Download URL: keycloak_manager-0.1.1.tar.gz
- Upload date:
- Size: 5.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.2 CPython/3.12.0 Linux/5.15.167.4-microsoft-standard-WSL2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
37e9cc04140c655809a27da756a2a68a5bd5f63376cc9e648b5c4f84d3f375d4
|
|
| MD5 |
5ef5c5e723ac2fce12b870edea0a6070
|
|
| BLAKE2b-256 |
1f685a3f8f3bb12d31ab810dc57c21eabacc948bd516e5b28095a2a9269e6e2b
|
File details
Details for the file keycloak_manager-0.1.1-py3-none-any.whl.
File metadata
- Download URL: keycloak_manager-0.1.1-py3-none-any.whl
- Upload date:
- Size: 6.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.2 CPython/3.12.0 Linux/5.15.167.4-microsoft-standard-WSL2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a7a0ec3eb66c0ba019c433cb919ad36944dff96262aa8138849436bdbc5a3401
|
|
| MD5 |
e6711212f1af02227e6e2ccee80982b4
|
|
| BLAKE2b-256 |
30ab011ef8e1ed0deeb1749ba5f1e229c7cd087a5b788c30afe1f1bf6abcf097
|