Skip to main content

Agent Identity Python SDK

The Agent Identity Python SDK is a Python development toolkit for accessing Agent Identity services. This SDK provides identity authentication, token management, API key acquisition, and more, supporting both synchronous and asynchronous invocation modes.

Features

  • OAuth2 Access Token Acquisition: Supports multiple OAuth2 flows for access token retrieval
  • API Key Acquisition: Programmatic API key retrieval
  • STS Credential Acquisition: Obtain temporary security token service credentials
  • Context Management: Thread-safe context variable management
  • Caching Mechanism: Built-in credential caching for improved performance
  • Decorator Support: Simplified authentication flow integration via decorators
  • Concurrency Safety: Supports multi-threading and asynchronous environments

Installation

pip install agent-identity-python-sdk

Quick Start

Basic Configuration

Before using the SDK, ensure you have set the correct environment variables:

export AGENT_IDENTITY_REGION_ID="cn-beijing"  # Optional, set your Region ID, default is cn-beijing
export AGENT_IDENTITY_WORKLOAD_ACCESS_TOKEN="<your-workload-access-token>" # Optional, set your workload access token, if not specified, the Agent Identity service will be automatically called to obtain it
export AGENT_IDENTITY_WORKLOAD_IDENTITY_NAME="<your-workload-identity-name>" # Optional, set your workload identity name, if specified, the workload identity will be used as the agent's identity, otherwise a random workload identity will be generated
export AGENT_IDENTITY_USE_STS="true/false" # Optional, set whether to use the agent identity associated role for resource credential acquisition, default is true

Using Decorators to Automatically Obtain Tokens

from agent_identity_python_sdk.core import requires_access_token

@requires_access_token(
    credential_provider_name="your-provider-name",
    inject_param_name="access_token",
    auth_flow="USER_FEDERATION",
    on_auth_url= lambda url: print(f"Please visit {url} to authenticate."),
    scopes=["openid", "profile", "email"],
    force_authentication=False,
    callback_url="http://localhost:8080",
    custom_parameters={
        "custom_param_1": "value_1",
        "custom_param_2": "value_2"
    }
)
def my_function(access_token: str):
    # Use access_token here
    print(f"Access token: {access_token}")
    # Your business logic

# Call the function
my_function()

Using Decorators to Obtain M2M Tokens

For machine-to-machine scenarios (no user login), create an M2M credential provider first, then set auth_flow="M2M":

from agent_identity_python_sdk.core import requires_access_token

@requires_access_token(
    credential_provider_name="your-m2m-provider-name",
    inject_param_name="access_token",
    auth_flow="M2M",
)
def my_function(access_token: str):
    # Use the M2M access token here
    print(f"Access token: {access_token}")

# Call the function
my_function()

M2M uses the client_credentials grant: no user context is required, and no authorization URL will be returned. See agent_identity_python_samples/end-to-end_sample for a complete M2M example (DingTalk work notification).

Using Decorators to Obtain API Keys

from agent_identity_python_sdk.core.decorators import requires_api_key

@requires_api_key(credential_provider_name="your-provider-name", inject_param_name="api_key")
def my_function(api_key: str):
    # Use api_key here
    print(f"API key: {api_key}")
    # Your business logic

# Call the function
my_function()

Using Decorators to Obtain STS Credentials

from agent_identity_python_sdk.core.decorators import requires_sts_token
from agent_identity_python_sdk.model.stscredential import STSCredential

@requires_sts_token(inject_param_name="sts_credential")
def my_function(sts_credential: STSCredential):
    # Use sts_credential here
    print(f"STS Access Key ID: {sts_credential.access_key_id}")
    # Your business logic

# Call the function
my_function()

Using Decorators to Obtain Workload Credentials

from agent_identity_python_sdk.core.decorators import requires_workload_access_token
@requires_workload_access_token(inject_param_name="workload_access_token")
def my_function(workload_access_token: str):
    # use workload_access_token here
    print(f"Workload Access Token: {workload_access_token}")
    # Your business logic

# call the function
my_function()

Core Modules

IdentityClient

IdentityClient is the core identity management client that provides methods for creating and managing identities and acquiring various types of credentials.

from agent_identity_python_sdk.core.identity import IdentityClient

client = IdentityClient(region_id="cn-beijing")

# Create workload identity
workload_identity_name = client.create_workload_identity(
    workload_identity_name="my-workload",
    allowed_resource_oauth2_return_urls=["https://example.com/callback"],
    role_arn="acs:ram::12****:role/example-role",
)

# Get workload access token
token = client.get_workload_access_token(
    workload_name=workload_identity_name,
    user_token="ejwyJ9***",
    user_id="example-user"
) # Prioritizes using user_token to obtain workload access token; if not available, uses user_id to obtain workload access token; if both are absent, obtains workload access token without end-user information

Context Management

The SDK provides context managers for storing thread/async task isolated data:

AgentIdentityContext

Used to manage workload access tokens, user ID, user tokens, session ID, etc. The SDK will read the current thread's context variables to obtain the workload access token when acquiring workload access tokens.

from agent_identity_python_sdk.context.context import AgentIdentityContext

# Set workload access token
AgentIdentityContext.set_workload_access_token("your-token")

# Get workload access token
token = AgentIdentityContext.get_workload_access_token()

# Set user token
AgentIdentityContext.set_user_token("your-token")

# Set user ID
AgentIdentityContext.set_user_id("user-123")

# Set custom state
AgentIdentityContext.set_custom_state("your-state")

# Clear current thread context, needs to be actively cleared at the end of a single session, otherwise permission leakage may occur due to thread sharing
AgentIdentityContext.clear()

If a workload access token is set in the context, it will be prioritized when acquiring workload access tokens from the current thread context variables.

If no workload access token is set, when the SDK automatically acquires a workload access token, it will retrieve user ID/user token information from the current thread context variables, following the user token/user ID/none priority to acquire the workload access token.

If a custom state is set, during OAuth2 authorization, the custom state will be passed along. User applications can use the custom state to handle authorization callbacks and perform verification. It is recommended that applications use custom state for session verification to prevent malicious sharing of authorization links to obtain other users' permissions.

⚠️ Note: After the current workflow execution is completed, you need to actively clear the current thread context, otherwise permission leakage may occur due to thread sharing.

Environment Variables Configuration

Variable Name Description Default Value
AGENT_IDENTITY_REGION_ID Region identifier cn-beijing
AGENT_IDENTITY_WORKLOAD_ACCESS_TOKEN Workload identity token None
AGENT_IDENTITY_WORKLOAD_IDENTITY_NAME Workload identity name None
AGENT_IDENTITY_USE_STS Whether to use STS for resource credential acquisition true

Contributing

Issues and Pull Requests are welcome to help improve this SDK.

License

This project is licensed under the Apache-2.0 License. See the LICENSE file for details.

Metadata

Release files for agent-identity-python-sdk 0.1.6

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

Source distribution (sdist)

Source distribution for agent-identity-python-sdk 0.1.6
File Size Uploaded
agent_identity_python_sdk-0.1.6.tar.gz 15.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for agent-identity-python-sdk 0.1.6
File Interpreter ABI Platform
agent_identity_python_sdk-0.1.6-py3-none-any.whl Python 3 none any Details

Total release size: 32.8 kB

Release files / agent_identity_python_sdk-0.1.6.tar.gz

Download URL agent_identity_python_sdk-0.1.6.tar.gz
Size 15.9 kB
Tags Source
SHA-256 checksum
How to use checksums
3cc0201b09a0b4c5b2a1f6d36a139ca390cb98e5929b00d146b8e401bc06fd44
BLAKE2b-256 checksum
How to use checksums
091e81993c3c389cb0ce73b96e75613e07288dc17cd8c50675ffd504bf53836a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.2

Release files / agent_identity_python_sdk-0.1.6-py3-none-any.whl

Download URL agent_identity_python_sdk-0.1.6-py3-none-any.whl
Size 16.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8d16b0ddf45982a3945e3ba210d2d5640cc062416163fd66cc05a94eeb772dfd
BLAKE2b-256 checksum
How to use checksums
ee6869c75b1aa1125838de2f3c4758e9c99bafe445e3e678665d8b7f5411a393
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.2

Release history Release notifications | RSS feed

This release

0.1.6 This release

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

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