Added PUT of credentials
MODIFY
src/client/ocpi/OCPIClient.ts
src/client/ocpi/OCPIClient.ts
@@ -132,6 +132,43 @@ export default abstract class OCPIClient {
return registerResult;
}
+ public async updateCredentials(): Promise<OCPIRegisterResult> {
+ const registerResult = {} as OCPIRegisterResult;
+ try {
+ // Check versions
+ const versionFound = await this.checkVersions();
+ if (!versionFound) {
+ throw new BackendError({
+ action: ServerAction.OCPI_REGISTER,
+ message: 'OCPI Endpoint version 2.1.1 not found',
+ module: MODULE_NAME, method: 'constructor',
+ });
+ }
+ // Try to read services
+ const endpointVersions = await this.getEndpointVersions();
+ // Set available endpoints
+ this.ocpiEndpoint.availableEndpoints = OCPIUtils.convertAvailableEndpoints(endpointVersions);
+ this.ocpiEndpoint.localToken = OCPIUtils.generateLocalToken(this.tenant.subdomain);
+ // Put credentials and receive response
+ const credentials = await this.putCredentials();
+ // Store information
+ this.ocpiEndpoint.token = credentials.token;
+ this.ocpiEndpoint.countryCode = credentials.country_code;
+ this.ocpiEndpoint.partyId = credentials.party_id;
+ this.ocpiEndpoint.businessDetails = credentials.business_details;
+ // Save endpoint
+ this.ocpiEndpoint.status = OCPIRegistrationStatus.REGISTERED;
+ await OCPIEndpointStorage.saveOcpiEndpoint(this.tenant, this.ocpiEndpoint);
+ // Send success
+ registerResult.statusCode = StatusCodes.OK;
+ registerResult.statusText = ReasonPhrases.OK;
+ } catch (error) {
+ registerResult.statusCode = error.response ? error.response.status : HTTPError.GENERAL_ERROR;
+ registerResult.statusText = error.message;
+ }
+ return registerResult;
+ }
+
public async getVersions(): Promise<OCPIVersion[]> {
await Logging.logInfo({
tenantID: this.tenant.id,
@@ -166,48 +203,6 @@ export default abstract class OCPIClient {
return response.data?.data;
}
- public async deleteCredentials(): Promise<OCPICredential> {
- // Get credentials url
- const credentialsUrl = this.getEndpointUrl('credentials', ServerAction.OCPI_CREATE_CREDENTIALS);
- await Logging.logInfo({
- tenantID: this.tenant.id,
- action: ServerAction.OCPI_CREATE_CREDENTIALS,
- message: `Delete Credentials at ${credentialsUrl}`,
- module: MODULE_NAME, method: 'postCredentials'
- });
- // Call eMSP with CPO credentials
- const response = await this.axiosInstance.delete(credentialsUrl,
- {
- headers: {
- Authorization: `Token ${this.ocpiEndpoint.token}`,
- 'Content-Type': 'application/json'
- },
- });
- return response.data?.data;
- }
-
- public async postCredentials(): Promise<OCPICredential> {
- // Get credentials url
- const credentialsUrl = this.getEndpointUrl('credentials', ServerAction.OCPI_CREATE_CREDENTIALS);
- const credentials = await OCPIUtils.buildOCPICredentialObject(this.tenant, this.ocpiEndpoint.localToken, this.ocpiEndpoint.role);
- await Logging.logInfo({
- tenantID: this.tenant.id,
- action: ServerAction.OCPI_CREATE_CREDENTIALS,
- message: `Post Credentials at ${credentialsUrl}`,
- module: MODULE_NAME, method: 'postCredentials',
- detailedMessages: { credentials }
- });
- // Call eMSP with CPO credentials
- const response = await this.axiosInstance.post(credentialsUrl, credentials,
- {
- headers: {
- Authorization: `Token ${this.ocpiEndpoint.token}`,
- 'Content-Type': 'application/json'
- },
- });
- return response.data?.data;
- }
-
public getLocalCountryCode(action: ServerAction): string {
if (!this.settings[this.role]) {
throw new BackendError({
@@ -269,4 +264,68 @@ export default abstract class OCPIClient {
}
return versionFound;
}
+
+ private async deleteCredentials(): Promise<OCPICredential> {
+ // Get credentials url
+ const credentialsUrl = this.getEndpointUrl('credentials', ServerAction.OCPI_CREATE_CREDENTIALS);
+ await Logging.logInfo({
+ tenantID: this.tenant.id,
+ action: ServerAction.OCPI_CREATE_CREDENTIALS,
+ message: `Delete Credentials at ${credentialsUrl}`,
+ module: MODULE_NAME, method: 'postCredentials'
+ });
+ // Call eMSP with CPO credentials
+ const response = await this.axiosInstance.delete(credentialsUrl,
+ {
+ headers: {
+ Authorization: `Token ${this.ocpiEndpoint.token}`,
+ 'Content-Type': 'application/json'
+ },
+ });
+ return response.data?.data;
+ }
+
+ private async postCredentials(): Promise<OCPICredential> {
+ // Get credentials url
+ const credentialsUrl = this.getEndpointUrl('credentials', ServerAction.OCPI_CREATE_CREDENTIALS);
+ const credentials = await OCPIUtils.buildOCPICredentialObject(this.tenant, this.ocpiEndpoint.localToken, this.ocpiEndpoint.role);
+ await Logging.logInfo({
+ tenantID: this.tenant.id,
+ action: ServerAction.OCPI_CREATE_CREDENTIALS,
+ message: `Post Credentials at ${credentialsUrl}`,
+ module: MODULE_NAME, method: 'postCredentials',
+ detailedMessages: { credentials }
+ });
+ // Call eMSP with CPO credentials
+ const response = await this.axiosInstance.post(credentialsUrl, credentials,
+ {
+ headers: {
+ Authorization: `Token ${this.ocpiEndpoint.token}`,
+ 'Content-Type': 'application/json'
+ },
+ });
+ return response.data?.data;
+ }
+
+ private async putCredentials(): Promise<OCPICredential> {
+ // Get credentials url
+ const credentialsUrl = this.getEndpointUrl('credentials', ServerAction.OCPI_UPDATE_CREDENTIALS);
+ const credentials = await OCPIUtils.buildOCPICredentialObject(this.tenant, this.ocpiEndpoint.localToken, this.ocpiEndpoint.role);
+ await Logging.logInfo({
+ tenantID: this.tenant.id,
+ action: ServerAction.OCPI_CREATE_CREDENTIALS,
+ message: `Put Credentials at ${credentialsUrl}`,
+ module: MODULE_NAME, method: 'putCredentials',
+ detailedMessages: { credentials }
+ });
+ // Call eMSP with CPO credentials
+ const response = await this.axiosInstance.put(credentialsUrl, credentials,
+ {
+ headers: {
+ Authorization: `Token ${this.ocpiEndpoint.token}`,
+ 'Content-Type': 'application/json'
+ },
+ });
+ return response.data?.data;
+ }
}

MODIFY
src/server/rest/v1/service/OCPIEndpointService.ts
src/server/rest/v1/service/OCPIEndpointService.ts
@@ -228,7 +228,7 @@ export default class OCPIEndpointService {
module: MODULE_NAME, method: 'handlePingOcpiEndpoint',
action,
errorCode: HTTPError.GENERAL_ERROR,
- message: `${result.statusText} (${result.statusCode})`,
+ message: `${result.statusText}`,
});
}
res.json(Object.assign(result, Constants.REST_RESPONSE_SUCCESS));
@@ -688,56 +688,55 @@ export default class OCPIEndpointService {
}
public static async handleUpdateCredentialsOcpiEndpoint(action: ServerAction, req: Request, res: Response, next: NextFunction): Promise<void> {
- // // Check if component is active
- // UtilsService.assertComponentIsActiveFromToken(req.user, TenantComponents.OCPI,
- // Action.READ, Entity.OCPI_ENDPOINT, MODULE_NAME, 'handleUnregisterOcpiEndpoint');
- // // Check auth
- // if (!await Authorizations.canRegisterOcpiEndpoint(req.user)) {
- // throw new AppAuthError({
- // errorCode: HTTPAuthError.FORBIDDEN,
- // user: req.user,
- // action: Action.REGISTER, entity: Entity.OCPI_ENDPOINT,
- // module: MODULE_NAME, method: 'handleUnregisterOcpiEndpoint'
- // });
- // }
- // // Filter
- // const filteredRequest = OCPIEndpointValidator.getInstance().validateOCPIEndpointByIdReq(req.body);
- // // Get OcpiEndpoint
- // const ocpiEndpoint = await OCPIEndpointStorage.getOcpiEndpoint(req.tenant, filteredRequest.id);
- // UtilsService.assertObjectExists(action, ocpiEndpoint, `OCPI Endpoint ID '${filteredRequest.id}' does not exist`,
- // MODULE_NAME, 'handleUnregisterOcpiEndpoint', req.user);
- // // Build OCPI Client
- // const ocpiClient = await OCPIClientFactory.getOcpiClient(req.tenant, ocpiEndpoint);
- // // Try to register
- // const result = await ocpiClient.unregister();
- // // Check ping result
- // if (result.statusCode === StatusCodes.OK) {
- // await Logging.logInfo({
- // tenantID: req.tenant.id,
- // user: req.user, module: MODULE_NAME, method: 'handleUnregisterOcpiEndpoint',
- // message: `Ocpi Endpoint '${ocpiEndpoint.name}' can be reached successfully`,
- // action,
- // detailedMessages: { result }
- // });
- // } else {
- // // Already unregistered
- // if (result.statusCode === 405) {
- // throw new AppError({
- // module: MODULE_NAME, method: 'handleUnregisterOcpiEndpoint',
- // action,
- // errorCode: HTTPError.OCPI_ENDPOINT_ALREADY_UNREGISTERED,
- // message: 'Ocpi Endpoint is already unregistered',
- // });
- // }
- // throw new AppError({
- // module: MODULE_NAME, method: 'handleUnregisterOcpiEndpoint',
- // action,
- // errorCode: HTTPError.GENERAL_ERROR,
- // message: `${result.statusText} (${result.statusCode})`,
- // });
- // }
- // res.json(Object.assign(result, Constants.REST_RESPONSE_SUCCESS));
- res.json(Constants.REST_RESPONSE_SUCCESS);
+ // Check if component is active
+ UtilsService.assertComponentIsActiveFromToken(req.user, TenantComponents.OCPI,
+ Action.READ, Entity.OCPI_ENDPOINT, MODULE_NAME, 'handleUpdateCredentialsOcpiEndpoint');
+ // Check auth
+ if (!await Authorizations.canRegisterOcpiEndpoint(req.user)) {
+ throw new AppAuthError({
+ errorCode: HTTPAuthError.FORBIDDEN,
+ user: req.user,
+ action: Action.REGISTER, entity: Entity.OCPI_ENDPOINT,
+ module: MODULE_NAME, method: 'handleUpdateCredentialsOcpiEndpoint'
+ });
+ }
+ // Filter
+ const filteredRequest = OCPIEndpointValidator.getInstance().validateOCPIEndpointByIdReq(req.body);
+ // Get OcpiEndpoint
+ const ocpiEndpoint = await OCPIEndpointStorage.getOcpiEndpoint(req.tenant, filteredRequest.id);
+ UtilsService.assertObjectExists(action, ocpiEndpoint, `OCPI Endpoint ID '${filteredRequest.id}' does not exist`,
+ MODULE_NAME, 'handleUpdateCredentialsOcpiEndpoint', req.user);
+ // Build OCPI Client
+ const ocpiClient = await OCPIClientFactory.getOcpiClient(req.tenant, ocpiEndpoint);
+ // Try to register
+ const result = await ocpiClient.updateCredentials();
+ // Check ping result
+ if (result.statusCode === StatusCodes.OK) {
+ await Logging.logInfo({
+ tenantID: req.tenant.id,
+ user: req.user, module: MODULE_NAME, method: 'handleUpdateCredentialsOcpiEndpoint',
+ message: `Ocpi Endpoint '${ocpiEndpoint.name}' can be reached successfully`,
+ action,
+ detailedMessages: { result }
+ });
+ } else {
+ // Not yet registered
+ if (result.statusCode === 405) {
+ throw new AppError({
+ module: MODULE_NAME, method: 'handleUpdateCredentialsOcpiEndpoint',
+ action,
+ errorCode: HTTPError.OCPI_ENDPOINT_ALREADY_UNREGISTERED,
+ message: 'Ocpi Endpoint is not yet registered',
+ });
+ }
+ throw new AppError({
+ module: MODULE_NAME, method: 'handleUpdateCredentialsOcpiEndpoint',
+ action,
+ errorCode: HTTPError.GENERAL_ERROR,
+ message: `${result.statusText}`,
+ });
+ }
+ res.json(Object.assign(result, Constants.REST_RESPONSE_SUCCESS));
next();
}
@@ -787,7 +786,7 @@ export default class OCPIEndpointService {
module: MODULE_NAME, method: 'handleUnregisterOcpiEndpoint',
action,
errorCode: HTTPError.GENERAL_ERROR,
- message: `${result.statusText} (${result.statusCode})`,
+ message: `${result.statusText}`,
});
}
res.json(Object.assign(result, Constants.REST_RESPONSE_SUCCESS));
@@ -840,7 +839,7 @@ export default class OCPIEndpointService {
module: MODULE_NAME, method: 'handleRegisterOcpiEndpoint',
action,
errorCode: HTTPError.GENERAL_ERROR,
- message: `${result.statusText} (${result.statusCode})`,
+ message: `${result.statusText}`,
});
}
res.json(Object.assign(result, Constants.REST_RESPONSE_SUCCESS));

Added PUT of credentials