enhancement(auth): better error message handling

Signed-off-by: Erik Michelson <github@erik.michelson.eu>
This commit is contained in:
Erik Michelson 2023-03-25 18:58:48 +01:00
parent 8e57188ab5
commit ca9836d691
37 changed files with 199 additions and 207 deletions

View file

@ -3,14 +3,10 @@
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
import {
BadRequestException,
CanActivate,
Inject,
Injectable,
} from '@nestjs/common';
import { CanActivate, Inject, Injectable } from '@nestjs/common';
import authConfiguration, { AuthConfig } from '../../config/auth.config';
import { FeatureDisabledError } from '../../errors/errors';
import { ConsoleLoggerService } from '../../logger/console-logger.service';
@Injectable()
@ -26,7 +22,7 @@ export class LoginEnabledGuard implements CanActivate {
canActivate(): boolean {
if (!this.authConfig.local.enableLogin) {
this.logger.debug('Local auth is disabled.', 'canActivate');
throw new BadRequestException('Local auth is disabled.');
throw new FeatureDisabledError('Local auth is disabled.');
}
return true;
}

View file

@ -6,7 +6,7 @@
import { CanActivate, Inject, Injectable } from '@nestjs/common';
import authConfiguration, { AuthConfig } from '../../config/auth.config';
import { RegistrationDisabledError } from '../../errors/errors';
import { FeatureDisabledError } from '../../errors/errors';
import { ConsoleLoggerService } from '../../logger/console-logger.service';
@Injectable()
@ -22,7 +22,7 @@ export class RegistrationEnabledGuard implements CanActivate {
canActivate(): boolean {
if (!this.authConfig.local.enableRegister) {
this.logger.debug('User registration is disabled.', 'canActivate');
throw new RegistrationDisabledError();
throw new FeatureDisabledError('User registration is disabled');
}
return true;
}

View file

@ -77,7 +77,7 @@ const mapOfHedgeDocErrorsToHttpErrors: Map<string, HttpExceptionConstructor> =
(object): HttpException => new PayloadTooLargeException(object),
],
[
'RegistrationDisabledError',
'FeatureDisabledError',
(object): HttpException => new ForbiddenException(object),
],
]);

View file

@ -60,6 +60,6 @@ export class MaximumDocumentLengthExceededError extends Error {
name = 'MaximumDocumentLengthExceededError';
}
export class RegistrationDisabledError extends Error {
name = 'RegistrationDisabledError';
export class FeatureDisabledError extends Error {
name = 'FeatureDisabledError';
}

View file

@ -25,7 +25,7 @@ describe('Auth', () => {
let displayName: string;
let password: string;
beforeAll(async () => {
beforeEach(async () => {
testSetup = await TestSetupBuilder.create().build();
await testSetup.app.init();
@ -34,7 +34,7 @@ describe('Auth', () => {
password = 'test_password';
});
afterAll(async () => {
afterEach(async () => {
// Yes, this is a bad hack, but there is a race somewhere and I have
// no idea how to fix it.
await new Promise((resolve) => {
@ -193,7 +193,7 @@ describe('Auth', () => {
.set('Content-Type', 'application/json')
.set('Cookie', cookie)
.send(JSON.stringify(changePasswordDto))
.expect(400);
.expect(403);
// enable login again
testSetup.configService.get('authConfig').local.enableLogin = true;
// new password doesn't work for login