Skip to content

Commit 72209c7

Browse files
committed
add support for encrypting objects and arrays
1 parent 6eaafd3 commit 72209c7

6 files changed

Lines changed: 32 additions & 10 deletions

File tree

OpenFlow/src/DBHelper.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Crypt } from "./Crypt";
2-
import { User, Role, Rolemember, WellknownIds, Rights } from "openflow-api";
2+
import { User, Role, Rolemember, WellknownIds, Rights, NoderedUtil } from "openflow-api";
33
import { Config } from "./Config";
44

55
export class DBHelper {
@@ -106,7 +106,7 @@ export class DBHelper {
106106
public static async EnsureRole(jwt: string, name: string, id: string): Promise<Role> {
107107
var role: Role = await this.FindRoleByNameOrId(name, id);
108108
if (role !== null && (role._id === id || id === null)) { return role; }
109-
if (role !== null && id !== null) { await Config.db.DeleteOne(role._id, "users", jwt); }
109+
if (role !== null && !NoderedUtil.IsNullEmpty(role._id)) { await Config.db.DeleteOne(role._id, "users", jwt); }
110110
role = new Role(); role.name = name; role._id = id;
111111
role = await Config.db.InsertOne(role, "users", 0, false, jwt);
112112
role = Role.assign(role);

OpenFlow/src/DatabaseConnection.ts

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,8 +1058,9 @@ export class DatabaseConnection {
10581058
*/
10591059
private _shouldEncryptValue(keys: string[], key: string, value: object = null): boolean {
10601060
const shouldEncryptThisKey: boolean = keys.includes(key);
1061-
const isString: boolean = typeof value === "string";
1062-
return value && shouldEncryptThisKey && isString;
1061+
return value && shouldEncryptThisKey;
1062+
// const isString: boolean = typeof value === "string";
1063+
// return value && shouldEncryptThisKey && isString;
10631064
}
10641065
/**
10651066
* Enumerate object, encrypting fields that needs to be encrypted
@@ -1072,7 +1073,16 @@ export class DatabaseConnection {
10721073
return (Object.keys(item).reduce((newObj, key) => {
10731074
const value: any = item[key];
10741075
try {
1075-
newObj[key] = this._shouldEncryptValue(item._encrypt, key, value) ? Crypt.encrypt(value) : value;
1076+
if (this._shouldEncryptValue(item._encrypt, key, (value as any))) {
1077+
if (typeof value === "string") {
1078+
newObj[key] = Crypt.encrypt(value);
1079+
} else {
1080+
var tempvalue: any = JSON.stringify(value);
1081+
newObj[key] = Crypt.encrypt(tempvalue);
1082+
}
1083+
} else {
1084+
newObj[key] = value;
1085+
}
10761086
} catch (err) {
10771087
me._logger.error("encryptentity " + err.message);
10781088
newObj[key] = value;
@@ -1091,7 +1101,18 @@ export class DatabaseConnection {
10911101
return (Object.keys(item).reduce((newObj, key) => {
10921102
const value: any = item[key];
10931103
try {
1094-
newObj[key] = this._shouldEncryptValue(item._encrypt, key, value) ? Crypt.decrypt(value) : value;
1104+
if (this._shouldEncryptValue(item._encrypt, key, value)) {
1105+
let newvalue = Crypt.decrypt(value);
1106+
if (newvalue.indexOf("{") === 0 || newvalue.indexOf("[") === 0) {
1107+
try {
1108+
newvalue = JSON.parse(newvalue);
1109+
} catch (error) {
1110+
}
1111+
}
1112+
newObj[key] = newvalue;
1113+
} else {
1114+
newObj[key] = value;
1115+
}
10951116
} catch (err) {
10961117
me._logger.error("decryptentity " + err.message);
10971118
newObj[key] = value;

OpenFlowNodeRED/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "openflow-nodered",
3-
"version": "1.0.84",
3+
"version": "1.0.85",
44
"description": "Simple wrapper around NodeRed, RabbitMQ and MongoDB to support a more scaleable NodeRed implementation.\r Also the \"backend\" for [OpenRPA](https://github.com/skadefro/OpenRPA)",
55
"main": "index.js",
66
"scripts": {

OpenFlowNodeRED/src/node-red-contrib-openflow-storage.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,13 +359,14 @@ export class noderedcontribopenflowstorage {
359359
var item: any = {
360360
name: "credentials for " + Config.nodered_id,
361361
credentials: credentials, credentialsarray: credentialsarray, _type: "credential", nodered_id: Config.nodered_id,
362-
_encrypt: ["credentials"]
362+
_encrypt: ["credentials", "credentialsarray"]
363363
};
364364
var subresult = await NoderedUtil.InsertOne("nodered", item, 1, true, null);
365365
} else {
366366
var item: any = result[0];
367367
item.credentials = credentials;
368368
item.credentialsarray = credentialsarray;
369+
item._encrypt = ["credentials", "credentialsarray"];
369370
var subresult = await NoderedUtil.UpdateOne("nodered", null, item, 1, true, null);
370371
}
371372
this._credentials = credentials;

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.0.84
1+
1.0.85

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "openiap",
3-
"version": "1.0.84",
3+
"version": "1.0.85",
44
"description": "Simple wrapper around NodeRed, RabbitMQ and MongoDB to support a more scaleable NodeRed implementation.\r Also the \"backend\" for [OpenRPA](https://github.com/skadefro/OpenRPA)",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)