Skip to content
This repository has been archived by the owner on Jan 10, 2025. It is now read-only.

Commit

Permalink
Fixed OCPI 401 response from IOP unlogged the end-user in front-end
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasBrazi06 committed Aug 4, 2021
1 parent 455868b commit 840bb8c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 14 deletions.
8 changes: 4 additions & 4 deletions src/client/ocpi/OCPIClient.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
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';
Expand Down Expand Up @@ -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 {
Expand All @@ -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;
Expand Down
20 changes: 10 additions & 10 deletions src/server/rest/v1/service/OCPIEndpointService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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));
Expand Down
5 changes: 5 additions & 0 deletions src/types/ocpi/OCPIEndpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ export interface OCPIVersion {
url: string;
}

export interface OCPIPingResult {
statusCode: number;
statusText: string;
}

export interface OCPIUnregisterResult {
statusCode: number;
statusText: string;
Expand Down

0 comments on commit 840bb8c

Please sign in to comment.