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

Authorize RecordsWrites with protocol roles #524

Merged
merged 8 commits into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ Here's to a thrilling Hacktoberfest voyage with us! 🎉
# Decentralized Web Node (DWN) SDK <!-- omit in toc -->

Code Coverage
thehenrytsai marked this conversation as resolved.
Show resolved Hide resolved
![Statements](https://img.shields.io/badge/statements-97.78%25-brightgreen.svg?style=flat) ![Branches](https://img.shields.io/badge/branches-94.92%25-brightgreen.svg?style=flat) ![Functions](https://img.shields.io/badge/functions-94.26%25-brightgreen.svg?style=flat) ![Lines](https://img.shields.io/badge/lines-97.78%25-brightgreen.svg?style=flat)
![Statements](https://img.shields.io/badge/statements-97.8%25-brightgreen.svg?style=flat) ![Branches](https://img.shields.io/badge/branches-95.18%25-brightgreen.svg?style=flat) ![Functions](https://img.shields.io/badge/functions-94.26%25-brightgreen.svg?style=flat) ![Lines](https://img.shields.io/badge/lines-97.8%25-brightgreen.svg?style=flat)

- [Introduction](#introduction)
- [Installation](#installation)
Expand Down
1 change: 0 additions & 1 deletion src/core/protocol-authorization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,6 @@ export class ProtocolAuthorization {
throw new Error(`no action rule defined for ${incomingMessage.message.descriptor.method}, ${author} is unauthorized`);
}

// Get role being invoked. Currently only Reads support role-based authorization
const invokedRole = incomingMessage.authorizationPayload?.protocolRole;

for (const actionRule of actionRules) {
Expand Down
26 changes: 11 additions & 15 deletions src/interfaces/records-write.ts
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ export class RecordsWrite {
await ProtocolAuthorization.authorize(tenant, this, this, messageStore);
} else if (this.author === tenant) {
// if author is the same as the target tenant, we can directly grant access
} else if (this.author !== undefined && this.authorizationPayload?.permissionsGrantId !== undefined) {
} else if (this.author !== undefined && this.authorizationPayload!.permissionsGrantId !== undefined) {
diehuxx marked this conversation as resolved.
Show resolved Hide resolved
await RecordsGrantAuthorization.authorizeWrite(tenant, this, this.author, messageStore);
} else {
throw new Error('message failed authorization');
Expand Down Expand Up @@ -653,23 +653,19 @@ export class RecordsWrite {
signer: Signer,
additionalProperties?: { permissionsGrantId?: string, protocolRole?: string },
): Promise<AuthorizationModel> {
const authorizationPayload: RecordsWriteAuthorSignaturePayload = {
recordId,
descriptorCid
};

const attestationCid = attestation ? await Cid.computeCid(attestation) : undefined;
const encryptionCid = encryption ? await Cid.computeCid(encryption) : undefined;

if (contextId !== undefined) { authorizationPayload.contextId = contextId; } // assign `contextId` only if it is defined
if (attestationCid !== undefined) { authorizationPayload.attestationCid = attestationCid; } // assign `attestationCid` only if it is defined
if (encryptionCid !== undefined) { authorizationPayload.encryptionCid = encryptionCid; } // assign `encryptionCid` only if it is defined
if (additionalProperties?.permissionsGrantId !== undefined) {
authorizationPayload.permissionsGrantId = additionalProperties?.permissionsGrantId;
}
if (additionalProperties?.protocolRole !== undefined) {
authorizationPayload.protocolRole = additionalProperties?.protocolRole;
}
const authorizationPayload: RecordsWriteAuthorSignaturePayload = {
recordId,
descriptorCid,
contextId,
attestationCid,
encryptionCid,
permissionsGrantId : additionalProperties?.permissionsGrantId,
protocolRole : additionalProperties?.protocolRole
};
removeUndefinedProperties(authorizationPayload);
diehuxx marked this conversation as resolved.
Show resolved Hide resolved

const authorizationPayloadBytes = Encoder.objectToBytes(authorizationPayload);

Expand Down
1 change: 0 additions & 1 deletion src/types/records-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ export type RecordsWriteDescriptor = {
method: DwnMethodName.Write;
protocol?: string;
protocolPath?: string;
protocolRole?: string;
recipient?: string;
schema?: string;
parentId?: string;
Expand Down
7 changes: 7 additions & 0 deletions tests/vectors/protocol-definitions/friend-role.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,15 @@
"friend": {
"$globalRole": true
},
"fan": {
"$globalRole": true
},
"chat": {
"$actions": [
{
"role": "fan",
"can": "read"
},
{
"role": "friend",
"can": "write"
Expand Down