Skip to main content

Agentic Identity Framework abstracting Keycloak/OIDC complexity for AI Agents

Project description

SecureAgent

A "Convention over Configuration" Python package for adding Agentic Identity to your AI Agents.

This package abstracts away the complexity of:

  • Dynamic Client Registration: Automatically registers your agent with Keycloak if no credentials exist.
  • Token Exchange: Implements RFC 8693 logic to exchange user tokens for downstream service access.
  • Token Verification: Provides FastAPI dependencies to validate incoming Bearer tokens.
  • Token Caching: Automatic token caching with refresh-before-expiry logic.
  • Async Support: Non-blocking async methods for FastAPI and aiohttp.
  • CLI Tools: Bootstrap credentials easily with secureagent init.

Installation

pip install SecureAgent

Note: You may need to install from source or a private repository until published.

Quick Start with CLI

Bootstrap your agent's credentials using the CLI:

secureagent init --realm-url http://localhost:8080 --client-id my-agent

This will prompt for an Initial Access Token and create credentials.json.

Usage

Initialization

Initialize the security module with your realm URL and service details.

from SecureAgent import AgentSecurity

security = AgentSecurity(
    realm_url="http://localhost:8080",
    service_name="my-specialist-agent",
    # initial_access_token is required only for first run to register the client
    initial_access_token="<YOUR_INITIAL_ACCESS_TOKEN>",
    # Optional: graceful degradation for development
    fail_open=False,  # Set True to return None instead of raising on errors
    cache_tokens=True  # Automatic token caching (default: True)
)

Getting a Token (Client Credentials Flow)

# Synchronous
token = security.get_token()

# Asynchronous (for FastAPI, aiohttp, etc.)
token = await security.get_token_async()

Protecting an Endpoint

Use the verify_token dependency to protect your FastAPI routes.

from fastapi import FastAPI, Depends

app = FastAPI()

@app.get("/secure-data")
def secure_endpoint(token_payload = Depends(security.verify_token)):
    return {
        "user": token_payload["sub"],
        "message": "You have access!"
    }

Exchanging Tokens (The Orchestrator Pattern)

If your agent needs to call another agent, use exchange_token.

# Synchronous
downstream_token = security.exchange_token(
    user_token=user_token,
    target_client="target-service"
)

# Asynchronous
downstream_token = await security.exchange_token_async(
    user_token=user_token,
    target_client="target-service"
)

# Use the new token to make the request
headers = {"Authorization": f"Bearer {downstream_token}"}

Cache Management

# Clear all cached tokens
security.clear_cache()

# Clear specific cache entry
security.clear_cache("client_credentials")

CLI Commands

Command Description
secureagent init Register a new client and create credentials
secureagent status Check credentials and Keycloak connectivity
secureagent token Get an access token (for scripting)

Configuration Options

Parameter Default Description
realm_url required Keycloak server URL
service_name required Client ID for this agent
realm_name "agent-mesh" Keycloak realm name
creds_file "credentials.json" Path to store credentials
fail_open False Return None instead of raising on errors
cache_tokens True Enable automatic token caching
cache_refresh_buffer 30 Seconds before expiry to refresh

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

secureagent-0.1.3.tar.gz (18.6 kB view details)

Uploaded Source

Built Distribution

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

secureagent-0.1.3-py3-none-any.whl (16.9 kB view details)

Uploaded Python 3

File details

Details for the file secureagent-0.1.3.tar.gz.

File metadata

  • Download URL: secureagent-0.1.3.tar.gz
  • Upload date:
  • Size: 18.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for secureagent-0.1.3.tar.gz
Algorithm Hash digest
SHA256 23d93a96e2ea28a27c7fc32d4fb3039ecb1f4da284616c2012a6c137546d2a9e
MD5 4538c5644a16dfad62b39ea127b46e6e
BLAKE2b-256 e8c3884296d4f9b33db3c47545ef7f4119fe07f1be2b3f5383a0bf2f698b08b6

See more details on using hashes here.

Provenance

The following attestation bundles were made for secureagent-0.1.3.tar.gz:

Publisher: test.yml on armstrongsam25/SecureAgent

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

File details

Details for the file secureagent-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: secureagent-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 16.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for secureagent-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 e785b8c7b58a06da7c603ce0568f63adf057efbc86d1152b6a4403721bd6deb9
MD5 ab019750e4123e0c8af02b16febbd06f
BLAKE2b-256 d2b034f5a17e5868a669c492499d34d3acdd0e512272168ae0a3d6cbae75e166

See more details on using hashes here.

Provenance

The following attestation bundles were made for secureagent-0.1.3-py3-none-any.whl:

Publisher: test.yml on armstrongsam25/SecureAgent

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