mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00
feat: new helper functions to replace regexes #807
Implemented new StringUtil helper functions: splitCommaSeparated, sanitizeUrlPart, isValidFileName. Added helper functions to HeaderUtil: matchesAuthorizationScheme, hasScheme. Added unit tests for the new helper functions. Refactored codebase to use helper functions instead of regexes if applicable.
This commit is contained in:
committed by
Joachim Van Herwegen
parent
1b7cc1ea3a
commit
283c301f08
@@ -4,6 +4,7 @@ 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 { CredentialGroup } from './Credentials';
|
||||
import type { CredentialSet } from './Credentials';
|
||||
import { CredentialsExtractor } from './CredentialsExtractor';
|
||||
@@ -19,7 +20,7 @@ export class BearerWebIdExtractor extends CredentialsExtractor {
|
||||
|
||||
public async canHandle({ headers }: HttpRequest): Promise<void> {
|
||||
const { authorization } = headers;
|
||||
if (!authorization || !/^Bearer /ui.test(authorization)) {
|
||||
if (!matchesAuthorizationScheme('Bearer', authorization)) {
|
||||
throw new NotImplementedHttpError('No Bearer Authorization header specified.');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ 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 { CredentialGroup } from './Credentials';
|
||||
import type { CredentialSet } from './Credentials';
|
||||
import { CredentialsExtractor } from './CredentialsExtractor';
|
||||
@@ -27,7 +28,7 @@ export class DPoPWebIdExtractor extends CredentialsExtractor {
|
||||
|
||||
public async canHandle({ headers }: HttpRequest): Promise<void> {
|
||||
const { authorization } = headers;
|
||||
if (!authorization || !/^DPoP /ui.test(authorization)) {
|
||||
if (!matchesAuthorizationScheme('DPoP', authorization)) {
|
||||
throw new NotImplementedHttpError('No DPoP-bound Authorization header specified.');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { getLoggerFor } from '../logging/LogUtil';
|
||||
import type { HttpRequest } from '../server/HttpRequest';
|
||||
import { NotImplementedHttpError } from '../util/errors/NotImplementedHttpError';
|
||||
import { matchesAuthorizationScheme } from '../util/HeaderUtil';
|
||||
import { CredentialGroup } from './Credentials';
|
||||
import type { CredentialSet } from './Credentials';
|
||||
import { CredentialsExtractor } from './CredentialsExtractor';
|
||||
@@ -13,7 +14,7 @@ export class UnsecureWebIdExtractor extends CredentialsExtractor {
|
||||
|
||||
public async canHandle({ headers }: HttpRequest): Promise<void> {
|
||||
const { authorization } = headers;
|
||||
if (!authorization || !/^WebID /ui.test(authorization)) {
|
||||
if (!matchesAuthorizationScheme('WebID', authorization)) {
|
||||
throw new NotImplementedHttpError('No WebID Authorization header specified.');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user