From cd3f3e699a565003030b859ce6667a8a8238d02e Mon Sep 17 00:00:00 2001 From: Matt Magoffin Date: Tue, 2 Jul 2024 07:51:10 +1200 Subject: [PATCH] Allow basic auth username to be explicitly defined, defaulting to charge point ID otherwise. --- README.md | 1 + index_16.ts | 1 + index_201.ts | 1 + src/vcp.ts | 3 ++- 4 files changed, 5 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index a3cce0b..d649d99 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,7 @@ Configure env variables: ``` WS_URL - websocket endpoint CP_ID - ID of this VCP +CP_USERNAME - if used for OCPP Authentication, defaults to CP_ID if only PASSWORD defined PASSWORD - if used for OCPP Authentication, otherwise can be left blank ``` diff --git a/index_16.ts b/index_16.ts index 867a508..ab26cc2 100644 --- a/index_16.ts +++ b/index_16.ts @@ -8,6 +8,7 @@ const vcp = new VCP({ endpoint: process.env["WS_URL"] ?? "ws://localhost:3000", chargePointId: process.env["CP_ID"] ?? "123456", ocppVersion: OcppVersion.OCPP_1_6, + basicAuthUsername: process.env["CP_USERNAME"] ?? undefined, basicAuthPassword: process.env["PASSWORD"] ?? undefined, adminWsPort: parseInt( process.env["ADMIN_PORT"] ?? "9999" diff --git a/index_201.ts b/index_201.ts index 500221b..f12140b 100644 --- a/index_201.ts +++ b/index_201.ts @@ -8,6 +8,7 @@ const vcp = new VCP({ endpoint: process.env["WS_URL"] ?? "ws://localhost:3000", chargePointId: process.env["CP_ID"] ?? "123456", ocppVersion: OcppVersion.OCPP_2_0_1, + basicAuthUsername: process.env["CP_USERNAME"] ?? undefined, basicAuthPassword: process.env["PASSWORD"] ?? undefined, adminWsPort: parseInt(process.env["ADMIN_WS_PORT"] ?? "9999"), }); diff --git a/src/vcp.ts b/src/vcp.ts index e55981f..b392e22 100644 --- a/src/vcp.ts +++ b/src/vcp.ts @@ -19,6 +19,7 @@ interface VCPOptions { ocppVersion: OcppVersion; endpoint: string; chargePointId: string; + basicAuthUsername?: string; basicAuthPassword?: string; adminWsPort?: number; } @@ -54,7 +55,7 @@ export class VCP { this.ws = new WebSocket(websocketUrl, [protocol], { rejectUnauthorized: false, auth: this.vcpOptions.basicAuthPassword - ? `${this.vcpOptions.chargePointId}:${this.vcpOptions.basicAuthPassword}` + ? `${this.vcpOptions.basicAuthUsername || this.vcpOptions.chargePointId}:${this.vcpOptions.basicAuthPassword}` : undefined, followRedirects: true, });