From: Jérôme Benoit Date: Sun, 4 Sep 2022 12:32:31 +0000 (+0200) Subject: UI Server: factor out authentication logic X-Git-Tag: v1.1.70~9 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=687de086719bd24565d7652c8e64bc99e56d1246;p=e-mobility-charging-stations-simulator.git UI Server: factor out authentication logic Signed-off-by: Jérôme Benoit --- 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(); } }