generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement `EventsGet` interface
- Loading branch information
Showing
16 changed files
with
417 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
{ | ||
"$schema": "http://json-schema.org/draft-07/schema#", | ||
"$id": "https://identity.foundation/dwn/json-schemas/events-get.json", | ||
"type": "object", | ||
"additionalProperties": false, | ||
"required": [ | ||
"authorization", | ||
"descriptor" | ||
], | ||
"properties": { | ||
"authorization": { | ||
"$ref": "https://identity.foundation/dwn/json-schemas/general-jws.json" | ||
}, | ||
"descriptor": { | ||
"type": "object", | ||
"additionalProperties": false, | ||
"required": [ | ||
"interface", | ||
"method" | ||
], | ||
"properties": { | ||
"interface": { | ||
"enum": [ | ||
"Events" | ||
], | ||
"type": "string" | ||
}, | ||
"method": { | ||
"enum": [ | ||
"Get" | ||
], | ||
"type": "string" | ||
}, | ||
"watermark": { | ||
"type": "string" | ||
} | ||
} | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import type { DidResolver } from '../../../index.js'; | ||
import type { EventLog } from '../../../event-log/event-log.js'; | ||
import type { GetEventsOptions } from '../../../event-log/event-log.js'; | ||
import type { MethodHandler } from '../../types.js'; | ||
import type { EventsGetMessage, EventsGetReply } from '../types.js'; | ||
|
||
import { EventsGet } from '../messages/events-get.js'; | ||
import { MessageReply } from '../../../core/message-reply.js'; | ||
import { authenticate, authorize } from '../../../core/auth.js'; | ||
|
||
type HandleArgs = {tenant: string, message: EventsGetMessage}; | ||
|
||
export class EventsGetHandler implements MethodHandler { | ||
constructor(private didResolver: DidResolver, private eventLog: EventLog) {} | ||
|
||
public async handle({ tenant, message }: HandleArgs): Promise<EventsGetReply> { | ||
let eventsGet: EventsGet; | ||
|
||
try { | ||
eventsGet = await EventsGet.parse(message); | ||
} catch (e) { | ||
return MessageReply.fromError(e, 400); | ||
} | ||
|
||
try { | ||
await authenticate(message.authorization, this.didResolver); | ||
await authorize(tenant, eventsGet); | ||
} catch (e) { | ||
return MessageReply.fromError(e, 401); | ||
} | ||
|
||
// if watermark was provided in message, get all events _after_ the watermark. | ||
// Otherwise, get all events. | ||
let options: GetEventsOptions | undefined; | ||
if (message.descriptor.watermark) { | ||
options = { gt: message.descriptor.watermark }; | ||
} | ||
|
||
const events = await this.eventLog.getEvents(tenant, options); | ||
|
||
return { | ||
status: { code: 200, detail: 'OK' }, | ||
events | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import type { SignatureInput } from '../../../jose/jws/general/types.js'; | ||
import type { EventsGetDescriptor, EventsGetMessage } from '../types.js'; | ||
|
||
import { validateAuthorizationIntegrity } from '../../../core/auth.js'; | ||
import { DwnInterfaceName, DwnMethodName, Message } from '../../../core/message.js'; | ||
|
||
export type EventsGetOptions = { | ||
watermark?: string; | ||
authorizationSignatureInput: SignatureInput; | ||
}; | ||
|
||
export class EventsGet extends Message<EventsGetMessage> { | ||
|
||
public static async parse(message: EventsGetMessage): Promise<EventsGet> { | ||
Message.validateJsonSchema(message); | ||
await validateAuthorizationIntegrity(message); | ||
|
||
return new EventsGet(message); | ||
} | ||
|
||
public static async create(options: EventsGetOptions): Promise<EventsGet> { | ||
const descriptor: EventsGetDescriptor = { | ||
interface : DwnInterfaceName.Events, | ||
method : DwnMethodName.Get, | ||
}; | ||
|
||
if (options.watermark) { | ||
descriptor.watermark = options.watermark; | ||
} | ||
|
||
const authorization = await Message.signAsAuthorization(descriptor, options.authorizationSignatureInput); | ||
const message = { descriptor, authorization }; | ||
|
||
Message.validateJsonSchema(message); | ||
|
||
return new EventsGet(message); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import type { BaseMessage } from '../../core/types.js'; | ||
import type { BaseMessageReply } from '../../core/message-reply.js'; | ||
import type { Event } from '../../event-log/event-log.js'; | ||
import type { DwnInterfaceName, DwnMethodName } from '../../core/message.js'; | ||
|
||
export type EventsGetDescriptor = { | ||
interface : DwnInterfaceName.Events; | ||
method: DwnMethodName.Get; | ||
watermark?: string; | ||
}; | ||
|
||
export type EventsGetMessage = BaseMessage & { | ||
descriptor: EventsGetDescriptor; | ||
}; | ||
|
||
export type EventsGetReply = BaseMessageReply & { | ||
events?: Event[]; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.