Skip to content

Commit

Permalink
remove unecessary abstraction
Browse files Browse the repository at this point in the history
  • Loading branch information
LiranCohen committed Aug 30, 2024
1 parent 0884e59 commit 2efa570
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 233 deletions.
88 changes: 29 additions & 59 deletions packages/api/src/dwn-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,45 +312,6 @@ export class DwnApi {
this.cachedPermissionsApi = new CachedPermissions({ agent: this.agent, cachedDefault: true });
}

/**
* API to interact with the DWN Permissions when the agent is connected to a delegateDid.
*
* NOTE: This is an EXPERIMENTAL API that will change behavior.
* @beta
*/
private get connected() {
return {
/**
* Finds the appropriate permission grants associated with a message request
*
* (optionally) Caches the results for the given parameters to avoid redundant queries.
*/
findPermissionGrantForMessage: async <T extends DwnInterface>({ messageParams, cached = true }:{
cached?: boolean;
messageParams: {
messageType: T;
protocol: string;
}
}) : Promise<PermissionGrant> => {
if(!this.delegateDid) {
throw new Error('AgentDwnApi: Cannot find connected grants without a signer DID');
}

const delegateGrant = await this.cachedPermissionsApi.getPermission({
connectedDid : this.connectedDid,
delegateDid : this.delegateDid,
messageType : messageParams.messageType,
protocol : messageParams.protocol,
delegate : true,
cached,
});

const grant = await PermissionGrant.parse({ connectedDid: this.delegateDid, agent: this.agent, message: delegateGrant.message });
return grant;
}
};
}

/**
* API to interact with Grants
*
Expand Down Expand Up @@ -586,11 +547,13 @@ export class DwnApi {

if (this.delegateDid) {
// if an app is scoped down to a specific protocolPath or contextId, it must include those filters in the read request
const { rawMessage: delegatedGrant } = await this.connected.findPermissionGrantForMessage({
messageParams: {
messageType : DwnInterface.RecordsDelete,
protocol : request.protocol,
}
const { message: delegatedGrant } = await this.cachedPermissionsApi.getPermission({
connectedDid : this.connectedDid,
delegateDid : this.delegateDid,
messageType : DwnInterface.RecordsDelete,
protocol : request.protocol,
cached : true,
delegate : true
});

// set the required delegated grant and grantee DID for the read operation
Expand Down Expand Up @@ -632,11 +595,13 @@ export class DwnApi {

if (this.delegateDid) {
// if an app is scoped down to a specific protocolPath or contextId, it must include those filters in the read request
const { rawMessage: delegatedGrant } = await this.connected.findPermissionGrantForMessage({
messageParams: {
messageType : DwnInterface.RecordsQuery,
protocol : agentRequest.messageParams.filter.protocol,
}
const { message: delegatedGrant } = await this.cachedPermissionsApi.getPermission({
connectedDid : this.connectedDid,
delegateDid : this.delegateDid,
messageType : DwnInterface.RecordsQuery,
protocol : request.protocol,
cached : true,
delegate : true
});

// set the required delegated grant and grantee DID for the read operation
Expand Down Expand Up @@ -710,11 +675,13 @@ export class DwnApi {

if (this.delegateDid) {
// if an app is scoped down to a specific protocolPath or contextId, it must include those filters in the read request
const { rawMessage: delegatedGrant } = await this.connected.findPermissionGrantForMessage({
messageParams: {
messageType : DwnInterface.RecordsRead,
protocol : request.protocol
}
const { message: delegatedGrant } = await this.cachedPermissionsApi.getPermission({
connectedDid : this.connectedDid,
delegateDid : this.delegateDid,
messageType : DwnInterface.RecordsRead,
protocol : request.protocol,
cached : true,
delegate : true
});

// set the required delegated grant and grantee DID for the read operation
Expand Down Expand Up @@ -838,11 +805,14 @@ export class DwnApi {

// if impersonation is enabled, fetch the delegated grant to use with the write operation
if (this.delegateDid) {
const { rawMessage: delegatedGrant } = await this.connected.findPermissionGrantForMessage({
messageParams: {
messageType : DwnInterface.RecordsWrite,
protocol : dwnRequestParams.messageParams.protocol,
}

const { message: delegatedGrant } = await this.cachedPermissionsApi.getPermission({
connectedDid : this.connectedDid,
delegateDid : this.delegateDid,
messageType : DwnInterface.RecordsWrite,
protocol : request.message?.protocol,
cached : true,
delegate : true
});

// set the required delegated grant and grantee DID for the write operation
Expand Down
101 changes: 45 additions & 56 deletions packages/api/src/record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -749,12 +749,15 @@ export class Record implements RecordModel {
target : this._connectedDid,
};
if (this._delegateDid) {

// if an app is scoped down to a specific protocolPath or contextId, it must include those filters in the read request
const { rawMessage: delegatedGrant } = await this.findPermissionGrantForMessage({
messageParams: {
messageType : DwnInterface.RecordsWrite,
protocol : this.protocol,
}
const { message: delegatedGrant } = await this._cachedPermissions.getPermission({
connectedDid : this._connectedDid,
delegateDid : this._delegateDid,
messageType : DwnInterface.RecordsWrite,
protocol : this.protocol,
delegate : true,
cached : true,
});

// set the required delegated grant and grantee DID for the read operation
Expand Down Expand Up @@ -827,12 +830,14 @@ export class Record implements RecordModel {
}

if (this._delegateDid) {
// if an app is scoped down to a specific protocolPath or contextId, it must include those filters in the read request
const { rawMessage: delegatedGrant } = await this.findPermissionGrantForMessage({
messageParams: {
messageType : DwnInterface.RecordsDelete,
protocol : this.protocol,
}

const { message: delegatedGrant } = await this._cachedPermissions.getPermission({
connectedDid : this._connectedDid,
delegateDid : this._delegateDid,
messageType : DwnInterface.RecordsDelete,
protocol : this.protocol,
delegate : true,
cached : true,
});

// set the required delegated grant and grantee DID for the read operation
Expand Down Expand Up @@ -886,12 +891,14 @@ export class Record implements RecordModel {


if (this._delegateDid) {
// if an app is scoped down to a specific protocolPath or contextId, it must include those filters in the read request
const { rawMessage: delegatedGrant } = await this.findPermissionGrantForMessage({
messageParams: {
messageType : DwnInterface.RecordsWrite,
protocol : this.protocol,
}
// if an app is scoped down to a specific protocolPath or contextId, it must include those filters in the read request
const { message: delegatedGrant } = await this._cachedPermissions.getPermission({
connectedDid : this._connectedDid,
delegateDid : this._delegateDid,
messageType : DwnInterface.RecordsWrite,
protocol : this.protocol,
delegate : true,
cached : true,
});

// set the required delegated grant and grantee DID for the read operation
Expand Down Expand Up @@ -930,11 +937,13 @@ export class Record implements RecordModel {
};
if (this._delegateDid) {
// if an app is scoped down to a specific protocolPath or contextId, it must include those filters in the read request
const { rawMessage: delegatedGrant } = await this.findPermissionGrantForMessage({
messageParams: {
messageType : DwnInterface.RecordsDelete,
protocol : this.protocol,
}
const { message: delegatedGrant } = await this._cachedPermissions.getPermission({
connectedDid : this._connectedDid,
delegateDid : this._delegateDid,
messageType : DwnInterface.RecordsDelete,
protocol : this.protocol,
delegate : true,
cached : true,
});

// set the required delegated grant and grantee DID for the read operation
Expand All @@ -953,11 +962,13 @@ export class Record implements RecordModel {
};
if (this._delegateDid) {
// if an app is scoped down to a specific protocolPath or contextId, it must include those filters in the read request
const { rawMessage: delegatedGrant } = await this.findPermissionGrantForMessage({
messageParams: {
messageType : DwnInterface.RecordsWrite,
protocol : this.protocol,
}
const { message: delegatedGrant } = await this._cachedPermissions.getPermission({
connectedDid : this._connectedDid,
delegateDid : this._delegateDid,
messageType : DwnInterface.RecordsWrite,
protocol : this.protocol,
delegate : true,
cached : true,
});

// set the required delegated grant and grantee DID for the read operation
Expand Down Expand Up @@ -1004,11 +1015,13 @@ export class Record implements RecordModel {

if (this._delegateDid) {
// if an app is scoped down to a specific protocolPath or contextId, it must include those filters in the read request
const { rawMessage: delegatedGrant } = await this.findPermissionGrantForMessage({
messageParams: {
messageType : DwnInterface.RecordsRead,
protocol : this.protocol,
}
const { message: delegatedGrant } = await this._cachedPermissions.getPermission({
connectedDid : this._connectedDid,
delegateDid : this._delegateDid,
messageType : DwnInterface.RecordsRead,
protocol : this.protocol,
delegate : true,
cached : true,
});

// set the required delegated grant and grantee DID for the read operation
Expand Down Expand Up @@ -1069,28 +1082,4 @@ export class Record implements RecordModel {
private isRecordsDeleteDescriptor(descriptor: DwnMessageDescriptor[DwnInterface.RecordsWrite | DwnInterface.RecordsDelete]): descriptor is DwnMessageDescriptor[DwnInterface.RecordsDelete] {
return descriptor.interface + descriptor.method === DwnInterface.RecordsDelete;
}

private async findPermissionGrantForMessage<T extends DwnInterface>({ messageParams, cached = true }:{
cached?: boolean;
messageParams: {
messageType: T;
protocol: string;
}
}) : Promise<PermissionGrant> {
if(!this._delegateDid) {
throw new Error('Record: Cannot find connected grants without a signer DID');
}

const delegateGrant = await this._cachedPermissions.getPermission({
connectedDid : this._connectedDid,
delegateDid : this._delegateDid,
messageType : messageParams.messageType,
protocol : messageParams.protocol,
delegate : true,
cached,
});

const grant = await PermissionGrant.parse({ connectedDid: this._delegateDid, agent: this._agent, message: delegateGrant.message });
return grant;
}
}
Loading

0 comments on commit 2efa570

Please sign in to comment.