From d95db60fe151d0e64e469fda17ed7eb68f2f18e0 Mon Sep 17 00:00:00 2001 From: Joachim Van Herwegen Date: Tue, 27 Jul 2021 10:20:32 +0200 Subject: [PATCH] feat: Create DenyAllAuthorizer --- src/authorization/DenyAllAuthorizer.ts | 11 +++++++++++ src/index.ts | 1 + test/unit/authorization/DenyAllAuthorizer.test.ts | 14 ++++++++++++++ 3 files changed, 26 insertions(+) create mode 100644 src/authorization/DenyAllAuthorizer.ts create mode 100644 test/unit/authorization/DenyAllAuthorizer.test.ts diff --git a/src/authorization/DenyAllAuthorizer.ts b/src/authorization/DenyAllAuthorizer.ts new file mode 100644 index 000000000..93cb26417 --- /dev/null +++ b/src/authorization/DenyAllAuthorizer.ts @@ -0,0 +1,11 @@ +import { ForbiddenHttpError } from '../util/errors/ForbiddenHttpError'; +import { Authorizer } from './Authorizer'; + +/** + * An authorizer that rejects all requests. + */ +export class DenyAllAuthorizer extends Authorizer { + public async handle(): Promise { + throw new ForbiddenHttpError(); + } +} diff --git a/src/index.ts b/src/index.ts index 80c039132..c0cd695ad 100644 --- a/src/index.ts +++ b/src/index.ts @@ -12,6 +12,7 @@ export * from './authorization/AllowAllAuthorizer'; export * from './authorization/Authorization'; export * from './authorization/Authorizer'; export * from './authorization/AuxiliaryAuthorizer'; +export * from './authorization/DenyAllAuthorizer'; export * from './authorization/WebAclAuthorization'; export * from './authorization/WebAclAuthorizer'; diff --git a/test/unit/authorization/DenyAllAuthorizer.test.ts b/test/unit/authorization/DenyAllAuthorizer.test.ts new file mode 100644 index 000000000..730d7095e --- /dev/null +++ b/test/unit/authorization/DenyAllAuthorizer.test.ts @@ -0,0 +1,14 @@ +import { DenyAllAuthorizer } from '../../../src/authorization/DenyAllAuthorizer'; +import { ForbiddenHttpError } from '../../../src/util/errors/ForbiddenHttpError'; + +describe('A DenyAllAuthorizer', (): void => { + const authorizer = new DenyAllAuthorizer(); + + it('can handle all requests.', async(): Promise => { + await expect(authorizer.canHandle({} as any)).resolves.toBeUndefined(); + }); + + it('rejects all requests.', async(): Promise => { + await expect(authorizer.handle()).rejects.toThrow(ForbiddenHttpError); + }); +});