@@ -24,43 +24,6 @@ function FieldLevelEncryption(config) {
2424 ] ;
2525}
2626
27- /**
28- * @private
29- */
30- function hasConfig ( config , endpoint ) {
31- if ( config && endpoint ) {
32- endpoint = endpoint . split ( "?" ) . shift ( ) ;
33- const conf = config . paths . find ( ( elem ) => {
34- const regex = new RegExp ( elem . path , "g" ) ;
35- return endpoint . match ( regex ) ;
36- } ) ;
37- return conf ? conf : null ;
38- }
39- return null ;
40- }
41-
42- /**
43- * @private
44- */
45- function elemFromPath ( path , obj ) {
46- try {
47- let parent = null ;
48- const paths = path . split ( "." ) ;
49- if ( path && paths . length > 0 ) {
50- paths . forEach ( ( e ) => {
51- parent = obj ;
52- obj = isJsonRoot ( e ) ? obj : obj [ e ] ;
53- } ) ;
54- }
55- return {
56- node : obj ,
57- parent : parent ,
58- } ;
59- } catch ( e ) {
60- return null ;
61- }
62- }
63-
6427/**
6528 * Set encryption header parameters
6629 *
@@ -86,7 +49,7 @@ function setHeader(header, params) {
8649 */
8750function encrypt ( endpoint , header , body ) {
8851 let bodyMap = body ;
89- const fleConfig = hasConfig ( this . config , endpoint ) ;
52+ const fleConfig = utils . hasConfig ( this . config , endpoint ) ;
9053 if ( fleConfig ) {
9154 if ( ! this . isWithHeader ) {
9255 bodyMap = fleConfig . toEncrypt . map ( ( v ) => {
@@ -102,7 +65,9 @@ function encrypt(endpoint, header, body) {
10265 }
10366 return {
10467 header : header ,
105- body : fleConfig ? computeBody ( fleConfig . toEncrypt , body , bodyMap ) : body ,
68+ body : fleConfig
69+ ? utils . computeBody ( fleConfig . toEncrypt , body , bodyMap )
70+ : body ,
10671 } ;
10772}
10873
@@ -116,7 +81,7 @@ function encrypt(endpoint, header, body) {
11681function decrypt ( response ) {
11782 const body = response . body ;
11883 let bodyMap = response . body ;
119- const fleConfig = hasConfig ( this . config , response . request . url ) ;
84+ const fleConfig = utils . hasConfig ( this . config , response . request . url ) ;
12085 if ( fleConfig ) {
12186 bodyMap = fleConfig . toDecrypt . map ( ( v ) => {
12287 if ( ! this . isWithHeader ) {
@@ -126,7 +91,9 @@ function decrypt(response) {
12691 }
12792 } ) ;
12893 }
129- return fleConfig ? computeBody ( fleConfig . toDecrypt , body , bodyMap ) : body ;
94+ return fleConfig
95+ ? utils . computeBody ( fleConfig . toDecrypt , body , bodyMap )
96+ : body ;
13097}
13198
13299/**
@@ -137,13 +104,13 @@ function decrypt(response) {
137104 * @param body Body to encrypt
138105 */
139106function encryptBody ( path , body ) {
140- const elem = elemFromPath ( path . element , body ) ;
107+ const elem = utils . elemFromPath ( path . element , body ) ;
141108 if ( elem && elem . node ) {
142109 const encryptedData = this . crypto . encryptData ( { data : elem . node } ) ;
143110 body = utils . mutateObjectProperty ( path . obj , encryptedData , body ) ;
144111 // delete encrypted field if not overridden
145112 if (
146- ! isJsonRoot ( path . obj ) &&
113+ ! utils . isJsonRoot ( path . obj ) &&
147114 path . element !== path . obj + "." + this . config . encryptedValueFieldName
148115 ) {
149116 utils . deleteNode ( path . element , body ) ;
@@ -162,13 +129,13 @@ function encryptBody(path, body) {
162129 * @returns {Object } Encrypted body
163130 */
164131function encryptWithHeader ( encParams , path , body ) {
165- const elem = elemFromPath ( path . element , body ) . node ;
132+ const elem = utils . elemFromPath ( path . element , body ) . node ;
166133 const encrypted = this . crypto . encryptData ( { data : elem } , encParams ) ;
167134 const data = {
168135 [ this . config . encryptedValueFieldName ] :
169136 encrypted [ this . config . encryptedValueFieldName ] ,
170137 } ;
171- return isJsonRoot ( path . obj ) ? data : { [ path . obj ] : data } ;
138+ return utils . isJsonRoot ( path . obj ) ? data : { [ path . obj ] : data } ;
172139}
173140
174141/**
@@ -180,7 +147,7 @@ function encryptWithHeader(encParams, path, body) {
180147 * @returns {Object } Decrypted body
181148 */
182149function decryptBody ( path , body ) {
183- const elem = elemFromPath ( path . element , body ) ;
150+ const elem = utils . elemFromPath ( path . element , body ) ;
184151 if ( elem && elem . node ) {
185152 const decryptedObj = this . crypto . decryptData (
186153 elem . node [ this . config . encryptedValueFieldName ] , // encrypted data
@@ -208,8 +175,8 @@ function decryptBody(path, body) {
208175 * @param response Response with header to update
209176 */
210177function decryptWithHeader ( path , body , response ) {
211- const elemEncryptedNode = elemFromPath ( path . obj , body ) ;
212- const node = isJsonRoot ( path . element )
178+ const elemEncryptedNode = utils . elemFromPath ( path . obj , body ) ;
179+ const node = utils . isJsonRoot ( path . element )
213180 ? elemEncryptedNode . node
214181 : elemEncryptedNode . node [ path . element ] ;
215182 if ( node ) {
@@ -224,20 +191,10 @@ function decryptWithHeader(path, body, response) {
224191 response . header [ this . config . oaepHashingAlgorithmHeaderName ] ,
225192 response . header [ this . config . encryptedKeyHeaderName ]
226193 ) ;
227- return isJsonRoot ( path . obj ) ? decrypted : Object . assign ( body , decrypted ) ;
194+ return utils . isJsonRoot ( path . obj )
195+ ? decrypted
196+ : Object . assign ( body , decrypted ) ;
228197 }
229198}
230199
231- function isJsonRoot ( elem ) {
232- return elem === "$" ;
233- }
234-
235- function computeBody ( configParam , body , bodyMap ) {
236- return hasEncryptionParam ( configParam , bodyMap ) ? bodyMap . pop ( ) : body ;
237- }
238-
239- function hasEncryptionParam ( encParams , bodyMap ) {
240- return encParams && encParams . length === 1 && bodyMap && bodyMap [ 0 ] ;
241- }
242-
243200module . exports = FieldLevelEncryption ;
0 commit comments