From f94ff818d9c5c176e1f47cc45f9cdd3453a7fbe4 Mon Sep 17 00:00:00 2001 From: Kegan Dougal <7190048+kegsay@users.noreply.github.com> Date: Wed, 10 Jan 2024 13:25:56 +0000 Subject: [PATCH] Get key backup restoring working in JS --- internal/api/js.go | 15 ++++++++------- tests/key_backup_test.go | 5 +---- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/internal/api/js.go b/internal/api/js.go index 95affeb..aeed210 100644 --- a/internal/api/js.go +++ b/internal/api/js.go @@ -360,14 +360,15 @@ func (c *JSClient) MustBackupKeys(t *testing.T) (recoveryKey string) { func (c *JSClient) MustLoadBackup(t *testing.T, recoveryKey string) { chrome.MustAwaitExecute(t, c.ctx, fmt.Sprintf(`(async () => { - // add the recovery key to secret storage - const recoveryKeyInfo = await window.__client.secretStorage.addKey("m.secret_storage.v1.aes-hmac-sha2", { + // we assume the recovery key is the private key for the default key id so + // figure out what that key id is. + const keyId = await window.__client.secretStorage.getDefaultKeyId(); + // now add this to the in-memory cache. We don't actually ever return key info so we just pass in {} here. + window._secretStorageKeys[keyId] = { + keyInfo: {}, key: window.decodeRecoveryKey("%s"), - }); - console.log("setting default key ID to " + recoveryKeyInfo.keyId); - // FIXME: this needs the client to be syncing already as this promise won't resolve until it comes down /sync, wedging forever - await window.__client.secretStorage.setDefaultKeyId(recoveryKeyInfo.keyId); - console.log("done!"); + } + console.log("will return recovery key for default key id " + keyId); const keyBackupCheck = await window.__client.getCrypto().checkKeyBackupAndEnable(); console.log("key backup: ", JSON.stringify(keyBackupCheck)); // FIXME: this just doesn't seem to work, causing 'Error: getSecretStorageKey callback returned invalid data' because the key ID diff --git a/tests/key_backup_test.go b/tests/key_backup_test.go index 76c7e85..6e2bc18 100644 --- a/tests/key_backup_test.go +++ b/tests/key_backup_test.go @@ -13,14 +13,11 @@ import ( // Test that backups can be restored using secret storage and the recovery key. func TestCanBackupKeys(t *testing.T) { ClientTypeMatrix(t, func(t *testing.T, clientTypeA, clientTypeB api.ClientType) { - if clientTypeB.Lang == api.ClientTypeJS { - t.Skipf("key backup restoring is unsupported (js)") - return - } if clientTypeA.HS != clientTypeB.HS { t.Skipf("client A and B must be on the same HS as this is testing key backups so A=backup creator B=backup restorer") return } + t.Logf("backup creator = %s backup restorer = %s", clientTypeA.Lang, clientTypeB.Lang) deployment := Deploy(t) csapiAlice := deployment.Register(t, clientTypeA.HS, helpers.RegistrationOpts{ LocalpartSuffix: "alice",