mirror of
https://github.com/bitwarden/browser.git
synced 2024-11-23 11:56:00 +01:00
add support for syncing collections
This commit is contained in:
parent
04cfd8040c
commit
2d81466d25
@ -6,6 +6,7 @@ import ApiService from './services/api.service';
|
|||||||
import AppIdService from './services/appId.service';
|
import AppIdService from './services/appId.service';
|
||||||
import AutofillService from './services/autofill.service';
|
import AutofillService from './services/autofill.service';
|
||||||
import CipherService from './services/cipher.service';
|
import CipherService from './services/cipher.service';
|
||||||
|
import CollectionService from './services/collection.service';
|
||||||
import ConstantsService from './services/constants.service';
|
import ConstantsService from './services/constants.service';
|
||||||
import CryptoService from './services/crypto.service';
|
import CryptoService from './services/crypto.service';
|
||||||
import EnvironmentService from './services/environment.service';
|
import EnvironmentService from './services/environment.service';
|
||||||
@ -77,6 +78,7 @@ var bg_isBackground = true,
|
|||||||
bg_settingsService,
|
bg_settingsService,
|
||||||
bg_cipherService,
|
bg_cipherService,
|
||||||
bg_folderService,
|
bg_folderService,
|
||||||
|
bg_collectionService,
|
||||||
bg_lockService,
|
bg_lockService,
|
||||||
bg_syncService,
|
bg_syncService,
|
||||||
bg_passwordGenerationService,
|
bg_passwordGenerationService,
|
||||||
@ -106,8 +108,9 @@ var bg_isBackground = true,
|
|||||||
window.bg_settingsService = bg_settingsService = new SettingsService(bg_userService);
|
window.bg_settingsService = bg_settingsService = new SettingsService(bg_userService);
|
||||||
window.bg_cipherService = bg_cipherService = new CipherService(bg_cryptoService, bg_userService, bg_settingsService, bg_apiService);
|
window.bg_cipherService = bg_cipherService = new CipherService(bg_cryptoService, bg_userService, bg_settingsService, bg_apiService);
|
||||||
window.bg_folderService = bg_folderService = new FolderService(bg_cryptoService, bg_userService, bg_i18nService, bg_apiService);
|
window.bg_folderService = bg_folderService = new FolderService(bg_cryptoService, bg_userService, bg_i18nService, bg_apiService);
|
||||||
|
window.bg_collectionService = bg_collectionService = new CollectionService(bg_cryptoService, bg_userService, bg_i18nService, bg_apiService);
|
||||||
window.bg_lockService = bg_lockService = new LockService(bg_cipherService, bg_folderService, bg_cryptoService, bg_utilsService, setIcon, refreshBadgeAndMenu);
|
window.bg_lockService = bg_lockService = new LockService(bg_cipherService, bg_folderService, bg_cryptoService, bg_utilsService, setIcon, refreshBadgeAndMenu);
|
||||||
window.bg_syncService = bg_syncService = new SyncService(bg_userService, bg_apiService, bg_settingsService, bg_folderService, bg_cipherService, bg_cryptoService, logout);
|
window.bg_syncService = bg_syncService = new SyncService(bg_userService, bg_apiService, bg_settingsService, bg_folderService, bg_cipherService, bg_cryptoService, bg_collectionService, logout);
|
||||||
window.bg_passwordGenerationService = bg_passwordGenerationService = new PasswordGenerationService(bg_cryptoService);
|
window.bg_passwordGenerationService = bg_passwordGenerationService = new PasswordGenerationService(bg_cryptoService);
|
||||||
window.bg_totpService = bg_totpService = new TotpService();
|
window.bg_totpService = bg_totpService = new TotpService();
|
||||||
window.bg_autofillService = bg_autofillService = new AutofillService(bg_cipherService, bg_tokenService, bg_totpService, bg_utilsService);
|
window.bg_autofillService = bg_autofillService = new AutofillService(bg_cipherService, bg_tokenService, bg_totpService, bg_utilsService);
|
||||||
|
@ -28,6 +28,7 @@ class CipherData {
|
|||||||
identity?: IdentityData;
|
identity?: IdentityData;
|
||||||
fields?: FieldData[];
|
fields?: FieldData[];
|
||||||
attachments?: AttachmentData[];
|
attachments?: AttachmentData[];
|
||||||
|
collectionIds?: string[];
|
||||||
|
|
||||||
constructor(response: CipherResponse, userId: string) {
|
constructor(response: CipherResponse, userId: string) {
|
||||||
this.id = response.id;
|
this.id = response.id;
|
||||||
@ -39,6 +40,7 @@ class CipherData {
|
|||||||
this.favorite = response.favorite;
|
this.favorite = response.favorite;
|
||||||
this.revisionDate = response.revisionDate;
|
this.revisionDate = response.revisionDate;
|
||||||
this.type = response.type;
|
this.type = response.type;
|
||||||
|
this.collectionIds = response.collectionIds;
|
||||||
|
|
||||||
this.name = response.data.Name;
|
this.name = response.data.Name;
|
||||||
this.notes = response.data.Notes;
|
this.notes = response.data.Notes;
|
||||||
|
16
src/models/data/collectionData.ts
Normal file
16
src/models/data/collectionData.ts
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
import { CollectionResponse } from '../response/collectionResponse';
|
||||||
|
|
||||||
|
class CollectionData {
|
||||||
|
id: string;
|
||||||
|
organizationId: string;
|
||||||
|
name: string;
|
||||||
|
|
||||||
|
constructor(response: CollectionResponse) {
|
||||||
|
this.id = response.id;
|
||||||
|
this.organizationId = response.organizationId;
|
||||||
|
this.name = response.name;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export { CollectionData };
|
||||||
|
(window as any).CollectionData = CollectionData;
|
@ -30,6 +30,7 @@ class Cipher extends Domain {
|
|||||||
secureNote: SecureNote;
|
secureNote: SecureNote;
|
||||||
attachments: Attachment[];
|
attachments: Attachment[];
|
||||||
fields: Field[];
|
fields: Field[];
|
||||||
|
collectionIds: string[];
|
||||||
|
|
||||||
private utilsService: UtilsService;
|
private utilsService: UtilsService;
|
||||||
|
|
||||||
@ -51,6 +52,7 @@ class Cipher extends Domain {
|
|||||||
this.favorite = obj.favorite;
|
this.favorite = obj.favorite;
|
||||||
this.organizationUseTotp = obj.organizationUseTotp;
|
this.organizationUseTotp = obj.organizationUseTotp;
|
||||||
this.edit = obj.edit;
|
this.edit = obj.edit;
|
||||||
|
this.collectionIds = obj.collectionIds;
|
||||||
this.localData = localData;
|
this.localData = localData;
|
||||||
|
|
||||||
switch (this.type) {
|
switch (this.type) {
|
||||||
@ -104,6 +106,7 @@ class Cipher extends Domain {
|
|||||||
subTitle: null as string,
|
subTitle: null as string,
|
||||||
attachments: null as any[],
|
attachments: null as any[],
|
||||||
fields: null as any[],
|
fields: null as any[],
|
||||||
|
collectionIds: this.collectionIds,
|
||||||
};
|
};
|
||||||
|
|
||||||
await this.decryptObj(model, {
|
await this.decryptObj(model, {
|
||||||
|
37
src/models/domain/collection.ts
Normal file
37
src/models/domain/collection.ts
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
import { CollectionData } from '../data/collectionData';
|
||||||
|
|
||||||
|
import { CipherString } from './cipherString';
|
||||||
|
import Domain from './domain';
|
||||||
|
|
||||||
|
class Collection extends Domain {
|
||||||
|
id: string;
|
||||||
|
organizationId: string;
|
||||||
|
name: CipherString;
|
||||||
|
|
||||||
|
constructor(obj?: CollectionData, alreadyEncrypted: boolean = false) {
|
||||||
|
super();
|
||||||
|
if (obj == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.buildDomainModel(this, obj, {
|
||||||
|
id: null,
|
||||||
|
organizationId: null,
|
||||||
|
name: null,
|
||||||
|
}, alreadyEncrypted, ['id', 'organizationId']);
|
||||||
|
}
|
||||||
|
|
||||||
|
decrypt(): Promise<any> {
|
||||||
|
const model = {
|
||||||
|
id: this.id,
|
||||||
|
organizationId: this.organizationId,
|
||||||
|
};
|
||||||
|
|
||||||
|
return this.decryptObj(model, {
|
||||||
|
name: null,
|
||||||
|
}, this.organizationId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export { Collection };
|
||||||
|
(window as any).Collection = Collection;
|
@ -11,6 +11,7 @@ class CipherResponse {
|
|||||||
data: any;
|
data: any;
|
||||||
revisionDate: string;
|
revisionDate: string;
|
||||||
attachments: AttachmentResponse[];
|
attachments: AttachmentResponse[];
|
||||||
|
collectionIds: string[];
|
||||||
|
|
||||||
constructor(response: any) {
|
constructor(response: any) {
|
||||||
this.id = response.Id;
|
this.id = response.Id;
|
||||||
@ -29,6 +30,13 @@ class CipherResponse {
|
|||||||
this.attachments.push(new AttachmentResponse(attachment));
|
this.attachments.push(new AttachmentResponse(attachment));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (response.CollectionIds) {
|
||||||
|
this.collectionIds = [];
|
||||||
|
response.CollectionIds.forEach((id: string) => {
|
||||||
|
this.collectionIds.push(id);
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
14
src/models/response/collectionResponse.ts
Normal file
14
src/models/response/collectionResponse.ts
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
class CollectionResponse {
|
||||||
|
id: string;
|
||||||
|
organizationId: string;
|
||||||
|
name: string;
|
||||||
|
|
||||||
|
constructor(response: any) {
|
||||||
|
this.id = response.Id;
|
||||||
|
this.organizationId = response.OrganizationId;
|
||||||
|
this.name = response.Name;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export { CollectionResponse };
|
||||||
|
(window as any).CollectionResponse = CollectionResponse;
|
@ -1,4 +1,5 @@
|
|||||||
import { CipherResponse } from './cipherResponse';
|
import { CipherResponse } from './cipherResponse';
|
||||||
|
import { CollectionResponse } from './collectionResponse';
|
||||||
import { DomainsResponse } from './domainsResponse';
|
import { DomainsResponse } from './domainsResponse';
|
||||||
import { FolderResponse } from './folderResponse';
|
import { FolderResponse } from './folderResponse';
|
||||||
import { ProfileResponse } from './profileResponse';
|
import { ProfileResponse } from './profileResponse';
|
||||||
@ -6,6 +7,7 @@ import { ProfileResponse } from './profileResponse';
|
|||||||
class SyncResponse {
|
class SyncResponse {
|
||||||
profile?: ProfileResponse;
|
profile?: ProfileResponse;
|
||||||
folders: FolderResponse[] = [];
|
folders: FolderResponse[] = [];
|
||||||
|
collections: CollectionResponse[] = [];
|
||||||
ciphers: CipherResponse[] = [];
|
ciphers: CipherResponse[] = [];
|
||||||
domains?: DomainsResponse;
|
domains?: DomainsResponse;
|
||||||
|
|
||||||
@ -20,6 +22,12 @@ class SyncResponse {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (response.Collections) {
|
||||||
|
response.Collections.forEach((collection: any) => {
|
||||||
|
this.collections.push(new CollectionResponse(collection));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
if (response.Ciphers) {
|
if (response.Ciphers) {
|
||||||
response.Ciphers.forEach((cipher: any) => {
|
response.Ciphers.forEach((cipher: any) => {
|
||||||
this.ciphers.push(new CipherResponse(cipher));
|
this.ciphers.push(new CipherResponse(cipher));
|
||||||
|
126
src/services/collection.service.ts
Normal file
126
src/services/collection.service.ts
Normal file
@ -0,0 +1,126 @@
|
|||||||
|
import { CipherString } from '../models/domain/cipherString';
|
||||||
|
import { Collection } from '../models/domain/collection';
|
||||||
|
|
||||||
|
import { CollectionData } from '../models/data/collectionData';
|
||||||
|
|
||||||
|
import ApiService from './api.service';
|
||||||
|
import CryptoService from './crypto.service';
|
||||||
|
import UserService from './user.service';
|
||||||
|
import UtilsService from './utils.service';
|
||||||
|
|
||||||
|
const Keys = {
|
||||||
|
collectionsPrefix: 'collections_',
|
||||||
|
};
|
||||||
|
|
||||||
|
export default class CollectionService {
|
||||||
|
decryptedCollectionCache: any[];
|
||||||
|
|
||||||
|
constructor(private cryptoService: CryptoService, private userService: UserService,
|
||||||
|
private apiService: ApiService) {
|
||||||
|
}
|
||||||
|
|
||||||
|
clearCache(): void {
|
||||||
|
this.decryptedCollectionCache = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
async get(id: string): Promise<Collection> {
|
||||||
|
const userId = await this.userService.getUserId();
|
||||||
|
const collections = await UtilsService.getObjFromStorage<{ [id: string]: CollectionData; }>(
|
||||||
|
Keys.collectionsPrefix + userId);
|
||||||
|
if (collections == null || !collections.hasOwnProperty(id)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return new Collection(collections[id]);
|
||||||
|
}
|
||||||
|
|
||||||
|
async getAll(): Promise<Collection[]> {
|
||||||
|
const userId = await this.userService.getUserId();
|
||||||
|
const collections = await UtilsService.getObjFromStorage<{ [id: string]: CollectionData; }>(
|
||||||
|
Keys.collectionsPrefix + userId);
|
||||||
|
const response: Collection[] = [];
|
||||||
|
for (const id in collections) {
|
||||||
|
if (collections.hasOwnProperty(id)) {
|
||||||
|
response.push(new Collection(collections[id]));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
|
||||||
|
async getAllDecrypted(): Promise<any[]> {
|
||||||
|
if (this.decryptedCollectionCache != null) {
|
||||||
|
return this.decryptedCollectionCache;
|
||||||
|
}
|
||||||
|
|
||||||
|
const key = await this.cryptoService.getKey();
|
||||||
|
if (key == null) {
|
||||||
|
throw new Error('No key.');
|
||||||
|
}
|
||||||
|
|
||||||
|
const decFolders: any[] = [];
|
||||||
|
const promises: Array<Promise<any>> = [];
|
||||||
|
const folders = await this.getAll();
|
||||||
|
folders.forEach((folder) => {
|
||||||
|
promises.push(folder.decrypt().then((f: any) => {
|
||||||
|
decFolders.push(f);
|
||||||
|
}));
|
||||||
|
});
|
||||||
|
|
||||||
|
await Promise.all(promises);
|
||||||
|
this.decryptedCollectionCache = decFolders;
|
||||||
|
return this.decryptedCollectionCache;
|
||||||
|
}
|
||||||
|
|
||||||
|
async upsert(collection: CollectionData | CollectionData[]): Promise<any> {
|
||||||
|
const userId = await this.userService.getUserId();
|
||||||
|
let collections = await UtilsService.getObjFromStorage<{ [id: string]: CollectionData; }>(
|
||||||
|
Keys.collectionsPrefix + userId);
|
||||||
|
if (collections == null) {
|
||||||
|
collections = {};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (collection instanceof CollectionData) {
|
||||||
|
const c = collection as CollectionData;
|
||||||
|
collections[c.id] = c;
|
||||||
|
} else {
|
||||||
|
(collection as CollectionData[]).forEach((c) => {
|
||||||
|
collections[c.id] = c;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
await UtilsService.saveObjToStorage(Keys.collectionsPrefix + userId, collections);
|
||||||
|
this.decryptedCollectionCache = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
async replace(collections: { [id: string]: CollectionData; }): Promise<any> {
|
||||||
|
const userId = await this.userService.getUserId();
|
||||||
|
await UtilsService.saveObjToStorage(Keys.collectionsPrefix + userId, collections);
|
||||||
|
this.decryptedCollectionCache = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
async clear(userId: string): Promise<any> {
|
||||||
|
await UtilsService.removeFromStorage(Keys.collectionsPrefix + userId);
|
||||||
|
this.decryptedCollectionCache = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
async delete(id: string | string[]): Promise<any> {
|
||||||
|
const userId = await this.userService.getUserId();
|
||||||
|
const collections = await UtilsService.getObjFromStorage<{ [id: string]: CollectionData; }>(
|
||||||
|
Keys.collectionsPrefix + userId);
|
||||||
|
if (collections == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (typeof id === 'string') {
|
||||||
|
const i = id as string;
|
||||||
|
delete collections[id];
|
||||||
|
} else {
|
||||||
|
(id as string[]).forEach((i) => {
|
||||||
|
delete collections[i];
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
await UtilsService.saveObjToStorage(Keys.collectionsPrefix + userId, collections);
|
||||||
|
this.decryptedCollectionCache = null;
|
||||||
|
}
|
||||||
|
}
|
@ -7,7 +7,6 @@ import { FolderRequest } from '../models/request/folderRequest';
|
|||||||
import { FolderResponse } from '../models/response/folderResponse';
|
import { FolderResponse } from '../models/response/folderResponse';
|
||||||
|
|
||||||
import ApiService from './api.service';
|
import ApiService from './api.service';
|
||||||
import ConstantsService from './constants.service';
|
|
||||||
import CryptoService from './crypto.service';
|
import CryptoService from './crypto.service';
|
||||||
import UserService from './user.service';
|
import UserService from './user.service';
|
||||||
import UtilsService from './utils.service';
|
import UtilsService from './utils.service';
|
||||||
|
@ -1,7 +1,9 @@
|
|||||||
import { CipherData } from '../models/data/cipherData';
|
import { CipherData } from '../models/data/cipherData';
|
||||||
|
import { CollectionData } from '../models/data/collectionData';
|
||||||
import { FolderData } from '../models/data/folderData';
|
import { FolderData } from '../models/data/folderData';
|
||||||
|
|
||||||
import { CipherResponse } from '../models/response/cipherResponse';
|
import { CipherResponse } from '../models/response/cipherResponse';
|
||||||
|
import { CollectionResponse } from '../models/response/collectionResponse';
|
||||||
import { DomainsResponse } from '../models/response/domainsResponse';
|
import { DomainsResponse } from '../models/response/domainsResponse';
|
||||||
import { FolderResponse } from '../models/response/folderResponse';
|
import { FolderResponse } from '../models/response/folderResponse';
|
||||||
import { ProfileResponse } from '../models/response/profileResponse';
|
import { ProfileResponse } from '../models/response/profileResponse';
|
||||||
@ -9,6 +11,7 @@ import { SyncResponse } from '../models/response/syncResponse';
|
|||||||
|
|
||||||
import ApiService from './api.service';
|
import ApiService from './api.service';
|
||||||
import CipherService from './cipher.service';
|
import CipherService from './cipher.service';
|
||||||
|
import CollectionService from './collection.service';
|
||||||
import CryptoService from './crypto.service';
|
import CryptoService from './crypto.service';
|
||||||
import FolderService from './folder.service';
|
import FolderService from './folder.service';
|
||||||
import SettingsService from './settings.service';
|
import SettingsService from './settings.service';
|
||||||
@ -25,7 +28,7 @@ export default class SyncService {
|
|||||||
constructor(private userService: UserService, private apiService: ApiService,
|
constructor(private userService: UserService, private apiService: ApiService,
|
||||||
private settingsService: SettingsService, private folderService: FolderService,
|
private settingsService: SettingsService, private folderService: FolderService,
|
||||||
private cipherService: CipherService, private cryptoService: CryptoService,
|
private cipherService: CipherService, private cryptoService: CryptoService,
|
||||||
private logoutCallback: Function) {
|
private collectionService: CollectionService, private logoutCallback: Function) {
|
||||||
}
|
}
|
||||||
|
|
||||||
async getLastSync() {
|
async getLastSync() {
|
||||||
@ -84,6 +87,7 @@ export default class SyncService {
|
|||||||
|
|
||||||
await this.syncProfile(response.profile);
|
await this.syncProfile(response.profile);
|
||||||
await this.syncFolders(userId, response.folders);
|
await this.syncFolders(userId, response.folders);
|
||||||
|
await this.syncCollections(response.collections);
|
||||||
await this.syncCiphers(userId, response.ciphers);
|
await this.syncCiphers(userId, response.ciphers);
|
||||||
await this.syncSettings(userId, response.domains);
|
await this.syncSettings(userId, response.domains);
|
||||||
|
|
||||||
@ -141,6 +145,14 @@ export default class SyncService {
|
|||||||
return await this.folderService.replace(folders);
|
return await this.folderService.replace(folders);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async syncCollections(response: CollectionResponse[]) {
|
||||||
|
const collections: { [id: string]: CollectionData; } = {};
|
||||||
|
response.forEach((c) => {
|
||||||
|
collections[c.id] = new CollectionData(c);
|
||||||
|
});
|
||||||
|
return await this.collectionService.replace(collections);
|
||||||
|
}
|
||||||
|
|
||||||
private async syncCiphers(userId: string, response: CipherResponse[]) {
|
private async syncCiphers(userId: string, response: CipherResponse[]) {
|
||||||
const ciphers: { [id: string]: CipherData; } = {};
|
const ciphers: { [id: string]: CipherData; } = {};
|
||||||
response.forEach((c) => {
|
response.forEach((c) => {
|
||||||
|
Loading…
Reference in New Issue
Block a user