Added connection schema validation + removed security class
MODIFY
src/assets/server/rest/v1/schemas/connections/connection-create.json
src/assets/server/rest/v1/schemas/connections/connection-create.json
"type": "object",
"properties": {
"connectorId": {
- "type": "string"
+ "$ref": "chargingstation.json#/definitions/connector/properties/connectorId"
},
"userId": {
- "type": "string"
+ "type": "user.json#/definitions/id"
},
"data": {
- "type": "object"
+ "type": "object",
+ "sanitize": "mongo"
}
},
"required": [

ADD
src/assets/server/rest/v1/schemas/connections/connection-get.json
+{
+ "title": "Get Connection Request",
+ "type": "object",
+ "properties": {
+ "ID": {
+ "$ref": "common.json#/definitions/id"
+ }
+ },
+ "required": [
+ "ID"
+ ]
+}

ADD
src/assets/server/rest/v1/schemas/connections/connections-get.json
+{
+ "title": "Get Connections Request",
+ "type": "object",
+ "properties": {
+ "UserID": {
+ "$ref": "user.json#/definitions/id"
+ }
+ },
+ "required": [
+ "UserID"
+ ]
+}

MODIFY
src/server/rest/v1/service/ConnectionService.ts
src/server/rest/v1/service/ConnectionService.ts
@@ -7,7 +7,6 @@ import AppAuthError from '../../../../exception/AppAuthError';
import AppError from '../../../../exception/AppError';
import Authorizations from '../../../../authorization/Authorizations';
import CarConnectorFactory from '../../../../integration/car-connector/CarConnectorFactory';
-import ConnectionSecurity from './security/ConnectionSecurity';
import ConnectionStorage from '../../../../storage/mongodb/ConnectionStorage';
import ConnectionValidator from '../validator/ConnectionValidator';
import Constants from '../../../../utils/Constants';
@@ -23,7 +22,7 @@ const MODULE_NAME = 'ConnectionService';
export default class ConnectionService {
public static async handleGetConnection(action: ServerAction, req: Request, res: Response, next: NextFunction): Promise<void> {
// Filter
- const connectionID = ConnectionSecurity.filterConnectionRequestByID(req.query);
+ const connectionID = ConnectionValidator.getInstance().validateConnectionGet(req.query).ID;
// Charge Box is mandatory
if (!connectionID) {
throw new AppError({
@@ -69,7 +68,7 @@ export default class ConnectionService {
});
}
// Filter
- const filteredRequest = ConnectionSecurity.filterConnectionsRequest(req.query);
+ const filteredRequest = ConnectionValidator.getInstance().validateConnectionsGet(req.query);
// Check Users
let userProject: string[] = [];
if ((await Authorizations.canListUsers(req.user)).authorized) {
@@ -92,10 +91,8 @@ export default class ConnectionService {
module: MODULE_NAME, method: 'handleCreateConnection'
});
}
- // Check
- ConnectionValidator.getInstance().validateConnectionCreation(req.body);
// Filter
- const filteredRequest = ConnectionSecurity.filterConnectionCreateRequest(req.body);
+ const filteredRequest = ConnectionValidator.getInstance().validateConnectionCreation(req.body);
let integrationConnector = null;
switch (filteredRequest.connectorId) {
case ConnectionType.MERCEDES:
@@ -130,7 +127,7 @@ export default class ConnectionService {
public static async handleDeleteConnection(action: ServerAction, req: Request, res: Response, next: NextFunction): Promise<void> {
// Filter
- const connectionID = ConnectionSecurity.filterConnectionRequestByID(req.query);
+ const connectionID = ConnectionValidator.getInstance().validateConnectionGet(req.query).ID;
if (!connectionID) {
throw new AppError({
source: Constants.CENTRAL_SERVER,

DELETE
src/server/rest/v1/service/security/ConnectionSecurity.ts
-import Connection from '../../../../../types/Connection';
-import { HttpConnectionsRequest } from '../../../../../types/requests/HttpConnectionRequest';
-import UtilsSecurity from './UtilsSecurity';
-import sanitize from 'mongo-sanitize';
-
-export default class ConnectionSecurity {
- public static filterConnectionDeleteRequest(request: any): Connection {
- const filteredRequest: Connection = {} as Connection;
- // Set
- filteredRequest.userId = sanitize(request.userId);
- filteredRequest.connectorId = sanitize(request.connectorId);
- return filteredRequest;
- }
-
- public static filterConnectionRequestByID(request: any): string {
- return sanitize(request.ID);
- }
-
- public static filterConnectionsRequest(request: any): HttpConnectionsRequest {
- const filteredRequest: HttpConnectionsRequest = {
- UserID: sanitize(request.UserID)
- } as HttpConnectionsRequest;
- UtilsSecurity.filterSkipAndLimit(request, filteredRequest);
- UtilsSecurity.filterSort(request, filteredRequest);
- return filteredRequest;
- }
-
- public static filterConnectionUpdateRequest(request: any): Connection {
- const filteredRequest = ConnectionSecurity.filterConnectionRequest(request);
- filteredRequest.id = sanitize(request.id);
- return filteredRequest;
- }
-
- public static filterConnectionCreateRequest(request: any): Connection {
- return ConnectionSecurity.filterConnectionRequest(request);
- }
-
- private static filterConnectionRequest(request: any): Connection {
- return {
- connectorId: sanitize(request.connectorId),
- userId: sanitize(request.userId),
- data: sanitize(request.data),
- };
- }
-}
-

MODIFY
src/server/rest/v1/validator/ConnectionValidator.ts
src/server/rest/v1/validator/ConnectionValidator.ts
+import { HttpConnectionRequest, HttpConnectionsRequest } from '../../../../types/requests/HttpConnectionRequest';
+
+import Connection from '../../../../types/Connection';
import Schema from '../../../../types/validator/Schema';
import SchemaValidator from './SchemaValidator';
import fs from 'fs';
@@ -6,10 +9,14 @@ import global from '../../../../types/GlobalType';
export default class ConnectionValidator extends SchemaValidator {
private static instance: ConnectionValidator|null = null;
private connectionCreation: Schema;
+ private connectionsGet: Schema;
+ private connectionGet: Schema;
private constructor() {
super('ConnectionValidator');
this.connectionCreation = JSON.parse(fs.readFileSync(`${global.appRoot}/assets/server/rest/v1/schemas/connections/connection-create.json`, 'utf8'));
+ this.connectionsGet = JSON.parse(fs.readFileSync(`${global.appRoot}/assets/server/rest/v1/schemas/connections/connections-get.json`, 'utf8'));
+ this.connectionGet = JSON.parse(fs.readFileSync(`${global.appRoot}/assets/server/rest/v1/schemas/connections/connection-get.json`, 'utf8'));
}
public static getInstance(): ConnectionValidator {
@@ -19,8 +26,19 @@ export default class ConnectionValidator extends SchemaValidator {
return ConnectionValidator.instance;
}
- validateConnectionCreation(content): void {
+ validateConnectionCreation(content): Connection {
this.validate(this.connectionCreation, content);
+ return content;
+ }
+
+ validateConnectionsGet(content): HttpConnectionsRequest {
+ this.validate(this.connectionsGet, content);
+ return content;
+ }
+
+ validateConnectionGet(content): HttpConnectionRequest {
+ this.validate(this.connectionGet, content);
+ return content;
}
}

MODIFY
src/types/requests/HttpConnectionRequest.ts
src/types/requests/HttpConnectionRequest.ts
@@ -3,3 +3,7 @@ import HttpDatabaseRequest from './HttpDatabaseRequest';
export interface HttpConnectionsRequest extends HttpDatabaseRequest {
UserID?: string;
}
+
+export interface HttpConnectionRequest extends HttpDatabaseRequest {
+ ID?: string;
+}

Added connection schema validation + removed security class