1
0
mirror of https://github.com/bitwarden/browser synced 2026-02-12 22:44:11 +00:00

Add handling missing private key in initialize org crypto for SdkService

This commit is contained in:
Thomas Avery
2024-11-07 11:48:24 -06:00
parent b42741f313
commit d0d2676f99
3 changed files with 26 additions and 9 deletions

View File

@@ -1370,6 +1370,7 @@ const safeProviders: SafeProvider[] = [
KdfConfigServiceAbstraction,
KeyServiceAbstraction,
ApiServiceAbstraction,
LogService,
],
}),
safeProvider({

View File

@@ -11,6 +11,7 @@ import { PBKDF2KdfConfig } from "../../../auth/models/domain/kdf-config";
import { UserId } from "../../../types/guid";
import { UserKey } from "../../../types/key";
import { Environment, EnvironmentService } from "../../abstractions/environment.service";
import { LogService } from "../../abstractions/log.service";
import { PlatformUtilsService } from "../../abstractions/platform-utils.service";
import { SdkClientFactory } from "../../abstractions/sdk/sdk-client-factory";
import { EncryptedString } from "../../models/domain/enc-string";
@@ -27,6 +28,7 @@ describe("DefaultSdkService", () => {
let kdfConfigService!: MockProxy<KdfConfigService>;
let keyService!: MockProxy<KeyService>;
let apiService!: MockProxy<ApiService>;
let logService!: MockProxy<LogService>;
let service!: DefaultSdkService;
let mockClient!: MockProxy<BitwardenClient>;
@@ -39,6 +41,7 @@ describe("DefaultSdkService", () => {
kdfConfigService = mock<KdfConfigService>();
keyService = mock<KeyService>();
apiService = mock<ApiService>();
logService = mock<LogService>();
// Can't use `of(mock<Environment>())` for some reason
environmentService.environment$ = new BehaviorSubject(mock<Environment>());
@@ -51,6 +54,7 @@ describe("DefaultSdkService", () => {
kdfConfigService,
keyService,
apiService,
logService,
);
mockClient = mock<BitwardenClient>();

View File

@@ -27,6 +27,7 @@ import { DeviceType } from "../../../enums/device-type.enum";
import { OrganizationId, UserId } from "../../../types/guid";
import { UserKey } from "../../../types/key";
import { Environment, EnvironmentService } from "../../abstractions/environment.service";
import { LogService } from "../../abstractions/log.service";
import { PlatformUtilsService } from "../../abstractions/platform-utils.service";
import { SdkClientFactory } from "../../abstractions/sdk/sdk-client-factory";
import { SdkService } from "../../abstractions/sdk/sdk.service";
@@ -59,6 +60,7 @@ export class DefaultSdkService implements SdkService {
private kdfConfigService: KdfConfigService,
private keyService: KeyService,
private apiService: ApiService, // Yes we shouldn't import ApiService, but it's temporary
private logService: LogService,
private userAgent: string = null,
) {}
@@ -181,15 +183,25 @@ export class DefaultSdkService implements SdkService {
privateKey,
});
// We initialize the org crypto even if the org_keys are
// null to make sure any existing org keys are cleared.
await client.crypto().initialize_org_crypto({
organizationKeys: new Map(
Object.entries(orgKeys ?? {})
.filter(([_, v]) => v.type === "organization")
.map(([k, v]) => [k, v.key]),
),
});
try {
// We initialize the org crypto even if the org_keys are
// null to make sure any existing org keys are cleared.
await client.crypto().initialize_org_crypto({
organizationKeys: new Map(
Object.entries(orgKeys ?? {})
.filter(([_, v]) => v.type === "organization")
.map(([k, v]) => [k, v.key]),
),
});
} catch (e) {
if (e.message === "Missing private key") {
this.logService.warning(
"[SdkService] organization crypto not initialized, missing private key",
);
} else {
throw e;
}
}
}
private toSettings(env: Environment): ClientSettings {