f2429ae1746b739f3429963686ef3059bfd2f453
[e-mobility-charging-stations-simulator.git] / src / charging-station / ui-server / AbstractUIServer.ts
1 import { type IncomingMessage, Server, type ServerResponse } from 'node:http';
2
3 import type { WebSocket } from 'ws';
4
5 import type { AbstractUIService } from './ui-services/AbstractUIService';
6 import { UIServiceFactory } from './ui-services/UIServiceFactory';
7 import {
8 AuthenticationType,
9 type ChargingStationData,
10 type ProcedureName,
11 type ProtocolRequest,
12 type ProtocolResponse,
13 type ProtocolVersion,
14 type RequestPayload,
15 type ResponsePayload,
16 type UIServerConfiguration,
17 } from '../../types';
18
19 export abstract class AbstractUIServer {
20 public readonly chargingStations: Map<string, ChargingStationData>;
21 protected readonly httpServer: Server;
22 protected readonly responseHandlers: Map<string, ServerResponse | WebSocket>;
23 protected readonly uiServices: Map<ProtocolVersion, AbstractUIService>;
24
25 public constructor(protected readonly uiServerConfiguration: UIServerConfiguration) {
26 this.chargingStations = new Map<string, ChargingStationData>();
27 this.httpServer = new Server();
28 this.responseHandlers = new Map<string, ServerResponse | WebSocket>();
29 this.uiServices = new Map<ProtocolVersion, AbstractUIService>();
30 }
31
32 public buildProtocolRequest(
33 id: string,
34 procedureName: ProcedureName,
35 requestPayload: RequestPayload
36 ): ProtocolRequest {
37 return [id, procedureName, requestPayload];
38 }
39
40 public buildProtocolResponse(id: string, responsePayload: ResponsePayload): ProtocolResponse {
41 return [id, responsePayload];
42 }
43
44 public stop(): void {
45 this.chargingStations.clear();
46 }
47
48 protected startHttpServer(): void {
49 if (this.httpServer.listening === false) {
50 this.httpServer.listen(this.uiServerConfiguration.options);
51 }
52 }
53
54 protected registerProtocolVersionUIService(version: ProtocolVersion): void {
55 if (this.uiServices.has(version) === false) {
56 this.uiServices.set(version, UIServiceFactory.getUIServiceImplementation(version, this));
57 }
58 }
59
60 protected authenticate(req: IncomingMessage, next: (err?: Error) => void): void {
61 if (this.isBasicAuthEnabled() === true) {
62 if (this.isValidBasicAuth(req) === false) {
63 next(new Error('Unauthorized'));
64 }
65 next();
66 }
67 next();
68 }
69
70 private isBasicAuthEnabled(): boolean {
71 return (
72 this.uiServerConfiguration.authentication?.enabled === true &&
73 this.uiServerConfiguration.authentication?.type === AuthenticationType.BASIC_AUTH
74 );
75 }
76
77 private isValidBasicAuth(req: IncomingMessage): boolean {
78 const authorizationHeader = req.headers.authorization ?? '';
79 const authorizationToken = authorizationHeader.split(/\s+/).pop() ?? '';
80 const authentication = Buffer.from(authorizationToken, 'base64').toString();
81 const authenticationParts = authentication.split(/:/);
82 const username = authenticationParts.shift();
83 const password = authenticationParts.join(':');
84 return (
85 this.uiServerConfiguration.authentication?.username === username &&
86 this.uiServerConfiguration.authentication?.password === password
87 );
88 }
89
90 public abstract start(): void;
91 public abstract sendRequest(request: ProtocolRequest): void;
92 public abstract sendResponse(response: ProtocolResponse): void;
93 public abstract logPrefix(
94 moduleName?: string,
95 methodName?: string,
96 prefixSuffix?: string
97 ): string;
98 }