Skip to main content

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-id headers
  • 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 exists
  • get_user(user_id: str) -> Optional[User] - Get user by ID
  • get_user_by_email(email: str) -> Optional[User] - Get user by email
  • get_user_by_username(username: str) -> Optional[User] - Get user by username
  • create_user(email, username, role, display_name) -> User - Create new user
  • get_or_create_user(email, username, role, display_name) -> User - Get or create user
  • verify_user_role(user_id, required_roles) -> bool - Check user role
  • update_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-cosmos package

📄 License

MIT License

👤 Author

Your Company Name

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

cosmos_auth_package-1.0.0.tar.gz (7.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cosmos_auth_package-1.0.0-py3-none-any.whl (7.6 kB view details)

Uploaded Python 3

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

Hashes for cosmos_auth_package-1.0.0.tar.gz
Algorithm Hash digest
SHA256 6b636f5686d7124f9f7639ad34cb325d24996617e50892304321be397c959a9e
MD5 7403f97a613d60096247b1d8e2594bea
BLAKE2b-256 a1532dd2d30fd2e9f6db92bc5c52f7c52c1ffdc3ad525ed76463a04c3774bbc6

See more details on using hashes here.

File details

Details for the file cosmos_auth_package-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for cosmos_auth_package-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ab8895092f447a8b1b59fc6d57ccce000ca2a231801922f181403240590afc6f
MD5 c50c874019504a3b04d4050d2902baf0
BLAKE2b-256 d9393184971d7b8f21a2d48dcbd449246cc45481d906d2049d5d3d367477d7b3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page