From 687de086719bd24565d7652c8e64bc99e56d1246 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sun, 4 Sep 2022 14:32:31 +0200 Subject: [PATCH] UI Server: factor out authentication logic MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/charging-station/ui-server/UIHttpServer.ts | 12 +++++++++++- src/charging-station/ui-server/UIWebSocketServer.ts | 6 +++--- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/charging-station/ui-server/UIHttpServer.ts b/src/charging-station/ui-server/UIHttpServer.ts index 280f1d38..289eda84 100644 --- a/src/charging-station/ui-server/UIHttpServer.ts +++ b/src/charging-station/ui-server/UIHttpServer.ts @@ -71,7 +71,7 @@ export default class UIHttpServer extends AbstractUIServer { } private requestListener(req: IncomingMessage, res: ServerResponse): void { - if (this.isBasicAuthEnabled() === true && this.isValidBasicAuth(req) === false) { + if (this.authenticate(req) === false) { res.setHeader('Content-Type', 'text/plain'); res.setHeader('WWW-Authenticate', 'Basic realm=users'); res.writeHead(StatusCodes.UNAUTHORIZED); @@ -128,6 +128,16 @@ export default class UIHttpServer extends AbstractUIServer { } } + private authenticate(req: IncomingMessage): boolean { + if (this.isBasicAuthEnabled() === true) { + if (this.isValidBasicAuth(req) === true) { + return true; + } + return false; + } + return true; + } + private responseStatusToStatusCode(status: ResponseStatus): StatusCodes { switch (status) { case ResponseStatus.SUCCESS: diff --git a/src/charging-station/ui-server/UIWebSocketServer.ts b/src/charging-station/ui-server/UIWebSocketServer.ts index c706cdd9..f7a0379d 100644 --- a/src/charging-station/ui-server/UIWebSocketServer.ts +++ b/src/charging-station/ui-server/UIWebSocketServer.ts @@ -116,15 +116,15 @@ export default class UIWebSocketServer extends AbstractUIServer { } } - private authenticate(req: IncomingMessage, next: (err: Error) => void): void { + private authenticate(req: IncomingMessage, next: (err?: Error) => void): void { if (this.isBasicAuthEnabled() === true) { if (this.isValidBasicAuth(req) === false) { next(new Error('Unauthorized')); } else { - next(undefined); + next(); } } else { - next(undefined); + next(); } } -- 2.34.1