-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathagent_auth.py
More file actions
83 lines (65 loc) · 2.87 KB
/
agent_auth.py
File metadata and controls
83 lines (65 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
"""
Copyright (c) 2025, WSO2 LLC. (https://www.wso2.com).
WSO2 LLC. licenses this file to you under the Apache License,
Version 2.0 (the "License"); you may not use this file except
in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
"""
"""
AI Agent authentication example using Asgardeo AI SDK.
This example shows how to authenticate an AI agent and get tokens
for the agent to perform operations.
"""
import asyncio
from asgardeo import AsgardeoConfig
from asgardeo_ai import AgentAuthManager, AgentConfig
async def main():
"""AI Agent authentication example."""
# Asgardeo configuration - Replace with your actual values
config = AsgardeoConfig(
base_url="https://api.asgardeo.io/t/<org-name>",
client_id="<client-id>",
redirect_uri="<redirect-uri>",
client_secret="<client-secret>"
)
# AI Agent configuration - Replace with your actual agent credentials
agent_config = AgentConfig(
agent_id="<agent-id>",
agent_secret="<agent-secret>"
)
try:
# Create agent auth manager with context manager for cleanup
async with AgentAuthManager(config, agent_config) as auth_manager:
print("Authenticating AI Agent...")
# Define scopes the agent needs
scopes = ["openid", "profile", "email"]
# Get token for the agent
agent_token = await auth_manager.get_agent_token(scopes)
print("Agent authentication successful!")
print(f"Agent Access Token: {agent_token.access_token[:20]}...")
if agent_token.id_token:
print(f"Agent ID Token: {agent_token.id_token[:20]}...")
if agent_token.refresh_token:
print(f"Agent Refresh Token: {agent_token.refresh_token[:20]}...")
print(f"Expires in: {agent_token.expires_in} seconds")
print(f"Scope: {agent_token.scope}")
# Example: Use the agent token for API calls
print("\n🔧 Agent token can now be used for API operations...")
print(" - Making requests on behalf of the agent")
print(" - Accessing agent-specific resources")
print(" - Initiating user authorization flows")
except Exception as e:
print(f"Agent authentication failed: {e}")
import traceback
traceback.print_exc()
if __name__ == "__main__":
print("Asgardeo AI Agent Authentication Example")
print("=" * 50)
asyncio.run(main())