2023-04-24 11:00:54 +02:00

39 lines
1.7 KiB
TypeScript

import { Parser } from 'n3';
import { OkResponseDescription } from '../../../http/output/response/OkResponseDescription';
import type { ResponseDescription } from '../../../http/output/response/ResponseDescription';
import { BasicRepresentation } from '../../../http/representation/BasicRepresentation';
import { TEXT_TURTLE } from '../../../util/ContentTypes';
import { createErrorMessage } from '../../../util/errors/ErrorUtil';
import { trimTrailingSlashes } from '../../../util/PathUtil';
import type { OperationHttpHandlerInput } from '../../OperationHttpHandler';
import { OperationHttpHandler } from '../../OperationHttpHandler';
/**
* Generates a fixed WebID that we use to identify the server for notifications sent using a WebHookChannel2023.
* This is used in tandem with the tokens generated by the {@link WebHookEmitter}.
* This is a minimal WebID with only the `solid:oidcIssuer` triple.
*/
export class WebHookWebId extends OperationHttpHandler {
private readonly turtle: string;
public constructor(baseUrl: string) {
super();
this.turtle = `@prefix solid: <http://www.w3.org/ns/solid/terms#>.
<> solid:oidcIssuer <${trimTrailingSlashes(baseUrl)}>.`;
// This will throw an error if something is wrong with the issuer URL
const parser = new Parser();
try {
parser.parse(this.turtle);
} catch (error: unknown) {
throw new Error(`Invalid issuer URL: ${createErrorMessage(error)}`);
}
}
public async handle(input: OperationHttpHandlerInput): Promise<ResponseDescription> {
const representation = new BasicRepresentation(this.turtle, input.operation.target, TEXT_TURTLE);
return new OkResponseDescription(representation.metadata, representation.data);
}
}