Created
September 19, 2020 10:58
-
-
Save gcnyin/da51affaca4a85cb1578f7c8e011d46f to your computer and use it in GitHub Desktop.
replaceEncryptedFields
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const replaceEncryptedFields = async ( | |
// eslint-disable-next-line | |
obj: any, | |
kmsDecryptFn: (encryptedValue: string) => Promise<string> | |
): Promise<void> => { | |
for (const [key, value] of Object.entries(obj)) { | |
if (typeof value === 'string' && value.startsWith(KMS_PREFIX)) { | |
const encryptedValue = value.slice(KMS_PREFIX.length, value.length); | |
obj[key] = await kmsDecryptFn(encryptedValue); | |
} else if (typeof value === 'object') { | |
await replaceEncryptedFields(value, kmsDecryptFn); | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment