Metadata-Version: 2.5
Name: cofide-sdk
Version: 0.1.0
Summary: Workload identity for Python: ID tokens, access tokens and mutual TLS with SPIFFE and Credex
Author: Cofide Limited
License-Expression: Apache-2.0
License-File: LICENSE
Keywords: credex,mtls,oauth,spiffe,token-exchange,workload-identity
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: Apache Software License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Programming Language :: Python :: 3.15
Classifier: Topic :: Security
Classifier: Typing :: Typed
Requires-Python: >=3.12
Requires-Dist: httpx2>=2.13.1
Requires-Dist: spiffe<0.4,>=0.3.1
Provides-Extra: anthropic
Requires-Dist: anthropic>=1.9; extra == 'anthropic'
Provides-Extra: aws
Requires-Dist: boto3>=1.34; extra == 'aws'
Provides-Extra: langchain-anthropic
Requires-Dist: anthropic>=1.9; extra == 'langchain-anthropic'
Requires-Dist: langchain-anthropic>=1.0; extra == 'langchain-anthropic'
Provides-Extra: langchain-aws
Requires-Dist: boto3>=1.34; extra == 'langchain-aws'
Requires-Dist: langchain-aws>=0.2; extra == 'langchain-aws'
Description-Content-Type: text/markdown

# cofide-sdk-python

Python SDK for workloads that authenticate with SPIFFE or platform identity (e.g. AWS) and use
[Credex](https://docs.cofide.dev/credex/overview/) to exchange credentials. Its entry point,
`WorkloadIdentityManager`, offers three things:

- **ID tokens** prove the workload's identity: the platform-native one with
  `await workload_identity_manager.id_token(audience)`, or a JWT-SVID with
  `await workload_identity_manager.spiffe_id_token(audience)`.
- **Access tokens** from Credex access a resource, for the workload itself or on a
  caller's behalf: `await workload_identity_manager.access_token(audience, scope)`.
- **Mutual TLS** connects workloads, each presenting its X.509-SVID and authorizing
  the other by SPIFFE ID: `workload_identity_manager.mtls_http_client(server_id=...)`.

Optional adapters connect these to httpx2, LangChain, MCP, uvicorn, Anthropic, and AWS
Bedrock. The building blocks behind `WorkloadIdentityManager` (identity token sources,
[RFC 8693](https://datatracker.ietf.org/doc/html/rfc8693) token exchange, SPIFFE mTLS)
are in submodules for the cases it doesn't cover.

> **Preview.** This SDK is in preview: the API may change between releases without notice.

## Features

- **Identity token sources**: JWT-SVIDs from the SPIFFE Workload API, or platform identity
  tokens (AWS STS, OIDC) federated through Credex.
- **Access tokens**: RFC 8693 exchange against Credex for access tokens to
  OAuth-protected APIs, including delegation on a caller's behalf.
- **HTTP authentication**: `httpx2` authentication that fetches a valid token for every
  request.
- **Mutual TLS**: `httpx2` clients, `ssl.SSLContext`s, and `uvicorn` servers that present
  the workload's X.509-SVID, rotate it automatically, and authorize peers by SPIFFE ID.
- **Integrations**: Anthropic and AWS workload identity federation (WIF), LangChain
  chat models for Anthropic and Bedrock, and authenticated LangChain MCP connections.

## Usage

```python
import asyncio

import httpx2
from cofide_sdk import WorkloadIdentityManager


async def main() -> None:
    # Configured from the environment.
    async with WorkloadIdentityManager() as workload_identity_manager:
        # Prove the workload's identity to a service that accepts it directly.
        auth = workload_identity_manager.id_token_auth("https://proxy.example.com")
        async with httpx2.AsyncClient(auth=auth) as http:
            await http.get("https://proxy.example.com/things")

        # Access an OAuth-protected API with a token from Credex.
        auth = workload_identity_manager.access_token_auth(
            "https://api.example.com", ["things:read"],
        )
        async with httpx2.AsyncClient(auth=auth) as http:
            await http.get("https://api.example.com/things")

        # Call another workload over mutual TLS.
        async with workload_identity_manager.mtls_http_client(
            server_id="spiffe://example.org/ledger",
        ) as http:
            await http.get("https://ledger:8443/accounts")


asyncio.run(main())
```

## Install

Requires Python 3.12 or later.

```sh
pip install cofide-sdk
```

Optional extras, e.g. `pip install "cofide-sdk[aws]"`:

| Extra | Adds |
|---|---|
| `aws` | boto3, for the `aws_sts` identity token source and AWS WIF support |
| `anthropic` | Anthropic SDK, for Anthropic WIF support |
| `langchain-anthropic` | Anthropic SDK and `langchain-anthropic`, for the `chat_anthropic` LangChain adapter |
| `langchain-aws` | boto3 and `langchain-aws`, for the `chat_bedrock` LangChain adapter |

MCP connections need no extra; install `langchain[mcp]` to use them with LangChain's
MCP adapter.

## Configuration

`WorkloadIdentityManager()` loads its configuration from environment variables with
`WorkloadIdentityConfig.from_env()`. Alternatively, pass a `WorkloadIdentityConfig` built directly; the
environment is then not read.

| Variable | `WorkloadIdentityConfig` field | Purpose |
|---|---|---|
| `COFIDE_IDENTITY_TOKEN_SOURCE` | `identity_token_source` | `spiffe` (default), `aws_sts`, or `static`; see below |
| `COFIDE_TOKEN_ENDPOINT` | `token_endpoint` | Credex OAuth token endpoint, e.g. `https://credex.example.com/token` |
| `COFIDE_CREDEX_URL` | `credex_url` | Default ID token audience for integrations that take an optional one, e.g. a Cofide-protected proxy |
| `COFIDE_BEARER_TOKEN` | `bearer_token` | Bearer token for the `static` source; not recommended |
| `SPIFFE_ENDPOINT_SOCKET` | `socket_path` | Workload API socket, for the `spiffe` source and mutual TLS; default `unix:///tmp/spire-agent/public/api.sock` |
| `COFIDE_SPIFFE_ID` | `spiffe_id` | The workload's SPIFFE ID, for ID tokens from the `spiffe` source and for mutual TLS, when the Workload API issues several; default: the Workload API chooses |
| `COFIDE_WORKLOAD_API_TIMEOUT` | `workload_api_timeout` | Workload API timeout in seconds; default `5` |
| `ANTHROPIC_WIF_FEDERATION_RULE_ID`, `ANTHROPIC_WIF_ORGANIZATION_ID`, `ANTHROPIC_WIF_SERVICE_ACCOUNT_ID` | `anthropic` | Enable Anthropic WIF; all three are required together |
| `ANTHROPIC_WIF_WORKSPACE_ID` | `anthropic.workspace_id` | Optional Anthropic workspace |
| `ANTHROPIC_WIF_AUDIENCE` | `anthropic.audience` | JWT audience for Anthropic; default `https://api.anthropic.com` |
| `AWS_REGION`, `AWS_DEFAULT_REGION` | `aws_sts_region`, `aws.region` | Region for the `aws_sts` STS client (unset: boto3's default) and for AWS WIF (unset: `us-east-1`) |
| `AWS_WIF_ROLE_ARN` | `aws.role_arn` | Enable AWS WIF with this role |
| `AWS_WIF_AUDIENCE` | `aws.audience` | JWT audience for AWS WIF; default `sts.amazonaws.com` |
| `AWS_WIF_ROLE_SESSION_NAME` | `aws.session_name` | AWS WIF role session name; default `cofide-workload` |

`identity_token_source` selects where the workload's ID tokens come from:

| Source | Behavior |
|---|---|
| `spiffe` (default) | Requires the Workload API socket |
| `aws_sts` | Requires AWS identity (boto3's standard credential chain); ID tokens are STS web identity tokens, and JWT-SVIDs (`spiffe_id_token()`) are issued by Credex at `token_endpoint` in exchange for them |
| `static` | Requires `bearer_token`, presented as the ID token for any audience; not recommended |

To use another identity token source, pass it to
`WorkloadIdentityManager(config, identity_token_source=...)`.

## Licence

Copyright 2026 Cofide Limited.

Licensed under the Apache License, Version 2.0.
