From 7c0aea258b6445cc5cb156abbd8c22a6be1de547 Mon Sep 17 00:00:00 2001 From: Abhinav-grd Date: Thu, 1 Apr 2021 18:45:00 +0530 Subject: [PATCH 01/17] updated keyAttribute interface --- src/types.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/types.ts b/src/types.ts index 578c15d42..4b0eeafe4 100644 --- a/src/types.ts +++ b/src/types.ts @@ -4,6 +4,9 @@ export interface keyAttributes { keyDecryptionNonce: string; opsLimit: number; memLimit: number; + publicKey: string; + encryptedSecretKey: string; + secretKeyDecryptionNonce: string; } export const ENCRYPTION_CHUNK_SIZE = 4 * 1024 * 1024; From d5a416997a9d2fde2da050de060a7fc31f66ad17 Mon Sep 17 00:00:00 2001 From: Abhinav-grd Date: Thu, 1 Apr 2021 18:45:51 +0530 Subject: [PATCH 02/17] added deriveIntermediateKey crytpo function --- src/utils/crypto/libsodium.ts | 34 +++++++++++++++++++++++++++++----- src/worker/crypto.worker.js | 4 ++++ 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/src/utils/crypto/libsodium.ts b/src/utils/crypto/libsodium.ts index 80a8363d4..6920d3f34 100644 --- a/src/utils/crypto/libsodium.ts +++ b/src/utils/crypto/libsodium.ts @@ -254,7 +254,12 @@ export async function hash(input: string) { ); } -export async function deriveKey(passphrase: string, salt: string, opsLimit: number, memLimit: number) { +export async function deriveKey( + passphrase: string, + salt: string, + opsLimit: number, + memLimit: number +) { await sodium.ready; return await toB64( sodium.crypto_pwhash( @@ -281,10 +286,29 @@ export async function deriveSensitiveKey(passphrase: string, salt: string) { ) ); return { - 'key': key, - 'opsLimit': sodium.crypto_pwhash_OPSLIMIT_SENSITIVE, - 'memLimit': sodium.crypto_pwhash_MEMLIMIT_SENSITIVE, - } + key: key, + opsLimit: sodium.crypto_pwhash_OPSLIMIT_SENSITIVE, + memLimit: sodium.crypto_pwhash_MEMLIMIT_SENSITIVE, + }; +} + +export async function deriveIntermediateKey(passphrase: string, salt: string) { + await sodium.ready; + const key = await toB64( + sodium.crypto_pwhash( + sodium.crypto_secretbox_KEYBYTES, + await fromString(passphrase), + await fromB64(salt), + sodium.crypto_pwhash_OPSLIMIT_INTERACTIVE, + sodium.crypto_pwhash_MEMLIMIT_INTERACTIVE, + sodium.crypto_pwhash_ALG_DEFAULT + ) + ); + return { + key: key, + opsLimit: sodium.crypto_pwhash_OPSLIMIT_INTERACTIVE, + memLimit: sodium.crypto_pwhash_MEMLIMIT_INTERACTIVE, + }; } export async function generateMasterKey() { diff --git a/src/worker/crypto.worker.js b/src/worker/crypto.worker.js index 78599c22f..417d24d28 100644 --- a/src/worker/crypto.worker.js +++ b/src/worker/crypto.worker.js @@ -84,6 +84,10 @@ export class Crypto { return libsodium.deriveSensitiveKey(passphrase, salt); } + async deriveIntermediateKey(passphrase, salt) { + return libsodium.deriveIntermediateKey(passphrase, salt); + } + async decryptB64(data, nonce, key) { return libsodium.decryptB64(data, nonce, key); } From 3aa69efc322029f565b882c86aa2b6d1fdcb5e40 Mon Sep 17 00:00:00 2001 From: Abhinav-grd Date: Thu, 1 Apr 2021 22:08:51 +0530 Subject: [PATCH 03/17] generate Intermediate Key and store it in localDB --- src/pages/generate/index.tsx | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/src/pages/generate/index.tsx b/src/pages/generate/index.tsx index 367d31395..9b2a08ad1 100644 --- a/src/pages/generate/index.tsx +++ b/src/pages/generate/index.tsx @@ -90,7 +90,15 @@ export default function Generate() { getData(LS_KEYS.USER).name, keyAttributes ); - setData(LS_KEYS.KEY_ATTRIBUTES, keyAttributes); + + setData( + LS_KEYS.KEY_ATTRIBUTES, + await generateIntermediateKey( + passphrase, + keyAttributes, + key + ) + ); const sessionKeyAttributes = await cryptoWorker.encryptToB64( key @@ -113,6 +121,29 @@ export default function Generate() { setLoading(false); }; + async function generateIntermediateKey(passphrase, keyAttributes, key) { + const cryptoWorker = await new CryptoWorker(); + const intermediateKekSalt: string = await cryptoWorker.generateSaltToDeriveKey(); + const intermediateKek: KEK = await cryptoWorker.deriveIntermediateKey( + passphrase, + intermediateKekSalt + ); + const encryptedKeyAttributes: B64EncryptionResult = await cryptoWorker.encryptToB64( + key, + intermediateKek.key + ); + return { + intermediateKekSalt, + encryptedKey: encryptedKeyAttributes.encryptedData, + keyDecryptionNonce: encryptedKeyAttributes.nonce, + publicKey: keyAttributes.publicKey, + encryptedSecretKey: keyAttributes.encryptedSecretKey, + secretKeyDecryptionNonce: keyAttributes.secretKeyDecryptionNonce, + opsLimit: intermediateKek.opsLimit, + memLimit: intermediateKek.memLimit, + }; + } + return ( {/* vault */} From a59eb49f89735df5b2c730e84b234242aba6effd Mon Sep 17 00:00:00 2001 From: Abhinav-grd Date: Fri, 2 Apr 2021 09:26:21 +0530 Subject: [PATCH 04/17] moved generate intermediate key to crypto util --- src/pages/generate/index.tsx | 26 ++------------------------ src/utils/crypto/index.ts | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 24 deletions(-) create mode 100644 src/utils/crypto/index.ts diff --git a/src/pages/generate/index.tsx b/src/pages/generate/index.tsx index 9b2a08ad1..b5b8db3ef 100644 --- a/src/pages/generate/index.tsx +++ b/src/pages/generate/index.tsx @@ -13,6 +13,7 @@ import { useRouter } from 'next/router'; import { getKey, SESSION_KEYS, setKey } from 'utils/storage/sessionStorage'; import { B64EncryptionResult } from 'services/uploadService'; import CryptoWorker from 'utils/crypto/cryptoWorker'; +import { generateIntermediateKey } from 'utils/crypto'; const Image = styled.img` width: 200px; @@ -25,7 +26,7 @@ interface formValues { confirm: string; } -interface KEK { +export interface KEK { key: string; opsLimit: number; memLimit: number; @@ -121,29 +122,6 @@ export default function Generate() { setLoading(false); }; - async function generateIntermediateKey(passphrase, keyAttributes, key) { - const cryptoWorker = await new CryptoWorker(); - const intermediateKekSalt: string = await cryptoWorker.generateSaltToDeriveKey(); - const intermediateKek: KEK = await cryptoWorker.deriveIntermediateKey( - passphrase, - intermediateKekSalt - ); - const encryptedKeyAttributes: B64EncryptionResult = await cryptoWorker.encryptToB64( - key, - intermediateKek.key - ); - return { - intermediateKekSalt, - encryptedKey: encryptedKeyAttributes.encryptedData, - keyDecryptionNonce: encryptedKeyAttributes.nonce, - publicKey: keyAttributes.publicKey, - encryptedSecretKey: keyAttributes.encryptedSecretKey, - secretKeyDecryptionNonce: keyAttributes.secretKeyDecryptionNonce, - opsLimit: intermediateKek.opsLimit, - memLimit: intermediateKek.memLimit, - }; - } - return ( {/* vault */} diff --git a/src/utils/crypto/index.ts b/src/utils/crypto/index.ts new file mode 100644 index 000000000..d0c2696ec --- /dev/null +++ b/src/utils/crypto/index.ts @@ -0,0 +1,32 @@ +import { KEK } from 'pages/generate'; +import { B64EncryptionResult } from 'services/uploadService'; +import { KeyAttributes } from 'types'; +import CryptoWorker from './cryptoWorker'; + +export async function generateIntermediateKey( + passphrase, + keyAttributes, + key +): KeyAttributes { + const cryptoWorker = await new CryptoWorker(); + const intermediateKekSalt: string = await cryptoWorker.generateSaltToDeriveKey(); + const intermediateKek: KEK = await cryptoWorker.deriveIntermediateKey( + passphrase, + intermediateKekSalt + ); + const encryptedKeyAttributes: B64EncryptionResult = await cryptoWorker.encryptToB64( + key, + intermediateKek.key + ); + const x = { + kekSalt: intermediateKekSalt, + encryptedKey: encryptedKeyAttributes.encryptedData, + keyDecryptionNonce: encryptedKeyAttributes.nonce, + publicKey: keyAttributes.publicKey, + encryptedSecretKey: keyAttributes.encryptedSecretKey, + secretKeyDecryptionNonce: keyAttributes.secretKeyDecryptionNonce, + opsLimit: intermediateKek.opsLimit, + memLimit: intermediateKek.memLimit, + }; + return x; +} From 281fab1b5a543a85fd4f0ca6f4b1578a01c791bf Mon Sep 17 00:00:00 2001 From: Abhinav-grd Date: Fri, 2 Apr 2021 09:33:00 +0530 Subject: [PATCH 05/17] updated type name KeyAttribute --- src/pages/credentials/index.tsx | 8 ++++---- src/pages/verify/index.tsx | 1 - src/services/userService.ts | 4 ++-- src/types.ts | 2 +- src/utils/crypto/index.ts | 2 +- 5 files changed, 8 insertions(+), 9 deletions(-) diff --git a/src/pages/credentials/index.tsx b/src/pages/credentials/index.tsx index a443f9315..f4e4b46bd 100644 --- a/src/pages/credentials/index.tsx +++ b/src/pages/credentials/index.tsx @@ -9,7 +9,7 @@ import { Formik, FormikHelpers } from 'formik'; import { getData, LS_KEYS, setData } from 'utils/storage/localStorage'; import { useRouter } from 'next/router'; import * as Yup from 'yup'; -import { keyAttributes } from 'types'; +import { KeyAttributes } from 'types'; import { setKey, SESSION_KEYS, getKey } from 'utils/storage/sessionStorage'; import CryptoWorker from 'utils/crypto/cryptoWorker'; import { logoutUser } from 'services/userService'; @@ -26,7 +26,7 @@ interface formValues { export default function Credentials() { const router = useRouter(); - const [keyAttributes, setKeyAttributes] = useState(); + const [keyAttributes, setKeyAttributes] = useState(); const [loading, setLoading] = useState(false); useEffect(() => { @@ -57,7 +57,7 @@ export default function Credentials() { passphrase, keyAttributes.kekSalt, keyAttributes.opsLimit, - keyAttributes.memLimit, + keyAttributes.memLimit ); try { @@ -128,7 +128,7 @@ export default function Credentials() { onBlur={handleBlur('passphrase')} isInvalid={Boolean( touched.passphrase && - errors.passphrase + errors.passphrase )} disabled={loading} autoFocus={true} diff --git a/src/pages/verify/index.tsx b/src/pages/verify/index.tsx index 2be9e87d3..f08b89d02 100644 --- a/src/pages/verify/index.tsx +++ b/src/pages/verify/index.tsx @@ -41,7 +41,6 @@ export default function Verify() { if (!user?.email) { router.push('/'); } else if (user.token) { - console.log(user.token); if (await isTokenValid()) { router.push('/credentials'); } else { diff --git a/src/services/userService.ts b/src/services/userService.ts index 7cc4fdf47..b30e1612d 100644 --- a/src/services/userService.ts +++ b/src/services/userService.ts @@ -1,5 +1,5 @@ import HTTPService from './HTTPService'; -import { keyAttributes } from 'types'; +import { KeyAttributes } from 'types'; import { getEndpoint } from 'utils/common/apiUtil'; import { clearKeys } from 'utils/storage/sessionStorage'; import router from 'next/router'; @@ -29,7 +29,7 @@ export const verifyOtt = (email: string, ott: string) => { export const putAttributes = ( token: string, name: string, - keyAttributes: keyAttributes + keyAttributes: KeyAttributes ) => { console.log('name ' + name); return HTTPService.put( diff --git a/src/types.ts b/src/types.ts index 4b0eeafe4..3aed8354c 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,4 +1,4 @@ -export interface keyAttributes { +export interface KeyAttributes { kekSalt: string; encryptedKey: string; keyDecryptionNonce: string; diff --git a/src/utils/crypto/index.ts b/src/utils/crypto/index.ts index d0c2696ec..4142e4187 100644 --- a/src/utils/crypto/index.ts +++ b/src/utils/crypto/index.ts @@ -7,7 +7,7 @@ export async function generateIntermediateKey( passphrase, keyAttributes, key -): KeyAttributes { +): Promise { const cryptoWorker = await new CryptoWorker(); const intermediateKekSalt: string = await cryptoWorker.generateSaltToDeriveKey(); const intermediateKek: KEK = await cryptoWorker.deriveIntermediateKey( From 38ce8ae2bca88b3d4873353e95b86ddf0c66f783 Mon Sep 17 00:00:00 2001 From: Abhinav-grd Date: Fri, 2 Apr 2021 09:51:42 +0530 Subject: [PATCH 06/17] added new local record for isFirstLogin --- src/utils/common/utilFunctions.ts | 7 +++++++ src/utils/storage/localStorage.ts | 1 + 2 files changed, 8 insertions(+) diff --git a/src/utils/common/utilFunctions.ts b/src/utils/common/utilFunctions.ts index 7aafba158..c77747a0c 100644 --- a/src/utils/common/utilFunctions.ts +++ b/src/utils/common/utilFunctions.ts @@ -1,3 +1,4 @@ +import { getData, LS_KEYS, setData } from 'utils/storage/localStorage'; import { errorCodes } from './errorUtil'; export function checkConnectivity() { @@ -15,3 +16,9 @@ export function getFileExtension(fileName): string { export function runningInBrowser() { return typeof window !== 'undefined'; } + +export const isFirstLogin = getData(LS_KEYS.IS_FIRST_LOGIN)?.status; + +export function setIsFirstLogin(status) { + setData(LS_KEYS.IS_FIRST_LOGIN, { status }); +} diff --git a/src/utils/storage/localStorage.ts b/src/utils/storage/localStorage.ts index c1b973f45..c30164b27 100644 --- a/src/utils/storage/localStorage.ts +++ b/src/utils/storage/localStorage.ts @@ -3,6 +3,7 @@ export enum LS_KEYS { SESSION = 'session', KEY_ATTRIBUTES = 'keyAttributes', SUBSCRIPTION = 'subscription', + IS_FIRST_LOGIN = 'isFirstLogin', } export const setData = (key: LS_KEYS, value: object) => { From 6f0fb87c6b40a8ab8cb92818f2378caa61ba53ed Mon Sep 17 00:00:00 2001 From: Abhinav-grd Date: Fri, 2 Apr 2021 09:56:25 +0530 Subject: [PATCH 07/17] check setIsFirstLogin true on verify and generateIntermediateKey if isFirstLogin is true --- src/pages/credentials/index.tsx | 11 +++++++++++ src/pages/verify/index.tsx | 2 ++ 2 files changed, 13 insertions(+) diff --git a/src/pages/credentials/index.tsx b/src/pages/credentials/index.tsx index f4e4b46bd..b44457e0c 100644 --- a/src/pages/credentials/index.tsx +++ b/src/pages/credentials/index.tsx @@ -13,6 +13,8 @@ import { KeyAttributes } from 'types'; import { setKey, SESSION_KEYS, getKey } from 'utils/storage/sessionStorage'; import CryptoWorker from 'utils/crypto/cryptoWorker'; import { logoutUser } from 'services/userService'; +import { isFirstLogin, setIsFirstLogin } from 'utils/common/utilFunctions'; +import { generateIntermediateKey } from 'utils/crypto'; const Image = styled.img` width: 200px; @@ -66,6 +68,15 @@ export default function Credentials() { keyAttributes.keyDecryptionNonce, kek ); + if (isFirstLogin) { + const intermediateKeyAttribute = await generateIntermediateKey( + passphrase, + keyAttributes, + key + ); + setData(LS_KEYS.KEY_ATTRIBUTES, intermediateKeyAttribute); + setIsFirstLogin(false); + } const sessionKeyAttributes = await cryptoWorker.encryptToB64( key ); diff --git a/src/pages/verify/index.tsx b/src/pages/verify/index.tsx index f08b89d02..7c063e137 100644 --- a/src/pages/verify/index.tsx +++ b/src/pages/verify/index.tsx @@ -16,6 +16,7 @@ import { clearFiles, isTokenValid, } from 'services/userService'; +import { setIsFirstLogin } from 'utils/common/utilFunctions'; const Image = styled.img` width: 350px; @@ -70,6 +71,7 @@ export default function Verify() { keyAttributes && setData(LS_KEYS.KEY_ATTRIBUTES, keyAttributes); subscription && setData(LS_KEYS.SUBSCRIPTION, subscription); clearFiles(); + setIsFirstLogin(true); if (resp.data.keyAttributes?.encryptedKey) { router.push('/credentials'); } else { From d22606636ef746f389e523ef247c7698f90b0371 Mon Sep 17 00:00:00 2001 From: Abhinav-grd Date: Fri, 2 Apr 2021 10:19:57 +0530 Subject: [PATCH 08/17] updated isFirstLogin to function to get Fresh values --- src/pages/credentials/index.tsx | 2 +- src/utils/common/utilFunctions.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/pages/credentials/index.tsx b/src/pages/credentials/index.tsx index b44457e0c..55f7aa81f 100644 --- a/src/pages/credentials/index.tsx +++ b/src/pages/credentials/index.tsx @@ -68,7 +68,7 @@ export default function Credentials() { keyAttributes.keyDecryptionNonce, kek ); - if (isFirstLogin) { + if (isFirstLogin()) { const intermediateKeyAttribute = await generateIntermediateKey( passphrase, keyAttributes, diff --git a/src/utils/common/utilFunctions.ts b/src/utils/common/utilFunctions.ts index c77747a0c..fef2d22ea 100644 --- a/src/utils/common/utilFunctions.ts +++ b/src/utils/common/utilFunctions.ts @@ -17,7 +17,7 @@ export function runningInBrowser() { return typeof window !== 'undefined'; } -export const isFirstLogin = getData(LS_KEYS.IS_FIRST_LOGIN)?.status; +export const isFirstLogin = () => getData(LS_KEYS.IS_FIRST_LOGIN)?.status; export function setIsFirstLogin(status) { setData(LS_KEYS.IS_FIRST_LOGIN, { status }); From 7dfcd528119f9513391e762c8b3720cccf167fa8 Mon Sep 17 00:00:00 2001 From: Abhinav-grd Date: Fri, 2 Apr 2021 10:22:45 +0530 Subject: [PATCH 09/17] removed temp variable --- src/utils/crypto/index.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/utils/crypto/index.ts b/src/utils/crypto/index.ts index 4142e4187..859f1be92 100644 --- a/src/utils/crypto/index.ts +++ b/src/utils/crypto/index.ts @@ -18,7 +18,7 @@ export async function generateIntermediateKey( key, intermediateKek.key ); - const x = { + return { kekSalt: intermediateKekSalt, encryptedKey: encryptedKeyAttributes.encryptedData, keyDecryptionNonce: encryptedKeyAttributes.nonce, @@ -28,5 +28,4 @@ export async function generateIntermediateKey( opsLimit: intermediateKek.opsLimit, memLimit: intermediateKek.memLimit, }; - return x; } From b89d0d0b960fe26a697600bdc854e841d407240e Mon Sep 17 00:00:00 2001 From: Abhinav-grd Date: Sat, 3 Apr 2021 09:45:25 +0530 Subject: [PATCH 10/17] renamed variables --- src/pages/credentials/index.tsx | 6 +++--- src/pages/generate/index.tsx | 4 ++-- src/utils/crypto/index.ts | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/pages/credentials/index.tsx b/src/pages/credentials/index.tsx index 55f7aa81f..3b168f26d 100644 --- a/src/pages/credentials/index.tsx +++ b/src/pages/credentials/index.tsx @@ -14,7 +14,7 @@ import { setKey, SESSION_KEYS, getKey } from 'utils/storage/sessionStorage'; import CryptoWorker from 'utils/crypto/cryptoWorker'; import { logoutUser } from 'services/userService'; import { isFirstLogin, setIsFirstLogin } from 'utils/common/utilFunctions'; -import { generateIntermediateKey } from 'utils/crypto'; +import { generateIntermediateKeyAttributes } from 'utils/crypto'; const Image = styled.img` width: 200px; @@ -69,12 +69,12 @@ export default function Credentials() { kek ); if (isFirstLogin()) { - const intermediateKeyAttribute = await generateIntermediateKey( + const intermediateKeyAttributes = await generateIntermediateKeyAttributes( passphrase, keyAttributes, key ); - setData(LS_KEYS.KEY_ATTRIBUTES, intermediateKeyAttribute); + setData(LS_KEYS.KEY_ATTRIBUTES, intermediateKeyAttributes); setIsFirstLogin(false); } const sessionKeyAttributes = await cryptoWorker.encryptToB64( diff --git a/src/pages/generate/index.tsx b/src/pages/generate/index.tsx index b5b8db3ef..af4daaffc 100644 --- a/src/pages/generate/index.tsx +++ b/src/pages/generate/index.tsx @@ -13,7 +13,7 @@ import { useRouter } from 'next/router'; import { getKey, SESSION_KEYS, setKey } from 'utils/storage/sessionStorage'; import { B64EncryptionResult } from 'services/uploadService'; import CryptoWorker from 'utils/crypto/cryptoWorker'; -import { generateIntermediateKey } from 'utils/crypto'; +import { generateIntermediateKeyAttributes } from 'utils/crypto'; const Image = styled.img` width: 200px; @@ -94,7 +94,7 @@ export default function Generate() { setData( LS_KEYS.KEY_ATTRIBUTES, - await generateIntermediateKey( + await generateIntermediateKeyAttributes( passphrase, keyAttributes, key diff --git a/src/utils/crypto/index.ts b/src/utils/crypto/index.ts index 859f1be92..514385bac 100644 --- a/src/utils/crypto/index.ts +++ b/src/utils/crypto/index.ts @@ -3,7 +3,7 @@ import { B64EncryptionResult } from 'services/uploadService'; import { KeyAttributes } from 'types'; import CryptoWorker from './cryptoWorker'; -export async function generateIntermediateKey( +export async function generateIntermediateKeyAttributes( passphrase, keyAttributes, key From 6c7de83a5f82018a532bc13cab0bdd9c88c2b8e2 Mon Sep 17 00:00:00 2001 From: Abhinav-grd Date: Sat, 3 Apr 2021 09:52:57 +0530 Subject: [PATCH 11/17] moved setting first login false logic to gallery --- src/pages/credentials/index.tsx | 1 - src/pages/gallery/index.tsx | 9 +++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/pages/credentials/index.tsx b/src/pages/credentials/index.tsx index 3b168f26d..6e9ae6fd1 100644 --- a/src/pages/credentials/index.tsx +++ b/src/pages/credentials/index.tsx @@ -75,7 +75,6 @@ export default function Credentials() { key ); setData(LS_KEYS.KEY_ATTRIBUTES, intermediateKeyAttributes); - setIsFirstLogin(false); } const sessionKeyAttributes = await cryptoWorker.encryptToB64( key diff --git a/src/pages/gallery/index.tsx b/src/pages/gallery/index.tsx index 1745bdabc..602521afe 100644 --- a/src/pages/gallery/index.tsx +++ b/src/pages/gallery/index.tsx @@ -29,7 +29,11 @@ import ConfirmDialog, { CONFIRM_ACTION } from 'components/ConfirmDialog'; import FullScreenDropZone from 'components/FullScreenDropZone'; import Sidebar from 'components/Sidebar'; import UploadButton from './components/UploadButton'; -import { checkConnectivity } from 'utils/common/utilFunctions'; +import { + checkConnectivity, + isFirstLogin, + setIsFirstLogin, +} from 'utils/common/utilFunctions'; import { logoutUser } from 'services/userService'; const DATE_CONTAINER_HEIGHT = 45; const IMAGE_CONTAINER_HEIGHT = 200; @@ -170,7 +174,8 @@ export default function Gallery(props: Props) { return; } const main = async () => { - setIsFirstLoad((await getCollectionUpdationTime()) == 0); + setIsFirstLoad(isFirstLogin()); + setIsFirstLogin(false); const data = await localFiles(); const collections = await getLocalCollections(); const nonEmptyCollections = getNonEmptyCollections( From 6a216f4e0f66c44e3ef0958033859981af9299b9 Mon Sep 17 00:00:00 2001 From: Abhinav-grd Date: Sat, 3 Apr 2021 09:53:16 +0530 Subject: [PATCH 12/17] set isFirstLogin defaukt value to false --- src/utils/common/utilFunctions.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/utils/common/utilFunctions.ts b/src/utils/common/utilFunctions.ts index fef2d22ea..22471f618 100644 --- a/src/utils/common/utilFunctions.ts +++ b/src/utils/common/utilFunctions.ts @@ -17,7 +17,8 @@ export function runningInBrowser() { return typeof window !== 'undefined'; } -export const isFirstLogin = () => getData(LS_KEYS.IS_FIRST_LOGIN)?.status; +export const isFirstLogin = () => + getData(LS_KEYS.IS_FIRST_LOGIN)?.status ?? false; export function setIsFirstLogin(status) { setData(LS_KEYS.IS_FIRST_LOGIN, { status }); From cddff63a3156fcde7e238300a08b93dda8248688 Mon Sep 17 00:00:00 2001 From: Abhinav-grd Date: Sat, 3 Apr 2021 10:06:15 +0530 Subject: [PATCH 13/17] moved crytoWorker to util/cryto/index --- src/pages/credentials/index.tsx | 2 +- src/pages/generate/index.tsx | 2 +- src/services/collectionService.ts | 2 +- src/services/downloadManager.ts | 4 ++-- src/services/fileService.ts | 2 +- src/services/uploadService.ts | 2 +- src/utils/common/key.ts | 2 +- src/utils/crypto/cryptoWorker.ts | 8 -------- src/utils/crypto/index.ts | 9 ++++++++- 9 files changed, 16 insertions(+), 17 deletions(-) delete mode 100644 src/utils/crypto/cryptoWorker.ts diff --git a/src/pages/credentials/index.tsx b/src/pages/credentials/index.tsx index 6e9ae6fd1..7c4f7a536 100644 --- a/src/pages/credentials/index.tsx +++ b/src/pages/credentials/index.tsx @@ -11,7 +11,7 @@ import { useRouter } from 'next/router'; import * as Yup from 'yup'; import { KeyAttributes } from 'types'; import { setKey, SESSION_KEYS, getKey } from 'utils/storage/sessionStorage'; -import CryptoWorker from 'utils/crypto/cryptoWorker'; +import CryptoWorker from 'utils/crypto'; import { logoutUser } from 'services/userService'; import { isFirstLogin, setIsFirstLogin } from 'utils/common/utilFunctions'; import { generateIntermediateKeyAttributes } from 'utils/crypto'; diff --git a/src/pages/generate/index.tsx b/src/pages/generate/index.tsx index af4daaffc..ab263deaf 100644 --- a/src/pages/generate/index.tsx +++ b/src/pages/generate/index.tsx @@ -12,7 +12,7 @@ import { getData, LS_KEYS, setData } from 'utils/storage/localStorage'; import { useRouter } from 'next/router'; import { getKey, SESSION_KEYS, setKey } from 'utils/storage/sessionStorage'; import { B64EncryptionResult } from 'services/uploadService'; -import CryptoWorker from 'utils/crypto/cryptoWorker'; +import CryptoWorker from 'utils/crypto'; import { generateIntermediateKeyAttributes } from 'utils/crypto'; const Image = styled.img` diff --git a/src/services/collectionService.ts b/src/services/collectionService.ts index 94d4ac21e..0c2f2d402 100644 --- a/src/services/collectionService.ts +++ b/src/services/collectionService.ts @@ -7,7 +7,7 @@ import HTTPService from './HTTPService'; import { B64EncryptionResult } from './uploadService'; import { getActualKey, getToken } from 'utils/common/key'; import { user } from './userService'; -import CryptoWorker from 'utils/crypto/cryptoWorker'; +import CryptoWorker from 'utils/crypto'; import { ErrorHandler } from 'utils/common/errorUtil'; const ENDPOINT = getEndpoint(); diff --git a/src/services/downloadManager.ts b/src/services/downloadManager.ts index 592c406d0..ed477797e 100644 --- a/src/services/downloadManager.ts +++ b/src/services/downloadManager.ts @@ -1,9 +1,9 @@ import { getToken } from 'utils/common/key'; import { file } from './fileService'; import HTTPService from './HTTPService'; -import { getEndpoint, getFileUrl, getThumbnailUrl } from 'utils/common/apiUtil'; +import { getFileUrl, getThumbnailUrl } from 'utils/common/apiUtil'; import { getFileExtension, runningInBrowser } from 'utils/common/utilFunctions'; -import CryptoWorker from 'utils/crypto/cryptoWorker'; +import CryptoWorker from 'utils/crypto'; const TYPE_HEIC = 'heic'; diff --git a/src/services/fileService.ts b/src/services/fileService.ts index be5c39fe9..cba72bee6 100644 --- a/src/services/fileService.ts +++ b/src/services/fileService.ts @@ -4,7 +4,7 @@ import localForage from 'utils/storage/localForage'; import { collection } from './collectionService'; import { DataStream, MetadataObject } from './uploadService'; -import CryptoWorker from 'utils/crypto/cryptoWorker'; +import CryptoWorker from 'utils/crypto'; import { getToken } from 'utils/common/key'; import { selectedState } from 'pages/gallery'; import { ErrorHandler } from 'utils/common/errorUtil'; diff --git a/src/services/uploadService.ts b/src/services/uploadService.ts index 8c03ee66f..fefdd9bd3 100644 --- a/src/services/uploadService.ts +++ b/src/services/uploadService.ts @@ -6,7 +6,7 @@ import { collection } from './collectionService'; import { FILE_TYPE } from 'pages/gallery'; import { checkConnectivity } from 'utils/common/utilFunctions'; import { ErrorHandler } from 'utils/common/errorUtil'; -import CryptoWorker from 'utils/crypto/cryptoWorker'; +import CryptoWorker from 'utils/crypto'; import * as convert from 'xml-js'; import { ENCRYPTION_CHUNK_SIZE } from 'types'; import { getToken } from 'utils/common/key'; diff --git a/src/utils/common/key.ts b/src/utils/common/key.ts index eab610eda..95689abf3 100644 --- a/src/utils/common/key.ts +++ b/src/utils/common/key.ts @@ -1,4 +1,4 @@ -import CryptoWorker from 'utils/crypto/cryptoWorker'; +import CryptoWorker from 'utils/crypto'; import { getData, LS_KEYS } from 'utils/storage/localStorage'; import { getKey, SESSION_KEYS } from 'utils/storage/sessionStorage'; diff --git a/src/utils/crypto/cryptoWorker.ts b/src/utils/crypto/cryptoWorker.ts deleted file mode 100644 index b20146e37..000000000 --- a/src/utils/crypto/cryptoWorker.ts +++ /dev/null @@ -1,8 +0,0 @@ -import * as Comlink from 'comlink'; -import { runningInBrowser } from 'utils/common/utilFunctions'; - -const CryptoWorker: any = - runningInBrowser() && - Comlink.wrap(new Worker('worker/crypto.worker.js', { type: 'module' })); - -export default CryptoWorker; diff --git a/src/utils/crypto/index.ts b/src/utils/crypto/index.ts index 514385bac..e4de0e2c3 100644 --- a/src/utils/crypto/index.ts +++ b/src/utils/crypto/index.ts @@ -1,7 +1,12 @@ import { KEK } from 'pages/generate'; import { B64EncryptionResult } from 'services/uploadService'; import { KeyAttributes } from 'types'; -import CryptoWorker from './cryptoWorker'; +import * as Comlink from 'comlink'; +import { runningInBrowser } from 'utils/common/utilFunctions'; + +const CryptoWorker: any = + runningInBrowser() && + Comlink.wrap(new Worker('worker/crypto.worker.js', { type: 'module' })); export async function generateIntermediateKeyAttributes( passphrase, @@ -29,3 +34,5 @@ export async function generateIntermediateKeyAttributes( memLimit: intermediateKek.memLimit, }; } + +export default CryptoWorker; From d45a12ceae6bc13829fc200a28e12846299e4a47 Mon Sep 17 00:00:00 2001 From: Abhinav-grd Date: Sat, 3 Apr 2021 10:13:13 +0530 Subject: [PATCH 14/17] renamed utilFunctions tpoindex and corrected imports --- src/pages/credentials/index.tsx | 2 +- src/pages/gallery/index.tsx | 6 +----- src/pages/verify/index.tsx | 2 +- src/services/downloadManager.ts | 2 +- src/services/uploadService.ts | 2 +- src/utils/common/{utilFunctions.ts => index.ts} | 0 src/utils/crypto/index.ts | 2 +- src/utils/storage/localForage.ts | 2 +- src/utils/strings/vernacularStrings.ts | 2 +- 9 files changed, 8 insertions(+), 12 deletions(-) rename src/utils/common/{utilFunctions.ts => index.ts} (100%) diff --git a/src/pages/credentials/index.tsx b/src/pages/credentials/index.tsx index 7c4f7a536..9c81883e5 100644 --- a/src/pages/credentials/index.tsx +++ b/src/pages/credentials/index.tsx @@ -13,7 +13,7 @@ import { KeyAttributes } from 'types'; import { setKey, SESSION_KEYS, getKey } from 'utils/storage/sessionStorage'; import CryptoWorker from 'utils/crypto'; import { logoutUser } from 'services/userService'; -import { isFirstLogin, setIsFirstLogin } from 'utils/common/utilFunctions'; +import { isFirstLogin } from 'utils/common'; import { generateIntermediateKeyAttributes } from 'utils/crypto'; const Image = styled.img` diff --git a/src/pages/gallery/index.tsx b/src/pages/gallery/index.tsx index 602521afe..31079faef 100644 --- a/src/pages/gallery/index.tsx +++ b/src/pages/gallery/index.tsx @@ -29,11 +29,7 @@ import ConfirmDialog, { CONFIRM_ACTION } from 'components/ConfirmDialog'; import FullScreenDropZone from 'components/FullScreenDropZone'; import Sidebar from 'components/Sidebar'; import UploadButton from './components/UploadButton'; -import { - checkConnectivity, - isFirstLogin, - setIsFirstLogin, -} from 'utils/common/utilFunctions'; +import { checkConnectivity, isFirstLogin, setIsFirstLogin } from 'utils/common'; import { logoutUser } from 'services/userService'; const DATE_CONTAINER_HEIGHT = 45; const IMAGE_CONTAINER_HEIGHT = 200; diff --git a/src/pages/verify/index.tsx b/src/pages/verify/index.tsx index 7c063e137..ee96e0c96 100644 --- a/src/pages/verify/index.tsx +++ b/src/pages/verify/index.tsx @@ -16,7 +16,7 @@ import { clearFiles, isTokenValid, } from 'services/userService'; -import { setIsFirstLogin } from 'utils/common/utilFunctions'; +import { setIsFirstLogin } from 'utils/common'; const Image = styled.img` width: 350px; diff --git a/src/services/downloadManager.ts b/src/services/downloadManager.ts index ed477797e..0dd22c01c 100644 --- a/src/services/downloadManager.ts +++ b/src/services/downloadManager.ts @@ -2,7 +2,7 @@ import { getToken } from 'utils/common/key'; import { file } from './fileService'; import HTTPService from './HTTPService'; import { getFileUrl, getThumbnailUrl } from 'utils/common/apiUtil'; -import { getFileExtension, runningInBrowser } from 'utils/common/utilFunctions'; +import { getFileExtension, runningInBrowser } from 'utils/common'; import CryptoWorker from 'utils/crypto'; const TYPE_HEIC = 'heic'; diff --git a/src/services/uploadService.ts b/src/services/uploadService.ts index fefdd9bd3..088fc5ff6 100644 --- a/src/services/uploadService.ts +++ b/src/services/uploadService.ts @@ -4,7 +4,7 @@ import EXIF from 'exif-js'; import { fileAttribute } from './fileService'; import { collection } from './collectionService'; import { FILE_TYPE } from 'pages/gallery'; -import { checkConnectivity } from 'utils/common/utilFunctions'; +import { checkConnectivity } from 'utils/common'; import { ErrorHandler } from 'utils/common/errorUtil'; import CryptoWorker from 'utils/crypto'; import * as convert from 'xml-js'; diff --git a/src/utils/common/utilFunctions.ts b/src/utils/common/index.ts similarity index 100% rename from src/utils/common/utilFunctions.ts rename to src/utils/common/index.ts diff --git a/src/utils/crypto/index.ts b/src/utils/crypto/index.ts index e4de0e2c3..6df2115f0 100644 --- a/src/utils/crypto/index.ts +++ b/src/utils/crypto/index.ts @@ -2,7 +2,7 @@ import { KEK } from 'pages/generate'; import { B64EncryptionResult } from 'services/uploadService'; import { KeyAttributes } from 'types'; import * as Comlink from 'comlink'; -import { runningInBrowser } from 'utils/common/utilFunctions'; +import { runningInBrowser } from 'utils/common'; const CryptoWorker: any = runningInBrowser() && diff --git a/src/utils/storage/localForage.ts b/src/utils/storage/localForage.ts index 3b1792e51..d4946fef9 100644 --- a/src/utils/storage/localForage.ts +++ b/src/utils/storage/localForage.ts @@ -1,4 +1,4 @@ -import { runningInBrowser } from 'utils/common/utilFunctions'; +import { runningInBrowser } from 'utils/common'; const localForage: LocalForage = runningInBrowser() && require('localforage'); if (runningInBrowser()) { diff --git a/src/utils/strings/vernacularStrings.ts b/src/utils/strings/vernacularStrings.ts index 15d198fda..1dc063ac3 100644 --- a/src/utils/strings/vernacularStrings.ts +++ b/src/utils/strings/vernacularStrings.ts @@ -1,4 +1,4 @@ -import { runningInBrowser } from 'utils/common/utilFunctions'; +import { runningInBrowser } from 'utils/common'; import englishConstants from './englishConstants'; /** Enums of supported locale */ From c6139bd05b292b49d8bea779706c899bb7a9f8d7 Mon Sep 17 00:00:00 2001 From: Abhinav-grd Date: Sat, 3 Apr 2021 10:19:37 +0530 Subject: [PATCH 15/17] moved isFirstLogin and setIsFIrstLogin to util/storage --- src/pages/credentials/index.tsx | 5 ++--- src/pages/gallery/index.tsx | 3 ++- src/pages/verify/index.tsx | 2 +- src/utils/common/index.ts | 7 ------- src/utils/storage/index.ts | 8 ++++++++ 5 files changed, 13 insertions(+), 12 deletions(-) create mode 100644 src/utils/storage/index.ts diff --git a/src/pages/credentials/index.tsx b/src/pages/credentials/index.tsx index 9c81883e5..b9b70c0b6 100644 --- a/src/pages/credentials/index.tsx +++ b/src/pages/credentials/index.tsx @@ -11,10 +11,9 @@ import { useRouter } from 'next/router'; import * as Yup from 'yup'; import { KeyAttributes } from 'types'; import { setKey, SESSION_KEYS, getKey } from 'utils/storage/sessionStorage'; -import CryptoWorker from 'utils/crypto'; +import CryptoWorker, { generateIntermediateKeyAttributes } from 'utils/crypto'; import { logoutUser } from 'services/userService'; -import { isFirstLogin } from 'utils/common'; -import { generateIntermediateKeyAttributes } from 'utils/crypto'; +import { isFirstLogin } from 'utils/storage'; const Image = styled.img` width: 200px; diff --git a/src/pages/gallery/index.tsx b/src/pages/gallery/index.tsx index 31079faef..a5964b132 100644 --- a/src/pages/gallery/index.tsx +++ b/src/pages/gallery/index.tsx @@ -29,7 +29,8 @@ import ConfirmDialog, { CONFIRM_ACTION } from 'components/ConfirmDialog'; import FullScreenDropZone from 'components/FullScreenDropZone'; import Sidebar from 'components/Sidebar'; import UploadButton from './components/UploadButton'; -import { checkConnectivity, isFirstLogin, setIsFirstLogin } from 'utils/common'; +import { checkConnectivity } from 'utils/common'; +import { isFirstLogin, setIsFirstLogin } from 'utils/storage'; import { logoutUser } from 'services/userService'; const DATE_CONTAINER_HEIGHT = 45; const IMAGE_CONTAINER_HEIGHT = 200; diff --git a/src/pages/verify/index.tsx b/src/pages/verify/index.tsx index ee96e0c96..5ae4452ca 100644 --- a/src/pages/verify/index.tsx +++ b/src/pages/verify/index.tsx @@ -16,7 +16,7 @@ import { clearFiles, isTokenValid, } from 'services/userService'; -import { setIsFirstLogin } from 'utils/common'; +import { setIsFirstLogin } from 'utils/storage'; const Image = styled.img` width: 350px; diff --git a/src/utils/common/index.ts b/src/utils/common/index.ts index 22471f618..77f30c5ac 100644 --- a/src/utils/common/index.ts +++ b/src/utils/common/index.ts @@ -16,10 +16,3 @@ export function getFileExtension(fileName): string { export function runningInBrowser() { return typeof window !== 'undefined'; } - -export const isFirstLogin = () => - getData(LS_KEYS.IS_FIRST_LOGIN)?.status ?? false; - -export function setIsFirstLogin(status) { - setData(LS_KEYS.IS_FIRST_LOGIN, { status }); -} diff --git a/src/utils/storage/index.ts b/src/utils/storage/index.ts new file mode 100644 index 000000000..65eaf7326 --- /dev/null +++ b/src/utils/storage/index.ts @@ -0,0 +1,8 @@ +import { getData, LS_KEYS, setData } from './localStorage'; + +export const isFirstLogin = () => + getData(LS_KEYS.IS_FIRST_LOGIN)?.status ?? false; + +export function setIsFirstLogin(status) { + setData(LS_KEYS.IS_FIRST_LOGIN, { status }); +} From 803ed2df9addbc6868f0a8998a42a0129cee1905 Mon Sep 17 00:00:00 2001 From: Abhinav-grd Date: Sat, 3 Apr 2021 10:22:37 +0530 Subject: [PATCH 16/17] moved sentry util to folder --- src/utils/{sentry.ts => sentry/index.ts} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/utils/{sentry.ts => sentry/index.ts} (100%) diff --git a/src/utils/sentry.ts b/src/utils/sentry/index.ts similarity index 100% rename from src/utils/sentry.ts rename to src/utils/sentry/index.ts From 6c97981e35eb9aa3948f53ae443bbcf43f32ba95 Mon Sep 17 00:00:00 2001 From: Abhinav-grd Date: Sat, 3 Apr 2021 10:30:37 +0530 Subject: [PATCH 17/17] added spinner on button during load --- src/pages/credentials/index.tsx | 7 ++++++- src/pages/generate/index.tsx | 7 ++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/pages/credentials/index.tsx b/src/pages/credentials/index.tsx index b9b70c0b6..4da3e5ef0 100644 --- a/src/pages/credentials/index.tsx +++ b/src/pages/credentials/index.tsx @@ -14,6 +14,7 @@ import { setKey, SESSION_KEYS, getKey } from 'utils/storage/sessionStorage'; import CryptoWorker, { generateIntermediateKeyAttributes } from 'utils/crypto'; import { logoutUser } from 'services/userService'; import { isFirstLogin } from 'utils/storage'; +import { Spinner } from 'react-bootstrap'; const Image = styled.img` width: 200px; @@ -147,7 +148,11 @@ export default function Credentials() {
diff --git a/src/pages/generate/index.tsx b/src/pages/generate/index.tsx index 1b6693484..51461b049 100644 --- a/src/pages/generate/index.tsx +++ b/src/pages/generate/index.tsx @@ -14,6 +14,7 @@ import { getKey, SESSION_KEYS, setKey } from 'utils/storage/sessionStorage'; import { B64EncryptionResult } from 'services/uploadService'; import CryptoWorker from 'utils/crypto'; import { generateIntermediateKeyAttributes } from 'utils/crypto'; +import { Spinner } from 'react-bootstrap'; const Image = styled.img` width: 200px; @@ -205,7 +206,11 @@ export default function Generate() { disabled={loading} style={{ marginTop: '28px' }} > - {constants.SET_PASSPHRASE} + {loading ? ( + + ) : ( + constants.SET_PASSPHRASE + )} )}