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.
Metadata
Release files for sanctum 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| sanctum-0.1.1.tar.gz | 15.2 kB | Without provenance | Details for sanctum-0.1.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| sanctum-0.1.1-py3-none-any.whl | Python 3 | none | any | Details for sanctum-0.1.1-py3-none-any.whl |
Total release size: 32.0 kB
Release files / sanctum-0.1.1.tar.gz
| Download URL | sanctum-0.1.1.tar.gz |
|---|---|
| Size | 15.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
0b712b3757bc2c33b7db55b62c0ba1a90a6819e71d0fbd260830d38b61aeadf7
|
|
BLAKE2b-256 checksum How to use checksums |
c180295ca1a04e9dd685ba9f3c05ec0d5ccca5dea14c328de0250495ec8236dd
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.8.5 CPython/3.13.3 Darwin/24.3.0
|
Release files / sanctum-0.1.1-py3-none-any.whl
| Download URL | sanctum-0.1.1-py3-none-any.whl |
|---|---|
| Size | 16.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
6e4a76295c411c7b16d92ad2aea2ea4d5bf73aa69d3323453d8e29e37e532020
|
|
BLAKE2b-256 checksum How to use checksums |
b82cb1b5beaa7545da479ae7191a779d53e5e75cb4e883142f96c0e6d20e834a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.8.5 CPython/3.13.3 Darwin/24.3.0
|
Security for sanctum 0.1.1
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.