Skip to content

Commit

Permalink
Removed write protocol action (#704)
Browse files Browse the repository at this point in the history
This is considered an unnecessary and potentially confusing "sugar" now
that we have explicit `create` and `update` actions.
  • Loading branch information
thehenrytsai authored Mar 12, 2024
1 parent 4ee1ede commit aa9a246
Show file tree
Hide file tree
Showing 19 changed files with 89 additions and 66 deletions.
6 changes: 2 additions & 4 deletions json-schemas/interface-methods/protocol-rule-set.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@
"create",
"delete",
"read",
"update",
"write"
"update"
]
}
}
Expand Down Expand Up @@ -81,8 +80,7 @@
"query",
"subscribe",
"read",
"update",
"write"
"update"
]
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/core/protocol-authorization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ export class ProtocolAuthorization {
const incomingRecordsWrite = incomingMessage as RecordsWrite;

if (await incomingRecordsWrite.isInitialWrite()) {
return [ProtocolAction.Write, ProtocolAction.Create];
return [ProtocolAction.Create];
} else {
// else incoming RecordsWrite not an initial write

Expand All @@ -567,8 +567,8 @@ export class ProtocolAuthorization {
}

if (incomingMessage.author === initialWrite.author) {
// 'write', 'update' or 'co-update' action authorizes the incoming message
return [ProtocolAction.Write, ProtocolAction.CoUpdate, ProtocolAction.Update];
// 'update' or 'co-update' action authorizes the incoming message
return [ProtocolAction.CoUpdate, ProtocolAction.Update];
} else {
// An update by someone who is not the record author can only be authorized by a 'co-update' rule.
return [ProtocolAction.CoUpdate];
Expand Down
17 changes: 8 additions & 9 deletions src/types/protocols-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,31 +44,30 @@ export enum ProtocolAction {
Query = 'query',
Read = 'read',
Subscribe = 'subscribe',
Update = 'update',
Write = 'write'
Update = 'update'
}

/**
* Rules defining which actors may access a record at the given protocol path.
* Rules take three forms:
* 1. Anyone can write.
* Rules take three forms, e.g.:
* 1. Anyone can create.
* {
* who: 'anyone',
* can: ['write']
* can: ['create']
* }
*
* 2. Author of protocolPath can write; OR
* 2. Author of protocolPath can create; OR
* Recipient of protocolPath can write.
* {
* who: 'recipient'
* of: 'requestForQuote',
* can: ['write']
* can: ['create']
* }
*
* 3. Role can write.
* 3. Role can create.
* {
* role: 'friend',
* can: ['write']
* can: ['create']
* }
*/
export type ProtocolActionRule = {
Expand Down
7 changes: 4 additions & 3 deletions tests/interfaces/protocols-configure.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { ProtocolsConfigureDescriptor, ProtocolsConfigureMessage } from '..

import dexProtocolDefinition from '../vectors/protocol-definitions/dex.json' assert { type: 'json' };
import { Jws } from '../../src/utils/jws.js';
import { ProtocolAction } from '../../src/types/protocols-types.js';
import { ProtocolsConfigure } from '../../src/interfaces/protocols-configure.js';
import { TestDataGenerator } from '../utils/test-data-generator.js';
import { Time } from '../../src/utils/time.js';
Expand Down Expand Up @@ -143,14 +144,14 @@ describe('ProtocolsConfigure', () => {
secondLevel : {
$actions: [{
role : 'rootRole', // valid because 'rootRole` has $role: true
can : ['write']
can : [ProtocolAction.Create]
}]
}
},
firstLevel: {
$actions: [{
role : 'rootRole', // valid because 'rootRole` has $role: true
can : ['write']
can : [ProtocolAction.Create]
}]
}
}
Expand Down Expand Up @@ -183,7 +184,7 @@ describe('ProtocolsConfigure', () => {
chat: {
$actions: [{
role : 'thread/participant', // valid because 'thread/participant` has $role: true
can : ['write']
can : [ProtocolAction.Create]
}]
}
}
Expand Down
17 changes: 9 additions & 8 deletions tests/scenarios/delegated-grant.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { RecordEvent, RecordsWriteMessage } from '../../src/types/records-t

import chaiAsPromised from 'chai-as-promised';
import emailProtocolDefinition from '../vectors/protocol-definitions/email.json' assert { type: 'json' };
import messageProtocolDefinition from '../vectors/protocol-definitions/message.json' assert { type: 'json' };
import sinon from 'sinon';
import threadRoleProtocolDefinition from '../vectors/protocol-definitions/thread-role.json' assert { type: 'json' };

Expand Down Expand Up @@ -73,8 +74,8 @@ export function testDelegatedGrantScenarios(): void {
const bob = await TestDataGenerator.generateDidKeyPersona();
const carol = await TestDataGenerator.generateDidKeyPersona();

// Bob has the email protocol installed
const protocolDefinition = emailProtocolDefinition;
// Bob has the message protocol installed
const protocolDefinition = messageProtocolDefinition;
const protocol = protocolDefinition.protocol;
const protocolsConfig = await TestDataGenerator.generateProtocolsConfigure({
author: bob,
Expand Down Expand Up @@ -117,9 +118,9 @@ export function testDelegatedGrantScenarios(): void {
signer : Jws.createSigner(deviceX),
delegatedGrant : deviceXGrant.asDelegatedGrant(),
protocol,
protocolPath : 'email', // this comes from `types` in protocol definition
schema : protocolDefinition.types.email.schema,
dataFormat : protocolDefinition.types.email.dataFormats[0],
protocolPath : 'message', // this comes from `types` in protocol definition
schema : protocolDefinition.types.message.schema,
dataFormat : protocolDefinition.types.message.dataFormats[0],
data : deviceXData
});

Expand Down Expand Up @@ -171,9 +172,9 @@ export function testDelegatedGrantScenarios(): void {
signer : Jws.createSigner(carol),
delegatedGrant : deviceXGrant.asDelegatedGrant(),
protocol,
protocolPath : 'email', // this comes from `types` in protocol definition
schema : protocolDefinition.types.email.schema,
dataFormat : protocolDefinition.types.email.dataFormats[0],
protocolPath : 'message', // this comes from `types` in protocol definition
schema : protocolDefinition.types.message.schema,
dataFormat : protocolDefinition.types.message.dataFormats[0],
data : messageByCarolAsAlice
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ProtocolDefinition, ProtocolsConfigureMessage } from '../../../../src/types/protocols-types.js';
import { ProtocolAction, type ProtocolDefinition, type ProtocolsConfigureMessage } from '../../../../src/types/protocols-types.js';

import { expect } from 'chai';
import { Message } from '../../../../src/core/message.js';
Expand All @@ -22,7 +22,7 @@ describe('ProtocolsConfigure schema definition', () => {
$actions: [
{
who : 'unknown',
can : ['write']
can : [ProtocolAction.Create]
}
]
}
Expand Down
3 changes: 2 additions & 1 deletion tests/vectors/protocol-definitions/author-can.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
{
"who": "anyone",
"can": [
"write"
"create",
"update"
]
}
],
Expand Down
6 changes: 4 additions & 2 deletions tests/vectors/protocol-definitions/chat.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
{
"who": "anyone",
"can": [
"write"
"create",
"update"
]
},
{
Expand All @@ -44,7 +45,8 @@
{
"who": "anyone",
"can": [
"write"
"create",
"update"
]
},
{
Expand Down
3 changes: 2 additions & 1 deletion tests/vectors/protocol-definitions/contribution-reward.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
{
"who": "anyone",
"can": [
"write"
"create",
"update"
]
}
]
Expand Down
4 changes: 2 additions & 2 deletions tests/vectors/protocol-definitions/credential-issuance.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
{
"who": "anyone",
"can": [
"write"
"create"
]
}
],
Expand All @@ -31,7 +31,7 @@
"who": "recipient",
"of": "credentialApplication",
"can": [
"write"
"create"
]
}
]
Expand Down
6 changes: 3 additions & 3 deletions tests/vectors/protocol-definitions/dex.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
{
"who": "anyone",
"can": [
"write"
"create"
]
}
],
Expand All @@ -37,7 +37,7 @@
"who": "recipient",
"of": "ask",
"can": [
"write"
"create"
]
}
],
Expand All @@ -47,7 +47,7 @@
"who": "recipient",
"of": "ask/offer",
"can": [
"write"
"create"
]
}
]
Expand Down
4 changes: 2 additions & 2 deletions tests/vectors/protocol-definitions/email.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
{
"who": "anyone",
"can": [
"write"
"create"
]
},
{
Expand All @@ -38,7 +38,7 @@
{
"who": "anyone",
"can": [
"write"
"create"
]
},
{
Expand Down
3 changes: 2 additions & 1 deletion tests/vectors/protocol-definitions/free-for-all.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
{
"who": "anyone",
"can": [
"write",
"create",
"update",
"read",
"co-delete"
]
Expand Down
3 changes: 2 additions & 1 deletion tests/vectors/protocol-definitions/friend-role.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
{
"role": "friend",
"can": [
"write",
"create",
"update",
"read",
"query",
"subscribe"
Expand Down
3 changes: 2 additions & 1 deletion tests/vectors/protocol-definitions/message.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
{
"who": "anyone",
"can": [
"write"
"create",
"update"
]
}
]
Expand Down
3 changes: 2 additions & 1 deletion tests/vectors/protocol-definitions/post-comment.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
"who": "anyone",
"can": [
"read",
"write"
"create",
"update"
]
},
{
Expand Down
Loading

0 comments on commit aa9a246

Please sign in to comment.