Skip to content

Commit fb331c7

Browse files
committed
restructure dbhelpr includes
1 parent 006fe4d commit fb331c7

31 files changed

Lines changed: 412 additions & 385 deletions

OpenFlow/src/Auth.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { Crypt } from "./Crypt";
22
import { User } from "@openiap/openflow-api";
3-
import { DBHelper } from "./DBHelper";
43
import { Span } from "@opentelemetry/api";
54
import { Logger } from "./Logger";
65
export class Auth {
@@ -10,7 +9,7 @@ export class Auth {
109
if (username === null || username === undefined || username === "") { throw Error("Username cannot be null"); }
1110
span?.setAttribute("username", username);
1211
if (password === null || password === undefined || password === "") { throw Error("Password cannot be null"); }
13-
const user: User = await DBHelper.FindByUsername(username, null, span);
12+
const user: User = await Logger.DBHelper.FindByUsername(username, null, span);
1413
if (user === null || user === undefined) { return null; }
1514
if ((await Crypt.compare(password, user.passwordhash, span)) !== true) { return null; }
1615
return user;

OpenFlow/src/Config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ export class Config {
4949
Config.log_watches = Config.parseBoolean(Config.getEnv("log_watches", "false"));
5050
Config.log_watches_notify = Config.parseBoolean(Config.getEnv("log_watches_notify", "false"));
5151
Config.log_missing_jwt = Config.parseBoolean(Config.getEnv("log_missing_jwt", "true"));
52+
Config.log_login_provider = Config.parseBoolean(Config.getEnv("log_login_provider", "false"));
5253

5354
Config.openflow_uniqueid = Config.getEnv("openflow_uniqueid", "");
5455
Config.enable_openflow_amqp = Config.parseBoolean(Config.getEnv("enable_openflow_amqp", "false"));
@@ -228,6 +229,7 @@ export class Config {
228229
public static log_watches: boolean = Config.parseBoolean(Config.getEnv("log_watches", "false"));
229230
public static log_watches_notify: boolean = Config.parseBoolean(Config.getEnv("log_watches_notify", "false"));
230231
public static log_missing_jwt: boolean = Config.parseBoolean(Config.getEnv("log_missing_jwt", "true"));
232+
public static log_login_provider: boolean = Config.parseBoolean(Config.getEnv("log_login_provider", "false"));
231233

232234
public static openflow_uniqueid: string = Config.getEnv("openflow_uniqueid", "");
233235
public static enable_openflow_amqp: boolean = Config.parseBoolean(Config.getEnv("enable_openflow_amqp", "false"));

OpenFlow/src/Crypt.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@ import { Config } from "./Config";
55
import { NoderedUtil, TokenUser, WellknownIds, Rolemember, User } from "@openiap/openflow-api";
66
import { Span } from "@opentelemetry/api";
77
import { Logger } from "./Logger";
8-
import { DBHelper } from "./DBHelper";
98
export class Crypt {
10-
static encryption_key: string = Config.aes_secret.substr(0, 32); // must be 256 bytes (32 characters)
9+
static encryption_key: string = null; // must be 256 bytes (32 characters))
1110
static iv_length: number = 16; // for AES, this is always 16
1211
static bcrypt_salt_rounds: number = 12;
1312
static rootUser(): User {
@@ -48,6 +47,7 @@ export class Crypt {
4847
}
4948
static encrypt(text: string): string {
5049
let iv: Buffer = crypto.randomBytes(Crypt.iv_length);
50+
if (NoderedUtil.IsNullEmpty(Crypt.encryption_key)) Crypt.encryption_key = Config.aes_secret.substr(0, 32);
5151
let cipher: crypto.CipherGCM = crypto.createCipheriv('aes-256-gcm', Buffer.from(Crypt.encryption_key), iv);
5252
let encrypted: Buffer = cipher.update((text as any));
5353
encrypted = Buffer.concat([encrypted, cipher.final()]);
@@ -60,14 +60,15 @@ export class Crypt {
6060
let encryptedText: Buffer = Buffer.from(textParts.shift(), "hex");
6161
let authTag: Buffer = null;
6262
if (textParts.length > 0) authTag = Buffer.from(textParts.shift(), "hex");
63-
let decrypted: Buffer
63+
let decrypted: Buffer;
64+
if (NoderedUtil.IsNullEmpty(Crypt.encryption_key)) Crypt.encryption_key = Config.aes_secret.substr(0, 32);
6465
if (authTag != null) {
6566
let decipher: crypto.DecipherGCM = crypto.createDecipheriv('aes-256-gcm', Buffer.from(Crypt.encryption_key), iv);
6667
decipher.setAuthTag(authTag);
6768
decrypted = decipher.update(encryptedText);
6869
decrypted = Buffer.concat([decrypted, decipher.final()]);
6970
} else {
70-
let decipher2: crypto.Decipher = crypto.createDecipheriv("aes-256-cbc", Buffer.from(this.encryption_key), iv);
71+
let decipher2: crypto.Decipher = crypto.createDecipheriv("aes-256-cbc", Buffer.from(Crypt.encryption_key), iv);
7172
decrypted = decipher2.update(encryptedText);
7273
decrypted = Buffer.concat([decrypted, decipher2.final()]);
7374
}
@@ -115,6 +116,7 @@ export class Crypt {
115116
user.selectedcustomerid = item.selectedcustomerid;
116117
user.dblocked = item.dblocked;
117118

119+
if (NoderedUtil.IsNullEmpty(Crypt.encryption_key)) Crypt.encryption_key = Config.aes_secret.substr(0, 32);
118120
const key = Crypt.encryption_key;
119121
if (NoderedUtil.IsNullEmpty(Config.aes_secret)) throw new Error("Config missing aes_secret");
120122
if (NoderedUtil.IsNullEmpty(key)) throw new Error("Config missing aes_secret");
@@ -125,6 +127,7 @@ export class Crypt {
125127
if (NoderedUtil.IsNullEmpty(token)) {
126128
throw new Error('jwt must be provided');
127129
}
130+
if (NoderedUtil.IsNullEmpty(Crypt.encryption_key)) Crypt.encryption_key = Config.aes_secret.substr(0, 32);
128131
const o: any = jsonwebtoken.verify(token, Crypt.encryption_key);
129132
let impostor: string = null;
130133
if (!NoderedUtil.IsNullUndefinded(o) && !NoderedUtil.IsNullUndefinded(o.data) && !NoderedUtil.IsNullEmpty(o.data._id)) {
@@ -134,7 +137,7 @@ export class Crypt {
134137
}
135138
if (!NoderedUtil.IsNullUndefinded(o) && !NoderedUtil.IsNullUndefinded(o.data) && !NoderedUtil.IsNullEmpty(o.data._id) && o.data._id != WellknownIds.root) {
136139
var id = o.data._id;
137-
o.data = await DBHelper.FindById(o.data._id, token, null);
140+
o.data = await Logger.DBHelper.FindById(o.data._id, token, null);
138141
if (NoderedUtil.IsNullUndefinded(o)) {
139142
var b = true;
140143
}
@@ -147,6 +150,7 @@ export class Crypt {
147150

148151
}
149152
static decryptToken(token: string): any {
153+
if (NoderedUtil.IsNullEmpty(Crypt.encryption_key)) Crypt.encryption_key = Config.aes_secret.substr(0, 32);
150154
return jsonwebtoken.verify(token, Crypt.encryption_key);
151155
}
152156
}

0 commit comments

Comments
 (0)