Skip to main content

Python bindings for the Hessra authorization system with Biscuit token verification and mTLS support

Project description

Hessra Python SDK

Python bindings for the Hessra authentication and authorization system.

Overview

This Python package provides bindings to the Rust-based Hessra SDK, enabling Python applications to:

  • Verify Biscuit authorization tokens (locally and remotely)
  • Manage service chain token attestation
  • Handle mTLS-authenticated communication with Hessra services
  • Configure and manage authorization settings

Note: This library is focused on token verification and service chain operations. Token creation is handled by the Hessra authorization service.

Installation

From PyPI

# With uv
uv add hessra-py

# With pip
pip install hessra-py

From Source

cd hessra-py

# Sync development dependencies (includes maturin)
uv sync --dev

# Build and install in development mode
uv run maturin develop

# Or build wheel and install
uv run maturin build --release
uv add target/wheels/hessra_py-*.whl

Requirements

  • Python 3.8+ (recommended: Python 3.12)
  • uv for Python package management
  • Rust toolchain (for building from source)

Quick Start

The Python SDK connects to your Hessra authorization service using the same test certificates as the Rust SDK examples.

import hessra_py

# Load certificates (same as used in Rust SDK examples)
mtls_cert = open("../certs/client.crt").read()
mtls_key = open("../certs/client.key").read()
server_ca = open("../certs/ca-2030.pem").read()

# Create configuration
config = (hessra_py.HessraConfig.builder()
    .base_url("test.hessra.net")
    .port(443)
    .protocol("http1")
    .mtls_cert(mtls_cert)
    .mtls_key(mtls_key)
    .server_ca(server_ca)
    .build())

# Create client and setup with public key
client = hessra_py.HessraClient(config)
client_with_key = client.setup_new()

# Verify a token locally (requires real Biscuit token)
client_with_key.verify_token_local(
    token="<biscuit_token_base64>",
    subject="uri:urn:test:argo-cli0",
    resource="resource1",
    operation="read"
)

# Create configuration from environment variables
config = hessra_py.HessraConfig.from_env()

# Or build configuration manually
config = (hessra_py.HessraConfig.builder()
    .base_url("your-auth-service.com")
    .port(443)
    .protocol("http1")
    .mtls_cert(your_client_cert)
    .mtls_key(your_client_key)
    .server_ca(your_server_ca)
    .public_key(your_public_key)
    .build())

# Create client
client = hessra_py.HessraClient(config)

# Setup client (fetches public key if needed)
client = client.setup_new()

# Verify a token locally
client.verify_token_local(
    token="your_base64_token",
    subject="user_id",
    resource="api/endpoint",
    operation="read"
)

# Verify token remotely via API
result = client.verify_token_remote(
    token="your_base64_token",
    subject="user_id",
    resource="api/endpoint",
    operation="read"
)
print(f"Verification result: {result}")

Service Chain Operations

For complex authorization flows involving multiple services:

# Attest a token with your service
attested_token = client.attest_service_chain_token(
    token="original_token",
    service="my-service"
)

# Verify a service chain token
service_chain_json = '''[
    {
        "component": "auth-service",
        "public_key": "ed25519/public_key_1"
    },
    {
        "component": "payment-service",
        "public_key": "ed25519/public_key_2"
    }
]'''

client.verify_service_chain_token_local(
    token=attested_token,
    subject="user_id",
    resource="api/endpoint",
    operation="read",
    service_chain_json=service_chain_json,
    component=None  # Verify entire chain
)

Configuration

Environment Variables

The library supports loading configuration from environment variables:

export HESSRA_BASE_URL="your-service.com"
export HESSRA_PORT="443"
export HESSRA_PROTOCOL="http1"
export HESSRA_MTLS_CERT="$(cat client.crt)"
export HESSRA_MTLS_KEY="$(cat client.key)"
export HESSRA_SERVER_CA="$(cat ca.crt)"
export HESSRA_PUBLIC_KEY="$(cat public.key)"
export HESSRA_PERSONAL_KEYPAIR="$(cat personal.key)"

Then load with:

config = hessra_py.HessraConfig.from_env()

Builder Pattern

All configuration and client objects support a fluent builder pattern:

config = (hessra_py.HessraConfig.builder()
    .base_url("service.com")
    .port(443)
    .protocol("http1")  # or "http3"
    .mtls_cert(cert_string)
    .mtls_key(key_string)
    .server_ca(ca_string)
    .public_key(public_key_string)
    .personal_keypair(keypair_string)
    .build())

client = (hessra_py.HessraClient.builder()
    .base_url("service.com")
    .port(443)
    # ... other configuration
    .build())

Error Handling

All operations can raise HessraPyError:

try:
    client.verify_token_local(token, subject, resource, operation)
    print("Token is valid")
except hessra_py.HessraPyError as e:
    print(f"Verification failed: {e}")

API Reference

HessraConfig

Configuration object for Hessra services.

Methods:

  • from_env() - Load from environment variables
  • builder() - Create configuration builder
  • Properties: base_url, port, protocol, public_key

HessraClient

Main client for interacting with Hessra services.

Methods:

  • __init__(config) - Create client with configuration
  • setup_new() - Setup client and return new instance with public key
  • verify_token_local(token, subject, resource, operation) - Verify token locally
  • verify_token_remote(token, subject, resource, operation) - Verify token via API
  • get_public_key() - Fetch public key from service
  • attest_service_chain_token(token, service) - Add service attestation to token
  • verify_service_chain_token_local(...) - Verify service chain token locally
  • verify_service_chain_token_remote(...) - Verify service chain token via API

Examples

See the examples/ directory for complete usage examples:

  • basic_usage.py - Basic token verification and configuration
  • test_verification.py - Test suite demonstrating all features

Development

Building and testing the Python bindings:

# Clone and navigate to the project
cd hessra-py

# Initialize uv project and sync dependencies
uv sync --dev

# Build in development mode
uv run maturin develop

# Run example tests
uv run python examples/basic_usage.py
uv run python examples/test_verification.py

# Run tests with pytest (when available)
uv run pytest

# Build release wheel
uv run maturin build --release

# Check the built wheel
ls -la target/wheels/

Development Workflow

The Python bindings use uv for fast dependency management and maturin for building the Rust extension:

  1. Initial setup: uv sync --dev installs all development dependencies
  2. Development builds: uv run maturin develop builds and installs for testing
  3. Running examples: uv run python examples/... executes Python scripts
  4. Release builds: uv run maturin build --release creates optimized wheels

Publishing and Releases

This package is published to PyPI independently from the main Hessra SDK. See RELEASE.md for the complete release process.

For Maintainers

To release a new version:

  1. Update version in Cargo.toml
  2. Commit changes and create a tag: git tag hessra-py-v0.1.1
  3. Push the tag: git push origin hessra-py-v0.1.1
  4. GitHub Actions will automatically build and publish to PyPI

License

Apache License 2.0 - See LICENSE file for details.

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

hessra_py-0.2.0.tar.gz (151.0 kB view details)

Uploaded Source

Built Distributions

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

hessra_py-0.2.0-cp38-abi3-win_amd64.whl (2.9 MB view details)

Uploaded CPython 3.8+Windows x86-64

hessra_py-0.2.0-cp38-abi3-win32.whl (2.5 MB view details)

Uploaded CPython 3.8+Windows x86

hessra_py-0.2.0-cp38-abi3-manylinux_2_28_aarch64.whl (3.5 MB view details)

Uploaded CPython 3.8+manylinux: glibc 2.28+ ARM64

hessra_py-0.2.0-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (3.7 MB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ x86-64

hessra_py-0.2.0-cp38-abi3-macosx_11_0_arm64.whl (3.1 MB view details)

Uploaded CPython 3.8+macOS 11.0+ ARM64

hessra_py-0.2.0-cp38-abi3-macosx_10_12_x86_64.whl (3.3 MB view details)

Uploaded CPython 3.8+macOS 10.12+ x86-64

File details

Details for the file hessra_py-0.2.0.tar.gz.

File metadata

  • Download URL: hessra_py-0.2.0.tar.gz
  • Upload date:
  • Size: 151.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for hessra_py-0.2.0.tar.gz
Algorithm Hash digest
SHA256 2207ebc9e6c094b747fdf9ef4a4e90616f8ae690a3e73f7f2c426b7ea6827582
MD5 382bf43c94a8ca80a912f18a0fa4426f
BLAKE2b-256 9997f5d747d9cebe62738bf0bd0040fd210ea2bdd6f5066e0eb21d47aab36a33

See more details on using hashes here.

Provenance

The following attestation bundles were made for hessra_py-0.2.0.tar.gz:

Publisher: publish-python.yml on Hessra-Labs/hessra-sdk.rs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file hessra_py-0.2.0-cp38-abi3-win_amd64.whl.

File metadata

  • Download URL: hessra_py-0.2.0-cp38-abi3-win_amd64.whl
  • Upload date:
  • Size: 2.9 MB
  • Tags: CPython 3.8+, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for hessra_py-0.2.0-cp38-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 5cfd0cf261647673eae3827ca867bda85ebc317d8801e1166673fcac1d2dd286
MD5 418cf5b1e0372f0341f3c770fdd54b6f
BLAKE2b-256 5850ee05f6833f76ace0dff639bd7aed7bd73f5f943bb8a0c24f28d0e122749d

See more details on using hashes here.

Provenance

The following attestation bundles were made for hessra_py-0.2.0-cp38-abi3-win_amd64.whl:

Publisher: publish-python.yml on Hessra-Labs/hessra-sdk.rs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file hessra_py-0.2.0-cp38-abi3-win32.whl.

File metadata

  • Download URL: hessra_py-0.2.0-cp38-abi3-win32.whl
  • Upload date:
  • Size: 2.5 MB
  • Tags: CPython 3.8+, Windows x86
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for hessra_py-0.2.0-cp38-abi3-win32.whl
Algorithm Hash digest
SHA256 e3ad603f1e2f2e3da84899e60f128bd6984b56d7d5587425c5c33e2d567bd215
MD5 5aba7d6f78ee6c851421ced15308b152
BLAKE2b-256 3f0b1d34b15695df5bb2b75048ad741318e672abdd06525140c4fd402cac97bd

See more details on using hashes here.

Provenance

The following attestation bundles were made for hessra_py-0.2.0-cp38-abi3-win32.whl:

Publisher: publish-python.yml on Hessra-Labs/hessra-sdk.rs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file hessra_py-0.2.0-cp38-abi3-manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for hessra_py-0.2.0-cp38-abi3-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 f52684d8b08542d4ac9c0b65f210f2ca99792a238e75dce0cea06b7c56bd42f8
MD5 ae93fb3cd876de7299f0841cb7aba4a5
BLAKE2b-256 87af32dc740cec8c74042801d9ecf091d3149c5531080dd6ea32b2de115664bf

See more details on using hashes here.

Provenance

The following attestation bundles were made for hessra_py-0.2.0-cp38-abi3-manylinux_2_28_aarch64.whl:

Publisher: publish-python.yml on Hessra-Labs/hessra-sdk.rs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file hessra_py-0.2.0-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for hessra_py-0.2.0-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 e338b08871b85f83b1ab5cf8ac889cccab0e71d17f495ed9b3311e6f4b6d5a09
MD5 5790407d3fed685ceab512f704aef108
BLAKE2b-256 efa78fff2c557553c65750d99ba3e159678beef3327acb56dc5e949da61afc9d

See more details on using hashes here.

Provenance

The following attestation bundles were made for hessra_py-0.2.0-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: publish-python.yml on Hessra-Labs/hessra-sdk.rs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file hessra_py-0.2.0-cp38-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for hessra_py-0.2.0-cp38-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 ec2f1ee76eb093061c84f7d185c8d1426609a7c60accf29b3280d4801960f9e6
MD5 23d9078ece7d4be0c1db296d15233919
BLAKE2b-256 b2b1a28727c9c2a6a45db164984cdc870370a16b33bcc80582b09df04992efb8

See more details on using hashes here.

Provenance

The following attestation bundles were made for hessra_py-0.2.0-cp38-abi3-macosx_11_0_arm64.whl:

Publisher: publish-python.yml on Hessra-Labs/hessra-sdk.rs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file hessra_py-0.2.0-cp38-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for hessra_py-0.2.0-cp38-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 33b695bbe43eb57778dc1f31097692450f8dfe14565e92dd71c2c0225d283e7a
MD5 2a12942bb153e52aaafef40407c5a112
BLAKE2b-256 b335bb1fbe90260d0e490a92467d9389aebf9ef341c45f7a2c287ff80eddaf79

See more details on using hashes here.

Provenance

The following attestation bundles were made for hessra_py-0.2.0-cp38-abi3-macosx_10_12_x86_64.whl:

Publisher: publish-python.yml on Hessra-Labs/hessra-sdk.rs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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