-
Notifications
You must be signed in to change notification settings - Fork 121
/
WebHookUnsubscriber.ts
45 lines (38 loc) · 1.95 KB
/
WebHookUnsubscriber.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import type { CredentialsExtractor } from '../../../authentication/CredentialsExtractor';
import { ResetResponseDescription } from '../../../http/output/response/ResetResponseDescription';
import type { ResponseDescription } from '../../../http/output/response/ResponseDescription';
import { getLoggerFor } from '../../../logging/LogUtil';
import { ForbiddenHttpError } from '../../../util/errors/ForbiddenHttpError';
import { NotFoundHttpError } from '../../../util/errors/NotFoundHttpError';
import type { OperationHttpHandlerInput } from '../../OperationHttpHandler';
import { OperationHttpHandler } from '../../OperationHttpHandler';
import type { NotificationChannelStorage } from '../NotificationChannelStorage';
import { isWebHook2021Channel } from './WebHookSubscription2021';
/**
* Allows clients to unsubscribe from a WebHookSubscription2021.
* Should be wrapped in a route handler that only allows `DELETE` operations.
*/
export class WebHookUnsubscriber extends OperationHttpHandler {
protected readonly logger = getLoggerFor(this);
private readonly credentialsExtractor: CredentialsExtractor;
private readonly storage: NotificationChannelStorage;
public constructor(credentialsExtractor: CredentialsExtractor, storage: NotificationChannelStorage) {
super();
this.credentialsExtractor = credentialsExtractor;
this.storage = storage;
}
public async handle({ operation, request }: OperationHttpHandlerInput): Promise<ResponseDescription> {
const id = operation.target.path;
const channel = await this.storage.get(id);
if (!channel || !isWebHook2021Channel(channel)) {
throw new NotFoundHttpError();
}
const credentials = await this.credentialsExtractor.handleSafe(request);
if (channel.webId !== credentials.agent?.webId) {
throw new ForbiddenHttpError();
}
this.logger.debug(`Deleting WebHook notification channel ${id}`);
await this.storage.delete(id);
return new ResetResponseDescription();
}
}