@agentunion/fastaun 0.5.20 → 0.5.22
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +104 -0
- package/_packed_docs/CHANGELOG.md +104 -0
- package/_packed_docs/INDEX.md +59 -34
- package/_packed_docs/KITE_DOCS_GUIDE.md +59 -52
- package/_packed_docs/agent.md/working/AID/347/261/273/345/236/213-owner/345/205/263/347/263/273/344/270/216/346/215/242owner/347/274/226/346/216/222/346/226/271/346/241/210.md +530 -0
- package/_packed_docs/agent.md/working/AID/347/261/273/345/236/213-owner/345/205/263/347/263/273/345/256/214/346/225/264/350/256/276/350/256/241/344/270/216/345/256/236/346/226/275/350/256/241/345/210/222.md +685 -0
- package/_packed_docs/agent.md/working/agent.md-schema/344/270/216/347/255/276/345/217/221/351/252/214/347/255/276/346/265/201/347/250/213/346/276/204/346/270/205.md +349 -0
- package/_packed_docs/agent.md/working//346/234/254/345/234/260Docker/346/265/213/350/257/225Bug/346/270/205/345/215/225.md +40 -0
- package/_packed_docs/aun/345/210/206/345/270/203/345/274/217/346/265/213/350/257/225/350/277/220/350/241/214/346/214/207/345/215/227.md +307 -67
- package/_packed_docs/aun/346/265/213/350/257/225/350/277/220/350/241/214/346/214/207/345/215/227.md +41 -15
- package/_packed_docs/protocol/06-/346/234/215/345/212/241/345/215/217/350/256/256.md +1433 -1392
- package/_packed_docs/protocol/08-AUN-E2EE.md +17 -12
- package/_packed_docs/protocol/11-Storage-/345/255/220/345/215/217/350/256/256.md +30 -8
- package/_packed_docs/protocol/Ownership/345/215/217/350/256/256/345/256/232/344/271/211.md +2 -0
- package/_packed_docs/protocol/Ownership/345/215/217/350/256/256/345/256/232/344/271/2112.1.md +33 -22
- package/_packed_docs/sdk/0.5.21-SDK/345/272/224/347/224/250/351/233/206/346/210/220/346/214/207/345/215/227.md +368 -0
- package/_packed_docs/sdk/0.5.21-SDK/345/274/200/345/217/221/344/270/216/347/247/273/346/244/215/346/214/207/345/215/227.md +350 -0
- package/_packed_docs/sdk/0.5.22-SDK/345/272/224/347/224/250/351/233/206/346/210/220/346/214/207/345/215/227.md +212 -0
- package/_packed_docs/sdk/0.5.22-SDK/345/274/200/345/217/221/344/270/216/347/247/273/346/244/215/346/214/207/345/215/227.md +199 -0
- package/_packed_docs/sdk/01-/345/277/253/351/200/237/345/274/200/345/247/213.md +1 -1
- package/_packed_docs/sdk/02-WebSocket/345/215/217/350/256/256.md +5 -1
- package/_packed_docs/sdk/03-/346/240/270/345/277/203/346/246/202/345/277/265.md +64 -64
- package/_packed_docs/sdk/04-/350/277/236/346/216/245/344/270/216/350/256/244/350/257/201.md +4 -1
- package/_packed_docs/sdk/05-E2EE/345/212/240/345/257/206/351/200/232/344/277/241.md +42 -39
- package/_packed_docs/sdk/06-API/346/211/213/345/206/214.md +163 -14
- package/_packed_docs/sdk/08-/346/234/200/344/275/263/345/256/236/350/267/265.md +48 -47
- package/_packed_docs/sdk/09-group-rpc-manual.md +15 -2
- package/_packed_docs/sdk/09-message-rpc-manual.md +250 -160
- package/_packed_docs/sdk/09-storage-rpc-manual.md +64 -7
- package/_packed_docs/sdk/09-/345/207/255/350/257/201/346/234/215/345/212/241RPC/346/211/213/345/206/214.md +73 -0
- package/_packed_docs/sdk/09-/347/273/204/347/273/207/346/234/215/345/212/241RPC/346/211/213/345/206/214.md +90 -0
- package/_packed_docs/sdk/AUN_DOCS_GUIDE.md +33 -25
- package/_packed_docs/sdk/INDEX.md +67 -33
- package/_packed_docs/sdk/Ownership-SDK/347/274/226/346/216/222/346/214/207/345/215/2272.1.md +74 -57
- package/_packed_docs/sdk/Ownership2.1-SDK/345/272/224/347/224/250/351/233/206/346/210/220/346/214/207/345/215/227.md +312 -300
- package/_packed_docs/sdk/Ownership2.1-SDK/345/274/200/345/217/221/344/270/216/347/247/273/346/244/215/346/214/207/345/215/227.md +322 -382
- package/_packed_docs/sdk/Ownership2.1-TDD/345/210/206/351/230/266/346/256/265/345/256/236/346/226/275/350/256/241/345/210/222.md +708 -708
- package/_packed_docs/sdk/Ownership2.1/350/257/246/347/273/206/350/256/276/350/256/241.md +28 -20
- package/_packed_docs/sdk//345/207/255/350/257/201/344/270/216/347/273/204/347/273/207/346/234/215/345/212/241SDK/345/272/224/347/224/250/351/233/206/346/210/220/346/214/207/345/215/227.md +190 -97
- package/_packed_docs/sdk//345/207/255/350/257/201/344/270/216/347/273/204/347/273/207/346/234/215/345/212/241SDK/345/274/200/345/217/221/344/270/216/347/247/273/346/244/215/346/214/207/345/215/227.md +158 -127
- package/dist/agent-md-schema.js +6 -4
- package/dist/agent-md-schema.js.map +1 -1
- package/dist/agent-md.d.ts +22 -2
- package/dist/agent-md.js +92 -68
- package/dist/agent-md.js.map +1 -1
- package/dist/aid-store.d.ts +13 -0
- package/dist/aid-store.js +50 -15
- package/dist/aid-store.js.map +1 -1
- package/dist/auth.d.ts +1 -0
- package/dist/auth.js +22 -2
- package/dist/auth.js.map +1 -1
- package/dist/cert-utils.d.ts +1 -0
- package/dist/cert-utils.js +13 -0
- package/dist/cert-utils.js.map +1 -1
- package/dist/client/delivery.d.ts +87 -1
- package/dist/client/delivery.js +1387 -139
- package/dist/client/delivery.js.map +1 -1
- package/dist/client/group-state.js +3 -0
- package/dist/client/group-state.js.map +1 -1
- package/dist/client/rpc-pipeline.d.ts +4 -1
- package/dist/client/rpc-pipeline.js +149 -37
- package/dist/client/rpc-pipeline.js.map +1 -1
- package/dist/client/v2-e2ee.d.ts +12 -2
- package/dist/client/v2-e2ee.js +831 -304
- package/dist/client/v2-e2ee.js.map +1 -1
- package/dist/client.d.ts +26 -2
- package/dist/client.js +412 -68
- package/dist/client.js.map +1 -1
- package/dist/config.d.ts +4 -0
- package/dist/config.js +1 -0
- package/dist/config.js.map +1 -1
- package/dist/events.d.ts +18 -1
- package/dist/events.js +148 -9
- package/dist/events.js.map +1 -1
- package/dist/facades.d.ts +43 -1
- package/dist/facades.js +363 -7
- package/dist/facades.js.map +1 -1
- package/dist/group-fs.d.ts +1 -0
- package/dist/group-fs.js +37 -19
- package/dist/group-fs.js.map +1 -1
- package/dist/index.d.ts +5 -3
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/keystore/aid-db.d.ts +6 -1
- package/dist/keystore/aid-db.js +184 -42
- package/dist/keystore/aid-db.js.map +1 -1
- package/dist/keystore/index.d.ts +14 -0
- package/dist/keystore/local-token-store.d.ts +9 -1
- package/dist/keystore/local-token-store.js +79 -14
- package/dist/keystore/local-token-store.js.map +1 -1
- package/dist/org-protocol.d.ts +46 -0
- package/dist/org-protocol.js +802 -12
- package/dist/org-protocol.js.map +1 -1
- package/dist/ownership-v21.d.ts +36 -5
- package/dist/ownership-v21.js +168 -52
- package/dist/ownership-v21.js.map +1 -1
- package/dist/ownership-workflow.d.ts +24 -2
- package/dist/ownership-workflow.js +682 -41
- package/dist/ownership-workflow.js.map +1 -1
- package/dist/secret-store/file-store.js +84 -0
- package/dist/secret-store/file-store.js.map +1 -1
- package/dist/seq-tracker.d.ts +1 -0
- package/dist/seq-tracker.js +2 -2
- package/dist/seq-tracker.js.map +1 -1
- package/dist/storage/lowlevel.d.ts +6 -0
- package/dist/storage/lowlevel.js +24 -7
- package/dist/storage/lowlevel.js.map +1 -1
- package/dist/storage/read-cache.d.ts +70 -0
- package/dist/storage/read-cache.js +625 -0
- package/dist/storage/read-cache.js.map +1 -0
- package/dist/storage/vfs.d.ts +2 -0
- package/dist/storage/vfs.js +50 -15
- package/dist/storage/vfs.js.map +1 -1
- package/dist/tools/cross-sdk-agent.d.ts +86 -3
- package/dist/tools/cross-sdk-agent.js +758 -46
- package/dist/tools/cross-sdk-agent.js.map +1 -1
- package/dist/transport.d.ts +3 -0
- package/dist/transport.js +104 -46
- package/dist/transport.js.map +1 -1
- package/dist/types.d.ts +1 -0
- package/dist/v2/e2ee/encrypt-group.js +1 -0
- package/dist/v2/e2ee/encrypt-group.js.map +1 -1
- package/dist/v2/e2ee/encrypt-p2p.js +1 -0
- package/dist/v2/e2ee/encrypt-p2p.js.map +1 -1
- package/dist/v2/e2ee/types.d.ts +1 -0
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +1 -1
package/dist/org-protocol.js
CHANGED
|
@@ -32,7 +32,7 @@ const SERVICE_EVENTS = new Set([
|
|
|
32
32
|
'org.member.join_requested', 'org.member.join_approved', 'org.member.join_rejected',
|
|
33
33
|
'org.member.invite_created', 'org.member.invite_accepted', 'org.member.invite_rejected',
|
|
34
34
|
'org.member.changed', 'org.lifecycle.created', 'org.lifecycle.dissolving',
|
|
35
|
-
'org.lifecycle.dissolved', 'credential.revoked',
|
|
35
|
+
'org.lifecycle.dissolved', 'credential.revoked', 'credential.revocationList',
|
|
36
36
|
]);
|
|
37
37
|
export class ProtocolError extends Error {
|
|
38
38
|
code;
|
|
@@ -44,6 +44,783 @@ export class ProtocolError extends Error {
|
|
|
44
44
|
this.retryable = protocolErrorRetryable(code);
|
|
45
45
|
}
|
|
46
46
|
}
|
|
47
|
+
export class CredentialRenewal {
|
|
48
|
+
tail = Promise.resolve();
|
|
49
|
+
ports;
|
|
50
|
+
scanGuard;
|
|
51
|
+
constructor(ports) {
|
|
52
|
+
this.ports = {
|
|
53
|
+
...ports,
|
|
54
|
+
currentAid: () => { this.checkScan(); return ports.currentAid(); },
|
|
55
|
+
load: (aid) => this.scanOperation(() => ports.load(aid)),
|
|
56
|
+
save: (aid, records, guard) => {
|
|
57
|
+
const scan = this.scanGuard;
|
|
58
|
+
return this.scanOperation(() => ports.save(aid, records, scan ? { current: () => scan.current() && (!guard || guard.current()), signal: scan.signal } : guard));
|
|
59
|
+
},
|
|
60
|
+
signer: (aid) => this.scanOperation(() => ports.signer(aid, this.scanGuard)),
|
|
61
|
+
certificate: (aid, fingerprint, signedAt) => this.scanOperation(() => ports.certificate(aid, fingerprint, signedAt)),
|
|
62
|
+
call: (method, params, onSent) => this.scanOperation(() => onSent ? ports.call(method, params, onSent) : ports.call(method, params)),
|
|
63
|
+
send: (to, payload) => this.scanOperation(() => ports.send(to, payload)),
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
checkScan() {
|
|
67
|
+
if (this.scanGuard && !this.scanGuard.current())
|
|
68
|
+
throw invalid('credential renewal was superseded');
|
|
69
|
+
}
|
|
70
|
+
async scanOperation(operation) {
|
|
71
|
+
this.checkScan();
|
|
72
|
+
const result = await operation();
|
|
73
|
+
this.checkScan();
|
|
74
|
+
return result;
|
|
75
|
+
}
|
|
76
|
+
serial(operation) {
|
|
77
|
+
const result = this.tail.then(operation);
|
|
78
|
+
this.tail = result.catch(() => { });
|
|
79
|
+
return result;
|
|
80
|
+
}
|
|
81
|
+
remember(credential, material = {}) {
|
|
82
|
+
return this.serial(() => this.rememberNow(credential, material));
|
|
83
|
+
}
|
|
84
|
+
current(input) {
|
|
85
|
+
return this.serial(async () => {
|
|
86
|
+
const records = await this.ports.load(this.ports.currentAid());
|
|
87
|
+
let cid = typeof input === 'string' ? input : String(input.cid);
|
|
88
|
+
const seen = new Set();
|
|
89
|
+
while (records[cid]?.successor_cid && !seen.has(cid)) {
|
|
90
|
+
seen.add(cid);
|
|
91
|
+
cid = String(records[cid].successor_cid);
|
|
92
|
+
}
|
|
93
|
+
return records[cid] ? structuredClone(records[cid]) : null;
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
async rememberNow(credential, material, actor = this.ports.currentAid(), guard, storeShortTerm = false) {
|
|
97
|
+
validateCredential(credential);
|
|
98
|
+
if (!material.long_term && !guard && !storeShortTerm)
|
|
99
|
+
return;
|
|
100
|
+
if (guard && !guard.current())
|
|
101
|
+
throw invalid('membership event receiver identity changed');
|
|
102
|
+
const accounts = new Set();
|
|
103
|
+
if (credential.sub === actor)
|
|
104
|
+
accounts.add(actor);
|
|
105
|
+
if (credential.iss === actor || material.issued)
|
|
106
|
+
accounts.add(String(credential.iss));
|
|
107
|
+
for (const aid of accounts) {
|
|
108
|
+
const records = structuredClone(await this.ports.load(aid));
|
|
109
|
+
if (guard && !guard.current())
|
|
110
|
+
throw invalid('membership event receiver identity changed');
|
|
111
|
+
const previous = records[String(credential.cid)] ?? {};
|
|
112
|
+
const defined = Object.fromEntries(Object.entries(material).filter(([, value]) => value !== undefined));
|
|
113
|
+
records[String(credential.cid)] = { ...previous, ...structuredClone(defined), credential: structuredClone(credential), long_term: material.long_term === true };
|
|
114
|
+
if (guard)
|
|
115
|
+
await this.ports.save(aid, records, guard);
|
|
116
|
+
else
|
|
117
|
+
await this.ports.save(aid, records);
|
|
118
|
+
if (guard && !guard.current())
|
|
119
|
+
throw invalid('membership event receiver identity changed');
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
importMembershipPackage(payload, guard) {
|
|
123
|
+
const actor = this.ports.currentAid();
|
|
124
|
+
return this.serial(async () => {
|
|
125
|
+
const check = () => { if (!guard.current())
|
|
126
|
+
throw invalid('membership event receiver identity changed'); };
|
|
127
|
+
check();
|
|
128
|
+
const material = payload.membership_package;
|
|
129
|
+
if (!isRecord(material) || Object.keys(material).length !== 5 || !['credential', 'membership_binding', 'registrationReceipt', 'terms', 'org_signer_aid'].every((key) => key in material)
|
|
130
|
+
|| payload.member_status !== 'active' || !isInt(payload.version) || payload.version < 1)
|
|
131
|
+
throw invalid('membership package is invalid');
|
|
132
|
+
const { credential, membership_binding: binding, registrationReceipt: receipt, terms, org_signer_aid: orgSigner } = material;
|
|
133
|
+
if (!isRecord(credential) || !isRecord(binding) || !isRecord(receipt) || !isRecord(terms) || !isRecord(credential.ext)
|
|
134
|
+
|| credential.iss !== payload.org_id || credential.sub !== payload.member_aid || binding.org_id !== payload.org_id || binding.member_aid !== payload.member_aid
|
|
135
|
+
|| binding.membership_id !== payload.membership_id || credential.cid !== payload.credential_cid || digest(terms) !== binding.terms_hash
|
|
136
|
+
|| credential.ext.credential_type !== 'member' || credential.ext.bound_to_membership !== true || credential.ext.org_id !== payload.org_id
|
|
137
|
+
|| credential.ext.membership_id !== binding.membership_id || credential.ext.binding_version !== binding.binding_version || credential.ext.membership_terms_hash !== binding.terms_hash
|
|
138
|
+
|| credential.parent != null || credential.delegate !== false || !Array.isArray(credential.cap) || credential.cap.length !== 1
|
|
139
|
+
|| credential.cap[0].ns !== 'org' || credential.cap[0].action !== 'role.' + String(binding.role) || credential.cap[0].resource !== payload.org_id)
|
|
140
|
+
throw invalid('membership package does not match the event');
|
|
141
|
+
aid(orgSigner, 'membership organization signer');
|
|
142
|
+
validateCredential(credential);
|
|
143
|
+
validateMembershipBinding(binding);
|
|
144
|
+
const certificates = new Map();
|
|
145
|
+
for (const [signerAid, signed] of [[credential.iss, credential.signature], [binding.member_aid, binding.member_signature], [orgSigner, binding.org_signature], ['credential.' + String(payload.org_id).split('.').slice(1).join('.'), receipt.signature]]) {
|
|
146
|
+
if (!isRecord(signed))
|
|
147
|
+
throw invalid('membership package signature is missing');
|
|
148
|
+
const certificate = await this.ports.certificate(String(signerAid), String(signed.cert_fingerprint));
|
|
149
|
+
check();
|
|
150
|
+
certificates.set(String(signed.cert_fingerprint), certificate);
|
|
151
|
+
}
|
|
152
|
+
const resolve = (fingerprint) => certificates.get(fingerprint);
|
|
153
|
+
const now = Math.trunc(Date.now() / 1000);
|
|
154
|
+
verifyCredentialSignature(credential, resolve, now, String(payload.org_id));
|
|
155
|
+
verifyMembershipBinding(binding, resolve, now, String(orgSigner), true);
|
|
156
|
+
check();
|
|
157
|
+
const service = 'credential.' + String(payload.org_id).split('.').slice(1).join('.');
|
|
158
|
+
if (!isInt(receipt.registered_at) || !isInt(receipt.exp) || receipt.exp > receipt.registered_at + 300 || receipt.registered_at > now)
|
|
159
|
+
throw invalid('membership receipt window is invalid');
|
|
160
|
+
const late = now >= receipt.exp;
|
|
161
|
+
verifyRegistrationReceipt(receipt, credential, { request_id: binding.request_id, nonce: binding.nonce, registrar_aid: payload.org_id }, resolve, service, late ? receipt.registered_at : now);
|
|
162
|
+
check();
|
|
163
|
+
const validUntil = Math.min(Number(credential.exp), late ? await this.status(credential, actor, false, receipt.registered_at) : receipt.exp);
|
|
164
|
+
check();
|
|
165
|
+
const completedAt = Math.trunc(Date.now() / 1000);
|
|
166
|
+
verifyCredentialSignature(credential, resolve, completedAt, String(payload.org_id));
|
|
167
|
+
check();
|
|
168
|
+
if (actor !== payload.member_aid)
|
|
169
|
+
return;
|
|
170
|
+
const issuerCertificate = certificates.get(String(credential.signature.cert_fingerprint));
|
|
171
|
+
await this.rememberNow(credential, { membership_binding: binding, registrationReceipt: receipt, terms, version: payload.version,
|
|
172
|
+
service_aid: 'org.' + String(payload.org_id).split('.').slice(1).join('.'), long_term: credential.exp === Math.floor(Date.parse(new X509Certificate(issuerCertificate).validTo) / 1000) }, actor, { ...guard, current: () => guard.current() && Date.now() / 1000 < validUntil });
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
observe(method, params, result) {
|
|
176
|
+
if (method !== 'credential.register' && !method.startsWith('org.'))
|
|
177
|
+
return Promise.resolve();
|
|
178
|
+
return this.serial(async () => {
|
|
179
|
+
if (!isRecord(result))
|
|
180
|
+
return;
|
|
181
|
+
const values = [params, ...['previous_owner', 'new_owner'].map((key) => params[key]).filter(isRecord),
|
|
182
|
+
...Object.entries(params.credential_templates ?? {}).map(([aid, credential]) => ({ credential_template: credential, membership_binding: params.membership_bindings?.[aid], terms: params.terms }))];
|
|
183
|
+
for (const value of values) {
|
|
184
|
+
const credential = value.credential_template ?? value.credential;
|
|
185
|
+
if (!isRecord(credential))
|
|
186
|
+
continue;
|
|
187
|
+
const member = isRecord(credential.ext) && credential.ext.credential_type === 'member';
|
|
188
|
+
const renewal = member && method === 'org.member.renewCredential';
|
|
189
|
+
const actor = this.ports.currentAid();
|
|
190
|
+
const existing = (await this.ports.load(actor))[String(credential.cid)];
|
|
191
|
+
let longTerm = existing?.long_term === true;
|
|
192
|
+
if (!longTerm && isRecord(credential.signature)) {
|
|
193
|
+
const cert = await this.ports.certificate(String(credential.iss), String(credential.signature.cert_fingerprint));
|
|
194
|
+
longTerm = credential.exp === Math.min(Math.floor(new Date(new X509Certificate(cert).validTo).getTime() / 1000), Number(credential.proof_chain?.[0]?.exp ?? Infinity));
|
|
195
|
+
}
|
|
196
|
+
await this.rememberNow(credential, {
|
|
197
|
+
long_term: longTerm, issued: !renewal,
|
|
198
|
+
...(renewal ? { registrationReceipt: result.registrationReceipt, credential_authority: 'credential.' + String(credential.iss).split('.').slice(1).join('.') } : {}),
|
|
199
|
+
...(member ? { membership_binding: result.membership_binding ?? value.membership_binding,
|
|
200
|
+
terms: value.terms ?? (value.membership_binding?.role === 'owner' ? { org_id: credential.iss, owner_aid: credential.sub } : undefined),
|
|
201
|
+
version: result.member?.version ?? result.version ?? value.expected_version ?? (['org.member.approveJoin', 'org.member.inviteAccept'].includes(method) ? 1 : undefined),
|
|
202
|
+
service_aid: 'org.' + String(credential.iss).split('.').slice(1).join('.') } : {}),
|
|
203
|
+
}, actor, undefined, renewal);
|
|
204
|
+
}
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
resume(guard) {
|
|
208
|
+
return this.serial(async () => {
|
|
209
|
+
this.scanGuard = guard;
|
|
210
|
+
try {
|
|
211
|
+
const actor = this.ports.currentAid();
|
|
212
|
+
const records = await this.ports.load(actor);
|
|
213
|
+
for (const [cid, initial] of Object.entries(records)) {
|
|
214
|
+
if (!isRecord(initial.credential))
|
|
215
|
+
continue;
|
|
216
|
+
try {
|
|
217
|
+
if (initial.response?.registration_request && !initial.response.registrationReceipt && Number(initial.response.registration_request.request_binding?.exp) <= Math.trunc(Date.now() / 1000)) {
|
|
218
|
+
if (!initial.registration_confirmation)
|
|
219
|
+
await this.recoverRegistration(actor, cid, initial, records);
|
|
220
|
+
continue;
|
|
221
|
+
}
|
|
222
|
+
if (initial.submission && initial.received_result && !initial.successor_cid)
|
|
223
|
+
await this.receiveNow(String(initial.credential.iss), initial.received_result, undefined, true);
|
|
224
|
+
else if (initial.incoming?.payload.exp > Math.trunc(Date.now() / 1000))
|
|
225
|
+
await this.receiveNow(initial.incoming.from, initial.incoming.payload);
|
|
226
|
+
const record = (await this.ports.load(actor))[cid];
|
|
227
|
+
if (record.commit_pending)
|
|
228
|
+
await this.sendCommit(record, actor);
|
|
229
|
+
if (record.revoke_pending)
|
|
230
|
+
await this.revokePrevious(record, actor);
|
|
231
|
+
const credential = record.credential;
|
|
232
|
+
const now = Math.trunc(Date.now() / 1000);
|
|
233
|
+
if (record.successor_cid || !record.long_term || credential.sub !== actor || !record.membership_binding && !record.pending && now >= credential.exp || credential.exp - now >= (credential.exp - credential.iat) / 2)
|
|
234
|
+
continue;
|
|
235
|
+
let request = record.pending;
|
|
236
|
+
if (request?.membership_binding && request.membership_binding.exp <= now)
|
|
237
|
+
throw invalid('renewal member consent has expired');
|
|
238
|
+
if (!request || request.exp <= now) {
|
|
239
|
+
const signer = await this.ports.signer(actor);
|
|
240
|
+
if (!signer)
|
|
241
|
+
continue;
|
|
242
|
+
request = request ? { ...request, iat: now, exp: now + 300 } : { type: 'credential.renewal.request', ver: 'aun-credential-renewal/1', action: 'renew', issuer_aid: credential.iss,
|
|
243
|
+
subject_aid: actor, previous_credential_cid: cid, credential_hash: digest(credential), request_id: organizationIdentifier(), nonce: organizationNonce(), iat: now, exp: now + 300 };
|
|
244
|
+
delete request.signature;
|
|
245
|
+
if (record.membership_binding && !request.membership_binding) {
|
|
246
|
+
if (now < credential.exp) {
|
|
247
|
+
const credentials = { [record.membership_binding.role === 'admin' ? 'approval_credential' : 'member_credential']: credential, membership_binding: record.membership_binding };
|
|
248
|
+
const query = prepareOrganizationRequest('org.member.get', { org_id: credential.iss, member_aid: actor, credentials }, record.service_aid, signer);
|
|
249
|
+
const current = await this.ports.call('org.member.get', query);
|
|
250
|
+
const member = current.member ?? current;
|
|
251
|
+
if (!isInt(member.version) || member.credential_cid !== undefined && member.credential_cid !== cid)
|
|
252
|
+
throw invalid('current membership differs from managed credential');
|
|
253
|
+
record.version = member.version;
|
|
254
|
+
}
|
|
255
|
+
if (!isInt(record.version))
|
|
256
|
+
throw invalid('current membership version is unavailable');
|
|
257
|
+
const binding = { ...record.membership_binding, binding_version: Number(record.membership_binding.binding_version) + 1,
|
|
258
|
+
request_id: request.request_id, nonce: request.nonce, iat: now, exp: now + 3600 };
|
|
259
|
+
delete binding.member_signature;
|
|
260
|
+
delete binding.org_signature;
|
|
261
|
+
request.membership_binding = signMembershipBinding(binding, signer, 'member');
|
|
262
|
+
request.expected_version = record.version;
|
|
263
|
+
}
|
|
264
|
+
request.signature = signProtocolPayload(request, signer);
|
|
265
|
+
record.pending = request;
|
|
266
|
+
const latest = await this.ports.load(actor);
|
|
267
|
+
latest[cid] = record;
|
|
268
|
+
await this.ports.save(actor, latest);
|
|
269
|
+
}
|
|
270
|
+
if (this.ports.currentAid() !== actor)
|
|
271
|
+
return;
|
|
272
|
+
const localIssuer = record.membership_binding && await this.ports.signer(String(credential.iss));
|
|
273
|
+
if (localIssuer) {
|
|
274
|
+
await this.receiveNow(actor, request, String(credential.iss));
|
|
275
|
+
const issued = (await this.ports.load(String(credential.iss)))[cid]?.response;
|
|
276
|
+
if (issued)
|
|
277
|
+
await this.receiveNow(String(credential.iss), issued);
|
|
278
|
+
}
|
|
279
|
+
else
|
|
280
|
+
await this.ports.send(String(credential.iss), request);
|
|
281
|
+
}
|
|
282
|
+
catch (error) {
|
|
283
|
+
const latest = await this.ports.load(actor);
|
|
284
|
+
if (latest[cid]) {
|
|
285
|
+
latest[cid].last_error = error instanceof Error ? error.message : String(error);
|
|
286
|
+
await this.ports.save(actor, latest);
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
finally {
|
|
292
|
+
this.scanGuard = undefined;
|
|
293
|
+
}
|
|
294
|
+
});
|
|
295
|
+
}
|
|
296
|
+
receive(from, payload) {
|
|
297
|
+
return this.serial(() => this.receiveNow(from, payload));
|
|
298
|
+
}
|
|
299
|
+
async verifyEnvelope(from, payload, expected, replay = false) {
|
|
300
|
+
const now = Math.trunc(Date.now() / 1000);
|
|
301
|
+
if (from !== expected || payload.ver !== 'aun-credential-renewal/1' || !isInt(payload.iat) || !isInt(payload.exp) || payload.iat > (replay ? payload.iat : now) || payload.exp <= (replay ? payload.iat : now) || payload.exp > payload.iat + 300 || !isRecord(payload.signature))
|
|
302
|
+
throw invalid('renewal message identity or validity is invalid');
|
|
303
|
+
const cert = await this.ports.certificate(expected, String(payload.signature.cert_fingerprint));
|
|
304
|
+
const unsigned = { ...payload };
|
|
305
|
+
delete unsigned.signature;
|
|
306
|
+
verifyAgentSignature(payload.signature, unsigned, expected, () => cert, now);
|
|
307
|
+
}
|
|
308
|
+
async status(credential, actor, allowExpired = false, registrationConfirmedAt, confirmation) {
|
|
309
|
+
const target = 'credential.' + String(credential.iss).split('.').slice(1).join('.');
|
|
310
|
+
const query = { credential_cid: credential.cid, request_id: organizationIdentifier(), nonce: organizationNonce(), include_chain: true };
|
|
311
|
+
const requestedAt = Math.trunc(Date.now() / 1000);
|
|
312
|
+
query.request_binding = buildRequestBinding('credential.status', target, query, String(credential.cid), String(query.nonce), String(query.request_id), requestedAt, requestedAt + 300);
|
|
313
|
+
const proof = await this.ports.call('credential.status', query);
|
|
314
|
+
const now = Math.trunc(Date.now() / 1000);
|
|
315
|
+
if (!isRecord(proof) || proof.ver !== 'aun-credential-status/1' || proof.credentialCid !== credential.cid || proof.credential_hash !== digest(credential)
|
|
316
|
+
|| proof.issuerAid !== credential.iss || proof.subjectAid !== credential.sub || proof.expiresAt !== credential.exp || proof.responder !== target
|
|
317
|
+
|| proof.request_id !== query.request_id || proof.nonce !== query.nonce || proof.target !== actor
|
|
318
|
+
|| !isInt(proof.asOf) || !isInt(proof.proofExpiresAt) || proof.asOf > now + 300 || proof.proofExpiresAt <= proof.asOf || proof.proofExpiresAt <= now || proof.proofExpiresAt > proof.asOf + 300
|
|
319
|
+
|| !isRecord(proof.signature) || proof.signature.timestamp !== proof.asOf)
|
|
320
|
+
throw new ProtocolError('CREDENTIAL_STATUS_UNKNOWN', 'renewal status proof does not match the query');
|
|
321
|
+
const cert = await this.ports.certificate(target, String(proof.signature.cert_fingerprint));
|
|
322
|
+
const unsigned = { ...proof };
|
|
323
|
+
delete unsigned.signature;
|
|
324
|
+
verifyAgentSignature(proof.signature, unsigned, target, () => cert, now, false);
|
|
325
|
+
if (proof.status !== 'active' && !(allowExpired && proof.status === 'expired' && proof.credentialStatus === 'expired' && Array.isArray(proof.chain) && proof.chain.every((item) => item.status === 'active')))
|
|
326
|
+
throw new ProtocolError('CREDENTIAL_STATUS_UNKNOWN', 'renewal credential is not active');
|
|
327
|
+
if (registrationConfirmedAt !== undefined && (proof.asOf < requestedAt || proof.asOf > now || !isInt(proof.registrationConfirmedAt) || proof.registrationConfirmedAt < registrationConfirmedAt || proof.registrationConfirmedAt > proof.asOf || proof.credentialStatus !== 'active' || proof.proofExpiresAt <= Math.trunc(Date.now() / 1000)))
|
|
328
|
+
throw new ProtocolError('CREDENTIAL_STATUS_UNKNOWN', 'membership registration confirmation is invalid');
|
|
329
|
+
if (confirmation)
|
|
330
|
+
Object.assign(confirmation, structuredClone(proof));
|
|
331
|
+
return proof.proofExpiresAt;
|
|
332
|
+
}
|
|
333
|
+
async receiveNow(from, value, localIssuer, replay = false) {
|
|
334
|
+
if (!isRecord(value) || !['credential.renewal.request', 'credential.renewal.result'].includes(String(value.type)))
|
|
335
|
+
return false;
|
|
336
|
+
const payload = value;
|
|
337
|
+
if (!replay && isInt(payload.exp) && payload.exp <= Math.trunc(Date.now() / 1000))
|
|
338
|
+
return true;
|
|
339
|
+
const isRequest = payload.type === 'credential.renewal.request';
|
|
340
|
+
const actor = localIssuer ?? this.ports.currentAid();
|
|
341
|
+
await this.verifyEnvelope(from, payload, String(isRequest ? payload.subject_aid : payload.issuer_aid), replay);
|
|
342
|
+
if (actor !== (isRequest ? payload.issuer_aid : payload.subject_aid))
|
|
343
|
+
throw invalid('renewal message has a different recipient');
|
|
344
|
+
let records = await this.ports.load(actor);
|
|
345
|
+
const cid = String(payload.previous_credential_cid);
|
|
346
|
+
const record = records[cid];
|
|
347
|
+
if (!record || !record.long_term || payload.credential_hash !== digest(record.credential) || payload.issuer_aid !== record.credential.iss || payload.subject_aid !== record.credential.sub)
|
|
348
|
+
throw invalid('renewal does not match a known grant');
|
|
349
|
+
if (replay && (!record.submission || digest(record.received_result) !== digest(payload)))
|
|
350
|
+
throw invalid('renewal replay differs from the saved result');
|
|
351
|
+
record.incoming = { from, payload: structuredClone(payload) };
|
|
352
|
+
await this.ports.save(actor, records);
|
|
353
|
+
if (isRequest && payload.action === 'commit') {
|
|
354
|
+
if (!record.response || record.response.request_id !== payload.request_id || record.response.nonce !== payload.nonce || record.response.credential.cid !== payload.new_credential_cid)
|
|
355
|
+
throw invalid('renewal acknowledgement differs from issued result');
|
|
356
|
+
record.revoke_pending = true;
|
|
357
|
+
await this.ports.save(actor, records);
|
|
358
|
+
await this.revokePrevious(record, actor);
|
|
359
|
+
}
|
|
360
|
+
else if (isRequest && payload.action === 'renew') {
|
|
361
|
+
if (payload.exp > payload.iat + 300)
|
|
362
|
+
throw invalid('renewal request window is too long');
|
|
363
|
+
if (record.successor_cid && record.response?.request_id !== payload.request_id)
|
|
364
|
+
throw invalid('grant was already replaced');
|
|
365
|
+
if (!record.response || record.response.request_id !== payload.request_id) {
|
|
366
|
+
const checkedAt = Math.trunc(Date.now() / 1000);
|
|
367
|
+
if (record.credential.exp - checkedAt >= (record.credential.exp - record.credential.iat) / 2)
|
|
368
|
+
throw invalid('credential renewal threshold is not reached');
|
|
369
|
+
await this.status(record.credential, this.ports.currentAid(), !!record.membership_binding);
|
|
370
|
+
const signer = await this.ports.signer(actor);
|
|
371
|
+
if (!signer)
|
|
372
|
+
throw invalid('issuer signing identity is unavailable');
|
|
373
|
+
const now = Math.trunc(Date.now() / 1000);
|
|
374
|
+
const response = { type: 'credential.renewal.result', ver: payload.ver, action: 'issued', issuer_aid: actor, subject_aid: from,
|
|
375
|
+
previous_credential_cid: cid, credential_hash: payload.credential_hash, request_id: payload.request_id, nonce: payload.nonce, iat: now };
|
|
376
|
+
if (record.membership_binding) {
|
|
377
|
+
const binding = payload.membership_binding;
|
|
378
|
+
const previous = record.membership_binding;
|
|
379
|
+
if (!isRecord(binding) || binding.binding_version !== Number(previous.binding_version) + 1 || binding.request_id !== payload.request_id || binding.nonce !== payload.nonce
|
|
380
|
+
|| ['org_id', 'member_aid', 'membership_id', 'member_type', 'category', 'role', 'ownership', 'visibility', 'terms_hash'].some((key) => binding[key] !== previous[key]))
|
|
381
|
+
throw invalid('renewal changes membership terms');
|
|
382
|
+
const memberSignature = binding.member_signature;
|
|
383
|
+
const memberCert = await this.ports.certificate(from, String(memberSignature?.cert_fingerprint));
|
|
384
|
+
const unsigned = { ...binding };
|
|
385
|
+
delete unsigned.member_signature;
|
|
386
|
+
delete unsigned.org_signature;
|
|
387
|
+
verifyAgentSignature(memberSignature, unsigned, from, () => memberCert, now);
|
|
388
|
+
const credentialOptions = Object.fromEntries(['security_level', 'revocation_endpoint', 'credential_service'].filter((key) => record.credential[key] !== undefined).map((key) => [key, record.credential[key]]));
|
|
389
|
+
const prepared = prepareOrganizationRequest('org.member.renewCredential', { org_id: actor, member_aid: from, previous_credential_cid: cid,
|
|
390
|
+
expected_version: payload.expected_version, terms: record.terms, membership_binding: binding }, record.service_aid, signer, { orgSigner: signer, memberSigner: signer, credentialOptions });
|
|
391
|
+
delete prepared.request_binding;
|
|
392
|
+
response.renewal_params = prepared;
|
|
393
|
+
response.credential = prepared.credential_template;
|
|
394
|
+
response.exp = Math.min(Number(binding.exp), Number(response.credential.exp));
|
|
395
|
+
}
|
|
396
|
+
else {
|
|
397
|
+
if (actor !== this.ports.currentAid())
|
|
398
|
+
throw invalid('generic renewal requires the issuer session');
|
|
399
|
+
let options = { ...record.credential };
|
|
400
|
+
for (const key of ['signature', 'cid', 'iat', 'nbf', 'exp'])
|
|
401
|
+
delete options[key];
|
|
402
|
+
if (options.parent) {
|
|
403
|
+
let parent = records[String(options.parent)];
|
|
404
|
+
const visited = new Set();
|
|
405
|
+
while (parent?.successor_cid && !visited.has(String(parent.credential.cid))) {
|
|
406
|
+
visited.add(String(parent.credential.cid));
|
|
407
|
+
parent = records[String(parent.successor_cid)];
|
|
408
|
+
}
|
|
409
|
+
const successor = parent?.credential;
|
|
410
|
+
if (successor) {
|
|
411
|
+
options.parent = successor.cid;
|
|
412
|
+
options.proof_chain = [successor, ...(successor.proof_chain ?? [])];
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
options.key_version = signer.certFingerprint;
|
|
416
|
+
options.iat = now;
|
|
417
|
+
options.nbf = Math.max(Number(record.credential.nbf), now, Number(options.proof_chain?.[0]?.nbf ?? 0));
|
|
418
|
+
response.credential = createCredential(options, signer);
|
|
419
|
+
response.exp = Math.min(now + 300, response.credential.exp);
|
|
420
|
+
const registration = { request_id: payload.request_id, nonce: payload.nonce, registrar_aid: actor, credential: response.credential };
|
|
421
|
+
registration.request_binding = buildRequestBinding('credential.register', 'credential.' + actor.split('.').slice(1).join('.'), registration, String(response.credential.cid), payload.nonce, payload.request_id, now, now + 300);
|
|
422
|
+
response.registration_request = registration;
|
|
423
|
+
record.response = response;
|
|
424
|
+
await this.ports.save(actor, records);
|
|
425
|
+
}
|
|
426
|
+
record.response = response;
|
|
427
|
+
await this.ports.save(actor, records);
|
|
428
|
+
}
|
|
429
|
+
const response = record.response;
|
|
430
|
+
if (response.registration_request && !response.registrationReceipt) {
|
|
431
|
+
if (Number(response.registration_request.request_binding?.exp) <= Math.trunc(Date.now() / 1000)) {
|
|
432
|
+
if (!record.registration_confirmation)
|
|
433
|
+
await this.recoverRegistration(actor, cid, record, records);
|
|
434
|
+
return true;
|
|
435
|
+
}
|
|
436
|
+
const registered = await this.ports.call('credential.register', response.registration_request, async () => {
|
|
437
|
+
const sentAt = Math.trunc(Date.now() / 1000);
|
|
438
|
+
if (this.ports.currentAid() !== actor || sentAt >= Number(response.registration_request.request_binding.exp))
|
|
439
|
+
throw invalid('registration expired before it was sent');
|
|
440
|
+
if (this.ports.atomicCheckpointSave?.() !== true)
|
|
441
|
+
return;
|
|
442
|
+
const deadline = Number(response.registration_request.request_binding.exp), hash = digest(response.registration_request);
|
|
443
|
+
record.registration_submitted_at ??= sentAt;
|
|
444
|
+
record.registration_submission_hash = hash;
|
|
445
|
+
await this.ports.save(actor, records, { current: () => this.ports.currentAid() === actor && Date.now() / 1000 < deadline && digest(response.registration_request) === hash });
|
|
446
|
+
});
|
|
447
|
+
await this.receipt(response.credential, registered.registrationReceipt, response.registration_request);
|
|
448
|
+
await this.status(response.credential, actor);
|
|
449
|
+
response.registrationReceipt = registered.registrationReceipt;
|
|
450
|
+
}
|
|
451
|
+
const responseNow = Math.trunc(Date.now() / 1000);
|
|
452
|
+
const responseLimit = Math.min(responseNow + 300, Number(response.credential.exp), ...['membership_binding', 'credential_template', 'registration_authorization'].map((key) => Number(response.renewal_params?.[key]?.exp ?? Infinity)));
|
|
453
|
+
if (responseLimit <= responseNow)
|
|
454
|
+
throw invalid('renewal result authorization has expired');
|
|
455
|
+
if (!response.signature || response.exp <= responseNow || response.exp > responseLimit) {
|
|
456
|
+
response.iat = responseNow;
|
|
457
|
+
response.exp = responseLimit;
|
|
458
|
+
delete response.signature;
|
|
459
|
+
response.signature = signProtocolPayload(response, (await this.ports.signer(actor)));
|
|
460
|
+
}
|
|
461
|
+
records[String(response.credential.cid)] = { ...records[String(response.credential.cid)], credential: structuredClone(response.credential), long_term: true,
|
|
462
|
+
...(record.membership_binding ? { membership_binding: structuredClone(response.renewal_params.membership_binding), terms: record.terms, version: Number(payload.expected_version) + 1, service_aid: record.service_aid } : {}) };
|
|
463
|
+
await this.ports.save(actor, records);
|
|
464
|
+
if (!localIssuer)
|
|
465
|
+
await this.ports.send(from, response);
|
|
466
|
+
}
|
|
467
|
+
else if (!isRequest && payload.action === 'issued') {
|
|
468
|
+
if (!record.pending || record.pending.request_id !== payload.request_id || record.pending.nonce !== payload.nonce || record.successor_cid && record.successor_cid !== payload.credential?.cid)
|
|
469
|
+
throw invalid('renewal result does not match pending request');
|
|
470
|
+
if (record.successor_cid) {
|
|
471
|
+
if (record.commit_pending)
|
|
472
|
+
await this.sendCommit(record, actor);
|
|
473
|
+
records = await this.ports.load(actor);
|
|
474
|
+
delete records[cid].incoming;
|
|
475
|
+
await this.ports.save(actor, records);
|
|
476
|
+
return true;
|
|
477
|
+
}
|
|
478
|
+
const credential = payload.credential;
|
|
479
|
+
if (!isRecord(credential) || credential.cid === cid || Number(credential.nbf) < record.credential.nbf || ['iss', 'sub', 'aud', 'cap', 'delegate', 'delegation_depth', 'security_level', 'revocation_endpoint', 'credential_service'].some((key) => canonicalText(credential[key] ?? null) !== canonicalText(record.credential[key] ?? null)))
|
|
480
|
+
throw invalid('renewal result changes the grant');
|
|
481
|
+
const expectedExt = record.membership_binding ? { ...record.credential.ext, binding_version: Number(record.membership_binding.binding_version) + 1 } : record.credential.ext;
|
|
482
|
+
if (canonicalText(credential.ext ?? null) !== canonicalText(expectedExt ?? null))
|
|
483
|
+
throw invalid('renewal result changes grant extensions');
|
|
484
|
+
const credentialSignature = credential.signature;
|
|
485
|
+
const certificate = await this.ports.certificate(from, String(credentialSignature?.cert_fingerprint));
|
|
486
|
+
verifyCredentialSignature(credential, () => certificate, Math.trunc(Date.now() / 1000));
|
|
487
|
+
const previousChain = credentialChainDocuments(record.credential);
|
|
488
|
+
const renewedChain = credentialChainDocuments(credential);
|
|
489
|
+
if (previousChain.length !== renewedChain.length)
|
|
490
|
+
throw invalid('renewal changes the authorization chain');
|
|
491
|
+
for (let index = 1; index < renewedChain.length; index++) {
|
|
492
|
+
const parent = renewedChain[index], previous = previousChain[index];
|
|
493
|
+
if (Number(parent.nbf) < Number(previous.nbf) || ['iss', 'sub', 'aud', 'cap', 'delegate', 'delegation_depth', 'ext', 'security_level', 'revocation_endpoint', 'credential_service'].some((key) => canonicalText(parent[key] ?? null) !== canonicalText(previous[key] ?? null)))
|
|
494
|
+
throw invalid('renewal changes parent authorization');
|
|
495
|
+
const cert = await this.ports.certificate(String(parent.iss), String(parent.signature.cert_fingerprint));
|
|
496
|
+
verifyCredentialSignature(parent, () => cert, Math.trunc(Date.now() / 1000), undefined, renewedChain.slice(index + 1));
|
|
497
|
+
}
|
|
498
|
+
let material = {};
|
|
499
|
+
let recoveryValidUntil = Infinity;
|
|
500
|
+
if (record.membership_binding) {
|
|
501
|
+
const params = payload.renewal_params;
|
|
502
|
+
if (!isRecord(params) || digest(params.credential_template) !== digest(credential) || params.previous_credential_cid !== cid
|
|
503
|
+
|| !isRecord(params.membership_binding) || digest(params.membership_binding.member_signature) !== digest(record.pending.membership_binding.member_signature)
|
|
504
|
+
|| digest(params.terms) !== digest(record.terms))
|
|
505
|
+
throw invalid('renewal membership result is invalid');
|
|
506
|
+
const agreedBinding = { ...params.membership_binding };
|
|
507
|
+
delete agreedBinding.org_signature;
|
|
508
|
+
if (digest(agreedBinding) !== digest(record.pending.membership_binding) || params.org_id !== record.credential.iss || params.member_aid !== actor || params.expected_version !== record.pending.expected_version)
|
|
509
|
+
throw invalid('renewal differs from the signed membership request');
|
|
510
|
+
const signer = await this.ports.signer(actor);
|
|
511
|
+
if (!signer)
|
|
512
|
+
throw invalid('member signing identity is unavailable');
|
|
513
|
+
const consentEnd = Math.min(Number(payload.exp), Number(params.membership_binding.exp), Number(params.registration_authorization?.exp), Number(credential.exp));
|
|
514
|
+
if (replay && consentEnd <= Math.trunc(Date.now() / 1000)) {
|
|
515
|
+
if (this.ports.atomicCheckpointSave?.() !== true)
|
|
516
|
+
throw invalid('atomic checkpoint persistence is required for renewal recovery');
|
|
517
|
+
if (!isInt(record.submitted_at) || record.submitted_at < payload.iat || record.submitted_at >= consentEnd || record.submission_hash !== digest(record.submission))
|
|
518
|
+
throw invalid('renewal operation was not submitted within its authorization window');
|
|
519
|
+
const original = { ...record.submission };
|
|
520
|
+
delete original.request_binding;
|
|
521
|
+
delete original.client_signature;
|
|
522
|
+
const expected = { ...params };
|
|
523
|
+
delete expected.request_binding;
|
|
524
|
+
delete expected.client_signature;
|
|
525
|
+
if (digest(original) !== digest(expected))
|
|
526
|
+
throw invalid('renewal submission differs from the original result');
|
|
527
|
+
const credentialProof = {};
|
|
528
|
+
const proofExpiresAt = await this.status(credential, actor, false, record.submitted_at, credentialProof);
|
|
529
|
+
const query = prepareOrganizationRequest('org.member.get', { org_id: credential.iss, member_aid: actor,
|
|
530
|
+
credentials: { [params.membership_binding.role === 'admin' ? 'approval_credential' : 'member_credential']: credential, membership_binding: params.membership_binding } }, record.service_aid, signer);
|
|
531
|
+
const current = await this.ports.call('org.member.get', query);
|
|
532
|
+
const proof = current.membership_status;
|
|
533
|
+
const now = Math.trunc(Date.now() / 1000);
|
|
534
|
+
if (!isRecord(proof) || proof.ver !== 'aun-membership-status/1' || proof.org_id !== credential.iss || proof.member_aid !== actor || proof.credential_cid !== credential.cid
|
|
535
|
+
|| proof.membership_id !== params.membership_binding.membership_id || proof.role !== params.membership_binding.role || proof.version !== Number(params.expected_version) + 1
|
|
536
|
+
|| proof.status !== 'active' || proof.credential_status !== 'active' || proof.responder !== record.service_aid || proof.target !== actor || proof.request_id !== query.request_id || proof.nonce !== query.nonce
|
|
537
|
+
|| !isInt(proof.as_of) || proof.as_of < Number(query.request_binding.iat) || proof.as_of > now || !isInt(proof.expires_at) || proof.expires_at <= now || proof.expires_at > proof.as_of + 300
|
|
538
|
+
|| !isRecord(proof.signature) || proof.signature.timestamp !== proof.as_of || proofExpiresAt <= now)
|
|
539
|
+
throw invalid('renewal membership confirmation is invalid');
|
|
540
|
+
const cert = await this.ports.certificate(record.service_aid, String(proof.signature.cert_fingerprint));
|
|
541
|
+
const unsigned = { ...proof };
|
|
542
|
+
delete unsigned.signature;
|
|
543
|
+
verifyAgentSignature(proof.signature, unsigned, record.service_aid, () => cert, now, false);
|
|
544
|
+
recoveryValidUntil = Math.min(proofExpiresAt, proof.expires_at, Number(credential.exp));
|
|
545
|
+
material = { membership_binding: params.membership_binding, terms: record.terms, version: proof.version, service_aid: record.service_aid,
|
|
546
|
+
recovery_confirmation: { membership_status: proof, credential_status: credentialProof } };
|
|
547
|
+
}
|
|
548
|
+
else {
|
|
549
|
+
const prepared = record.submission ? structuredClone(record.submission) : structuredClone(params);
|
|
550
|
+
delete prepared.request_binding;
|
|
551
|
+
delete prepared.client_signature;
|
|
552
|
+
const submittedAt = Math.trunc(Date.now() / 1000);
|
|
553
|
+
prepared.request_binding = buildRequestBinding('org.member.renewCredential', record.service_aid, prepared, String(credential.cid), String(prepared.nonce), String(prepared.request_id), submittedAt, Math.min(submittedAt + 300, Number(credential.exp)));
|
|
554
|
+
record.submission = structuredClone(prepared);
|
|
555
|
+
record.received_result = structuredClone(payload);
|
|
556
|
+
records[cid] = record;
|
|
557
|
+
await this.ports.save(actor, records);
|
|
558
|
+
const result = await this.ports.call('org.member.renewCredential', prepared, async () => {
|
|
559
|
+
const sentAt = Math.trunc(Date.now() / 1000);
|
|
560
|
+
if (this.ports.currentAid() !== actor || sentAt >= Math.min(consentEnd, Number(prepared.request_binding.exp)))
|
|
561
|
+
throw invalid('renewal authorization expired before it was sent');
|
|
562
|
+
if (this.ports.atomicCheckpointSave?.() !== true)
|
|
563
|
+
return;
|
|
564
|
+
const deadline = Math.min(consentEnd, Number(prepared.request_binding.exp)), hash = digest(record.submission);
|
|
565
|
+
record.submitted_at ??= sentAt;
|
|
566
|
+
record.submission_hash = hash;
|
|
567
|
+
await this.ports.save(actor, records, { current: () => this.ports.currentAid() === actor && Date.now() / 1000 < deadline && digest(record.submission) === hash });
|
|
568
|
+
});
|
|
569
|
+
await this.receipt(credential, result.registrationReceipt, prepared);
|
|
570
|
+
await this.status(credential, actor);
|
|
571
|
+
material = { membership_binding: result.membership_binding ?? params.membership_binding, terms: record.terms, version: result.version, service_aid: record.service_aid };
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
else {
|
|
575
|
+
await this.receipt(credential, payload.registrationReceipt, payload.registration_request);
|
|
576
|
+
await this.status(credential, actor);
|
|
577
|
+
}
|
|
578
|
+
records = await this.ports.load(actor);
|
|
579
|
+
records[String(credential.cid)] = { credential: structuredClone(credential), long_term: true, ...material };
|
|
580
|
+
records[cid].successor_cid = credential.cid;
|
|
581
|
+
records[cid].response = structuredClone(payload);
|
|
582
|
+
records[cid].commit_pending = true;
|
|
583
|
+
const recoveryGuard = { current: () => Date.now() / 1000 >= Number(credential.nbf) && Date.now() / 1000 < Math.min(recoveryValidUntil, Number(credential.exp)) };
|
|
584
|
+
if (!recoveryGuard.current())
|
|
585
|
+
throw invalid('renewal recovery confirmation has expired');
|
|
586
|
+
await this.ports.save(actor, records, recoveryGuard);
|
|
587
|
+
await this.sendCommit(records[cid], actor);
|
|
588
|
+
}
|
|
589
|
+
else
|
|
590
|
+
throw invalid('renewal message action is invalid');
|
|
591
|
+
records = await this.ports.load(actor);
|
|
592
|
+
delete records[cid].incoming;
|
|
593
|
+
await this.ports.save(actor, records);
|
|
594
|
+
return true;
|
|
595
|
+
}
|
|
596
|
+
async recoverRegistration(actor, cid, record, records) {
|
|
597
|
+
if (this.ports.atomicCheckpointSave?.() !== true)
|
|
598
|
+
throw invalid('atomic checkpoint persistence is required for registration recovery');
|
|
599
|
+
const response = record.response, request = response.registration_request, binding = request.request_binding;
|
|
600
|
+
if (!isInt(record.registration_submitted_at) || record.registration_submitted_at < binding.iat || record.registration_submitted_at >= binding.exp
|
|
601
|
+
|| record.registration_submission_hash !== digest(request) || digest(request.credential) !== digest(response.credential))
|
|
602
|
+
throw invalid('registration was not sent within its authorization window');
|
|
603
|
+
const credential = response.credential;
|
|
604
|
+
const certificate = await this.ports.certificate(String(credential.iss), String(credential.signature?.cert_fingerprint));
|
|
605
|
+
verifyCredentialSignature(credential, () => certificate, Math.trunc(Date.now() / 1000));
|
|
606
|
+
const confirmation = {};
|
|
607
|
+
const validUntil = await this.status(credential, actor, false, record.registration_submitted_at, confirmation);
|
|
608
|
+
records[String(credential.cid)] = { credential: structuredClone(credential), long_term: true, issued: true };
|
|
609
|
+
records[cid].registration_confirmation = confirmation;
|
|
610
|
+
const guard = { current: () => Date.now() / 1000 >= Number(credential.nbf) && Date.now() / 1000 < Math.min(validUntil, Number(credential.exp)) };
|
|
611
|
+
if (!guard.current())
|
|
612
|
+
throw invalid('registration recovery confirmation has expired');
|
|
613
|
+
await this.ports.save(actor, records, guard);
|
|
614
|
+
}
|
|
615
|
+
async receipt(credential, receipt, request) {
|
|
616
|
+
const service = 'credential.' + String(credential.iss).split('.').slice(1).join('.');
|
|
617
|
+
if (!isRecord(receipt) || !isRecord(receipt.signature))
|
|
618
|
+
throw invalid('renewal registration receipt is missing');
|
|
619
|
+
const cert = await this.ports.certificate(service, String(receipt.signature.cert_fingerprint));
|
|
620
|
+
verifyRegistrationReceipt(receipt, credential, request, () => cert, service, Math.trunc(Date.now() / 1000));
|
|
621
|
+
}
|
|
622
|
+
async sendCommit(record, actor) {
|
|
623
|
+
const signer = await this.ports.signer(actor);
|
|
624
|
+
if (!signer)
|
|
625
|
+
throw invalid('renewal holder signing identity is unavailable');
|
|
626
|
+
const now = Math.trunc(Date.now() / 1000);
|
|
627
|
+
const payload = { ...record.pending, action: 'commit', new_credential_cid: record.successor_cid, iat: now, exp: now + 300 };
|
|
628
|
+
delete payload.signature;
|
|
629
|
+
payload.signature = signProtocolPayload(payload, signer);
|
|
630
|
+
if (record.membership_binding && await this.ports.signer(String(record.credential.iss)))
|
|
631
|
+
await this.receiveNow(actor, payload, String(record.credential.iss));
|
|
632
|
+
else
|
|
633
|
+
await this.ports.send(String(record.credential.iss), payload);
|
|
634
|
+
const records = await this.ports.load(actor);
|
|
635
|
+
delete records[String(record.credential.cid)].commit_pending;
|
|
636
|
+
await this.ports.save(actor, records);
|
|
637
|
+
}
|
|
638
|
+
async revokePrevious(record, actor) {
|
|
639
|
+
await this.status(record.response.credential, this.ports.currentAid());
|
|
640
|
+
if (!record.membership_binding && !record.credential.delegate) {
|
|
641
|
+
const now = Math.trunc(Date.now() / 1000);
|
|
642
|
+
const request = { credential_cid: record.credential.cid, registrar_aid: actor,
|
|
643
|
+
request_id: record.response.request_id + '-revoke', nonce: record.response.nonce + '-revoke', reason_code: 'credential_renewed', cascade: false };
|
|
644
|
+
request.request_binding = buildRequestBinding('credential.revoke', 'credential.' + actor.split('.').slice(1).join('.'), request, String(record.credential.cid), String(request.nonce), String(request.request_id), now, now + 300);
|
|
645
|
+
await this.ports.call('credential.revoke', request);
|
|
646
|
+
}
|
|
647
|
+
const records = await this.ports.load(actor);
|
|
648
|
+
records[String(record.credential.cid)].successor_cid = record.response.credential.cid;
|
|
649
|
+
delete records[String(record.credential.cid)].revoke_pending;
|
|
650
|
+
await this.ports.save(actor, records);
|
|
651
|
+
}
|
|
652
|
+
revocationAuthority(document, record) {
|
|
653
|
+
const derived = 'credential.' + String(document.iss).split('.').slice(1).join('.');
|
|
654
|
+
const authority = record?.credential_authority ?? derived;
|
|
655
|
+
if (typeof authority !== 'string' || !/^credential\.[^\s/]+$/.test(authority) || !AID_RE.test(String(document.iss)))
|
|
656
|
+
throw new ProtocolError('CREDENTIAL_STATUS_UNKNOWN', 'credential authority is unavailable');
|
|
657
|
+
return authority;
|
|
658
|
+
}
|
|
659
|
+
revocationState(value) {
|
|
660
|
+
if (value == null || isRecord(value) && Object.keys(value).length === 0)
|
|
661
|
+
return { version: 1, authorities: {} };
|
|
662
|
+
if (!isRecord(value) || value.version !== 1 || !isRecord(value.authorities))
|
|
663
|
+
throw new ProtocolError('CREDENTIAL_STATUS_UNKNOWN', 'credential revocation storage is damaged');
|
|
664
|
+
for (const [authority, partition] of Object.entries(value.authorities)) {
|
|
665
|
+
if (!/^credential\.[^\s/]+$/.test(authority) || !isRecord(partition) || !isRecord(partition.revoked) || !isInt(partition.since_event_seq) || partition.since_event_seq < 0
|
|
666
|
+
|| typeof partition.cursor !== 'string' || partition.cursor !== '' && (!/^(0|[1-9][0-9]*)$/.test(partition.cursor) || !isInt(Number(partition.cursor)) || Number(partition.cursor) !== partition.since_event_seq)
|
|
667
|
+
|| !isInt(partition.as_of) || !isInt(partition.expires_at) || partition.expires_at <= partition.as_of
|
|
668
|
+
|| Object.values(partition.revoked).some((sequence) => !isInt(sequence) || sequence <= 0))
|
|
669
|
+
throw new ProtocolError('CREDENTIAL_STATUS_UNKNOWN', 'credential revocation storage is damaged');
|
|
670
|
+
}
|
|
671
|
+
return value;
|
|
672
|
+
}
|
|
673
|
+
_assertNotRevoked(credential, proofChain) {
|
|
674
|
+
if (!this.ports.loadRevocations)
|
|
675
|
+
return;
|
|
676
|
+
const actor = this.ports.currentAid();
|
|
677
|
+
const state = this.revocationState(this.ports.loadRevocations(actor));
|
|
678
|
+
const records = this.ports.load(actor);
|
|
679
|
+
if (!isRecord(records))
|
|
680
|
+
throw new ProtocolError('CREDENTIAL_STATUS_UNKNOWN', 'managed credential storage is unavailable');
|
|
681
|
+
const documents = [credential, ...(proofChain ?? [])];
|
|
682
|
+
const seen = new Set();
|
|
683
|
+
for (let index = 0; index < documents.length; index++) {
|
|
684
|
+
const document = documents[index];
|
|
685
|
+
if (!isRecord(document) || seen.has(document))
|
|
686
|
+
continue;
|
|
687
|
+
seen.add(document);
|
|
688
|
+
if (seen.size > 128)
|
|
689
|
+
throw new ProtocolError('CREDENTIAL_CHAIN_INVALID', 'credential chain is too deep');
|
|
690
|
+
const authority = this.revocationAuthority(document, records[String(document.cid)]);
|
|
691
|
+
const revoked = state.authorities[authority]?.revoked;
|
|
692
|
+
if (revoked && Object.hasOwn(revoked, String(document.cid)))
|
|
693
|
+
throw new ProtocolError('CREDENTIAL_REVOKED', 'credential was revoked');
|
|
694
|
+
if (Array.isArray(document.proof_chain))
|
|
695
|
+
documents.push(...document.proof_chain);
|
|
696
|
+
}
|
|
697
|
+
if (this.ports.currentAid() !== actor)
|
|
698
|
+
throw new ProtocolError('CREDENTIAL_STATUS_UNKNOWN', 'credential receiver identity changed');
|
|
699
|
+
}
|
|
700
|
+
_receiveRevocations(payload, guard) {
|
|
701
|
+
if (!this.ports.loadRevocations || !this.ports.saveRevocations)
|
|
702
|
+
return Promise.resolve();
|
|
703
|
+
return this.serial(async () => {
|
|
704
|
+
const actor = this.ports.currentAid();
|
|
705
|
+
const current = () => this.ports.currentAid() === actor && guard.current() && Date.now() / 1000 < Number(payload.expires_at);
|
|
706
|
+
if (!current())
|
|
707
|
+
throw new ProtocolError('CREDENTIAL_STATUS_UNKNOWN', 'credential revocation event was superseded');
|
|
708
|
+
const list = payload.type === 'credential.revocationList';
|
|
709
|
+
const since = list ? payload.since_event_seq : null;
|
|
710
|
+
const entries = list ? payload.events : [{ cid: payload.credential_cid, event_seq: payload.event_seq, reason_code: payload.reason_code, revoked_at: payload.revoked_at }];
|
|
711
|
+
if (payload.target !== actor || typeof payload.responder !== 'string' || !AID_RE.test(payload.responder) || !payload.responder.startsWith('credential.')
|
|
712
|
+
|| !Array.isArray(entries) || entries.length > 1000 || list && (!isInt(since) || since < 0 || payload.issuer_aid !== '') || !list && payload.status !== 'revoked')
|
|
713
|
+
throw invalid('credential revocation event is invalid');
|
|
714
|
+
let last = list ? Number(since) : 0;
|
|
715
|
+
for (const entry of entries) {
|
|
716
|
+
if (!isRecord(entry) || typeof entry.cid !== 'string' || !entry.cid || !isInt(entry.event_seq) || entry.event_seq <= last
|
|
717
|
+
|| typeof entry.reason_code !== 'string' || !entry.reason_code || !isInt(entry.revoked_at) || entry.revoked_at < 0 || entry.revoked_at > Number(payload.as_of)
|
|
718
|
+
|| list && (typeof entry.revoked_by_aid !== 'string' || !AID_RE.test(entry.revoked_by_aid)))
|
|
719
|
+
throw invalid('credential revocation event is invalid');
|
|
720
|
+
last = entry.event_seq;
|
|
721
|
+
}
|
|
722
|
+
const cursor = list ? payload.next_cursor : '';
|
|
723
|
+
if (typeof cursor !== 'string' || cursor !== '' && (!/^[1-9][0-9]*$/.test(cursor) || !isInt(Number(cursor)) || Number(cursor) <= Number(since) || Number(cursor) < last))
|
|
724
|
+
throw invalid('credential revocation cursor is invalid');
|
|
725
|
+
const state = structuredClone(this.revocationState(await this.ports.loadRevocations(actor)));
|
|
726
|
+
const previous = state.authorities[payload.responder] ?? { revoked: {}, since_event_seq: 0, cursor: '', as_of: 0, expires_at: 0 };
|
|
727
|
+
const revoked = { ...previous.revoked };
|
|
728
|
+
for (const entry of entries)
|
|
729
|
+
Object.defineProperty(revoked, String(entry.cid), { value: Math.max(Object.hasOwn(revoked, String(entry.cid)) ? Number(revoked[String(entry.cid)]) : 0, Number(entry.event_seq)), enumerable: true, configurable: true, writable: true });
|
|
730
|
+
const advance = list && Number(since) <= previous.since_event_seq && Math.max(last, Number(cursor)) >= previous.since_event_seq;
|
|
731
|
+
state.authorities[payload.responder] = { ...previous, revoked,
|
|
732
|
+
...(advance ? { since_event_seq: Math.max(last, Number(cursor)), cursor } : {}),
|
|
733
|
+
...(Number(payload.as_of) >= previous.as_of ? { as_of: payload.as_of, expires_at: payload.expires_at } : {}) };
|
|
734
|
+
if (!current())
|
|
735
|
+
throw new ProtocolError('CREDENTIAL_STATUS_UNKNOWN', 'credential revocation event was superseded');
|
|
736
|
+
await this.ports.saveRevocations(actor, state, { ...guard, current });
|
|
737
|
+
if (!current())
|
|
738
|
+
throw new ProtocolError('CREDENTIAL_STATUS_UNKNOWN', 'credential revocation event was superseded');
|
|
739
|
+
});
|
|
740
|
+
}
|
|
741
|
+
_syncRevocations(guard) {
|
|
742
|
+
if (!this.ports.loadRevocations || !this.ports.saveRevocations)
|
|
743
|
+
return Promise.resolve();
|
|
744
|
+
return this.serial(async () => {
|
|
745
|
+
const actor = this.ports.currentAid();
|
|
746
|
+
const check = () => { if (this.ports.currentAid() !== actor || guard && !guard.current())
|
|
747
|
+
throw new ProtocolError('CREDENTIAL_STATUS_UNKNOWN', 'credential revocation synchronization was superseded'); };
|
|
748
|
+
check();
|
|
749
|
+
let state = structuredClone(this.revocationState(await this.ports.loadRevocations(actor)));
|
|
750
|
+
check();
|
|
751
|
+
const records = await this.ports.load(actor);
|
|
752
|
+
check();
|
|
753
|
+
const authorities = new Set();
|
|
754
|
+
if (AID_RE.test(actor))
|
|
755
|
+
authorities.add('credential.' + actor.split('.').slice(1).join('.'));
|
|
756
|
+
for (const record of Object.values(records)) {
|
|
757
|
+
if (!isRecord(record) || !isRecord(record.credential))
|
|
758
|
+
continue;
|
|
759
|
+
validateCredential(record.credential);
|
|
760
|
+
const documents = [record.credential];
|
|
761
|
+
const seen = new Set();
|
|
762
|
+
for (let index = 0; index < documents.length; index++) {
|
|
763
|
+
const document = documents[index];
|
|
764
|
+
if (!isRecord(document) || seen.has(document))
|
|
765
|
+
continue;
|
|
766
|
+
seen.add(document);
|
|
767
|
+
if (seen.size > 128)
|
|
768
|
+
throw new ProtocolError('CREDENTIAL_CHAIN_INVALID', 'credential chain is too deep');
|
|
769
|
+
authorities.add(this.revocationAuthority(document, document === record.credential ? record : undefined));
|
|
770
|
+
if (Array.isArray(document.proof_chain))
|
|
771
|
+
documents.push(...document.proof_chain);
|
|
772
|
+
}
|
|
773
|
+
}
|
|
774
|
+
for (const authority of authorities) {
|
|
775
|
+
for (let page = 0; page < 32; page++) {
|
|
776
|
+
check();
|
|
777
|
+
const previous = state.authorities[authority] ?? { revoked: {}, since_event_seq: 0, cursor: '' };
|
|
778
|
+
const since = previous.since_event_seq;
|
|
779
|
+
const query = { cursor: previous.cursor || String(since), limit: 1000, request_id: organizationIdentifier(), nonce: organizationNonce() };
|
|
780
|
+
const requestedAt = Math.trunc(Date.now() / 1000);
|
|
781
|
+
query.request_binding = buildRequestBinding('credential.revocationList', authority, query, '', String(query.nonce), String(query.request_id), requestedAt, requestedAt + 300);
|
|
782
|
+
const proof = await this.ports.call('credential.revocationList', query);
|
|
783
|
+
check();
|
|
784
|
+
const now = Math.trunc(Date.now() / 1000);
|
|
785
|
+
if (!isRecord(proof) || proof.responder !== authority || proof.target !== actor || proof.request_id !== query.request_id || proof.nonce !== query.nonce || proof.issuer_aid !== ''
|
|
786
|
+
|| !Array.isArray(proof.events) || proof.events.length > 1000 || typeof proof.next_cursor !== 'string'
|
|
787
|
+
|| !isInt(proof.as_of) || proof.as_of <= 0 || proof.as_of > now + 300 || !isInt(proof.expires_at) || proof.expires_at <= now || proof.expires_at <= proof.as_of || proof.expires_at > proof.as_of + 300
|
|
788
|
+
|| !isRecord(proof.signature) || proof.signature.timestamp !== proof.as_of)
|
|
789
|
+
throw new ProtocolError('CREDENTIAL_STATUS_UNKNOWN', 'revocation list does not match the query');
|
|
790
|
+
let last = since;
|
|
791
|
+
for (const event of proof.events) {
|
|
792
|
+
if (!isRecord(event) || !isInt(event.event_seq) || event.event_seq <= last || typeof event.cid !== 'string' || !event.cid
|
|
793
|
+
|| typeof event.reason_code !== 'string' || !event.reason_code || !isInt(event.revoked_at) || event.revoked_at < 0 || event.revoked_at > proof.as_of
|
|
794
|
+
|| typeof event.revoked_by_aid !== 'string' || !AID_RE.test(event.revoked_by_aid))
|
|
795
|
+
throw new ProtocolError('CREDENTIAL_STATUS_UNKNOWN', 'revocation event sequence is invalid');
|
|
796
|
+
last = event.event_seq;
|
|
797
|
+
}
|
|
798
|
+
if (proof.next_cursor !== '' && (!/^[1-9][0-9]*$/.test(proof.next_cursor) || !isInt(Number(proof.next_cursor)) || Number(proof.next_cursor) <= since || Number(proof.next_cursor) < last))
|
|
799
|
+
throw new ProtocolError('CREDENTIAL_STATUS_UNKNOWN', 'revocation cursor did not advance');
|
|
800
|
+
const certificate = await this.ports.certificate(authority, String(proof.signature.cert_fingerprint));
|
|
801
|
+
check();
|
|
802
|
+
const unsigned = { ...proof };
|
|
803
|
+
delete unsigned.signature;
|
|
804
|
+
verifyAgentSignature(proof.signature, unsigned, authority, () => certificate, Math.trunc(Date.now() / 1000), false);
|
|
805
|
+
const next = structuredClone(state);
|
|
806
|
+
const revoked = { ...previous.revoked };
|
|
807
|
+
for (const event of proof.events)
|
|
808
|
+
Object.defineProperty(revoked, String(event.cid), { value: Math.max(Object.hasOwn(revoked, String(event.cid)) ? Number(revoked[String(event.cid)]) : 0, Number(event.event_seq)), enumerable: true, configurable: true, writable: true });
|
|
809
|
+
next.authorities[authority] = { revoked, since_event_seq: proof.next_cursor === '' ? last : Number(proof.next_cursor), cursor: proof.next_cursor, as_of: proof.as_of, expires_at: proof.expires_at };
|
|
810
|
+
const commitGuard = { ...guard, current: () => this.ports.currentAid() === actor && (!guard || guard.current()) && Date.now() / 1000 < Number(proof.expires_at) };
|
|
811
|
+
if (!commitGuard.current())
|
|
812
|
+
throw new ProtocolError('CREDENTIAL_STATUS_UNKNOWN', 'revocation list expired before persistence');
|
|
813
|
+
await this.ports.saveRevocations(actor, next, commitGuard);
|
|
814
|
+
if (!commitGuard.current())
|
|
815
|
+
throw new ProtocolError('CREDENTIAL_STATUS_UNKNOWN', 'revocation synchronization was superseded');
|
|
816
|
+
state = next;
|
|
817
|
+
if (proof.next_cursor === '')
|
|
818
|
+
break;
|
|
819
|
+
}
|
|
820
|
+
}
|
|
821
|
+
});
|
|
822
|
+
}
|
|
823
|
+
}
|
|
47
824
|
function certificatePem(material) {
|
|
48
825
|
if (!material)
|
|
49
826
|
throw new Error('certificate is missing');
|
|
@@ -77,6 +854,10 @@ export async function verifyServiceEvent(eventName, payload, context) {
|
|
|
77
854
|
if (typeof payload.org_id !== 'string' || !AID_RE.test(payload.org_id) || responderKey !== aidKey('org.' + payload.org_id.slice(payload.org_id.indexOf('.') + 1)))
|
|
78
855
|
throw invalid('organization event authority is invalid');
|
|
79
856
|
}
|
|
857
|
+
else if (event === 'credential.revocationList') {
|
|
858
|
+
if (!responderKey.startsWith('credential.') || typeof payload.target !== 'string' || !targetAids.has(aidKey(payload.target)) || !Array.isArray(payload.events))
|
|
859
|
+
throw invalid('credential event authority is invalid');
|
|
860
|
+
}
|
|
80
861
|
else if (event.startsWith('credential.')) {
|
|
81
862
|
const cid = payload.credential_cid ?? payload.credentialCid;
|
|
82
863
|
if (typeof cid !== 'string' || !cid || payload.credential_cid !== undefined && payload.credentialCid !== undefined && payload.credential_cid !== payload.credentialCid || !responderKey.startsWith('credential.') || typeof payload.target !== 'string' || !targetAids.has(aidKey(payload.target)))
|
|
@@ -554,12 +1335,12 @@ export function validateCredential(credential, chainDepth = 0, externalProofChai
|
|
|
554
1335
|
throw new ProtocolError('CREDENTIAL_CHAIN_INVALID', 'proof_chain is too deep');
|
|
555
1336
|
for (let index = 0; index < chain.length; index++) {
|
|
556
1337
|
const item = chain[index];
|
|
557
|
-
validateCredential(item, chainDepth + 1);
|
|
558
|
-
if (canonicalText(item.proof_chain
|
|
1338
|
+
validateCredential(item, chainDepth + 1, chain.slice(index + 1));
|
|
1339
|
+
if (item.proof_chain !== undefined && canonicalText(item.proof_chain) !== canonicalText(chain.slice(index + 1)))
|
|
559
1340
|
throw new ProtocolError('CREDENTIAL_CHAIN_INVALID', 'embedded proof_chain conflicts with outer chain');
|
|
560
1341
|
}
|
|
561
1342
|
}
|
|
562
|
-
else if ('proof_chain' in credential && JSON.stringify(credential.proof_chain) !== '[]') {
|
|
1343
|
+
else if ('proof_chain' in credential && JSON.stringify(credential.proof_chain) !== '[]' || parent == null && externalProofChain !== undefined && JSON.stringify(externalProofChain) !== '[]') {
|
|
563
1344
|
throw new ProtocolError('CREDENTIAL_CHAIN_INVALID', 'root proof_chain must be empty');
|
|
564
1345
|
}
|
|
565
1346
|
if (!['low', 'medium', 'high'].includes(Object.hasOwn(credential, 'security_level') ? credential.security_level : 'medium'))
|
|
@@ -598,6 +1379,10 @@ export function createCredential(options, signer) {
|
|
|
598
1379
|
throw invalid('issuer does not match signing AID');
|
|
599
1380
|
const chain = value.proof_chain;
|
|
600
1381
|
const parentDepth = Array.isArray(chain) && isRecord(chain[0]) ? chain[0].delegation_depth ?? 0 : 0;
|
|
1382
|
+
if (!('exp' in value)) {
|
|
1383
|
+
const certExp = Math.floor(new Date(new X509Certificate(signer.certPem).validTo).getTime() / 1000);
|
|
1384
|
+
value.exp = value.parent != null && Array.isArray(chain) && isRecord(chain[0]) && isInt(chain[0].exp) ? Math.min(certExp, chain[0].exp) : certExp;
|
|
1385
|
+
}
|
|
601
1386
|
if (!('delegation_depth' in value))
|
|
602
1387
|
value.delegation_depth = value.parent !== undefined && value.parent !== null && isInt(parentDepth) ? parentDepth + 1 : 0;
|
|
603
1388
|
if (Array.isArray(value.cap))
|
|
@@ -823,7 +1608,7 @@ export function prepareOrganizationRequest(method, params, target, signer, optio
|
|
|
823
1608
|
throw invalid('organization signer or signed member CAP is required');
|
|
824
1609
|
value.credential_template = createMemberCredential(binding, options.orgSigner, { key_version: options.orgSigner.certFingerprint, ...options.credentialOptions });
|
|
825
1610
|
}
|
|
826
|
-
if (
|
|
1611
|
+
if (!['org.member.inviteAccept', 'org.member.inviteReject'].includes(method) && isRecord(value.credential_template) && !('registration_authorization' in value)) {
|
|
827
1612
|
if (!binding)
|
|
828
1613
|
throw invalid('membership binding is required for registration');
|
|
829
1614
|
const issuerSigner = options.orgSigner ?? (signer.aid === value.credential_template.iss ? signer : undefined);
|
|
@@ -856,9 +1641,14 @@ export function prepareOrganizationRequest(method, params, target, signer, optio
|
|
|
856
1641
|
const credentials = isRecord(value.credentials) ? value.credentials : {};
|
|
857
1642
|
if ('credentials' in value && !isRecord(value.credentials))
|
|
858
1643
|
throw invalid('credentials must be an object');
|
|
859
|
-
if (['org.member.join', 'org.member.inviteAccept'].includes(method)) {
|
|
860
|
-
|
|
861
|
-
|
|
1644
|
+
if (['org.member.join', 'org.member.inviteAccept', 'org.member.inviteReject'].includes(method)) {
|
|
1645
|
+
const rejection = method === 'org.member.inviteReject';
|
|
1646
|
+
if (rejection && binding && !('member_aid' in value))
|
|
1647
|
+
value.member_aid = binding.member_aid;
|
|
1648
|
+
if (!binding || ('for' in value ? value.for : signer.aid) !== binding.member_aid || value.member_aid !== binding.member_aid || binding.org_id !== value.org_id || target !== 'org.' + String(binding.org_id).slice(String(binding.org_id).indexOf('.') + 1) || !isRecord(rejection ? binding.org_signature : binding.member_signature))
|
|
1649
|
+
throw representationBindingInvalid('admission requires the original membership signature');
|
|
1650
|
+
if (rejection && (value.request_id === binding.request_id || value.nonce === binding.nonce))
|
|
1651
|
+
throw representationBindingInvalid('invitation rejection requires a distinct action identity');
|
|
862
1652
|
value.credentials = credentials;
|
|
863
1653
|
const now = Math.trunc(Date.now() / 1000);
|
|
864
1654
|
if (now >= Number(binding.exp))
|
|
@@ -927,7 +1717,7 @@ export function createMemberCredential(binding, signer, options) {
|
|
|
927
1717
|
const text = (value) => new TextDecoder().decode(canonicalJson(value));
|
|
928
1718
|
if (!isRecord(options) || Object.entries(fixed).some(([key, value]) => key in options && text(options[key]) !== text(value)) || options.parent !== undefined && options.parent !== null)
|
|
929
1719
|
throw invalid('member credential options conflict with binding');
|
|
930
|
-
return createCredential({
|
|
1720
|
+
return createCredential({ ...options, ...fixed }, signer);
|
|
931
1721
|
}
|
|
932
1722
|
export function verifyCredential(credential, context) {
|
|
933
1723
|
if (!context || !context.audience || !context.subject)
|
|
@@ -1051,8 +1841,8 @@ export function verifyCredential(credential, context) {
|
|
|
1051
1841
|
}
|
|
1052
1842
|
if (context.now === undefined) {
|
|
1053
1843
|
const completedAt = Math.trunc(Date.now() / 1000);
|
|
1054
|
-
for (const document of documents)
|
|
1055
|
-
validateCredentialAt(document, completedAt, context.audience);
|
|
1844
|
+
for (const [index, document] of documents.entries())
|
|
1845
|
+
validateCredentialAt(document, completedAt, context.audience, documents.slice(index + 1));
|
|
1056
1846
|
validateCredentialCapability(credential, { ns: context.ns, action: context.action, resource: context.resource, params: context.params, units: context.units, now: completedAt, client_ip: context.clientIp, proof_chain: context.proofChain }, () => true);
|
|
1057
1847
|
validateRequestBinding(context.requestBinding, context.method, context.target, context.params ?? {}, String(value.cid), context.nonce ?? '', context.requestId ?? '', completedAt);
|
|
1058
1848
|
for (const proof of statusProofs) {
|
|
@@ -1374,7 +2164,7 @@ export function verifyCredentialChain(credential, resolveCertificate, resolveSta
|
|
|
1374
2164
|
throw new ProtocolError('CREDENTIAL_STATUS_UNKNOWN', 'credential status is unavailable');
|
|
1375
2165
|
}
|
|
1376
2166
|
for (const [index, document] of documents.entries())
|
|
1377
|
-
verifyCredentialSignature(document, resolveCertificate, now, audience, index
|
|
2167
|
+
verifyCredentialSignature(document, resolveCertificate, now, audience, documents.slice(index + 1));
|
|
1378
2168
|
}
|
|
1379
2169
|
function validateMembershipBindingPayload(binding, allowPartial = false) {
|
|
1380
2170
|
if (!isRecord(binding))
|