diff --git a/src/pages/generate/index.tsx b/src/pages/generate/index.tsx index 8a72193c9..e68656b89 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 * as Comlink from 'comlink'; -import { KeyEncryptionResult } from 'services/uploadService'; +import { B64EncryptionResult } from 'services/uploadService'; const CryptoWorker: any = typeof window !== 'undefined' && @@ -63,12 +63,12 @@ export default function Generate() { kekSalt ); const kekHash: string = await cryptoWorker.hash(kek); - const encryptedKeyAttributes: KeyEncryptionResult = await cryptoWorker.encryptToB64( + const encryptedKeyAttributes: B64EncryptionResult = await cryptoWorker.encryptToB64( key, kek ); const keyPair = await cryptoWorker.generateKeyPair(); - const encryptedKeyPairAttributes: KeyEncryptionResult = await cryptoWorker.encryptToB64( + const encryptedKeyPairAttributes: B64EncryptionResult = await cryptoWorker.encryptToB64( keyPair.privateKey, key ); diff --git a/src/services/collectionService.ts b/src/services/collectionService.ts index 4fbea94eb..30dbce9e8 100644 --- a/src/services/collectionService.ts +++ b/src/services/collectionService.ts @@ -1,12 +1,13 @@ import { getEndpoint } from 'utils/common/apiUtil'; import { getData, LS_KEYS } from 'utils/storage/localStorage'; -import { file, user, getFiles } from './fileService'; +import { file } from './fileService'; import localForage from 'localforage'; import HTTPService from './HTTPService'; import * as Comlink from 'comlink'; -import { KeyEncryptionResult } from './uploadService'; +import { B64EncryptionResult } from './uploadService'; import { getActualKey, getToken } from 'utils/common/key'; +import { user } from './userService'; const CryptoWorker: any = typeof window !== 'undefined' && @@ -77,7 +78,7 @@ const getCollectionSecrets = async ( } collection.name = collection.name || - (await worker.decryptString( + (await worker.decryptToUTF8( collection.encryptedName, collection.nameDecryptionNonce, decryptedKey @@ -122,7 +123,6 @@ export const syncCollections = async (token: string, key: string) => { (await localForage.getItem(COLLECTION_UPDATION_TIME)) ?? '0'; const updatedCollections = (await getCollections(token, lastCollectionUpdationTime, key)) || []; - if (updatedCollections.length == 0) { return localCollections; } @@ -152,6 +152,7 @@ export const syncCollections = async (token: string, key: string) => { updationTime = Math.max(updationTime, collection.updationTime); } } + collections.sort((a, b) => b.updationTime - a.updationTime); await localForage.setItem(COLLECTION_UPDATION_TIME, updationTime); await localForage.setItem(COLLECTIONS, collections); return collections; @@ -164,19 +165,24 @@ export const getCollectionAndItsLatestFile = ( const latestFile = new Map(); const collectionMap = new Map(); - collections.forEach((collection) => - collectionMap.set(collection.id, collection) - ); files.forEach((file) => { if (!latestFile.has(file.collectionID)) { latestFile.set(file.collectionID, file); } }); let allCollectionAndItsLatestFile: CollectionAndItsLatestFile[] = []; - for (const [collectionID, file] of latestFile) { + const userID = getData(LS_KEYS.USER).id; + + for (const collection of collections) { + if ( + collection.owner.id != userID || + collection.type == CollectionType.favorites + ) { + continue; + } allCollectionAndItsLatestFile.push({ - collection: collectionMap.get(collectionID), - file, + collection, + file: latestFile.get(collection.id), }); } return allCollectionAndItsLatestFile; @@ -208,14 +214,14 @@ export const AddCollection = async ( const { encryptedData: encryptedKey, nonce: keyDecryptionNonce, - }: KeyEncryptionResult = await worker.encryptToB64( + }: B64EncryptionResult = await worker.encryptToB64( collectionKey, encryptionKey ); const { encryptedData: encryptedName, nonce: nameDecryptionNonce, - }: KeyEncryptionResult = await worker.encryptToB64( + }: B64EncryptionResult = await worker.encryptToB64( collectionName, collectionKey ); @@ -290,7 +296,7 @@ const addToCollection = async (collection: collection, files: file[]) => { await Promise.all( files.map(async (file) => { file.collectionID = collection.id; - const newEncryptedKey: KeyEncryptionResult = await worker.encryptToB64( + const newEncryptedKey: B64EncryptionResult = await worker.encryptToB64( file.key, collection.key ); diff --git a/src/services/fileService.ts b/src/services/fileService.ts index f5db01ed6..11ae3bc57 100644 --- a/src/services/fileService.ts +++ b/src/services/fileService.ts @@ -25,11 +25,6 @@ export interface fileAttribute { decryptionHeader: string; } -export interface user { - id: number; - name: string; - email: string; -} export interface file { id: number; diff --git a/src/services/uploadService.ts b/src/services/uploadService.ts index 61dd86f51..8bdb453f9 100644 --- a/src/services/uploadService.ts +++ b/src/services/uploadService.ts @@ -18,7 +18,7 @@ interface EncryptionResult { file: fileAttribute; key: string; } -export interface KeyEncryptionResult { +export interface B64EncryptionResult { encryptedData: string; key: string; nonce: string; @@ -46,7 +46,7 @@ interface FileinMemory { interface EncryptedFile { file: ProcessedFile; - fileKey: KeyEncryptionResult; + fileKey: B64EncryptionResult; } interface ProcessedFile { file: fileAttribute; @@ -251,7 +251,7 @@ class UploadService { fileKey ); - const encryptedKey: KeyEncryptionResult = await worker.encryptToB64( + const encryptedKey: B64EncryptionResult = await worker.encryptToB64( fileKey, encryptionKey ); @@ -299,7 +299,7 @@ class UploadService { private getuploadFile( collection: collection, backupedFile: BackupedFile, - fileKey: KeyEncryptionResult + fileKey: B64EncryptionResult ): uploadFile { const uploadFile: uploadFile = { collectionID: collection.id, diff --git a/src/services/userService.ts b/src/services/userService.ts index c15dea953..1812e29a1 100644 --- a/src/services/userService.ts +++ b/src/services/userService.ts @@ -4,6 +4,12 @@ import { getEndpoint } from 'utils/common/apiUtil'; const ENDPOINT = getEndpoint(); +export interface user { + id: number; + name: string; + email: string; +} + export const getOtt = (email: string) => { return HTTPService.get(`${ENDPOINT}/users/ott`, { email: email, diff --git a/src/utils/crypto/libsodium.ts b/src/utils/crypto/libsodium.ts index e9bb30587..87cd5e055 100644 --- a/src/utils/crypto/libsodium.ts +++ b/src/utils/crypto/libsodium.ts @@ -138,6 +138,10 @@ export async function encryptToB64(data: string, key?: string) { nonce: await toB64(encrypted.nonce), }; } +export async function encryptUTF8(data: string, key?: string) { + const b64Data = await toB64(await fromString(data)); + return await encryptToB64(b64Data, key); +} export async function decryptB64(data: string, nonce: string, key: string) { await sodium.ready; @@ -150,7 +154,7 @@ export async function decryptB64(data: string, nonce: string, key: string) { return await toB64(decrypted); } -export async function decryptString(data: string, nonce: string, key: string) { +export async function decryptToUTF8(data: string, nonce: string, key: string) { await sodium.ready; const decrypted = await decrypt( await fromB64(data), @@ -246,14 +250,7 @@ export async function boxSealOpen( export async function fromB64(input: string) { await sodium.ready; - let result; - try { - result = sodium.from_base64(input, sodium.base64_variants.ORIGINAL); - } catch (e) { - result = await fromB64(await toB64(await fromString(input))); - } finally { - return result; - } + return sodium.from_base64(input, sodium.base64_variants.ORIGINAL); } export async function toB64(input: Uint8Array) { diff --git a/src/worker/crypto.worker.js b/src/worker/crypto.worker.js index 403628b1b..3585b7024 100644 --- a/src/worker/crypto.worker.js +++ b/src/worker/crypto.worker.js @@ -69,14 +69,18 @@ export class Crypto { return libsodium.decryptB64(data, nonce, key); } - async decryptString(data, nonce, key) { - return libsodium.decryptString(data, nonce, key); + async decryptToUTF8(data, nonce, key) { + return libsodium.decryptToUTF8(data, nonce, key); } async encryptToB64(data, key) { return libsodium.encryptToB64(data, key); } + async encryptUTF8(data, key) { + return libsodium.encryptUTF8(data, key); + } + async generateMasterKey() { return libsodium.generateMasterKey(); }