Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 0 additions & 22 deletions libs/common/src/key-management/ephemeral-pin-envelope-mapper.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ import {
USER_KEY_ENCRYPTED_PIN,
} from "./pin.state";

const EPHEMERAL_PIN_ENVELOPE_KEY = "";

export class PinStateService implements PinStateServiceAbstraction {
constructor(private stateProvider: StateProvider) {}

Expand Down Expand Up @@ -82,7 +80,7 @@ export class PinStateService implements PinStateServiceAbstraction {
if (pinLockType === "EPHEMERAL") {
await this.stateProvider.setUserState(
PIN_PROTECTED_USER_KEY_ENVELOPE_EPHEMERAL,
{ [EPHEMERAL_PIN_ENVELOPE_KEY]: { pin_envelope: pinProtectedUserKeyEnvelope } },
pinProtectedUserKeyEnvelope,
userId,
);
} else if (pinLockType === "PERSISTENT") {
Expand Down Expand Up @@ -121,7 +119,7 @@ export class PinStateService implements PinStateServiceAbstraction {
if (pinLockType === "EPHEMERAL") {
return this.stateProvider
.getUserState$(PIN_PROTECTED_USER_KEY_ENVELOPE_EPHEMERAL, userId)
.pipe(map((record) => record?.[EPHEMERAL_PIN_ENVELOPE_KEY]?.pin_envelope ?? null));
.pipe(map((envelope) => envelope ?? null));
} else if (pinLockType === "PERSISTENT") {
return this.stateProvider.getUserState$(PIN_PROTECTED_USER_KEY_ENVELOPE_PERSISTENT, userId);
} else {
Expand Down
4 changes: 2 additions & 2 deletions libs/common/src/key-management/pin/pin.state.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { PIN_DISK, PIN_MEMORY, UserKeyDefinition } from "@bitwarden/common/platform/state";
import { PasswordProtectedKeyEnvelope, EphemeralPinEnvelopeState } from "@bitwarden/sdk-internal";
import { PasswordProtectedKeyEnvelope } from "@bitwarden/sdk-internal";

import { EncryptedString } from "../crypto/models/enc-string";

Expand All @@ -23,7 +23,7 @@ export const PIN_PROTECTED_USER_KEY_ENVELOPE_PERSISTENT =
* The ephemeral (stored in memory) version of the UserKey, stored in a `PasswordProtectedKeyEnvelope`.
*/
export const PIN_PROTECTED_USER_KEY_ENVELOPE_EPHEMERAL =
UserKeyDefinition.record<EphemeralPinEnvelopeState>(
new UserKeyDefinition<PasswordProtectedKeyEnvelope>(
PIN_MEMORY,
"pinProtectedUserKeyEnvelopeEphemeral",
{
Expand Down
27 changes: 14 additions & 13 deletions libs/common/src/key-management/state-bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,15 +122,18 @@ export class JsWasmStateBridge implements WasmStateBridge {
}

async set_user_key(userKey: SymmetricKey): Promise<void> {
await writeAtomic(this.stateProvider, this.userId, USER_KEY, {
"": SymmetricCryptoKey.fromSdk(userKey) as UserKey,
});
await writeAtomic(
this.stateProvider,
this.userId,
USER_KEY,
SymmetricCryptoKey.fromSdk(userKey) as UserKey,
);
}

async get_user_key(): Promise<SymmetricKey | null> {
const key = await readAtomic(this.stateProvider, this.userId, USER_KEY);
if (key != null) {
return key[""].toSdk();
return key.toSdk();
} else {
return null;
}
Expand All @@ -141,22 +144,20 @@ export class JsWasmStateBridge implements WasmStateBridge {
}

async set_ephemeral_pin_envelope(pinEnvelope: PasswordProtectedKeyEnvelope): Promise<void> {
await writeAtomic(this.stateProvider, this.userId, PIN_PROTECTED_USER_KEY_ENVELOPE_EPHEMERAL, {
"": { pin_envelope: pinEnvelope },
});
await writeAtomic(
this.stateProvider,
this.userId,
PIN_PROTECTED_USER_KEY_ENVELOPE_EPHEMERAL,
pinEnvelope,
);
}

async get_ephemeral_pin_envelope(): Promise<PasswordProtectedKeyEnvelope | null> {
const result = await readAtomic(
return await readAtomic(
this.stateProvider,
this.userId,
PIN_PROTECTED_USER_KEY_ENVELOPE_EPHEMERAL,
);
if (result != null) {
return result[""]?.pin_envelope ?? null;
} else {
return null;
}
}

async clear_ephemeral_pin_envelope(): Promise<void> {
Expand Down
21 changes: 0 additions & 21 deletions libs/common/src/key-management/user-key-mapper.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const USER_EVER_HAD_USER_KEY = new UserKeyDefinition<boolean>(
},
);

export const USER_KEY = UserKeyDefinition.record<UserKey>(CRYPTO_MEMORY, "userKey", {
export const USER_KEY = new UserKeyDefinition<UserKey>(CRYPTO_MEMORY, "userKey", {
deserializer: (obj) => SymmetricCryptoKey.fromJSON(obj) as UserKey,
clearOn: ["logout", "lock"],
// Prevents the state from caching and rxjs observable becoming hot observable.
Expand Down
10 changes: 2 additions & 8 deletions libs/common/src/platform/services/sdk/client-managed-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import { UserId } from "@bitwarden/common/types/guid";
import { CipherRecordMapper } from "@bitwarden/common/vault/models/domain/cipher-sdk-mapper";
import { Repository, StateClient } from "@bitwarden/sdk-internal";

import { EphemeralPinEnvelopeMapper } from "../../../key-management/ephemeral-pin-envelope-mapper";
import { LocalUserDataKeyRecordMapper } from "../../../key-management/local-user-data-key-mapper";
import { UserKeyRecordMapper } from "../../../key-management/user-key-mapper";
import { StateProvider, UserKeyDefinition } from "../../state";

export async function initializeClientManagedState(
Expand All @@ -17,17 +15,13 @@ export async function initializeClientManagedState(
stateClient.register_client_managed_repositories({
cipher: new RepositoryRecord(userId, stateProvider, new CipherRecordMapper(), true),
folder: null,
user_key_state: new RepositoryRecord(userId, stateProvider, new UserKeyRecordMapper()),
user_key_state: null,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This won't exist anymore and needs to be removed, instead of nulled.

local_user_data_key_state: new RepositoryRecord(
userId,
stateProvider,
new LocalUserDataKeyRecordMapper(),
),
ephemeral_pin_envelope_state: new RepositoryRecord(
userId,
stateProvider,
new EphemeralPinEnvelopeMapper(),
),
ephemeral_pin_envelope_state: null,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This won't exist anymore and needs to be removed, instead of nulled.

organization_shared_key: null,
});
}
Expand Down
8 changes: 2 additions & 6 deletions libs/key-management/src/key.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,7 @@ describe("keyService", () => {
});

const setUserKeyState = (userId: UserId, userKey: UserKey | null) => {
stateProvider.singleUser
.getFake(userId, USER_KEY)
.nextState(userKey == null ? null : ({ "": userKey } as Record<string, UserKey>));
stateProvider.singleUser.getFake(userId, USER_KEY).nextState(userKey);
};

afterEach(() => {
Expand Down Expand Up @@ -899,9 +897,7 @@ describe("keyService", () => {
masterPasswordService.masterKeySubject.next(fakeMasterKey);
userKeyState.nextState(null);
const fakeUserKey = makeUserKey ? makeSymmetricCryptoKey<UserKey>(64) : null;
userKeyState.nextState(
fakeUserKey == null ? null : ({ "": fakeUserKey } as Record<string, UserKey>),
);
userKeyState.nextState(fakeUserKey);
return [fakeUserKey, fakeMasterKey];
}

Expand Down
27 changes: 4 additions & 23 deletions libs/key-management/src/key.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,6 @@ import {
} from "./abstractions/key.service";
import { KdfConfig } from "./models/kdf-config";

const USER_KEY_STATE_KEY: string = "";

export class DefaultKeyService implements KeyServiceAbstraction {
/**
* Retrieves a stream of the active users organization keys,
Expand Down Expand Up @@ -110,7 +108,7 @@ export class DefaultKeyService implements KeyServiceAbstraction {
}

// Set userId to ensure we have one for the account status update
await this.stateProvider.setUserState(USER_KEY, this.userKeyToStateObject(key), userId);
await this.stateProvider.setUserState(USER_KEY, key, userId);
await this.stateProvider.setUserState(USER_EVER_HAD_USER_KEY, true, userId);

await this.storeAdditionalKeys(key, userId);
Expand Down Expand Up @@ -145,15 +143,14 @@ export class DefaultKeyService implements KeyServiceAbstraction {
getInMemoryUserKeyFor$(userId: UserId): Observable<UserKey | null> {
return this.stateProvider
.getUserState$(USER_KEY, userId)
.pipe(map((userKey) => this.stateObjectToUserKey(userKey)));
.pipe(map((userKey) => userKey ?? null));
}

/**
* @deprecated Use {@link userKey$} with a required {@link UserId} instead.
*/
async getUserKey(userId?: UserId): Promise<UserKey | null> {
const userKey = await firstValueFrom(this.stateProvider.getUserState$(USER_KEY, userId));
return this.stateObjectToUserKey(userKey);
return (await firstValueFrom(this.stateProvider.getUserState$(USER_KEY, userId))) ?? null;
}

async getUserKeyFromStorage(
Expand Down Expand Up @@ -640,9 +637,7 @@ export class DefaultKeyService implements KeyServiceAbstraction {
}

userKey$(userId: UserId): Observable<UserKey | null> {
return this.stateProvider
.getUser(userId, USER_KEY)
.state$.pipe(map((key) => (key != null ? (key[""] as UserKey) : null)));
return this.stateProvider.getUser(userId, USER_KEY).state$.pipe(map((key) => key ?? null));
}

userPublicKey$(userId: UserId) {
Expand Down Expand Up @@ -921,18 +916,4 @@ export class DefaultKeyService implements KeyServiceAbstraction {
}),
);
}

private userKeyToStateObject(userKey: UserKey | null): Record<string, UserKey> | null {
if (userKey == null) {
return null;
}
return { [USER_KEY_STATE_KEY]: userKey };
}

private stateObjectToUserKey(stateObject: Record<string, UserKey> | null): UserKey | null {
if (stateObject == null) {
return null;
}
return stateObject[USER_KEY_STATE_KEY] ?? null;
}
}
Loading