2018-02-19 19:07:19 +01:00
|
|
|
import { CipherType } from '../enums/cipherType';
|
2018-03-02 18:03:03 +01:00
|
|
|
import { UriMatchType } from '../enums/uriMatchType';
|
2018-01-10 05:01:16 +01:00
|
|
|
|
2018-02-19 19:07:19 +01:00
|
|
|
import { CipherData } from '../models/data/cipherData';
|
2018-01-10 05:01:16 +01:00
|
|
|
|
2018-06-12 17:45:02 +02:00
|
|
|
import { Attachment } from '../models/domain/attachment';
|
2018-02-19 19:07:19 +01:00
|
|
|
import { Card } from '../models/domain/card';
|
|
|
|
import { Cipher } from '../models/domain/cipher';
|
|
|
|
import { CipherString } from '../models/domain/cipherString';
|
2018-01-24 17:33:15 +01:00
|
|
|
import Domain from '../models/domain/domain';
|
2018-02-19 19:07:19 +01:00
|
|
|
import { Field } from '../models/domain/field';
|
|
|
|
import { Identity } from '../models/domain/identity';
|
|
|
|
import { Login } from '../models/domain/login';
|
2018-03-02 05:44:29 +01:00
|
|
|
import { LoginUri } from '../models/domain/loginUri';
|
2018-02-19 19:07:19 +01:00
|
|
|
import { SecureNote } from '../models/domain/secureNote';
|
|
|
|
import { SymmetricCryptoKey } from '../models/domain/symmetricCryptoKey';
|
2018-01-10 05:01:16 +01:00
|
|
|
|
2018-06-12 19:07:06 +02:00
|
|
|
import { CipherCollectionsRequest } from '../models/request/cipherCollectionsRequest';
|
2018-02-19 19:07:19 +01:00
|
|
|
import { CipherRequest } from '../models/request/cipherRequest';
|
2018-01-10 05:01:16 +01:00
|
|
|
|
2018-02-19 19:07:19 +01:00
|
|
|
import { CipherResponse } from '../models/response/cipherResponse';
|
|
|
|
import { ErrorResponse } from '../models/response/errorResponse';
|
2018-01-10 05:01:16 +01:00
|
|
|
|
2018-06-12 17:45:02 +02:00
|
|
|
import { AttachmentView } from '../models/view/attachmentView';
|
2018-02-19 19:07:19 +01:00
|
|
|
import { CardView } from '../models/view/cardView';
|
|
|
|
import { CipherView } from '../models/view/cipherView';
|
|
|
|
import { FieldView } from '../models/view/fieldView';
|
|
|
|
import { IdentityView } from '../models/view/identityView';
|
|
|
|
import { LoginView } from '../models/view/loginView';
|
|
|
|
import { View } from '../models/view/view';
|
2018-01-24 17:33:15 +01:00
|
|
|
|
2018-01-10 05:01:16 +01:00
|
|
|
import { ConstantsService } from './constants.service';
|
|
|
|
|
2018-02-19 19:07:19 +01:00
|
|
|
import { ApiService } from '../abstractions/api.service';
|
|
|
|
import { CipherService as CipherServiceAbstraction } from '../abstractions/cipher.service';
|
|
|
|
import { CryptoService } from '../abstractions/crypto.service';
|
|
|
|
import { I18nService } from '../abstractions/i18n.service';
|
2018-03-02 18:03:03 +01:00
|
|
|
import { PlatformUtilsService } from '../abstractions/platformUtils.service';
|
2018-02-19 19:07:19 +01:00
|
|
|
import { SettingsService } from '../abstractions/settings.service';
|
|
|
|
import { StorageService } from '../abstractions/storage.service';
|
|
|
|
import { UserService } from '../abstractions/user.service';
|
2018-04-23 19:03:47 +02:00
|
|
|
|
|
|
|
import { Utils } from '../misc/utils';
|
2018-06-12 17:45:02 +02:00
|
|
|
import { CipherShareRequest } from '../models/request/cipherShareRequest';
|
2018-01-10 05:01:16 +01:00
|
|
|
|
|
|
|
const Keys = {
|
|
|
|
ciphersPrefix: 'ciphers_',
|
|
|
|
localData: 'sitesLocalData',
|
|
|
|
neverDomains: 'neverDomains',
|
|
|
|
};
|
|
|
|
|
2018-01-23 23:29:15 +01:00
|
|
|
export class CipherService implements CipherServiceAbstraction {
|
2018-01-24 17:33:15 +01:00
|
|
|
decryptedCipherCache: CipherView[];
|
2018-01-10 05:01:16 +01:00
|
|
|
|
|
|
|
constructor(private cryptoService: CryptoService, private userService: UserService,
|
|
|
|
private settingsService: SettingsService, private apiService: ApiService,
|
2018-03-02 18:03:03 +01:00
|
|
|
private storageService: StorageService, private i18nService: I18nService,
|
2018-04-23 19:03:47 +02:00
|
|
|
private platformUtilsService: PlatformUtilsService) {
|
2018-01-10 05:01:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
clearCache(): void {
|
|
|
|
this.decryptedCipherCache = null;
|
|
|
|
}
|
|
|
|
|
2018-01-24 17:33:15 +01:00
|
|
|
async encrypt(model: CipherView): Promise<Cipher> {
|
2018-01-10 05:01:16 +01:00
|
|
|
const cipher = new Cipher();
|
|
|
|
cipher.id = model.id;
|
|
|
|
cipher.folderId = model.folderId;
|
|
|
|
cipher.favorite = model.favorite;
|
|
|
|
cipher.organizationId = model.organizationId;
|
|
|
|
cipher.type = model.type;
|
|
|
|
cipher.collectionIds = model.collectionIds;
|
|
|
|
|
|
|
|
const key = await this.cryptoService.getOrgKey(cipher.organizationId);
|
|
|
|
await Promise.all([
|
|
|
|
this.encryptObjProperty(model, cipher, {
|
|
|
|
name: null,
|
|
|
|
notes: null,
|
|
|
|
}, key),
|
2018-01-24 17:33:15 +01:00
|
|
|
this.encryptCipherData(cipher, model, key),
|
2018-01-10 05:01:16 +01:00
|
|
|
this.encryptFields(model.fields, key).then((fields) => {
|
|
|
|
cipher.fields = fields;
|
|
|
|
}),
|
2018-06-12 17:45:02 +02:00
|
|
|
this.encryptAttachments(model.attachments, key).then((attachments) => {
|
|
|
|
cipher.attachments = attachments;
|
|
|
|
}),
|
2018-01-10 05:01:16 +01:00
|
|
|
]);
|
|
|
|
|
|
|
|
return cipher;
|
|
|
|
}
|
|
|
|
|
2018-06-12 17:45:02 +02:00
|
|
|
async encryptAttachments(attachmentsModel: AttachmentView[], key: SymmetricCryptoKey): Promise<Attachment[]> {
|
|
|
|
if (attachmentsModel == null || attachmentsModel.length === 0) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const promises: Array<Promise<any>> = [];
|
|
|
|
const encAttachments: Attachment[] = [];
|
|
|
|
attachmentsModel.forEach(async (model) => {
|
|
|
|
const attachment = new Attachment();
|
|
|
|
attachment.id = model.id;
|
|
|
|
attachment.size = model.size;
|
|
|
|
attachment.sizeName = model.sizeName;
|
|
|
|
attachment.url = model.url;
|
|
|
|
const promise = this.encryptObjProperty(model, attachment, {
|
|
|
|
fileName: null,
|
|
|
|
}, key).then(() => {
|
|
|
|
encAttachments.push(attachment);
|
|
|
|
});
|
|
|
|
promises.push(promise);
|
|
|
|
});
|
|
|
|
|
|
|
|
await Promise.all(promises);
|
|
|
|
return encAttachments;
|
|
|
|
}
|
|
|
|
|
2018-01-24 17:33:15 +01:00
|
|
|
async encryptFields(fieldsModel: FieldView[], key: SymmetricCryptoKey): Promise<Field[]> {
|
2018-01-10 05:01:16 +01:00
|
|
|
if (!fieldsModel || !fieldsModel.length) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const self = this;
|
|
|
|
const encFields: Field[] = [];
|
|
|
|
await fieldsModel.reduce((promise, field) => {
|
|
|
|
return promise.then(() => {
|
|
|
|
return self.encryptField(field, key);
|
|
|
|
}).then((encField: Field) => {
|
|
|
|
encFields.push(encField);
|
|
|
|
});
|
|
|
|
}, Promise.resolve());
|
|
|
|
|
|
|
|
return encFields;
|
|
|
|
}
|
|
|
|
|
2018-01-24 17:33:15 +01:00
|
|
|
async encryptField(fieldModel: FieldView, key: SymmetricCryptoKey): Promise<Field> {
|
2018-01-10 05:01:16 +01:00
|
|
|
const field = new Field();
|
|
|
|
field.type = fieldModel.type;
|
|
|
|
|
|
|
|
await this.encryptObjProperty(fieldModel, field, {
|
|
|
|
name: null,
|
|
|
|
value: null,
|
|
|
|
}, key);
|
|
|
|
|
|
|
|
return field;
|
|
|
|
}
|
|
|
|
|
|
|
|
async get(id: string): Promise<Cipher> {
|
|
|
|
const userId = await this.userService.getUserId();
|
|
|
|
const localData = await this.storageService.get<any>(Keys.localData);
|
|
|
|
const ciphers = await this.storageService.get<{ [id: string]: CipherData; }>(
|
|
|
|
Keys.ciphersPrefix + userId);
|
|
|
|
if (ciphers == null || !ciphers.hasOwnProperty(id)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Cipher(ciphers[id], false, localData ? localData[id] : null);
|
|
|
|
}
|
|
|
|
|
|
|
|
async getAll(): Promise<Cipher[]> {
|
|
|
|
const userId = await this.userService.getUserId();
|
|
|
|
const localData = await this.storageService.get<any>(Keys.localData);
|
|
|
|
const ciphers = await this.storageService.get<{ [id: string]: CipherData; }>(
|
|
|
|
Keys.ciphersPrefix + userId);
|
|
|
|
const response: Cipher[] = [];
|
|
|
|
for (const id in ciphers) {
|
|
|
|
if (ciphers.hasOwnProperty(id)) {
|
|
|
|
response.push(new Cipher(ciphers[id], false, localData ? localData[id] : null));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
2018-01-24 17:33:15 +01:00
|
|
|
async getAllDecrypted(): Promise<CipherView[]> {
|
2018-01-10 05:01:16 +01:00
|
|
|
if (this.decryptedCipherCache != null) {
|
|
|
|
return this.decryptedCipherCache;
|
|
|
|
}
|
|
|
|
|
2018-01-24 17:33:15 +01:00
|
|
|
const decCiphers: CipherView[] = [];
|
2018-01-10 05:01:16 +01:00
|
|
|
const key = await this.cryptoService.getKey();
|
|
|
|
if (key == null) {
|
|
|
|
throw new Error('No key.');
|
|
|
|
}
|
|
|
|
|
|
|
|
const promises: any[] = [];
|
|
|
|
const ciphers = await this.getAll();
|
|
|
|
ciphers.forEach((cipher) => {
|
2018-01-24 17:33:15 +01:00
|
|
|
promises.push(cipher.decrypt().then((c) => decCiphers.push(c)));
|
2018-01-10 05:01:16 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
await Promise.all(promises);
|
2018-01-27 04:38:22 +01:00
|
|
|
decCiphers.sort(this.getLocaleSortingFunction());
|
2018-01-10 05:01:16 +01:00
|
|
|
this.decryptedCipherCache = decCiphers;
|
|
|
|
return this.decryptedCipherCache;
|
|
|
|
}
|
|
|
|
|
2018-01-24 17:33:15 +01:00
|
|
|
async getAllDecryptedForGrouping(groupingId: string, folder: boolean = true): Promise<CipherView[]> {
|
2018-01-10 05:01:16 +01:00
|
|
|
const ciphers = await this.getAllDecrypted();
|
|
|
|
|
2018-01-24 17:33:15 +01:00
|
|
|
return ciphers.filter((cipher) => {
|
2018-01-10 05:01:16 +01:00
|
|
|
if (folder && cipher.folderId === groupingId) {
|
2018-01-24 17:33:15 +01:00
|
|
|
return true;
|
2018-01-10 05:01:16 +01:00
|
|
|
} else if (!folder && cipher.collectionIds != null && cipher.collectionIds.indexOf(groupingId) > -1) {
|
2018-01-24 17:33:15 +01:00
|
|
|
return true;
|
2018-01-10 05:01:16 +01:00
|
|
|
}
|
|
|
|
|
2018-01-24 17:33:15 +01:00
|
|
|
return false;
|
|
|
|
});
|
2018-01-10 05:01:16 +01:00
|
|
|
}
|
|
|
|
|
2018-03-02 18:03:03 +01:00
|
|
|
async getAllDecryptedForUrl(url: string, includeOtherTypes?: CipherType[]): Promise<CipherView[]> {
|
|
|
|
if (url == null && !includeOtherTypes) {
|
2018-01-10 05:01:16 +01:00
|
|
|
return Promise.resolve([]);
|
|
|
|
}
|
|
|
|
|
2018-03-02 18:03:03 +01:00
|
|
|
const domain = this.platformUtilsService.getDomain(url);
|
2018-01-10 05:01:16 +01:00
|
|
|
const eqDomainsPromise = domain == null ? Promise.resolve([]) :
|
|
|
|
this.settingsService.getEquivalentDomains().then((eqDomains: any[][]) => {
|
|
|
|
let matches: any[] = [];
|
|
|
|
eqDomains.forEach((eqDomain) => {
|
|
|
|
if (eqDomain.length && eqDomain.indexOf(domain) >= 0) {
|
|
|
|
matches = matches.concat(eqDomain);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!matches.length) {
|
|
|
|
matches.push(domain);
|
|
|
|
}
|
|
|
|
|
|
|
|
return matches;
|
|
|
|
});
|
|
|
|
|
|
|
|
const result = await Promise.all([eqDomainsPromise, this.getAllDecrypted()]);
|
|
|
|
const matchingDomains = result[0];
|
|
|
|
const ciphers = result[1];
|
|
|
|
|
2018-01-24 17:33:15 +01:00
|
|
|
return ciphers.filter((cipher) => {
|
2018-03-02 18:03:03 +01:00
|
|
|
if (includeOtherTypes && includeOtherTypes.indexOf(cipher.type) > -1) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (url != null && cipher.type === CipherType.Login && cipher.login.uris != null) {
|
|
|
|
for (let i = 0; i < cipher.login.uris.length; i++) {
|
|
|
|
const u = cipher.login.uris[i];
|
|
|
|
if (u.uri == null) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (u.match) {
|
|
|
|
case null:
|
|
|
|
case undefined:
|
2018-03-02 19:50:26 +01:00
|
|
|
case UriMatchType.Domain:
|
2018-03-02 18:03:03 +01:00
|
|
|
if (domain != null && u.domain != null && matchingDomains.indexOf(u.domain) > -1) {
|
|
|
|
return true;
|
|
|
|
}
|
2018-03-02 18:36:53 +01:00
|
|
|
break;
|
2018-03-02 18:03:03 +01:00
|
|
|
case UriMatchType.Host:
|
2018-04-23 19:03:47 +02:00
|
|
|
const urlHost = Utils.getHost(url);
|
|
|
|
if (urlHost != null && urlHost === Utils.getHost(u.uri)) {
|
2018-03-02 18:03:03 +01:00
|
|
|
return true;
|
|
|
|
}
|
2018-03-02 18:36:53 +01:00
|
|
|
break;
|
2018-03-02 18:03:03 +01:00
|
|
|
case UriMatchType.Exact:
|
|
|
|
if (url === u.uri) {
|
|
|
|
return true;
|
|
|
|
}
|
2018-03-02 18:36:53 +01:00
|
|
|
break;
|
2018-03-02 18:03:03 +01:00
|
|
|
case UriMatchType.StartsWith:
|
|
|
|
if (url.startsWith(u.uri)) {
|
|
|
|
return true;
|
|
|
|
}
|
2018-03-02 18:36:53 +01:00
|
|
|
break;
|
2018-03-02 18:03:03 +01:00
|
|
|
case UriMatchType.RegularExpression:
|
2018-05-31 05:19:12 +02:00
|
|
|
try {
|
|
|
|
const regex = new RegExp(u.uri, 'i');
|
|
|
|
if (regex.test(url)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} catch { }
|
2018-03-02 18:36:53 +01:00
|
|
|
break;
|
2018-03-02 18:03:03 +01:00
|
|
|
case UriMatchType.Never:
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-01-10 05:01:16 +01:00
|
|
|
|
2018-01-24 17:33:15 +01:00
|
|
|
return false;
|
|
|
|
});
|
2018-01-10 05:01:16 +01:00
|
|
|
}
|
|
|
|
|
2018-03-02 18:03:03 +01:00
|
|
|
async getLastUsedForUrl(url: string): Promise<CipherView> {
|
|
|
|
const ciphers = await this.getAllDecryptedForUrl(url);
|
2018-01-10 05:01:16 +01:00
|
|
|
if (ciphers.length === 0) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-01-27 04:54:02 +01:00
|
|
|
const sortedCiphers = ciphers.sort(this.sortCiphersByLastUsed);
|
2018-01-10 05:01:16 +01:00
|
|
|
return sortedCiphers[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
async updateLastUsedDate(id: string): Promise<void> {
|
|
|
|
let ciphersLocalData = await this.storageService.get<any>(Keys.localData);
|
|
|
|
if (!ciphersLocalData) {
|
|
|
|
ciphersLocalData = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ciphersLocalData[id]) {
|
|
|
|
ciphersLocalData[id].lastUsedDate = new Date().getTime();
|
|
|
|
} else {
|
|
|
|
ciphersLocalData[id] = {
|
|
|
|
lastUsedDate: new Date().getTime(),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
await this.storageService.save(Keys.localData, ciphersLocalData);
|
|
|
|
|
|
|
|
if (this.decryptedCipherCache == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let i = 0; i < this.decryptedCipherCache.length; i++) {
|
|
|
|
const cached = this.decryptedCipherCache[i];
|
|
|
|
if (cached.id === id) {
|
|
|
|
cached.localData = ciphersLocalData[id];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async saveNeverDomain(domain: string): Promise<void> {
|
|
|
|
if (domain == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let domains = await this.storageService.get<{ [id: string]: any; }>(Keys.neverDomains);
|
|
|
|
if (!domains) {
|
|
|
|
domains = {};
|
|
|
|
}
|
|
|
|
domains[domain] = null;
|
|
|
|
await this.storageService.save(Keys.neverDomains, domains);
|
|
|
|
}
|
|
|
|
|
|
|
|
async saveWithServer(cipher: Cipher): Promise<any> {
|
|
|
|
const request = new CipherRequest(cipher);
|
|
|
|
|
|
|
|
let response: CipherResponse;
|
|
|
|
if (cipher.id == null) {
|
|
|
|
response = await this.apiService.postCipher(request);
|
|
|
|
cipher.id = response.id;
|
|
|
|
} else {
|
|
|
|
response = await this.apiService.putCipher(cipher.id, request);
|
|
|
|
}
|
|
|
|
|
|
|
|
const userId = await this.userService.getUserId();
|
|
|
|
const data = new CipherData(response, userId, cipher.collectionIds);
|
|
|
|
await this.upsert(data);
|
|
|
|
}
|
|
|
|
|
2018-06-12 17:45:02 +02:00
|
|
|
async shareWithServer(cipher: Cipher): Promise<any> {
|
|
|
|
const request = new CipherShareRequest(cipher);
|
2018-06-12 19:07:06 +02:00
|
|
|
await this.apiService.putShareCipher(cipher.id, request);
|
2018-06-12 17:45:02 +02:00
|
|
|
const userId = await this.userService.getUserId();
|
|
|
|
await this.upsert(cipher.toCipherData(userId));
|
|
|
|
}
|
|
|
|
|
|
|
|
async shareAttachmentWithServer(attachmentView: AttachmentView, cipherId: string,
|
|
|
|
organizationId: string): Promise<any> {
|
|
|
|
const attachmentResponse = await fetch(new Request(attachmentView.url, { cache: 'no-cache' }));
|
|
|
|
if (attachmentResponse.status !== 200) {
|
|
|
|
throw Error('Failed to download attachment: ' + attachmentResponse.status.toString());
|
|
|
|
}
|
|
|
|
|
|
|
|
const buf = await attachmentResponse.arrayBuffer();
|
|
|
|
const decBuf = await this.cryptoService.decryptFromBytes(buf, null);
|
|
|
|
const key = await this.cryptoService.getOrgKey(organizationId);
|
|
|
|
const encData = await this.cryptoService.encryptToBytes(decBuf, key);
|
|
|
|
const encFileName = await this.cryptoService.encrypt(attachmentView.fileName, key);
|
|
|
|
|
|
|
|
const fd = new FormData();
|
|
|
|
try {
|
|
|
|
const blob = new Blob([encData], { type: 'application/octet-stream' });
|
|
|
|
fd.append('data', blob, encFileName.encryptedString);
|
|
|
|
} catch (e) {
|
|
|
|
if (Utils.isNode && !Utils.isBrowser) {
|
|
|
|
fd.append('data', new Buffer(encData) as any, {
|
|
|
|
filepath: encFileName.encryptedString,
|
|
|
|
contentType: 'application/octet-stream',
|
|
|
|
} as any);
|
|
|
|
} else {
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let response: CipherResponse;
|
|
|
|
try {
|
2018-06-12 19:07:06 +02:00
|
|
|
response = await this.apiService.postShareCipherAttachment(cipherId, attachmentView.id, fd,
|
|
|
|
organizationId);
|
2018-06-12 17:45:02 +02:00
|
|
|
} catch (e) {
|
|
|
|
throw new Error((e as ErrorResponse).getSingleMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-17 21:35:02 +02:00
|
|
|
saveAttachmentWithServer(cipher: Cipher, unencryptedFile: any): Promise<Cipher> {
|
2018-01-10 05:01:16 +01:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const reader = new FileReader();
|
|
|
|
reader.readAsArrayBuffer(unencryptedFile);
|
|
|
|
reader.onload = async (evt: any) => {
|
|
|
|
try {
|
2018-05-17 21:35:02 +02:00
|
|
|
const cData = await this.saveAttachmentRawWithServer(cipher,
|
|
|
|
unencryptedFile.name, evt.target.result);
|
|
|
|
resolve(cData);
|
2018-01-10 05:01:16 +01:00
|
|
|
} catch (e) {
|
2018-05-17 21:35:02 +02:00
|
|
|
reject(e);
|
2018-01-10 05:01:16 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
reader.onerror = (evt) => {
|
|
|
|
reject('Error reading file.');
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-05-17 21:35:02 +02:00
|
|
|
async saveAttachmentRawWithServer(cipher: Cipher, filename: string, data: ArrayBuffer): Promise<Cipher> {
|
|
|
|
const key = await this.cryptoService.getOrgKey(cipher.organizationId);
|
|
|
|
const encFileName = await this.cryptoService.encrypt(filename, key);
|
|
|
|
const encData = await this.cryptoService.encryptToBytes(data, key);
|
|
|
|
|
|
|
|
const fd = new FormData();
|
|
|
|
try {
|
|
|
|
const blob = new Blob([encData], { type: 'application/octet-stream' });
|
|
|
|
fd.append('data', blob, encFileName.encryptedString);
|
|
|
|
} catch (e) {
|
2018-05-31 15:07:56 +02:00
|
|
|
if (Utils.isNode && !Utils.isBrowser) {
|
2018-05-17 21:35:02 +02:00
|
|
|
fd.append('data', new Buffer(encData) as any, {
|
2018-05-25 14:39:48 +02:00
|
|
|
filepath: encFileName.encryptedString,
|
2018-05-17 21:35:02 +02:00
|
|
|
contentType: 'application/octet-stream',
|
|
|
|
} as any);
|
|
|
|
} else {
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let response: CipherResponse;
|
|
|
|
try {
|
|
|
|
response = await this.apiService.postCipherAttachment(cipher.id, fd);
|
|
|
|
} catch (e) {
|
|
|
|
throw new Error((e as ErrorResponse).getSingleMessage());
|
|
|
|
}
|
|
|
|
|
|
|
|
const userId = await this.userService.getUserId();
|
|
|
|
const cData = new CipherData(response, userId, cipher.collectionIds);
|
|
|
|
this.upsert(cData);
|
|
|
|
return new Cipher(cData);
|
|
|
|
}
|
|
|
|
|
2018-06-12 19:07:06 +02:00
|
|
|
async saveCollectionsWithServer(cipher: Cipher): Promise<any> {
|
|
|
|
const request = new CipherCollectionsRequest(cipher.collectionIds);
|
|
|
|
const response = await this.apiService.putCipherCollections(cipher.id, request);
|
|
|
|
const userId = await this.userService.getUserId();
|
|
|
|
const data = cipher.toCipherData(userId);
|
|
|
|
await this.upsert(data);
|
|
|
|
}
|
|
|
|
|
2018-01-10 05:01:16 +01:00
|
|
|
async upsert(cipher: CipherData | CipherData[]): Promise<any> {
|
|
|
|
const userId = await this.userService.getUserId();
|
|
|
|
let ciphers = await this.storageService.get<{ [id: string]: CipherData; }>(
|
|
|
|
Keys.ciphersPrefix + userId);
|
|
|
|
if (ciphers == null) {
|
|
|
|
ciphers = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cipher instanceof CipherData) {
|
|
|
|
const c = cipher as CipherData;
|
|
|
|
ciphers[c.id] = c;
|
|
|
|
} else {
|
|
|
|
(cipher as CipherData[]).forEach((c) => {
|
|
|
|
ciphers[c.id] = c;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
await this.storageService.save(Keys.ciphersPrefix + userId, ciphers);
|
|
|
|
this.decryptedCipherCache = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
async replace(ciphers: { [id: string]: CipherData; }): Promise<any> {
|
|
|
|
const userId = await this.userService.getUserId();
|
|
|
|
await this.storageService.save(Keys.ciphersPrefix + userId, ciphers);
|
|
|
|
this.decryptedCipherCache = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
async clear(userId: string): Promise<any> {
|
|
|
|
await this.storageService.remove(Keys.ciphersPrefix + userId);
|
|
|
|
this.decryptedCipherCache = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
async delete(id: string | string[]): Promise<any> {
|
|
|
|
const userId = await this.userService.getUserId();
|
|
|
|
const ciphers = await this.storageService.get<{ [id: string]: CipherData; }>(
|
|
|
|
Keys.ciphersPrefix + userId);
|
|
|
|
if (ciphers == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof id === 'string') {
|
|
|
|
const i = id as string;
|
|
|
|
delete ciphers[id];
|
|
|
|
} else {
|
|
|
|
(id as string[]).forEach((i) => {
|
|
|
|
delete ciphers[i];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
await this.storageService.save(Keys.ciphersPrefix + userId, ciphers);
|
|
|
|
this.decryptedCipherCache = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
async deleteWithServer(id: string): Promise<any> {
|
|
|
|
await this.apiService.deleteCipher(id);
|
|
|
|
await this.delete(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
async deleteAttachment(id: string, attachmentId: string): Promise<void> {
|
|
|
|
const userId = await this.userService.getUserId();
|
|
|
|
const ciphers = await this.storageService.get<{ [id: string]: CipherData; }>(
|
|
|
|
Keys.ciphersPrefix + userId);
|
|
|
|
|
|
|
|
if (ciphers == null || !ciphers.hasOwnProperty(id) || ciphers[id].attachments == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let i = 0; i < ciphers[id].attachments.length; i++) {
|
|
|
|
if (ciphers[id].attachments[i].id === attachmentId) {
|
|
|
|
ciphers[id].attachments.splice(i, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
await this.storageService.save(Keys.ciphersPrefix + userId, ciphers);
|
|
|
|
this.decryptedCipherCache = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
async deleteAttachmentWithServer(id: string, attachmentId: string): Promise<void> {
|
|
|
|
try {
|
|
|
|
await this.apiService.deleteCipherAttachment(id, attachmentId);
|
|
|
|
} catch (e) {
|
|
|
|
return Promise.reject((e as ErrorResponse).getSingleMessage());
|
|
|
|
}
|
|
|
|
await this.deleteAttachment(id, attachmentId);
|
|
|
|
}
|
|
|
|
|
2018-01-27 04:54:02 +01:00
|
|
|
sortCiphersByLastUsed(a: CipherView, b: CipherView): number {
|
|
|
|
const aLastUsed = a.localData && a.localData.lastUsedDate ? a.localData.lastUsedDate as number : null;
|
|
|
|
const bLastUsed = b.localData && b.localData.lastUsedDate ? b.localData.lastUsedDate as number : null;
|
|
|
|
|
|
|
|
if (aLastUsed != null && bLastUsed != null && aLastUsed < bLastUsed) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (aLastUsed != null && bLastUsed == null) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bLastUsed != null && aLastUsed != null && aLastUsed > bLastUsed) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (bLastUsed != null && aLastUsed == null) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2018-01-10 05:01:16 +01:00
|
|
|
}
|
|
|
|
|
2018-01-27 04:54:02 +01:00
|
|
|
sortCiphersByLastUsedThenName(a: CipherView, b: CipherView): number {
|
|
|
|
const result = this.sortCiphersByLastUsed(a, b);
|
|
|
|
if (result !== 0) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.getLocaleSortingFunction()(a, b);
|
2018-01-10 05:01:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Helpers
|
|
|
|
|
2018-01-27 04:16:35 +01:00
|
|
|
private getLocaleSortingFunction(): (a: CipherView, b: CipherView) => number {
|
|
|
|
return (a, b) => {
|
2018-01-27 04:54:02 +01:00
|
|
|
let aName = a.name;
|
|
|
|
let bName = b.name;
|
|
|
|
|
2018-03-20 00:42:38 +01:00
|
|
|
if (aName == null && bName != null) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (aName != null && bName == null) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (aName == null && bName == null) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-02-02 04:55:49 +01:00
|
|
|
const result = this.i18nService.collator ? this.i18nService.collator.compare(aName, bName) :
|
2018-01-27 04:54:02 +01:00
|
|
|
aName.localeCompare(bName);
|
|
|
|
|
|
|
|
if (result !== 0 || a.type !== CipherType.Login || b.type !== CipherType.Login) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (a.login.username != null) {
|
|
|
|
aName += a.login.username;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (b.login.username != null) {
|
|
|
|
bName += b.login.username;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.i18nService.collator ? this.i18nService.collator.compare(aName, bName) :
|
|
|
|
aName.localeCompare(bName);
|
2018-01-27 04:16:35 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-01-24 17:33:15 +01:00
|
|
|
private async encryptObjProperty<V extends View, D extends Domain>(model: V, obj: D,
|
|
|
|
map: any, key: SymmetricCryptoKey): Promise<void> {
|
2018-01-10 05:01:16 +01:00
|
|
|
const promises = [];
|
|
|
|
const self = this;
|
|
|
|
|
|
|
|
for (const prop in map) {
|
|
|
|
if (!map.hasOwnProperty(prop)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// tslint:disable-next-line
|
|
|
|
(function (theProp, theObj) {
|
|
|
|
const p = Promise.resolve().then(() => {
|
2018-01-24 17:33:15 +01:00
|
|
|
const modelProp = (model as any)[(map[theProp] || theProp)];
|
2018-01-10 05:01:16 +01:00
|
|
|
if (modelProp && modelProp !== '') {
|
|
|
|
return self.cryptoService.encrypt(modelProp, key);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}).then((val: CipherString) => {
|
2018-01-24 17:33:15 +01:00
|
|
|
(theObj as any)[theProp] = val;
|
2018-01-10 05:01:16 +01:00
|
|
|
});
|
|
|
|
promises.push(p);
|
|
|
|
})(prop, obj);
|
|
|
|
}
|
|
|
|
|
2018-01-24 17:33:15 +01:00
|
|
|
await Promise.all(promises);
|
2018-01-10 05:01:16 +01:00
|
|
|
}
|
|
|
|
|
2018-01-24 17:33:15 +01:00
|
|
|
private async encryptCipherData(cipher: Cipher, model: CipherView, key: SymmetricCryptoKey) {
|
2018-01-10 05:01:16 +01:00
|
|
|
switch (cipher.type) {
|
|
|
|
case CipherType.Login:
|
2018-01-24 19:27:32 +01:00
|
|
|
cipher.login = new Login();
|
2018-01-24 17:33:15 +01:00
|
|
|
await this.encryptObjProperty(model.login, cipher.login, {
|
2018-01-10 05:01:16 +01:00
|
|
|
username: null,
|
|
|
|
password: null,
|
|
|
|
totp: null,
|
|
|
|
}, key);
|
2018-03-02 05:44:29 +01:00
|
|
|
|
|
|
|
if (model.login.uris != null) {
|
|
|
|
cipher.login.uris = [];
|
|
|
|
for (let i = 0; i < model.login.uris.length; i++) {
|
|
|
|
const loginUri = new LoginUri();
|
|
|
|
loginUri.match = model.login.uris[i].match;
|
|
|
|
await this.encryptObjProperty(model.login.uris[i], loginUri, {
|
|
|
|
uri: null,
|
|
|
|
}, key);
|
|
|
|
cipher.login.uris.push(loginUri);
|
|
|
|
}
|
|
|
|
}
|
2018-01-24 17:33:15 +01:00
|
|
|
return;
|
2018-01-10 05:01:16 +01:00
|
|
|
case CipherType.SecureNote:
|
2018-01-24 19:27:32 +01:00
|
|
|
cipher.secureNote = new SecureNote();
|
|
|
|
cipher.secureNote.type = model.secureNote.type;
|
2018-01-24 17:33:15 +01:00
|
|
|
return;
|
2018-01-10 05:01:16 +01:00
|
|
|
case CipherType.Card:
|
2018-01-24 19:27:32 +01:00
|
|
|
cipher.card = new Card();
|
2018-01-24 17:33:15 +01:00
|
|
|
await this.encryptObjProperty(model.card, cipher.card, {
|
2018-01-10 05:01:16 +01:00
|
|
|
cardholderName: null,
|
|
|
|
brand: null,
|
|
|
|
number: null,
|
|
|
|
expMonth: null,
|
|
|
|
expYear: null,
|
|
|
|
code: null,
|
|
|
|
}, key);
|
2018-01-24 17:33:15 +01:00
|
|
|
return;
|
2018-01-10 05:01:16 +01:00
|
|
|
case CipherType.Identity:
|
2018-01-24 19:27:32 +01:00
|
|
|
cipher.identity = new Identity();
|
2018-01-24 17:33:15 +01:00
|
|
|
await this.encryptObjProperty(model.identity, cipher.identity, {
|
2018-01-10 05:01:16 +01:00
|
|
|
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);
|
2018-01-24 17:33:15 +01:00
|
|
|
return;
|
2018-01-10 05:01:16 +01:00
|
|
|
default:
|
|
|
|
throw new Error('Unknown cipher type.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|