2017-10-17 05:11:32 +02:00
|
|
|
function CipherService(cryptoService, userService, apiService, settingsService, utilsService, constantsService) {
|
2017-01-04 00:40:07 +01:00
|
|
|
this.cryptoService = cryptoService;
|
|
|
|
this.userService = userService;
|
|
|
|
this.apiService = apiService;
|
2017-01-14 19:43:59 +01:00
|
|
|
this.settingsService = settingsService;
|
2017-10-13 23:07:20 +02:00
|
|
|
this.utilsService = utilsService;
|
2017-10-14 20:27:14 +02:00
|
|
|
this.constantsService = constantsService;
|
2017-10-13 23:07:20 +02:00
|
|
|
this.decryptedCipherCache = null;
|
|
|
|
this.localDataKey = 'sitesLocalData';
|
2017-10-14 20:27:14 +02:00
|
|
|
this.neverDomainsKey = 'neverDomains';
|
2017-01-04 00:40:07 +01:00
|
|
|
|
2017-10-17 05:11:32 +02:00
|
|
|
initCipherService();
|
2017-07-14 21:34:05 +02:00
|
|
|
}
|
2017-01-04 00:40:07 +01:00
|
|
|
|
2017-10-17 05:11:32 +02:00
|
|
|
function initCipherService() {
|
|
|
|
CipherService.prototype.clearCache = function () {
|
2017-10-13 23:07:20 +02:00
|
|
|
this.decryptedCipherCache = null;
|
2017-01-04 00:40:07 +01:00
|
|
|
};
|
|
|
|
|
2017-10-17 05:11:32 +02:00
|
|
|
CipherService.prototype.encrypt = function (cipher) {
|
2017-06-27 05:55:51 +02:00
|
|
|
var self = this;
|
|
|
|
|
2017-01-04 00:40:07 +01:00
|
|
|
var model = {
|
2017-10-17 05:11:32 +02:00
|
|
|
id: cipher.id,
|
|
|
|
folderId: cipher.folderId,
|
|
|
|
favorite: cipher.favorite,
|
|
|
|
organizationId: cipher.organizationId,
|
|
|
|
type: cipher.type
|
2017-01-04 00:40:07 +01:00
|
|
|
};
|
|
|
|
|
2017-10-17 05:11:32 +02:00
|
|
|
return self.cryptoService.getOrgKey(cipher.organizationId).then(function (key) {
|
2017-10-14 04:34:36 +02:00
|
|
|
return Q.all([
|
2017-10-17 05:11:32 +02:00
|
|
|
encryptObjProperty(cipher, model, {
|
2017-10-14 20:27:14 +02:00
|
|
|
name: null,
|
|
|
|
notes: null
|
2017-10-14 04:34:36 +02:00
|
|
|
}, key, self),
|
2017-10-17 05:11:32 +02:00
|
|
|
encryptCipherData(cipher, model, key, self),
|
|
|
|
self.encryptFields(cipher.fields, key).then(function (fields) {
|
2017-10-14 04:34:36 +02:00
|
|
|
model.fields = fields;
|
|
|
|
})
|
|
|
|
]);
|
|
|
|
}).then(function () {
|
2017-09-22 04:45:24 +02:00
|
|
|
return model;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-10-16 16:09:17 +02:00
|
|
|
function encryptCipherData(cipher, model, key, self) {
|
|
|
|
switch (cipher.type) {
|
|
|
|
case self.constantsService.cipherType.login:
|
|
|
|
model.login = {};
|
|
|
|
return encryptObjProperty(cipher.login, model.login, {
|
|
|
|
uri: null,
|
|
|
|
username: null,
|
|
|
|
password: null,
|
|
|
|
totp: null
|
|
|
|
}, key, self);
|
|
|
|
case self.constantsService.cipherType.secureNote:
|
|
|
|
model.secureNote = {
|
|
|
|
type: cipher.secureNote.type
|
|
|
|
};
|
|
|
|
return Q();
|
|
|
|
case self.constantsService.cipherType.card:
|
|
|
|
model.card = {};
|
|
|
|
return encryptObjProperty(cipher.card, model.card, {
|
|
|
|
cardholderName: null,
|
|
|
|
brand: null,
|
|
|
|
number: null,
|
|
|
|
expMonth: null,
|
|
|
|
expYear: null,
|
|
|
|
code: null
|
|
|
|
}, key, self);
|
|
|
|
case self.constantsService.cipherType.identity:
|
|
|
|
model.identity = {};
|
|
|
|
return encryptObjProperty(cipher.identity, model.identity, {
|
|
|
|
title: null,
|
|
|
|
firstName: null,
|
|
|
|
middleName: null,
|
|
|
|
lastName: null,
|
|
|
|
address1: null,
|
|
|
|
address2: null,
|
|
|
|
address3: null,
|
|
|
|
city: null,
|
|
|
|
state: null,
|
|
|
|
postalCode: null,
|
|
|
|
country: null,
|
|
|
|
company: null,
|
|
|
|
email: null,
|
|
|
|
phone: null,
|
|
|
|
ssn: null,
|
|
|
|
username: null,
|
|
|
|
passportNumber: null,
|
|
|
|
licenseNumber: null
|
|
|
|
}, key, self);
|
|
|
|
default:
|
|
|
|
throw 'Unknown type.';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-17 05:11:32 +02:00
|
|
|
CipherService.prototype.encryptFields = function (fields, key) {
|
2017-09-22 04:45:24 +02:00
|
|
|
var self = this;
|
|
|
|
if (!fields || !fields.length) {
|
2017-10-16 16:09:17 +02:00
|
|
|
return Q(null);
|
2017-09-22 04:45:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var encFields = [];
|
|
|
|
return fields.reduce(function (promise, field) {
|
|
|
|
return promise.then(function () {
|
|
|
|
return self.encryptField(field, key);
|
|
|
|
}).then(function (encField) {
|
|
|
|
encFields.push(encField);
|
|
|
|
});
|
|
|
|
}, Q()).then(function () {
|
|
|
|
return encFields;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-10-17 05:11:32 +02:00
|
|
|
CipherService.prototype.encryptField = function (field, key) {
|
2017-09-22 04:45:24 +02:00
|
|
|
var self = this;
|
|
|
|
|
|
|
|
var model = {
|
|
|
|
type: field.type
|
|
|
|
};
|
|
|
|
|
2017-10-14 04:34:36 +02:00
|
|
|
return encryptObjProperty(field, model, {
|
2017-10-14 20:27:14 +02:00
|
|
|
name: null,
|
|
|
|
value: null
|
2017-10-14 04:34:36 +02:00
|
|
|
}, key, self).then(function () {
|
2017-01-04 00:40:07 +01:00
|
|
|
return model;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-10-14 04:34:36 +02:00
|
|
|
function encryptObjProperty(obj, model, map, key, self) {
|
|
|
|
var promises = [];
|
|
|
|
|
|
|
|
for (var prop in map) {
|
|
|
|
if (map.hasOwnProperty(prop)) {
|
|
|
|
/* jshint ignore:start */
|
2017-10-16 16:09:17 +02:00
|
|
|
(function (theProp, theModel) {
|
2017-10-14 04:34:36 +02:00
|
|
|
var promise = Q().then(function () {
|
2017-10-16 16:09:17 +02:00
|
|
|
var objProp = obj[(map[theProp] || theProp)];
|
|
|
|
if (objProp && objProp !== '') {
|
|
|
|
return self.cryptoService.encrypt(objProp, key);
|
2017-10-14 04:34:36 +02:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}).then(function (val) {
|
2017-10-16 16:09:17 +02:00
|
|
|
theModel[theProp] = val;
|
2017-10-14 04:34:36 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
promises.push(promise);
|
2017-10-16 16:09:17 +02:00
|
|
|
})(prop, model);
|
2017-10-14 04:34:36 +02:00
|
|
|
/* jshint ignore:end */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Q.all(promises);
|
|
|
|
}
|
|
|
|
|
2017-10-17 05:11:32 +02:00
|
|
|
CipherService.prototype.get = function (id) {
|
2017-10-13 23:07:20 +02:00
|
|
|
var self = this,
|
|
|
|
key = null,
|
|
|
|
localData;
|
|
|
|
|
|
|
|
return self.userService.getUserIdPromise().then(function (userId) {
|
|
|
|
key = 'ciphers_' + userId;
|
|
|
|
return self.utilsService.getObjFromStorage(self.localDataKey);
|
|
|
|
}).then(function (data) {
|
|
|
|
localData = data;
|
|
|
|
if (!localData) {
|
|
|
|
localData = {};
|
|
|
|
}
|
|
|
|
return self.utilsService.getObjFromStorage(key);
|
|
|
|
}).then(function (ciphers) {
|
|
|
|
if (ciphers && id in ciphers) {
|
2017-10-16 16:09:17 +02:00
|
|
|
return new Cipher(ciphers[id], false, localData[id]);
|
2017-10-13 23:07:20 +02:00
|
|
|
}
|
2017-08-29 18:52:11 +02:00
|
|
|
|
2017-10-13 23:07:20 +02:00
|
|
|
return null;
|
2017-01-04 00:40:07 +01:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-10-17 05:11:32 +02:00
|
|
|
CipherService.prototype.getAll = function () {
|
2017-10-13 23:07:20 +02:00
|
|
|
var self = this,
|
|
|
|
key = null,
|
|
|
|
localData = null;
|
|
|
|
|
2017-10-14 05:11:42 +02:00
|
|
|
return self.userService.getUserIdPromise().then(function (userId) {
|
2017-10-13 23:07:20 +02:00
|
|
|
key = 'ciphers_' + userId;
|
|
|
|
return self.utilsService.getObjFromStorage(self.localDataKey);
|
|
|
|
}).then(function (data) {
|
|
|
|
localData = data;
|
|
|
|
if (!localData) {
|
|
|
|
localData = {};
|
|
|
|
}
|
|
|
|
return self.utilsService.getObjFromStorage(key);
|
2017-10-14 05:11:42 +02:00
|
|
|
}).then(function (ciphers) {
|
2017-10-13 23:07:20 +02:00
|
|
|
var response = [];
|
2017-10-14 05:11:42 +02:00
|
|
|
for (var id in ciphers) {
|
2017-10-13 23:07:20 +02:00
|
|
|
if (id) {
|
2017-10-14 05:11:42 +02:00
|
|
|
response.push(new Cipher(ciphers[id], false, localData[id]));
|
2017-01-04 00:40:07 +01:00
|
|
|
}
|
2017-10-13 23:07:20 +02:00
|
|
|
}
|
2017-01-04 00:40:07 +01:00
|
|
|
|
2017-10-13 23:07:20 +02:00
|
|
|
return response;
|
2017-01-04 00:40:07 +01:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-10-17 05:11:32 +02:00
|
|
|
CipherService.prototype.getAllDecrypted = function () {
|
2017-10-14 20:27:14 +02:00
|
|
|
if (this.decryptedCipherCache) {
|
|
|
|
return Q(this.decryptedCipherCache);
|
|
|
|
}
|
|
|
|
|
|
|
|
var deferred = Q.defer(),
|
|
|
|
decCiphers = [],
|
|
|
|
self = this;
|
2017-01-04 00:40:07 +01:00
|
|
|
|
2017-10-14 20:27:14 +02:00
|
|
|
self.cryptoService.getKey().then(function (key) {
|
2017-01-04 00:40:07 +01:00
|
|
|
if (!key) {
|
|
|
|
deferred.reject();
|
2017-10-14 20:27:14 +02:00
|
|
|
return true;
|
2017-01-04 00:40:07 +01:00
|
|
|
}
|
|
|
|
|
2017-10-14 20:27:14 +02:00
|
|
|
return self.getAll();
|
|
|
|
}).then(function (ciphers) {
|
|
|
|
if (ciphers === true) {
|
2017-10-17 06:03:27 +02:00
|
|
|
return true;
|
2017-01-04 00:40:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var promises = [];
|
2017-10-14 05:11:42 +02:00
|
|
|
for (var i = 0; i < ciphers.length; i++) {
|
2017-10-13 23:07:20 +02:00
|
|
|
/* jshint ignore:start */
|
2017-10-14 05:11:42 +02:00
|
|
|
promises.push(ciphers[i].decrypt().then(function (cipher) {
|
|
|
|
decCiphers.push(cipher);
|
2017-10-13 23:07:20 +02:00
|
|
|
}));
|
|
|
|
/* jshint ignore:end */
|
|
|
|
}
|
2017-01-04 00:40:07 +01:00
|
|
|
|
2017-10-13 23:07:20 +02:00
|
|
|
return Q.all(promises);
|
2017-10-14 20:27:14 +02:00
|
|
|
}).then(function (stop) {
|
|
|
|
if (stop === true) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-10-14 05:11:42 +02:00
|
|
|
self.decryptedCipherCache = decCiphers;
|
2017-10-14 20:27:14 +02:00
|
|
|
deferred.resolve(self.decryptedCipherCache);
|
2017-01-04 00:40:07 +01:00
|
|
|
});
|
2017-10-14 20:27:14 +02:00
|
|
|
|
|
|
|
return deferred.promise;
|
2017-01-04 00:40:07 +01:00
|
|
|
};
|
|
|
|
|
2017-10-17 05:11:32 +02:00
|
|
|
CipherService.prototype.getAllDecryptedForFolder = function (folderId) {
|
2017-10-13 23:07:20 +02:00
|
|
|
return this.getAllDecrypted().then(function (ciphers) {
|
|
|
|
var ciphersToReturn = [];
|
|
|
|
for (var i = 0; i < ciphers.length; i++) {
|
|
|
|
if (ciphers[i].folderId === folderId) {
|
|
|
|
ciphersToReturn.push(ciphers[i]);
|
2017-01-04 00:40:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-13 23:07:20 +02:00
|
|
|
return ciphersToReturn;
|
2017-01-04 00:40:07 +01:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-10-17 21:22:16 +02:00
|
|
|
CipherService.prototype.getAllDecryptedForDomain = function (domain, includeOtherTypes) {
|
2017-01-04 00:40:07 +01:00
|
|
|
var self = this;
|
|
|
|
|
2017-01-14 19:43:59 +01:00
|
|
|
var eqDomainsPromise = self.settingsService.getEquivalentDomains().then(function (eqDomains) {
|
|
|
|
var matchingDomains = [];
|
|
|
|
for (var i = 0; i < eqDomains.length; i++) {
|
2017-01-18 03:43:26 +01:00
|
|
|
if (eqDomains[i].length && eqDomains[i].indexOf(domain) >= 0) {
|
|
|
|
matchingDomains = matchingDomains.concat(eqDomains[i]);
|
2017-01-14 19:43:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-14 23:58:22 +01:00
|
|
|
if (!matchingDomains.length) {
|
|
|
|
matchingDomains.push(domain);
|
|
|
|
}
|
|
|
|
|
2017-01-14 19:43:59 +01:00
|
|
|
return matchingDomains;
|
|
|
|
});
|
|
|
|
|
2017-10-13 23:07:20 +02:00
|
|
|
return Q.all([eqDomainsPromise, self.getAllDecrypted()]).then(function (result) {
|
|
|
|
var matchingDomains = result[0],
|
|
|
|
ciphers = result[1],
|
|
|
|
ciphersToReturn = [];
|
2017-01-14 19:43:59 +01:00
|
|
|
|
2017-10-13 23:07:20 +02:00
|
|
|
for (var i = 0; i < ciphers.length; i++) {
|
2017-10-16 20:45:47 +02:00
|
|
|
if (ciphers[i].type === self.constantsService.cipherType.login && ciphers[i].login.domain &&
|
|
|
|
matchingDomains.indexOf(ciphers[i].login.domain) > -1) {
|
2017-10-13 23:07:20 +02:00
|
|
|
ciphersToReturn.push(ciphers[i]);
|
2017-01-04 00:40:07 +01:00
|
|
|
}
|
2017-10-17 21:22:16 +02:00
|
|
|
else if (includeOtherTypes && includeOtherTypes.indexOf(ciphers[i].type) > -1) {
|
|
|
|
ciphersToReturn.push(ciphers[i]);
|
|
|
|
}
|
2017-01-04 00:40:07 +01:00
|
|
|
}
|
|
|
|
|
2017-10-13 23:07:20 +02:00
|
|
|
return ciphersToReturn;
|
2017-01-04 00:40:07 +01:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-10-17 05:11:32 +02:00
|
|
|
CipherService.prototype.getLastUsedForDomain = function (domain) {
|
2017-10-13 23:07:20 +02:00
|
|
|
var self = this,
|
|
|
|
deferred = Q.defer();
|
|
|
|
|
|
|
|
self.getAllDecryptedForDomain(domain).then(function (ciphers) {
|
|
|
|
if (!ciphers.length) {
|
2017-09-08 05:26:56 +02:00
|
|
|
deferred.reject();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-10-13 23:07:20 +02:00
|
|
|
var sortedCiphers = ciphers.sort(self.sortCiphersByLastUsed);
|
|
|
|
deferred.resolve(sortedCiphers[0]);
|
2017-09-08 05:26:56 +02:00
|
|
|
});
|
2017-10-13 23:07:20 +02:00
|
|
|
|
2017-09-08 05:26:56 +02:00
|
|
|
return deferred.promise;
|
|
|
|
};
|
|
|
|
|
2017-10-17 05:11:32 +02:00
|
|
|
CipherService.prototype.saveWithServer = function (cipher) {
|
2017-01-04 00:40:07 +01:00
|
|
|
var deferred = Q.defer();
|
|
|
|
|
|
|
|
var self = this,
|
2017-10-16 16:20:46 +02:00
|
|
|
request = new CipherRequest(cipher);
|
2017-01-04 00:40:07 +01:00
|
|
|
|
2017-10-13 23:07:20 +02:00
|
|
|
if (!cipher.id) {
|
|
|
|
self.apiService.postCipher(request).then(apiSuccess, function (response) {
|
|
|
|
deferred.reject(response);
|
2017-01-04 00:40:07 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
2017-10-13 23:07:20 +02:00
|
|
|
self.apiService.putCipher(cipher.id, request).then(apiSuccess, function (response) {
|
|
|
|
deferred.reject(response);
|
2017-01-04 00:40:07 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function apiSuccess(response) {
|
2017-10-13 23:07:20 +02:00
|
|
|
cipher.id = response.id;
|
|
|
|
self.userService.getUserIdPromise().then(function (userId) {
|
2017-10-16 16:09:17 +02:00
|
|
|
var data = new CipherData(response, userId);
|
2017-10-13 23:07:20 +02:00
|
|
|
return self.upsert(data);
|
|
|
|
}).then(function () {
|
|
|
|
deferred.resolve(cipher);
|
2017-01-04 00:40:07 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return deferred.promise;
|
|
|
|
};
|
|
|
|
|
2017-10-17 05:11:32 +02:00
|
|
|
CipherService.prototype.upsert = function (cipher) {
|
2017-10-13 23:07:20 +02:00
|
|
|
var self = this,
|
|
|
|
key = null;
|
|
|
|
|
|
|
|
return self.userService.getUserIdPromise().then(function (userId) {
|
|
|
|
key = 'ciphers_' + userId;
|
|
|
|
return self.utilsService.getObjFromStorage(key);
|
|
|
|
}).then(function (ciphers) {
|
|
|
|
if (!ciphers) {
|
|
|
|
ciphers = {};
|
|
|
|
}
|
2017-01-04 00:40:07 +01:00
|
|
|
|
2017-10-13 23:07:20 +02:00
|
|
|
if (cipher.constructor === Array) {
|
|
|
|
for (var i = 0; i < cipher.length; i++) {
|
|
|
|
ciphers[cipher[i].id] = cipher[i];
|
2017-01-04 00:40:07 +01:00
|
|
|
}
|
2017-10-13 23:07:20 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
ciphers[cipher.id] = cipher;
|
|
|
|
}
|
2017-01-04 00:40:07 +01:00
|
|
|
|
2017-10-13 23:07:20 +02:00
|
|
|
return self.utilsService.saveObjToStorage(key, ciphers);
|
|
|
|
}).then(function () {
|
|
|
|
self.decryptedCipherCache = null;
|
2017-01-04 00:40:07 +01:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-10-17 05:11:32 +02:00
|
|
|
CipherService.prototype.updateLastUsedDate = function (id) {
|
2017-08-29 18:52:11 +02:00
|
|
|
var self = this;
|
|
|
|
|
2017-10-13 23:07:20 +02:00
|
|
|
var ciphersLocalData = null;
|
|
|
|
return self.utilsService.getObjFromStorage(self.localDataKey).then(function (obj) {
|
|
|
|
ciphersLocalData = obj;
|
|
|
|
|
|
|
|
if (!ciphersLocalData) {
|
|
|
|
ciphersLocalData = {};
|
2017-08-29 18:52:11 +02:00
|
|
|
}
|
|
|
|
|
2017-10-13 23:07:20 +02:00
|
|
|
if (ciphersLocalData[id]) {
|
|
|
|
ciphersLocalData[id].lastUsedDate = new Date().getTime();
|
2017-08-29 18:52:11 +02:00
|
|
|
}
|
|
|
|
else {
|
2017-10-13 23:07:20 +02:00
|
|
|
ciphersLocalData[id] = {
|
2017-08-29 18:52:11 +02:00
|
|
|
lastUsedDate: new Date().getTime()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-10-13 23:07:20 +02:00
|
|
|
return self.utilsService.saveObjToStorage(key, ciphersLocalData);
|
|
|
|
}).then(function () {
|
|
|
|
if (!self.decryptedCipherCache) {
|
|
|
|
return;
|
|
|
|
}
|
2017-08-29 18:52:11 +02:00
|
|
|
|
2017-10-13 23:07:20 +02:00
|
|
|
for (var i = 0; i < self.decryptedCipherCache.length; i++) {
|
|
|
|
if (self.decryptedCipherCache[i].id === id) {
|
|
|
|
self.decryptedCipherCache[i].localData = ciphersLocalData[id];
|
|
|
|
break;
|
2017-08-29 18:52:11 +02:00
|
|
|
}
|
2017-10-13 23:07:20 +02:00
|
|
|
}
|
2017-08-29 18:52:11 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-10-17 05:11:32 +02:00
|
|
|
CipherService.prototype.replace = function (ciphers) {
|
2017-01-04 00:40:07 +01:00
|
|
|
var self = this;
|
2017-10-17 06:03:27 +02:00
|
|
|
return self.userService.getUserIdPromise().then(function (userId) {
|
2017-10-13 23:07:20 +02:00
|
|
|
return self.utilsService.saveObjToStorage('ciphers_' + userId, ciphers);
|
|
|
|
}).then(function () {
|
|
|
|
self.decryptedCipherCache = null;
|
2017-01-04 00:40:07 +01:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-10-17 05:11:32 +02:00
|
|
|
CipherService.prototype.clear = function (userId) {
|
2017-01-04 00:40:07 +01:00
|
|
|
var self = this;
|
2017-10-13 23:07:20 +02:00
|
|
|
return self.utilsService.removeFromStorage('ciphers_' + userId).then(function () {
|
|
|
|
self.decryptedCipherCache = null;
|
2017-01-04 00:40:07 +01:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-10-17 05:11:32 +02:00
|
|
|
CipherService.prototype.delete = function (id) {
|
2017-10-13 23:07:20 +02:00
|
|
|
var self = this,
|
|
|
|
key = null;
|
2017-01-04 00:40:07 +01:00
|
|
|
|
2017-10-17 06:03:27 +02:00
|
|
|
return self.userService.getUserIdPromise().then(function () {
|
2017-10-13 23:07:20 +02:00
|
|
|
key = 'ciphers_' + userId;
|
|
|
|
return self.utilsService.getObjFromStorage(key);
|
2017-10-17 05:11:32 +02:00
|
|
|
}).then(function (ciphers) {
|
|
|
|
if (!ciphers) {
|
2017-10-13 23:07:20 +02:00
|
|
|
return null;
|
|
|
|
}
|
2017-01-04 00:40:07 +01:00
|
|
|
|
2017-10-13 23:07:20 +02:00
|
|
|
if (id.constructor === Array) {
|
|
|
|
for (var i = 0; i < id.length; i++) {
|
2017-10-17 05:11:32 +02:00
|
|
|
if (id[i] in ciphers) {
|
|
|
|
delete ciphers[id[i]];
|
2017-01-04 00:40:07 +01:00
|
|
|
}
|
|
|
|
}
|
2017-10-13 23:07:20 +02:00
|
|
|
}
|
2017-10-17 05:11:32 +02:00
|
|
|
else if (id in ciphers) {
|
|
|
|
delete ciphers[id];
|
2017-10-13 23:07:20 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
return null;
|
|
|
|
}
|
2017-01-04 00:40:07 +01:00
|
|
|
|
2017-10-17 05:11:32 +02:00
|
|
|
return ciphers;
|
|
|
|
}).then(function (ciphers) {
|
|
|
|
if (!ciphers) {
|
2017-10-13 23:07:20 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-10-17 05:11:32 +02:00
|
|
|
return self.utilsService.saveObjToStorage(key, ciphers);
|
2017-10-13 23:07:20 +02:00
|
|
|
}).then(function (clearCache) {
|
|
|
|
if (clearCache !== false) {
|
|
|
|
self.decryptedCipherCache = null;
|
|
|
|
}
|
2017-01-04 00:40:07 +01:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-10-17 05:11:32 +02:00
|
|
|
CipherService.prototype.deleteWithServer = function (id) {
|
2017-01-04 00:40:07 +01:00
|
|
|
var self = this;
|
2017-10-13 23:07:20 +02:00
|
|
|
return self.apiService.deleteCipher(id).then(function () {
|
|
|
|
return self.delete(id);
|
2017-01-04 00:40:07 +01:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-10-17 05:11:32 +02:00
|
|
|
CipherService.prototype.saveNeverDomain = function (domain) {
|
2017-06-13 19:59:16 +02:00
|
|
|
if (!domain) {
|
2017-10-14 03:39:31 +02:00
|
|
|
return Q();
|
2017-06-13 19:59:16 +02:00
|
|
|
}
|
|
|
|
|
2017-10-14 20:27:14 +02:00
|
|
|
var self = this;
|
|
|
|
return self.utilsService.getObjFromStorage(self.neverDomainsKey).then(function (domains) {
|
2017-10-13 23:07:20 +02:00
|
|
|
if (!domains) {
|
|
|
|
domains = {};
|
|
|
|
}
|
2017-06-13 19:59:16 +02:00
|
|
|
|
2017-10-13 23:07:20 +02:00
|
|
|
domains[domain] = null;
|
|
|
|
return self.utilsService.saveObjToStorage(key, domains);
|
|
|
|
});
|
2017-06-13 19:59:16 +02:00
|
|
|
};
|
|
|
|
|
2017-10-17 05:11:32 +02:00
|
|
|
CipherService.prototype.saveAttachmentWithServer = function (cipher, unencryptedFile) {
|
2017-10-13 23:07:20 +02:00
|
|
|
var deferred = Q.defer(),
|
|
|
|
self = this,
|
|
|
|
response = null,
|
|
|
|
data = null,
|
|
|
|
apiErrored = false;
|
2017-07-12 15:57:08 +02:00
|
|
|
|
|
|
|
var key, encFileName;
|
|
|
|
var reader = new FileReader();
|
|
|
|
reader.readAsArrayBuffer(unencryptedFile);
|
|
|
|
reader.onload = function (evt) {
|
2017-10-13 23:07:20 +02:00
|
|
|
self.cryptoService.getOrgKey(cipher.organizationId).then(function (theKey) {
|
2017-07-12 15:57:08 +02:00
|
|
|
key = theKey;
|
|
|
|
return self.cryptoService.encrypt(unencryptedFile.name, key);
|
|
|
|
}).then(function (fileName) {
|
|
|
|
encFileName = fileName;
|
|
|
|
return self.cryptoService.encryptToBytes(evt.target.result, key);
|
|
|
|
}).then(function (encData) {
|
|
|
|
var fd = new FormData();
|
|
|
|
var blob = new Blob([encData], { type: 'application/octet-stream' });
|
|
|
|
fd.append('data', blob, encFileName.encryptedString);
|
|
|
|
|
2017-10-13 23:07:20 +02:00
|
|
|
return self.apiService.postCipherAttachment(cipher.id, fd);
|
|
|
|
}).then(function (resp) {
|
|
|
|
response = resp;
|
|
|
|
return self.userService.getUserIdPromise();
|
|
|
|
}, function (resp) {
|
|
|
|
apiErrored = true;
|
|
|
|
handleErrorMessage(resp, deferred);
|
|
|
|
}).then(function (userId) {
|
|
|
|
if (apiErrored === true) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-10-16 16:09:17 +02:00
|
|
|
data = new CipherData(response, userId);
|
2017-10-13 23:07:20 +02:00
|
|
|
return self.upsert(data);
|
|
|
|
}).then(function () {
|
|
|
|
if (data) {
|
2017-10-16 16:09:17 +02:00
|
|
|
deferred.resolve(new CipherData(data));
|
2017-10-13 23:07:20 +02:00
|
|
|
}
|
2017-07-12 15:57:08 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
reader.onerror = function (evt) {
|
|
|
|
deferred.reject('Error reading file.');
|
|
|
|
};
|
|
|
|
|
|
|
|
return deferred.promise;
|
|
|
|
};
|
|
|
|
|
2017-10-17 05:11:32 +02:00
|
|
|
CipherService.prototype.deleteAttachment = function (id, attachmentId) {
|
2017-10-13 23:07:20 +02:00
|
|
|
var self = this,
|
|
|
|
key = null;
|
|
|
|
|
2017-10-17 06:03:27 +02:00
|
|
|
return self.userService.getUserIdPromise().then(function () {
|
2017-10-13 23:07:20 +02:00
|
|
|
key = 'ciphers_' + userId;
|
|
|
|
return self.utilsService.getObjFromStorage(key);
|
2017-10-17 05:11:32 +02:00
|
|
|
}).then(function (ciphers) {
|
|
|
|
if (ciphers && id in ciphers && ciphers[id].attachments) {
|
|
|
|
for (var i = 0; i < ciphers[id].attachments.length; i++) {
|
|
|
|
if (ciphers[id].attachments[i].id === attachmentId) {
|
|
|
|
ciphers[id].attachments.splice(i, 1);
|
2017-07-12 15:57:08 +02:00
|
|
|
}
|
|
|
|
}
|
2017-10-13 23:07:20 +02:00
|
|
|
|
2017-10-17 05:11:32 +02:00
|
|
|
return self.utilsService.saveObjToStorage(key, ciphers);
|
2017-10-13 23:07:20 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}).then(function (clearCache) {
|
|
|
|
if (clearCache !== false) {
|
|
|
|
self.decryptedCipherCache = null;
|
|
|
|
}
|
2017-07-12 15:57:08 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-10-17 05:11:32 +02:00
|
|
|
CipherService.prototype.deleteAttachmentWithServer = function (id, attachmentId) {
|
2017-10-13 23:07:20 +02:00
|
|
|
var self = this,
|
|
|
|
deferred = Q.defer();
|
2017-07-12 15:57:08 +02:00
|
|
|
|
2017-10-13 23:07:20 +02:00
|
|
|
self.apiService.deleteCipherAttachment(id, attachmentId).then(function () {
|
|
|
|
return self.deleteAttachment(id, attachmentId);
|
2017-07-12 15:57:08 +02:00
|
|
|
}, function (response) {
|
2017-07-12 19:38:06 +02:00
|
|
|
handleErrorMessage(response, deferred);
|
2017-10-13 23:07:20 +02:00
|
|
|
return false;
|
|
|
|
}).then(function (apiSuccess) {
|
|
|
|
if (apiSuccess !== false) {
|
|
|
|
deferred.resolve();
|
|
|
|
}
|
2017-07-12 15:57:08 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
return deferred.promise;
|
|
|
|
};
|
|
|
|
|
2017-10-17 05:21:12 +02:00
|
|
|
CipherService.prototype.sortCiphersByLastUsed = sortCiphersByLastUsed;
|
2017-08-29 18:52:11 +02:00
|
|
|
|
2017-10-17 05:21:12 +02:00
|
|
|
CipherService.prototype.sortCiphersByLastUsedThenName = function (a, b) {
|
|
|
|
var result = sortCiphersByLastUsed(a, b);
|
2017-08-30 14:43:06 +02:00
|
|
|
if (result !== 0) {
|
|
|
|
return result;
|
2017-08-29 18:52:11 +02:00
|
|
|
}
|
|
|
|
|
2017-08-30 14:43:06 +02:00
|
|
|
var nameA = (a.name + '_' + a.username).toUpperCase();
|
|
|
|
var nameB = (b.name + '_' + b.username).toUpperCase();
|
|
|
|
|
|
|
|
if (nameA < nameB) {
|
2017-08-29 19:03:08 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2017-08-30 14:43:06 +02:00
|
|
|
if (nameA > nameB) {
|
2017-08-29 19:03:08 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-08-29 18:52:11 +02:00
|
|
|
return 0;
|
|
|
|
};
|
|
|
|
|
2017-10-17 05:21:12 +02:00
|
|
|
function sortCiphersByLastUsed(a, b) {
|
2017-08-30 14:43:06 +02:00
|
|
|
var aLastUsed = a.localData && a.localData.lastUsedDate ? a.localData.lastUsedDate : null;
|
|
|
|
var bLastUsed = b.localData && b.localData.lastUsedDate ? b.localData.lastUsedDate : null;
|
2017-08-30 14:35:34 +02:00
|
|
|
|
2017-08-30 14:43:06 +02:00
|
|
|
if (aLastUsed && bLastUsed && aLastUsed < bLastUsed) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (aLastUsed && !bLastUsed) {
|
|
|
|
return -1;
|
|
|
|
}
|
2017-08-30 14:35:34 +02:00
|
|
|
|
2017-08-30 14:43:06 +02:00
|
|
|
if (bLastUsed && aLastUsed && aLastUsed > bLastUsed) {
|
2017-08-30 14:35:34 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2017-08-30 14:43:06 +02:00
|
|
|
if (bLastUsed && !aLastUsed) {
|
2017-08-30 14:35:34 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2017-08-30 14:43:06 +02:00
|
|
|
}
|
2017-08-30 14:35:34 +02:00
|
|
|
|
2017-01-04 00:40:07 +01:00
|
|
|
function handleError(error, deferred) {
|
|
|
|
deferred.reject(error);
|
|
|
|
}
|
2017-07-12 19:38:06 +02:00
|
|
|
|
|
|
|
function handleErrorMessage(error, deferred) {
|
|
|
|
if (error.validationErrors) {
|
|
|
|
for (var key in error.validationErrors) {
|
|
|
|
if (!error.validationErrors.hasOwnProperty(key)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (error.validationErrors[key].length) {
|
|
|
|
deferred.reject(error.validationErrors[key][0]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
deferred.reject(error.message);
|
|
|
|
return;
|
|
|
|
}
|
2017-07-14 21:34:05 +02:00
|
|
|
}
|