Fixed OCPI 401 response from IOP unlogged the end-user in front-end
MODIFY
src/client/ocpi/OCPIClient.ts
src/client/ocpi/OCPIClient.ts
import { AxiosInstance, AxiosResponse } from 'axios';
-import OCPIEndpoint, { OCPIEndpointVersions, OCPIRegisterResult, OCPIUnregisterResult, OCPIVersion } from '../../types/ocpi/OCPIEndpoint';
+import OCPIEndpoint, { OCPIEndpointVersions, OCPIPingResult, OCPIRegisterResult, OCPIUnregisterResult, OCPIVersion } from '../../types/ocpi/OCPIEndpoint';
import { ReasonPhrases, StatusCodes } from 'http-status-codes';
import AxiosFactory from '../../utils/AxiosFactory';
@@ -40,8 +40,8 @@ export default abstract class OCPIClient {
this.role = role.toLowerCase();
}
- public async ping(): Promise<any> {
- const pingResult: any = {};
+ public async ping(): Promise<OCPIPingResult> {
+ const pingResult = {} as OCPIPingResult;
// Try to access base Url (GET .../versions)
// Access versions API
try {
@@ -60,7 +60,7 @@ export default abstract class OCPIClient {
pingResult.statusText = response.statusText;
}
} catch (error) {
- pingResult.message = error.message;
+ pingResult.statusText = error.message;
pingResult.statusCode = (error.response) ? error.response.status : HTTPError.GENERAL_ERROR;
}
return pingResult;

MODIFY
src/server/rest/v1/service/OCPIEndpointService.ts
src/server/rest/v1/service/OCPIEndpointService.ts
@@ -222,26 +222,26 @@ export default class OCPIEndpointService {
// Build OCPI Client
const ocpiClient = await OCPIClientFactory.getOcpiClient(req.tenant, filteredRequest);
// Try to ping
- const pingResult = await ocpiClient.ping();
+ const result = await ocpiClient.ping();
// Check ping result
- if (pingResult.statusCode === StatusCodes.OK) {
+ if (result.statusCode === StatusCodes.OK) {
await Logging.logSecurityInfo({
tenantID: req.user.tenantID,
user: req.user, module: MODULE_NAME, method: 'handlePingOcpiEndpoint',
message: `Ocpi Endpoint '${filteredRequest.name}' can be reached successfully`,
action,
- detailedMessages: { pingResult }
+ detailedMessages: { result }
});
} else {
throw new AppError({
source: Constants.CENTRAL_SERVER,
module: MODULE_NAME, method: 'handlePingOcpiEndpoint',
action,
- errorCode: pingResult.statusCode,
- message: pingResult.statusText,
+ errorCode: HTTPError.GENERAL_ERROR,
+ message: `${result.statusText} (${result.statusCode})`,
});
}
- res.json(Object.assign(pingResult, Constants.REST_RESPONSE_SUCCESS));
+ res.json(Object.assign(result, Constants.REST_RESPONSE_SUCCESS));
next();
}
@@ -771,8 +771,8 @@ export default class OCPIEndpointService {
source: Constants.CENTRAL_SERVER,
module: MODULE_NAME, method: 'handleUnregisterOcpiEndpoint',
action,
- errorCode: result.statusCode,
- message: result.statusText,
+ errorCode: HTTPError.GENERAL_ERROR,
+ message: `${result.statusText} (${result.statusCode})`,
});
}
res.json(Object.assign(result, Constants.REST_RESPONSE_SUCCESS));
@@ -827,8 +827,8 @@ export default class OCPIEndpointService {
source: Constants.CENTRAL_SERVER,
module: MODULE_NAME, method: 'handleRegisterOcpiEndpoint',
action,
- errorCode: result.statusCode,
- message: result.statusText,
+ errorCode: HTTPError.GENERAL_ERROR,
+ message: `${result.statusText} (${result.statusCode})`,
});
}
res.json(Object.assign(result, Constants.REST_RESPONSE_SUCCESS));

MODIFY
src/types/ocpi/OCPIEndpoint.ts
src/types/ocpi/OCPIEndpoint.ts
@@ -53,6 +53,11 @@ export interface OCPIVersion {
url: string;
}
+export interface OCPIPingResult {
+ statusCode: number;
+ statusText: string;
+}
+
export interface OCPIUnregisterResult {
statusCode: number;
statusText: string;

Fixed OCPI 401 response from IOP unlogged the end-user in front-end