From: Jérôme Benoit Date: Thu, 8 Sep 2022 20:07:15 +0000 (+0200) Subject: UI Server: factor out authentication code X-Git-Tag: v1.1.72~8 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=623b39b56185797f9976508571ca38619518a975;p=e-mobility-charging-stations-simulator.git UI Server: factor out authentication code Signed-off-by: Jérôme Benoit --- diff --git a/src/charging-station/ChargingStationWorkerBroadcastChannel.ts b/src/charging-station/ChargingStationWorkerBroadcastChannel.ts index a67b9423..99b16e65 100644 --- a/src/charging-station/ChargingStationWorkerBroadcastChannel.ts +++ b/src/charging-station/ChargingStationWorkerBroadcastChannel.ts @@ -62,6 +62,16 @@ export default class ChargingStationWorkerBroadcastChannel extends WorkerBroadca BroadcastChannelProcedureName.CLOSE_CONNECTION, () => this.chargingStation.closeWSConnection(), ], + [ + BroadcastChannelProcedureName.START_AUTOMATIC_TRANSACTION_GENERATOR, + (requestPayload?: BroadcastChannelRequestPayload) => + this.chargingStation.startAutomaticTransactionGenerator(requestPayload.connectorIds), + ], + [ + BroadcastChannelProcedureName.STOP_AUTOMATIC_TRANSACTION_GENERATOR, + (requestPayload?: BroadcastChannelRequestPayload) => + this.chargingStation.stopAutomaticTransactionGenerator(requestPayload.connectorIds), + ], [ BroadcastChannelProcedureName.START_TRANSACTION, async (requestPayload?: BroadcastChannelRequestPayload) => @@ -84,16 +94,6 @@ export default class ChargingStationWorkerBroadcastChannel extends WorkerBroadca ), }), ], - [ - BroadcastChannelProcedureName.START_AUTOMATIC_TRANSACTION_GENERATOR, - (requestPayload?: BroadcastChannelRequestPayload) => - this.chargingStation.startAutomaticTransactionGenerator(requestPayload.connectorIds), - ], - [ - BroadcastChannelProcedureName.STOP_AUTOMATIC_TRANSACTION_GENERATOR, - (requestPayload?: BroadcastChannelRequestPayload) => - this.chargingStation.stopAutomaticTransactionGenerator(requestPayload.connectorIds), - ], [ BroadcastChannelProcedureName.AUTHORIZE, async (requestPayload?: BroadcastChannelRequestPayload) => @@ -190,8 +190,9 @@ export default class ChargingStationWorkerBroadcastChannel extends WorkerBroadca errorStack: (error as Error).stack, errorDetails: (error as OCPPError).details, }; + } finally { + this.sendResponse([uuid, responsePayload]); } - this.sendResponse([uuid, responsePayload]); } private messageErrorHandler(messageEvent: MessageEvent): void { diff --git a/src/charging-station/ui-server/AbstractUIServer.ts b/src/charging-station/ui-server/AbstractUIServer.ts index 75f6e8a8..dad1e0cb 100644 --- a/src/charging-station/ui-server/AbstractUIServer.ts +++ b/src/charging-station/ui-server/AbstractUIServer.ts @@ -71,6 +71,16 @@ export abstract class AbstractUIServer { ); } + protected authenticate(req: IncomingMessage, next: (err?: Error) => void): void { + if (this.isBasicAuthEnabled() === true) { + if (this.isValidBasicAuth(req) === false) { + next(new Error('Unauthorized')); + } + next(); + } + next(); + } + public abstract start(): void; public abstract sendRequest(request: ProtocolRequest): void; public abstract sendResponse(response: ProtocolResponse): void; diff --git a/src/charging-station/ui-server/UIHttpServer.ts b/src/charging-station/ui-server/UIHttpServer.ts index d1615dbb..6ae03d5d 100644 --- a/src/charging-station/ui-server/UIHttpServer.ts +++ b/src/charging-station/ui-server/UIHttpServer.ts @@ -61,13 +61,16 @@ export default class UIHttpServer extends AbstractUIServer { } private requestListener(req: IncomingMessage, res: ServerResponse): void { - if (this.authenticate(req) === false) { - res.setHeader('Content-Type', 'text/plain'); - res.setHeader('WWW-Authenticate', 'Basic realm=users'); - res.writeHead(StatusCodes.UNAUTHORIZED); - res.end(`${StatusCodes.UNAUTHORIZED} Unauthorized`); - return; - } + this.authenticate(req, (err) => { + if (err) { + res.setHeader('Content-Type', 'text/plain'); + res.setHeader('WWW-Authenticate', 'Basic realm=users'); + res.writeHead(StatusCodes.UNAUTHORIZED); + res.end(`${StatusCodes.UNAUTHORIZED} Unauthorized`); + req.destroy(); + res.destroy(); + } + }); // Expected request URL pathname: /ui/:version/:procedureName const [protocol, version, procedureName] = req.url?.split('/').slice(1) as [ Protocol, @@ -116,16 +119,6 @@ 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 ce773db3..c067fb36 100644 --- a/src/charging-station/ui-server/UIWebSocketServer.ts +++ b/src/charging-station/ui-server/UIWebSocketServer.ts @@ -127,18 +127,6 @@ export default class UIWebSocketServer extends AbstractUIServer { } } - private authenticate(req: IncomingMessage, next: (err?: Error) => void): void { - if (this.isBasicAuthEnabled() === true) { - if (this.isValidBasicAuth(req) === false) { - next(new Error('Unauthorized')); - } else { - next(); - } - } else { - next(); - } - } - private validateRawDataRequest(rawData: RawData): ProtocolRequest | false { // logger.debug( // `${this.logPrefix( diff --git a/src/charging-station/ui-server/ui-services/AbstractUIService.ts b/src/charging-station/ui-server/ui-services/AbstractUIService.ts index 356e908c..6d285926 100644 --- a/src/charging-station/ui-server/ui-services/AbstractUIService.ts +++ b/src/charging-station/ui-server/ui-services/AbstractUIService.ts @@ -73,10 +73,11 @@ export default abstract class AbstractUIService { errorStack: (error as Error).stack, errorDetails: (error as OCPPError).details, }; - } - // Send response for payload not forwarded to broadcast channel - if (responsePayload !== undefined) { - this.sendResponse(messageId ?? 'error', responsePayload); + } finally { + // Send response for payload not forwarded to broadcast channel + if (responsePayload !== undefined) { + this.sendResponse(messageId ?? 'error', responsePayload); + } } } diff --git a/src/charging-station/ui-server/ui-services/UIService001.ts b/src/charging-station/ui-server/ui-services/UIService001.ts index 82d0a953..28286d3b 100644 --- a/src/charging-station/ui-server/ui-services/UIService001.ts +++ b/src/charging-station/ui-server/ui-services/UIService001.ts @@ -47,19 +47,19 @@ export default class UIService001 extends AbstractUIService { this.handleProtocolRequest.bind(this) as ProtocolRequestHandler ); this.requestHandlers.set( - ProcedureName.START_TRANSACTION, + ProcedureName.START_AUTOMATIC_TRANSACTION_GENERATOR, this.handleProtocolRequest.bind(this) as ProtocolRequestHandler ); this.requestHandlers.set( - ProcedureName.STOP_TRANSACTION, + ProcedureName.STOP_AUTOMATIC_TRANSACTION_GENERATOR, this.handleProtocolRequest.bind(this) as ProtocolRequestHandler ); this.requestHandlers.set( - ProcedureName.START_AUTOMATIC_TRANSACTION_GENERATOR, + ProcedureName.START_TRANSACTION, this.handleProtocolRequest.bind(this) as ProtocolRequestHandler ); this.requestHandlers.set( - ProcedureName.STOP_AUTOMATIC_TRANSACTION_GENERATOR, + ProcedureName.STOP_TRANSACTION, this.handleProtocolRequest.bind(this) as ProtocolRequestHandler ); this.requestHandlers.set(