From: Jérôme Benoit Date: Fri, 27 Mar 2026 16:07:17 +0000 (+0100) Subject: refactor: use `JsonObject` and generic `OCPPAuthAdapter` in auth module X-Git-Tag: ocpp-server@v4.0.0~36 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=d93b5347015bb319d52c8b09655173c1bae9e439;p=e-mobility-charging-stations-simulator.git refactor: use `JsonObject` and generic `OCPPAuthAdapter` in auth module --- diff --git a/src/charging-station/ocpp/auth/adapters/OCPP16AuthAdapter.ts b/src/charging-station/ocpp/auth/adapters/OCPP16AuthAdapter.ts index b2983c0a..7e189980 100644 --- a/src/charging-station/ocpp/auth/adapters/OCPP16AuthAdapter.ts +++ b/src/charging-station/ocpp/auth/adapters/OCPP16AuthAdapter.ts @@ -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 { 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 { + 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 { + getStatus (): JsonObject { return { isOnline: this.chargingStation.inAcceptedState(), localAuthEnabled: this.chargingStation.getLocalAuthListEnabled(), diff --git a/src/charging-station/ocpp/auth/adapters/OCPP20AuthAdapter.ts b/src/charging-station/ocpp/auth/adapters/OCPP20AuthAdapter.ts index 744913c0..44d382f3 100644 --- a/src/charging-station/ocpp/auth/adapters/OCPP20AuthAdapter.ts +++ b/src/charging-station/ocpp/auth/adapters/OCPP20AuthAdapter.ts @@ -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 { + 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 { + getStatus (): JsonObject { return { isOnline: this.chargingStation.inAcceptedState(), localAuthEnabled: true, // Configuration dependent diff --git a/src/charging-station/ocpp/auth/interfaces/OCPPAuthService.ts b/src/charging-station/ocpp/auth/interfaces/OCPPAuthService.ts index 7122f981..b6e5bc98 100644 --- a/src/charging-station/ocpp/auth/interfaces/OCPPAuthService.ts +++ b/src/charging-station/ocpp/auth/interfaces/OCPPAuthService.ts @@ -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 + getStats(): JsonObject | Promise /** * 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 { /** * 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 ): UnifiedIdentifier /** * Get adapter-specific configuration requirements */ - getConfigurationSchema(): Record + getConfigurationSchema(): JsonObject /** * Check if remote authorization is available diff --git a/src/charging-station/ocpp/auth/strategies/CertificateAuthStrategy.ts b/src/charging-station/ocpp/auth/strategies/CertificateAuthStrategy.ts index 6d42dfef..a8482a94 100644 --- a/src/charging-station/ocpp/auth/strategies/CertificateAuthStrategy.ts +++ b/src/charging-station/ocpp/auth/strategies/CertificateAuthStrategy.ts @@ -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 { + getStats (): JsonObject { return { ...this.stats, isInitialized: this.isInitialized, diff --git a/src/charging-station/ocpp/auth/strategies/LocalAuthStrategy.ts b/src/charging-station/ocpp/auth/strategies/LocalAuthStrategy.ts index 7a94730d..d80f1a04 100644 --- a/src/charging-station/ocpp/auth/strategies/LocalAuthStrategy.ts +++ b/src/charging-station/ocpp/auth/strategies/LocalAuthStrategy.ts @@ -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 { + public getStats (): JsonObject { const cacheStats = this.authCache ? this.authCache.getStats() : null return { diff --git a/src/charging-station/ocpp/auth/strategies/RemoteAuthStrategy.ts b/src/charging-station/ocpp/auth/strategies/RemoteAuthStrategy.ts index d09eb164..bae4ac19 100644 --- a/src/charging-station/ocpp/auth/strategies/RemoteAuthStrategy.ts +++ b/src/charging-station/ocpp/auth/strategies/RemoteAuthStrategy.ts @@ -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> { + public async getStats (): Promise { const cacheStats = this.authCache ? this.authCache.getStats() : null let adapterAvailable = false diff --git a/tests/charging-station/ocpp/auth/helpers/MockFactories.ts b/tests/charging-station/ocpp/auth/helpers/MockFactories.ts index dd5929b2..edd7447c 100644 --- a/tests/charging-station/ocpp/auth/helpers/MockFactories.ts +++ b/tests/charging-station/ocpp/auth/helpers/MockFactories.ts @@ -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,