Skip to content

Commit d07a8a4

Browse files
author
Bnonni
committed
squash and rebase: protocol-rule-record-expiry
1 parent cf50d0a commit d07a8a4

File tree

5 files changed

+52
-1
lines changed

5 files changed

+52
-1
lines changed

json-schemas/interface-methods/protocol-rule-set.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,11 @@
106106
"minimum": 0
107107
}
108108
}
109+
},
110+
"$expiry": {
111+
"$comment": "Time in milliseconds from dateCreated to wait until the record expires",
112+
"type": "number",
113+
"minimum": 1
109114
}
110115
},
111116
"patternProperties": {

src/core/dwn-error.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export enum DwnErrorCode {
5555
PrivateKeySignerUnsupportedCurve = 'PrivateKeySignerUnsupportedCurve',
5656
ProtocolAuthorizationActionNotAllowed = 'ProtocolAuthorizationActionNotAllowed',
5757
ProtocolAuthorizationActionRulesNotFound = 'ProtocolAuthorizationActionRulesNotFound',
58+
ProtocolAuthorizationExpiryReached = 'ProtocolAuthorizationExpiryReached',
5859
ProtocolAuthorizationIncorrectDataFormat = 'ProtocolAuthorizationIncorrectDataFormat',
5960
ProtocolAuthorizationIncorrectContextId = 'ProtocolAuthorizationIncorrectContextId',
6061
ProtocolAuthorizationIncorrectProtocolPath = 'ProtocolAuthorizationIncorrectProtocolPath',
@@ -74,6 +75,7 @@ export enum DwnErrorCode {
7475
ProtocolAuthorizationRoleMissingRecipient = 'ProtocolAuthorizationRoleMissingRecipient',
7576
ProtocolsConfigureDuplicateActorInRuleSet = 'ProtocolsConfigureDuplicateActorInRuleSet',
7677
ProtocolsConfigureDuplicateRoleInRuleSet = 'ProtocolsConfigureDuplicateRoleInRuleSet',
78+
ProtocolsConfigureInvalidExpiry = 'ProtocolsConfigureInvalidExpiry',
7779
ProtocolsConfigureInvalidSize = 'ProtocolsConfigureInvalidSize',
7880
ProtocolsConfigureInvalidActionMissingOf = 'ProtocolsConfigureInvalidActionMissingOf',
7981
ProtocolsConfigureInvalidActionOfNotAllowed = 'ProtocolsConfigureInvalidActionOfNotAllowed',

src/core/protocol-authorization.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,9 @@ export class ProtocolAuthorization {
157157
ancestorMessageChain,
158158
messageStore,
159159
);
160+
161+
// Verify expiry
162+
ProtocolAuthorization.verifyExpiry(incomingMessage, ruleSet)
160163
}
161164

162165
public static async authorizeQueryOrSubscribe(
@@ -687,6 +690,31 @@ export class ProtocolAuthorization {
687690
}
688691
}
689692

693+
/**
694+
* Verifies that reads adhere to the $expiry constraint if provided
695+
* @throws {Error} if expiry date is passed.
696+
*/
697+
private static verifyExpiry(
698+
incomingMessage: RecordsRead,
699+
ruleSet: ProtocolRuleSet
700+
): void {
701+
const ruleExpiry = ruleSet.$expiry;
702+
if (!ruleExpiry) {
703+
return;
704+
}
705+
706+
const dateCreated = incomingMessage.message.descriptor.filter?.dateCreated;
707+
if (!dateCreated) {
708+
return;
709+
}
710+
711+
const dateExpiry = dateCreated + ruleExpiry;
712+
if (Date.now() > dateExpiry) {
713+
throw new DwnError(DwnErrorCode.ProtocolAuthorizationExpiryReached, `dateExpiry ${dateExpiry} has passed`);
714+
}
715+
716+
}
717+
690718
/**
691719
* If the given RecordsWrite is not a role record, this method does nothing and succeeds immediately.
692720
*

src/interfaces/protocols-configure.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ export class ProtocolsConfigure extends AbstractMessage<ProtocolsConfigureMessag
132132

133133
const { ruleSet, ruleSetProtocolPath, recordTypes, roles } = input;
134134

135-
// Validate $actions in the rule set
135+
// Validate $size in the rule set
136136
if (ruleSet.$size !== undefined) {
137137
const { min = 0, max } = ruleSet.$size;
138138

@@ -144,6 +144,17 @@ export class ProtocolsConfigure extends AbstractMessage<ProtocolsConfigureMessag
144144
}
145145
}
146146

147+
// Validate $expiry in the rule set
148+
if (ruleSet.$expiry !== undefined) {
149+
150+
if (ruleSet.$expiry < 1) {
151+
throw new DwnError(
152+
DwnErrorCode.ProtocolsConfigureInvalidSize,
153+
`Invalid expiry found: expiry ${ruleSet.$expiry} less than minimum 1 at protocol path '${ruleSetProtocolPath}'`
154+
);
155+
}
156+
}
157+
147158
// validate each action rule
148159
const actionRules = ruleSet.$actions ?? [];
149160
for (let i = 0; i < actionRules.length; i++) {

src/types/protocols-types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,11 @@ export type ProtocolRuleSet = {
128128
max?: number
129129
}
130130

131+
/**
132+
* If $expiry is set, the record expiry in ms.
133+
*/
134+
$expiry?: number;
135+
131136
// JSON Schema verifies that properties other than properties prefixed with $ will actually have type ProtocolRuleSet
132137
[key: string]: any;
133138
};

0 commit comments

Comments
 (0)