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 |
Metadata
Release files for SecureAgent 0.1.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| secureagent-0.1.3.tar.gz | 18.6 kB | With provenance | Details for secureagent-0.1.3.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| secureagent-0.1.3-py3-none-any.whl | Python 3 | none | any | Details for secureagent-0.1.3-py3-none-any.whl |
Total release size: 35.5 kB
Release files / secureagent-0.1.3.tar.gz
| Download URL | secureagent-0.1.3.tar.gz |
|---|---|
| Size | 18.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
23d93a96e2ea28a27c7fc32d4fb3039ecb1f4da284616c2012a6c137546d2a9e
|
|
BLAKE2b-256 checksum How to use checksums |
e8c3884296d4f9b33db3c47545ef7f4119fe07f1be2b3f5383a0bf2f698b08b6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Dec 12, 2025.
Transparency logRelease files / secureagent-0.1.3-py3-none-any.whl
| Download URL | secureagent-0.1.3-py3-none-any.whl |
|---|---|
| Size | 16.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
e785b8c7b58a06da7c603ce0568f63adf057efbc86d1152b6a4403721bd6deb9
|
|
BLAKE2b-256 checksum How to use checksums |
d2b034f5a17e5868a669c492499d34d3acdd0e512272168ae0a3d6cbae75e166
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Dec 12, 2025.
Transparency logSecurity for SecureAgent 0.1.3
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.
Full provenance
All 2 files in this release have been uploaded with provenance metadata.
Attestation for secureagent-0.1.3.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Dec 12, 2025.
Transparency logA 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.