Skip to content

Commit 002f24a

Browse files
committed
feat: add HMAC authentication to secure agent endpoints
1 parent 28c2166 commit 002f24a

File tree

5 files changed

+773
-2
lines changed

5 files changed

+773
-2
lines changed

README.md

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
- **🤖 Smart Agents** with custom personalities and instructions
1515
- **🔧 Custom Tools** that agents can execute seamlessly
1616
- **🛡️ Built-in Guardrails** for safe, controlled AI interactions
17+
- **🔐 HMAC Security** for enterprise-grade API authentication
1718
- **📡 Real-time Streaming** for responsive user experiences
1819
- **🔗 MCP Protocol** for enterprise-grade integrations
1920
- **📝 Intelligent Logging** with contextual insights
@@ -100,6 +101,7 @@ console.log(data.result.text); // "Hello, Sarah! 👋"
100101
| **🔧 Tools** | Local tools and MCP integration | `tool.build([{ type: 'local', executeFn }])` |
101102
| **📝 Logging** | Contextual and configurable logging system | `new Logger({ level: 'info' })` |
102103
| **🛡️ Guardrails** | Input and output validation | `guardrails: { input: [...], output: [...] }` |
104+
| **🔐 Security** | HMAC authentication for secure APIs | `security.generateHMACAuth(id, key, ...)` |
103105
| **🔄 Handoffs** | Transfer between agents | `{ type: 'agent', agent: otherAgent }` |
104106
| **📡 Streaming** | Real-time responses | `result.textStream` |
105107
| **🌐 HTTP API** | Expose agents as REST endpoints | `new Endpoint(agent, { port: 3001 })` |
@@ -122,11 +124,139 @@ const llm = model.create({
122124
});
123125
```
124126

127+
## 🔐 HMAC Security
128+
129+
Secure your agent endpoints with enterprise-grade HMAC authentication. Perfect for production APIs that need to verify client identity and request integrity.
130+
131+
### 🛡️ Secure Server Implementation
132+
133+
```javascript
134+
import { Agent, Endpoint, Logger, model, security } from '@riligar/agents-sdk';
135+
136+
// Create your agent
137+
const agent = new Agent({
138+
name: 'SecureAgent',
139+
instructions: 'You are a secure AI assistant.',
140+
model: model.create({ apiKey: process.env.OPENROUTER_API_KEY }),
141+
logger: new Logger({ level: 'info' })
142+
});
143+
144+
// Create endpoint with HMAC security
145+
const endpoint = new Endpoint(agent, {
146+
port: 3001,
147+
// Option 1: Client-specific keys (recommended)
148+
hmacClients: {
149+
'frontend-app': process.env.FRONTEND_SECRET,
150+
'mobile-app': process.env.MOBILE_SECRET,
151+
'admin-panel': process.env.ADMIN_SECRET
152+
},
153+
// Option 2: Master secret (simpler)
154+
hmacSecret: process.env.HMAC_MASTER_SECRET,
155+
hmacTolerance: 300 // 5 minutes tolerance
156+
});
157+
158+
await endpoint.start();
159+
console.log('🔒 Secure agent running on http://localhost:3001');
160+
```
161+
162+
### 👤 Authenticated Client Implementation
163+
164+
```javascript
165+
import { security } from '@riligar/agents-sdk';
166+
167+
class SecureAgentClient {
168+
constructor(clientId, secretKey, baseUrl) {
169+
this.clientId = clientId;
170+
this.secretKey = secretKey;
171+
this.baseUrl = baseUrl;
172+
}
173+
174+
async processTask(task) {
175+
const body = { task };
176+
177+
// Generate HMAC authentication
178+
const authHeader = security.generateHMACAuth(
179+
this.clientId,
180+
this.secretKey,
181+
'POST',
182+
'/process',
183+
body
184+
);
185+
186+
const response = await fetch(`${this.baseUrl}/process`, {
187+
method: 'POST',
188+
headers: {
189+
'Content-Type': 'application/json',
190+
'Authorization': authHeader
191+
},
192+
body: JSON.stringify(body)
193+
});
194+
195+
if (!response.ok) {
196+
throw new Error(`Authentication failed: ${response.status}`);
197+
}
198+
199+
return response.json();
200+
}
201+
202+
async checkHealth() {
203+
const authHeader = security.generateHMACAuth(
204+
this.clientId,
205+
this.secretKey,
206+
'GET',
207+
'/health'
208+
);
209+
210+
const response = await fetch(`${this.baseUrl}/health`, {
211+
headers: { 'Authorization': authHeader }
212+
});
213+
214+
return response.json();
215+
}
216+
}
217+
218+
// Usage
219+
const client = new SecureAgentClient(
220+
'frontend-app',
221+
process.env.FRONTEND_SECRET,
222+
'https://your-agent-api.com'
223+
);
224+
225+
const result = await client.processTask('Analyze customer data');
226+
console.log(result.result.text);
227+
```
228+
229+
### 🔑 Security Features
230+
231+
- **🛡️ HMAC-SHA256**: Industry-standard message authentication
232+
- **⏱️ Timestamp Protection**: Prevents replay attacks
233+
- **👥 Multi-Client Support**: Individual keys per client
234+
- **🔐 Timing-Safe Validation**: Resistant to timing attacks
235+
- **🚫 Request Integrity**: Detects message tampering
236+
- **⚙️ Configurable Tolerance**: Flexible timestamp validation
237+
238+
### 📋 Environment Variables
239+
240+
```bash
241+
# Server configuration
242+
OPENROUTER_API_KEY=your-openrouter-api-key
243+
HMAC_MASTER_SECRET=your-master-secret-key
244+
FRONTEND_SECRET=frontend-client-secret-key
245+
MOBILE_SECRET=mobile-client-secret-key
246+
ADMIN_SECRET=admin-client-secret-key
247+
248+
# Client configuration
249+
AGENT_CLIENT_ID=frontend-app
250+
AGENT_SECRET_KEY=frontend-client-secret-key
251+
AGENT_BASE_URL=https://your-agent-api.com
252+
```
253+
125254
## 📚 Practical Examples
126255

127256
| Example | File | Description |
128257
| -------------- | ----------------------- | ----------------------- |
129258
| **Basic** | `examples/hello.js` | Simple agent with tools |
259+
| **Security** | `examples/security.js` | HMAC authentication demo |
130260
| **Guardrails** | `examples/guardrail.js` | Input/output validation |
131261
| **Handoffs** | `examples/handoffs.js` | Transfer between agents |
132262
| **MCP** | `examples/mcp/` | MCP server integration |

0 commit comments

Comments
 (0)