Skip to content

Commit

Permalink
Review changes
Browse files Browse the repository at this point in the history
  • Loading branch information
florianduros committed Jan 14, 2025
1 parent 9c5504a commit 80a0981
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions src/secret-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ export interface ServerSideSecretStorage {

/**
* Set the default key ID for encrypting secrets.
*
* If keyId is `null`, the default key id value in the account data will be set to an empty object.
* This is considered as "disabling" the default key.
*
Expand Down Expand Up @@ -367,19 +368,26 @@ export class ServerSideSecretStorageImpl implements ServerSideSecretStorage {
public setDefaultKeyId(keyId: string | null): Promise<void> {
return new Promise<void>((resolve, reject) => {
const listener = (ev: MatrixEvent): void => {
if (ev.getType() === "m.secret_storage.default_key") {
// Different account data item
return;
}

const content = ev.getContent();
// If keyId === null, the content should be an empty object.
// Otherwise, keyId should be returned in the content object.
const isSameKey = keyId === null ? !Object.keys(content).length : content.key === keyId;
if (ev.getType() === "m.secret_storage.default_key" && isSameKey) {
if (isSameKey) {
this.accountDataAdapter.removeListener(ClientEvent.AccountData, listener);
resolve();
}
};
this.accountDataAdapter.on(ClientEvent.AccountData, listener);

// The spec says that the key should be an object with a `key` property
// https://spec.matrix.org/v1.13/client-server-api/#key-storage
// To delete the default key, we send an empty object like the rust sdk does
// (see https://docs.rs/matrix-sdk/latest/matrix_sdk/encryption/recovery/struct.Recovery.html#method.reset_identity)
// The spec [1] says that the value of the account data entry should be an object with a `key` property.
// It doesn't specify how to delete the default key; we do it by setting the account data to an empty object.
//
// [1]: https://spec.matrix.org/v1.13/client-server-api/#key-storage
const newValue: Record<string, never> | { key: string } = keyId === null ? {} : { key: keyId };
this.accountDataAdapter.setAccountData("m.secret_storage.default_key", newValue).catch((e) => {
this.accountDataAdapter.removeListener(ClientEvent.AccountData, listener);
Expand Down

0 comments on commit 80a0981

Please sign in to comment.