2018-02-19 19:07:19 +01:00
|
|
|
import { ApiService } from '../abstractions/api.service';
|
|
|
|
import { CipherService } from '../abstractions/cipher.service';
|
|
|
|
import { CollectionService } from '../abstractions/collection.service';
|
|
|
|
import { CryptoService } from '../abstractions/crypto.service';
|
|
|
|
import { FolderService } from '../abstractions/folder.service';
|
|
|
|
import { MessagingService } from '../abstractions/messaging.service';
|
2020-01-29 04:24:02 +01:00
|
|
|
import { PolicyService } from '../abstractions/policy.service';
|
2018-02-19 19:07:19 +01:00
|
|
|
import { SettingsService } from '../abstractions/settings.service';
|
|
|
|
import { StorageService } from '../abstractions/storage.service';
|
|
|
|
import { SyncService as SyncServiceAbstraction } from '../abstractions/sync.service';
|
|
|
|
import { UserService } from '../abstractions/user.service';
|
2018-01-10 05:25:59 +01:00
|
|
|
|
2018-02-19 19:07:19 +01:00
|
|
|
import { CipherData } from '../models/data/cipherData';
|
|
|
|
import { CollectionData } from '../models/data/collectionData';
|
|
|
|
import { FolderData } from '../models/data/folderData';
|
2018-05-18 21:26:46 +02:00
|
|
|
import { OrganizationData } from '../models/data/organizationData';
|
2020-01-29 04:24:02 +01:00
|
|
|
import { PolicyData } from '../models/data/policyData';
|
2018-01-10 05:25:59 +01:00
|
|
|
|
2018-02-19 19:07:19 +01:00
|
|
|
import { CipherResponse } from '../models/response/cipherResponse';
|
2018-07-06 18:40:43 +02:00
|
|
|
import { CollectionDetailsResponse } from '../models/response/collectionResponse';
|
2018-02-19 19:07:19 +01:00
|
|
|
import { DomainsResponse } from '../models/response/domainsResponse';
|
|
|
|
import { FolderResponse } from '../models/response/folderResponse';
|
2018-08-20 22:01:26 +02:00
|
|
|
import {
|
|
|
|
SyncCipherNotification,
|
|
|
|
SyncFolderNotification,
|
|
|
|
} from '../models/response/notificationResponse';
|
2020-01-29 04:24:02 +01:00
|
|
|
import { PolicyResponse } from '../models/response/policyResponse';
|
2018-02-19 19:07:19 +01:00
|
|
|
import { ProfileResponse } from '../models/response/profileResponse';
|
2018-01-10 05:25:59 +01:00
|
|
|
|
|
|
|
const Keys = {
|
|
|
|
lastSyncPrefix: 'lastSync_',
|
|
|
|
};
|
|
|
|
|
2018-02-08 16:49:00 +01:00
|
|
|
export class SyncService implements SyncServiceAbstraction {
|
2018-01-10 05:25:59 +01:00
|
|
|
syncInProgress: boolean = false;
|
|
|
|
|
|
|
|
constructor(private userService: UserService, private apiService: ApiService,
|
|
|
|
private settingsService: SettingsService, private folderService: FolderService,
|
|
|
|
private cipherService: CipherService, private cryptoService: CryptoService,
|
|
|
|
private collectionService: CollectionService, private storageService: StorageService,
|
2020-01-29 04:24:02 +01:00
|
|
|
private messagingService: MessagingService, private policyService: PolicyService,
|
|
|
|
private logoutCallback: (expired: boolean) => Promise<void>) {
|
2018-01-10 05:25:59 +01:00
|
|
|
}
|
|
|
|
|
2018-01-10 05:34:33 +01:00
|
|
|
async getLastSync(): Promise<Date> {
|
2018-01-10 05:25:59 +01:00
|
|
|
const userId = await this.userService.getUserId();
|
2018-02-15 19:39:23 +01:00
|
|
|
if (userId == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-01-10 05:25:59 +01:00
|
|
|
const lastSync = await this.storageService.get<any>(Keys.lastSyncPrefix + userId);
|
|
|
|
if (lastSync) {
|
|
|
|
return new Date(lastSync);
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-01-10 05:34:33 +01:00
|
|
|
async setLastSync(date: Date): Promise<any> {
|
2018-01-10 05:25:59 +01:00
|
|
|
const userId = await this.userService.getUserId();
|
2018-02-15 19:39:23 +01:00
|
|
|
if (userId == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-10 05:25:59 +01:00
|
|
|
await this.storageService.save(Keys.lastSyncPrefix + userId, date.toJSON());
|
|
|
|
}
|
|
|
|
|
2019-10-15 17:06:55 +02:00
|
|
|
async fullSync(forceSync: boolean, allowThrowOnError = false): Promise<boolean> {
|
2018-01-10 05:25:59 +01:00
|
|
|
this.syncStarted();
|
|
|
|
const isAuthenticated = await this.userService.isAuthenticated();
|
|
|
|
if (!isAuthenticated) {
|
2018-08-20 22:01:26 +02:00
|
|
|
return this.syncCompleted(false);
|
2018-01-10 05:25:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const now = new Date();
|
|
|
|
const needsSyncResult = await this.needsSyncing(forceSync);
|
|
|
|
const needsSync = needsSyncResult[0];
|
|
|
|
const skipped = needsSyncResult[1];
|
|
|
|
|
|
|
|
if (skipped) {
|
2018-08-20 22:01:26 +02:00
|
|
|
return this.syncCompleted(false);
|
2018-01-10 05:25:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!needsSync) {
|
|
|
|
await this.setLastSync(now);
|
2018-08-20 22:01:26 +02:00
|
|
|
return this.syncCompleted(false);
|
2018-01-10 05:25:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const userId = await this.userService.getUserId();
|
|
|
|
try {
|
|
|
|
const response = await this.apiService.getSync();
|
|
|
|
|
|
|
|
await this.syncProfile(response.profile);
|
|
|
|
await this.syncFolders(userId, response.folders);
|
|
|
|
await this.syncCollections(response.collections);
|
|
|
|
await this.syncCiphers(userId, response.ciphers);
|
|
|
|
await this.syncSettings(userId, response.domains);
|
2020-01-29 04:24:02 +01:00
|
|
|
await this.syncPolicies(response.policies);
|
2018-01-10 05:25:59 +01:00
|
|
|
|
|
|
|
await this.setLastSync(now);
|
2018-08-20 22:01:26 +02:00
|
|
|
return this.syncCompleted(true);
|
|
|
|
} catch (e) {
|
2019-10-15 17:06:55 +02:00
|
|
|
if (allowThrowOnError) {
|
|
|
|
throw e;
|
|
|
|
} else {
|
|
|
|
return this.syncCompleted(false);
|
|
|
|
}
|
2018-08-20 22:01:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-21 14:20:43 +02:00
|
|
|
async syncUpsertFolder(notification: SyncFolderNotification, isEdit: boolean): Promise<boolean> {
|
2018-08-20 22:01:26 +02:00
|
|
|
this.syncStarted();
|
|
|
|
if (await this.userService.isAuthenticated()) {
|
|
|
|
try {
|
|
|
|
const localFolder = await this.folderService.get(notification.id);
|
2018-08-21 14:20:43 +02:00
|
|
|
if ((!isEdit && localFolder == null) ||
|
|
|
|
(isEdit && localFolder != null && localFolder.revisionDate < notification.revisionDate)) {
|
|
|
|
const remoteFolder = await this.apiService.getFolder(notification.id);
|
|
|
|
if (remoteFolder != null) {
|
|
|
|
const userId = await this.userService.getUserId();
|
|
|
|
await this.folderService.upsert(new FolderData(remoteFolder, userId));
|
|
|
|
this.messagingService.send('syncedUpsertedFolder', { folderId: notification.id });
|
|
|
|
return this.syncCompleted(true);
|
|
|
|
}
|
2018-08-20 22:01:26 +02:00
|
|
|
}
|
|
|
|
} catch { }
|
|
|
|
}
|
|
|
|
return this.syncCompleted(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
async syncDeleteFolder(notification: SyncFolderNotification): Promise<boolean> {
|
|
|
|
this.syncStarted();
|
|
|
|
if (await this.userService.isAuthenticated()) {
|
|
|
|
await this.folderService.delete(notification.id);
|
|
|
|
this.messagingService.send('syncedDeletedFolder', { folderId: notification.id });
|
2018-01-10 05:25:59 +01:00
|
|
|
this.syncCompleted(true);
|
|
|
|
return true;
|
|
|
|
}
|
2018-08-20 22:01:26 +02:00
|
|
|
return this.syncCompleted(false);
|
|
|
|
}
|
|
|
|
|
2018-08-21 14:20:43 +02:00
|
|
|
async syncUpsertCipher(notification: SyncCipherNotification, isEdit: boolean): Promise<boolean> {
|
2018-08-20 22:01:26 +02:00
|
|
|
this.syncStarted();
|
|
|
|
if (await this.userService.isAuthenticated()) {
|
|
|
|
try {
|
2018-08-21 15:25:16 +02:00
|
|
|
let shouldUpdate = true;
|
2018-08-20 22:01:26 +02:00
|
|
|
const localCipher = await this.cipherService.get(notification.id);
|
2018-08-21 15:25:16 +02:00
|
|
|
if (localCipher != null && localCipher.revisionDate >= notification.revisionDate) {
|
|
|
|
shouldUpdate = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
let checkCollections = false;
|
|
|
|
if (shouldUpdate) {
|
|
|
|
if (isEdit) {
|
|
|
|
shouldUpdate = localCipher != null;
|
|
|
|
checkCollections = true;
|
|
|
|
} else {
|
|
|
|
if (notification.collectionIds == null || notification.organizationId == null) {
|
|
|
|
shouldUpdate = localCipher == null;
|
|
|
|
} else {
|
|
|
|
shouldUpdate = false;
|
|
|
|
checkCollections = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!shouldUpdate && checkCollections && notification.organizationId != null &&
|
|
|
|
notification.collectionIds != null && notification.collectionIds.length > 0) {
|
|
|
|
const collections = await this.collectionService.getAll();
|
|
|
|
if (collections != null) {
|
|
|
|
for (let i = 0; i < collections.length; i++) {
|
2019-04-17 18:13:04 +02:00
|
|
|
if (notification.collectionIds.indexOf(collections[i].id) > -1) {
|
2018-08-21 15:25:16 +02:00
|
|
|
shouldUpdate = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (shouldUpdate) {
|
2018-08-21 14:20:43 +02:00
|
|
|
const remoteCipher = await this.apiService.getCipher(notification.id);
|
|
|
|
if (remoteCipher != null) {
|
|
|
|
const userId = await this.userService.getUserId();
|
|
|
|
await this.cipherService.upsert(new CipherData(remoteCipher, userId));
|
|
|
|
this.messagingService.send('syncedUpsertedCipher', { cipherId: notification.id });
|
|
|
|
return this.syncCompleted(true);
|
|
|
|
}
|
2018-08-20 22:01:26 +02:00
|
|
|
}
|
2018-08-22 05:10:12 +02:00
|
|
|
} catch (e) {
|
|
|
|
if (e != null && e.statusCode === 404 && isEdit) {
|
|
|
|
await this.cipherService.delete(notification.id);
|
|
|
|
this.messagingService.send('syncedDeletedCipher', { cipherId: notification.id });
|
|
|
|
return this.syncCompleted(true);
|
|
|
|
}
|
|
|
|
}
|
2018-08-20 22:01:26 +02:00
|
|
|
}
|
|
|
|
return this.syncCompleted(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
async syncDeleteCipher(notification: SyncCipherNotification): Promise<boolean> {
|
|
|
|
this.syncStarted();
|
|
|
|
if (await this.userService.isAuthenticated()) {
|
|
|
|
await this.cipherService.delete(notification.id);
|
|
|
|
this.messagingService.send('syncedDeletedCipher', { cipherId: notification.id });
|
|
|
|
return this.syncCompleted(true);
|
|
|
|
}
|
|
|
|
return this.syncCompleted(false);
|
2018-01-10 05:25:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Helpers
|
|
|
|
|
2018-08-20 22:01:26 +02:00
|
|
|
private syncStarted() {
|
|
|
|
this.syncInProgress = true;
|
|
|
|
this.messagingService.send('syncStarted');
|
|
|
|
}
|
|
|
|
|
|
|
|
private syncCompleted(successfully: boolean): boolean {
|
|
|
|
this.syncInProgress = false;
|
|
|
|
this.messagingService.send('syncCompleted', { successfully: successfully });
|
|
|
|
return successfully;
|
|
|
|
}
|
|
|
|
|
2018-01-10 05:25:59 +01:00
|
|
|
private async needsSyncing(forceSync: boolean) {
|
|
|
|
if (forceSync) {
|
|
|
|
return [true, false];
|
|
|
|
}
|
|
|
|
|
2018-08-31 23:22:38 +02:00
|
|
|
const lastSync = await this.getLastSync();
|
|
|
|
if (lastSync == null || lastSync.getTime() === 0) {
|
|
|
|
return [true, false];
|
|
|
|
}
|
|
|
|
|
2018-01-10 05:25:59 +01:00
|
|
|
try {
|
|
|
|
const response = await this.apiService.getAccountRevisionDate();
|
2018-08-31 23:22:38 +02:00
|
|
|
if (new Date(response) <= lastSync) {
|
2018-01-10 05:25:59 +01:00
|
|
|
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) {
|
2018-05-16 05:40:15 +02:00
|
|
|
await this.logoutCallback(true);
|
2018-01-10 05:25:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2018-05-18 21:26:46 +02:00
|
|
|
|
|
|
|
const organizations: { [id: string]: OrganizationData; } = {};
|
|
|
|
response.organizations.forEach((o) => {
|
|
|
|
organizations[o.id] = new OrganizationData(o);
|
|
|
|
});
|
|
|
|
return await this.userService.replaceOrganizations(organizations);
|
2018-01-10 05:25:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private async syncFolders(userId: string, response: FolderResponse[]) {
|
|
|
|
const folders: { [id: string]: FolderData; } = {};
|
|
|
|
response.forEach((f) => {
|
|
|
|
folders[f.id] = new FolderData(f, userId);
|
|
|
|
});
|
|
|
|
return await this.folderService.replace(folders);
|
|
|
|
}
|
|
|
|
|
2018-07-06 18:40:43 +02:00
|
|
|
private async syncCollections(response: CollectionDetailsResponse[]) {
|
2018-01-10 05:25:59 +01:00
|
|
|
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[]) {
|
|
|
|
const ciphers: { [id: string]: CipherData; } = {};
|
|
|
|
response.forEach((c) => {
|
|
|
|
ciphers[c.id] = new CipherData(c, userId);
|
|
|
|
});
|
|
|
|
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) {
|
|
|
|
response.globalEquivalentDomains.forEach((global) => {
|
|
|
|
if (global.domains.length > 0) {
|
|
|
|
eqDomains.push(global.domains);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.settingsService.setEquivalentDomains(eqDomains);
|
|
|
|
}
|
2020-01-29 04:24:02 +01:00
|
|
|
|
|
|
|
private async syncPolicies(response: PolicyResponse[]) {
|
|
|
|
const policies: { [id: string]: PolicyData; } = {};
|
2020-03-12 20:16:22 +01:00
|
|
|
if (response != null) {
|
|
|
|
response.forEach((p) => {
|
|
|
|
policies[p.id] = new PolicyData(p);
|
|
|
|
});
|
|
|
|
}
|
2020-01-29 04:24:02 +01:00
|
|
|
return await this.policyService.replace(policies);
|
|
|
|
}
|
2018-01-10 05:25:59 +01:00
|
|
|
}
|