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
//! The offered grant: the WAC authorisations the node offers an
//! installation's key, rendered as Turtle.
//!
//! The controller renders them at installation and stores each container's
//! text and hash; the client renders them again from the manifest and
//! compares, so the bytes must be identical on both sides. For each
//! container `C` there is one authorisation: agent the application key's
//! `did:nostr`, `acl:default <C>`, and the manifest's modes. One
//! `acl:default` rule gives the key read (and write) on `C`'s listing, its
//! existing and new children and nested containers, and nothing outside
//! `C`, not even `C`'s own ACL.
//!
//! The combined grant, whose hash consent names, is the per-container
//! texts in manifest order joined by one blank line.
use ;
use crateDidNostr;
use crateSha256Digest;
use crate;
/// The authorisation offered for one container.
/// The whole offered grant: the combined text and hash, and each
/// container's.
///
/// On the wire it is the tenant answer's `grant` member,
/// `{"turtle":str,"sha256":hex,"containers":[{"path","turtle","sha256"}]}`.
/// Renders one container's offered authorisation.
///
/// `owner` is the pod's owner; the container IRI is root-relative,
/// `</pods/<owner>/<path>>`, as the profile ACL writes its IRIs.
///
/// # Examples
///
/// ```
/// use stratum_node_core::{render_container, ContainerGrant, DidNostr};
///
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let app = DidNostr::from_hex(&"c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee5")?;
/// let owner = DidNostr::from_hex(&"79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798")?;
/// let grant: ContainerGrant =
/// serde_json::from_str(r#"{"path":"reports/","modes":["Read","Write"],"operations":["GET","PUT"]}"#)?;
/// let offered = render_container(&app, &owner, &grant);
/// assert!(offered.turtle.contains(&format!("acl:agent <{app}>")));
/// assert!(offered.turtle.contains(&format!("acl:default </pods/{}/reports/>", owner.to_hex())));
/// assert!(offered.turtle.contains("acl:mode acl:Read, acl:Write ."));
/// assert!(!offered.turtle.contains("acl:accessTo"));
/// # Ok(())
/// # }
/// ```
/// Renders the offered grant for an application key `app` in `owner`'s
/// pod, one authorisation per container in manifest order.
///
/// The same function runs in the controller and in the client, so the
/// combined hash the client checks is the one the controller stored.
///
/// # Examples
///
/// ```
/// use stratum_node_core::{render_offered, ContainerGrant, DidNostr, Sha256Digest};
///
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let app = DidNostr::from_hex(&"c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee5")?;
/// let owner = DidNostr::from_hex(&"79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798")?;
/// let grants: Vec<ContainerGrant> = serde_json::from_str(
/// r#"[{"path":"in/","modes":["Read"],"operations":["GET"]},
/// {"path":"out/","modes":["Read","Write"],"operations":["PUT"]}]"#,
/// )?;
/// let offered = render_offered(&app, &owner, &grants);
/// assert_eq!(offered.containers.len(), 2);
/// assert_eq!(
/// offered.turtle,
/// format!("{}\n{}", offered.containers[0].turtle, offered.containers[1].turtle),
/// );
/// assert_eq!(offered.sha256, Sha256Digest::of(offered.turtle.as_bytes()));
/// # Ok(())
/// # }
/// ```