-
Notifications
You must be signed in to change notification settings - Fork 414
Add tutorial: Clade Workload Identty integration with Agent ID #6132
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| {% set product_name = "Asgardeo" %} | ||
| {% set api_base_path = "https://api.asgardeo.io/t/{organization_name}" %} | ||
|
Check warning on line 2 in en/asgardeo/docs/tutorials/agent-id-claude-authentication.md
|
||
|
|
||
| {% include "../../../includes/tutorials/agent-id-claude-authentication.md" %} | ||
|
Check failure on line 4 in en/asgardeo/docs/tutorials/agent-id-claude-authentication.md
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -739,6 +739,7 @@ nav: | |||||
| - Secure Spring Boot API with Asgardeo: tutorials/secure-spring-boot-api.md | ||||||
| - Integrate Asgardeo with Smart on FHIR healthcare apps: tutorials/smart-on-fhir.md | ||||||
| - Secure Agentic AI Systems with Asgardeo: tutorials/secure-agentic-ai-systems.md | ||||||
| - Authenticate Agents with their own Agent ID to Claude API Using Workload Identity Federation: tutorials/agent-id-claude-authentication.md | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use sentence case for the navigation entry. The navigation entry should follow sentence case guidelines, matching the corrected tutorial title. 📝 Proposed fix- - Authenticate Agents with their own Agent ID to Claude API Using Workload Identity Federation: tutorials/agent-id-claude-authentication.md
+ - Authenticate agents with their own Agent ID to Claude API using Workload Identity Federation: tutorials/agent-id-claude-authentication.md📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||
| - Implement End-to-End Authorization for MCP servers: tutorials/end-to-end-mcp-authorization-with-asgardeo.md | ||||||
| - Integrating Asgardeo With WSO2 AI Gateway for Agent Identity-Aware Access Control: tutorials/wso2-ai-gateway-with-agent-identity-aware-access-control.md | ||||||
| - Integrating Asgardeo With Kong AI Gateway for Agent Identity-Aware Access Control: tutorials/kong-ai-gateway-with-agent-identity-aware-access-control.md | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| {% set product_name = "WSO2 Identity Server" %} | ||
| {% set api_base_path = "https://localhost:9443" %} | ||
|
Check warning on line 2 in en/identity-server/7.2.0/docs/tutorials/agent-id-claude-authentication.md
|
||
|
|
||
| {% include "../../../../includes/tutorials/agent-id-claude-authentication.md" %} | ||
|
Check failure on line 4 in en/identity-server/7.2.0/docs/tutorials/agent-id-claude-authentication.md
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,3 +11,4 @@ | |
| - [Integrating WSO2 Identity Server With WSO2 AI Gateway for Agent Identity-Aware Access Control]({{base_path}}/tutorials/wso2-ai-gateway-with-agent-identity-aware-access-control/) | ||
| - [Integrating WSO2 Identity Server With Kong AI Gateway for Agent Identity-Aware Access Control]({{base_path}}/tutorials/kong-ai-gateway-with-agent-identity-aware-access-control/) | ||
| - [Secure MCP servers with WSO2 Identity Server and integrate them with n8n agentic flows]({{base_path}}/tutorials/secure-mcp-servers-and-integrate-them-with-n8n-agentic-flows/) | ||
| - [Authenticate Agents with their own Agent ID to Claude API Using Workload Identity Federation]({{base_path}}/tutorials/agent-id-claude-authentication/) | ||
|
Check failure on line 14 in en/identity-server/7.2.0/docs/tutorials/index.md
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| {% set product_name = "WSO2 Identity Server" %} | ||
| {% set api_base_path = "https://localhost:9443" %} | ||
|
Check warning on line 2 in en/identity-server/7.3.0/docs/tutorials/agent-id-claude-authentication.md
|
||
|
|
||
| {% include "../../../../includes/tutorials/agent-id-claude-authentication.md" %} | ||
|
Check failure on line 4 in en/identity-server/7.3.0/docs/tutorials/agent-id-claude-authentication.md
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| {% set product_name = "WSO2 Identity Server" %} | ||
| {% set api_base_path = "https://localhost:9443" %} | ||
|
Check warning on line 2 in en/identity-server/next/docs/tutorials/agent-id-claude-authentication.md
|
||
|
|
||
| {% include "../../../../includes/tutorials/agent-id-claude-authentication.md" %} | ||
|
Check failure on line 4 in en/identity-server/next/docs/tutorials/agent-id-claude-authentication.md
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -11,3 +11,4 @@ | |||||
| - [Integrating WSO2 Identity Server With WSO2 AI Gateway for Agent Identity-Aware Access Control]({{base_path}}/tutorials/wso2-ai-gateway-with-agent-identity-aware-access-control/) | ||||||
| - [Integrating WSO2 Identity Server With Kong AI Gateway for Agent Identity-Aware Access Control]({{base_path}}/tutorials/kong-ai-gateway-with-agent-identity-aware-access-control/) | ||||||
| - [Secure MCP servers with WSO2 Identity Server and integrate them with n8n agentic flows]({{base_path}}/tutorials/secure-mcp-servers-and-integrate-them-with-n8n-agentic-flows/) | ||||||
| - [Authenticate Agents with their own Agent ID to Claude API Using Workload Identity Federation]({{base_path}}/tutorials/agent-id-claude-authentication/) | ||||||
|
Check failure on line 14 in en/identity-server/next/docs/tutorials/index.md
|
||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use sentence case for the tutorial link text. The link text should follow sentence case guidelines, matching the corrected tutorial title. 📝 Proposed fix-- [Authenticate Agents with their own Agent ID to Claude API Using Workload Identity Federation]({{base_path}}/tutorials/agent-id-claude-authentication/)
+- [Authenticate agents with their own Agent ID to Claude API using Workload Identity Federation]({{base_path}}/tutorials/agent-id-claude-authentication/)📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -1001,6 +1001,7 @@ nav: | |||||
| - Integrating WSO2 Identity Server With WSO2 AI Gateway for Agent Identity-Aware Access Control: tutorials/wso2-ai-gateway-with-agent-identity-aware-access-control.md | ||||||
| - Integrating WSO2 Identity Server With Kong AI Gateway for Agent Identity-Aware Access Control: tutorials/kong-ai-gateway-with-agent-identity-aware-access-control.md | ||||||
| - Secure MCP servers with WSO2 Identity Server and integrate them with n8n agentic flows: tutorials/secure-mcp-servers-and-integrate-them-with-n8n-agentic-flows.md | ||||||
| - Authenticate Agents with their own Agent ID to Claude API Using Workload Identity Federation: tutorials/agent-id-claude-authentication.md | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use sentence case for the navigation entry. The navigation entry should follow sentence case guidelines, matching the corrected tutorial title. 📝 Proposed fix- - Authenticate Agents with their own Agent ID to Claude API Using Workload Identity Federation: tutorials/agent-id-claude-authentication.md
+ - Authenticate agents with their own Agent ID to Claude API using Workload Identity Federation: tutorials/agent-id-claude-authentication.md📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||
| - Setup: | ||||||
| - Install: | ||||||
| - Install: deploy/get-started/install.md | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,236 @@ | ||||||
| # Authenticate Agents with their own Agent ID to Claude API Using Workload Identity Federation | ||||||
|
Check warning on line 1 in en/includes/tutorials/agent-id-claude-authentication.md
|
||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use sentence case for the title. The title capitalizes words mid-sentence that are not proper nouns. As per coding guidelines, use sentence case for all headings and document titles, capitalizing only the first word and proper nouns. 📝 Proposed fix-# Authenticate Agents with their own Agent ID to Claude API Using Workload Identity Federation
+# Authenticate agents with their own Agent ID to Claude API using Workload Identity Federation📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||
|
|
||||||
| This tutorial walks you through connecting an AI agent registered in {{product_name}} to the Claude API using [Anthropic's Workload Identity Federation (WIF)](https://docs.anthropic.com/en/docs/build-with-claude/workload-identity-federation/). By the end of this tutorial, your agent will authenticate with {{product_name}}, obtain a signed JWT, exchange it for a short-lived Claude access token, and make API calls without any static API keys. | ||||||
|
Check warning on line 3 in en/includes/tutorials/agent-id-claude-authentication.md
|
||||||
|
|
||||||
| ## Overview | ||||||
|
|
||||||
| The authentication flow works as follows: | ||||||
|
|
||||||
| 1. Your AI agent authenticates with {{product_name}} using its Agent ID and Secret. | ||||||
| 2. {{product_name}} issues a signed JWT (access token) to the agent. | ||||||
| 3. The agent presents this JWT to Anthropic's token endpoint. | ||||||
|
Check warning on line 11 in en/includes/tutorials/agent-id-claude-authentication.md
|
||||||
| 4. Anthropic validates the JWT and returns a short-lived access token. | ||||||
| 5. The agent uses the access token to call the Claude API. | ||||||
|
|
||||||
| ## Prerequisites | ||||||
|
|
||||||
| Before you begin, ensure you have the following: | ||||||
|
|
||||||
| - An [Anthropic](https://console.anthropic.com/) account with access to the Claude Console. | ||||||
| - `curl` and `jq` installed on your machine (for the shell-based approach). | ||||||
| - Python 3.9+ (for the SDK-based approach). | ||||||
|
|
||||||
| ## Step 1: Register an Agent in {{product_name}} | ||||||
|
|
||||||
| 1. Log in to the {{product_name}} Console. | ||||||
| 2. Navigate to **Agentic AI**. | ||||||
| 3. Click **New Agent** and provide a descriptive name for your agent. | ||||||
| 4. Assign the agent to appropriate groups and roles based on the resources it needs to access. | ||||||
| 5. Note down the **Agent ID** and **Agent Secret** that {{product_name}} generates. | ||||||
|
|
||||||
| ## Step 2: Create an Application in {{product_name}} | ||||||
|
|
||||||
| Your agent needs an OAuth application to obtain tokens through the app-native authentication flow. | ||||||
|
Check warning on line 33 in en/includes/tutorials/agent-id-claude-authentication.md
|
||||||
|
|
||||||
| 1. Go to **Applications** and click **New Application**. | ||||||
| 2. Select **Standard-Based Application**. | ||||||
| 3. In the **Protocol** tab, configure the following: | ||||||
| - Enable the **Authorization Code** grant type. | ||||||
| - Set the access token type to **JWT**. | ||||||
| - Add `https://api.anthropic.com` as an **audience** for the access token. | ||||||
| - Set an authorized redirect URI (e.g., `https://example.com/callback`). | ||||||
| 4. In the **Advanced** tab: | ||||||
| - Enable **App Native Authentication**. | ||||||
| 5. Click **Save** and note down the **Client ID**. | ||||||
|
|
||||||
| ## Step 3: Configure {{product_name}} as a Federation Issuer in Claude Console | ||||||
|
Check warning on line 46 in en/includes/tutorials/agent-id-claude-authentication.md
|
||||||
|
|
||||||
| 1. In the Claude Console, go to **Settings > Workload Identity**. | ||||||
| 2. Under **Issuers**, click **Add Issuer** and enter: | ||||||
| - **Issuer URL**: `{{ api_base_path }}/oauth2/token` | ||||||
| - **JWKS URL**: `{{ api_base_path }}/oauth2/jwks` | ||||||
| - **Name**: `{{product_name}}-agents` | ||||||
| 3. Click **Save**. | ||||||
|
|
||||||
| ## Step 4: Create a Service Account in Claude | ||||||
|
|
||||||
| 1. In the Claude Console, go to **Settings > Service Accounts**. | ||||||
| 2. Click **Create Service Account** and give it a descriptive name (e.g., `booking-agent`). | ||||||
| 3. Add the service account to the workspace where it needs API access. | ||||||
| 4. Note down the **Service Account ID** (format: `svac_...`). | ||||||
|
|
||||||
| ## Step 5: Create a Federation Rule | ||||||
|
|
||||||
| 1. In the Claude Console, go to **Settings > Workload Identity > Federation Rules**. | ||||||
| 2. Click **Create Rule** and configure: | ||||||
| - **Issuer**: Select the `{{product_name}}-agents` issuer you created in Step 3. | ||||||
| - **Subject claim**: Set this to the Agent ID from {{product_name}} (the `sub` claim value in the JWT). | ||||||
| - **Audience**: `https://api.anthropic.com` | ||||||
| - **Service Account**: Select the service account you created in Step 4. | ||||||
| - **Token lifetime**: `600` seconds (10 minutes). | ||||||
| 3. Enable the rule for the workspace(s) where the agent needs access. | ||||||
| 4. Click **Save** and note down the **Federation Rule ID** (format: `fdrl_...`). | ||||||
|
|
||||||
| !!! note | ||||||
| If you enable the rule across multiple workspaces, you must include the `workspace_id` parameter in the token exchange request. Without it, Anthropic returns an error: `"rule is enabled for more than one workspace; pass workspace_id to choose one"`. | ||||||
|
|
||||||
| ## Step 6: Authenticate Your Agent | ||||||
|
|
||||||
| With all the configuration in place, your agent can now authenticate and call the Claude API. | ||||||
|
|
||||||
| ### Option A: Shell Script | ||||||
|
|
||||||
| This approach demonstrates the full flow using `curl` commands. | ||||||
|
|
||||||
| #### Phase 1: Obtain an Identity Token from {{product_name}} | ||||||
|
|
||||||
| The agent authenticates with {{product_name}} using the app-native authentication API. This is a redirect-free, API-driven flow that uses PKCE (RFC 7636) for security. The flow involves three API calls: | ||||||
|
|
||||||
| 1. Initiate an authorize request with `response_mode=direct` and PKCE parameters. | ||||||
| 2. Authenticate with the Agent ID and Secret via the `/authn` endpoint. | ||||||
| 3. Exchange the resulting authorization code for a JWT access token. | ||||||
|
|
||||||
| For the complete walkthrough of this flow, see the [{{product_name}} documentation on AI agent authentication](/guides/agentic-ai/ai-agents/agent-authentication/#ai-agent-acting-on-its-own/). | ||||||
|
|
||||||
| The resulting `access_token` from {{product_name}} is a signed JWT that you will present to Claude's WIF. | ||||||
|
|
||||||
| #### Phase 2: Exchange the JWT for a Claude Access Token | ||||||
|
|
||||||
| Present the {{product_name}}-issued JWT to Anthropic's token endpoint: | ||||||
|
|
||||||
| ```bash | ||||||
| ANTHROPIC_RESPONSE=$(curl -s https://api.anthropic.com/v1/oauth/token \ | ||||||
| -H 'content-type: application/json' \ | ||||||
| --data '{ | ||||||
| "grant_type": "urn:ietf:params:oauth:grant-type:jwt-bearer", | ||||||
| "assertion": "'$JWT'", | ||||||
| "federation_rule_id": "fdrl_...", | ||||||
| "organization_id": "<your-anthropic-org-id>", | ||||||
| "service_account_id": "svac_...", | ||||||
| "workspace_id": "wrkspc_..." | ||||||
| }') | ||||||
|
|
||||||
| ACCESS_TOKEN=$(echo "$ANTHROPIC_RESPONSE" | jq -r .access_token) | ||||||
| ``` | ||||||
|
|
||||||
| #### Phase 3: Call the Claude API | ||||||
|
|
||||||
| Use the short-lived access token to make API calls: | ||||||
|
|
||||||
| ```bash | ||||||
| curl -s https://api.anthropic.com/v1/messages \ | ||||||
| -H "authorization: Bearer $ACCESS_TOKEN" \ | ||||||
| -H 'anthropic-version: 2023-06-01' \ | ||||||
| -H 'content-type: application/json' \ | ||||||
| --data '{ | ||||||
| "model": "claude-sonnet-4-6", | ||||||
| "max_tokens": 1024, | ||||||
| "messages": [{"role": "user", "content": "Hello, Claude"}] | ||||||
| }' | ||||||
| ``` | ||||||
|
|
||||||
| ### Option B: Python SDK | ||||||
|
|
||||||
| For production agents, use the Anthropic Python SDK with the {{product_name}} Python SDK for automatic token management. | ||||||
|
|
||||||
| #### Install dependencies | ||||||
|
|
||||||
| ```bash | ||||||
| pip install anthropic {{product_name}} {{product_name}}-ai | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: #!/bin/bash
# Verify if there are product-specific Python SDK package names defined
# Check if there's a variable or configuration for Python package names
rg -n "product.*package|python.*sdk|pip.*install" en/asgardeo en/identity-server --type md -C2Repository: wso2/docs-is Length of output: 2314 🏁 Script executed: #!/bin/bash
# First, find the target file
find . -name "agent-id-claude-authentication.md" -type f
# Then search for Python package definitions for both products
rg -n "from.*import|import.*" --type py -g "*asgardeo*" -g "*identity*server*" | head -30
# Look for pip install commands with actual package names
rg "pip install" --type md | grep -v "{{" | head -20Repository: wso2/docs-is Length of output: 4689 🏁 Script executed: #!/bin/bash
# Search for Python SDK or package information in README or setup files
find . -name "pyproject.toml" -o -name "setup.py" -o -name "requirements.txt" | head -20
# Look for any documentation about Python package names
rg "wso2.*identity.*python|asgardeo.*python|pip.*wso2|pip.*asgardeo" --type md -iRepository: wso2/docs-is Length of output: 7295 🏁 Script executed: #!/bin/bash
# Read the target file around line 139 to see the exact content
sed -n '130,150p' en/includes/tutorials/agent-id-claude-authentication.mdRepository: wso2/docs-is Length of output: 546 🏁 Script executed: #!/bin/bash
# Also check if this is a template file and how template variables are used elsewhere
head -20 en/includes/tutorials/agent-id-claude-authentication.md
# Check if there are any notes about template variables
rg "product_name.*package|product_name.*-ai" en/includes/tutorials/Repository: wso2/docs-is Length of output: 3261 🏁 Script executed: #!/bin/bash
# Check if there are WSO2 Identity Server Python packages documented or referenced
rg "wso2.*identity.*python|wso2-identity" --type md -i | head -20
# Also check the agent-auth-py.md file for both products to see if they differ
echo "=== Asgardeo version ==="
cat en/asgardeo/docs/quick-starts/agent-auth-py.md | grep -A 5 "pip install" | head -10
echo "=== WSO2 Identity Server version ==="
cat en/identity-server/next/docs/quick-starts/agent-auth-py.md | grep -A 5 "pip install" | head -10Repository: wso2/docs-is Length of output: 5253 🏁 Script executed: #!/bin/bash
# Read the agent-auth-py.md files to compare Asgardeo and WSO2 IS versions
echo "=== Checking Asgardeo agent-auth-py.md ==="
grep -n "pip install" en/asgardeo/docs/quick-starts/agent-auth-py.md
echo ""
echo "=== Checking WSO2 Identity Server agent-auth-py.md ==="
grep -n "pip install" en/identity-server/next/docs/quick-starts/agent-auth-py.md
# Check if the files are identical
echo ""
echo "=== File comparison ==="
diff en/asgardeo/docs/quick-starts/agent-auth-py.md en/identity-server/next/docs/quick-starts/agent-auth-py.md | head -50Repository: wso2/docs-is Length of output: 1017 🏁 Script executed: #!/bin/bash
# The files are including a shared template, let's check that template file
echo "=== Checking shared template file ==="
grep -n "pip install" en/includes/quick-starts/agent-auth-py.md
# Also check if the tutorial file (agent-id-claude-authentication.md) is similar
echo ""
echo "=== Checking if agent-id-claude-authentication.md is a shared template ==="
ls -la en/includes/tutorials/agent-id-claude-authentication.md
ls -la en/asgardeo/docs/tutorials/agent-id-claude-authentication.md
# Check if the Asgardeo version includes the shared template
echo ""
echo "=== Checking Asgardeo claude tutorial ==="
head -50 en/asgardeo/docs/tutorials/agent-id-claude-authentication.md | tail -20Repository: wso2/docs-is Length of output: 984 🏁 Script executed: #!/bin/bash
# Check the WSO2 Identity Server version of the claude tutorial
echo "=== Checking WSO2 Identity Server claude tutorial ==="
head -50 en/identity-server/next/docs/tutorials/agent-id-claude-authentication.md
# Check what product_name is set to
echo ""
echo "=== Checking product_name settings ==="
head -5 en/identity-server/next/docs/tutorials/agent-id-claude-authentication.md
head -5 en/asgardeo/docs/tutorials/agent-id-claude-authentication.mdRepository: wso2/docs-is Length of output: 698 🏁 Script executed: #!/bin/bash
# Verify if WSO2 Identity Server has Python SDK packages
# Search for any WSO2 IS Python package information
rg "wso2.*identity" en/includes/quick-starts/ --type md | grep -i python
# Also check PyPI or package registries referenced
rg "pypi|python.*package|wso2.*sdk" --type md | grep -v asgardeo | head -20Repository: wso2/docs-is Length of output: 1433 Fix Python package names to work with template variable substitution. The installation command on line 139 uses
The 🤖 Prompt for AI Agents |
||||||
| ``` | ||||||
|
|
||||||
| #### Complete example | ||||||
|
|
||||||
| ```python | ||||||
| import asyncio | ||||||
| import time | ||||||
| from {{product_name}} import {{product_name}}Config | ||||||
| from {{product_name}}_ai import AgentAuthManager, AgentConfig | ||||||
| from anthropic import Anthropic, WorkloadIdentityCredentials | ||||||
|
|
||||||
| # {{product_name}} configuration | ||||||
| config = {{product_name}}Config( | ||||||
| base_url="{{ api_base_path }}", | ||||||
| client_id="<your-client-id>", | ||||||
| redirect_uri="<your-redirect-uri>", | ||||||
| ) | ||||||
|
|
||||||
| # Agent credentials | ||||||
| agent_config = AgentConfig( | ||||||
| agent_id="<your-agent-id>", | ||||||
| agent_secret="<your-agent-secret>", | ||||||
| ) | ||||||
|
|
||||||
|
|
||||||
| class {{product_name}}TokenProvider: | ||||||
| """Provides identity tokens from {{product_name}} for Anthropic WIF. | ||||||
|
|
||||||
| Caches the token and refreshes automatically when expired. | ||||||
| The Anthropic SDK calls this provider whenever it needs a | ||||||
| new identity token for the WIF exchange. | ||||||
| """ | ||||||
|
|
||||||
| EXPIRY_BUFFER_SECONDS = 60 | ||||||
|
|
||||||
| def __init__(self, {{product_name}}_config, agent_cfg, scopes=None): | ||||||
| self.{{product_name}}_config = {{product_name}}_config | ||||||
| self.agent_cfg = agent_cfg | ||||||
| self.scopes = scopes or ["openid", "profile"] | ||||||
| self._cached_token = None | ||||||
| self._token_expiry = 0 | ||||||
|
|
||||||
| def __call__(self): | ||||||
| if self._is_token_valid(): | ||||||
| return self._cached_token | ||||||
| return asyncio.run(self._refresh_token()) | ||||||
|
|
||||||
| def _is_token_valid(self): | ||||||
| if self._cached_token is None: | ||||||
| return False | ||||||
| return time.time() < ( | ||||||
| self._token_expiry - self.EXPIRY_BUFFER_SECONDS | ||||||
| ) | ||||||
|
|
||||||
| async def _refresh_token(self): | ||||||
| async with AgentAuthManager( | ||||||
| self.{{product_name}}_config, self.agent_cfg | ||||||
| ) as auth_manager: | ||||||
| agent_token = await auth_manager.get_agent_token( | ||||||
| self.scopes | ||||||
| ) | ||||||
| self._cached_token = agent_token.access_token | ||||||
| self._token_expiry = ( | ||||||
| time.time() + agent_token.expires_in | ||||||
| ) | ||||||
| return self._cached_token | ||||||
|
|
||||||
|
|
||||||
| # Initialize the token provider and Anthropic client | ||||||
| token_provider = {{product_name}}TokenProvider(config, agent_config) | ||||||
|
|
||||||
| client = Anthropic( | ||||||
| credentials=WorkloadIdentityCredentials( | ||||||
| identity_token_provider=token_provider, | ||||||
| federation_rule_id="fdrl_...", | ||||||
| organization_id="<your-anthropic-org-id>", | ||||||
| service_account_id="svac_...", | ||||||
| workspace_id="wrkspc_...", | ||||||
| ), | ||||||
| ) | ||||||
|
|
||||||
| # Make an API call | ||||||
| message = client.messages.create( | ||||||
| model="claude-sonnet-4-6", | ||||||
| max_tokens=1024, | ||||||
| messages=[{"role": "user", "content": "Hello, Claude"}], | ||||||
| ) | ||||||
| print(message.content[0].text) | ||||||
| ``` | ||||||
|
|
||||||
| The `{{product_name}}TokenProvider` handles token caching and refresh with a 60-second buffer before expiry. The Anthropic SDK's `WorkloadIdentityCredentials` manages the Claude access token lifecycle independently, calling the provider whenever it needs a fresh identity token for the WIF exchange. Together, they allow long-running agents to operate continuously without manual token management. | ||||||
|
|
||||||
|
|
||||||
|
Check failure on line 232 in en/includes/tutorials/agent-id-claude-authentication.md
|
||||||
| ## What's Next | ||||||
|
|
||||||
| - Learn more about [{{product_name}} Agent Identity]({{base_path}}/guides/agentic-ai/ai-agents/). | ||||||
| - Secure agents access to [MCP Servers]({{base_path}}/guides/agentic-ai/mcp/) | ||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use sentence case for the tutorial link text.
The link text should follow sentence case guidelines, matching the corrected tutorial title.
📝 Proposed fix
🤖 Prompt for AI Agents