fixes and cleanup

This commit is contained in:
Kyle Spearrin 2017-11-04 23:18:38 -04:00
parent bdd40d8755
commit bd109f4ed9
6 changed files with 33 additions and 244 deletions

View File

@ -4,7 +4,6 @@
<script type="text/javascript" src="lib/q/q.js"></script> <script type="text/javascript" src="lib/q/q.js"></script>
<script type="text/javascript" src="lib/tldjs/tld.js"></script> <script type="text/javascript" src="lib/tldjs/tld.js"></script>
<script type="text/javascript" src="lib/forge/forge.js"></script> <script type="text/javascript" src="lib/forge/forge.js"></script>
<script type="text/javascript" src="services/cipherService.js"></script>
<script type="text/javascript" src="services/syncService.js"></script> <script type="text/javascript" src="services/syncService.js"></script>
<script type="text/javascript" src="services/autofillService.js"></script> <script type="text/javascript" src="services/autofillService.js"></script>
</head> </head>

View File

@ -1,6 +1,7 @@
// Service imports // Service imports
import ApiService from './services/api.service'; import ApiService from './services/api.service';
import AppIdService from './services/appId.service'; import AppIdService from './services/appId.service';
import CipherService from './services/cipherService.js';
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';
@ -100,7 +101,7 @@ 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_apiService, bg_settingsService, bg_utilsService, window.bg_cipherService = bg_cipherService = new CipherService(bg_cryptoService, bg_userService, bg_apiService, bg_settingsService, bg_utilsService,
bg_constantsService); bg_constantsService);
window.bg_folderService = bg_folderService = new FolderService(bg_cryptoService, bg_userService, bg_apiService, bg_i18nService, bg_utilsService); window.bg_folderService = bg_folderService = new FolderService(bg_cryptoService, bg_userService, bg_i18nService, bg_apiService);
window.bg_lockService = bg_lockService = new LockService(bg_constantsService, bg_cryptoService, bg_folderService, bg_cipherService, bg_utilsService, window.bg_lockService = bg_lockService = new LockService(bg_constantsService, bg_cryptoService, bg_folderService, bg_cipherService, bg_utilsService,
setIcon, refreshBadgeAndMenu); setIcon, refreshBadgeAndMenu);
window.bg_syncService = bg_syncService = new SyncService(bg_cipherService, bg_folderService, bg_userService, bg_apiService, bg_settingsService, window.bg_syncService = bg_syncService = new SyncService(bg_cipherService, bg_folderService, bg_userService, bg_apiService, bg_settingsService,

View File

@ -178,4 +178,4 @@ class Cipher extends Domain {
} }
export { Cipher }; export { Cipher };
(window as any).Attachment = Cipher; (window as any).Cipher = Cipher;

View File

@ -1,4 +1,11 @@
function CipherService(cryptoService, userService, apiService, settingsService, utilsService, constantsService) { import { Cipher } from '../models/domain/cipher';
import { CipherData } from '../models/data/cipherData';
import { CipherRequest } from '../models/request/cipherRequest';
import { CipherResponse } from '../models/response/cipherResponse';
export default function CipherService(cryptoService, userService, apiService, settingsService, utilsService, constantsService) {
this.cryptoService = cryptoService; this.cryptoService = cryptoService;
this.userService = userService; this.userService = userService;
this.apiService = apiService; this.apiService = apiService;

View File

@ -29,27 +29,32 @@ export default class FolderService {
async encrypt(model: any): Promise<Folder> { async encrypt(model: any): Promise<Folder> {
const folder = new Folder(); const folder = new Folder();
folder.id = model.id;
folder.name = await this.cryptoService.encrypt(model.name); folder.name = await this.cryptoService.encrypt(model.name);
return folder; return folder;
} }
async get(id: string): Promise<Folder> { async get(id: string): Promise<Folder> {
const userId = await this.userService.getUserId(); const userId = await this.userService.getUserId();
const folders = await UtilsService.getObjFromStorage<Map<string, FolderData>>(Keys.foldersPrefix + userId); const folders = await UtilsService.getObjFromStorage<{ [id: string]: FolderData; }>(
if (folders == null || !folders.has(id)) { Keys.foldersPrefix + userId);
if (folders == null || !folders.hasOwnProperty(id)) {
return null; return null;
} }
return new Folder(folders.get(id)); return new Folder(folders[id]);
} }
async getAll(): Promise<Folder[]> { async getAll(): Promise<Folder[]> {
const userId = await this.userService.getUserId(); const userId = await this.userService.getUserId();
const folders = await UtilsService.getObjFromStorage<Map<string, FolderData>>(Keys.foldersPrefix + userId); const folders = await UtilsService.getObjFromStorage<{ [id: string]: FolderData; }>(
Keys.foldersPrefix + userId);
const response: Folder[] = []; const response: Folder[] = [];
folders.forEach((folder) => { for (const id in folders) {
response.push(new Folder(folder)); if (folders.hasOwnProperty(id)) {
}); response.push(new Folder(folders[id]));
}
}
return response; return response;
} }
@ -99,17 +104,18 @@ export default class FolderService {
async upsert(folder: FolderData | FolderData[]): Promise<any> { async upsert(folder: FolderData | FolderData[]): Promise<any> {
const userId = await this.userService.getUserId(); const userId = await this.userService.getUserId();
let folders = await UtilsService.getObjFromStorage<Map<string, FolderData>>(Keys.foldersPrefix + userId); let folders = await UtilsService.getObjFromStorage<{ [id: string]: FolderData; }>(
Keys.foldersPrefix + userId);
if (folders == null) { if (folders == null) {
folders = new Map<string, FolderData>(); folders = {};
} }
if (folder instanceof FolderData) { if (folder instanceof FolderData) {
const f = folder as FolderData; const f = folder as FolderData;
folders.set(f.id, f); folders[f.id] = f;
} else { } else {
for (const f of (folder as FolderData[])) { for (const f of (folder as FolderData[])) {
folders.set(f.id, f); folders[f.id] = f;
} }
} }
@ -130,17 +136,18 @@ export default class FolderService {
async delete(id: string | string[]): Promise<any> { async delete(id: string | string[]): Promise<any> {
const userId = await this.userService.getUserId(); const userId = await this.userService.getUserId();
const folders = await UtilsService.getObjFromStorage<Map<string, FolderData>>(Keys.foldersPrefix + userId); const folders = await UtilsService.getObjFromStorage<{ [id: string]: FolderData; }>(
Keys.foldersPrefix + userId);
if (folders == null) { if (folders == null) {
return; return;
} }
if (id instanceof String) { if (typeof id === 'string') {
const i = id as string; const i = id as string;
folders.delete(i); delete folders[id];
} else { } else {
for (const i of (id as string[])) { for (const i of (id as string[])) {
folders.delete(i); delete folders[i];
} }
} }

View File

@ -1,225 +0,0 @@
function FolderService(cryptoService, userService, apiService, i18nService, utilsService) {
this.cryptoService = cryptoService;
this.userService = userService;
this.apiService = apiService;
this.i18nService = i18nService;
this.utilsService = utilsService;
this.decryptedFolderCache = null;
initFolderService();
}
function initFolderService() {
FolderService.prototype.clearCache = function () {
this.decryptedFolderCache = null;
};
FolderService.prototype.encrypt = function (folder) {
var model = {
id: folder.id
};
return this.cryptoService.encrypt(folder.name).then(function (cs) {
model.name = cs;
return model;
});
};
FolderService.prototype.get = function (id) {
var self = this;
return self.userService.getUserId().then(function (userId) {
return self.utilsService.getObjFromStorage('folders_' + userId);
}).then(function (folders) {
if (folders && id in folders) {
return new Folder(folders[id]);
}
return null;
});
};
FolderService.prototype.getAll = function () {
var self = this;
return self.userService.getUserId().then(function (userId) {
return self.utilsService.getObjFromStorage('folders_' + userId);
}).then(function (folders) {
var response = [];
for (var id in folders) {
var folder = folders[id];
response.push(new Folder(folder));
}
return response;
});
};
FolderService.prototype.getAllDecrypted = function () {
if (this.decryptedFolderCache) {
return Q(this.decryptedFolderCache);
}
var deferred = Q.defer(),
self = this,
decFolders = [{
id: null,
name: self.i18nService.noneFolder
}];
self.cryptoService.getKey().then(function (key) {
if (!key) {
deferred.reject();
return true;
}
return self.getAll();
}).then(function (folders) {
if (folders === true) {
return;
}
var promises = [];
for (var i = 0; i < folders.length; i++) {
/* jshint ignore:start */
promises.push(folders[i].decrypt().then(function (folder) {
decFolders.push(folder);
}));
/* jshint ignore:end */
}
return Q.all(promises);
}).then(function (stop) {
if (stop === true) {
return;
}
self.decryptedFolderCache = decFolders;
deferred.resolve(self.decryptedFolderCache);
});
return deferred.promise;
};
FolderService.prototype.saveWithServer = function (folder) {
var deferred = Q.defer(),
self = this,
request = new FolderRequest(folder);
if (!folder.id) {
self.apiService.postFolder(request).then(apiSuccess, function (response) {
deferred.reject(response);
});
}
else {
self.apiService.putFolder(folder.id, request).then(apiSuccess, function (response) {
deferred.reject(response);
});
}
function apiSuccess(response) {
folder.id = response.id;
self.userService.getUserId().then(function (userId) {
var data = new FolderData(response, userId);
return self.upsert(data);
}).then(function () {
deferred.resolve(folder);
});
}
return deferred.promise;
};
FolderService.prototype.upsert = function (folder) {
var self = this,
key = null;
return self.userService.getUserId().then(function (userId) {
key = 'folders_' + userId;
return self.utilsService.getObjFromStorage(key);
}).then(function (folders) {
if (!folders) {
folders = {};
}
if (folder.constructor === Array) {
for (var i = 0; i < folder.length; i++) {
folders[folder[i].id] = folder[i];
}
}
else {
folders[folder.id] = folder;
}
return self.utilsService.saveObjToStorage(key, folders);
}).then(function () {
self.decryptedFolderCache = null;
});
};
FolderService.prototype.replace = function (folders) {
var self = this;
return self.userService.getUserId().then(function (userId) {
return self.utilsService.saveObjToStorage('folders_' + userId, folders);
}).then(function () {
self.decryptedFolderCache = null;
});
};
FolderService.prototype.clear = function (userId) {
var self = this;
return self.utilsService.removeFromStorage('folders_' + userId).then(function () {
self.decryptedFolderCache = null;
});
};
FolderService.prototype.delete = function (id) {
var self = this,
key = null;
// TODO: Delete folder reference for associated ciphers
return self.userService.getUserId().then(function (userId) {
key = 'folders_' + userId;
return self.utilsService.getObjFromStorage(key);
}).then(function (folders) {
if (!folders) {
return null;
}
if (id.constructor === Array) {
for (var i = 0; i < id.length; i++) {
if (id[i] in folders) {
delete folders[id[i]];
}
}
}
else if (id in folders) {
delete folders[id];
}
else {
return null;
}
return folders;
}).then(function (folders) {
if (!folders) {
return false;
}
return self.utilsService.saveObjToStorage(key, folders);
}).then(function (clearCache) {
if (clearCache !== false) {
self.decryptedFolderCache = null;
}
});
};
FolderService.prototype.deleteWithServer = function (id) {
var self = this;
return self.apiService.deleteFolder(id).then(function () {
return self.delete(id);
});
};
}