Azure Cosmos DB authentication and user verification package with Flask and FastAPI support. Provides easy-to-use decorators and dependencies for header-based authentication, user verification, role-based access control, and automatic user creation.
Project description
Cosmos Auth Package
Azure Cosmos DB authentication and user verification package for Python. Provides easy-to-use authentication decorators and user management for Flask and FastAPI applications.
🎯 Features
- ✅ User Verification: Check if users exist in Cosmos DB
- ✅ Auto-Create Users: Automatically create users if they don't exist
- ✅ Role-Based Access Control: Support for user roles (admin, user, moderator, etc.)
- ✅ Framework Support: Works with Flask and FastAPI
- ✅ Header-Based Auth: Supports
x-user-email,x-user-idheaders - ✅ Flexible: Accepts existing Cosmos DB connection (no connection management needed)
📦 Installation
pip install cosmos-auth-package
# With Flask support
pip install cosmos-auth-package[flask]
# With FastAPI support
pip install cosmos-auth-package[fastapi]
# With both
pip install cosmos-auth-package[all]
🚀 Quick Start
1. Setup Cosmos DB Connection
from azure.cosmos import CosmosClient
# Create your Cosmos DB connection (in your project)
COSMOS_ENDPOINT = "https://your-account.documents.azure.com:443/"
COSMOS_KEY = "your-key"
COSMOS_DATABASE = "your-database"
COSMOS_CONTAINER = "User"
client = CosmosClient(COSMOS_ENDPOINT, COSMOS_KEY)
database = client.get_database_client(COSMOS_DATABASE)
user_container = database.get_container_client(COSMOS_CONTAINER)
2. Initialize Package
from cosmos_auth_package import UserVerifier
# Pass your existing connection
verifier = UserVerifier(user_container)
3. Use in Flask
from flask import Flask, jsonify, g
from cosmos_auth_package import require_auth
app = Flask(__name__)
@app.route('/protected')
@require_auth(verifier, required_roles=['admin'])
def protected_route():
user = g.current_user
return jsonify({
"message": "Access granted",
"user": user.email,
"role": user.role
})
4. Use in FastAPI
from fastapi import FastAPI, Depends
from cosmos_auth_package import get_current_user_fastapi, User
app = FastAPI()
@app.get("/protected")
async def protected_route(
current_user: User = Depends(get_current_user_fastapi(verifier))
):
return {
"message": "Access granted",
"user": current_user.email,
"role": current_user.role
}
📚 API Reference
UserVerifier
Methods
verify_user_exists(user_id: str) -> bool- Check if user existsget_user(user_id: str) -> Optional[User]- Get user by IDget_user_by_email(email: str) -> Optional[User]- Get user by emailget_user_by_username(username: str) -> Optional[User]- Get user by usernamecreate_user(email, username, role, display_name) -> User- Create new userget_or_create_user(email, username, role, display_name) -> User- Get or create userverify_user_role(user_id, required_roles) -> bool- Check user roleupdate_user_role(user_id, new_role) -> bool- Update user role
Decorators
Flask
@require_auth(
verifier,
required_roles=['admin'], # Optional
header_name='x-user-email', # Default
auto_create=False # Auto-create user if doesn't exist
)
FastAPI
# Basic auth
current_user: User = Depends(get_current_user_fastapi(verifier))
# With role check
current_user: User = Depends(require_role_fastapi(verifier, ['admin']))
💡 Use Cases
- API authentication with Cosmos DB
- User verification in microservices
- Role-based access control
- Auto-user creation for new users
- Header-based authentication (API Gateway integration)
🔧 Requirements
- Python 3.8+
- Azure Cosmos DB account
azure-cosmospackage
📄 License
MIT License
👤 Author
Your Company Name
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 cosmos_auth_package-1.0.0.tar.gz.
File metadata
- Download URL: cosmos_auth_package-1.0.0.tar.gz
- Upload date:
- Size: 7.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6b636f5686d7124f9f7639ad34cb325d24996617e50892304321be397c959a9e
|
|
| MD5 |
7403f97a613d60096247b1d8e2594bea
|
|
| BLAKE2b-256 |
a1532dd2d30fd2e9f6db92bc5c52f7c52c1ffdc3ad525ed76463a04c3774bbc6
|
File details
Details for the file cosmos_auth_package-1.0.0-py3-none-any.whl.
File metadata
- Download URL: cosmos_auth_package-1.0.0-py3-none-any.whl
- Upload date:
- Size: 7.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ab8895092f447a8b1b59fc6d57ccce000ca2a231801922f181403240590afc6f
|
|
| MD5 |
c50c874019504a3b04d4050d2902baf0
|
|
| BLAKE2b-256 |
d9393184971d7b8f21a2d48dcbd449246cc45481d906d2049d5d3d367477d7b3
|