@lucidcms/core
Version:
The core of the Lucid CMS. It's responsible for spinning up the API and serving the CMS.
1 lines • 2.86 kB
Source Map (JSON)
{"version":3,"file":"encryption.mjs","names":["constants"],"sources":["../../../../src/libs/email/storage/encryption.ts"],"sourcesContent":["import constants from \"../../../constants/constants.js\";\nimport { decrypt, encrypt } from \"../../../utils/helpers/encrypt-decrypt.js\";\nimport type { ServiceResponse } from \"../../../utils/services/types.js\";\nimport { copy } from \"../../i18n/index.js\";\nimport { isRecord } from \"./paths.js\";\nimport type { EmailStorageEncryptedValue } from \"./types.js\";\n\nconst isEncryptedEmailStorageValue = (\n\tvalue: unknown,\n): value is EmailStorageEncryptedValue =>\n\tisRecord(value) &&\n\tvalue[constants.email.storage.encryptedValueMarker] === true &&\n\tvalue.version === constants.email.storage.encryptedValueVersion &&\n\ttypeof value.value === \"string\";\n\nexport const encryptEmailStorageValue = (\n\tvalue: unknown,\n\tencryptionKey: string,\n): Awaited<ServiceResponse<EmailStorageEncryptedValue>> => {\n\ttry {\n\t\treturn {\n\t\t\terror: undefined,\n\t\t\tdata: {\n\t\t\t\t[constants.email.storage.encryptedValueMarker]: true,\n\t\t\t\tversion: constants.email.storage.encryptedValueVersion,\n\t\t\t\tvalue: encrypt(JSON.stringify({ value }), encryptionKey),\n\t\t\t},\n\t\t};\n\t} catch (_) {\n\t\treturn {\n\t\t\terror: {\n\t\t\t\ttype: \"validation\",\n\t\t\t\tstatus: 400,\n\t\t\t\tmessage: copy(\"server:core.email.storage.encrypt.failed\"),\n\t\t\t},\n\t\t\tdata: undefined,\n\t\t};\n\t}\n};\n\nexport const decryptEmailStorageValue = (\n\tvalue: unknown,\n\tencryptionKey: string,\n): Awaited<ServiceResponse<unknown>> => {\n\tif (!isEncryptedEmailStorageValue(value)) {\n\t\treturn {\n\t\t\terror: undefined,\n\t\t\tdata: value,\n\t\t};\n\t}\n\n\ttry {\n\t\tconst decrypted = decrypt(value.value, encryptionKey);\n\t\treturn {\n\t\t\terror: undefined,\n\t\t\tdata: (JSON.parse(decrypted) as { value: unknown }).value,\n\t\t};\n\t} catch (_) {\n\t\treturn {\n\t\t\terror: {\n\t\t\t\ttype: \"validation\",\n\t\t\t\tstatus: 400,\n\t\t\t\tmessage: copy(\"server:core.email.storage.decrypt.failed\"),\n\t\t\t},\n\t\t\tdata: undefined,\n\t\t};\n\t}\n};\n"],"mappings":"oNAOA,MAAM,EACL,GAEA,EAAS,CAAK,GACd,EAAMA,EAAU,MAAM,QAAQ,wBAA0B,IACxD,EAAM,UAAYA,EAAU,MAAM,QAAQ,uBAC1C,OAAO,EAAM,OAAU,SAEX,GACZ,EACA,IAC0D,CAC1D,GAAI,CACH,MAAO,CACN,MAAO,IAAA,GACP,KAAM,EACJA,EAAU,MAAM,QAAQ,sBAAuB,GAChD,QAASA,EAAU,MAAM,QAAQ,sBACjC,MAAO,EAAQ,KAAK,UAAU,CAAE,OAAM,CAAC,EAAG,CAAa,CACxD,CACD,CACD,MAAY,CACX,MAAO,CACN,MAAO,CACN,KAAM,aACN,OAAQ,IACR,QAAS,EAAK,0CAA0C,CACzD,EACA,KAAM,IAAA,EACP,CACD,CACD,EAEa,GACZ,EACA,IACuC,CACvC,GAAI,CAAC,EAA6B,CAAK,EACtC,MAAO,CACN,MAAO,IAAA,GACP,KAAM,CACP,EAGD,GAAI,CACH,IAAM,EAAY,EAAQ,EAAM,MAAO,CAAa,EACpD,MAAO,CACN,MAAO,IAAA,GACP,KAAO,KAAK,MAAM,CAAS,CAAC,CAAwB,KACrD,CACD,MAAY,CACX,MAAO,CACN,MAAO,CACN,KAAM,aACN,OAAQ,IACR,QAAS,EAAK,0CAA0C,CACzD,EACA,KAAM,IAAA,EACP,CACD,CACD"}