mirror of
https://github.com/bitwarden/browser.git
synced 2025-01-16 20:21:31 +01:00
move sync service to jslib
This commit is contained in:
parent
7ca71cb9a1
commit
482a1c6d57
@ -13,6 +13,7 @@ import {
|
|||||||
LockService,
|
LockService,
|
||||||
PasswordGenerationService,
|
PasswordGenerationService,
|
||||||
SettingsService,
|
SettingsService,
|
||||||
|
SyncService,
|
||||||
TokenService,
|
TokenService,
|
||||||
TotpService,
|
TotpService,
|
||||||
UserService,
|
UserService,
|
||||||
@ -33,6 +34,7 @@ import {
|
|||||||
PlatformUtilsService as PlatformUtilsServiceAbstraction,
|
PlatformUtilsService as PlatformUtilsServiceAbstraction,
|
||||||
SettingsService as SettingsServiceAbstraction,
|
SettingsService as SettingsServiceAbstraction,
|
||||||
StorageService as StorageServiceAbstraction,
|
StorageService as StorageServiceAbstraction,
|
||||||
|
SyncService as SyncServiceAbstraction,
|
||||||
TokenService as TokenServiceAbstraction,
|
TokenService as TokenServiceAbstraction,
|
||||||
TotpService as TotpServiceAbstraction,
|
TotpService as TotpServiceAbstraction,
|
||||||
UserService as UserServiceAbstraction,
|
UserService as UserServiceAbstraction,
|
||||||
@ -54,7 +56,6 @@ import BrowserMessagingService from '../services/browserMessaging.service';
|
|||||||
import BrowserPlatformUtilsService from '../services/browserPlatformUtils.service';
|
import BrowserPlatformUtilsService from '../services/browserPlatformUtils.service';
|
||||||
import BrowserStorageService from '../services/browserStorage.service';
|
import BrowserStorageService from '../services/browserStorage.service';
|
||||||
import i18nService from '../services/i18n.service';
|
import i18nService from '../services/i18n.service';
|
||||||
import SyncService from '../services/sync.service';
|
|
||||||
|
|
||||||
export default class MainBackground {
|
export default class MainBackground {
|
||||||
messagingService: MessagingServiceAbstraction;
|
messagingService: MessagingServiceAbstraction;
|
||||||
@ -74,7 +75,7 @@ export default class MainBackground {
|
|||||||
folderService: FolderServiceAbstraction;
|
folderService: FolderServiceAbstraction;
|
||||||
collectionService: CollectionServiceAbstraction;
|
collectionService: CollectionServiceAbstraction;
|
||||||
lockService: LockServiceAbstraction;
|
lockService: LockServiceAbstraction;
|
||||||
syncService: SyncService;
|
syncService: SyncServiceAbstraction;
|
||||||
passwordGenerationService: PasswordGenerationServiceAbstraction;
|
passwordGenerationService: PasswordGenerationServiceAbstraction;
|
||||||
totpService: TotpServiceAbstraction;
|
totpService: TotpServiceAbstraction;
|
||||||
autofillService: AutofillService;
|
autofillService: AutofillService;
|
||||||
|
@ -12,6 +12,7 @@ import { PasswordGenerationService } from 'jslib/abstractions/passwordGeneration
|
|||||||
import { PlatformUtilsService } from 'jslib/abstractions/platformUtils.service';
|
import { PlatformUtilsService } from 'jslib/abstractions/platformUtils.service';
|
||||||
import { SettingsService } from 'jslib/abstractions/settings.service';
|
import { SettingsService } from 'jslib/abstractions/settings.service';
|
||||||
import { StorageService } from 'jslib/abstractions/storage.service';
|
import { StorageService } from 'jslib/abstractions/storage.service';
|
||||||
|
import { SyncService } from 'jslib/abstractions/sync.service';
|
||||||
import { TokenService } from 'jslib/abstractions/token.service';
|
import { TokenService } from 'jslib/abstractions/token.service';
|
||||||
import { TotpService } from 'jslib/abstractions/totp.service';
|
import { TotpService } from 'jslib/abstractions/totp.service';
|
||||||
import { UserService } from 'jslib/abstractions/user.service';
|
import { UserService } from 'jslib/abstractions/user.service';
|
||||||
@ -31,7 +32,7 @@ export const userService = getBackgroundService<UserService>('userService');
|
|||||||
export const apiService = getBackgroundService<ApiService>('apiService');
|
export const apiService = getBackgroundService<ApiService>('apiService');
|
||||||
export const folderService = getBackgroundService<FolderService>('folderService');
|
export const folderService = getBackgroundService<FolderService>('folderService');
|
||||||
export const cipherService = getBackgroundService<CipherService>('cipherService');
|
export const cipherService = getBackgroundService<CipherService>('cipherService');
|
||||||
export const syncService = getBackgroundService<any>('syncService');
|
export const syncService = getBackgroundService<SyncService>('syncService');
|
||||||
export const autofillService = getBackgroundService<any>('autofillService');
|
export const autofillService = getBackgroundService<any>('autofillService');
|
||||||
export const passwordGenerationService = getBackgroundService<PasswordGenerationService>('passwordGenerationService');
|
export const passwordGenerationService = getBackgroundService<PasswordGenerationService>('passwordGenerationService');
|
||||||
export const platformUtilsService = getBackgroundService<PlatformUtilsService>('platformUtilsService');
|
export const platformUtilsService = getBackgroundService<PlatformUtilsService>('platformUtilsService');
|
||||||
|
@ -1,186 +0,0 @@
|
|||||||
import {
|
|
||||||
ApiService,
|
|
||||||
CipherService,
|
|
||||||
CollectionService,
|
|
||||||
CryptoService,
|
|
||||||
FolderService,
|
|
||||||
MessagingService,
|
|
||||||
SettingsService,
|
|
||||||
StorageService,
|
|
||||||
UserService,
|
|
||||||
} from 'jslib/abstractions';
|
|
||||||
|
|
||||||
import {
|
|
||||||
CipherData,
|
|
||||||
CollectionData,
|
|
||||||
FolderData,
|
|
||||||
} from 'jslib/models/data';
|
|
||||||
|
|
||||||
import {
|
|
||||||
CipherResponse,
|
|
||||||
CollectionResponse,
|
|
||||||
DomainsResponse,
|
|
||||||
FolderResponse,
|
|
||||||
ProfileResponse,
|
|
||||||
} from 'jslib/models/response';
|
|
||||||
|
|
||||||
const Keys = {
|
|
||||||
lastSyncPrefix: 'lastSync_',
|
|
||||||
};
|
|
||||||
|
|
||||||
export default class SyncService {
|
|
||||||
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,
|
|
||||||
private messagingService: MessagingService, private logoutCallback: Function) {
|
|
||||||
}
|
|
||||||
|
|
||||||
async getLastSync() {
|
|
||||||
const userId = await this.userService.getUserId();
|
|
||||||
const lastSync = await this.storageService.get<any>(Keys.lastSyncPrefix + userId);
|
|
||||||
if (lastSync) {
|
|
||||||
return new Date(lastSync);
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
async setLastSync(date: Date) {
|
|
||||||
const userId = await this.userService.getUserId();
|
|
||||||
await this.storageService.save(Keys.lastSyncPrefix + userId, date.toJSON());
|
|
||||||
}
|
|
||||||
|
|
||||||
syncStarted() {
|
|
||||||
this.syncInProgress = true;
|
|
||||||
this.messagingService.send('syncStarted');
|
|
||||||
}
|
|
||||||
|
|
||||||
syncCompleted(successfully: boolean) {
|
|
||||||
this.syncInProgress = false;
|
|
||||||
this.messagingService.send('syncCompleted', { successfully: successfully });
|
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
|
||||||
await this.syncCollections(response.collections);
|
|
||||||
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; } = {};
|
|
||||||
response.forEach((f) => {
|
|
||||||
folders[f.id] = new FolderData(f, userId);
|
|
||||||
});
|
|
||||||
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[]) {
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user