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
/*!
* Active-identity resolution over the config v2 [`Account`] model.
*
* A community presents a persona; many communities may present the **same**
* persona (D1 reuse). Because the DIDComm layer keys connections by DID and a
* reused persona must share one connection, the **session key is the persona**,
* not the community (see `docs/design/t1-active-identity-api.md`).
*
* This is the metadata-only routing layer: it resolves communities → personas
* and groups them by the persona session that serves them. The runtime-heavy
* `IdentityContext` (resolved DID Document, ATM messaging profile, key/secret
* loading) is layered on during config load in a later T1 commit; this module
* gives the session manager the persona-keyed routing it needs without runtime
* state.
*/
use crate::config::account::{Account, CommunityRecord, PersonaId, PersonaRecord, VtcDid};
use affinidi_tdk::{did_common::Document, messaging::profiles::ATMProfile};
use std::collections::{HashMap, HashSet};
use std::sync::Arc;
/// A resolved community membership paired with the persona it presents.
///
/// Borrows from the owning [`Account`]; cheap to construct.
#[derive(Clone, Copy, Debug)]
pub struct IdentityRef<'a> {
/// The community membership.
pub community: &'a CommunityRecord,
/// The persona presented to this community.
pub persona: &'a PersonaRecord,
}
impl<'a> IdentityRef<'a> {
/// The persona's `did:webvh`.
pub fn persona_did(&self) -> &'a str {
&self.persona.did
}
/// The DIDComm session key for this membership — the **persona** id.
/// Memberships sharing a persona share one session.
pub fn session_key(&self) -> PersonaId {
self.persona.persona_id
}
/// The persona's mediator DID, if set.
pub fn mediator_did(&self) -> Option<&'a str> {
self.persona.mediator_did.as_deref()
}
}
/// Read-only index over an [`Account`] that resolves communities to the persona
/// identity they present and groups them by persona session.
#[derive(Clone, Copy, Debug)]
pub struct IdentityRegistry<'a> {
account: &'a Account,
}
impl<'a> IdentityRegistry<'a> {
/// Build a registry view over an account. Cheap (borrows).
pub fn new(account: &'a Account) -> Self {
Self { account }
}
/// Resolve a specific membership (community `vtc` presented as `persona`) to
/// its identity. Returns `None` if there is no such membership or its
/// `persona_ref` dangles.
pub fn get(&self, vtc: &str, persona: PersonaId) -> Option<IdentityRef<'a>> {
let community = self
.account
.communities
.get(vtc)?
.iter()
.find(|c| c.persona_ref == persona)?;
let persona = self.account.personas.get(&community.persona_ref)?;
Some(IdentityRef { community, persona })
}
/// Iterator over all resolvable memberships (skips any with a dangling
/// `persona_ref`, which [`Account::dangling_refs`] reports separately).
pub fn all(&self) -> impl Iterator<Item = IdentityRef<'a>> {
self.account
.communities
.values()
.flatten()
.filter_map(move |c| {
self.account
.personas
.get(&c.persona_ref)
.map(|persona| IdentityRef {
community: c,
persona,
})
})
}
/// Memberships in the `Active` (live) state.
pub fn active(&self) -> impl Iterator<Item = IdentityRef<'a>> {
self.all().filter(|r| r.community.status.is_active())
}
/// Persona sessions that must be live, mapped to the communities they serve.
///
/// A persona needs a session if any of its communities is `Active` or
/// `Pending` ([`CommunityStatus::requires_live_session`]). A reused persona
/// appears once, serving several communities — one shared connection.
///
/// [`CommunityStatus::requires_live_session`]: crate::config::account::CommunityStatus::requires_live_session
pub fn sessions(&self) -> HashMap<PersonaId, Vec<&'a VtcDid>> {
let mut map: HashMap<PersonaId, Vec<&'a VtcDid>> = HashMap::new();
for c in self.account.communities.values().flatten() {
if c.status.requires_live_session()
&& self.account.personas.contains_key(&c.persona_ref)
{
map.entry(c.persona_ref).or_default().push(&c.vtc_did);
}
}
map
}
/// The set of persona ids that should currently have a live session.
pub fn active_session_keys(&self) -> HashSet<PersonaId> {
self.sessions().into_keys().collect()
}
}
/// A fully-resolved, runtime-ready identity: a persona plus its resolved DID
/// document and registered ATM messaging profile.
///
/// Built at config load (and at setup) and held on [`crate::config::Config`].
/// This is what consumers use to *act* as a persona — sign, send, and receive.
/// It replaces the singleton `Config.persona_did` (`PersonaDID`) as the runtime
/// identity as consumers migrate onto it.
#[derive(Clone, Debug)]
pub struct IdentityContext {
/// Stable id of the persona this context resolves.
pub persona_id: PersonaId,
/// The persona's `did:webvh`.
pub did: String,
/// Resolved DID document.
pub document: Document,
/// Registered ATM messaging profile for this persona (not connected here;
/// the DIDComm session manager owns connections).
pub profile: Arc<ATMProfile>,
/// The persona's mediator DID, if any.
pub mediator_did: Option<String>,
}
impl IdentityContext {
/// The persona's `did:webvh`.
pub fn persona_did(&self) -> &str {
&self.did
}
/// The resolved DID document.
pub fn document(&self) -> &Document {
&self.document
}
/// The persona's ATM messaging profile.
pub fn profile(&self) -> &Arc<ATMProfile> {
&self.profile
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::config::account::{CommunityStatus, KeyRef, PersonaRecord};
use chrono::Utc;
use uuid::Uuid;
fn persona() -> PersonaRecord {
PersonaRecord {
extra: serde_json::Map::new(),
persona_id: PersonaId::new(),
did: "did:webvh:example:p".into(),
did_document: None,
key_refs: Vec::<KeyRef>::new(),
mediator_did: Some("did:webvh:mediator".into()),
origin_context_id: "openvtc/p".into(),
created_at: Utc::now(),
label: None,
}
}
fn community(vtc: &str, persona_ref: PersonaId, status: CommunityStatus) -> CommunityRecord {
CommunityRecord {
member_vmc: None,
decision: None,
relationship_identifier_default: None,
retired_credentials: Vec::new(),
extra: serde_json::Map::new(),
vtc_did: vtc.into(),
display_name: None,
sub_context_id: format!("openvtc/{vtc}"),
submit_transport: None,
request_id_confirmed: false,
persona_ref,
status,
favourite: false,
archived: false,
acknowledged: false,
member_since: None,
requested_at: None,
receipt_at: None,
approved_at: None,
credential_delivery: None,
relationships: Default::default(),
tasks: Default::default(),
vrcs_issued: Default::default(),
vrcs_received: Default::default(),
credentials: Default::default(),
}
}
#[test]
fn resolves_and_exposes_session_key() {
let mut acct = Account::default();
let p = persona();
let pid = p.persona_id;
acct.personas.insert(pid, p);
acct.add_membership(community("a", pid, CommunityStatus::Active));
let reg = IdentityRegistry::new(&acct);
let id = reg.get("a", pid).expect("resolves");
assert_eq!(id.session_key(), pid);
assert_eq!(id.persona_did(), "did:webvh:example:p");
assert_eq!(id.mediator_did(), Some("did:webvh:mediator"));
}
#[test]
fn reused_persona_shares_one_session() {
let mut acct = Account::default();
let p = persona();
let pid = p.persona_id;
acct.personas.insert(pid, p);
// Two Active communities present the SAME persona.
acct.add_membership(community("a", pid, CommunityStatus::Active));
acct.add_membership(community("b", pid, CommunityStatus::Active));
let reg = IdentityRegistry::new(&acct);
let sessions = reg.sessions();
assert_eq!(sessions.len(), 1, "one shared persona session");
assert_eq!(sessions[&pid].len(), 2, "serving both communities");
assert_eq!(reg.active_session_keys().len(), 1);
}
#[test]
fn pending_needs_session_inactive_does_not() {
let mut acct = Account::default();
let p1 = persona();
let p2 = persona();
let (id1, id2) = (p1.persona_id, p2.persona_id);
acct.personas.insert(id1, p1);
acct.personas.insert(id2, p2);
// p1 → Pending (needs session); p2 → Left (does not).
acct.add_membership(community(
"pending",
id1,
CommunityStatus::Pending {
request_id: Uuid::new_v4(),
},
));
acct.add_membership(community("left", id2, CommunityStatus::Left));
let reg = IdentityRegistry::new(&acct);
let keys = reg.active_session_keys();
assert!(keys.contains(&id1), "pending persona needs a session");
assert!(!keys.contains(&id2), "left persona needs no session");
assert_eq!(reg.active().count(), 0, "no Active communities here");
}
#[test]
fn dangling_ref_is_skipped() {
let mut acct = Account::default();
// Community references a persona that doesn't exist.
let ghost = PersonaId::new();
acct.add_membership(community("x", ghost, CommunityStatus::Active));
let reg = IdentityRegistry::new(&acct);
assert!(reg.get("x", ghost).is_none());
assert_eq!(reg.all().count(), 0);
assert!(reg.sessions().is_empty());
}
}