]> Piment Noir Git Repositories - e-mobility-charging-stations-simulator.git/commitdiff
refactor(ocpp): move isIdTagAuthorized into OCPPServiceOperations
authorJérôme Benoit <jerome.benoit@sap.com>
Wed, 1 Apr 2026 14:23:42 +0000 (16:23 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Wed, 1 Apr 2026 14:23:42 +0000 (16:23 +0200)
The circular dependency that motivated the separate IdTagAuthorization
file no longer exists after auth module refactoring. Move the function
to OCPPServiceOperations alongside other cross-stack transaction
helpers and delete the standalone file.

src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts
src/charging-station/ocpp/IdTagAuthorization.ts [deleted file]
src/charging-station/ocpp/OCPPServiceOperations.ts
src/charging-station/ocpp/index.ts
tests/charging-station/ocpp/IdTagAuthorization.test.ts

index eeb7655cdd3f0ea784b4dd49790171958a84855b..c387da4b07ccaeef74a7e9c08dd31f83505a29a2 100644 (file)
@@ -116,9 +116,9 @@ import {
   truncateId,
 } from '../../../utils/index.js'
 import { AuthContext } from '../auth/index.js'
-import { isIdTagAuthorized } from '../IdTagAuthorization.js'
 import { OCPPConstants } from '../OCPPConstants.js'
 import { OCPPIncomingRequestService } from '../OCPPIncomingRequestService.js'
+import { isIdTagAuthorized } from '../OCPPServiceOperations.js'
 import {
   buildMeterValue,
   createPayloadValidatorMap,
diff --git a/src/charging-station/ocpp/IdTagAuthorization.ts b/src/charging-station/ocpp/IdTagAuthorization.ts
deleted file mode 100644 (file)
index 8520c45..0000000
+++ /dev/null
@@ -1,64 +0,0 @@
-import type { ChargingStation } from '../../charging-station/index.js'
-
-import { logger, truncateId } from '../../utils/index.js'
-import {
-  AuthContext,
-  AuthenticationMethod,
-  AuthorizationStatus,
-  IdentifierType,
-  OCPPAuthServiceFactory,
-} from './auth/index.js'
-
-export const isIdTagAuthorized = async (
-  chargingStation: ChargingStation,
-  connectorId: number,
-  idTag: string,
-  context?: AuthContext
-): Promise<boolean> => {
-  try {
-    logger.debug(
-      `${chargingStation.logPrefix()} Authorizing idTag '${truncateId(idTag)}' on connector ${connectorId.toString()}`
-    )
-
-    const authService = OCPPAuthServiceFactory.getInstance(chargingStation)
-
-    const authResult = await authService.authorize({
-      allowOffline: false,
-      connectorId,
-      context: context ?? AuthContext.TRANSACTION_START,
-      identifier: {
-        type: IdentifierType.ID_TAG,
-        value: idTag,
-      },
-      timestamp: new Date(),
-    })
-
-    logger.debug(
-      `${chargingStation.logPrefix()} Authorization result for idTag '${truncateId(idTag)}': ${authResult.status} using ${authResult.method} method`
-    )
-
-    if (authResult.status === AuthorizationStatus.ACCEPTED) {
-      const connectorStatus = chargingStation.getConnectorStatus(connectorId)
-      if (connectorStatus != null) {
-        switch (authResult.method) {
-          case AuthenticationMethod.CACHE:
-          case AuthenticationMethod.LOCAL_LIST:
-          case AuthenticationMethod.OFFLINE_FALLBACK:
-            connectorStatus.localAuthorizeIdTag = idTag
-            connectorStatus.idTagLocalAuthorized = true
-            break
-          case AuthenticationMethod.CERTIFICATE_BASED:
-          case AuthenticationMethod.NONE:
-          case AuthenticationMethod.REMOTE_AUTHORIZATION:
-            break
-        }
-      }
-      return true
-    }
-
-    return false
-  } catch (error) {
-    logger.error(`${chargingStation.logPrefix()} Authorization failed`, error)
-    return false
-  }
-}
index 57db83dfcfa088152acf3ea96bc634c6ff0d1558..0607932c396bada7ef417470ca541f5479d4e66e 100644 (file)
@@ -15,9 +15,16 @@ import {
   type StartTransactionResult,
   type StopTransactionResult,
 } from '../../types/index.js'
-import { generateUUID, logger } from '../../utils/index.js'
+import { generateUUID, logger, truncateId } from '../../utils/index.js'
 import { OCPP16ServiceUtils } from './1.6/OCPP16ServiceUtils.js'
 import { OCPP20ServiceUtils } from './2.0/OCPP20ServiceUtils.js'
+import {
+  AuthContext,
+  AuthenticationMethod,
+  AuthorizationStatus as AuthStatus,
+  IdentifierType,
+  OCPPAuthServiceFactory,
+} from './auth/index.js'
 import { mapStopReasonToOCPP20 } from './OCPPServiceUtils.js'
 
 /**
@@ -267,3 +274,57 @@ export const buildBootNotificationRequest = (
       return undefined
   }
 }
+
+export const isIdTagAuthorized = async (
+  chargingStation: ChargingStation,
+  connectorId: number,
+  idTag: string,
+  context?: AuthContext
+): Promise<boolean> => {
+  try {
+    logger.debug(
+      `${chargingStation.logPrefix()} Authorizing idTag '${truncateId(idTag)}' on connector ${connectorId.toString()}`
+    )
+
+    const authService = OCPPAuthServiceFactory.getInstance(chargingStation)
+
+    const authResult = await authService.authorize({
+      allowOffline: false,
+      connectorId,
+      context: context ?? AuthContext.TRANSACTION_START,
+      identifier: {
+        type: IdentifierType.ID_TAG,
+        value: idTag,
+      },
+      timestamp: new Date(),
+    })
+
+    logger.debug(
+      `${chargingStation.logPrefix()} Authorization result for idTag '${truncateId(idTag)}': ${authResult.status} using ${authResult.method} method`
+    )
+
+    if (authResult.status === AuthStatus.ACCEPTED) {
+      const connectorStatus = chargingStation.getConnectorStatus(connectorId)
+      if (connectorStatus != null) {
+        switch (authResult.method) {
+          case AuthenticationMethod.CACHE:
+          case AuthenticationMethod.LOCAL_LIST:
+          case AuthenticationMethod.OFFLINE_FALLBACK:
+            connectorStatus.localAuthorizeIdTag = idTag
+            connectorStatus.idTagLocalAuthorized = true
+            break
+          case AuthenticationMethod.CERTIFICATE_BASED:
+          case AuthenticationMethod.NONE:
+          case AuthenticationMethod.REMOTE_AUTHORIZATION:
+            break
+        }
+      }
+      return true
+    }
+
+    return false
+  } catch (error) {
+    logger.error(`${chargingStation.logPrefix()} Authorization failed`, error)
+    return false
+  }
+}
index 56a33059dfd603f821257758d9b76e1aff1741c7..4d084d960179aafc1a5d6d8cccf21c706dad7aa3 100644 (file)
@@ -8,7 +8,6 @@ export { OCPP20ResponseService } from './2.0/OCPP20ResponseService.js'
 export { buildTransactionEvent, OCPP20ServiceUtils } from './2.0/OCPP20ServiceUtils.js'
 export { OCPP20VariableManager } from './2.0/OCPP20VariableManager.js'
 export { OCPPAuthServiceFactory } from './auth/index.js'
-export { isIdTagAuthorized } from './IdTagAuthorization.js'
 export { OCPPConstants } from './OCPPConstants.js'
 export { OCPPIncomingRequestService } from './OCPPIncomingRequestService.js'
 export { OCPPRequestService } from './OCPPRequestService.js'
@@ -16,6 +15,7 @@ export { createOCPPServices } from './OCPPServiceFactory.js'
 export {
   buildBootNotificationRequest,
   flushQueuedTransactionMessages,
+  isIdTagAuthorized,
   startTransactionOnConnector,
   stopRunningTransactions,
   stopTransactionOnConnector,
index 7e2c408b804f344a563efd4676be577e88292a3e..c556dab870792b3e6fbc8ef608c0b3acb3660e07 100644 (file)
@@ -20,7 +20,7 @@ import {
   AuthorizationStatus,
   OCPPAuthServiceFactory,
 } from '../../../src/charging-station/ocpp/auth/index.js'
-import { isIdTagAuthorized } from '../../../src/charging-station/ocpp/IdTagAuthorization.js'
+import { isIdTagAuthorized } from '../../../src/charging-station/ocpp/OCPPServiceOperations.js'
 import { OCPPVersion } from '../../../src/types/index.js'
 import { standardCleanup } from '../../helpers/TestLifecycleHelpers.js'
 import { createMockChargingStation } from '../ChargingStationTestUtils.js'