OCPI getOne/getMany/update json schema + removed security class
ADD
src/assets/server/rest/v1/schemas/ocpi/ocpi-endpoint-get.json
+{
+ "title": "Get OCPI Endpoint Request",
+ "description": "",
+ "type": "object",
+ "properties": {
+ "ID": {
+ "$ref": "ocpi-endpoint.json#/definitions/id"
+ }
+ },
+ "required": [
+ "ID"
+ ]
+}

ADD
src/assets/server/rest/v1/schemas/ocpi/ocpi-endpoint-update.json
+{
+ "title": "OCPI Endpoint update Request",
+ "description": "",
+ "type": "object",
+ "properties": {
+ "id": {
+ "$ref": "ocpi-endpoint.json#/definitions/id"
+ },
+ "name": {
+ "$ref": "ocpi-endpoint.json#/definitions/name"
+ },
+ "role": {
+ "$ref": "ocpi-endpoint.json#/definitions/role"
+ },
+ "baseUrl": {
+ "$ref": "ocpi-endpoint.json#/definitions/baseUrl"
+ },
+ "countryCode": {
+ "$ref": "ocpi-endpoint.json#/definitions/countryCode"
+ },
+ "partyId": {
+ "$ref": "ocpi-endpoint.json#/definitions/partyId"
+ },
+ "localToken": {
+ "$ref": "ocpi-endpoint.json#/definitions/localToken"
+ },
+ "token": {
+ "$ref": "ocpi-endpoint.json#/definitions/token"
+ },
+ "backgroundPatchJob": {
+ "$ref": "ocpi-endpoint.json#/definitions/name"
+ }
+ },
+ "required": [
+ "id",
+ "name",
+ "role",
+ "baseUrl",
+ "localToken",
+ "token"
+ ]
+}

ADD
src/assets/server/rest/v1/schemas/ocpi/ocpi-endpoints-get.json
+{
+ "title": "Get OCPI Endpoints list Request",
+ "description": "",
+ "type": "object",
+ "properties": {
+ "Search": {
+ "$ref": "ocpi-endpoint.json#/definitions/id"
+ },
+ "Limit": {
+ "$ref": "common.json#/definitions/limit"
+ },
+ "SortFields": {
+ "$ref": "common.json#/definitions/sortFields"
+ },
+ "Skip": {
+ "$ref": "common.json#/definitions/skip"
+ },
+ "OnlyRecordCount": {
+ "$ref": "common.json#/definitions/onlyRecordCount"
+ },
+ "ProjectFields": {
+ "$ref": "common.json#/definitions/projectFields"
+ }
+ },
+ "required": []
+}

MODIFY
src/server/rest/v1/service/OCPIEndpointService.ts
src/server/rest/v1/service/OCPIEndpointService.ts
@@ -13,7 +13,6 @@ import LockingManager from '../../../../locking/LockingManager';
import Logging from '../../../../utils/Logging';
import OCPIClientFactory from '../../../../client/ocpi/OCPIClientFactory';
import OCPIEndpoint from '../../../../types/ocpi/OCPIEndpoint';
-import OCPIEndpointSecurity from './security/OCPIEndpointSecurity';
import OCPIEndpointStorage from '../../../../storage/mongodb/OCPIEndpointStorage';
import OCPIEndpointValidator from '../validator/OCPIEndpointValidator';
import { OCPIRegistrationStatus } from '../../../../types/ocpi/OCPIRegistrationStatus';
@@ -32,8 +31,7 @@ export default class OCPIEndpointService {
UtilsService.assertComponentIsActiveFromToken(req.user, TenantComponents.OCPI,
Action.DELETE, Entity.OCPI_ENDPOINT, MODULE_NAME, 'handleDeleteOcpiEndpoint');
// Filter
- const filteredRequest = OCPIEndpointSecurity.filterOcpiEndpointDeleteRequest(req.query);
- UtilsService.assertIdIsProvided(action, filteredRequest.ID, MODULE_NAME, 'handleDeleteOcpiEndpoint', req.user);
+ const filteredRequest = OCPIEndpointValidator.getInstance().validateOCPIEndpointGet(req.query);
// Check auth
if (!await Authorizations.canDeleteOcpiEndpoint(req.user)) {
throw new AppAuthError({
@@ -66,8 +64,7 @@ export default class OCPIEndpointService {
UtilsService.assertComponentIsActiveFromToken(req.user, TenantComponents.OCPI,
Action.READ, Entity.OCPI_ENDPOINT, MODULE_NAME, 'handleGetOcpiEndpoint');
// Filter
- const endpointID = OCPIEndpointSecurity.filterOcpiEndpointRequestByID(req.query);
- UtilsService.assertIdIsProvided(action, endpointID, MODULE_NAME, 'handleGetOcpiEndpoint', req.user);
+ const endpointID = OCPIEndpointValidator.getInstance().validateOCPIEndpointGet(req.query).ID;
// Check auth
if (!await Authorizations.canReadOcpiEndpoint(req.user)) {
throw new AppAuthError({
@@ -109,7 +106,7 @@ export default class OCPIEndpointService {
userProject = [ 'createdBy.name', 'createdBy.firstName', 'lastChangedBy.name', 'lastChangedBy.firstName' ];
}
// Filter
- const filteredRequest = OCPIEndpointSecurity.filterOcpiEndpointsRequest(req.query);
+ const filteredRequest = OCPIEndpointValidator.getInstance().validateOCPIEndpointsGet(req.query);
// Get all ocpiendpoints
const ocpiEndpoints = await OCPIEndpointStorage.getOcpiEndpoints(req.tenant,
{
@@ -117,7 +114,7 @@ export default class OCPIEndpointService {
}, {
limit: filteredRequest.Limit,
skip: filteredRequest.Skip,
- sort: filteredRequest.SortFields,
+ sort: UtilsService.httpSortFieldsToMongoDB(filteredRequest.SortFields),
onlyRecordCount: filteredRequest.OnlyRecordCount
},
[
@@ -168,9 +165,7 @@ export default class OCPIEndpointService {
UtilsService.assertComponentIsActiveFromToken(req.user, TenantComponents.OCPI,
Action.UPDATE, Entity.OCPI_ENDPOINT, MODULE_NAME, 'handleUpdateOcpiEndpoint');
// Filter
- const filteredRequest = OCPIEndpointSecurity.filterOcpiEndpointUpdateRequest(req.body);
- // Check Mandatory fields
- UtilsService.checkIfOCPIEndpointValid(filteredRequest, req);
+ const filteredRequest = OCPIEndpointValidator.getInstance().validateOCPIEndpointUpdate(req.body);
// Check auth
if (!await Authorizations.canUpdateOcpiEndpoint(req.user)) {
throw new AppAuthError({
@@ -464,8 +459,7 @@ export default class OCPIEndpointService {
});
}
// Filter
- const filteredRequest = OCPIEndpointSecurity.filterOcpiCheckCdrsRequest(req.body);
- UtilsService.assertIdIsProvided(action, filteredRequest.id, MODULE_NAME, 'handleCheckCdrsEndpoint', req.user);
+ const filteredRequest = OCPIEndpointValidator.getInstance().validateOCPIEndpointById(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`,
@@ -725,8 +719,7 @@ export default class OCPIEndpointService {
});
}
// Filter
- const filteredRequest = OCPIEndpointSecurity.filterOcpiEndpointRegisterRequest(req.body);
- UtilsService.assertIdIsProvided(action, filteredRequest.id, MODULE_NAME, 'handleUnregisterOcpiEndpoint', req.user);
+ const filteredRequest = OCPIEndpointValidator.getInstance().validateOCPIEndpointById(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`,
@@ -782,8 +775,7 @@ export default class OCPIEndpointService {
});
}
// Filter
- const filteredRequest = OCPIEndpointSecurity.filterOcpiEndpointRegisterRequest(req.body);
- UtilsService.assertIdIsProvided(action, filteredRequest.id, MODULE_NAME, 'handleRegisterOcpiEndpoint', req.user);
+ const filteredRequest = OCPIEndpointValidator.getInstance().validateOCPIEndpointById(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`,

DELETE
src/server/rest/v1/service/security/OCPIEndpointSecurity.ts
-import { HttpOCPIEndpointsRequest } from '../../../../../types/requests/HttpOCPIEndpointRequest';
-import OCPIEndpoint from '../../../../../types/ocpi/OCPIEndpoint';
-import UtilsSecurity from './UtilsSecurity';
-import sanitize from 'mongo-sanitize';
-
-export default class OCPIEndpointSecurity {
- static filterOcpiEndpointDeleteRequest(request: any): HttpOCPIEndpointsRequest {
- const filteredRequest: HttpOCPIEndpointsRequest = {} as HttpOCPIEndpointsRequest;
- // Set
- filteredRequest.ID = sanitize(request.ID);
- return filteredRequest;
- }
-
- static filterOcpiEndpointRequestByID(request: any): string {
- return sanitize(request.ID);
- }
-
- public static filterOcpiEndpointsRequest(request: any): HttpOCPIEndpointsRequest {
- const filteredRequest: HttpOCPIEndpointsRequest = {} as HttpOCPIEndpointsRequest;
- filteredRequest.Search = sanitize(request.Search);
- UtilsSecurity.filterSkipAndLimit(request, filteredRequest);
- UtilsSecurity.filterSort(request, filteredRequest);
- return filteredRequest;
- }
-
- static filterOcpiEndpointUpdateRequest(request: any): Partial<OCPIEndpoint> {
- const filteredRequest = OCPIEndpointSecurity._filterOcpiEndpointRequest(request);
- filteredRequest.id = sanitize(request.id);
- return filteredRequest;
- }
-
- static filterOcpiEndpointCreateRequest(request: any): Partial<OCPIEndpoint> {
- return OCPIEndpointSecurity._filterOcpiEndpointRequest(request);
- }
-
- static filterOcpiEndpointPingRequest(request: any): OCPIEndpoint {
- return OCPIEndpointSecurity._filterOcpiEndpointRequest(request) as OCPIEndpoint;
- }
-
- static filterOcpiEndpointTriggerJobRequest(request: any): Partial<OCPIEndpoint> {
- const filteredRequest = OCPIEndpointSecurity._filterOcpiEndpointRequest(request);
- filteredRequest.id = sanitize(request.id);
- return filteredRequest;
- }
-
- static filterOcpiEndpointSendEVSEStatusesRequest(request: any): Partial<OCPIEndpoint> {
- const filteredRequest = OCPIEndpointSecurity._filterOcpiEndpointRequest(request);
- filteredRequest.id = sanitize(request.id);
- return filteredRequest;
- }
-
- static filterOcpiCheckCdrsRequest(request: any): Partial<OCPIEndpoint> {
- const filteredRequest = OCPIEndpointSecurity._filterOcpiEndpointRequest(request);
- filteredRequest.id = sanitize(request.id);
- return filteredRequest;
- }
-
- static filterOcpiCheckSessionsRequest(request: any): Partial<OCPIEndpoint> {
- const filteredRequest = OCPIEndpointSecurity._filterOcpiEndpointRequest(request);
- filteredRequest.id = sanitize(request.id);
- return filteredRequest;
- }
-
- static filterOcpiCheckLocationsRequest(request: any): Partial<OCPIEndpoint> {
- const filteredRequest = OCPIEndpointSecurity._filterOcpiEndpointRequest(request);
- filteredRequest.id = sanitize(request.id);
- return filteredRequest;
- }
-
- static filterOcpiEndpointSendTokensRequest(request: any): Partial<OCPIEndpoint> {
- const filteredRequest = OCPIEndpointSecurity._filterOcpiEndpointRequest(request);
- filteredRequest.id = sanitize(request.id);
- return filteredRequest;
- }
-
- static filterOcpiEndpointRegisterRequest(request: any): Partial<OCPIEndpoint> {
- const filteredRequest = OCPIEndpointSecurity._filterOcpiEndpointRequest(request);
- filteredRequest.id = sanitize(request.id);
- return filteredRequest;
- }
-
- static filterOcpiEndpointGenerateLocalTokenRequest(request: any): Partial<OCPIEndpoint> {
- const filteredRequest = OCPIEndpointSecurity._filterOcpiEndpointRequest(request);
- filteredRequest.id = sanitize(request.id);
- return filteredRequest;
- }
-
- // eslint-disable-next-line no-unused-vars
- static _filterOcpiEndpointRequest(request: any): Partial<OCPIEndpoint> {
- const filteredRequest: Partial<OCPIEndpoint> = {};
- filteredRequest.name = sanitize(request.name);
- filteredRequest.role = sanitize(request.role);
- filteredRequest.baseUrl = sanitize(request.baseUrl);
- filteredRequest.countryCode = sanitize(request.countryCode);
- filteredRequest.partyId = sanitize(request.partyId);
- filteredRequest.localToken = sanitize(request.localToken);
- filteredRequest.token = sanitize(request.token);
- filteredRequest.backgroundPatchJob = sanitize(request.backgroundPatchJob);
- return filteredRequest;
- }
-}
-

MODIFY
src/server/rest/v1/validator/OCPIEndpointValidator.ts
src/server/rest/v1/validator/OCPIEndpointValidator.ts
-import { HttpOCPIEndpointByIdRequest, HttpOCPIEndpointGenerateLocalTokenRequest } from '../../../../types/requests/HttpOCPIEndpointRequest';
+import { HttpOCPIEndpointByIdRequest, HttpOCPIEndpointGenerateLocalTokenRequest, HttpOCPIEndpointRequest, HttpOCPIEndpointsRequest } from '../../../../types/requests/HttpOCPIEndpointRequest';
import OCPIEndpoint from '../../../../types/ocpi/OCPIEndpoint';
import Schema from '../../../../types/validator/Schema';
@@ -12,6 +12,9 @@ export default class OCPIEndpointValidator extends SchemaValidator {
private ocpiEndpointPing: Schema;
private ocpiEndpointById: Schema;
private ocpiEndpointGenerateLocalToken: Schema;
+ private ocpiEndpointGet: Schema;
+ private ocpiEndpointsGet: Schema;
+ private ocpiEndpointUpdate: Schema;
private constructor() {
@@ -20,6 +23,9 @@ export default class OCPIEndpointValidator extends SchemaValidator {
this.ocpiEndpointPing = JSON.parse(fs.readFileSync(`${global.appRoot}/assets/server/rest/v1/schemas/ocpi/ocpi-endpoint-ping.json`, 'utf8'));
this.ocpiEndpointById = JSON.parse(fs.readFileSync(`${global.appRoot}/assets/server/rest/v1/schemas/ocpi/ocpi-endpoint-by-id.json`, 'utf8'));
this.ocpiEndpointGenerateLocalToken = JSON.parse(fs.readFileSync(`${global.appRoot}/assets/server/rest/v1/schemas/ocpi/ocpi-endpoint-generate-local-token.json`, 'utf8'));
+ this.ocpiEndpointGet = JSON.parse(fs.readFileSync(`${global.appRoot}/assets/server/rest/v1/schemas/ocpi/ocpi-endpoint-get.json`, 'utf8'));
+ this.ocpiEndpointsGet = JSON.parse(fs.readFileSync(`${global.appRoot}/assets/server/rest/v1/schemas/ocpi/ocpi-endpoints-get.json`, 'utf8'));
+ this.ocpiEndpointUpdate = JSON.parse(fs.readFileSync(`${global.appRoot}/assets/server/rest/v1/schemas/ocpi/ocpi-endpoint-update.json`, 'utf8'));
}
public static getInstance(): OCPIEndpointValidator {
@@ -52,4 +58,22 @@ export default class OCPIEndpointValidator extends SchemaValidator {
this.validate(this.ocpiEndpointGenerateLocalToken, data);
return data;
}
+
+ public validateOCPIEndpointGet(data: any): HttpOCPIEndpointRequest {
+ // Validate schema
+ this.validate(this.ocpiEndpointGet, data);
+ return data;
+ }
+
+ public validateOCPIEndpointsGet(data: any): HttpOCPIEndpointsRequest {
+ // Validate schema
+ this.validate(this.ocpiEndpointsGet, data);
+ return data;
+ }
+
+ public validateOCPIEndpointUpdate(data: any): OCPIEndpoint {
+ // Validate schema
+ this.validate(this.ocpiEndpointUpdate, data);
+ return data;
+ }
}

MODIFY
src/types/requests/HttpOCPIEndpointRequest.ts
src/types/requests/HttpOCPIEndpointRequest.ts
+import HttpByIDRequest from './HttpByIDRequest';
import HttpDatabaseRequest from './HttpDatabaseRequest';
export interface HttpOCPIEndpointsRequest extends HttpDatabaseRequest{
@@ -12,3 +13,7 @@ export interface HttpOCPIEndpointByIdRequest {
export interface HttpOCPIEndpointGenerateLocalTokenRequest extends HttpOCPIEndpointByIdRequest {
name: string;
}
+
+export interface HttpOCPIEndpointRequest extends HttpByIDRequest {
+ ID: string;
+}

OCPI getOne/getMany/update json schema + removed security class