]> Piment Noir Git Repositories - e-mobility-charging-stations-simulator.git/commitdiff
chore: align utils implementation
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Thu, 14 Aug 2025 19:52:39 +0000 (21:52 +0200)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Thu, 14 Aug 2025 19:52:39 +0000 (21:52 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
src/charging-station/ui-server/AbstractUIServer.ts
src/worker/WorkerUtils.ts
ui/web/src/composables/Utils.ts

index 2795570ddae92de99c9d91e39e7b18527912bcc7..74bb3ad07724f04b0b70c6d33535b2e7c3255111 100644 (file)
@@ -19,7 +19,7 @@ import {
   type ResponsePayload,
   type UIServerConfiguration,
 } from '../../types/index.js'
-import { logger } from '../../utils/index.js'
+import { isEmpty, logger } from '../../utils/index.js'
 import { UIServiceFactory } from './ui-services/UIServiceFactory.js'
 import { getUsernameAndPasswordFromAuthorizationToken } from './UIServerUtils.js'
 
@@ -105,25 +105,23 @@ export abstract class AbstractUIServer {
     for (const uiService of this.uiServices.values()) {
       uiService.stop()
     }
+    this.uiServices.clear()
+    this.responseHandlers.clear()
     this.clearCaches()
   }
 
   protected authenticate (req: IncomingMessage, next: (err?: Error) => void): void {
-    const authorizationError = new BaseError('Unauthorized')
-    if (this.isBasicAuthEnabled()) {
-      if (!this.isValidBasicAuth(req, next)) {
-        next(authorizationError)
-      }
+    if (this.uiServerConfiguration.authentication?.enabled !== true) {
       next()
+      return
+    }
+    let ok = false
+    if (this.isBasicAuthEnabled()) {
+      ok = this.isValidBasicAuth(req, next)
     } else if (this.isProtocolBasicAuthEnabled()) {
-      if (!this.isValidProtocolBasicAuth(req, next)) {
-        next(authorizationError)
-      }
-      next()
-    } else if (this.uiServerConfiguration.authentication?.enabled === true) {
-      next(authorizationError)
+      ok = this.isValidProtocolBasicAuth(req, next)
     }
-    next()
+    next(ok ? undefined : new BaseError('Unauthorized'))
   }
 
   protected registerProtocolVersionUIService (version: ProtocolVersion): void {
@@ -172,9 +170,11 @@ export abstract class AbstractUIServer {
 
   private isValidProtocolBasicAuth (req: IncomingMessage, next: (err?: Error) => void): boolean {
     const authorizationProtocol = req.headers['sec-websocket-protocol']?.split(/,\s+/).pop()
+    if (authorizationProtocol == null || isEmpty(authorizationProtocol)) {
+      return false
+    }
     const usernameAndPassword = getUsernameAndPasswordFromAuthorizationToken(
-      // eslint-disable-next-line @typescript-eslint/no-non-null-assertion, @typescript-eslint/restrict-template-expressions
-      `${authorizationProtocol}${Array(((4 - (authorizationProtocol!.length % 4)) % 4) + 1).join(
+      `${authorizationProtocol}${Array(((4 - (authorizationProtocol.length % 4)) % 4) + 1).join(
         '='
       )}`
         .split('.')
index 958ecccc2f9d4a0d08e20785d7f37fbf471b726e..b982cf05e99d260ff4b09332b7e19cae189321e6 100644 (file)
@@ -2,9 +2,11 @@ import chalk from 'chalk'
 import { getRandomValues } from 'node:crypto'
 
 export const sleep = async (milliSeconds: number): Promise<NodeJS.Timeout> => {
-  return await new Promise<NodeJS.Timeout>(resolve =>
-    setTimeout(resolve as () => void, milliSeconds)
-  )
+  return await new Promise<NodeJS.Timeout>(resolve => {
+    const timeout = setTimeout(() => {
+      resolve(timeout)
+    }, milliSeconds)
+  })
 }
 
 export const defaultExitHandler = (code: number): void => {
index d34749e4b2f4857042940911b14b55c76c56a52e..fef017132a6b903b0676d2dd0099461af83f3f9f 100644 (file)
@@ -61,7 +61,9 @@ export const randomUUID = (): `${string}-${string}-${string}-${string}-${string}
 export const validateUUID = (
   uuid: `${string}-${string}-${string}-${string}-${string}`
 ): uuid is `${string}-${string}-${string}-${string}-${string}` => {
-  return /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-4[0-9a-fA-F]{3}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/.test(uuid)
+  return /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-4[0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$/.test(
+    uuid
+  )
 }
 
 export const useUIClient = (): UIClient => {