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

Moving Events interface methods to Messages interface #773

Merged
merged 3 commits into from
Jun 26, 2024
Merged
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
12 changes: 6 additions & 6 deletions build/compile-validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ import Authorization from '../json-schemas/authorization.json' assert { type: 'j
import AuthorizationDelegatedGrant from '../json-schemas/authorization-delegated-grant.json' assert { type: 'json' };
import AuthorizationOwner from '../json-schemas/authorization-owner.json' assert { type: 'json' };
import Definitions from '../json-schemas/definitions.json' assert { type: 'json' };
import EventsFilter from '../json-schemas/interface-methods/events-filter.json' assert { type: 'json' };
import EventsQuery from '../json-schemas/interface-methods/events-query.json' assert { type: 'json' };
import EventsSubscribe from '../json-schemas/interface-methods/events-subscribe.json' assert { type: 'json' };
import GeneralJwk from '../json-schemas/jwk/general-jwk.json' assert { type: 'json' };
import GeneralJws from '../json-schemas/general-jws.json' assert { type: 'json' };
import GenericSignaturePayload from '../json-schemas/signature-payloads/generic-signature-payload.json' assert { type: 'json' };
import JwkVerificationMethod from '../json-schemas/jwk-verification-method.json' assert { type: 'json' };
import MessagesFilter from '../json-schemas/interface-methods/messages-filter.json' assert { type: 'json' };
import MessagesGet from '../json-schemas/interface-methods/messages-get.json' assert { type: 'json' };
import MessagesQuery from '../json-schemas/interface-methods/messages-query.json' assert { type: 'json' };
import MessagesSubscribe from '../json-schemas/interface-methods/messages-subscribe.json' assert { type: 'json' };
import NumberRangeFilter from '../json-schemas/interface-methods/number-range-filter.json' assert { type: 'json' };
import PaginationCursor from '../json-schemas/interface-methods/pagination-cursor.json' assert { type: 'json' };
import PermissionGrantData from '../json-schemas/permissions/permission-grant-data.json' assert { type: 'json' };
Expand Down Expand Up @@ -62,14 +62,14 @@ const schemas = {
RecordsWrite,
RecordsWriteDataEncoded,
RecordsWriteUnidentified,
EventsFilter,
EventsQuery,
EventsSubscribe,
Definitions,
GeneralJwk,
GeneralJws,
JwkVerificationMethod,
MessagesFilter,
MessagesGet,
MessagesQuery,
MessagesSubscribe,
NumberRangeFilter,
PaginationCursor,
PermissionGrantData,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://identity.foundation/dwn/json-schemas/events-filter.json",
"$id": "https://identity.foundation/dwn/json-schemas/messages-filter.json",
"type": "object",
"additionalProperties": false,
"minProperties": 1,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://identity.foundation/dwn/json-schemas/events-query.json",
"$id": "https://identity.foundation/dwn/json-schemas/messages-query.json",
"type": "object",
"additionalProperties": false,
"required": [
Expand All @@ -23,7 +23,7 @@
"properties": {
"interface": {
"enum": [
"Events"
"Messages"
],
"type": "string"
},
Expand All @@ -39,7 +39,7 @@
"filters": {
"type": "array",
"items": {
"$ref": "https://identity.foundation/dwn/json-schemas/events-filter.json"
"$ref": "https://identity.foundation/dwn/json-schemas/messages-filter.json"
}
},
"cursor": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://identity.foundation/dwn/json-schemas/events-subscribe.json",
"$id": "https://identity.foundation/dwn/json-schemas/messages-subscribe.json",
"type": "object",
"additionalProperties": false,
"required": [
Expand All @@ -23,7 +23,7 @@
"properties": {
"interface": {
"enum": [
"Events"
"Messages"
],
"type": "string"
},
Expand All @@ -39,7 +39,7 @@
"filters": {
"type": "array",
"items": {
"$ref": "https://identity.foundation/dwn/json-schemas/events-filter.json"
"$ref": "https://identity.foundation/dwn/json-schemas/messages-filter.json"
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions json-schemas/permissions/permissions-definitions.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
"scope": {
"oneOf": [
{
"$ref": "https://identity.foundation/dwn/json-schemas/permissions/scopes.json#/$defs/events-query-scope"
"$ref": "https://identity.foundation/dwn/json-schemas/permissions/scopes.json#/$defs/messages-get-scope"
},
{
"$ref": "https://identity.foundation/dwn/json-schemas/permissions/scopes.json#/$defs/events-subscribe-scope"
"$ref": "https://identity.foundation/dwn/json-schemas/permissions/scopes.json#/$defs/messages-query-scope"
},
{
"$ref": "https://identity.foundation/dwn/json-schemas/permissions/scopes.json#/$defs/messages-get-scope"
"$ref": "https://identity.foundation/dwn/json-schemas/permissions/scopes.json#/$defs/messages-subscribe-scope"
},
{
"$ref": "https://identity.foundation/dwn/json-schemas/permissions/scopes.json#/$defs/protocols-query-scope"
Expand Down
8 changes: 4 additions & 4 deletions json-schemas/permissions/scopes.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"$id": "https://identity.foundation/dwn/json-schemas/permissions/scopes.json",
"type": "object",
"$defs": {
"events-query-scope": {
"messages-query-scope": {
"type": "object",
"additionalProperties": false,
"required" : [
Expand All @@ -12,7 +12,7 @@
],
"properties": {
"interface": {
"const": "Events"
"const": "Messages"
},
"method": {
"const": "Query"
Expand All @@ -22,7 +22,7 @@
}
}
},
"events-subscribe-scope": {
"messages-subscribe-scope": {
"type": "object",
"additionalProperties": false,
"required" : [
Expand All @@ -31,7 +31,7 @@
],
"properties": {
"interface": {
"const": "Events"
"const": "Messages"
},
"method": {
"const": "Subscribe"
Expand Down
9 changes: 5 additions & 4 deletions src/core/dwn-error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@ export enum DwnErrorCode {
ComputeCidMultihashNotSupported = 'ComputeCidMultihashNotSupported',
Ed25519InvalidJwk = 'Ed25519InvalidJwk',
EventEmitterStreamNotOpenError = 'EventEmitterStreamNotOpenError',
EventsGrantAuthorizationMismatchedProtocol = 'EventsGrantAuthorizationMismatchedProtocol',
EventsQueryAuthorizationFailed = 'EventsQueryAuthorizationFailed',
EventsSubscribeAuthorizationFailed = 'EventsSubscribeAuthorizationFailed',
EventsSubscribeEventStreamUnimplemented = 'EventsSubscribeEventStreamUnimplemented',
MessagesGrantAuthorizationMismatchedProtocol = 'EventsGrantAuthorizationMismatchedProtocol',
MessagesSubscribeAuthorizationFailed = 'MessagesSubscribeAuthorizationFailed',
MessagesSubscribeEventStreamUnimplemented = 'MessagesSubscribeEventStreamUnimplemented',
GeneralJwsVerifierGetPublicKeyNotFound = 'GeneralJwsVerifierGetPublicKeyNotFound',
GeneralJwsVerifierInvalidSignature = 'GeneralJwsVerifierInvalidSignature',
GrantAuthorizationGrantExpired = 'GrantAuthorizationGrantExpired',
Expand All @@ -46,6 +45,8 @@ export enum DwnErrorCode {
MessagesGetInvalidCid = 'MessagesGetInvalidCid',
MessagesGetAuthorizationFailed = 'MessagesGetAuthorizationFailed',
MessagesGetVerifyScopeFailed = 'MessagesGetVerifyScopeFailed',
MessageGetInvalidCid = 'MessageGetInvalidCid',
MessagesQueryAuthorizationFailed = 'MessagesQueryAuthorizationFailed',
ParseCidCodecNotSupported = 'ParseCidCodecNotSupported',
ParseCidMultihashNotSupported = 'ParseCidMultihashNotSupported',
PermissionsProtocolCreateGrantRecordsScopeMissingProtocol = 'PermissionsProtocolCreateGrantRecordsScopeMissingProtocol',
Expand Down
46 changes: 0 additions & 46 deletions src/core/events-grant-authorization.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/core/message-reply.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export function messageReplyFromError(e: unknown, code: number): GenericMessageR
export type UnionMessageReply = GenericMessageReply & {
/**
* Resulting message entries or events returned from the invocation of the corresponding message.
* e.g. the resulting messages from a RecordsQuery, or array of messageCid strings for EventsQuery
* e.g. the resulting messages from a RecordsQuery, or array of messageCid strings for MessagesQuery
* Mutually exclusive with `record`.
*/
entries?: QueryResultEntry[] | ProtocolsConfigureMessage[] | MessagesGetReplyEntry[] | string[];
Expand Down
39 changes: 38 additions & 1 deletion src/core/messages-grant-authorization.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type { GenericMessage } from '../types/message-types.js';
import type { MessagesGetMessage } from '../types/messages-types.js';
import type { MessagesPermissionScope } from '../types/permission-types.js';
import type { MessageStore } from '../types/message-store.js';
import type { PermissionGrant } from '../protocols/permission-grant.js';
import type { ProtocolsConfigureMessage } from '../types/protocols-types.js';
import type { DataEncodedRecordsWriteMessage, RecordsDeleteMessage, RecordsWriteMessage } from '../types/records-types.js';
import type { MessagesGetMessage, MessagesQueryMessage, MessagesSubscribeMessage } from '../types/messages-types.js';

import { DwnInterfaceName } from '../enums/dwn-interface-method.js';
import { GrantAuthorization } from './grant-authorization.js';
Expand Down Expand Up @@ -43,6 +43,43 @@ export class MessagesGrantAuthorization {
await MessagesGrantAuthorization.verifyScope(expectedGrantor, messageToGet, scope, messageStore);
}

/**
* Authorizes the scope of a permission grant for MessagesQuery or MessagesSubscribe.
* @param messageStore Used to check if the grant has been revoked.
*/
public static async authorizeQueryOrSubscribe(input: {
incomingMessage: MessagesQueryMessage | MessagesSubscribeMessage,
expectedGrantor: string,
expectedGrantee: string,
permissionGrant: PermissionGrant,
messageStore: MessageStore,
}): Promise<void> {
const {
incomingMessage, expectedGrantor, expectedGrantee, permissionGrant, messageStore
} = input;

await GrantAuthorization.performBaseValidation({
incomingMessage,
expectedGrantor,
expectedGrantee,
permissionGrant,
messageStore
});

// if the grant is scoped to a specific protocol, ensure that all of the query filters must include that protocol
if (PermissionsProtocol.hasProtocolScope(permissionGrant.scope)) {
const scopedProtocol = permissionGrant.scope.protocol;
for (const filter of incomingMessage.descriptor.filters) {
if (filter.protocol !== scopedProtocol) {
throw new DwnError(
DwnErrorCode.MessagesGrantAuthorizationMismatchedProtocol,
`The protocol ${filter.protocol} does not match the scoped protocol ${scopedProtocol}`
);
}
}
}
}

/**
* Verifies the given record against the scope of the given grant.
*/
Expand Down
23 changes: 11 additions & 12 deletions src/dwn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,17 @@ import type { Readable } from 'readable-stream';
import type { ResumableTaskStore } from './types/resumable-task-store.js';
import type { TenantGate } from './core/tenant-gate.js';
import type { UnionMessageReply } from './core/message-reply.js';
import type { EventsQueryMessage, EventsQueryReply, EventsSubscribeMessage, EventsSubscribeMessageOptions, EventsSubscribeReply, MessageSubscriptionHandler } from './types/events-types.js';
import type { GenericMessage, GenericMessageReply } from './types/message-types.js';
import type { MessagesGetMessage, MessagesGetReply } from './types/messages-types.js';
import type { MessagesGetMessage, MessagesGetReply, MessagesQueryMessage, MessagesQueryReply, MessagesSubscribeMessage, MessagesSubscribeMessageOptions, MessagesSubscribeReply, MessageSubscriptionHandler } from './types/messages-types.js';
import type { ProtocolsConfigureMessage, ProtocolsQueryMessage, ProtocolsQueryReply } from './types/protocols-types.js';
import type { RecordsDeleteMessage, RecordsQueryMessage, RecordsQueryReply, RecordsReadMessage, RecordsReadReply, RecordsSubscribeMessage, RecordsSubscribeMessageOptions, RecordsSubscribeReply, RecordSubscriptionHandler, RecordsWriteMessage, RecordsWriteMessageOptions } from './types/records-types.js';

import { AllowAllTenantGate } from './core/tenant-gate.js';
import { EventsQueryHandler } from './handlers/events-query.js';
import { EventsSubscribeHandler } from './handlers/events-subscribe.js';
import { Message } from './core/message.js';
import { messageReplyFromError } from './core/message-reply.js';
import { MessagesGetHandler } from './handlers/messages-get.js';
import { MessagesQueryHandler } from './handlers/messages-query.js';
import { MessagesSubscribeHandler } from './handlers/messages-subscribe.js';
import { ProtocolsConfigureHandler } from './handlers/protocols-configure.js';
import { ProtocolsQueryHandler } from './handlers/protocols-query.js';
import { RecordsDeleteHandler } from './handlers/records-delete.js';
Expand Down Expand Up @@ -66,20 +65,20 @@ export class Dwn {
);

this.methodHandlers = {
[DwnInterfaceName.Events + DwnMethodName.Query]: new EventsQueryHandler(
[DwnInterfaceName.Messages + DwnMethodName.Get]: new MessagesGetHandler(
this.didResolver,
this.messageStore,
this.eventLog,
this.dataStore,
),
[DwnInterfaceName.Events+ DwnMethodName.Subscribe]: new EventsSubscribeHandler(
[DwnInterfaceName.Messages + DwnMethodName.Query]: new MessagesQueryHandler(
this.didResolver,
this.messageStore,
this.eventStream,
this.eventLog,
),
[DwnInterfaceName.Messages + DwnMethodName.Get]: new MessagesGetHandler(
[DwnInterfaceName.Messages + DwnMethodName.Subscribe]: new MessagesSubscribeHandler(
this.didResolver,
this.messageStore,
this.dataStore,
this.eventStream,
),
[DwnInterfaceName.Protocols + DwnMethodName.Configure]: new ProtocolsConfigureHandler(
this.didResolver,
Expand Down Expand Up @@ -162,9 +161,9 @@ export class Dwn {
* Processes the given DWN message and returns with a reply.
* @param tenant The tenant DID to route the given message to.
*/
public async processMessage(tenant: string, rawMessage: EventsQueryMessage): Promise<EventsQueryReply>;
public async processMessage(tenant: string, rawMessage: MessagesQueryMessage): Promise<MessagesQueryReply>;
public async processMessage(
tenant: string, rawMessage: EventsSubscribeMessage, options?: EventsSubscribeMessageOptions): Promise<EventsSubscribeReply>;
tenant: string, rawMessage: MessagesSubscribeMessage, options?: MessagesSubscribeMessageOptions): Promise<MessagesSubscribeReply>;
public async processMessage(tenant: string, rawMessage: MessagesGetMessage): Promise<MessagesGetReply>;
public async processMessage(tenant: string, rawMessage: ProtocolsConfigureMessage): Promise<GenericMessageReply>;
public async processMessage(tenant: string, rawMessage: ProtocolsQueryMessage): Promise<ProtocolsQueryReply>;
Expand Down
1 change: 0 additions & 1 deletion src/enums/dwn-interface-method.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export enum DwnInterfaceName {
Events = 'Events',
Messages = 'Messages',
Protocols = 'Protocols',
Records = 'Records'
Expand Down
Loading