chore(deps): Update ts-dpop to v0.2.7.

This commit is contained in:
Matthieu Bosquet
2020-12-19 17:26:24 +00:00
committed by GitHub
parent 731b44d5b9
commit 6077cc1f4c
5 changed files with 105 additions and 85 deletions

View File

@@ -1,4 +1,5 @@
import { verify } from 'ts-dpop';
import type { RequestMethod, VerifySolidIdentityFunction } from 'ts-dpop';
import { createSolidIdentityVerifier } from 'ts-dpop';
import type { TargetExtractor } from '../ldp/http/TargetExtractor';
import { getLoggerFor } from '../logging/LogUtil';
import type { HttpRequest } from '../server/HttpRequest';
@@ -13,31 +14,40 @@ import { CredentialsExtractor } from './CredentialsExtractor';
export class DPoPWebIdExtractor extends CredentialsExtractor {
protected readonly logger = getLoggerFor(this);
private readonly targetExtractor: TargetExtractor;
private readonly verify: VerifySolidIdentityFunction;
public constructor(targetExtractor: TargetExtractor) {
super();
this.targetExtractor = targetExtractor;
this.verify = createSolidIdentityVerifier();
}
public async canHandle({ headers }: HttpRequest): Promise<void> {
const { authorization } = headers;
if (!authorization || !authorization.startsWith('DPoP ')) {
throw new NotImplementedHttpError('No DPoP Authorization header specified.');
throw new NotImplementedHttpError('No DPoP-bound Authorization header specified.');
}
}
public async handle(request: HttpRequest): Promise<Credentials> {
const { headers: { authorization, dpop }, method } = request;
if (!dpop) {
throw new BadRequestHttpError('No DPoP token specified.');
throw new BadRequestHttpError('No DPoP header specified.');
}
const resource = await this.targetExtractor.handleSafe(request);
try {
const webId = await verify(authorization as string, dpop as string, method as any, resource.path);
this.logger.info(`Verified WebID via DPoP token: ${webId}`);
const { webid: webId } = await this.verify(
authorization as string,
dpop as string,
method as RequestMethod,
resource.path,
);
this.logger.info(`Verified WebID via DPoP-bound access token: ${webId}`);
return { webId };
} catch (error: unknown) {
const message = `Error verifying WebID via DPoP token: ${(error as Error).message}`;
const message = `Error verifying WebID via DPoP-bound access token: ${(error as Error).message}`;
this.logger.warn(message);
throw new BadRequestHttpError(message);
}