X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2Fui-server%2FUIServerUtils.ts;h=cfe4fd2f6198ee6ae544d0b46b786e9f496f69af;hb=8fa6f565c86b72f3d6f040d283da83740a51542e;hp=b590594989e6d410e5e7d5c0621204ec774a54b7;hpb=60a743910478b70e39dcefa5e1b752ec8a93880e;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/ui-server/UIServerUtils.ts b/src/charging-station/ui-server/UIServerUtils.ts index b5905949..cfe4fd2f 100644 --- a/src/charging-station/ui-server/UIServerUtils.ts +++ b/src/charging-station/ui-server/UIServerUtils.ts @@ -1,60 +1,64 @@ -import type { IncomingMessage } from 'http'; +import type { IncomingMessage } from 'node:http' -import { Protocol, ProtocolVersion } from '../../types'; -import { Utils, logger } from '../../utils'; +import { BaseError } from '../../exception/index.js' +import { Protocol, ProtocolVersion } from '../../types/index.js' +import { logger, logPrefix } from '../../utils/index.js' -export class UIServerUtils { - private constructor() { - // This is intentional +export const getUsernameAndPasswordFromAuthorizationToken = ( + authorizationToken: string, + next: (err?: Error) => void +): [string, string] => { + if ( + !/^([0-9a-zA-Z+/]{4})*(([0-9a-zA-Z+/]{2}==)|([0-9a-zA-Z+/]{3}=))?$/.test(authorizationToken) + ) { + next(new BaseError('Invalid basic authentication token format')) } + const authentication = Buffer.from(authorizationToken, 'base64').toString() + const authenticationParts = authentication.split(/:/) + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + return [authenticationParts.shift()!, authenticationParts.join(':')] +} - public static handleProtocols = ( - protocols: Set, - // eslint-disable-next-line @typescript-eslint/no-unused-vars - request: IncomingMessage - ): string | false => { - let protocol: Protocol; - let version: ProtocolVersion; - if (protocols.size === 0) { - return false; - } - for (const fullProtocol of protocols) { - if (UIServerUtils.isProtocolAndVersionSupported(fullProtocol) === true) { - return fullProtocol; - } +export const handleProtocols = ( + protocols: Set, + _request: IncomingMessage +): string | false => { + let protocol: Protocol | undefined + let version: ProtocolVersion | undefined + if (protocols.size === 0) { + return false + } + for (const fullProtocol of protocols) { + if (isProtocolAndVersionSupported(fullProtocol)) { + return fullProtocol } - logger.error( - `${Utils.logPrefix( - ' UI WebSocket Server |' - )} Unsupported protocol: ${protocol} or protocol version: ${version}` - ); - return false; - }; + } + logger.error( + `${logPrefix( + ' UI WebSocket Server |' + )} Unsupported protocol: '${protocol}' or protocol version: '${version}'` + ) + return false +} - public static isProtocolAndVersionSupported = (protocolStr: string): boolean => { - const [protocol, version] = UIServerUtils.getProtocolAndVersion(protocolStr); - return ( - Object.values(Protocol).includes(protocol) === true && - Object.values(ProtocolVersion).includes(version) === true - ); - }; +export const isProtocolAndVersionSupported = (protocolStr: string): boolean => { + const [protocol, version] = getProtocolAndVersion(protocolStr) + return ( + Object.values(Protocol).includes(protocol) && Object.values(ProtocolVersion).includes(version) + ) +} - public static getProtocolAndVersion = (protocolStr: string): [Protocol, ProtocolVersion] => { - const protocolIndex = protocolStr.indexOf(Protocol.UI); - const protocol = protocolStr.substring( - protocolIndex, - protocolIndex + Protocol.UI.length - ) as Protocol; - const version = protocolStr.substring(protocolIndex + Protocol.UI.length) as ProtocolVersion; - return [protocol, version]; - }; +export const getProtocolAndVersion = (protocolStr: string): [Protocol, ProtocolVersion] => { + const protocolIndex = protocolStr.indexOf(Protocol.UI) + const protocol = protocolStr.substring( + protocolIndex, + protocolIndex + Protocol.UI.length + ) as Protocol + const version = protocolStr.substring(protocolIndex + Protocol.UI.length) as ProtocolVersion + return [protocol, version] +} - public static isLoopback(address: string): boolean { - const isLoopbackRegExp = new RegExp( - // eslint-disable-next-line no-useless-escape - /^localhost$|^127(?:\.\d+){0,2}\.\d+$|^(?:0*\:)*?:?0*1$/, - 'i' - ); - return isLoopbackRegExp.test(address); - } +export const isLoopback = (address: string): boolean => { + // eslint-disable-next-line no-useless-escape + return /^localhost$|^127(?:\.\d+){0,2}\.\d+$|^(?:0*\:)*?:?0*1$/i.test(address) }