Merge branch 'master' into improve-uploading

This commit is contained in:
Abhinav-grd 2021-02-16 18:30:01 +05:30
commit e7689f79e1
7 changed files with 44 additions and 36 deletions

View file

@ -12,7 +12,7 @@ import { getData, LS_KEYS, setData } from 'utils/storage/localStorage';
import { useRouter } from 'next/router'; import { useRouter } from 'next/router';
import { getKey, SESSION_KEYS, setKey } from 'utils/storage/sessionStorage'; import { getKey, SESSION_KEYS, setKey } from 'utils/storage/sessionStorage';
import * as Comlink from 'comlink'; import * as Comlink from 'comlink';
import { KeyEncryptionResult } from 'services/uploadService'; import { B64EncryptionResult } from 'services/uploadService';
const CryptoWorker: any = const CryptoWorker: any =
typeof window !== 'undefined' && typeof window !== 'undefined' &&
@ -63,12 +63,12 @@ export default function Generate() {
kekSalt kekSalt
); );
const kekHash: string = await cryptoWorker.hash(kek); const kekHash: string = await cryptoWorker.hash(kek);
const encryptedKeyAttributes: KeyEncryptionResult = await cryptoWorker.encryptToB64( const encryptedKeyAttributes: B64EncryptionResult = await cryptoWorker.encryptToB64(
key, key,
kek kek
); );
const keyPair = await cryptoWorker.generateKeyPair(); const keyPair = await cryptoWorker.generateKeyPair();
const encryptedKeyPairAttributes: KeyEncryptionResult = await cryptoWorker.encryptToB64( const encryptedKeyPairAttributes: B64EncryptionResult = await cryptoWorker.encryptToB64(
keyPair.privateKey, keyPair.privateKey,
key key
); );

View file

@ -1,12 +1,13 @@
import { getEndpoint } from 'utils/common/apiUtil'; import { getEndpoint } from 'utils/common/apiUtil';
import { getData, LS_KEYS } from 'utils/storage/localStorage'; import { getData, LS_KEYS } from 'utils/storage/localStorage';
import { file, user, getFiles } from './fileService'; import { file } from './fileService';
import localForage from 'localforage'; import localForage from 'localforage';
import HTTPService from './HTTPService'; import HTTPService from './HTTPService';
import * as Comlink from 'comlink'; import * as Comlink from 'comlink';
import { KeyEncryptionResult } from './uploadService'; import { B64EncryptionResult } from './uploadService';
import { getActualKey, getToken } from 'utils/common/key'; import { getActualKey, getToken } from 'utils/common/key';
import { user } from './userService';
const CryptoWorker: any = const CryptoWorker: any =
typeof window !== 'undefined' && typeof window !== 'undefined' &&
@ -77,7 +78,7 @@ const getCollectionSecrets = async (
} }
collection.name = collection.name =
collection.name || collection.name ||
(await worker.decryptString( (await worker.decryptToUTF8(
collection.encryptedName, collection.encryptedName,
collection.nameDecryptionNonce, collection.nameDecryptionNonce,
decryptedKey decryptedKey
@ -122,7 +123,6 @@ export const syncCollections = async (token: string, key: string) => {
(await localForage.getItem<string>(COLLECTION_UPDATION_TIME)) ?? '0'; (await localForage.getItem<string>(COLLECTION_UPDATION_TIME)) ?? '0';
const updatedCollections = const updatedCollections =
(await getCollections(token, lastCollectionUpdationTime, key)) || []; (await getCollections(token, lastCollectionUpdationTime, key)) || [];
if (updatedCollections.length == 0) { if (updatedCollections.length == 0) {
return localCollections; return localCollections;
} }
@ -152,6 +152,7 @@ export const syncCollections = async (token: string, key: string) => {
updationTime = Math.max(updationTime, collection.updationTime); updationTime = Math.max(updationTime, collection.updationTime);
} }
} }
collections.sort((a, b) => b.updationTime - a.updationTime);
await localForage.setItem(COLLECTION_UPDATION_TIME, updationTime); await localForage.setItem(COLLECTION_UPDATION_TIME, updationTime);
await localForage.setItem(COLLECTIONS, collections); await localForage.setItem(COLLECTIONS, collections);
return collections; return collections;
@ -164,19 +165,24 @@ export const getCollectionAndItsLatestFile = (
const latestFile = new Map<number, file>(); const latestFile = new Map<number, file>();
const collectionMap = new Map<number, collection>(); const collectionMap = new Map<number, collection>();
collections.forEach((collection) =>
collectionMap.set(collection.id, collection)
);
files.forEach((file) => { files.forEach((file) => {
if (!latestFile.has(file.collectionID)) { if (!latestFile.has(file.collectionID)) {
latestFile.set(file.collectionID, file); latestFile.set(file.collectionID, file);
} }
}); });
let allCollectionAndItsLatestFile: CollectionAndItsLatestFile[] = []; 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({ allCollectionAndItsLatestFile.push({
collection: collectionMap.get(collectionID), collection,
file, file: latestFile.get(collection.id),
}); });
} }
return allCollectionAndItsLatestFile; return allCollectionAndItsLatestFile;
@ -208,14 +214,14 @@ export const AddCollection = async (
const { const {
encryptedData: encryptedKey, encryptedData: encryptedKey,
nonce: keyDecryptionNonce, nonce: keyDecryptionNonce,
}: KeyEncryptionResult = await worker.encryptToB64( }: B64EncryptionResult = await worker.encryptToB64(
collectionKey, collectionKey,
encryptionKey encryptionKey
); );
const { const {
encryptedData: encryptedName, encryptedData: encryptedName,
nonce: nameDecryptionNonce, nonce: nameDecryptionNonce,
}: KeyEncryptionResult = await worker.encryptToB64( }: B64EncryptionResult = await worker.encryptToB64(
collectionName, collectionName,
collectionKey collectionKey
); );
@ -290,7 +296,7 @@ const addToCollection = async (collection: collection, files: file[]) => {
await Promise.all( await Promise.all(
files.map(async (file) => { files.map(async (file) => {
file.collectionID = collection.id; file.collectionID = collection.id;
const newEncryptedKey: KeyEncryptionResult = await worker.encryptToB64( const newEncryptedKey: B64EncryptionResult = await worker.encryptToB64(
file.key, file.key,
collection.key collection.key
); );

View file

@ -25,11 +25,6 @@ export interface fileAttribute {
decryptionHeader: string; decryptionHeader: string;
} }
export interface user {
id: number;
name: string;
email: string;
}
export interface file { export interface file {
id: number; id: number;

View file

@ -18,7 +18,7 @@ interface EncryptionResult {
file: fileAttribute; file: fileAttribute;
key: string; key: string;
} }
export interface KeyEncryptionResult { export interface B64EncryptionResult {
encryptedData: string; encryptedData: string;
key: string; key: string;
nonce: string; nonce: string;
@ -46,7 +46,7 @@ interface FileinMemory {
interface EncryptedFile { interface EncryptedFile {
file: ProcessedFile; file: ProcessedFile;
fileKey: KeyEncryptionResult; fileKey: B64EncryptionResult;
} }
interface ProcessedFile { interface ProcessedFile {
file: fileAttribute; file: fileAttribute;
@ -251,7 +251,7 @@ class UploadService {
fileKey fileKey
); );
const encryptedKey: KeyEncryptionResult = await worker.encryptToB64( const encryptedKey: B64EncryptionResult = await worker.encryptToB64(
fileKey, fileKey,
encryptionKey encryptionKey
); );
@ -299,7 +299,7 @@ class UploadService {
private getuploadFile( private getuploadFile(
collection: collection, collection: collection,
backupedFile: BackupedFile, backupedFile: BackupedFile,
fileKey: KeyEncryptionResult fileKey: B64EncryptionResult
): uploadFile { ): uploadFile {
const uploadFile: uploadFile = { const uploadFile: uploadFile = {
collectionID: collection.id, collectionID: collection.id,

View file

@ -4,6 +4,12 @@ import { getEndpoint } from 'utils/common/apiUtil';
const ENDPOINT = getEndpoint(); const ENDPOINT = getEndpoint();
export interface user {
id: number;
name: string;
email: string;
}
export const getOtt = (email: string) => { export const getOtt = (email: string) => {
return HTTPService.get(`${ENDPOINT}/users/ott`, { return HTTPService.get(`${ENDPOINT}/users/ott`, {
email: email, email: email,

View file

@ -138,6 +138,10 @@ export async function encryptToB64(data: string, key?: string) {
nonce: await toB64(encrypted.nonce), 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) { export async function decryptB64(data: string, nonce: string, key: string) {
await sodium.ready; await sodium.ready;
@ -150,7 +154,7 @@ export async function decryptB64(data: string, nonce: string, key: string) {
return await toB64(decrypted); 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; await sodium.ready;
const decrypted = await decrypt( const decrypted = await decrypt(
await fromB64(data), await fromB64(data),
@ -246,14 +250,7 @@ export async function boxSealOpen(
export async function fromB64(input: string) { export async function fromB64(input: string) {
await sodium.ready; await sodium.ready;
let result; return sodium.from_base64(input, sodium.base64_variants.ORIGINAL);
try {
result = sodium.from_base64(input, sodium.base64_variants.ORIGINAL);
} catch (e) {
result = await fromB64(await toB64(await fromString(input)));
} finally {
return result;
}
} }
export async function toB64(input: Uint8Array) { export async function toB64(input: Uint8Array) {

View file

@ -69,14 +69,18 @@ export class Crypto {
return libsodium.decryptB64(data, nonce, key); return libsodium.decryptB64(data, nonce, key);
} }
async decryptString(data, nonce, key) { async decryptToUTF8(data, nonce, key) {
return libsodium.decryptString(data, nonce, key); return libsodium.decryptToUTF8(data, nonce, key);
} }
async encryptToB64(data, key) { async encryptToB64(data, key) {
return libsodium.encryptToB64(data, key); return libsodium.encryptToB64(data, key);
} }
async encryptUTF8(data, key) {
return libsodium.encryptUTF8(data, key);
}
async generateMasterKey() { async generateMasterKey() {
return libsodium.generateMasterKey(); return libsodium.generateMasterKey();
} }