From 35a3b294db090818d9e93124605b74445d2f8206 Mon Sep 17 00:00:00 2001 From: Samuel Denis-D'Ortun Date: Sun, 3 Oct 2021 23:05:48 -0400 Subject: [PATCH] Update Crypto/CryptoUtils --- src/{Crypto/index.ts => CryptoUtils.ts} | 2 +- src/index.ts | 3 ++- src/parse/Query.ts | 2 +- src/parse/SecureObject.ts | 14 +++++++------- 4 files changed, 11 insertions(+), 10 deletions(-) rename src/{Crypto/index.ts => CryptoUtils.ts} (99%) diff --git a/src/Crypto/index.ts b/src/CryptoUtils.ts similarity index 99% rename from src/Crypto/index.ts rename to src/CryptoUtils.ts index cd7681c..3d1c89e 100644 --- a/src/Crypto/index.ts +++ b/src/CryptoUtils.ts @@ -28,7 +28,7 @@ export interface DerivedKey { PBKDF2: JsonWebKey; } -export class Crypto { +export default class Crypto { static isEncrypted(value: { [key: string]: unknown }, strict = true): boolean { if (typeof value !== 'object' || value === undefined || value === null) { return false; diff --git a/src/index.ts b/src/index.ts index 89b2c96..2751e42 100644 --- a/src/index.ts +++ b/src/index.ts @@ -17,7 +17,7 @@ import { CacheableQuery } from './parse/CacheableQuery'; import Query, * as QueryUtils from './parse/Query'; import { SecureObject } from './parse/SecureObject'; import { User } from './parse/User'; -import * as CryptoUtils from './crypto'; +import Crypto , * as CryptoUtils from './CryptoUtils'; export { ArrayUtils, @@ -34,6 +34,7 @@ export { RegexUtils, StringUtils, CryptoUtils, + Crypto, // This will be moved in a separate 'parse-utils' repo later BaseObject, CacheableQuery, diff --git a/src/parse/Query.ts b/src/parse/Query.ts index 49b850b..e7181d9 100644 --- a/src/parse/Query.ts +++ b/src/parse/Query.ts @@ -69,7 +69,7 @@ export default class Query extends Parse.Query { return this; } - public includeAll(): this { + public includeAll(): this { return super.includeAll() as this; } diff --git a/src/parse/SecureObject.ts b/src/parse/SecureObject.ts index 6f647dc..d6717a4 100644 --- a/src/parse/SecureObject.ts +++ b/src/parse/SecureObject.ts @@ -1,4 +1,4 @@ -import { Crypto, DerivedKey, EncryptedValue } from '../Crypto'; +import { Crypto, CryptoUtils } from '../index'; import { BaseObject } from './BaseObject'; const perf = { @@ -8,7 +8,7 @@ const perf = { export abstract class SecureObject extends BaseObject { private static isServer = false; - private static sessionDerivedKey: DerivedKey; + private static sessionDerivedKey: CryptoUtils.DerivedKey; private readonly secureFields: string[] = []; private _decryptedReadCache: { [key: string]: unknown } = {}; @@ -24,7 +24,7 @@ export abstract class SecureObject extends BaseObject { this.secureFields = secureFields; } - static setSessionDerivedKey(derived: DerivedKey): void { + static setSessionDerivedKey(derived: CryptoUtils.DerivedKey): void { SecureObject.sessionDerivedKey = derived; } @@ -66,7 +66,7 @@ export abstract class SecureObject extends BaseObject { return clone; } - public static async decryptField(val: EncryptedValue): Promise { + public static async decryptField(val: CryptoUtils.EncryptedValue): Promise { const start = window.performance.now(); const decrypted = await Crypto.decrypt(SecureObject.sessionDerivedKey, val); @@ -76,11 +76,11 @@ export abstract class SecureObject extends BaseObject { return decrypted; } - public static async encryptField(val: T): Promise { - if (Crypto.isEncrypted(val as unknown as EncryptedValue)) { + public static async encryptField(val: T): Promise { + if (Crypto.isEncrypted(val as unknown as CryptoUtils.EncryptedValue)) { throw 'Already encrypted'; - return val as unknown as EncryptedValue; + return val as unknown as CryptoUtils.EncryptedValue; } if (val instanceof Parse.Object) {