Skip to main content

Multi-tenant authentication and authorization Python SDK supporting OAuth2, OpenID Connect, API keys, and multiple authentication methods

Project description

Binoauth Python SDK

A multi-tenant authentication and authorization Python SDK that supports OAuth2, OpenID Connect, API keys, and multiple authentication methods.

Features

  • Admin API: Management endpoints for tenants, clients, users, API keys, and provider settings
  • Tenant API: Authentication endpoints for login, signup, OAuth2 flows, magic links, phone OTP, and user profile management
  • Multiple Authentication Methods: API keys, Bearer tokens, session cookies
  • Auto-generated from OpenAPI: Always up-to-date with the latest API specifications
  • Type-safe: Full type hints and Pydantic models
  • Python 3.9+: Modern Python support

Installation

pip install binoauth

For development:

pip install -e .[dev]

Quick Start

Admin API Usage

from binoauth import BinoauthAdmin

# Initialize admin client
admin = BinoauthAdmin(
    host="https://api.auth.example.com",
    access_token="your_admin_token"
)

# List tenants
tenants = admin.api.list_tenants_api_v1_tenants_get()

# Create a new client
client_data = {
    "name": "My App",
    "redirect_uris": ["https://myapp.com/callback"]
}
new_client = admin.api.create_client_api_v1_clients_post(client_data)

Tenant API Usage

from binoauth import BinoauthTenant

# Initialize tenant client
tenant = BinoauthTenant(
    host="https://tenant.auth.example.com",
    api_key="your_api_key"
)

# User signup
signup_data = {
    "email": "user@example.com",
    "password": "secure_password"
}
response = tenant.auth.signup_api_v1_auth_signup_post(signup_data)

# OAuth2 authorization
auth_url = tenant.oauth2.authorize_api_v1_oauth2_authorize_get(
    client_id="your_client_id",
    redirect_uri="https://yourapp.com/callback",
    response_type="code"
)

Context Manager Usage

# Both classes support context managers
with BinoauthAdmin(host="...", access_token="...") as admin:
    tenants = admin.api.list_tenants_api_v1_tenants_get()

with BinoauthTenant(host="...", api_key="...") as tenant:
    response = tenant.auth.login_api_v1_auth_login_post(login_data)

Development Setup

Prerequisites

  • Python 3.9+
  • Git
  • OpenAPI Generator CLI (for code generation)

Development Installation

  1. Clone the repository:
git clone <repository-url>
cd binoauth-python-sdk
  1. Install development dependencies:
pip install -e .[dev]
  1. Install pre-commit hooks:
pre-commit install

Development Commands

Code Generation

# Regenerate SDK from latest OpenAPI specs
./codegen.sh

Testing

# Run all tests
python -m pytest

# Run tests with coverage
python -m pytest --cov=binoauth

# Run specific test file
python -m pytest binoauth/admin/test/test_admin_api.py

Code Quality & Linting

# Format code
black binoauth/

# Sort imports
isort binoauth/

# Lint code
flake8 binoauth/

# Type checking (manual only, auto-generated code conflicts)
mypy binoauth/__init__.py --ignore-missing-imports --allow-untyped-calls

# Run all quality checks
pre-commit run --all-files

Version Management

# Bump patch version (1.0.0 → 1.0.1)
bump-my-version bump patch

# Bump minor version (1.0.0 → 1.1.0)
bump-my-version bump minor

# Bump major version (1.0.0 → 2.0.0)
bump-my-version bump major

# Show what would be changed (dry run)
bump-my-version bump --dry-run --verbose patch

Package Publishing

# Install publishing tools
pip install build twine

# Build the package
python -m build

# Upload to Test PyPI (recommended first)
twine upload --repository testpypi dist/*

# Upload to production PyPI
twine upload dist/*

# Check package on PyPI
pip install binoauth  # test installation

# Automated release (recommended)
./release.sh patch  # or minor/major

Release Process

The project includes an automated release script that handles the complete publishing workflow:

# Make sure you're on main branch with clean working directory
git checkout main
git pull origin main

# Run automated release (handles testing, building, and publishing)
./release.sh patch   # for bug fixes (1.0.0 → 1.0.1)
./release.sh minor   # for new features (1.0.0 → 1.1.0)
./release.sh major   # for breaking changes (1.0.0 → 2.0.0)

The release script:

  1. Verifies you're on main branch with clean working directory
  2. Runs all tests and quality checks
  3. Shows version bump preview and asks for confirmation
  4. Bumps version and creates git commit/tag
  5. Builds the package (source + wheel)
  6. Uploads to Test PyPI first for verification
  7. Asks for final confirmation before production upload
  8. Uploads to production PyPI
  9. Pushes git tag to remote repository

Prerequisites for publishing:

  • PyPI account with API tokens
  • Configure ~/.pypirc with your tokens (see .pypirc.template)

## Architecture

### Package Structure

```shell
binoauth/
├── __init__.py                 # Main SDK with convenience wrappers
├── admin/                      # Admin API (auto-generated)
│   ├── api/                    # API endpoint classes
│   ├── models/                 # Pydantic data models
│   ├── docs/                   # Auto-generated documentation
│   └── test/                   # Unit tests
└── tenant/                     # Tenant API (auto-generated)
    ├── api/                    # API endpoint classes
    ├── models/                 # Pydantic data models
    ├── docs/                   # Auto-generated documentation
    └── test/                   # Unit tests

High-Level Design

The SDK provides two main convenience wrapper classes:

  • BinoauthAdmin: Wraps the admin API for tenant/client/user management
  • BinoauthTenant: Wraps the tenant API for authentication operations

Both classes handle:

  • Configuration management
  • Multiple authentication methods
  • Context manager support
  • Error handling

Code Generation

The binoauth/admin/ and binoauth/tenant/ directories contain auto-generated code from OpenAPI specifications. Never manually edit files in these directories.

The ./codegen.sh script:

  1. Downloads latest OpenAPI specs from production endpoints
  2. Generates Python clients using OpenAPI Generator
  3. Maintains unified package structure
  4. Preserves manually maintained convenience wrappers

Authentication Methods

1. API Keys (Recommended for Backend Services)

tenant = BinoauthTenant(
    host="https://tenant.auth.example.com",
    api_key="your_api_key"
)

2. Bearer Tokens (OAuth2/JWT)

admin = BinoauthAdmin(
    host="https://api.auth.example.com",
    access_token="your_jwt_token"
)

3. Session Cookies

Session authentication is handled automatically by the underlying HTTP client when cookies are present.

Contributing

  1. Code Style: This project uses Black (88 character line length) and isort for formatting
  2. Type Hints: All new code should include proper type annotations
  3. Testing: Write tests for new functionality in the appropriate test directories
  4. Linting: All code must pass flake8, mypy, and other quality checks
  5. Pre-commit: Install and use pre-commit hooks for automated quality checks

Important Notes

  • Auto-generated Code: Never edit files in binoauth/admin/ or binoauth/tenant/ directories
  • Version Management: Use bump-my-version for version updates
  • Dependencies: Keep core dependencies minimal; add development tools to [dev] extra
  • Multi-tenant Architecture: Admin API operates on public tenant, Tenant API is tenant-specific

License

MIT

Support

coming soon!

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

binoauth-0.0.4.tar.gz (104.5 kB view details)

Uploaded Source

Built Distribution

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

binoauth-0.0.4-py3-none-any.whl (323.1 kB view details)

Uploaded Python 3

File details

Details for the file binoauth-0.0.4.tar.gz.

File metadata

  • Download URL: binoauth-0.0.4.tar.gz
  • Upload date:
  • Size: 104.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.12

File hashes

Hashes for binoauth-0.0.4.tar.gz
Algorithm Hash digest
SHA256 0f2431a013d7aea819dae4f00ee61ae4d59949794dd8d26abf54f4959f602b26
MD5 9c3a0957007c93a5252a1c6b77d3e256
BLAKE2b-256 a2804cbed99b1adac6d5ee3a00396ddd2182f43d1eb436023a676c04de4b69cd

See more details on using hashes here.

File details

Details for the file binoauth-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: binoauth-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 323.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.12

File hashes

Hashes for binoauth-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 38774c09c0186d0ecc29820fa4b0bf000d1c2d422279ab0e79e41e46d88f9141
MD5 6e5731c3bfaff3e49df1380efd431ef0
BLAKE2b-256 f50aba9b0ea9e5b839d28750ea4cd008d005eaa2a9dbaecac625c9ba85bbcd6e

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