OICP settings: certificate encryption refactoring
MODIFY
src/server/oicp/OICPUtils.ts
src/server/oicp/OICPUtils.ts
@@ -4,18 +4,22 @@ import User, { UserStatus } from '../../types/User';
import ChargingStationStorage from '../../storage/mongodb/ChargingStationStorage';
import Constants from '../../utils/Constants';
+import Cypher from '../../utils/Cypher';
import OCPPStorage from '../../storage/mongodb/OCPPStorage';
import { OICPAcknowledgment } from '../../types/oicp/OICPAcknowledgment';
import { OICPEvseID } from '../../types/oicp/OICPEvse';
import { OICPSession } from '../../types/oicp/OICPSession';
import { OICPStatusCode } from '../../types/oicp/OICPStatusCode';
+import { OicpSetting } from '../../types/Setting';
import RoamingUtils from '../../utils/RoamingUtils';
import { ServerAction } from '../../types/Server';
+import SettingStorage from '../../storage/mongodb/SettingStorage';
import Tenant from '../../types/Tenant';
import Transaction from '../../types/Transaction';
import TransactionStorage from '../../storage/mongodb/TransactionStorage';
import UserStorage from '../../storage/mongodb/UserStorage';
import moment from 'moment';
+import sanitize from 'mongo-sanitize';
export default class OICPUtils {
@@ -192,4 +196,33 @@ export default class OICPUtils {
// Save User Status
await UserStorage.saveUserStatus(tenantID, newVirtualOICPUser.id, UserStatus.ACTIVE);
}
+
+ public static async encryptCertificates(tenantID: string, oicpSetting: OicpSetting): Promise<void> {
+ const roamingSettings = await SettingStorage.getOICPSettings(tenantID);
+ // Encrypt key and certificate if not already encrypted
+ // CPO
+ if (roamingSettings.oicp.cpo?.key !== oicpSetting.cpo.key) {
+ oicpSetting.cpo.key = sanitize(Cypher.encrypt(oicpSetting.cpo.key));
+ } else {
+ oicpSetting.cpo.key = oicpSetting.cpo.key;
+ }
+ if (roamingSettings.oicp.cpo?.cert !== oicpSetting.cpo.cert) {
+ oicpSetting.cpo.cert = sanitize(Cypher.encrypt(oicpSetting.cpo.cert));
+ } else {
+ oicpSetting.cpo.cert = oicpSetting.cpo.cert;
+ }
+
+ // EMSP
+ if (roamingSettings.oicp.emsp?.key !== oicpSetting.emsp.key) {
+ oicpSetting.emsp.key = sanitize(Cypher.encrypt(oicpSetting.emsp.key));
+ } else {
+ oicpSetting.emsp.key = oicpSetting.emsp.key;
+ }
+ if (roamingSettings.oicp.emsp?.cert !== oicpSetting.emsp.cert) {
+ oicpSetting.emsp.cert = sanitize(Cypher.encrypt(oicpSetting.emsp.cert));
+ } else {
+ oicpSetting.emsp.cert = oicpSetting.emsp.cert;
+ }
+
+ }
}

MODIFY
src/server/rest/v1/service/SettingService.ts
src/server/rest/v1/service/SettingService.ts
@@ -8,6 +8,7 @@ import Authorizations from '../../../../authorization/Authorizations';
import Constants from '../../../../utils/Constants';
import Cypher from '../../../../utils/Cypher';
import Logging from '../../../../utils/Logging';
+import OICPUtils from '../../../oicp/OICPUtils';
import { ServerAction } from '../../../../types/Server';
import SettingSecurity from './security/SettingSecurity';
import SettingStorage from '../../../../storage/mongodb/SettingStorage';
@@ -114,9 +115,12 @@ export default class SettingService {
});
}
// Filter
- const filteredRequest = await SettingSecurity.filterSettingCreateRequest(req.body);
+ const filteredRequest = SettingSecurity.filterSettingCreateRequest(req.body);
// Process the sensitive data if any
Cypher.encryptSensitiveDataInJSON(filteredRequest);
+ if (filteredRequest.content.oicp) {
+ await OICPUtils.encryptCertificates(req.user.tenantID, filteredRequest.content.oicp);
+ }
// Update timestamp
filteredRequest.createdBy = { 'id': req.user.id };
filteredRequest.createdOn = new Date();
@@ -137,7 +141,7 @@ export default class SettingService {
public static async handleUpdateSetting(action: ServerAction, req: Request, res: Response, next: NextFunction): Promise<void> {
// Filter
- const settingUpdate = await SettingSecurity.filterSettingUpdateRequest(req.body, req.user.tenantID);
+ const settingUpdate = SettingSecurity.filterSettingUpdateRequest(req.body);
UtilsService.assertIdIsProvided(action, settingUpdate.id, MODULE_NAME, 'handleUpdateSetting', req.user);
// Check auth
if (!Authorizations.canUpdateSetting(req.user)) {

MODIFY
src/server/rest/v1/service/security/SettingSecurity.ts
src/server/rest/v1/service/security/SettingSecurity.ts
@@ -29,17 +29,17 @@ export default class SettingSecurity {
return filteredRequest;
}
- public static async filterSettingUpdateRequest(request: any, tenantID?: string): Promise<Partial<SettingDB>> {
- const filteredRequest = await SettingSecurity._filterSettingRequest(request, tenantID);
+ public static filterSettingUpdateRequest(request: any): Partial<SettingDB> {
+ const filteredRequest = SettingSecurity._filterSettingRequest(request);
filteredRequest.id = sanitize(request.id);
return filteredRequest;
}
- public static async filterSettingCreateRequest(request: any): Promise<Partial<SettingDB>> {
- return await SettingSecurity._filterSettingRequest(request);
+ public static filterSettingCreateRequest(request: any): Partial<SettingDB> {
+ return SettingSecurity._filterSettingRequest(request);
}
- public static async _filterSettingRequest(request: Partial<SettingDB>, tenantID?: string): Promise<Partial<SettingDB>> {
+ public static _filterSettingRequest(request: Partial<SettingDB>): Partial<SettingDB> {
const settings: SettingDB = {
identifier: sanitize(request.identifier),
sensitiveData: request.sensitiveData ? request.sensitiveData.map(sanitize) : []
@@ -124,25 +124,15 @@ export default class SettingSecurity {
countryCode: sanitize(request.content.oicp.cpo.countryCode),
partyID: sanitize(request.content.oicp.cpo.partyID),
key: '',
- cert:'',
+ cert:''
};
- const oicpSettings = await SettingStorage.getOICPSettings(tenantID);
- // Encrypt key and certificate if not already encrypted
- if (oicpSettings.oicp.cpo?.key !== request.content.oicp.cpo.key) {
- settings.content.oicp.cpo.key = sanitize(Cypher.encrypt(request.content.oicp.cpo.key));
- } else {
- settings.content.oicp.cpo.key = request.content.oicp.cpo.key;
- }
- if (oicpSettings.oicp.cpo?.cert !== request.content.oicp.cpo.cert) {
- settings.content.oicp.cpo.cert = sanitize(Cypher.encrypt(request.content.oicp.cpo.cert));
- } else {
- settings.content.oicp.cpo.cert = request.content.oicp.cpo.cert;
- }
}
if (request.content.oicp.emsp) {
settings.content.oicp.emsp = {
countryCode: sanitize(request.content.oicp.emsp.countryCode),
- partyID: sanitize(request.content.oicp.emsp.partyID)
+ partyID: sanitize(request.content.oicp.emsp.partyID),
+ key: '',
+ cert:''
};
}
if (request.content.oicp.currency) {

OICP settings: certificate encryption refactoring