1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-09-18 02:41:15 +02:00

Remove userId from data models (#771)

This commit is contained in:
Oscar Hinton 2022-04-20 15:59:51 +02:00 committed by GitHub
parent f6e3481fe9
commit 366d9b3c63
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 30 additions and 70 deletions

View File

@ -24,7 +24,6 @@ describe("Cipher DTO", () => {
expect(cipher).toEqual({ expect(cipher).toEqual({
id: null, id: null,
userId: null,
organizationId: null, organizationId: null,
folderId: null, folderId: null,
name: null, name: null,
@ -53,7 +52,6 @@ describe("Cipher DTO", () => {
id: "id", id: "id",
organizationId: "orgId", organizationId: "orgId",
folderId: "folderId", folderId: "folderId",
userId: "userId",
edit: true, edit: true,
viewPassword: true, viewPassword: true,
organizationUseTotp: true, organizationUseTotp: true,
@ -115,7 +113,6 @@ describe("Cipher DTO", () => {
expect(cipher).toEqual({ expect(cipher).toEqual({
id: "id", id: "id",
userId: "userId",
organizationId: "orgId", organizationId: "orgId",
folderId: "folderId", folderId: "folderId",
name: { encryptedString: "EncryptedString", encryptionType: 0 }, name: { encryptedString: "EncryptedString", encryptionType: 0 },
@ -181,7 +178,7 @@ describe("Cipher DTO", () => {
it("toCipherData", () => { it("toCipherData", () => {
const cipher = new Cipher(cipherData); const cipher = new Cipher(cipherData);
expect(cipher.toCipherData("userId")).toEqual(cipherData); expect(cipher.toCipherData()).toEqual(cipherData);
}); });
it("Decrypt", async () => { it("Decrypt", async () => {
@ -242,7 +239,6 @@ describe("Cipher DTO", () => {
id: "id", id: "id",
organizationId: "orgId", organizationId: "orgId",
folderId: "folderId", folderId: "folderId",
userId: "userId",
edit: true, edit: true,
viewPassword: true, viewPassword: true,
organizationUseTotp: true, organizationUseTotp: true,
@ -264,7 +260,6 @@ describe("Cipher DTO", () => {
expect(cipher).toEqual({ expect(cipher).toEqual({
id: "id", id: "id",
userId: "userId",
organizationId: "orgId", organizationId: "orgId",
folderId: "folderId", folderId: "folderId",
name: { encryptedString: "EncryptedString", encryptionType: 0 }, name: { encryptedString: "EncryptedString", encryptionType: 0 },
@ -288,7 +283,7 @@ describe("Cipher DTO", () => {
it("toCipherData", () => { it("toCipherData", () => {
const cipher = new Cipher(cipherData); const cipher = new Cipher(cipherData);
expect(cipher.toCipherData("userId")).toEqual(cipherData); expect(cipher.toCipherData()).toEqual(cipherData);
}); });
it("Decrypt", async () => { it("Decrypt", async () => {
@ -343,7 +338,6 @@ describe("Cipher DTO", () => {
id: "id", id: "id",
organizationId: "orgId", organizationId: "orgId",
folderId: "folderId", folderId: "folderId",
userId: "userId",
edit: true, edit: true,
viewPassword: true, viewPassword: true,
organizationUseTotp: true, organizationUseTotp: true,
@ -370,7 +364,6 @@ describe("Cipher DTO", () => {
expect(cipher).toEqual({ expect(cipher).toEqual({
id: "id", id: "id",
userId: "userId",
organizationId: "orgId", organizationId: "orgId",
folderId: "folderId", folderId: "folderId",
name: { encryptedString: "EncryptedString", encryptionType: 0 }, name: { encryptedString: "EncryptedString", encryptionType: 0 },
@ -401,7 +394,7 @@ describe("Cipher DTO", () => {
it("toCipherData", () => { it("toCipherData", () => {
const cipher = new Cipher(cipherData); const cipher = new Cipher(cipherData);
expect(cipher.toCipherData("userId")).toEqual(cipherData); expect(cipher.toCipherData()).toEqual(cipherData);
}); });
it("Decrypt", async () => { it("Decrypt", async () => {
@ -462,7 +455,6 @@ describe("Cipher DTO", () => {
id: "id", id: "id",
organizationId: "orgId", organizationId: "orgId",
folderId: "folderId", folderId: "folderId",
userId: "userId",
edit: true, edit: true,
viewPassword: true, viewPassword: true,
organizationUseTotp: true, organizationUseTotp: true,
@ -501,7 +493,6 @@ describe("Cipher DTO", () => {
expect(cipher).toEqual({ expect(cipher).toEqual({
id: "id", id: "id",
userId: "userId",
organizationId: "orgId", organizationId: "orgId",
folderId: "folderId", folderId: "folderId",
name: { encryptedString: "EncryptedString", encryptionType: 0 }, name: { encryptedString: "EncryptedString", encryptionType: 0 },
@ -544,7 +535,7 @@ describe("Cipher DTO", () => {
it("toCipherData", () => { it("toCipherData", () => {
const cipher = new Cipher(cipherData); const cipher = new Cipher(cipherData);
expect(cipher.toCipherData("userId")).toEqual(cipherData); expect(cipher.toCipherData()).toEqual(cipherData);
}); });
it("Decrypt", async () => { it("Decrypt", async () => {

View File

@ -9,7 +9,6 @@ describe("Folder", () => {
beforeEach(() => { beforeEach(() => {
data = { data = {
id: "id", id: "id",
userId: "userId",
name: "encName", name: "encName",
revisionDate: "2022-01-31T12:00:00.000Z", revisionDate: "2022-01-31T12:00:00.000Z",
}; };

View File

@ -17,7 +17,6 @@ describe("Send", () => {
data = { data = {
id: "id", id: "id",
accessId: "accessId", accessId: "accessId",
userId: "userId",
type: SendType.Text, type: SendType.Text,
name: "encName", name: "encName",
notes: "encNotes", notes: "encNotes",
@ -45,7 +44,6 @@ describe("Send", () => {
expect(send).toEqual({ expect(send).toEqual({
id: null, id: null,
accessId: null, accessId: null,
userId: null,
type: undefined, type: undefined,
name: null, name: null,
notes: null, notes: null,
@ -69,7 +67,6 @@ describe("Send", () => {
expect(send).toEqual({ expect(send).toEqual({
id: "id", id: "id",
accessId: "accessId", accessId: "accessId",
userId: "userId",
type: SendType.Text, type: SendType.Text,
name: { encryptedString: "encName", encryptionType: 0 }, name: { encryptedString: "encName", encryptionType: 0 },
notes: { encryptedString: "encNotes", encryptionType: 0 }, notes: { encryptedString: "encNotes", encryptionType: 0 },
@ -96,7 +93,6 @@ describe("Send", () => {
const send = new Send(); const send = new Send();
send.id = "id"; send.id = "id";
send.accessId = "accessId"; send.accessId = "accessId";
send.userId = "userId";
send.type = SendType.Text; send.type = SendType.Text;
send.name = mockEnc("name"); send.name = mockEnc("name");
send.notes = mockEnc("notes"); send.notes = mockEnc("notes");

View File

@ -14,7 +14,6 @@ export class CipherData {
id: string; id: string;
organizationId: string; organizationId: string;
folderId: string; folderId: string;
userId: string;
edit: boolean; edit: boolean;
viewPassword: boolean; viewPassword: boolean;
organizationUseTotp: boolean; organizationUseTotp: boolean;
@ -34,7 +33,7 @@ export class CipherData {
deletedDate: string; deletedDate: string;
reprompt: CipherRepromptType; reprompt: CipherRepromptType;
constructor(response?: CipherResponse, userId?: string, collectionIds?: string[]) { constructor(response?: CipherResponse, collectionIds?: string[]) {
if (response == null) { if (response == null) {
return; return;
} }
@ -42,7 +41,6 @@ export class CipherData {
this.id = response.id; this.id = response.id;
this.organizationId = response.organizationId; this.organizationId = response.organizationId;
this.folderId = response.folderId; this.folderId = response.folderId;
this.userId = userId;
this.edit = response.edit; this.edit = response.edit;
this.viewPassword = response.viewPassword; this.viewPassword = response.viewPassword;
this.organizationUseTotp = response.organizationUseTotp; this.organizationUseTotp = response.organizationUseTotp;

View File

@ -2,12 +2,10 @@ import { FolderResponse } from "../response/folderResponse";
export class FolderData { export class FolderData {
id: string; id: string;
userId: string;
name: string; name: string;
revisionDate: string; revisionDate: string;
constructor(response: FolderResponse, userId: string) { constructor(response: FolderResponse) {
this.userId = userId;
this.name = response.name; this.name = response.name;
this.id = response.id; this.id = response.id;
this.revisionDate = response.revisionDate; this.revisionDate = response.revisionDate;

View File

@ -7,7 +7,6 @@ import { SendTextData } from "./sendTextData";
export class SendData { export class SendData {
id: string; id: string;
accessId: string; accessId: string;
userId: string;
type: SendType; type: SendType;
name: string; name: string;
notes: string; notes: string;
@ -23,14 +22,13 @@ export class SendData {
disabled: boolean; disabled: boolean;
hideEmail: boolean; hideEmail: boolean;
constructor(response?: SendResponse, userId?: string) { constructor(response?: SendResponse) {
if (response == null) { if (response == null) {
return; return;
} }
this.id = response.id; this.id = response.id;
this.accessId = response.accessId; this.accessId = response.accessId;
this.userId = userId;
this.type = response.type; this.type = response.type;
this.name = response.name; this.name = response.name;
this.notes = response.notes; this.notes = response.notes;

View File

@ -49,13 +49,12 @@ export class Cipher extends Domain {
obj, obj,
{ {
id: null, id: null,
userId: null,
organizationId: null, organizationId: null,
folderId: null, folderId: null,
name: null, name: null,
notes: null, notes: null,
}, },
["id", "userId", "organizationId", "folderId"] ["id", "organizationId", "folderId"]
); );
this.type = obj.type; this.type = obj.type;
@ -186,12 +185,11 @@ export class Cipher extends Domain {
return model; return model;
} }
toCipherData(userId: string): CipherData { toCipherData(): CipherData {
const c = new CipherData(); const c = new CipherData();
c.id = this.id; c.id = this.id;
c.organizationId = this.organizationId; c.organizationId = this.organizationId;
c.folderId = this.folderId; c.folderId = this.folderId;
c.userId = this.organizationId != null ? userId : null;
c.edit = this.edit; c.edit = this.edit;
c.viewPassword = this.viewPassword; c.viewPassword = this.viewPassword;
c.organizationUseTotp = this.organizationUseTotp; c.organizationUseTotp = this.organizationUseTotp;

View File

@ -12,7 +12,6 @@ import { SendText } from "./sendText";
export class Send extends Domain { export class Send extends Domain {
id: string; id: string;
accessId: string; accessId: string;
userId: string;
type: SendType; type: SendType;
name: EncString; name: EncString;
notes: EncString; notes: EncString;
@ -40,12 +39,11 @@ export class Send extends Domain {
{ {
id: null, id: null,
accessId: null, accessId: null,
userId: null,
name: null, name: null,
notes: null, notes: null,
key: null, key: null,
}, },
["id", "accessId", "userId"] ["id", "accessId"]
); );
this.type = obj.type; this.type = obj.type;

View File

@ -605,11 +605,7 @@ export class CipherService implements CipherServiceAbstraction {
response = await this.apiService.putCipher(cipher.id, request); response = await this.apiService.putCipher(cipher.id, request);
} }
const data = new CipherData( const data = new CipherData(response, cipher.collectionIds);
response,
await this.stateService.getUserId(),
cipher.collectionIds
);
await this.upsert(data); await this.upsert(data);
} }
@ -635,7 +631,7 @@ export class CipherService implements CipherServiceAbstraction {
const encCipher = await this.encrypt(cipher); const encCipher = await this.encrypt(cipher);
const request = new CipherShareRequest(encCipher); const request = new CipherShareRequest(encCipher);
const response = await this.apiService.putShareCipher(cipher.id, request); const response = await this.apiService.putShareCipher(cipher.id, request);
const data = new CipherData(response, await this.stateService.getUserId(), collectionIds); const data = new CipherData(response, collectionIds);
await this.upsert(data); await this.upsert(data);
} }
@ -666,8 +662,7 @@ export class CipherService implements CipherServiceAbstraction {
} }
throw e; throw e;
} }
const userId = await this.stateService.getUserId(); await this.upsert(encCiphers.map((c) => c.toCipherData()));
await this.upsert(encCiphers.map((c) => c.toCipherData(userId)));
} }
saveAttachmentWithServer(cipher: Cipher, unencryptedFile: any, admin = false): Promise<Cipher> { saveAttachmentWithServer(cipher: Cipher, unencryptedFile: any, admin = false): Promise<Cipher> {
@ -741,11 +736,7 @@ export class CipherService implements CipherServiceAbstraction {
} }
} }
const cData = new CipherData( const cData = new CipherData(response, cipher.collectionIds);
response,
await this.stateService.getUserId(),
cipher.collectionIds
);
if (!admin) { if (!admin) {
await this.upsert(cData); await this.upsert(cData);
} }
@ -801,7 +792,7 @@ export class CipherService implements CipherServiceAbstraction {
async saveCollectionsWithServer(cipher: Cipher): Promise<any> { async saveCollectionsWithServer(cipher: Cipher): Promise<any> {
const request = new CipherCollectionsRequest(cipher.collectionIds); const request = new CipherCollectionsRequest(cipher.collectionIds);
await this.apiService.putCipherCollections(cipher.id, request); await this.apiService.putCipherCollections(cipher.id, request);
const data = cipher.toCipherData(await this.stateService.getUserId()); const data = cipher.toCipherData();
await this.upsert(data); await this.upsert(data);
} }

View File

@ -117,8 +117,7 @@ export class FolderService implements FolderServiceAbstraction {
response = await this.apiService.putFolder(folder.id, request); response = await this.apiService.putFolder(folder.id, request);
} }
const userId = await this.stateService.getUserId(); const data = new FolderData(response);
const data = new FolderData(response, userId);
await this.upsert(data); await this.upsert(data);
} }

View File

@ -166,8 +166,7 @@ export class SendService implements SendServiceAbstraction {
response = await this.apiService.putSend(sendData[0].id, request); response = await this.apiService.putSend(sendData[0].id, request);
} }
const userId = await this.stateService.getUserId(); const data = new SendData(response);
const data = new SendData(response, userId);
await this.upsert(data); await this.upsert(data);
} }
@ -257,8 +256,7 @@ export class SendService implements SendServiceAbstraction {
async removePasswordWithServer(id: string): Promise<any> { async removePasswordWithServer(id: string): Promise<any> {
const response = await this.apiService.putSendRemovePassword(id); const response = await this.apiService.putSendRemovePassword(id);
const userId = await this.stateService.getUserId(); const data = new SendData(response);
const data = new SendData(response, userId);
await this.upsert(data); await this.upsert(data);
} }

View File

@ -95,16 +95,15 @@ export class SyncService implements SyncServiceAbstraction {
return this.syncCompleted(false); return this.syncCompleted(false);
} }
const userId = await this.stateService.getUserId();
try { try {
await this.apiService.refreshIdentityToken(); await this.apiService.refreshIdentityToken();
const response = await this.apiService.getSync(); const response = await this.apiService.getSync();
await this.syncProfile(response.profile); await this.syncProfile(response.profile);
await this.syncFolders(userId, response.folders); await this.syncFolders(response.folders);
await this.syncCollections(response.collections); await this.syncCollections(response.collections);
await this.syncCiphers(userId, response.ciphers); await this.syncCiphers(response.ciphers);
await this.syncSends(userId, response.sends); await this.syncSends(response.sends);
await this.syncSettings(response.domains); await this.syncSettings(response.domains);
await this.syncPolicies(response.policies); await this.syncPolicies(response.policies);
@ -130,8 +129,7 @@ export class SyncService implements SyncServiceAbstraction {
) { ) {
const remoteFolder = await this.apiService.getFolder(notification.id); const remoteFolder = await this.apiService.getFolder(notification.id);
if (remoteFolder != null) { if (remoteFolder != null) {
const userId = await this.stateService.getUserId(); await this.folderService.upsert(new FolderData(remoteFolder));
await this.folderService.upsert(new FolderData(remoteFolder, userId));
this.messagingService.send("syncedUpsertedFolder", { folderId: notification.id }); this.messagingService.send("syncedUpsertedFolder", { folderId: notification.id });
return this.syncCompleted(true); return this.syncCompleted(true);
} }
@ -200,8 +198,7 @@ export class SyncService implements SyncServiceAbstraction {
if (shouldUpdate) { if (shouldUpdate) {
const remoteCipher = await this.apiService.getCipher(notification.id); const remoteCipher = await this.apiService.getCipher(notification.id);
if (remoteCipher != null) { if (remoteCipher != null) {
const userId = await this.stateService.getUserId(); await this.cipherService.upsert(new CipherData(remoteCipher));
await this.cipherService.upsert(new CipherData(remoteCipher, userId));
this.messagingService.send("syncedUpsertedCipher", { cipherId: notification.id }); this.messagingService.send("syncedUpsertedCipher", { cipherId: notification.id });
return this.syncCompleted(true); return this.syncCompleted(true);
} }
@ -238,8 +235,7 @@ export class SyncService implements SyncServiceAbstraction {
) { ) {
const remoteSend = await this.apiService.getSend(notification.id); const remoteSend = await this.apiService.getSend(notification.id);
if (remoteSend != null) { if (remoteSend != null) {
const userId = await this.stateService.getUserId(); await this.sendService.upsert(new SendData(remoteSend));
await this.sendService.upsert(new SendData(remoteSend, userId));
this.messagingService.send("syncedUpsertedSend", { sendId: notification.id }); this.messagingService.send("syncedUpsertedSend", { sendId: notification.id });
return this.syncCompleted(true); return this.syncCompleted(true);
} }
@ -339,10 +335,10 @@ export class SyncService implements SyncServiceAbstraction {
} }
} }
private async syncFolders(userId: string, response: FolderResponse[]) { private async syncFolders(response: FolderResponse[]) {
const folders: { [id: string]: FolderData } = {}; const folders: { [id: string]: FolderData } = {};
response.forEach((f) => { response.forEach((f) => {
folders[f.id] = new FolderData(f, userId); folders[f.id] = new FolderData(f);
}); });
return await this.folderService.replace(folders); return await this.folderService.replace(folders);
} }
@ -355,18 +351,18 @@ export class SyncService implements SyncServiceAbstraction {
return await this.collectionService.replace(collections); return await this.collectionService.replace(collections);
} }
private async syncCiphers(userId: string, response: CipherResponse[]) { private async syncCiphers(response: CipherResponse[]) {
const ciphers: { [id: string]: CipherData } = {}; const ciphers: { [id: string]: CipherData } = {};
response.forEach((c) => { response.forEach((c) => {
ciphers[c.id] = new CipherData(c, userId); ciphers[c.id] = new CipherData(c);
}); });
return await this.cipherService.replace(ciphers); return await this.cipherService.replace(ciphers);
} }
private async syncSends(userId: string, response: SendResponse[]) { private async syncSends(response: SendResponse[]) {
const sends: { [id: string]: SendData } = {}; const sends: { [id: string]: SendData } = {};
response.forEach((s) => { response.forEach((s) => {
sends[s.id] = new SendData(s, userId); sends[s.id] = new SendData();
}); });
return await this.sendService.replace(sends); return await this.sendService.replace(sends);
} }