@@ -5,9 +5,8 @@ import { Config } from "./Config";
55import { NoderedUtil , TokenUser , WellknownIds , Rolemember , User } from "@openiap/openflow-api" ;
66import { Span } from "@opentelemetry/api" ;
77import { Logger } from "./Logger" ;
8- import { DBHelper } from "./DBHelper" ;
98export 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