bitwarden-estensione-browser/src/services/sync.service.ts

182 lines
6.2 KiB
TypeScript
Raw Normal View History

2017-11-06 17:55:17 +01:00
import { CipherData } from '../models/data/cipherData';
2017-11-22 19:17:59 +01:00
import { CollectionData } from '../models/data/collectionData';
2017-11-06 17:55:17 +01:00
import { FolderData } from '../models/data/folderData';
import { CipherResponse } from '../models/response/cipherResponse';
2017-11-22 19:17:59 +01:00
import { CollectionResponse } from '../models/response/collectionResponse';
2017-11-06 17:55:17 +01:00
import { DomainsResponse } from '../models/response/domainsResponse';
import { FolderResponse } from '../models/response/folderResponse';
import { ProfileResponse } from '../models/response/profileResponse';
import { SyncResponse } from '../models/response/syncResponse';
import ApiService from './api.service';
import CipherService from './cipher.service';
2017-11-22 19:17:59 +01:00
import CollectionService from './collection.service';
2017-11-06 17:55:17 +01:00
import CryptoService from './crypto.service';
import FolderService from './folder.service';
import SettingsService from './settings.service';
import UserService from './user.service';
2018-01-04 16:51:08 +01:00
2018-01-06 21:47:46 +01:00
import { MessagingService, StorageService } from '@bitwarden/jslib';
2017-11-06 17:55:17 +01:00
const Keys = {
lastSyncPrefix: 'lastSync_',
};
export default class SyncService {
syncInProgress: boolean = false;
constructor(private userService: UserService, private apiService: ApiService,
2017-11-16 18:49:23 +01:00
private settingsService: SettingsService, private folderService: FolderService,
private cipherService: CipherService, private cryptoService: CryptoService,
2018-01-04 16:51:08 +01:00
private collectionService: CollectionService, private storageService: StorageService,
2018-01-04 22:06:00 +01:00
private messagingService: MessagingService, private logoutCallback: Function) {
2017-11-06 17:55:17 +01:00
}
async getLastSync() {
const userId = await this.userService.getUserId();
2018-01-04 16:51:08 +01:00
const lastSync = await this.storageService.get<any>(Keys.lastSyncPrefix + userId);
2017-11-06 17:55:17 +01:00
if (lastSync) {
return new Date(lastSync);
}
return null;
}
async setLastSync(date: Date) {
const userId = await this.userService.getUserId();
2018-01-04 16:51:08 +01:00
await this.storageService.save(Keys.lastSyncPrefix + userId, date.toJSON());
2017-11-06 17:55:17 +01:00
}
syncStarted() {
this.syncInProgress = true;
2018-01-04 22:06:00 +01:00
this.messagingService.send('syncStarted');
2017-11-06 17:55:17 +01:00
}
syncCompleted(successfully: boolean) {
this.syncInProgress = false;
2018-01-04 22:06:00 +01:00
this.messagingService.send('syncCompleted', { successfully: successfully });
2017-11-06 17:55:17 +01:00
}
async fullSync(forceSync: boolean) {
this.syncStarted();
const isAuthenticated = await this.userService.isAuthenticated();
if (!isAuthenticated) {
this.syncCompleted(false);
return false;
}
const now = new Date();
const needsSyncResult = await this.needsSyncing(forceSync);
const needsSync = needsSyncResult[0];
const skipped = needsSyncResult[1];
if (skipped) {
this.syncCompleted(false);
return false;
}
if (!needsSync) {
await this.setLastSync(now);
this.syncCompleted(false);
return false;
}
const userId = await this.userService.getUserId();
try {
const response = await this.apiService.getSync();
await this.syncProfile(response.profile);
await this.syncFolders(userId, response.folders);
2017-11-22 19:17:59 +01:00
await this.syncCollections(response.collections);
2017-11-06 17:55:17 +01:00
await this.syncCiphers(userId, response.ciphers);
await this.syncSettings(userId, response.domains);
await this.setLastSync(now);
this.syncCompleted(true);
return true;
} catch (e) {
this.syncCompleted(false);
return false;
}
}
// Helpers
private async needsSyncing(forceSync: boolean) {
if (forceSync) {
return [true, false];
}
try {
const response = await this.apiService.getAccountRevisionDate();
const accountRevisionDate = new Date(response);
const lastSync = await this.getLastSync();
if (lastSync != null && accountRevisionDate <= lastSync) {
return [false, false];
}
return [true, false];
} catch (e) {
return [false, true];
}
}
private async syncProfile(response: ProfileResponse) {
const stamp = await this.userService.getSecurityStamp();
if (stamp != null && stamp !== response.securityStamp) {
if (this.logoutCallback != null) {
this.logoutCallback(true);
}
throw new Error('Stamp has changed');
}
await this.cryptoService.setEncKey(response.key);
await this.cryptoService.setEncPrivateKey(response.privateKey);
await this.cryptoService.setOrgKeys(response.organizations);
await this.userService.setSecurityStamp(response.securityStamp);
}
private async syncFolders(userId: string, response: FolderResponse[]) {
const folders: { [id: string]: FolderData; } = {};
2017-11-16 18:49:23 +01:00
response.forEach((f) => {
2017-11-06 17:55:17 +01:00
folders[f.id] = new FolderData(f, userId);
2017-11-16 18:49:23 +01:00
});
2017-11-06 17:55:17 +01:00
return await this.folderService.replace(folders);
}
2017-11-22 19:17:59 +01:00
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);
}
2017-11-06 17:55:17 +01:00
private async syncCiphers(userId: string, response: CipherResponse[]) {
const ciphers: { [id: string]: CipherData; } = {};
2017-11-16 18:49:23 +01:00
response.forEach((c) => {
2017-11-06 17:55:17 +01:00
ciphers[c.id] = new CipherData(c, userId);
2017-11-16 18:49:23 +01:00
});
2017-11-06 17:55:17 +01:00
return await this.cipherService.replace(ciphers);
}
private async syncSettings(userId: string, response: DomainsResponse) {
let eqDomains: string[][] = [];
if (response != null && response.equivalentDomains != null) {
eqDomains = eqDomains.concat(response.equivalentDomains);
}
if (response != null && response.globalEquivalentDomains != null) {
2017-11-16 18:49:23 +01:00
response.globalEquivalentDomains.forEach((global) => {
2017-11-06 17:55:17 +01:00
if (global.domains.length > 0) {
eqDomains.push(global.domains);
}
2017-11-16 18:49:23 +01:00
});
2017-11-06 17:55:17 +01:00
}
return this.settingsService.setEquivalentDomains(eqDomains);
}
}