Skip to content

Commit

Permalink
#439 - Return authorization in queries and reads (#523)
Browse files Browse the repository at this point in the history
* #439 - Return authorization in queries and reads
  • Loading branch information
thehenrytsai authored Sep 28, 2023
1 parent 13eb608 commit d5d418b
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 36 deletions.
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
![Statements](https://img.shields.io/badge/statements-97.77%25-brightgreen.svg?style=flat) ![Branches](https://img.shields.io/badge/branches-95.04%25-brightgreen.svg?style=flat) ![Functions](https://img.shields.io/badge/functions-94.28%25-brightgreen.svg?style=flat) ![Lines](https://img.shields.io/badge/lines-97.77%25-brightgreen.svg?style=flat)
![Statements](https://img.shields.io/badge/statements-97.77%25-brightgreen.svg?style=flat) ![Branches](https://img.shields.io/badge/branches-95.03%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.77%25-brightgreen.svg?style=flat)

- [Introduction](#introduction)
- [Installation](#installation)
Expand Down
21 changes: 3 additions & 18 deletions src/handlers/records-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { MethodHandler } from '../types/method-handler.js';
import type { RecordsWriteMessageWithOptionalEncodedData } from '../store/storage-controller.js';
import type { DataStore, DidResolver, MessageStore } from '../index.js';
import type { Filter, GenericMessage, MessageSort } from '../types/message-types.js';
import type { RecordsQueryMessage, RecordsQueryReply, RecordsQueryReplyEntry } from '../types/records-types.js';
import type { RecordsQueryMessage, RecordsQueryReply } from '../types/records-types.js';

import { authenticate } from '../core/auth.js';
import { messageReplyFromError } from '../core/message-reply.js';
Expand Down Expand Up @@ -53,28 +53,13 @@ export class RecordsQueryHandler implements MethodHandler {
}
}

const entries = RecordsQueryHandler.removeAuthorization(recordsWrites);

return {
status: { code: 200, detail: 'OK' },
entries,
status : { code: 200, detail: 'OK' },
entries : recordsWrites,
paginationMessageCid
};
}

/**
* Removes `authorization` property from each and every `RecordsWrite` message given and returns the result as a different array.
*/
private static removeAuthorization(recordsWriteMessages: RecordsWriteMessageWithOptionalEncodedData[]): RecordsQueryReplyEntry[] {
const recordsQueryReplyEntries: RecordsQueryReplyEntry[] = [];
for (const record of recordsWriteMessages) {
const { authorization: _, ...objectWithRemainingProperties } = record; // a trick to stripping away `authorization`
recordsQueryReplyEntries.push(objectWithRemainingProperties);
}

return recordsQueryReplyEntries;
}

/**
* Convert an incoming DateSort to a sort type accepted by MessageStore
* Defaults to 'dateCreated' in Descending order if no sort is supplied.
Expand Down
5 changes: 2 additions & 3 deletions src/handlers/records-read.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,10 @@ export class RecordsReadHandler implements MethodHandler {
data = result.dataStream;
}

const { authorization: _, ...recordsWriteWithoutAuthorization } = newestRecordsWrite; // a trick to stripping away `authorization`
const messageReply: RecordsReadReply ={
const messageReply: RecordsReadReply = {
status : { code: 200, detail: 'OK' },
record : {
...recordsWriteWithoutAuthorization,
...newestRecordsWrite,
data,
}
};
Expand Down
8 changes: 1 addition & 7 deletions src/types/records-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,13 +145,7 @@ export type RecordsReadMessage = {
};

export type RecordsReadReply = GenericMessageReply & {
record?: {
recordId: string,
contextId?: string;
descriptor: RecordsWriteDescriptor;
// authorization: AuthorizationModel; // intentionally omitted
attestation?: GeneralJws;
encryption?: EncryptionProperty;
record?: RecordsWriteMessage & {
data: Readable;
}
};
Expand Down
14 changes: 8 additions & 6 deletions tests/handlers/records-query.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,13 @@ export function testRecordsQueryHandler(): void {
await dwn.close();
});

it('should return recordId, descriptor, and attestation', async () => {
it('should return recordId, descriptor, authorization and attestation', async () => {
const alice = await TestDataGenerator.generatePersona();
TestStubGenerator.stubDidResolver(didResolver, [alice]);
const bob = await TestDataGenerator.generatePersona();
TestStubGenerator.stubDidResolver(didResolver, [alice, bob]);
const dataFormat = 'myAwesomeDataFormat';

const write = await TestDataGenerator.generateRecordsWrite({ author: alice, dataFormat });
const write = await TestDataGenerator.generateRecordsWrite({ author: alice, attesters: [bob], dataFormat });
const writeReply = await dwn.processMessage(alice.did, write.message, write.dataStream);
expect(writeReply.status.code).to.equal(202);

Expand All @@ -80,7 +81,8 @@ export function testRecordsQueryHandler(): void {

expect(reply.entries?.length).to.equal(1);
const entry = reply.entries![0];
expect(entry.attestation).to.equal(write.message.attestation);
expect(entry.authorization).to.deep.equal(write.message.authorization);
expect(entry.attestation).to.deep.equal(write.message.attestation);
expect(entry.descriptor).to.deep.equal(write.message.descriptor);
expect(entry.recordId).to.equal(write.message.recordId);
});
Expand Down Expand Up @@ -302,7 +304,7 @@ export function testRecordsQueryHandler(): void {
expect(reply.entries![0].encodedData).to.equal(Encoder.bytesToBase64Url(write2.dataBytes!));
});

it('should not include `authorization` in returned records', async () => {
it('should include `authorization` in returned records', async () => {
const alice = await TestDataGenerator.generatePersona();
const { message, dataStream } = await TestDataGenerator.generateRecordsWrite({ author: alice });

Expand All @@ -321,7 +323,7 @@ export function testRecordsQueryHandler(): void {
const queryReply = await dwn.processMessage(alice.did, queryData.message);
expect(queryReply.status.code).to.equal(200);
expect(queryReply.entries?.length).to.equal(1);
expect((queryReply.entries![0] as any).authorization).to.equal(undefined);
expect((queryReply.entries![0] as any).authorization).to.deep.equal(message.authorization);
});

it('should include `attestation` in returned records', async () => {
Expand Down
3 changes: 2 additions & 1 deletion tests/handlers/records-read.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ export function testRecordsReadHandler(): void {
const readReply = await dwn.handleRecordsRead(alice.did, recordsRead.message);
expect(readReply.status.code).to.equal(200);
expect(readReply.record).to.exist;
expect(readReply.record?.descriptor).to.exist;
expect(readReply.record?.authorization).to.deep.equal(message.authorization);
expect(readReply.record?.descriptor).to.deep.equal(message.descriptor);

const dataFetched = await DataStream.toBytes(readReply.record!.data!);
expect(ArrayUtility.byteArraysEqual(dataFetched, dataBytes!)).to.be.true;
Expand Down

0 comments on commit d5d418b

Please sign in to comment.