Merge pull request #21 from ente-io/collection-service-corrections

Collection service corrections
This commit is contained in:
Vishnu Mohandas 2021-02-16 18:16:08 +05:30 committed by GitHub
commit d2a19c8cd8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 46 additions and 38 deletions

View file

@ -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
);

View file

@ -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<string>(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<number, file>();
const collectionMap = new Map<number, collection>();
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.encryptUTF8(
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
);

View file

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

View file

@ -19,7 +19,7 @@ interface encryptionResult {
file: fileAttribute;
key: string;
}
export interface keyEncryptionResult {
export interface B64EncryptionResult {
encryptedData: string;
key: string;
nonce: string;
@ -39,7 +39,7 @@ interface FileinMemory {
interface encryptedFile {
filedata: fileAttribute;
thumbnail: fileAttribute;
fileKey: keyEncryptionResult;
fileKey: B64EncryptionResult;
}
interface objectKey {
@ -63,7 +63,7 @@ interface uploadFile extends objectKeys {
interface UploadFileWithoutMetaData {
tempUploadFile: uploadFile;
encryptedFileKey: keyEncryptionResult;
encryptedFileKey: B64EncryptionResult;
fileName: string;
}
@ -270,7 +270,7 @@ class UploadService {
fileKey
);
const encryptedKey: keyEncryptionResult = await worker.encryptToB64(
const encryptedKey: B64EncryptionResult = await worker.encryptB64(
fileKey,
encryptionKey
);
@ -289,7 +289,7 @@ class UploadService {
private async encryptMetadata(
worker: any,
fileName: string,
encryptedFileKey: keyEncryptionResult
encryptedFileKey: B64EncryptionResult
) {
const metaData = this.metadataMap.get(fileName);
const fileKey = await worker.decryptB64(
@ -340,7 +340,7 @@ class UploadService {
private getuploadFile(
collection: collection,
encryptedKey: keyEncryptionResult,
encryptedKey: B64EncryptionResult,
objectKeys: objectKeys
): uploadFile {
const uploadFile: uploadFile = {

View file

@ -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,

View file

@ -142,6 +142,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;
@ -154,7 +158,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),
@ -250,14 +254,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) {

View file

@ -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();
}