Skip to content

Commit

Permalink
construction of grants and requests are not async
Browse files Browse the repository at this point in the history
  • Loading branch information
LiranCohen committed Aug 11, 2024
1 parent a7543fc commit b75e8d3
Show file tree
Hide file tree
Showing 18 changed files with 30 additions and 40 deletions.
2 changes: 1 addition & 1 deletion src/interfaces/records-delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export class RecordsDelete extends AbstractMessage<RecordsDeleteMessage> {
* @param messageStore Used to check if the grant has been revoked.
*/
public async authorizeDelegate(recordsWriteToDelete: RecordsWriteMessage, messageStore: MessageStore): Promise<void> {
const delegatedGrant = await PermissionGrant.parse(this.message.authorization!.authorDelegatedGrant!);
const delegatedGrant = new PermissionGrant(this.message.authorization!.authorDelegatedGrant!);
await RecordsGrantAuthorization.authorizeDelete({
recordsDeleteMessage : this.message,
recordsWriteToDelete,
Expand Down
2 changes: 1 addition & 1 deletion src/interfaces/records-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export class RecordsQuery extends AbstractMessage<RecordsQueryMessage> {
* @param messageStore Used to check if the grant has been revoked.
*/
public async authorizeDelegate(messageStore: MessageStore): Promise<void> {
const delegatedGrant = await PermissionGrant.parse(this.message.authorization!.authorDelegatedGrant!);
const delegatedGrant = new PermissionGrant(this.message.authorization!.authorDelegatedGrant!);
await RecordsGrantAuthorization.authorizeQueryOrSubscribe({
incomingMessage : this.message,
expectedGrantee : this.signer!,
Expand Down
2 changes: 1 addition & 1 deletion src/interfaces/records-read.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export class RecordsRead extends AbstractMessage<RecordsReadMessage> {
* @param messageStore Used to check if the grant has been revoked.
*/
public async authorizeDelegate(matchedRecordsWrite: RecordsWriteMessage, messageStore: MessageStore): Promise<void> {
const delegatedGrant = await PermissionGrant.parse(this.message.authorization!.authorDelegatedGrant!);
const delegatedGrant = new PermissionGrant(this.message.authorization!.authorDelegatedGrant!);
await RecordsGrantAuthorization.authorizeRead({
recordsReadMessage : this.message,
recordsWriteMessageToBeRead : matchedRecordsWrite,
Expand Down
2 changes: 1 addition & 1 deletion src/interfaces/records-subscribe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export class RecordsSubscribe extends AbstractMessage<RecordsSubscribeMessage> {
* @param messageStore Used to check if the grant has been revoked.
*/
public async authorizeDelegate(messageStore: MessageStore): Promise<void> {
const delegatedGrant = await PermissionGrant.parse(this.message.authorization!.authorDelegatedGrant!);
const delegatedGrant = new PermissionGrant(this.message.authorization!.authorDelegatedGrant!);
await RecordsGrantAuthorization.authorizeQueryOrSubscribe({
incomingMessage : this.message,
expectedGrantor : this.author!,
Expand Down
4 changes: 2 additions & 2 deletions src/interfaces/records-write.ts
Original file line number Diff line number Diff line change
Expand Up @@ -795,7 +795,7 @@ export class RecordsWrite implements MessageInterface<RecordsWriteMessage> {
* @param messageStore Used to check if the grant has been revoked.
*/
public async authorizeAuthorDelegate(messageStore: MessageStore): Promise<void> {
const delegatedGrant = await PermissionGrant.parse(this.message.authorization.authorDelegatedGrant!);
const delegatedGrant = new PermissionGrant(this.message.authorization.authorDelegatedGrant!);
await RecordsGrantAuthorization.authorizeWrite({
recordsWriteMessage : this.message,
expectedGrantor : this.author!,
Expand All @@ -810,7 +810,7 @@ export class RecordsWrite implements MessageInterface<RecordsWriteMessage> {
* @param messageStore Used to check if the grant has been revoked.
*/
public async authorizeOwnerDelegate(messageStore: MessageStore): Promise<void> {
const delegatedGrant = await PermissionGrant.parse(this.message.authorization.ownerDelegatedGrant!);
const delegatedGrant = new PermissionGrant(this.message.authorization.ownerDelegatedGrant!);
await RecordsGrantAuthorization.authorizeWrite({
recordsWriteMessage : this.message,
expectedGrantor : this.owner!,
Expand Down
7 changes: 1 addition & 6 deletions src/protocols/permission-grant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,7 @@ export class PermissionGrant {
*/
public readonly conditions?: PermissionConditions;

public static async parse(message: DataEncodedRecordsWriteMessage): Promise<PermissionGrant> {
const permissionGrant = new PermissionGrant(message);
return permissionGrant;
}

private constructor(message: DataEncodedRecordsWriteMessage) {
constructor(message: DataEncodedRecordsWriteMessage) {
// properties derived from the generic DWN message properties
this.id = message.recordId;
this.grantor = Message.getSigner(message)!;
Expand Down
7 changes: 1 addition & 6 deletions src/protocols/permission-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,7 @@ export class PermissionRequest {
*/
public readonly conditions?: PermissionConditions;

public static async parse(message: DataEncodedRecordsWriteMessage): Promise<PermissionRequest> {
const permissionRequest = new PermissionRequest(message);
return permissionRequest;
}

private constructor(message: DataEncodedRecordsWriteMessage) {
constructor(message: DataEncodedRecordsWriteMessage) {
// properties derived from the generic DWN message properties
this.id = message.recordId;
this.requester = Message.getSigner(message)!;
Expand Down
6 changes: 3 additions & 3 deletions src/protocols/permissions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ export class PermissionsProtocol {
}

const permissionGrantMessage = possibleGrantMessage as DataEncodedRecordsWriteMessage;
const permissionGrant = await PermissionGrant.parse(permissionGrantMessage);
const permissionGrant = new PermissionGrant(permissionGrantMessage);

return permissionGrant;
}
Expand All @@ -421,11 +421,11 @@ export class PermissionsProtocol {
const grant = await PermissionsProtocol.fetchGrant(tenant, messageStore, incomingMessage.descriptor.parentId!);
return grant.scope;
} else if (incomingMessage.descriptor.protocolPath === PermissionsProtocol.grantPath) {
const grant = await PermissionGrant.parse(incomingMessage);
const grant = new PermissionGrant(incomingMessage);
return grant.scope;
} else {
// if the record is not a grant or revocation, it must be a request
const request = await PermissionRequest.parse(incomingMessage);
const request = new PermissionRequest(incomingMessage);
return request.scope;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/utils/records.ts
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ export class Records {
if (authorDelegatedGrantDefined) {
const delegatedGrant = message.authorization!.authorDelegatedGrant!;

const permissionGrant = await PermissionGrant.parse(delegatedGrant);
const permissionGrant = new PermissionGrant(delegatedGrant);
if (permissionGrant.delegated !== true) {
throw new DwnError(
DwnErrorCode.RecordsAuthorDelegatedGrantNotADelegatedGrant,
Expand Down Expand Up @@ -455,7 +455,7 @@ export class Records {

if (ownerDelegatedGrantDefined) {
const delegatedGrant = message.authorization!.ownerDelegatedGrant!;
const permissionGrant = await PermissionGrant.parse(delegatedGrant);
const permissionGrant = new PermissionGrant(delegatedGrant);

if (permissionGrant.delegated !== true) {
throw new DwnError(
Expand Down
2 changes: 1 addition & 1 deletion tests/features/author-delegated-grant.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1221,7 +1221,7 @@ export function testAuthorDelegatedGrant(): void {
// 3. Alice revokes the grant
const permissionRevoke = await PermissionsProtocol.createRevocation({
signer : Jws.createSigner(alice),
grant : await PermissionGrant.parse(deviceXGrant.dataEncodedMessage),
grant : new PermissionGrant(deviceXGrant.dataEncodedMessage),
});
const revocationDataStream = DataStream.fromBytes(permissionRevoke.permissionRevocationBytes);
const permissionRevokeReply = await dwn.processMessage(alice.did, permissionRevoke.recordsWrite.message, { dataStream: revocationDataStream });
Expand Down
2 changes: 1 addition & 1 deletion tests/features/owner-delegated-grant.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -603,7 +603,7 @@ export function testOwnerDelegatedGrant(): void {
// 3. Alice revokes the grant
const permissionRevoke = await PermissionsProtocol.createRevocation({
signer : Jws.createSigner(alice),
grant : await PermissionGrant.parse(appXGrant.dataEncodedMessage),
grant : new PermissionGrant(appXGrant.dataEncodedMessage),
});
const revocationDataStream = DataStream.fromBytes(permissionRevoke.permissionRevocationBytes);
const permissionRevokeReply = await dwn.processMessage(
Expand Down
8 changes: 4 additions & 4 deletions tests/features/permissions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export function testPermissions(): void {
// createRevocation with a protocol derived from the grant
const revokeWrite = await PermissionsProtocol.createRevocation({
signer : Jws.createSigner(alice),
grant : await PermissionGrant.parse(grantWrite.dataEncodedMessage),
grant : new PermissionGrant(grantWrite.dataEncodedMessage),
dateRevoked : Time.getCurrentTimestamp()
});
expect(revokeWrite.recordsWrite.message.descriptor.tags).to.deep.equal({ protocol: testProtocol });
Expand Down Expand Up @@ -188,7 +188,7 @@ export function testPermissions(): void {
// derive the grantId and protocol from the grant record
const revokeWrite = await PermissionsProtocol.createRevocation({
signer : Jws.createSigner(alice),
grant : await PermissionGrant.parse(grantWrite.dataEncodedMessage),
grant : new PermissionGrant(grantWrite.dataEncodedMessage),
dateRevoked : Time.getCurrentTimestamp()
});

Expand Down Expand Up @@ -316,7 +316,7 @@ export function testPermissions(): void {
// 7. Verify that non-owner cannot revoke the grant
const unauthorizedRevokeWrite = await PermissionsProtocol.createRevocation({
signer : Jws.createSigner(bob),
grant : await PermissionGrant.parse(grantWrite.dataEncodedMessage),
grant : new PermissionGrant(grantWrite.dataEncodedMessage),
dateRevoked : Time.getCurrentTimestamp(),
});

Expand All @@ -331,7 +331,7 @@ export function testPermissions(): void {
// 8. Alice revokes the permission grant for Bob
const revokeWrite = await PermissionsProtocol.createRevocation({
signer : Jws.createSigner(alice),
grant : await PermissionGrant.parse(grantWrite.dataEncodedMessage),
grant : new PermissionGrant(grantWrite.dataEncodedMessage),
dateRevoked : Time.getCurrentTimestamp(),
});

Expand Down
2 changes: 1 addition & 1 deletion tests/handlers/messages-read.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ export function testMessagesReadHandler(): void {
// Alice revokes Carol's grant
const permissionRevocationCarol = await PermissionsProtocol.createRevocation({
signer : Jws.createSigner(alice),
grant : await PermissionGrant.parse(permissionGrantCarol.dataEncodedMessage),
grant : new PermissionGrant(permissionGrantCarol.dataEncodedMessage),
});
const permissionRevocationCarolDataStream = DataStream.fromBytes(permissionRevocationCarol.permissionRevocationBytes);
const permissionRevocationCarolReply = await dwn.processMessage(
Expand Down
2 changes: 1 addition & 1 deletion tests/handlers/protocols-query.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ export function testProtocolsQueryHandler(): void {
// 4. Alice revokes Bob's grant
const revokeWrite = await PermissionsProtocol.createRevocation({
signer : Jws.createSigner(alice),
grant : await PermissionGrant.parse(permissionGrant.dataEncodedMessage),
grant : new PermissionGrant(permissionGrant.dataEncodedMessage),
dateRevoked : Time.getCurrentTimestamp()
});

Expand Down
2 changes: 1 addition & 1 deletion tests/protocols/permission-request.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('PermissionRequest', () => {
scope
});

const parsedPermissionRequest = await PermissionRequest.parse(permissionRequest.dataEncodedMessage);
const parsedPermissionRequest = new PermissionRequest(permissionRequest.dataEncodedMessage);
expect (parsedPermissionRequest.id).to.equal(permissionRequest.dataEncodedMessage.recordId);
expect (parsedPermissionRequest.delegated).to.equal(true);
expect (parsedPermissionRequest.scope).to.deep.equal(scope);
Expand Down
8 changes: 4 additions & 4 deletions tests/protocols/permissions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ describe('PermissionsProtocol', () => {
}
});

const request = await PermissionRequest.parse(permissionRequest.dataEncodedMessage);
const request = new PermissionRequest(permissionRequest.dataEncodedMessage);

const scope = await PermissionsProtocol.getScopeFromPermissionRecord(
alice.did,
Expand All @@ -77,7 +77,7 @@ describe('PermissionsProtocol', () => {
dateExpires : Time.createOffsetTimestamp({ seconds: 100 })
});

const grant = await PermissionGrant.parse(grantMessage);
const grant = new PermissionGrant(grantMessage);

const scope = await PermissionsProtocol.getScopeFromPermissionRecord(
alice.did,
Expand Down Expand Up @@ -107,7 +107,7 @@ describe('PermissionsProtocol', () => {
const indexes = await grantRecordsWrite.constructIndexes(true);
await messageStore.put(alice.did, grantMessage, indexes);

const grant = await PermissionGrant.parse(grantMessage);
const grant = new PermissionGrant(grantMessage);

const revocation = await PermissionsProtocol.createRevocation({
signer : Jws.createSigner(alice),
Expand Down Expand Up @@ -139,7 +139,7 @@ describe('PermissionsProtocol', () => {
});

// notice the grant is not stored in the message store
const grant = await PermissionGrant.parse(grantMessage);
const grant = new PermissionGrant(grantMessage);

const revocation = await PermissionsProtocol.createRevocation({
signer : Jws.createSigner(alice),
Expand Down
4 changes: 2 additions & 2 deletions tests/scenarios/messages-query.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ export function testMessagesQueryScenarios(): void {
// revoke permissions for proto1
const revokeProto1 = await PermissionsProtocol.createRevocation({
signer : Jws.createSigner(alice),
grant : await PermissionGrant.parse(grantProto1.dataEncodedMessage),
grant : new PermissionGrant(grantProto1.dataEncodedMessage),
});
const revokeProto1Response = await dwn.processMessage(
alice.did,
Expand All @@ -412,7 +412,7 @@ export function testMessagesQueryScenarios(): void {
// revoke permissions for proto2
const revokeProto2 = await PermissionsProtocol.createRevocation({
signer : Jws.createSigner(alice),
grant : await PermissionGrant.parse(grantProto2.dataEncodedMessage),
grant : new PermissionGrant(grantProto2.dataEncodedMessage),
});
const revokeProto2Response = await dwn.processMessage(
alice.did,
Expand Down
4 changes: 2 additions & 2 deletions tests/scenarios/subscriptions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ export function testSubscriptionScenarios(): void {
// revoke permissions for proto1
const revokeProto1 = await PermissionsProtocol.createRevocation({
signer : Jws.createSigner(alice),
grant : await PermissionGrant.parse(grantProto1.dataEncodedMessage),
grant : new PermissionGrant(grantProto1.dataEncodedMessage),
});
const revokeProto1Response = await dwn.processMessage(
alice.did,
Expand All @@ -495,7 +495,7 @@ export function testSubscriptionScenarios(): void {
// revoke permissions for proto2
const revokeProto2 = await PermissionsProtocol.createRevocation({
signer : Jws.createSigner(alice),
grant : await PermissionGrant.parse(grantProto2.dataEncodedMessage),
grant : new PermissionGrant(grantProto2.dataEncodedMessage),
});
const revokeProto2Response = await dwn.processMessage(
alice.did,
Expand Down

0 comments on commit b75e8d3

Please sign in to comment.