generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 105
/
protocol-authorization.ts
417 lines (365 loc) · 16.4 KB
/
protocol-authorization.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
import type { MessageStore } from '../store/message-store.js';
import type { RecordsRead } from '../interfaces/records/messages/records-read.js';
import type { Filter, TimestampedMessage } from './types.js';
import type { ProtocolDefinition, ProtocolRecordDefinition, ProtocolRuleSet, ProtocolsConfigureMessage } from '../interfaces/protocols/types.js';
import type { RecordsReadMessage, RecordsWriteMessage } from '../interfaces/records/types.js';
import { Protocols } from '../utils/protocols.js';
import { RecordsWrite } from '../interfaces/records/messages/records-write.js';
import { DwnError, DwnErrorCode } from './dwn-error.js';
import { DwnInterfaceName, DwnMethodName, Message } from './message.js';
import { ProtocolAction, ProtocolActor } from '../interfaces/protocols/types.js';
const methodToAllowedActionMap: Record<string, string> = {
[DwnMethodName.Write] : ProtocolAction.Write,
[DwnMethodName.Read] : ProtocolAction.Read,
};
export class ProtocolAuthorization {
/**
* Performs protocol-based authorization against the given message.
* @throws {Error} if authorization fails.
*/
public static async authorize(
tenant: string,
incomingMessage: RecordsRead | RecordsWrite,
requesterDid: string | undefined,
messageStore: MessageStore
): Promise<void> {
// fetch ancestor message chain
const ancestorMessageChain: RecordsWriteMessage[] =
await ProtocolAuthorization.constructAncestorMessageChain(tenant, incomingMessage, messageStore);
// fetch the protocol definition
const protocolDefinition = await ProtocolAuthorization.fetchProtocolDefinition(
tenant,
incomingMessage,
ancestorMessageChain,
messageStore
);
// validate `protocolPath`
ProtocolAuthorization.verifyProtocolPath(
incomingMessage,
ancestorMessageChain,
protocolDefinition.recordDefinitions
);
// get the rule set for the inbound message
const inboundMessageRuleSet = ProtocolAuthorization.getRuleSet(
incomingMessage.message,
protocolDefinition,
ancestorMessageChain
);
// Verify `dataFormat` and `schema` for the given `recordDefinition`
ProtocolAuthorization.verifyRecordDefinition(
incomingMessage.message,
protocolDefinition,
);
// verify method invoked against the allowed actions
ProtocolAuthorization.verifyAllowedActions(
tenant,
requesterDid,
incomingMessage.message.descriptor.method,
inboundMessageRuleSet,
ancestorMessageChain,
);
// verify allowed condition of incoming message
await ProtocolAuthorization.verifyActionCondition(tenant, incomingMessage, messageStore);
}
/**
* Fetches the protocol definition based on the protocol specified in the given message.
*/
private static async fetchProtocolDefinition(
tenant: string,
incomingMessage: RecordsRead | RecordsWrite,
ancestorMessageChain: RecordsWriteMessage[],
messageStore: MessageStore
): Promise<ProtocolDefinition> {
// get the protocol URI
let protocolUri: string;
if (incomingMessage.message.descriptor.method === DwnMethodName.Write) {
protocolUri = (incomingMessage as RecordsWrite).message.descriptor.protocol!;
} else {
protocolUri = ancestorMessageChain[ancestorMessageChain.length-1].descriptor.protocol!;
}
// fetch the corresponding protocol definition
const query: Filter = {
interface : DwnInterfaceName.Protocols,
method : DwnMethodName.Configure,
protocol : protocolUri
};
const protocols = await messageStore.query(tenant, query) as ProtocolsConfigureMessage[];
if (protocols.length === 0) {
throw new Error(`unable to find protocol definition for ${protocolUri}`);
}
const protocolMessage = protocols[0];
return protocolMessage.descriptor.definition;
}
/**
* Constructs a chain of ancestor messages
* @returns the ancestor chain of messages where the first element is the root of the chain; returns empty array if no parent is specified.
*/
private static async constructAncestorMessageChain(
tenant: string,
incomingMessage: RecordsRead | RecordsWrite,
messageStore: MessageStore
)
: Promise<RecordsWriteMessage[]> {
const ancestorMessageChain: RecordsWriteMessage[] = [];
// Get first RecordsWrite in ancestor chain, or use incoming write message
let recordsWrite: RecordsWrite;
if (incomingMessage.message.descriptor.method === DwnMethodName.Write) {
recordsWrite = incomingMessage as RecordsWrite;
} else {
const recordsRead = incomingMessage as RecordsRead;
const query = {
interface : DwnInterfaceName.Records,
method : DwnMethodName.Write,
recordId : recordsRead.message.descriptor.recordId,
};
const existingMessages = await messageStore.query(tenant, query) as TimestampedMessage[];
const recordsWriteMessage = await RecordsWrite.getNewestMessage(existingMessages) as RecordsWriteMessage;
recordsWrite = await RecordsWrite.parse(recordsWriteMessage);
ancestorMessageChain.push(recordsWrite.message);
}
const protocol = recordsWrite.message.descriptor.protocol!;
const contextId = recordsWrite.message.contextId!;
// keep walking up the chain from the inbound message's parent, until there is no more parent
let currentParentId = recordsWrite.message.descriptor.parentId;
while (currentParentId !== undefined) {
// fetch parent
const query: Filter = {
interface : DwnInterfaceName.Records,
method : DwnMethodName.Write,
protocol,
contextId,
recordId : currentParentId
};
const parentMessages = await messageStore.query(tenant, query) as RecordsWriteMessage[];
if (parentMessages.length === 0) {
throw new Error(`no parent found with ID ${currentParentId}`);
}
const parent = parentMessages[0];
ancestorMessageChain.push(parent);
currentParentId = parent.descriptor.parentId;
}
return ancestorMessageChain.reverse(); // root ancestor first
}
/**
* Gets the rule set corresponding to the given message chain.
*/
private static getRuleSet(
inboundMessage: RecordsReadMessage | RecordsWriteMessage,
protocolDefinition: ProtocolDefinition,
ancestorMessageChain: RecordsWriteMessage[],
): ProtocolRuleSet {
let protocolPath: string;
if (inboundMessage.descriptor.method === DwnMethodName.Write) {
protocolPath = (inboundMessage as RecordsWriteMessage).descriptor.protocolPath!;
} else {
protocolPath = ancestorMessageChain[ancestorMessageChain.length-1].descriptor.protocolPath!;
}
const protocolPathArray = protocolPath.split('/');
// traverse rule sets using protocolPath
let currentRuleSet: { records?: { [key: string]: ProtocolRuleSet; } } = protocolDefinition;
let i = 0;
while (i < protocolPathArray.length) {
const currentRecordDefinitionId = protocolPathArray[i];
const nextRuleSet = currentRuleSet.records?.[currentRecordDefinitionId];
if (nextRuleSet === undefined) {
const partialProtocolPath = protocolPathArray.slice(0, i + 1).join('/');
throw new DwnError(DwnErrorCode.ProtocolAuthorizationMissingRuleSet,
`No rule set defined for protocolPath ${partialProtocolPath}`);
}
currentRuleSet = nextRuleSet;
i++;
}
return currentRuleSet;
}
/**
* Verifies the `protocolPath` declared in the given message (if it is a RecordsWrite) matches the path of actual ancestor chain.
* @throws {DwnError} if fails verification.
*/
private static verifyProtocolPath(
inboundMessage: RecordsRead | RecordsWrite,
ancestorMessageChain: RecordsWriteMessage[],
recordDefinitions: ProtocolRecordDefinition[],
): void {
// skip verification if this is not a RecordsWrite
if (inboundMessage.message.descriptor.method !== DwnMethodName.Write) {
return;
}
const recordDefinitionIds = recordDefinitions.map((recordDefinition) => recordDefinition.id);
const declaredProtocolPath = (inboundMessage as RecordsWrite).message.descriptor.protocolPath!;
const declaredRecordDefinitionId = ProtocolAuthorization.getRecordDefinitionId(declaredProtocolPath);
if (!recordDefinitionIds.includes(declaredRecordDefinitionId)) {
throw new DwnError(DwnErrorCode.ProtocolAuthorizationInvalidRecordDefinition,
`record with recordDefinition ${declaredRecordDefinitionId} not allowed in protocol`);
}
let ancestorProtocolPath: string = '';
for (const ancestor of ancestorMessageChain) {
const protocolPath = ancestor.descriptor.protocolPath!;
const ancestorRecordDefinitionId = ProtocolAuthorization.getRecordDefinitionId(protocolPath);
ancestorProtocolPath += `${ancestorRecordDefinitionId}/`; // e.g. `foo/bar/`, notice the trailing slash
}
const actualProtocolPath = ancestorProtocolPath + declaredRecordDefinitionId; // e.g. `foo/bar/baz`
if (declaredProtocolPath !== actualProtocolPath) {
throw new DwnError(
DwnErrorCode.ProtocolAuthorizationIncorrectProtocolPath,
`Declared protocol path '${declaredProtocolPath}' is not the same as actual protocol path '${actualProtocolPath}'.`
);
}
}
/**
* Verifies the `dataFormat` and `schema` declared in the given message (if it is a RecordsWrite) matches dataFormat
* and schema of the recordDefinition in the given protocol.
* @throws {DwnError} if fails verification.
*/
private static verifyRecordDefinition(
inboundMessage: RecordsReadMessage | RecordsWriteMessage,
protocolDefinition: ProtocolDefinition,
): void {
// skip verification if this is not a RecordsWrite
if (inboundMessage.descriptor.method !== DwnMethodName.Write) {
return;
}
const recordsWriteMessage = inboundMessage as RecordsWriteMessage;
const protocolPath = recordsWriteMessage.descriptor.protocolPath!;
const recordDefinitionId = ProtocolAuthorization.getRecordDefinitionId(protocolPath);
// existence of recordDefinition has already been verified
const recordDefinition: ProtocolRecordDefinition = Protocols.getRecordDefinition(protocolDefinition, recordDefinitionId)!;
// no `schema` specified in protocol definition means that any schema is allowed
const { schema } = recordsWriteMessage.descriptor;
if (recordDefinition.schema !== undefined && recordDefinition.schema !== schema) {
throw new DwnError(
DwnErrorCode.ProtocolAuthorizationInvalidSchema,
`recordDefinition '${recordDefinitionId}' must have schema '${recordDefinition.schema}', \
instead has '${schema}'`
);
}
// no `dataFormats` specified in protocol definition means that all dataFormats are allowed
const { dataFormat } = recordsWriteMessage.descriptor;
if (recordDefinition.dataFormats !== undefined && !recordDefinition.dataFormats.includes(dataFormat)) {
throw new DwnError(
DwnErrorCode.ProtocolAuthorizationIncorrectDataFormat,
`recordDefinition '${recordDefinitionId}' must have data format in (${recordDefinition.dataFormats}), \
instead has '${dataFormat}'`
);
}
}
/**
* Verifies the actions specified in the given message matches the allowed actions in the rule set.
* @throws {Error} if action not allowed.
*/
private static verifyAllowedActions(
tenant: string,
requesterDid: string | undefined,
incomingMessageMethod: DwnMethodName,
inboundMessageRuleSet: ProtocolRuleSet,
ancestorMessageChain: RecordsWriteMessage[],
): void {
const allowRules = inboundMessageRuleSet.allow;
if (allowRules === undefined) {
// if no allow rule is defined, owner of DWN can do everything
if (requesterDid === tenant) {
return;
} else {
throw new Error(`no allow rule defined for ${incomingMessageMethod}, ${requesterDid} is unauthorized`);
}
}
const allowedActions = new Set<string>();
for (const allowRule of allowRules) {
switch (allowRule.actor) {
case ProtocolActor.Anyone:
allowRule.actions.forEach((operation) => allowedActions.add(operation));
break;
case ProtocolActor.Author:
const messageForAuthorCheck = ProtocolAuthorization.getMessage(
ancestorMessageChain,
allowRule.protocolPath!,
);
if (messageForAuthorCheck !== undefined) {
const expectedRequesterDid = Message.getAuthor(messageForAuthorCheck);
if (requesterDid === expectedRequesterDid) {
allowRule.actions.forEach(action => allowedActions.add(action));
}
}
break;
case ProtocolActor.Recipient:
const messageForRecipientCheck = ProtocolAuthorization.getMessage(
ancestorMessageChain,
allowRule.protocolPath!,
);
if (messageForRecipientCheck !== undefined) {
const expectedRequesterDid = messageForRecipientCheck.descriptor.recipient;
if (requesterDid === expectedRequesterDid) {
allowRule.actions.forEach(action => allowedActions.add(action));
}
}
break;
// default:
// This is handled by protocol-rule-set.json validator
}
}
const inboundMessageAction = methodToAllowedActionMap[incomingMessageMethod];
if (!allowedActions.has(inboundMessageAction)) {
throw new Error(`inbound message action '${inboundMessageAction}' not in list of allowed actions (${new Array(...allowedActions).join(',')})`);
}
}
/**
* Verifies if the desired action can be taken.
* Currently the only check is: if the write is not the initial write, the author must be the same as the initial write
* @throws {Error} if fails verification
*/
private static async verifyActionCondition(tenant: string, incomingMessage: RecordsRead | RecordsWrite, messageStore: MessageStore): Promise<void> {
if (incomingMessage.message.descriptor.method === DwnMethodName.Read) {
// Currently no conditions for reads
} else if (incomingMessage.message.descriptor.method === DwnMethodName.Write) {
const recordsWrite = incomingMessage as RecordsWrite;
const isInitialWrite = await recordsWrite.isInitialWrite();
if (!isInitialWrite) {
// fetch the initialWrite
const query = {
entryId: recordsWrite.message.recordId
};
const result = await messageStore.query(tenant, query) as RecordsWriteMessage[];
// check the author of the initial write matches the author of the incoming message
const initialWrite = result[0];
const authorOfInitialWrite = Message.getAuthor(initialWrite);
if (recordsWrite.author !== authorOfInitialWrite) {
throw new Error(`author of incoming message '${recordsWrite.author}' must match to author of initial write '${authorOfInitialWrite}'`);
}
}
}
}
/**
* Gets the message from the message chain based on the path specified.
* Returns undefined if matching message does not existing in ancestor chain
* @param protocolPath `/` delimited path starting from the root ancestor.
* Each path segment denotes the expected record type declared in protocol definition.
* e.g. `A/B/C` means that the root ancestor must be of type A, its child must be of type B, followed by a child of type C.
* NOTE: the path scheme use here may be temporary dependent on final protocol spec.
*/
private static getMessage(
ancestorMessageChain: RecordsWriteMessage[],
protocolPath: string,
): RecordsWriteMessage | undefined {
const expectedAncestors = protocolPath.split('/');
// consider moving this check to ProtocolsConfigure message ingestion
if (expectedAncestors.length > ancestorMessageChain.length) {
return undefined;
}
let i = 0;
while (true) {
const expectedDefinitionId = expectedAncestors[i];
const ancestorMessage = ancestorMessageChain[i];
const actualDefinitionId = ProtocolAuthorization.getRecordDefinitionId(ancestorMessage.descriptor.protocolPath!);
if (actualDefinitionId !== expectedDefinitionId) {
throw new Error(`mismatching record schema: expecting ${expectedDefinitionId} but actual ${actualDefinitionId}`);
}
// we have found the message if we are looking at the last message specified by the path
if (i + 1 === expectedAncestors.length) {
return ancestorMessage;
}
i++;
}
}
private static getRecordDefinitionId(protocolPath: string): string {
return protocolPath.split('/').slice(-1)[0];
}
}