Commit | Line | Data |
---|---|---|
eb3abc4f | 1 | import type { IncomingMessage, Server } from 'http'; |
fe94fce0 | 2 | |
6c1761d4 | 3 | import type { ChargingStationData } from '../../types/ChargingStationWorker'; |
eb3abc4f JB |
4 | import type { UIServerConfiguration } from '../../types/ConfigurationData'; |
5 | import { | |
6 | AuthenticationType, | |
852a4c5f JB |
7 | ProcedureName, |
8 | ProtocolRequest, | |
9 | ProtocolResponse, | |
10 | ProtocolVersion, | |
11 | RequestPayload, | |
12 | ResponsePayload, | |
13 | } from '../../types/UIProtocol'; | |
db2336d9 | 14 | import type AbstractUIService from './ui-services/AbstractUIService'; |
8114d10e | 15 | |
fe94fce0 | 16 | export abstract class AbstractUIServer { |
32de5a57 | 17 | public readonly chargingStations: Map<string, ChargingStationData>; |
eb3abc4f | 18 | protected httpServer: Server; |
5e3cb728 | 19 | protected readonly uiServices: Map<ProtocolVersion, AbstractUIService>; |
fe94fce0 | 20 | |
eb3abc4f | 21 | public constructor(protected readonly uiServerConfiguration: UIServerConfiguration) { |
32de5a57 | 22 | this.chargingStations = new Map<string, ChargingStationData>(); |
fe94fce0 JB |
23 | this.uiServices = new Map<ProtocolVersion, AbstractUIService>(); |
24 | } | |
25 | ||
852a4c5f JB |
26 | public buildProtocolRequest( |
27 | id: string, | |
28 | procedureName: ProcedureName, | |
29 | requestPayload: RequestPayload | |
5e3cb728 JB |
30 | ): ProtocolRequest { |
31 | return [id, procedureName, requestPayload]; | |
852a4c5f JB |
32 | } |
33 | ||
5e3cb728 JB |
34 | public buildProtocolResponse(id: string, responsePayload: ResponsePayload): ProtocolResponse { |
35 | return [id, responsePayload]; | |
852a4c5f JB |
36 | } |
37 | ||
eb3abc4f JB |
38 | protected isBasicAuthEnabled(): boolean { |
39 | return ( | |
40 | this.uiServerConfiguration.authentication?.enabled === true && | |
41 | this.uiServerConfiguration.authentication?.type === AuthenticationType.BASIC_AUTH | |
42 | ); | |
43 | } | |
44 | ||
45 | protected isValidBasicAuth(req: IncomingMessage): boolean { | |
46 | const authorizationHeader = req.headers.authorization ?? ''; | |
47 | const authorizationToken = authorizationHeader.split(/\s+/).pop() ?? ''; | |
48 | const authentication = Buffer.from(authorizationToken, 'base64').toString(); | |
49 | const authenticationParts = authentication.split(/:/); | |
50 | const username = authenticationParts.shift(); | |
51 | const password = authenticationParts.join(':'); | |
52 | return ( | |
53 | this.uiServerConfiguration.authentication?.username === username && | |
54 | this.uiServerConfiguration.authentication?.password === password | |
55 | ); | |
56 | } | |
57 | ||
fe94fce0 JB |
58 | public abstract start(): void; |
59 | public abstract stop(): void; | |
5e3cb728 JB |
60 | public abstract sendRequest(request: ProtocolRequest): void; |
61 | public abstract sendResponse(response: ProtocolResponse): void; | |
62 | public abstract logPrefix( | |
63 | moduleName?: string, | |
64 | methodName?: string, | |
65 | prefixSuffix?: string | |
66 | ): string; | |
fe94fce0 | 67 | } |