]> Piment Noir Git Repositories - e-mobility-charging-stations-simulator.git/commitdiff
refactor: use `JsonObject` and generic `OCPPAuthAdapter<TVersionId>` in auth module
authorJérôme Benoit <jerome.benoit@sap.com>
Fri, 27 Mar 2026 16:07:17 +0000 (17:07 +0100)
committerJérôme Benoit <jerome.benoit@sap.com>
Fri, 27 Mar 2026 16:07:17 +0000 (17:07 +0100)
src/charging-station/ocpp/auth/adapters/OCPP16AuthAdapter.ts
src/charging-station/ocpp/auth/adapters/OCPP20AuthAdapter.ts
src/charging-station/ocpp/auth/interfaces/OCPPAuthService.ts
src/charging-station/ocpp/auth/strategies/CertificateAuthStrategy.ts
src/charging-station/ocpp/auth/strategies/LocalAuthStrategy.ts
src/charging-station/ocpp/auth/strategies/RemoteAuthStrategy.ts
tests/charging-station/ocpp/auth/helpers/MockFactories.ts

index b2983c0a0afd343e69fb0965d9eb41e8e265b691..7e189980c58526dfc7f5d7e3218a1fa3e56075ea 100644 (file)
@@ -1,4 +1,5 @@
 import type { ChargingStation } from '../../../../charging-station/index.js'
+import type { JsonObject } from '../../../../types/index.js'
 import type { OCPPAuthAdapter } from '../interfaces/OCPPAuthService.js'
 import type {
   AuthConfiguration,
@@ -34,7 +35,7 @@ const moduleName = 'OCPP16AuthAdapter'
  * Handles authentication for OCPP 1.6 charging stations by translating
  * between unified auth types and OCPP 1.6 specific types and protocols.
  */
-export class OCPP16AuthAdapter implements OCPPAuthAdapter {
+export class OCPP16AuthAdapter implements OCPPAuthAdapter<string> {
   readonly ocppVersion = OCPPVersion.VERSION_16
 
   constructor (private readonly chargingStation: ChargingStation) {}
@@ -215,7 +216,7 @@ export class OCPP16AuthAdapter implements OCPPAuthAdapter {
    * Get OCPP 1.6 specific configuration schema
    * @returns JSON schema object describing valid OCPP 1.6 auth configuration properties
    */
-  getConfigurationSchema (): Record<string, unknown> {
+  getConfigurationSchema (): JsonObject {
     return {
       properties: {
         allowOfflineTxForUnknownId: {
@@ -258,7 +259,7 @@ export class OCPP16AuthAdapter implements OCPPAuthAdapter {
    * Get adapter-specific status information
    * @returns Status object with online state, auth settings, and station identifier
    */
-  getStatus (): Record<string, unknown> {
+  getStatus (): JsonObject {
     return {
       isOnline: this.chargingStation.inAcceptedState(),
       localAuthEnabled: this.chargingStation.getLocalAuthListEnabled(),
index 744913c0ce1b3242b16a0be5037df0862ef046d3..44d382f35a444baf9abaec1c89eea81e05b33cb9 100644 (file)
@@ -1,5 +1,6 @@
 import type {
   AdditionalInfoType,
+  JsonObject,
   OCPP20AuthorizeRequest,
   OCPP20AuthorizeResponse,
   RequestStartStopStatusEnumType,
@@ -321,7 +322,7 @@ export class OCPP20AuthAdapter implements OCPPAuthAdapter {
    * Get OCPP 2.0 specific configuration schema
    * @returns Configuration schema object for OCPP 2.0 authorization settings
    */
-  getConfigurationSchema (): Record<string, unknown> {
+  getConfigurationSchema (): JsonObject {
     return {
       properties: {
         authCacheEnabled: {
@@ -364,7 +365,7 @@ export class OCPP20AuthAdapter implements OCPPAuthAdapter {
    * Get adapter-specific status information
    * @returns Status object containing adapter state and capabilities
    */
-  getStatus (): Record<string, unknown> {
+  getStatus (): JsonObject {
     return {
       isOnline: this.chargingStation.inAcceptedState(),
       localAuthEnabled: true, // Configuration dependent
index 7122f981b46ae43e694c0c5dcf0af2d2df8f2d2d..b6e5bc989b4306ce616aa29bea21d2216e2243ea 100644 (file)
@@ -1,4 +1,9 @@
-import type { OCPP20IdTokenInfoType, OCPPVersion } from '../../../../types/index.js'
+import type {
+  JsonObject,
+  OCPP20IdTokenInfoType,
+  OCPP20IdTokenType,
+  OCPPVersion,
+} from '../../../../types/index.js'
 import type {
   AuthConfiguration,
   AuthorizationResult,
@@ -154,7 +159,7 @@ export interface AuthStrategy {
   /**
    * Get strategy-specific statistics
    */
-  getStats(): Promise<Record<string, unknown>> | Record<string, unknown>
+  getStats(): JsonObject | Promise<JsonObject>
 
   /**
    * Initialize the strategy with configuration
@@ -173,7 +178,7 @@ export interface AuthStrategy {
   readonly priority: number
 }
 
-export interface CacheStats {
+export interface CacheStats extends JsonObject {
   /** Number of entries evicted due to capacity limits */
   evictions: number
 
@@ -322,7 +327,7 @@ export interface LocalAuthListManager {
  * Adapters handle the translation between unified auth types
  * and version-specific OCPP types and protocols.
  */
-export interface OCPPAuthAdapter {
+export interface OCPPAuthAdapter<TVersionId = OCPP20IdTokenType | string> {
   /**
    * Perform remote authorization using version-specific protocol
    * @param identifier - Unified identifier to authorize
@@ -341,7 +346,7 @@ export interface OCPPAuthAdapter {
    * @param identifier - Unified identifier
    * @returns Version-specific identifier
    */
-  convertFromUnifiedIdentifier(identifier: UnifiedIdentifier): object | string
+  convertFromUnifiedIdentifier(identifier: UnifiedIdentifier): TVersionId
 
   /**
    * Convert a version-specific identifier to unified format
@@ -350,14 +355,14 @@ export interface OCPPAuthAdapter {
    * @returns Unified identifier
    */
   convertToUnifiedIdentifier(
-    identifier: object | string,
+    identifier: TVersionId,
     additionalData?: Record<string, unknown>
   ): UnifiedIdentifier
 
   /**
    * Get adapter-specific configuration requirements
    */
-  getConfigurationSchema(): Record<string, unknown>
+  getConfigurationSchema(): JsonObject
 
   /**
    * Check if remote authorization is available
index 6d42dfef2b6c7a2b6cb7e77ae41bcb9b927256d8..a8482a94fb15dd23d68e43f88a9a75cf85851111 100644 (file)
@@ -1,3 +1,4 @@
+import type { JsonObject } from '../../../../types/index.js'
 import type { ChargingStation } from '../../../ChargingStation.js'
 import type { AuthStrategy, OCPPAuthAdapter } from '../interfaces/OCPPAuthService.js'
 import type {
@@ -131,7 +132,7 @@ export class CertificateAuthStrategy implements AuthStrategy {
     logger.debug(`${moduleName}: Certificate authentication strategy cleaned up`)
   }
 
-  getStats (): Record<string, unknown> {
+  getStats (): JsonObject {
     return {
       ...this.stats,
       isInitialized: this.isInitialized,
index 7a94730d6645ff5aac5c86c5d13351ae1a1166a9..d80f1a046cb5a5a190554824b1c65e4a2365e552 100644 (file)
@@ -1,3 +1,4 @@
+import type { JsonObject } from '../../../../types/index.js'
 import type {
   AuthCache,
   AuthStrategy,
@@ -205,7 +206,7 @@ export class LocalAuthStrategy implements AuthStrategy {
    * Get strategy statistics
    * @returns Strategy statistics including hit rates, request counts, and cache status
    */
-  public getStats (): Record<string, unknown> {
+  public getStats (): JsonObject {
     const cacheStats = this.authCache ? this.authCache.getStats() : null
 
     return {
index d09eb1642a796471b675606710889ce9eefaa6a6..bae4ac19d673616e8cef6b35e1491bd5566cc48e 100644 (file)
@@ -1,3 +1,4 @@
+import type { JsonObject } from '../../../../types/index.js'
 import type {
   AuthCache,
   AuthStrategy,
@@ -216,7 +217,7 @@ export class RemoteAuthStrategy implements AuthStrategy {
    * Get strategy statistics
    * @returns Strategy statistics including success rates, response times, and error counts
    */
-  public async getStats (): Promise<Record<string, unknown>> {
+  public async getStats (): Promise<JsonObject> {
     const cacheStats = this.authCache ? this.authCache.getStats() : null
 
     let adapterAvailable = false
index dd5929b23e3b9018807cab600e7538a4023da821..edd7447cd70bfdbf5edc783ef11fadd941d21733 100644 (file)
@@ -24,6 +24,7 @@ import {
   type UnifiedIdentifier,
 } from '../../../../../src/charging-station/ocpp/auth/types/AuthTypes.js'
 import { OCPPVersion } from '../../../../../src/types/index.js'
+import { OCPP20IdTokenEnumType, type OCPP20IdTokenType } from '../../../../../src/types/index.js'
 
 /**
  * Factory functions for creating test mocks and fixtures
@@ -204,13 +205,10 @@ export const createMockOCPPAdapter = (
   convertFromUnifiedIdentifier: (identifier: UnifiedIdentifier) =>
     ocppVersion === OCPPVersion.VERSION_16
       ? identifier.value
-      : { idToken: identifier.value, type: identifier.type },
-  convertToUnifiedIdentifier: (identifier: object | string) => ({
+      : { idToken: identifier.value, type: OCPP20IdTokenEnumType.Central },
+  convertToUnifiedIdentifier: (identifier: OCPP20IdTokenType | string) => ({
     type: IdentifierType.ID_TAG,
-    value:
-      typeof identifier === 'string'
-        ? identifier
-        : ((identifier as { idToken?: string }).idToken ?? 'unknown'),
+    value: typeof identifier === 'string' ? identifier : identifier.idToken,
   }),
   getConfigurationSchema: () => ({}),
   isRemoteAvailable: () => true,