Developer-friendly AWS Cognito client for Python, with clean APIs for managing user pools and authentication flows.
Project description
Sanctum
Developer-friendly AWS Cognito client for Python, with clean APIs for managing user pools and authentication flows.
Installation
pip install sanctum
Quick Start
from sanctum import SanctumClient
# Initialize the client
client = SanctumClient(
user_pool_id="us-east-1_123456789",
client_id="your-app-client-id",
region="us-east-1"
)
# User authentication
try:
auth_result = client.authenticate_user(
username="user@example.com",
password="SecurePassword123!"
)
print(f"Login successful! Access token: {auth_result['AccessToken']}")
except AuthenticationError as e:
print(f"Authentication failed: {e}")
# User management
user_info = client.create_user(
username="newuser@example.com",
password="TempPassword123!",
email="newuser@example.com",
temporary_password=True
)
# RBAC (Role-Based Access Control)
# Create groups and manage permissions
admin_group = client.create_group(
group_name="admin",
description="System administrators",
precedence=1
)
# Add user to group
client.add_user_to_group(
username="user@example.com",
group_name="admin"
)
# Check user permissions
has_admin_access = client.user_has_group(
username="user@example.com",
group_name="admin"
)
Features
🔐 Authentication & Authorization
- Multiple Auth Flows: USER_PASSWORD_AUTH, ADMIN_NO_SRP_AUTH, CUSTOM_AUTH
- MFA Support: SMS, TOTP, and custom challenge responses
- Token Management: Automatic refresh token handling
- Role-Based Access Control (RBAC): AWS Cognito User Pool Groups integration
👥 User Management
- User Lifecycle: Create, update, delete users
- Profile Management: Update user attributes and preferences
- Admin Operations: Admin-initiated password changes and user management
- User Status: Enable, disable, confirm users
🛡️ Security Features
- JWT Token Processing: Parse and validate access tokens
- Group-Based Permissions: Check user group memberships from tokens
- Secure Hash Calculation: HMAC-SHA256 for client secret validation
- Comprehensive Error Handling: Detailed error reporting and handling
🏗️ Developer Experience
- Clean, Intuitive API: Pythonic interface for AWS Cognito operations
- Comprehensive Testing: 151 tests with 89% code coverage
- Type Hints: Full type annotation support
- Modular Architecture: Use individual components or the unified client
API Overview
Main Client
from sanctum import SanctumClient
client = SanctumClient(
user_pool_id="us-east-1_123456789",
client_id="your-app-client-id",
region="us-east-1",
client_secret="your-client-secret" # Optional
)
Authentication Methods
# User password authentication
auth_result = client.authenticate_user("user@example.com", "password")
# Admin authentication (no SRP)
auth_result = client.admin_authenticate("user@example.com", "password")
# Custom authentication flow
auth_result = client.initiate_custom_auth("user@example.com", auth_parameters={})
User Management
# Create user
user = client.create_user(
username="user@example.com",
password="TempPass123!",
email="user@example.com",
temporary_password=True
)
# Update user attributes
client.update_user_attributes("user@example.com", {"name": "John Doe"})
# Delete user
client.delete_user("user@example.com")
RBAC Operations
# Group management
client.create_group("admin", "Administrators", precedence=1)
client.add_user_to_group("user@example.com", "admin")
# Permission checking
is_admin = client.user_has_group("user@example.com", "admin")
# JWT token group extraction
groups = client.rbac.get_groups_from_token(access_token)
Token Management
# Refresh tokens
new_tokens = client.refresh_token(refresh_token)
# Respond to auth challenges
challenge_response = client.respond_to_auth_challenge(
challenge_name="SMS_MFA",
session="challenge-session",
challenge_responses={"SMS_MFA_CODE": "123456"}
)
Advanced Usage
Using Individual Components
If you prefer to use specific components directly:
from sanctum import RBACManager, TokenManager, UserManager
# Direct component usage
rbac = RBACManager(user_pool_id, client_id, region)
tokens = TokenManager(user_pool_id, client_id, region)
users = UserManager(user_pool_id, client_id, region)
Error Handling
from sanctum.exceptions import AuthenticationError, ValidationError, SanctumError
try:
result = client.authenticate_user("user@example.com", "wrong-password")
except AuthenticationError as e:
print(f"Authentication failed: {e}")
except ValidationError as e:
print(f"Validation error: {e}")
except SanctumError as e:
print(f"General Sanctum error: {e}")
Documentation
Examples
Check out the examples directory for comprehensive usage examples:
- Basic Usage: Authentication and user management
- Advanced Usage: Complex workflows and error handling
- RBAC Examples: Role-based access control scenarios
- Test Workflows: Integration testing patterns
Development
Requirements
- Python 3.8+
- AWS account with Cognito User Pool configured
- boto3 and other dependencies (see
requirements.txt)
Testing
The project includes comprehensive tests with 89% coverage:
# Run all tests
python -m pytest tests/ --cov=sanctum --cov-report=term-missing
# Run specific test modules
python -m pytest tests/test_rbac.py -v
python -m pytest tests/test_client.py -v
# Run tests with coverage report
python -m pytest tests/ --cov=sanctum --cov-report=html
Contributing
We welcome contributions! Please see CONTRIBUTING.md for guidelines.
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Make your changes
- Add tests for new functionality
- Ensure all tests pass (
python -m pytest) - Submit a pull request
License
MIT License - see LICENSE file for details.
Changelog
See CHANGELOG.md for a detailed history of changes.
Project details
Release history Release notifications | RSS feed
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 sanctum-0.1.1.tar.gz.
File metadata
- Download URL: sanctum-0.1.1.tar.gz
- Upload date:
- Size: 15.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.5 CPython/3.13.3 Darwin/24.3.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0b712b3757bc2c33b7db55b62c0ba1a90a6819e71d0fbd260830d38b61aeadf7
|
|
| MD5 |
9aca455c71e2b24547d06825f1b74a87
|
|
| BLAKE2b-256 |
c180295ca1a04e9dd685ba9f3c05ec0d5ccca5dea14c328de0250495ec8236dd
|
File details
Details for the file sanctum-0.1.1-py3-none-any.whl.
File metadata
- Download URL: sanctum-0.1.1-py3-none-any.whl
- Upload date:
- Size: 16.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.5 CPython/3.13.3 Darwin/24.3.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6e4a76295c411c7b16d92ad2aea2ea4d5bf73aa69d3323453d8e29e37e532020
|
|
| MD5 |
9975a1ab9f235241b6db4e0f97a1b1d0
|
|
| BLAKE2b-256 |
b82cb1b5beaa7545da479ae7191a779d53e5e75cb4e883142f96c0e6d20e834a
|