-
Notifications
You must be signed in to change notification settings - Fork 57
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import type { DwnResponse, Web5Agent } from '@web5/agent'; | ||
import type { | ||
import { | ||
UnionMessageReply, | ||
RecordsReadOptions, | ||
RecordsQueryOptions, | ||
|
@@ -11,6 +11,7 @@ import type { | |
ProtocolsConfigureMessage, | ||
ProtocolsConfigureOptions, | ||
ProtocolsConfigureDescriptor, | ||
Message, | ||
} from '@tbd54566975/dwn-sdk-js'; | ||
|
||
import { isEmptyObject } from '@web5/common'; | ||
|
@@ -19,6 +20,20 @@ import { DwnInterfaceName, DwnMethodName } from '@tbd54566975/dwn-sdk-js'; | |
import { Record } from './record.js'; | ||
import { Protocol } from './protocol.js'; | ||
import { dataToBlob } from './utils.js'; | ||
import { PermissionsGrant } from '@tbd54566975/dwn-sdk-js'; | ||
import { PermissionsGrantMessage } from '@tbd54566975/dwn-sdk-js'; | ||
import { PermissionsGrantOptions } from '@tbd54566975/dwn-sdk-js'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can these be consolidated into the existing import statement a few lines up? |
||
|
||
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'>; | ||
|
@@ -91,6 +106,11 @@ export type RecordsWriteRequest = { | |
store?: boolean; | ||
} | ||
|
||
export type PermissionGrantRequest = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @diehuxx This type appears to be a duplicate. It isn't used anywhere as best I can tell. Should it be removed? |
||
target?: string; | ||
message?: Omit<Partial<PermissionsGrantOptions>, 'authorizationSigner'>; | ||
} | ||
|
||
export type RecordsWriteResponse = { | ||
status: UnionMessageReply['status']; | ||
record?: Record | ||
|
@@ -377,4 +397,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 }; | ||
}, | ||
}; | ||
} | ||
} |
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'; | ||
|
@@ -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]; | ||
|
||
|
@@ -569,4 +571,57 @@ describe('DwnApi', () => { | |
}); | ||
}); | ||
}); | ||
|
||
describe('permissions.grant()', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could a test for sending a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If I understand agents correctly, setting |
||
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; | ||
}); | ||
}); | ||
}); | ||
}); |
There was a problem hiding this comment.
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?There was a problem hiding this comment.
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 intooptions
then re built and signed. Why not just send the existing message?