-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathconfig_example.py
More file actions
87 lines (70 loc) · 2.26 KB
/
Copy pathconfig_example.py
File metadata and controls
87 lines (70 loc) · 2.26 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
84
85
86
87
#!/usr/bin/env python3
"""
FastANP with DidWbaVerifierConfig Example
This example demonstrates using DidWbaVerifierConfig for authentication.
"""
import sys
from pathlib import Path
# Add project root to path
project_root = Path(__file__).parent.parent.parent.parent
sys.path.insert(0, str(project_root))
from fastapi import FastAPI
from anp.authentication.did_wba_verifier import DidWbaVerifierConfig
from anp.fastanp import FastANP
from anp.fastanp.utils import load_private_key
# Initialize FastAPI app
app = FastAPI(
title="Config Example",
description="Example using DidWbaVerifierConfig",
version="1.0.0"
)
# Load JWT keys
jwt_private_key = load_private_key(
str(project_root / "docs" / "jwt_rs256" / "private_key.pem")
).decode('utf-8')
jwt_public_key = load_private_key(
str(project_root / "docs" / "jwt_rs256" / "public_key.pem")
).decode('utf-8')
# Create custom auth config with allowed domains
auth_config = DidWbaVerifierConfig(
jwt_private_key=jwt_private_key,
jwt_public_key=jwt_public_key,
jwt_algorithm="RS256",
access_token_expire_minutes=120, # 2 hours
nonce_expiration_minutes=10,
timestamp_expiration_minutes=5,
allowed_domains=["example.com", "*.example.com"] # Domain whitelist in config
)
# Initialize FastANP with auth config
anp = FastANP(
app=app,
name="Config Example Agent",
description="Demonstrates custom authentication configuration",
agent_domain="https://example.com",
did="did:wba:example.com:agent:config-example",
auth_config=auth_config, # Pass the config directly (includes allowed_domains)
enable_auth_middleware=True,
)
# Define ad.json route
@app.get("/ad.json", tags=["agent"])
def get_agent_description():
"""Get Agent Description."""
ad = anp.get_common_header(agent_description_path="/ad.json")
ad["interfaces"] = [
anp.interfaces[hello].link_summary,
]
return ad
# Register a simple interface
@anp.interface("/info/hello.json", description="Say hello")
def hello(name: str) -> dict:
"""
Greet someone by name.
Args:
name: Name to greet
Returns:
Greeting message
"""
return {"message": f"Hello, {name}!"}
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)