mirror of
https://github.com/bitwarden/browser
synced 2025-12-15 15:53:27 +00:00
[PM-25131] Initialize provider keys on the SDK (#16183)
* [PM-25131] Initialize provider keys on the SDK * Remove null default * Typechecking
This commit is contained in:
@@ -907,10 +907,57 @@ export class DefaultKeyService implements KeyServiceAbstraction {
|
||||
return this.cipherDecryptionKeys$(userId).pipe(map((keys) => keys?.orgKeys ?? null));
|
||||
}
|
||||
|
||||
encryptedOrgKeys$(
|
||||
userId: UserId,
|
||||
): Observable<Record<OrganizationId, EncryptedOrganizationKeyData> | null> {
|
||||
return this.stateProvider.getUser(userId, USER_ENCRYPTED_ORGANIZATION_KEYS).state$;
|
||||
encryptedOrgKeys$(userId: UserId): Observable<Record<OrganizationId, EncString>> {
|
||||
return this.userPrivateKey$(userId)?.pipe(
|
||||
switchMap((userPrivateKey) => {
|
||||
if (userPrivateKey == null) {
|
||||
// We can't do any org based decryption
|
||||
return of({});
|
||||
}
|
||||
|
||||
return combineLatest([
|
||||
this.stateProvider.getUser(userId, USER_ENCRYPTED_ORGANIZATION_KEYS).state$,
|
||||
this.providerKeysHelper$(userId, userPrivateKey),
|
||||
]).pipe(
|
||||
switchMap(async ([encryptedOrgKeys, providerKeys]) => {
|
||||
const userPubKey = await this.derivePublicKey(userPrivateKey);
|
||||
|
||||
const result: Record<OrganizationId, EncString> = {};
|
||||
encryptedOrgKeys = encryptedOrgKeys ?? {};
|
||||
for (const orgId of Object.keys(encryptedOrgKeys) as OrganizationId[]) {
|
||||
if (result[orgId] != null) {
|
||||
continue;
|
||||
}
|
||||
const encrypted = BaseEncryptedOrganizationKey.fromData(encryptedOrgKeys[orgId]);
|
||||
if (encrypted == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
let orgKey: EncString;
|
||||
|
||||
// Because the SDK only supports user encrypted org keys, we need to re-encrypt
|
||||
// any provider encrypted org keys with the user's public key. This should be removed
|
||||
// once the SDK has support for provider keys.
|
||||
if (BaseEncryptedOrganizationKey.isProviderEncrypted(encrypted)) {
|
||||
if (providerKeys == null) {
|
||||
continue;
|
||||
}
|
||||
orgKey = await this.encryptService.encapsulateKeyUnsigned(
|
||||
await encrypted.decrypt(this.encryptService, providerKeys!),
|
||||
userPubKey!,
|
||||
);
|
||||
} else {
|
||||
orgKey = encrypted.encryptedOrganizationKey;
|
||||
}
|
||||
|
||||
result[orgId] = orgKey;
|
||||
}
|
||||
|
||||
return result;
|
||||
}),
|
||||
);
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
cipherDecryptionKeys$(userId: UserId): Observable<CipherDecryptionKeys | null> {
|
||||
|
||||
Reference in New Issue
Block a user