Skip to content

Commit

Permalink
initial rename of EventsQuery to MessagesQuery
Browse files Browse the repository at this point in the history
  • Loading branch information
LiranCohen committed Jun 26, 2024
1 parent d96caec commit a4faf47
Show file tree
Hide file tree
Showing 25 changed files with 259 additions and 254 deletions.
8 changes: 4 additions & 4 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 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,
NumberRangeFilter,
PaginationCursor,
PermissionGrantData,
Expand Down
2 changes: 1 addition & 1 deletion json-schemas/interface-methods/events-subscribe.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
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
2 changes: 1 addition & 1 deletion json-schemas/permissions/permissions-definitions.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"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-query-scope"
},
{
"$ref": "https://identity.foundation/dwn/json-schemas/permissions/scopes.json#/$defs/events-subscribe-scope"
Expand Down
4 changes: 2 additions & 2 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 Down
2 changes: 1 addition & 1 deletion src/core/dwn-error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ export enum DwnErrorCode {
Ed25519InvalidJwk = 'Ed25519InvalidJwk',
EventEmitterStreamNotOpenError = 'EventEmitterStreamNotOpenError',
EventsGrantAuthorizationMismatchedProtocol = 'EventsGrantAuthorizationMismatchedProtocol',
EventsQueryAuthorizationFailed = 'EventsQueryAuthorizationFailed',
EventsSubscribeAuthorizationFailed = 'EventsSubscribeAuthorizationFailed',
EventsSubscribeEventStreamUnimplemented = 'EventsSubscribeEventStreamUnimplemented',
GeneralJwsVerifierGetPublicKeyNotFound = 'GeneralJwsVerifierGetPublicKeyNotFound',
Expand All @@ -48,6 +47,7 @@ export enum DwnErrorCode {
IndexMissingIndexableProperty = 'IndexMissingIndexableProperty',
JwsDecodePlainObjectPayloadInvalid = 'JwsDecodePlainObjectPayloadInvalid',
MessageGetInvalidCid = 'MessageGetInvalidCid',
MessagesQueryAuthorizationFailed = 'MessagesQueryAuthorizationFailed',
ParseCidCodecNotSupported = 'ParseCidCodecNotSupported',
ParseCidMultihashNotSupported = 'ParseCidMultihashNotSupported',
PermissionsProtocolCreateGrantRecordsScopeMissingProtocol = 'PermissionsProtocolCreateGrantRecordsScopeMissingProtocol',
Expand Down
5 changes: 3 additions & 2 deletions src/core/events-grant-authorization.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { EventsSubscribeMessage } from '../types/events-types.js';
import type { MessagesQueryMessage } from '../types/messages-types.js';
import type { MessageStore } from '../types/message-store.js';
import type { PermissionGrant } from '../protocols/permission-grant.js';
import type { EventsQueryMessage, EventsSubscribeMessage } from '../types/events-types.js';

import { GrantAuthorization } from './grant-authorization.js';
import { PermissionsProtocol } from '../protocols/permissions.js';
Expand All @@ -12,7 +13,7 @@ export class EventsGrantAuthorization {
* @param messageStore Used to check if the grant has been revoked.
*/
public static async authorizeQueryOrSubscribe(input: {
incomingMessage: EventsQueryMessage | EventsSubscribeMessage,
incomingMessage: MessagesQueryMessage | EventsSubscribeMessage,
expectedGrantor: string,
expectedGrantee: string,
permissionGrant: PermissionGrant,
Expand Down
18 changes: 9 additions & 9 deletions src/dwn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@ 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 { 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 } 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 { 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,11 +66,6 @@ export class Dwn {
);

this.methodHandlers = {
[DwnInterfaceName.Events + DwnMethodName.Query]: new EventsQueryHandler(
this.didResolver,
this.messageStore,
this.eventLog,
),
[DwnInterfaceName.Events+ DwnMethodName.Subscribe]: new EventsSubscribeHandler(
this.didResolver,
this.messageStore,
Expand All @@ -81,6 +76,11 @@ export class Dwn {
this.messageStore,
this.dataStore,
),
[DwnInterfaceName.Messages + DwnMethodName.Query]: new MessagesQueryHandler(
this.didResolver,
this.messageStore,
this.eventLog,
),
[DwnInterfaceName.Protocols + DwnMethodName.Configure]: new ProtocolsConfigureHandler(
this.didResolver,
this.messageStore,
Expand Down Expand Up @@ -162,7 +162,7 @@ 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>;
public async processMessage(tenant: string, rawMessage: MessagesGetMessage): Promise<MessagesGetReply>;
Expand Down
30 changes: 15 additions & 15 deletions src/handlers/events-query.ts → src/handlers/messages-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,36 @@ import type { DidResolver } from '@web5/dids';
import type { EventLog } from '../types/event-log.js';
import type { MessageStore } from '../types/message-store.js';
import type { MethodHandler } from '../types/method-handler.js';
import type { EventsQueryMessage, EventsQueryReply } from '../types/events-types.js';
import type { MessagesQueryMessage, MessagesQueryReply } from '../types/messages-types.js';

import { authenticate } from '../core/auth.js';
import { Events } from '../utils/events.js';
import { EventsGrantAuthorization } from '../core/events-grant-authorization.js';
import { EventsQuery } from '../interfaces/events-query.js';
import { messageReplyFromError } from '../core/message-reply.js';
import { MessagesQuery } from '../interfaces/messages-query.js';
import { PermissionsProtocol } from '../protocols/permissions.js';
import { DwnError, DwnErrorCode } from '../core/dwn-error.js';


export class EventsQueryHandler implements MethodHandler {
export class MessagesQueryHandler implements MethodHandler {

constructor(private didResolver: DidResolver, private messageStore: MessageStore, private eventLog: EventLog) { }

public async handle({
tenant,
message
}: {tenant: string, message: EventsQueryMessage}): Promise<EventsQueryReply> {
let eventsQuery: EventsQuery;
}: {tenant: string, message: MessagesQueryMessage}): Promise<MessagesQueryReply> {
let messagesQuery: MessagesQuery;

try {
eventsQuery = await EventsQuery.parse(message);
messagesQuery = await MessagesQuery.parse(message);
} catch (e) {
return messageReplyFromError(e, 400);
}

try {
await authenticate(message.authorization, this.didResolver);
await EventsQueryHandler.authorizeEventsQuery(tenant, eventsQuery, this.messageStore);
await MessagesQueryHandler.authorizeMessagesQuery(tenant, messagesQuery, this.messageStore);
} catch (e) {
return messageReplyFromError(e, 401);
}
Expand All @@ -47,21 +47,21 @@ export class EventsQueryHandler implements MethodHandler {
};
}

private static async authorizeEventsQuery(tenant: string, eventsQuery: EventsQuery, messageStore: MessageStore): Promise<void> {
// if `EventsQuery` author is the same as the target tenant, we can directly grant access
if (eventsQuery.author === tenant) {
private static async authorizeMessagesQuery(tenant: string, messagesQuery: MessagesQuery, messageStore: MessageStore): Promise<void> {
// if `MessagesQuery` author is the same as the target tenant, we can directly grant access
if (messagesQuery.author === tenant) {
return;
} else if (eventsQuery.author !== undefined && eventsQuery.signaturePayload!.permissionGrantId !== undefined) {
const permissionGrant = await PermissionsProtocol.fetchGrant(tenant, messageStore, eventsQuery.signaturePayload!.permissionGrantId);
} else if (messagesQuery.author !== undefined && messagesQuery.signaturePayload!.permissionGrantId !== undefined) {
const permissionGrant = await PermissionsProtocol.fetchGrant(tenant, messageStore, messagesQuery.signaturePayload!.permissionGrantId);
await EventsGrantAuthorization.authorizeQueryOrSubscribe({
incomingMessage : eventsQuery.message,
incomingMessage : messagesQuery.message,
expectedGrantor : tenant,
expectedGrantee : eventsQuery.author,
expectedGrantee : messagesQuery.author,
permissionGrant,
messageStore
});
} else {
throw new DwnError(DwnErrorCode.EventsQueryAuthorizationFailed, 'message failed authorization');
throw new DwnError(DwnErrorCode.MessagesQueryAuthorizationFailed, 'message failed authorization');
}
}
}
6 changes: 3 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// export everything that we want to be consumable
export type { DwnConfig } from './dwn.js';
export type { EventLog } from './types/event-log.js';
export type { EventsQueryMessage, EventsQueryReply, EventsSubscribeDescriptor, EventsSubscribeMessage, EventsSubscribeReply, MessageSubscriptionHandler } from './types/events-types.js';
export type { EventsSubscribeDescriptor, EventsSubscribeMessage, EventsSubscribeReply, MessageSubscriptionHandler } from './types/events-types.js';
export type { EventListener, EventStream, EventSubscription, MessageEvent, SubscriptionReply } from './types/subscriptions.js';
export type { GenericMessage, GenericMessageReply, MessageSort, MessageSubscription, Pagination, QueryResultEntry } from './types/message-types.js';
export type { MessagesGetMessage, MessagesGetReply, MessagesGetReplyEntry } from './types/messages-types.js';
export type { MessagesFilter, MessagesGetMessage, MessagesGetReply, MessagesGetReplyEntry, MessagesQueryMessage, MessagesQueryReply } from './types/messages-types.js';
export type { Filter, EqualFilter, OneOfFilter, RangeFilter, RangeCriterion, PaginationCursor, QueryOptions } from './types/query-types.js';
export type { ProtocolsConfigureDescriptor, ProtocolDefinition, ProtocolTypes, ProtocolRuleSet, ProtocolsQueryFilter, ProtocolsConfigureMessage, ProtocolsQueryMessage, ProtocolsQueryReply } from './types/protocols-types.js';
export type { EncryptionProperty, RecordsDeleteMessage, RecordsQueryMessage, RecordsQueryReply, RecordsQueryReplyEntry, RecordsReadMessage, RecordsReadReply, RecordsSubscribeDescriptor, RecordsSubscribeMessage, RecordsSubscribeReply, RecordSubscriptionHandler, RecordsWriteDescriptor, RecordsWriteTags, RecordsWriteTagValue, RecordsWriteMessage } from './types/records-types.js';
Expand All @@ -22,7 +22,6 @@ export { DwnConstant } from './core/dwn-constant.js';
export { DwnError, DwnErrorCode } from './core/dwn-error.js';
export { DwnInterfaceName, DwnMethodName } from './enums/dwn-interface-method.js';
export { Encoder } from './utils/encoder.js';
export { EventsQuery, EventsQueryOptions } from './interfaces/events-query.js';
export { EventsSubscribe, EventsSubscribeOptions } from './interfaces/events-subscribe.js';
export { Encryption, EncryptionAlgorithm } from './utils/encryption.js';
export { EncryptionInput, KeyEncryptionInput, RecordsWrite, RecordsWriteOptions, CreateFromOptions } from './interfaces/records-write.js';
Expand All @@ -31,6 +30,7 @@ export { Jws } from './utils/jws.js';
export { KeyMaterial, PrivateJwk, PublicJwk } from './types/jose-types.js';
export { Message } from './core/message.js';
export { MessagesGet, MessagesGetOptions } from './interfaces/messages-get.js';
export { MessagesQuery, MessagesQueryOptions } from './interfaces/messages-query.js';
export { UnionMessageReply } from './core/message-reply.js';
export { MessageStore, MessageStoreOptions } from './types/message-store.js';
export { PermissionGrant } from './protocols/permission-grant.js';
Expand Down
5 changes: 3 additions & 2 deletions src/interfaces/events-subscribe.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { MessagesFilter } from '../types/messages-types.js';
import type { Signer } from '../types/signer.js';
import type { EventsFilter, EventsSubscribeDescriptor, EventsSubscribeMessage } from '../types/events-types.js';
import type { EventsSubscribeDescriptor, EventsSubscribeMessage } from '../types/events-types.js';

import { AbstractMessage } from '../core/abstract-message.js';
import { Message } from '../core/message.js';
Expand All @@ -12,7 +13,7 @@ import { DwnInterfaceName, DwnMethodName } from '../enums/dwn-interface-method.j
export type EventsSubscribeOptions = {
signer: Signer;
messageTimestamp?: string;
filters?: EventsFilter[]
filters?: MessagesFilter[]
permissionGrantId?: string;
};

Expand Down
20 changes: 10 additions & 10 deletions src/interfaces/events-query.ts → src/interfaces/messages-query.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { PaginationCursor } from '../types/query-types.js';
import type { Signer } from '../types/signer.js';
import type { EventsFilter, EventsQueryDescriptor, EventsQueryMessage } from '../types/events-types.js';
import type { MessagesFilter, MessagesQueryDescriptor, MessagesQueryMessage } from '../types/messages-types.js';

import { AbstractMessage } from '../core/abstract-message.js';
import { Events } from '../utils/events.js';
Expand All @@ -10,17 +10,17 @@ import { Time } from '../utils/time.js';
import { validateProtocolUrlNormalized } from '../utils/url.js';
import { DwnInterfaceName, DwnMethodName } from '../enums/dwn-interface-method.js';

export type EventsQueryOptions = {
export type MessagesQueryOptions = {
signer: Signer;
filters?: EventsFilter[];
filters?: MessagesFilter[];
cursor?: PaginationCursor;
messageTimestamp?: string;
permissionGrantId?: string;
};

export class EventsQuery extends AbstractMessage<EventsQueryMessage>{
export class MessagesQuery extends AbstractMessage<MessagesQueryMessage>{

public static async parse(message: EventsQueryMessage): Promise<EventsQuery> {
public static async parse(message: MessagesQueryMessage): Promise<MessagesQuery> {
Message.validateJsonSchema(message);
await Message.validateSignatureStructure(message.authorization.signature, message.descriptor);

Expand All @@ -30,12 +30,12 @@ export class EventsQuery extends AbstractMessage<EventsQueryMessage>{
}
}

return new EventsQuery(message);
return new MessagesQuery(message);
}

public static async create(options: EventsQueryOptions): Promise<EventsQuery> {
const descriptor: EventsQueryDescriptor = {
interface : DwnInterfaceName.Events,
public static async create(options: MessagesQueryOptions): Promise<MessagesQuery> {
const descriptor: MessagesQueryDescriptor = {
interface : DwnInterfaceName.Messages,
method : DwnMethodName.Query,
filters : options.filters ? Events.normalizeFilters(options.filters) : [],
messageTimestamp : options.messageTimestamp ?? Time.getCurrentTimestamp(),
Expand All @@ -55,6 +55,6 @@ export class EventsQuery extends AbstractMessage<EventsQueryMessage>{

Message.validateJsonSchema(message);

return new EventsQuery(message);
return new MessagesQuery(message);
}
}
Loading

0 comments on commit a4faf47

Please sign in to comment.