solid-pod-rs 0.4.0-alpha.4

Rust-native Solid Pod server library — LDP, WAC, WebID, Solid-OIDC, Solid Notifications, NIP-98. Framework-agnostic.
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
//! WAC 2.0 conditions framework tests.
//!
//! WAC 2.0 (https://webacl.org/secure-access-conditions/) extends the
//! WAC ontology with `acl:condition` triples. The tests below verify:
//!
//! 1. `acl:ClientCondition` grants when the client matches.
//! 2. `acl:ClientCondition` denies when the client does not match.
//! 3. `acl:IssuerCondition` grants via issuer group membership.
//! 4. Unknown condition types fail closed (NotApplicable, not Satisfied).
//! 5. Conjunctive AND semantics across multiple conditions.
//! 6. Monotonicity invariant — conditions can only restrict.
//! 7. `WAC-Allow` omits modes gated by unsatisfiable conditions.

use solid_pod_rs::wac::{
    evaluate_access_ctx, wac_allow_header_with_dispatcher, AccessMode, AclAuthorization,
    AclDocument, ClientConditionBody, Condition, ConditionOutcome, ConditionRegistry,
    IdOrIds, IdRef, IssuerConditionBody, RequestContext, StaticGroupMembership,
};

fn owner_rule_with_condition(
    agent: &str,
    path: &str,
    mode: &str,
    cond: Option<Vec<Condition>>,
) -> AclAuthorization {
    AclAuthorization {
        id: None,
        r#type: None,
        agent: Some(IdOrIds::Single(IdRef { id: agent.into() })),
        agent_class: None,
        agent_group: None,
        origin: None,
        access_to: Some(IdOrIds::Single(IdRef { id: path.into() })),
        default: None,
        mode: Some(IdOrIds::Single(IdRef { id: mode.into() })),
        condition: cond,
    }
}

fn doc_with(authzs: Vec<AclAuthorization>) -> AclDocument {
    AclDocument {
        context: None,
        graph: Some(authzs),
    }
}

// ---------------------------------------------------------------------------
// 1. ClientCondition match → grant
// ---------------------------------------------------------------------------

#[test]
fn wac2_acl_condition_client_matches_permits() {
    let cond = Condition::Client(ClientConditionBody {
        client: Some(IdOrIds::Single(IdRef {
            id: "https://app.example/webid#client".into(),
        })),
        client_group: None,
        client_class: None,
    });
    let doc = doc_with(vec![owner_rule_with_condition(
        "did:nostr:alice",
        "/private/note",
        "acl:Read",
        Some(vec![cond]),
    )]);
    let registry = ConditionRegistry::default_with_client_and_issuer();
    let ctx = RequestContext {
        web_id: Some("did:nostr:alice"),
        client_id: Some("https://app.example/webid#client"),
        issuer: None,
    };
    assert!(evaluate_access_ctx(
        Some(&doc),
        &ctx,
        "/private/note",
        AccessMode::Read,
        None,
        &StaticGroupMembership::new(),
        &registry,
    ));
}

// ---------------------------------------------------------------------------
// 2. ClientCondition mismatch → deny
// ---------------------------------------------------------------------------

#[test]
fn wac2_acl_condition_client_mismatch_denies() {
    let cond = Condition::Client(ClientConditionBody {
        client: Some(IdOrIds::Single(IdRef {
            id: "https://app.example/webid#client".into(),
        })),
        client_group: None,
        client_class: None,
    });
    let doc = doc_with(vec![owner_rule_with_condition(
        "did:nostr:alice",
        "/private/note",
        "acl:Read",
        Some(vec![cond]),
    )]);
    let registry = ConditionRegistry::default_with_client_and_issuer();
    let ctx = RequestContext {
        web_id: Some("did:nostr:alice"),
        client_id: Some("https://evil.example/webid#client"),
        issuer: None,
    };
    assert!(!evaluate_access_ctx(
        Some(&doc),
        &ctx,
        "/private/note",
        AccessMode::Read,
        None,
        &StaticGroupMembership::new(),
        &registry,
    ));
}

// ---------------------------------------------------------------------------
// 3. IssuerCondition with group membership → grant
// ---------------------------------------------------------------------------

#[test]
fn wac2_acl_condition_issuer_group_membership() {
    let cond = Condition::Issuer(IssuerConditionBody {
        issuer: None,
        issuer_group: Some(IdOrIds::Single(IdRef {
            id: "https://issuers.example/trusted".into(),
        })),
        issuer_class: None,
    });
    let doc = doc_with(vec![owner_rule_with_condition(
        "did:nostr:alice",
        "/private/note",
        "acl:Read",
        Some(vec![cond]),
    )]);
    let mut groups = StaticGroupMembership::new();
    groups.add(
        "https://issuers.example/trusted",
        vec!["https://idp.example/".into()],
    );
    let registry = ConditionRegistry::default_with_client_and_issuer();
    let ctx = RequestContext {
        web_id: Some("did:nostr:alice"),
        client_id: None,
        issuer: Some("https://idp.example/"),
    };
    assert!(evaluate_access_ctx(
        Some(&doc),
        &ctx,
        "/private/note",
        AccessMode::Read,
        None,
        &groups,
        &registry,
    ));
}

// ---------------------------------------------------------------------------
// 4. Unknown condition type fails closed
// ---------------------------------------------------------------------------

#[test]
fn wac2_unknown_condition_type_fails_closed() {
    // Parse a JSON-LD doc that carries an unknown condition type.
    // serde(other) catches it → Condition::Unknown → NotApplicable.
    let json = r##"{
        "@graph": [{
            "acl:agent": {"@id": "did:nostr:alice"},
            "acl:accessTo": {"@id": "/private/note"},
            "acl:mode": {"@id": "acl:Read"},
            "acl:condition": [{
                "@type": "acl:UnknownCondition"
            }]
        }]
    }"##;
    let doc: AclDocument = serde_json::from_str(json).expect("parse");
    let registry = ConditionRegistry::default_with_client_and_issuer();
    let ctx = RequestContext {
        web_id: Some("did:nostr:alice"),
        client_id: Some("https://app.example/webid#client"),
        issuer: Some("https://idp.example/"),
    };
    assert!(
        !evaluate_access_ctx(
            Some(&doc),
            &ctx,
            "/private/note",
            AccessMode::Read,
            None,
            &StaticGroupMembership::new(),
            &registry,
        ),
        "unknown condition type must fail closed (NotApplicable)"
    );
}

// ---------------------------------------------------------------------------
// 5. Conjunctive AND: Client OK + Issuer FAIL → deny
// ---------------------------------------------------------------------------

#[test]
fn wac2_conjunctive_conditions_and_gate() {
    let client_cond = Condition::Client(ClientConditionBody {
        client: Some(IdOrIds::Single(IdRef {
            id: "https://app.example/webid#client".into(),
        })),
        client_group: None,
        client_class: None,
    });
    let issuer_cond = Condition::Issuer(IssuerConditionBody {
        issuer: Some(IdOrIds::Single(IdRef {
            id: "https://trusted.idp/".into(),
        })),
        issuer_group: None,
        issuer_class: None,
    });
    let doc = doc_with(vec![owner_rule_with_condition(
        "did:nostr:alice",
        "/private/note",
        "acl:Read",
        Some(vec![client_cond, issuer_cond]),
    )]);
    let registry = ConditionRegistry::default_with_client_and_issuer();
    // Client OK, issuer NOT OK → deny.
    let ctx = RequestContext {
        web_id: Some("did:nostr:alice"),
        client_id: Some("https://app.example/webid#client"),
        issuer: Some("https://untrusted.idp/"),
    };
    assert!(!evaluate_access_ctx(
        Some(&doc),
        &ctx,
        "/private/note",
        AccessMode::Read,
        None,
        &StaticGroupMembership::new(),
        &registry,
    ));
    // Both OK → grant.
    let ctx_ok = RequestContext {
        web_id: Some("did:nostr:alice"),
        client_id: Some("https://app.example/webid#client"),
        issuer: Some("https://trusted.idp/"),
    };
    assert!(evaluate_access_ctx(
        Some(&doc),
        &ctx_ok,
        "/private/note",
        AccessMode::Read,
        None,
        &StaticGroupMembership::new(),
        &registry,
    ));
}

// ---------------------------------------------------------------------------
// 6. Monotonicity invariant
// ---------------------------------------------------------------------------

#[test]
fn wac2_monotonicity_invariant() {
    // Rule WITHOUT conditions — always grants for matching agent/mode/path.
    let rule_plain = owner_rule_with_condition(
        "did:nostr:alice",
        "/private/note",
        "acl:Read",
        None,
    );
    let doc_plain = doc_with(vec![rule_plain.clone()]);

    // Rule WITH always-true condition (client matches).
    let always_true = Condition::Client(ClientConditionBody {
        client: Some(IdOrIds::Single(IdRef {
            id: "https://app.example/webid#client".into(),
        })),
        client_group: None,
        client_class: None,
    });
    let rule_true = owner_rule_with_condition(
        "did:nostr:alice",
        "/private/note",
        "acl:Read",
        Some(vec![always_true]),
    );
    let doc_true = doc_with(vec![rule_true]);

    // Rule WITH always-false condition (client mismatch).
    let always_false = Condition::Client(ClientConditionBody {
        client: Some(IdOrIds::Single(IdRef {
            id: "https://never-match.example/webid#client".into(),
        })),
        client_group: None,
        client_class: None,
    });
    let rule_false = owner_rule_with_condition(
        "did:nostr:alice",
        "/private/note",
        "acl:Read",
        Some(vec![always_false]),
    );
    let doc_false = doc_with(vec![rule_false]);

    let registry = ConditionRegistry::default_with_client_and_issuer();
    let ctx = RequestContext {
        web_id: Some("did:nostr:alice"),
        client_id: Some("https://app.example/webid#client"),
        issuer: None,
    };

    let plain = evaluate_access_ctx(
        Some(&doc_plain),
        &ctx,
        "/private/note",
        AccessMode::Read,
        None,
        &StaticGroupMembership::new(),
        &registry,
    );
    let satisfied = evaluate_access_ctx(
        Some(&doc_true),
        &ctx,
        "/private/note",
        AccessMode::Read,
        None,
        &StaticGroupMembership::new(),
        &registry,
    );
    let denied = evaluate_access_ctx(
        Some(&doc_false),
        &ctx,
        "/private/note",
        AccessMode::Read,
        None,
        &StaticGroupMembership::new(),
        &registry,
    );

    // Plain grant.
    assert!(plain);
    // Satisfied condition == plain rule (no further restriction).
    assert_eq!(plain, satisfied, "satisfied condition must be identical to no condition");
    // Unsatisfiable condition strictly narrower.
    assert!(!denied, "unsatisfiable condition must strictly narrow the grant");
}

// ---------------------------------------------------------------------------
// 7. WAC-Allow header omits gated modes
// ---------------------------------------------------------------------------

#[test]
fn wac2_wac_allow_header_omits_gated_modes() {
    // Rule grants Write only if client matches. In a request where
    // the client mismatches, Write must NOT appear in user="...".
    let gated_write = Condition::Client(ClientConditionBody {
        client: Some(IdOrIds::Single(IdRef {
            id: "https://only.example/webid#client".into(),
        })),
        client_group: None,
        client_class: None,
    });
    let rule_write = owner_rule_with_condition(
        "did:nostr:alice",
        "/private/note",
        "acl:Write",
        Some(vec![gated_write]),
    );
    // Also grant ungated Read so user="" still has something.
    let rule_read = owner_rule_with_condition(
        "did:nostr:alice",
        "/private/note",
        "acl:Read",
        None,
    );
    let doc = doc_with(vec![rule_read, rule_write]);
    let registry = ConditionRegistry::default_with_client_and_issuer();
    let ctx = RequestContext {
        web_id: Some("did:nostr:alice"),
        client_id: Some("https://not-the-one.example/webid#client"),
        issuer: None,
    };
    let hdr = wac_allow_header_with_dispatcher(
        Some(&doc),
        &ctx,
        "/private/note",
        &StaticGroupMembership::new(),
        &registry,
    );
    assert!(hdr.contains("user=\"read\""), "expected user=\"read\", got {hdr}");
    assert!(
        !hdr.contains("write"),
        "WAC-Allow must omit write gated by unsatisfied condition; got {hdr}"
    );
}

// Sanity: the `NotApplicable` outcome is distinct from `Satisfied`.
#[test]
fn condition_outcome_variants_are_distinct() {
    assert_ne!(ConditionOutcome::Satisfied, ConditionOutcome::NotApplicable);
    assert_ne!(ConditionOutcome::Satisfied, ConditionOutcome::Denied);
    assert_ne!(ConditionOutcome::NotApplicable, ConditionOutcome::Denied);
}