Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose DWN PermissionsGrant Message #252

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions packages/agent/src/dwn-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
RecordsWriteMessage,
RecordsWriteOptions,
Signer,
PermissionsGrant,
} from '@tbd54566975/dwn-sdk-js';

import { Jose } from '@web5/crypto';
Expand Down Expand Up @@ -69,6 +70,9 @@
[DwnInterfaceName.Records + DwnMethodName.Query] : RecordsQuery,
[DwnInterfaceName.Records + DwnMethodName.Write] : RecordsWrite,
[DwnInterfaceName.Records + DwnMethodName.Delete] : RecordsDelete,
[DwnInterfaceName.Permissions + DwnMethodName.Grant] : PermissionsGrant,
[DwnInterfaceName.Permissions + DwnMethodName.Request] : PermissionsGrant,
[DwnInterfaceName.Permissions + DwnMethodName.Revoke] : PermissionsGrant,
[DwnInterfaceName.Protocols + DwnMethodName.Query] : ProtocolsQuery,
[DwnInterfaceName.Protocols + DwnMethodName.Configure] : ProtocolsConfigure,
};
Expand Down Expand Up @@ -184,8 +188,11 @@
});
dwnRpcRequest.message = message;
messageData = data;

} else if ('message' in request) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@diehuxx The approach to sending a PermissionsGrant is different than how both Record and ProtocolsConfigure messages are sent to a remote DWN. What was the motivation for introducing a new message property to the send request object?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've been wanting to make that addition separate of PermissionsGrants. The existing code forces an existing message to be turned into options then re built and signed. Why not just send the existing message?

dwnRpcRequest.message = request.message;
messageData = request.dataStream;

Check warning on line 193 in packages/agent/src/dwn-manager.ts

View check run for this annotation

Codecov / codecov/patch

packages/agent/src/dwn-manager.ts#L192-L193

Added lines #L192 - L193 were not covered by tests
} else {
// construct message from messageOptions
const { message } = await this.constructDwnMessage({ request });
dwnRpcRequest.message = message;
messageData = request.dataStream;
Expand Down Expand Up @@ -224,7 +231,7 @@
dwnReply = await this.agent.rpcClient.sendDwnRequest(dwnRpcRequest as DwnRpcRequest);
break;
} catch(error: unknown) {
const message = (error instanceof Error) ? error.message : 'Uknown error';
const message = (error instanceof Error) ? error.message : 'Unknown error';

Check warning on line 234 in packages/agent/src/dwn-manager.ts

View check run for this annotation

Codecov / codecov/patch

packages/agent/src/dwn-manager.ts#L234

Added line #L234 was not covered by tests
errorMessages.push({ url: dwnUrl, message });
}
}
Expand Down Expand Up @@ -405,7 +412,7 @@
author: string,
messageOptions: unknown,
messageType: string
}): Promise<EventsGet | MessagesGet | RecordsRead | RecordsQuery | RecordsWrite | RecordsDelete | ProtocolsQuery | ProtocolsConfigure> {
}): Promise<Message<GenericMessage>> {
const { author, messageOptions, messageType } = options;

const dwnAuthorizationSigner = await this.constructDwnAuthorizationSigner(author);
Expand Down
5 changes: 4 additions & 1 deletion packages/agent/src/types/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type {
RecordsDeleteMessage,
ProtocolsQueryMessage,
ProtocolsConfigureMessage,
GenericMessage,
} from '@tbd54566975/dwn-sdk-js';

import { DidResolver } from '@web5/dids';
Expand Down Expand Up @@ -57,7 +58,9 @@ export type ProcessDwnRequest = DwnRequest & {
store?: boolean;
};

export type SendDwnRequest = DwnRequest & (ProcessDwnRequest | { messageCid: string })
export type SendDwnRequest = DwnRequest & (
ProcessDwnRequest | { messageCid: string } | { message: GenericMessage, dataStream?: Blob | ReadableStream | Readable }
);

/**
* TODO: add JSDoc
Expand Down
77 changes: 76 additions & 1 deletion packages/api/src/dwn-api.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import type { DwnResponse, Web5Agent } from '@web5/agent';
import type {
import {
UnionMessageReply,
PermissionsGrant,
PermissionsGrantMessage,
PermissionsGrantOptions,
RecordsReadOptions,
RecordsQueryOptions,
RecordsWriteMessage,
Expand All @@ -11,6 +14,7 @@ import type {
ProtocolsConfigureMessage,
ProtocolsConfigureOptions,
ProtocolsConfigureDescriptor,
Message,
} from '@tbd54566975/dwn-sdk-js';

import { isEmptyObject } from '@web5/common';
Expand All @@ -20,6 +24,17 @@ import { Record } from './record.js';
import { Protocol } from './protocol.js';
import { dataToBlob } from './utils.js';

export type PermissionsGrantRequest = {
target?: string;
message: Omit<PermissionsGrantOptions, 'authorizationSigner'>;
}

export type PermissionsGrantResponse = {
permissionsGrant: PermissionsGrant | undefined;
permissionsGrantId: string | undefined;
status: UnionMessageReply['status']
};

export type ProtocolsConfigureRequest = {
message: Omit<ProtocolsConfigureOptions, 'authorizationSigner'>;
}
Expand Down Expand Up @@ -377,4 +392,64 @@ export class DwnApi {
},
};
}

get permissions() {
return {
/**
* Create and store a PermissionsGrant DWN message
* @param request.target The DID whose DWN the PermissionsGrant message will be sent to. If undefined,
* the message will be stored in the local DWN of the connectedDid.
* @param request.message The message options used to create the PermissionsGrant messsage.
* @returns {PermissionsGrantResponse}
*/
grant: async (request: PermissionsGrantRequest): Promise<PermissionsGrantResponse> => {
const agentRequest = {
author : this.connectedDid,
messageOptions : request.message,
messageType : DwnInterfaceName.Permissions + DwnMethodName.Grant,
target : request.target || this.connectedDid
};

let agentResponse: DwnResponse;

if (request.target) {
agentResponse = await this.agent.sendDwnRequest(agentRequest);
} else {
agentResponse = await this.agent.processDwnRequest(agentRequest);
}

const { message, reply: { status } } = agentResponse;

let permissionsGrant: PermissionsGrant | undefined;
let permissionsGrantId: string | undefined;
if (200 <= status.code && status.code <= 299) {
permissionsGrant = await PermissionsGrant.parse(message as PermissionsGrantMessage);
permissionsGrantId = await Message.getCid(permissionsGrant.message);
}

return {
permissionsGrant,
permissionsGrantId,
status,
};
},

/**
* Send an existing PermissionsGrant message to a remote DWN.
* @param target DID whose remote DWN the Permissions message will be sent to.
* @param message The PermissionsGrant message that will be sent.
* @returns {UnionMessageReply['status']}
*/
send: async (target: string, message: PermissionsGrant): Promise<{ status: UnionMessageReply['status'] }> => {
const { reply: { status } } = await this.agent.sendDwnRequest({
messageType : message.message.descriptor.interface + message.message.descriptor.method,
author : message.author,
target : target,
message : message.message,
});

return { status };
},
};
}
}
55 changes: 55 additions & 0 deletions packages/api/tests/dwn-api.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { PortableDid } from '@web5/dids';
import type { ManagedIdentity } from '@web5/agent';
import { Temporal } from '@js-temporal/polyfill';

import { expect } from 'chai';
import { TestManagedAgent } from '@web5/agent';
Expand All @@ -8,6 +9,7 @@ import { DwnApi } from '../src/dwn-api.js';
import { testDwnUrl } from './test-config.js';
import { TestUserAgent } from './utils/test-user-agent.js';
import emailProtocolDefinition from './fixtures/protocol-definitions/email.json' assert { type: 'json' };
import { DwnInterfaceName, DwnMethodName } from '@tbd54566975/dwn-sdk-js';

let testDwnUrls: string[] = [testDwnUrl];

Expand Down Expand Up @@ -569,4 +571,57 @@ describe('DwnApi', () => {
});
});
});

describe('permissions.grant()', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could a test for sending a PermissionsGrant to a remote DWN be added in this PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I understand agents correctly, setting dwn.permissions.grant(target : bobDid.did, ...) will send to a remote. We do that in the test in describe('target: did'

describe('agent', () => {
it('returns a PermissionsGrant that can be invoked', async () => {
const { did: bobDid } = await testAgent.createIdentity({ testDwnUrls });

const { status, permissionsGrant, permissionsGrantId } = await dwn.permissions.grant({
message: {
dateExpires : Temporal.Now.instant().toString({ smallestUnit: 'microseconds' }),
grantedBy : aliceDid.did,
grantedFor : aliceDid.did,
grantedTo : bobDid.did,
scope : {
interface : DwnInterfaceName.Records,
method : DwnMethodName.Write,
}
},
});

expect(status.code).to.eq(202);
expect(permissionsGrant).not.to.be.undefined;
expect(permissionsGrantId).not.to.be.undefined;

// send to Alice's remote DWN
const { status: statusSend } = await dwn.permissions.send(aliceDid.did, permissionsGrant);

expect(statusSend.code).to.eq(202);
});
});

describe('target: did', async () => {
it('returns a PermissionsGrant that can be invoked', async () => {
const { did: bobDid } = await testAgent.createIdentity({ testDwnUrls });

const { status, permissionsGrant } = await dwn.permissions.grant({
target : bobDid.did,
message : {
dateExpires : Temporal.Now.instant().toString({ smallestUnit: 'microseconds' }),
grantedBy : aliceDid.did,
grantedFor : aliceDid.did,
grantedTo : bobDid.did,
scope : {
interface : DwnInterfaceName.Records,
method : DwnMethodName.Write,
}
},
});

expect(status.code).to.eq(202);
expect(permissionsGrant).not.to.be.undefined;
});
});
});
});
Loading