1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-11-27 12:36:14 +01:00

apply collection and folder views

This commit is contained in:
Kyle Spearrin 2018-01-25 14:57:42 -05:00
parent 8d2a90e496
commit 3869dcaf7b
10 changed files with 74 additions and 36 deletions

View File

@ -2,13 +2,15 @@ import { CollectionData } from '../models/data/collectionData';
import { Collection } from '../models/domain/collection';
import { CollectionView } from '../models/view/collectionView';
export abstract class CollectionService {
decryptedCollectionCache: any[];
decryptedCollectionCache: CollectionView[];
clearCache: () => void;
get: (id: string) => Promise<Collection>;
getAll: () => Promise<Collection[]>;
getAllDecrypted: () => Promise<any[]>;
getAllDecrypted: () => Promise<CollectionView[]>;
upsert: (collection: CollectionData | CollectionData[]) => Promise<any>;
replace: (collections: { [id: string]: CollectionData; }) => Promise<any>;
clear: (userId: string) => Promise<any>;

View File

@ -25,7 +25,8 @@ export abstract class CryptoService {
makeKey: (password: string, salt: string) => SymmetricCryptoKey;
hashPassword: (password: string, key: SymmetricCryptoKey) => Promise<string>;
makeEncKey: (key: SymmetricCryptoKey) => Promise<CipherString>;
encrypt: (plainValue: string | Uint8Array, key?: SymmetricCryptoKey, plainValueEncoding?: string) => Promise<CipherString>;
encrypt: (plainValue: string | Uint8Array, key?: SymmetricCryptoKey,
plainValueEncoding?: string) => Promise<CipherString>;
encryptToBytes: (plainValue: ArrayBuffer, key?: SymmetricCryptoKey) => Promise<ArrayBuffer>;
decrypt: (cipherString: CipherString, key?: SymmetricCryptoKey, outputEncoding?: string) => Promise<string>;
decryptFromBytes: (encBuf: ArrayBuffer, key: SymmetricCryptoKey) => Promise<ArrayBuffer>;

View File

@ -2,14 +2,16 @@ import { FolderData } from '../models/data/folderData';
import { Folder } from '../models/domain/folder';
import { FolderView } from '../models/view/folderView';
export abstract class FolderService {
decryptedFolderCache: any[];
decryptedFolderCache: FolderView[];
clearCache: () => void;
encrypt: (model: any) => Promise<Folder>;
encrypt: (model: FolderView) => Promise<Folder>;
get: (id: string) => Promise<Folder>;
getAll: () => Promise<Folder[]>;
getAllDecrypted: () => Promise<any[]>;
getAllDecrypted: () => Promise<FolderView[]>;
saveWithServer: (folder: Folder) => Promise<any>;
upsert: (folder: FolderData | FolderData[]) => Promise<any>;
replace: (folders: { [id: string]: FolderData; }) => Promise<any>;

View File

@ -1,5 +1,7 @@
import { CollectionData } from '../data/collectionData';
import { CollectionView } from '../view/collectionView';
import { CipherString } from './cipherString';
import Domain from './domain';
@ -21,13 +23,8 @@ export class Collection extends Domain {
}, alreadyEncrypted, ['id', 'organizationId']);
}
decrypt(): Promise<any> {
const model = {
id: this.id,
organizationId: this.organizationId,
};
return this.decryptObj(model, {
decrypt(): Promise<CollectionView> {
return this.decryptObj(new CollectionView(this), {
name: null,
}, this.organizationId);
}

View File

@ -1,5 +1,7 @@
import { FolderData } from '../data/folderData';
import { FolderView } from '../view/folderView';
import { CipherString } from './cipherString';
import Domain from './domain';
@ -19,12 +21,8 @@ export class Folder extends Domain {
}, alreadyEncrypted, ['id']);
}
decrypt(): Promise<any> {
const model = {
id: this.id,
};
return this.decryptObj(model, {
decrypt(): Promise<FolderView> {
return this.decryptObj(new FolderView(this), {
name: null,
}, null);
}

View File

@ -0,0 +1,18 @@
import { View } from './view';
import { Collection } from '../domain/collection';
export class CollectionView implements View {
id: string;
organizationId: string;
name: string;
constructor(c?: Collection) {
if (!c) {
return;
}
this.id = c.id;
this.organizationId = c.organizationId;
}
}

View File

@ -0,0 +1,16 @@
import { View } from './view';
import { Folder } from '../domain/folder';
export class FolderView implements View {
id: string;
name: string;
constructor(f?: Folder) {
if (!f) {
return;
}
this.id = f.id;
}
}

View File

@ -7,7 +7,11 @@ import { SecureNote } from '../domain/secureNote';
export class SecureNoteView implements View {
type: SecureNoteType;
constructor(n: SecureNote) {
constructor(n?: SecureNote) {
if (!n) {
return;
}
this.type = n.type;
}

View File

@ -2,6 +2,8 @@ import { CollectionData } from '../models/data/collectionData';
import { Collection } from '../models/domain/collection';
import { CollectionView } from '../models/view/collectionView';
import { CollectionService as CollectionServiceAbstraction } from '../abstractions/collection.service';
import { CryptoService } from '../abstractions/crypto.service';
import { StorageService } from '../abstractions/storage.service';
@ -12,7 +14,7 @@ const Keys = {
};
export class CollectionService implements CollectionServiceAbstraction {
decryptedCollectionCache: any[];
decryptedCollectionCache: CollectionView[];
constructor(private cryptoService: CryptoService, private userService: UserService,
private storageService: StorageService) {
@ -46,7 +48,7 @@ export class CollectionService implements CollectionServiceAbstraction {
return response;
}
async getAllDecrypted(): Promise<any[]> {
async getAllDecrypted(): Promise<CollectionView[]> {
if (this.decryptedCollectionCache != null) {
return this.decryptedCollectionCache;
}
@ -56,17 +58,15 @@ export class CollectionService implements CollectionServiceAbstraction {
throw new Error('No key.');
}
const decFolders: any[] = [];
const decCollections: CollectionView[] = [];
const promises: Array<Promise<any>> = [];
const folders = await this.getAll();
folders.forEach((folder) => {
promises.push(folder.decrypt().then((f: any) => {
decFolders.push(f);
}));
const collections = await this.getAll();
collections.forEach((collection) => {
promises.push(collection.decrypt().then((c) => decCollections.push(c)));
});
await Promise.all(promises);
this.decryptedCollectionCache = decFolders;
this.decryptedCollectionCache = decCollections;
return this.decryptedCollectionCache;
}

View File

@ -6,6 +6,8 @@ import { FolderRequest } from '../models/request/folderRequest';
import { FolderResponse } from '../models/response/folderResponse';
import { FolderView } from '../models/view/folderView';
import { ApiService } from '../abstractions/api.service';
import { CryptoService } from '../abstractions/crypto.service';
import { FolderService as FolderServiceAbstraction } from '../abstractions/folder.service';
@ -17,7 +19,7 @@ const Keys = {
};
export class FolderService implements FolderServiceAbstraction {
decryptedFolderCache: any[];
decryptedFolderCache: FolderView[];
constructor(private cryptoService: CryptoService, private userService: UserService,
private noneFolder: () => string, private apiService: ApiService,
@ -28,7 +30,7 @@ export class FolderService implements FolderServiceAbstraction {
this.decryptedFolderCache = null;
}
async encrypt(model: any): Promise<Folder> {
async encrypt(model: FolderView): Promise<Folder> {
const folder = new Folder();
folder.id = model.id;
folder.name = await this.cryptoService.encrypt(model.name);
@ -59,12 +61,12 @@ export class FolderService implements FolderServiceAbstraction {
return response;
}
async getAllDecrypted(): Promise<any[]> {
async getAllDecrypted(): Promise<FolderView[]> {
if (this.decryptedFolderCache != null) {
return this.decryptedFolderCache;
}
const decFolders: any[] = [{
const decFolders: FolderView[] = [{
id: null,
name: this.noneFolder(),
}];
@ -77,9 +79,7 @@ export class FolderService implements FolderServiceAbstraction {
const promises: Array<Promise<any>> = [];
const folders = await this.getAll();
folders.forEach((folder) => {
promises.push(folder.decrypt().then((f: any) => {
decFolders.push(f);
}));
promises.push(folder.decrypt().then((f) => decFolders.push(f)));
});
await Promise.all(promises);