1
0
mirror of https://github.com/bitwarden/jslib synced 2025-12-06 00:03:29 +00:00

Merge branch 'master' of https://github.com/bitwarden/jslib into feature/refactor-folder-example

# Conflicts:
#	common/spec/domain/folder.spec.ts
#	common/src/models/data/folderData.ts
#	common/src/services/folder.service.ts
#	common/src/services/sync.service.ts
This commit is contained in:
Hinton
2022-04-21 09:42:46 +02:00
14 changed files with 29 additions and 84 deletions

View File

@@ -61,7 +61,6 @@ export class UpdatePasswordComponent extends BaseChangePasswordComponent {
async cancel() { async cancel() {
await this.stateService.setOrganizationInvitation(null); await this.stateService.setOrganizationInvitation(null);
await this.stateService.setLoginRedirect(null);
this.router.navigate(["/vault"]); this.router.navigate(["/vault"]);
} }

View File

@@ -19,7 +19,7 @@ export class AuthGuardService implements CanActivate {
async canActivate(route: ActivatedRouteSnapshot, routerState: RouterStateSnapshot) { async canActivate(route: ActivatedRouteSnapshot, routerState: RouterStateSnapshot) {
const isAuthed = await this.stateService.getIsAuthenticated(); const isAuthed = await this.stateService.getIsAuthenticated();
if (!isAuthed) { if (!isAuthed) {
this.messagingService.send("authBlocked"); this.messagingService.send("authBlocked", { url: routerState.url });
return false; return false;
} }
@@ -28,16 +28,14 @@ export class AuthGuardService implements CanActivate {
if (routerState != null) { if (routerState != null) {
this.messagingService.send("lockedUrl", { url: routerState.url }); this.messagingService.send("lockedUrl", { url: routerState.url });
} }
this.router.navigate(["lock"], { queryParams: { promptBiometric: true } }); return this.router.createUrlTree(["lock"], { queryParams: { promptBiometric: true } });
return false;
} }
if ( if (
!routerState.url.includes("remove-password") && !routerState.url.includes("remove-password") &&
(await this.keyConnectorService.getConvertAccountRequired()) (await this.keyConnectorService.getConvertAccountRequired())
) { ) {
this.router.navigate(["/remove-password"]); return this.router.createUrlTree(["/remove-password"]);
return false;
} }
return true; return true;

View File

@@ -18,11 +18,9 @@ export class UnauthGuardService implements CanActivate {
if (isAuthed) { if (isAuthed) {
const locked = await this.vaultTimeoutService.isLocked(); const locked = await this.vaultTimeoutService.isLocked();
if (locked) { if (locked) {
this.router.navigate(["lock"]); return this.router.createUrlTree(["lock"]);
} else {
this.router.navigate([this.homepage]);
} }
return false; return this.router.createUrlTree([this.homepage]);
} }
return true; return true;
} }

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

@@ -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

@@ -242,8 +242,6 @@ export abstract class StateService<T extends Account = Account> {
setLocalData: (value: string, options?: StorageOptions) => Promise<void>; setLocalData: (value: string, options?: StorageOptions) => Promise<void>;
getLocale: (options?: StorageOptions) => Promise<string>; getLocale: (options?: StorageOptions) => Promise<string>;
setLocale: (value: string, options?: StorageOptions) => Promise<void>; setLocale: (value: string, options?: StorageOptions) => Promise<void>;
getLoginRedirect: (options?: StorageOptions) => Promise<any>;
setLoginRedirect: (value: any, options?: StorageOptions) => Promise<void>;
getMainWindowSize: (options?: StorageOptions) => Promise<number>; getMainWindowSize: (options?: StorageOptions) => Promise<number>;
setMainWindowSize: (value: number, options?: StorageOptions) => Promise<void>; setMainWindowSize: (value: number, options?: StorageOptions) => Promise<void>;
getMinimizeOnCopyToClipboard: (options?: StorageOptions) => Promise<boolean>; getMinimizeOnCopyToClipboard: (options?: StorageOptions) => Promise<boolean>;

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

@@ -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

@@ -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

@@ -1633,19 +1633,6 @@ export class StateService<
); );
} }
async getLoginRedirect(options?: StorageOptions): Promise<any> {
return (await this.getGlobals(this.reconcileOptions(options, this.defaultInMemoryOptions)))
?.loginRedirect;
}
async setLoginRedirect(value: any, options?: StorageOptions): Promise<void> {
const globals = await this.getGlobals(
this.reconcileOptions(options, this.defaultInMemoryOptions)
);
globals.loginRedirect = value;
await this.saveGlobals(globals, this.reconcileOptions(options, this.defaultInMemoryOptions));
}
async getMainWindowSize(options?: StorageOptions): Promise<number> { async getMainWindowSize(options?: StorageOptions): Promise<number> {
return (await this.getGlobals(this.reconcileOptions(options, this.defaultInMemoryOptions))) return (await this.getGlobals(this.reconcileOptions(options, this.defaultInMemoryOptions)))
?.mainWindowSize; ?.mainWindowSize;

View File

@@ -95,7 +95,6 @@ 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();
@@ -103,8 +102,8 @@ export class SyncService implements SyncServiceAbstraction {
await this.syncProfile(response.profile); await this.syncProfile(response.profile);
await this.syncFolders(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);
@@ -199,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);
} }
@@ -237,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);
} }
@@ -354,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);
} }