adk-auth 0.5.0

Access control and authentication for Rust Agent Development Kit (ADK-Rust)
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
//! Integration tests for the declarative scope-based security system.
//!
//! Demonstrates the "Security State Machine" pattern where tools declare
//! their required scopes and the framework enforces them automatically —
//! no imperative checks inside tool handlers.

use adk_auth::{
    ContextScopeResolver, ScopeDenied, ScopeGuard, ScopeResolver, ScopeToolExt,
    StaticScopeResolver, check_scopes,
};
use adk_core::{
    Artifacts, CallbackContext, Content, EventActions, MemoryEntry, ReadonlyContext, Tool,
    ToolContext,
};
use async_trait::async_trait;
use serde_json::{Value, json};
use std::sync::{Arc, Mutex};

// =============================================================================
// Mock tools — note: NO imperative security checks inside execute()
// =============================================================================

/// A tool that requires finance:write and verified scopes.
struct TransferFundsTool;

#[async_trait]
impl Tool for TransferFundsTool {
    fn name(&self) -> &str {
        "transfer_funds"
    }

    fn description(&self) -> &str {
        "Transfer funds between accounts"
    }

    fn required_scopes(&self) -> &[&str] {
        &["finance:write", "verified"]
    }

    async fn execute(&self, _ctx: Arc<dyn ToolContext>, args: Value) -> adk_core::Result<Value> {
        // No security checks here — the framework handles it
        Ok(json!({
            "status": "transferred",
            "amount": args["amount"],
        }))
    }
}

/// A tool that requires admin scope.
struct AdminPanelTool;

#[async_trait]
impl Tool for AdminPanelTool {
    fn name(&self) -> &str {
        "admin_panel"
    }

    fn description(&self) -> &str {
        "Access admin panel"
    }

    fn required_scopes(&self) -> &[&str] {
        &["admin"]
    }

    async fn execute(&self, _ctx: Arc<dyn ToolContext>, _args: Value) -> adk_core::Result<Value> {
        Ok(json!({ "status": "admin_access_granted" }))
    }
}

/// A tool with no scope requirements — open to everyone.
struct PublicSearchTool;

#[async_trait]
impl Tool for PublicSearchTool {
    fn name(&self) -> &str {
        "search"
    }

    fn description(&self) -> &str {
        "Public search tool"
    }

    // No required_scopes override — defaults to &[] (open)

    async fn execute(&self, _ctx: Arc<dyn ToolContext>, args: Value) -> adk_core::Result<Value> {
        Ok(json!({ "results": [], "query": args["query"] }))
    }
}

// =============================================================================
// Mock context with configurable scopes
// =============================================================================

struct ScopedMockContext {
    user_id: String,
    scopes: Vec<String>,
    content: Content,
    actions: Mutex<EventActions>,
}

impl ScopedMockContext {
    fn create(user_id: &str, scopes: Vec<&str>) -> Arc<dyn ToolContext> {
        Arc::new(Self {
            user_id: user_id.into(),
            scopes: scopes.into_iter().map(String::from).collect(),
            content: Content::new("user"),
            actions: Mutex::new(EventActions::default()),
        })
    }
}

#[async_trait]
impl ReadonlyContext for ScopedMockContext {
    fn invocation_id(&self) -> &str {
        "test-invocation"
    }
    fn agent_name(&self) -> &str {
        "test-agent"
    }
    fn user_id(&self) -> &str {
        &self.user_id
    }
    fn app_name(&self) -> &str {
        "test-app"
    }
    fn session_id(&self) -> &str {
        "test-session"
    }
    fn branch(&self) -> &str {
        ""
    }
    fn user_content(&self) -> &Content {
        &self.content
    }
}

#[async_trait]
impl CallbackContext for ScopedMockContext {
    fn artifacts(&self) -> Option<Arc<dyn Artifacts>> {
        None
    }
}

#[async_trait]
impl ToolContext for ScopedMockContext {
    fn function_call_id(&self) -> &str {
        "test-call-id"
    }
    fn actions(&self) -> EventActions {
        self.actions.lock().unwrap().clone()
    }
    fn set_actions(&self, actions: EventActions) {
        *self.actions.lock().unwrap() = actions;
    }
    async fn search_memory(&self, _query: &str) -> adk_core::Result<Vec<MemoryEntry>> {
        Ok(vec![])
    }
    fn user_scopes(&self) -> Vec<String> {
        self.scopes.clone()
    }
}

// =============================================================================
// check_scopes() unit tests
// =============================================================================

#[test]
fn test_check_scopes_no_requirements() {
    // Tools with no scope requirements always pass
    assert!(check_scopes(&[], &[]).is_ok());
    assert!(check_scopes(&[], &["admin".into()]).is_ok());
}

#[test]
fn test_check_scopes_all_satisfied() {
    let granted = vec!["finance:read".into(), "finance:write".into(), "verified".into()];
    assert!(check_scopes(&["finance:write", "verified"], &granted).is_ok());
}

#[test]
fn test_check_scopes_superset_granted() {
    // User has more scopes than required — should pass
    let granted = vec!["admin".into(), "finance:write".into(), "verified".into()];
    assert!(check_scopes(&["finance:write"], &granted).is_ok());
}

#[test]
fn test_check_scopes_partial_missing() {
    let granted = vec!["finance:read".into()];
    let err = check_scopes(&["finance:read", "finance:write"], &granted).unwrap_err();
    assert_eq!(err.missing, vec!["finance:write"]);
    assert_eq!(err.required, vec!["finance:read", "finance:write"]);
}

#[test]
fn test_check_scopes_all_missing() {
    let err = check_scopes(&["admin", "superuser"], &[]).unwrap_err();
    assert_eq!(err.missing.len(), 2);
    assert!(err.missing.contains(&"admin".to_string()));
    assert!(err.missing.contains(&"superuser".to_string()));
}

#[test]
fn test_scope_denied_display_message() {
    let denied = ScopeDenied {
        required: vec!["finance:write".into(), "verified".into()],
        missing: vec!["verified".into()],
    };
    let msg = denied.to_string();
    assert!(msg.contains("missing required scopes"));
    assert!(msg.contains("verified"));
    assert!(msg.contains("finance:write"));
}

// =============================================================================
// Tool::required_scopes() declarative tests
// =============================================================================

#[test]
fn test_tool_declares_scopes() {
    let tool = TransferFundsTool;
    assert_eq!(tool.required_scopes(), &["finance:write", "verified"]);
}

#[test]
fn test_tool_no_scopes_by_default() {
    let tool = PublicSearchTool;
    assert!(tool.required_scopes().is_empty());
}

#[test]
fn test_admin_tool_declares_admin_scope() {
    let tool = AdminPanelTool;
    assert_eq!(tool.required_scopes(), &["admin"]);
}

// =============================================================================
// ScopeGuard integration tests
// =============================================================================

#[tokio::test]
async fn test_scope_guard_allows_when_scopes_satisfied() {
    let guard = ScopeGuard::new(ContextScopeResolver);
    let protected = guard.protect(TransferFundsTool);

    // User has both required scopes
    let ctx = ScopedMockContext::create("alice", vec!["finance:write", "verified"]);
    let result = protected.execute(ctx, json!({"amount": 100})).await;

    assert!(result.is_ok());
    assert_eq!(result.unwrap()["status"], "transferred");
}

#[tokio::test]
async fn test_scope_guard_denies_when_scopes_missing() {
    let guard = ScopeGuard::new(ContextScopeResolver);
    let protected = guard.protect(TransferFundsTool);

    // User only has finance:read, missing finance:write and verified
    let ctx = ScopedMockContext::create("bob", vec!["finance:read"]);
    let result = protected.execute(ctx, json!({"amount": 100})).await;

    assert!(result.is_err());
    let err_msg = result.unwrap_err().to_string();
    assert!(err_msg.contains("missing required scopes"));
}

#[tokio::test]
async fn test_scope_guard_denies_with_no_scopes() {
    let guard = ScopeGuard::new(ContextScopeResolver);
    let protected = guard.protect(AdminPanelTool);

    // User has zero scopes
    let ctx = ScopedMockContext::create("anonymous", vec![]);
    let result = protected.execute(ctx, json!({})).await;

    assert!(result.is_err());
}

#[tokio::test]
async fn test_scope_guard_passthrough_for_unscoped_tools() {
    let guard = ScopeGuard::new(ContextScopeResolver);
    let protected = guard.protect(PublicSearchTool);

    // Even a user with no scopes can use an unscoped tool
    let ctx = ScopedMockContext::create("anonymous", vec![]);
    let result = protected.execute(ctx, json!({"query": "hello"})).await;

    assert!(result.is_ok());
    assert_eq!(result.unwrap()["query"], "hello");
}

#[tokio::test]
async fn test_scope_guard_superset_scopes_allowed() {
    let guard = ScopeGuard::new(ContextScopeResolver);
    let protected = guard.protect(TransferFundsTool);

    // User has more scopes than required — should still work
    let ctx = ScopedMockContext::create(
        "superuser",
        vec!["admin", "finance:read", "finance:write", "verified"],
    );
    let result = protected.execute(ctx, json!({"amount": 500})).await;

    assert!(result.is_ok());
}

// =============================================================================
// StaticScopeResolver tests
// =============================================================================

#[tokio::test]
async fn test_static_resolver_grants_fixed_scopes() {
    let resolver = StaticScopeResolver::new(vec!["finance:write", "verified"]);
    let guard = ScopeGuard::new(resolver);
    let protected = guard.protect(TransferFundsTool);

    // Context scopes don't matter — static resolver overrides
    let ctx = ScopedMockContext::create("anyone", vec![]);
    let result = protected.execute(ctx, json!({"amount": 50})).await;

    assert!(result.is_ok());
}

#[tokio::test]
async fn test_static_resolver_insufficient_scopes() {
    let resolver = StaticScopeResolver::new(vec!["finance:read"]);
    let guard = ScopeGuard::new(resolver);
    let protected = guard.protect(TransferFundsTool);

    let ctx = ScopedMockContext::create("anyone", vec![]);
    let result = protected.execute(ctx, json!({"amount": 50})).await;

    assert!(result.is_err());
}

// =============================================================================
// ScopeToolExt convenience tests
// =============================================================================

#[tokio::test]
async fn test_scope_tool_ext_shorthand() {
    // .with_scope_guard() is the ergonomic one-liner
    let protected = TransferFundsTool.with_scope_guard(ContextScopeResolver);

    let ctx = ScopedMockContext::create("alice", vec!["finance:write", "verified"]);
    let result = protected.execute(ctx, json!({"amount": 200})).await;

    assert!(result.is_ok());
}

#[tokio::test]
async fn test_scope_tool_ext_denied() {
    let protected = AdminPanelTool.with_scope_guard(ContextScopeResolver);

    let ctx = ScopedMockContext::create("bob", vec!["user"]);
    let result = protected.execute(ctx, json!({})).await;

    assert!(result.is_err());
}

// =============================================================================
// protect_all() batch tests
// =============================================================================

#[tokio::test]
async fn test_protect_all_mixed_tools() {
    let guard = ScopeGuard::new(ContextScopeResolver);

    let tools: Vec<Arc<dyn Tool>> =
        vec![Arc::new(PublicSearchTool), Arc::new(TransferFundsTool), Arc::new(AdminPanelTool)];

    let protected = guard.protect_all(tools);
    assert_eq!(protected.len(), 3);

    // User with finance scopes but not admin
    let ctx = ScopedMockContext::create("finance_user", vec!["finance:write", "verified"]);

    // Public tool: allowed (no scopes required)
    assert!(protected[0].execute(ctx.clone(), json!({})).await.is_ok());

    // Transfer tool: allowed (has finance:write + verified)
    assert!(protected[1].execute(ctx.clone(), json!({"amount": 100})).await.is_ok());

    // Admin tool: denied (missing admin scope)
    assert!(protected[2].execute(ctx, json!({})).await.is_err());
}

// =============================================================================
// Metadata preservation tests
// =============================================================================

#[test]
fn test_scoped_tool_preserves_metadata() {
    let guard = ScopeGuard::new(ContextScopeResolver);
    let protected = guard.protect(TransferFundsTool);

    assert_eq!(protected.name(), "transfer_funds");
    assert_eq!(protected.description(), "Transfer funds between accounts");
    assert_eq!(protected.required_scopes(), &["finance:write", "verified"]);
    assert!(!protected.is_long_running());
}

#[test]
fn test_scoped_dyn_tool_preserves_metadata() {
    let guard = ScopeGuard::new(ContextScopeResolver);
    let tools: Vec<Arc<dyn Tool>> = vec![Arc::new(AdminPanelTool)];
    let protected = guard.protect_all(tools);

    assert_eq!(protected[0].name(), "admin_panel");
    assert_eq!(protected[0].description(), "Access admin panel");
    assert_eq!(protected[0].required_scopes(), &["admin"]);
}

// =============================================================================
// Custom ScopeResolver test
// =============================================================================

/// A resolver that maps user IDs to scopes (simulating a database lookup).
struct UserDatabaseResolver {
    user_scopes: std::collections::HashMap<String, Vec<String>>,
}

#[async_trait]
impl ScopeResolver for UserDatabaseResolver {
    async fn resolve(&self, ctx: &dyn ToolContext) -> Vec<String> {
        self.user_scopes.get(ctx.user_id()).cloned().unwrap_or_default()
    }
}

#[tokio::test]
async fn test_custom_resolver_per_user_scopes() {
    let mut user_scopes = std::collections::HashMap::new();
    user_scopes.insert("alice".to_string(), vec!["admin".to_string()]);
    user_scopes.insert("bob".to_string(), vec!["finance:read".to_string()]);

    let resolver = UserDatabaseResolver { user_scopes };
    let guard = ScopeGuard::new(resolver);
    let protected = guard.protect(AdminPanelTool);

    // Alice has admin scope
    let alice_ctx = ScopedMockContext::create("alice", vec![]);
    assert!(protected.execute(alice_ctx, json!({})).await.is_ok());

    // Bob does not have admin scope
    let bob_ctx = ScopedMockContext::create("bob", vec![]);
    assert!(protected.execute(bob_ctx, json!({})).await.is_err());
}