|
| 1 | +--- |
| 2 | +title: Namespace Permissions |
| 3 | +description: Understanding the namespace permission system for agent endpoints |
| 4 | +--- |
| 5 | + |
| 6 | +import { CardGrid, LinkCard, Aside, Tabs, TabItem } from "@astrojs/starlight/components"; |
| 7 | + |
| 8 | +Namespace permissions provide fine-grained access control for agent endpoints on the Torus Network. This system allows agents to delegate specific permissions to users for accessing particular endpoints. |
| 9 | + |
| 10 | +## Overview |
| 11 | + |
| 12 | +The namespace permission system works by: |
| 13 | + |
| 14 | +1. **Automatic Path Generation**: Each endpoint automatically generates a namespace path |
| 15 | +2. **Permission Checking**: Verifying that users have explicit permission for that namespace |
| 16 | +3. **Blockchain Integration**: Permissions are stored and verified on the Torus Network |
| 17 | +4. **Granular Control**: Different endpoints can have different permission requirements |
| 18 | + |
| 19 | +## Automatic Namespace Path Generation |
| 20 | + |
| 21 | +By default, each agent endpoint checks a namespace path using the following format: |
| 22 | + |
| 23 | +``` |
| 24 | +agent.<agent_name>.<endpoint_name>.<http_method> |
| 25 | +``` |
| 26 | + |
| 27 | +### Path Components |
| 28 | + |
| 29 | +- **`agent`**: Fixed prefix indicating this is an agent namespace |
| 30 | +- **`<agent_name>`**: The resolved name of the agent from the blockchain (not the agentKey) |
| 31 | +- **`<endpoint_name>`**: The endpoint name as defined in `agent.method()` |
| 32 | +- **`<http_method>`**: The HTTP method in lowercase (`get`, `post`, `put`, `patch`, `delete`) |
| 33 | + |
| 34 | +### Examples |
| 35 | + |
| 36 | +<Tabs> |
| 37 | +<TabItem label="Basic Example"> |
| 38 | +```ts |
| 39 | +// Agent name: "alice" (resolved from blockchain) |
| 40 | +// Endpoint: "store" |
| 41 | +// Method: "post" |
| 42 | + |
| 43 | +agent.method("store", { |
| 44 | + method: "post", |
| 45 | + auth: { required: true }, |
| 46 | + namespace: { enabled: true }, // Uses automatic path generation |
| 47 | + // ... rest of configuration |
| 48 | +}); |
| 49 | + |
| 50 | +// Generated namespace path: "agent.alice.store.post" |
| 51 | +``` |
| 52 | +</TabItem> |
| 53 | +<TabItem label="Multiple Methods"> |
| 54 | +```ts |
| 55 | +// Same endpoint with different HTTP methods generates different namespaces |
| 56 | + |
| 57 | +// GET /memory |
| 58 | +agent.method("memory", { |
| 59 | + method: "get", |
| 60 | + namespace: { enabled: true } |
| 61 | +}); |
| 62 | +// Namespace: "agent.alice.memory.get" |
| 63 | + |
| 64 | +// POST /memory |
| 65 | +agent.method("memory", { |
| 66 | + method: "post", |
| 67 | + namespace: { enabled: true } |
| 68 | +}); |
| 69 | +// Namespace: "agent.alice.memory.post" |
| 70 | + |
| 71 | +// DELETE /memory |
| 72 | +agent.method("memory", { |
| 73 | + method: "delete", |
| 74 | + namespace: { enabled: true } |
| 75 | +}); |
| 76 | +// Namespace: "agent.alice.memory.delete" |
| 77 | +``` |
| 78 | +</TabItem> |
| 79 | +</Tabs> |
| 80 | + |
| 81 | +## Custom Namespace Paths |
| 82 | + |
| 83 | +You can override the automatic path generation by specifying a custom namespace path: |
| 84 | + |
| 85 | +```ts |
| 86 | +agent.method("sensitive-operation", { |
| 87 | + auth: { required: true }, |
| 88 | + namespace: { |
| 89 | + enabled: true, |
| 90 | + path: "agent.alice.admin.sensitive" // Custom namespace path |
| 91 | + }, |
| 92 | + // ... rest of configuration |
| 93 | +}); |
| 94 | +``` |
| 95 | + |
| 96 | +## Permission Requirements |
| 97 | + |
| 98 | +### Exact Match Required |
| 99 | + |
| 100 | +The namespace permission system requires **exact matches**. Users must have permission for the specific namespace path generated for each endpoint. |
| 101 | + |
| 102 | +```ts |
| 103 | +// Agent "alice" defines this endpoint: |
| 104 | +agent.method("store", { |
| 105 | + method: "post", |
| 106 | + auth: { required: true }, |
| 107 | + namespace: { enabled: true } |
| 108 | +}); |
| 109 | +// This generates namespace: "agent.alice.store.post" |
| 110 | +``` |
| 111 | + |
| 112 | +**Permission Requirements:** |
| 113 | +Users need permission for exactly: `agent.alice.store.post` |
| 114 | + |
| 115 | +These permissions will **NOT** work: |
| 116 | +- `agent.alice.store` (missing method) |
| 117 | +- `agent.alice.*` (wildcards not supported) |
| 118 | +- `agent.alice.store.get` (wrong method) |
| 119 | + |
| 120 | +### Permission Delegation |
| 121 | + |
| 122 | +Permissions are delegated by the agent owner to specific users through the Torus Network's permission system: |
| 123 | + |
| 124 | +1. **Agent Owner**: Has full access to all endpoints |
| 125 | +2. **Delegated Users**: Have access only to specifically granted namespace paths |
| 126 | +3. **Permission Contracts**: Define grantor, grantee, duration, and revocation terms |
| 127 | + |
| 128 | +**Creating Permissions via Web Portal:** |
| 129 | + |
| 130 | +- **Create Namespace Paths**: Use [https://portal.testnet.torus.network/create-namespace](https://portal.testnet.torus.network/create-namespace) to register namespace paths on the blockchain |
| 131 | +- **Grant Permissions**: Use [https://portal.testnet.torus.network/create-permission](https://portal.testnet.torus.network/create-permission) to delegate namespace permissions to specific users |
| 132 | + |
| 133 | +## Configuration Options |
| 134 | + |
| 135 | +### Enabling/Disabling Namespace Checking |
| 136 | + |
| 137 | +```ts |
| 138 | +// Enable namespace checking (default) |
| 139 | +agent.method("protected-endpoint", { |
| 140 | + auth: { required: true }, |
| 141 | + namespace: { enabled: true } |
| 142 | +}); |
| 143 | + |
| 144 | +// Disable namespace checking |
| 145 | +agent.method("public-endpoint", { |
| 146 | + auth: { required: true }, |
| 147 | + namespace: { enabled: false } // No permission check |
| 148 | +}); |
| 149 | + |
| 150 | +// Namespace checking disabled by default if no namespace config provided |
| 151 | +agent.method("open-endpoint", { |
| 152 | + auth: { required: true } |
| 153 | + // No namespace configuration = no permission checking |
| 154 | +}); |
| 155 | +``` |
| 156 | + |
| 157 | +### RPC Configuration |
| 158 | + |
| 159 | +You can specify which RPC endpoints to use for permission verification: |
| 160 | + |
| 161 | +```ts |
| 162 | +agent.method("endpoint", { |
| 163 | + auth: { required: true }, |
| 164 | + namespace: { |
| 165 | + enabled: true, |
| 166 | + rpcUrls: [ |
| 167 | + "wss://api.testnet.torus.network", |
| 168 | + "wss://backup-rpc.torus.network" |
| 169 | + ] |
| 170 | + } |
| 171 | +}); |
| 172 | +``` |
| 173 | + |
| 174 | +<Aside type="note" title="RPC Selection"> |
| 175 | +The system randomly selects one RPC URL from the list for each permission check to provide load balancing. |
| 176 | +</Aside> |
| 177 | + |
| 178 | +## Error Responses |
| 179 | + |
| 180 | +When users lack the required namespace permission, the server returns: |
| 181 | + |
| 182 | +```json |
| 183 | +{ |
| 184 | + "message": "Access denied: insufficient permissions for namespace agent.alice.store.post", |
| 185 | + "code": "NAMESPACE_ACCESS_DENIED" |
| 186 | +} |
| 187 | +``` |
| 188 | + |
| 189 | +**HTTP Status Code**: `403 Forbidden` |
| 190 | + |
| 191 | +## Real-World Examples |
| 192 | + |
| 193 | +### Memory Agent with Granular Permissions |
| 194 | + |
| 195 | +```ts |
| 196 | +const memoryAgent = new AgentServer({ |
| 197 | + agentKey: "5FgfC2DY4yreEWEughz46RZYQ8oBhHVqD9fVq6gV89E6z4Ea", |
| 198 | + // Agent name "alice" resolved from blockchain |
| 199 | +}); |
| 200 | + |
| 201 | +// Public read access - no permission required |
| 202 | +memoryAgent.method("status", { |
| 203 | + method: "get", |
| 204 | + namespace: { enabled: false } |
| 205 | +}); |
| 206 | +// No permission check |
| 207 | + |
| 208 | +// Memory storage - requires specific permission |
| 209 | +memoryAgent.method("store", { |
| 210 | + method: "post", |
| 211 | + auth: { required: true }, |
| 212 | + namespace: { enabled: true } |
| 213 | +}); |
| 214 | +// Requires: "agent.alice.store.post" |
| 215 | + |
| 216 | +// Memory retrieval - different permission |
| 217 | +memoryAgent.method("retrieve", { |
| 218 | + method: "get", |
| 219 | + auth: { required: true }, |
| 220 | + namespace: { enabled: true } |
| 221 | +}); |
| 222 | +// Requires: "agent.alice.retrieve.get" |
| 223 | + |
| 224 | +// Admin operations - custom namespace |
| 225 | +memoryAgent.method("admin-reset", { |
| 226 | + method: "post", |
| 227 | + auth: { required: true }, |
| 228 | + namespace: { |
| 229 | + enabled: true, |
| 230 | + path: "agent.alice.admin.reset" |
| 231 | + } |
| 232 | +}); |
| 233 | +// Requires: "agent.alice.admin.reset" |
| 234 | +``` |
| 235 | + |
| 236 | +### Multi-Agent Scenario |
| 237 | + |
| 238 | +```ts |
| 239 | +// Alice's memory agent |
| 240 | +const aliceAgent = new AgentServer({ |
| 241 | + agentKey: "5FgfC2DY4yreEWEughz46RZYQ8oBhHVqD9fVq6gV89E6z4Ea" |
| 242 | + // Resolves to agent name: "alice" |
| 243 | +}); |
| 244 | + |
| 245 | +// Bob's analysis agent |
| 246 | +const bobAgent = new AgentServer({ |
| 247 | + agentKey: "5D5FbRRUvQxdQnJLgNW6BdgZ86CRGreKRahzhxmdSj2REBnt" |
| 248 | + // Resolves to agent name: "bob-analysis" |
| 249 | +}); |
| 250 | + |
| 251 | +// Same endpoint name, different agents = different namespaces |
| 252 | +aliceAgent.method("process", { |
| 253 | + method: "post", |
| 254 | + namespace: { enabled: true } |
| 255 | +}); |
| 256 | +// Namespace: "agent.alice.process.post" |
| 257 | + |
| 258 | +bobAgent.method("process", { |
| 259 | + method: "post", |
| 260 | + namespace: { enabled: true } |
| 261 | +}); |
| 262 | +// Namespace: "agent.bob-analysis.process.post" |
| 263 | +``` |
| 264 | + |
| 265 | +## Best Practices |
| 266 | + |
| 267 | +### Endpoint Design |
| 268 | + |
| 269 | +- **Use descriptive endpoint names**: They become part of the namespace path |
| 270 | +- **Consider permission granularity**: Each endpoint+method combination requires separate permissions |
| 271 | +- **Group related operations**: Use consistent naming for related endpoints |
| 272 | + |
| 273 | +### Permission Management |
| 274 | + |
| 275 | +- **Principle of least privilege**: Only grant necessary permissions |
| 276 | +- **Regular audits**: Review and revoke unused permissions |
| 277 | +- **Clear documentation**: Document which permissions each endpoint requires |
| 278 | + |
| 279 | +### Development Workflow |
| 280 | + |
| 281 | +1. **Test without permissions first**: Use `namespace: { enabled: false }` during development |
| 282 | +2. **Enable permissions gradually**: Add namespace checking as you finalize endpoints |
| 283 | +3. **Document permission requirements**: Clearly communicate required permissions to users |
| 284 | + |
| 285 | +## Troubleshooting |
| 286 | + |
| 287 | +### Common Issues |
| 288 | + |
| 289 | +**Permission Denied Errors:** |
| 290 | +- Verify the exact namespace path being checked |
| 291 | +- Ensure the user has permission for that specific path |
| 292 | +- Check that permissions haven't expired |
| 293 | + |
| 294 | +**Agent Name Resolution:** |
| 295 | +- Confirm the agent is properly registered on the Torus Network |
| 296 | +- Verify the agentKey corresponds to the expected agent name |
| 297 | + |
| 298 | +**RPC Connectivity:** |
| 299 | +- Test RPC endpoints are accessible |
| 300 | +- Consider using multiple RPC URLs for redundancy |
| 301 | + |
| 302 | + |
| 303 | +<CardGrid> |
| 304 | + <LinkCard |
| 305 | + title="Agent Server" |
| 306 | + href="/agents/agent-server" |
| 307 | + description="Learn how to build Agent APIs with namespace permissions" |
| 308 | + /> |
| 309 | + <LinkCard |
| 310 | + title="Agent Client" |
| 311 | + href="/agents/agent-client" |
| 312 | + description="Understand how clients authenticate with agents" |
| 313 | + /> |
| 314 | + <LinkCard |
| 315 | + title="Register an Agent" |
| 316 | + href="/agents/register-an-agent" |
| 317 | + description="Register your agent on the Torus Network" |
| 318 | + /> |
| 319 | +</CardGrid> |
0 commit comments