mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00
46 lines
1.9 KiB
TypeScript
46 lines
1.9 KiB
TypeScript
import type { SolidTokenVerifierFunction } from '@solid/access-token-verifier';
|
|
import { createSolidTokenVerifier } from '@solid/access-token-verifier';
|
|
import { getLoggerFor } from '../logging/LogUtil';
|
|
import type { HttpRequest } from '../server/HttpRequest';
|
|
import { BadRequestHttpError } from '../util/errors/BadRequestHttpError';
|
|
import { NotImplementedHttpError } from '../util/errors/NotImplementedHttpError';
|
|
import { matchesAuthorizationScheme } from '../util/HeaderUtil';
|
|
import type { Credentials } from './Credentials';
|
|
import { CredentialsExtractor } from './CredentialsExtractor';
|
|
|
|
export class BearerWebIdExtractor extends CredentialsExtractor {
|
|
protected readonly logger = getLoggerFor(this);
|
|
private readonly verify: SolidTokenVerifierFunction;
|
|
|
|
public constructor() {
|
|
super();
|
|
this.verify = createSolidTokenVerifier();
|
|
}
|
|
|
|
public async canHandle({ headers }: HttpRequest): Promise<void> {
|
|
const { authorization } = headers;
|
|
if (!matchesAuthorizationScheme('Bearer', authorization)) {
|
|
throw new NotImplementedHttpError('No Bearer Authorization header specified.');
|
|
}
|
|
}
|
|
|
|
public async handle(request: HttpRequest): Promise<Credentials> {
|
|
const { headers: { authorization }} = request;
|
|
|
|
try {
|
|
const { webid: webId, client_id: clientId, iss: issuer } = await this.verify(authorization!);
|
|
this.logger.info(`Verified credentials via Bearer access token. WebID: ${webId
|
|
}, client ID: ${clientId}, issuer: ${issuer}`);
|
|
const credentials: Credentials = { agent: { webId }, issuer: { url: issuer }};
|
|
if (clientId) {
|
|
credentials.client = { clientId };
|
|
}
|
|
return credentials;
|
|
} catch (error: unknown) {
|
|
const message = `Error verifying WebID via Bearer access token: ${(error as Error).message}`;
|
|
this.logger.warn(message);
|
|
throw new BadRequestHttpError(message, { cause: error });
|
|
}
|
|
}
|
|
}
|