Skip to main content

cred-auth

Python SDK for Cred. OAuth2 credential delegation for AI agents. Supports both legacy short-lived access tokens and brokered Delegation Tokens (DPoP-bound).

Install

pip install cred-auth

Quick Start

import os
from cred import Cred, ConsentRequiredError

cred = Cred(
    agent_token=os.environ["CRED_AGENT_TOKEN"],
    base_url=os.environ["CRED_BASE_URL"],
)

try:
    result = cred.delegate(
        service="google",
        user_id="user_123",
        app_client_id="my_app_client_id",
        scopes=["calendar.readonly"],
    )
    print(result.access_token)
except ConsentRequiredError as e:
    # Redirect user to e.consent_url to complete OAuth consent
    print(f"Redirect user to: {e.consent_url}")

For brokered server-side use, the server issues a Delegation Token (a JWT) bound to the agent's own did:key. delegate_handle() requires an agent_identity; every use() carries a DPoP proof (RFC 9449) signed by the agent, so a token copied out of a log is useless without the agent's private key. delegation_id (the token's jti) is only for revocation and logging; use result.token to call use().

from cred import AgentIdentity, generate_agent_identity

identity = generate_agent_identity()  # persist with identity.export(); reload with AgentIdentity.from_export()

handle = cred.delegate_handle(
    service="google",
    user_id="user_123",
    app_client_id="my_app_client_id",
    scopes=["calendar.readonly"],
    agent_identity=identity,
)

events = cred.use(
    handle.token,
    url="https://www.googleapis.com/calendar/v3/calendars/primary/events",
    agent_identity=identity,
)

API

Cred(agent_token, base_url)

delegate(service, user_id, app_client_id, scopes=None) → DelegationResult

Get a delegated access token for a service on behalf of a user.

Raises ConsentRequiredError (with .consent_url) if the user hasn't connected the service.

delegate_handle(service, user_id, app_client_id, scopes=None, agent_did=None, agent_identity=None) → DelegationHandleResult

Get a brokered Delegation Token for a service. The provider access token stays on the Cred server. The result has token, jkt (key thumbprint), token_type ("DPoP"), delegation_id, receipt, expires_in, expires_at, service, user_id and scopes.

agent_identity is required (CredError with code invalid_config otherwise, before any HTTP call). The token is issued to that identity's did:key (agent_did is set from it; passing a different agent_did raises CredError) and the request carries a DPoP proof of the key.

use(token, url, method="GET", body=None, extra_headers=None, *, agent_identity) → BrokeredUseResult

Broker an upstream API request through the Cred server with a Delegation Token (DelegationHandleResult.token), sent as Authorization: DPoP <token>. The server enforces service URL allowlists, delegated scopes, and any configured Guard policies before forwarding.

token and the same agent_identity the token was issued to are required (invalid_config otherwise). The DPoP proof commits to the token (ath, SHA-256 of the token) and to this exact request (cred_req: URL, method, body and extra headers).

create_dpop_proof(identity, htm, htu, handle=None, request_body=None) → str

Build a DPoP proof (compact JWS, typ: dpop+jwt, EdDSA) for one request, signed with the agent's Ed25519 key. htu is the request URL without query or fragment; handle (the token string for /api/v1/use) adds ath, its base64url SHA-256. request_body adds cred_req, the base64url SHA-256 of the RFC 8785 (JCS) serialization of the whole JSON body sent, so the proof authorizes that one request; use() sets it for you. canonical_json(value) exposes the JCS serializer. The client methods above call this for you; make a new proof per request, since the server rejects a replayed jti or a proof older than two minutes.

get_user_connections(user_id, app_client_id=None) → list[Connection]

List all active service connections for a user.

Build a consent URL. Pure URL construction, no HTTP call.

revoke(service, user_id, app_client_id=None) → None

Revoke a user's connection to a service.

Context Manager

with Cred(agent_token=token, base_url=base_url) as cred:
    result = cred.delegate(service="github", user_id="u1", app_client_id="app1")

Self-Hosted Server Mode

Point base_url at your own @credninja/server deployment when you want a separate credential broker instead of local-only usage.

Metadata

Release files for cred-auth 2.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for cred-auth 2.0.1
File Size Uploaded Provenance Actions
cred_auth-2.0.1.tar.gz 34.7 kB Without provenance Details for cred_auth-2.0.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for cred-auth 2.0.1
File Interpreter ABI Platform Actions
cred_auth-2.0.1-py3-none-any.whl Python 3 none any Details for cred_auth-2.0.1-py3-none-any.whl

Total release size: 54.3 kB

Release files / cred_auth-2.0.1.tar.gz

Download URL cred_auth-2.0.1.tar.gz
Size 34.7 kB
Tags Source
SHA-256 checksum
How to use checksums
2d50aaa9e35c1480953b86920313d48cc5b28b6e332590d02bf0f6681c2a834e
BLAKE2b-256 checksum
How to use checksums
688e5d36b33a83ffb7a7daee97d7573435ed0ae46a690e60f33b800aca797ad3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.15

Release files / cred_auth-2.0.1-py3-none-any.whl

Download URL cred_auth-2.0.1-py3-none-any.whl
Size 19.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f158ec6685f1f0fe692e3ed8586c5016b3f83bb81cf7a07b165c41fd5dde605f
BLAKE2b-256 checksum
How to use checksums
f5cb3ce640f45b67c44dfbec3a92c5bcefb59b71643101e2c38cf79762502583
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.15

Security for cred-auth 2.0.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page