a2a-agents 0.7.0

Framework for building A2A Protocol agents with declarative configuration
Documentation
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
# Authentication Configuration Guide

This guide explains how to configure authentication for A2A agents using TOML configuration files.

## Overview

The agent builder supports multiple authentication schemes that can be configured declaratively:

1. **None** - No authentication (development only)
2. **Bearer Token** - Simple token-based authentication
3. **API Key** - Key-based authentication (header/query/cookie)
4. **JWT** - JSON Web Token authentication with HMAC or RSA
5. **OAuth2** - OAuth2 authentication (authorization code or client credentials flow)

## Who the caller is

A successful authentication produces a **principal**, and the principal's id
reaches the message handler — it is what
`[handler.llm.context] mode = "context"` uses to decide whose conversation a
`contextId` refers to. Each scheme names the caller differently:

| Scheme | Principal id | Stable across a credential change? |
|---|---|---|
| `none` | *(no principal)* | — |
| `bearer` | the token | No |
| `api_key` | the key | No |
| `jwt` | the `sub` claim | **Yes** |
| `oauth2` / `oidc` | `oauth2:{access_token}` | No — rotates on refresh |

Only `jwt` identifies a *user* rather than a *credential*. If anything downstream
keys state on the caller, that is the scheme to use.

## Configuration Format

All authentication is configured in the `[server.auth]` section of your TOML file.

### 1. No Authentication (Development)

```toml
[server.auth]
type = "none"
```

**Use when:** Local development, internal testing
**Security:** ⚠️ Not suitable for production

---

### 2. Bearer Token Authentication

Simple token-based authentication where clients send a token in the Authorization header.

```toml
[server.auth]
type = "bearer"
tokens = ["${API_TOKEN}", "backup-token-123"]
format = "JWT"  # Optional, describes token format
```

**Configuration:**
- `tokens` - List of valid bearer tokens (supports environment variables)
- `format` - Optional description (e.g., "JWT", "Opaque")

**Client Usage:**
```http
Authorization: Bearer your-token-here
```

**Use when:** Simple API protection, internal services
**Security:** ✅ Suitable for production with strong tokens

---

### 3. API Key Authentication

Key-based authentication that can be sent in headers, query parameters, or cookies.

```toml
[server.auth]
type = "apikey"
keys = ["${API_KEY}", "backup-key-456"]
location = "header"  # Options: "header", "query", "cookie"
name = "X-API-Key"   # Header/param/cookie name
```

**Configuration:**
- `keys` - List of valid API keys
- `location` - Where to look for the key (default: "header")
- `name` - Name of the header/parameter/cookie (default: "X-API-Key")

**Client Usage:**
```http
# Header
X-API-Key: your-api-key-here

# Query parameter
GET /api/endpoint?X-API-Key=your-api-key-here

# Cookie
Cookie: X-API-Key=your-api-key-here
```

**Use when:** Third-party API access, webhook callbacks
**Security:** ⚠️ Currently not implemented in runtime (falls back to no auth)

---

### 4. JWT Authentication

JSON Web Token authentication with support for HMAC and RSA algorithms.

#### HMAC-based JWT (HS256/HS384/HS512)

```toml
[server.auth]
type = "jwt"
secret = "${JWT_SECRET}"  # Shared secret for HMAC
algorithm = "HS256"       # HS256, HS384, or HS512
issuer = "https://auth.example.com"    # Optional: validate iss claim
audience = "api://my-agent"             # Optional: validate aud claim
```

#### RSA-based JWT (RS256/RS384/RS512)

```toml
[server.auth]
type = "jwt"
rsa_pem_path = "/path/to/public_key.pem"  # RSA public key
algorithm = "RS256"                        # RS256, RS384, or RS512
issuer = "https://auth.example.com"       # Optional
audience = "api://my-agent"                # Optional
```

**Configuration:**
- `secret` - Shared secret for HMAC algorithms (HS256/384/512)
- `rsa_pem_path` - Path to RSA public key PEM file for RSA algorithms
- `algorithm` - JWT signing algorithm (default: "HS256")
- `issuer` - Optional: required issuer (iss claim validation)
- `audience` - Optional: required audience (aud claim validation)

**Required Claims:**
- `sub` - Subject (user ID)
- `exp` - Expiration time (Unix timestamp)
- `iat` - Issued at (Unix timestamp)

**Client Usage:**
```http
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...
```

**Use when:** User authentication, microservices, federated identity
**Security:** ✅ Highly secure, industry standard

**Requires:** `auth` feature flag
```toml
# Cargo.toml
a2a-agents = { features = ["auth"] }
```

---

### 5. OAuth2 Authentication

OAuth2 authentication supporting both authorization code and client credentials flows.

#### Authorization Code Flow (User Authentication)

Used when human users need to authenticate via a third-party provider.

```toml
[server.auth]
type = "oauth2"
client_id = "${OAUTH_CLIENT_ID}"
client_secret = "${OAUTH_CLIENT_SECRET}"
authorization_url = "https://provider.com/oauth/authorize"
token_url = "https://provider.com/oauth/token"
redirect_url = "http://localhost:8080/oauth/callback"
flow = "authorization_code"
scopes = ["read", "write", "email"]
```

#### Client Credentials Flow (Machine-to-Machine)

Used for service-to-service authentication without user interaction.

```toml
[server.auth]
type = "oauth2"
client_id = "${OAUTH_CLIENT_ID}"
client_secret = "${OAUTH_CLIENT_SECRET}"
authorization_url = "http://localhost"  # Not used in client_credentials
token_url = "https://provider.com/oauth/token"
flow = "client_credentials"
scopes = ["api.read", "api.write"]
```

**Configuration:**
- `client_id` - OAuth2 client ID from your provider
- `client_secret` - OAuth2 client secret (use environment variables!)
- `authorization_url` - Authorization endpoint URL
- `token_url` - Token endpoint URL
- `redirect_url` - Callback URL (authorization_code flow only)
- `flow` - OAuth2 flow type: "authorization_code" or "client_credentials"
- `scopes` - Required OAuth2 scopes

**Common OAuth2 Providers:**

**GitHub:**
```toml
authorization_url = "https://github.com/login/oauth/authorize"
token_url = "https://github.com/login/oauth/access_token"
scopes = ["user", "repo"]
```

**Google:**
```toml
authorization_url = "https://accounts.google.com/o/oauth2/v2/auth"
token_url = "https://oauth2.googleapis.com/token"
scopes = ["openid", "email", "profile"]
```

**Microsoft/Azure AD:**
```toml
authorization_url = "https://login.microsoftonline.com/{tenant}/oauth2/v2.0/authorize"
token_url = "https://login.microsoftonline.com/{tenant}/oauth2/v2.0/token"
scopes = ["openid", "profile", "email"]
```

**Auth0:**
```toml
authorization_url = "https://YOUR_DOMAIN.auth0.com/authorize"
token_url = "https://YOUR_DOMAIN.auth0.com/oauth/token"
scopes = ["openid", "profile", "email"]
```

**Use when:** Third-party login, social authentication, enterprise SSO
**Security:** ✅ Highly secure, delegated authentication

**Requires:** `auth` feature flag

---

## Environment Variables

All authentication configurations support environment variable interpolation using `${VAR_NAME}` syntax:

```toml
[server.auth]
type = "jwt"
secret = "${JWT_SECRET}"  # Read from JWT_SECRET environment variable
issuer = "${JWT_ISSUER}"   # Read from JWT_ISSUER environment variable
```

**Best Practices:**
- ✅ Always use environment variables for secrets
- ✅ Never commit secrets to version control
- ✅ Use different secrets for dev/staging/production
- ✅ Rotate secrets regularly

---

## Security Best Practices

### Token Storage
- Use environment variables or secure vaults
- Never hardcode secrets in configuration files
- Use different secrets per environment

### Token Rotation
- Implement token expiration
- Support token refresh where applicable
- Revoke compromised tokens immediately

### HTTPS in Production
```toml
[server]
host = "0.0.0.0"
http_port = 443  # Use HTTPS in production
```

Use a reverse proxy (nginx, Caddy) for TLS termination.

### Minimal Scopes
Only request the OAuth2 scopes you actually need:
```toml
scopes = ["read"]  # Don't request "write" if not needed
```

---

## Testing Authentication

### Without Auth (Development)
```bash
curl http://localhost:8080/agent/card
```

### With Bearer Token
```bash
curl -H "Authorization: Bearer your-token-here" \
  http://localhost:8080/agent/card
```

### With JWT
```bash
# Generate a test JWT at https://jwt.io
curl -H "Authorization: Bearer eyJhbGc..." \
  http://localhost:8080/agent/card
```

### With API Key
```bash
curl -H "X-API-Key: your-api-key-here" \
  http://localhost:8080/agent/card
```

---

## Enabling Authentication Features

JWT and OAuth2 require the `auth` feature flag.

**In your agent's Cargo.toml:**
```toml
[dependencies]
a2a-agents = { path = "../a2a-agents", features = ["auth"] }
```

**Building with auth:**
```bash
cargo build --features auth
cargo run --features auth --example my_agent
```

---

## Example Configurations

[`examples/auth.toml`](../examples/auth.toml) is one runnable config with every
`[server.auth]` shape written out: JWT live, and bearer, API key and OAuth2
(both flows) as commented alternatives to swap in.

---

## Troubleshooting

### "auth feature not enabled"
Enable the `auth` feature in your Cargo.toml and rebuild:
```bash
cargo build --features auth
```

### "JWT validation failed"
- Check that the secret matches what was used to sign the token
- Verify the algorithm is correct (HS256 vs RS256, etc.)
- Ensure required claims (iss, aud) match configuration

### "Invalid OAuth2 URL"
- Verify authorization_url and token_url are valid HTTPS URLs
- Check for typos in provider URLs
- Ensure redirect_url is correctly configured

### Environment Variable Not Found
```toml
# ❌ Wrong - will fail if JWT_SECRET not set
secret = "${JWT_SECRET}"

# ✅ Better - provide default for development
secret = "${JWT_SECRET:-dev-secret-do-not-use-in-production}"
```

---

## Migration from Code-Based Auth

**Before (manual setup):**
```rust
let authenticator = JwtAuthenticator::new_with_secret(b"my-secret")
    .with_issuer("https://auth.example.com".to_string())
    .with_audience("api://my-agent".to_string());

let server = HttpServer::with_auth(processor, agent_info, bind_address, authenticator);
```

**After (TOML configuration):**
```toml
[server.auth]
type = "jwt"
secret = "${JWT_SECRET}"
issuer = "https://auth.example.com"
audience = "api://my-agent"
```

```rust
// Just use the builder - auth is automatic!
AgentBuilder::from_file("agent.toml")?
    .with_handler(handler)
    .build_with_auto_storage()
    .await?
    .run()
    .await?;
```

---

## Summary

| Auth Type | Security | Complexity | Use Case |
|-----------|----------|------------|----------|
| None | ⚠️ Low | Minimal | Development only |
| Bearer | ✅ Good | Low | Simple APIs |
| API Key | ✅ Good | Low | Third-party access |
| JWT | ✅ High | Medium | User auth, microservices |
| OAuth2 | ✅ High | High | Social login, SSO |

Choose the authentication method that best fits your security requirements and use case.