1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-08-09 10:22:46 +03:00

Rename ssss cache functions to be more general

This commit is contained in:
Zoe
2020-03-20 10:18:06 +00:00
parent 78d9111646
commit 6701fdd486
6 changed files with 12 additions and 123 deletions

View File

@@ -23,15 +23,10 @@ import {MatrixEvent} from "../../../src/models/event";
import * as algorithms from "../../../src/crypto/algorithms";
import {WebStorageSessionStore} from "../../../src/store/session/webstorage";
import {MemoryCryptoStore} from "../../../src/crypto/store/memory-crypto-store";
import {
IndexedDBCryptoStore,
} from '../../../src/crypto/store/indexeddb-crypto-store';
import {MockStorageApi} from "../../MockStorageApi";
import * as testUtils from "../../test-utils";
import {OlmDevice} from "../../../src/crypto/OlmDevice";
import {Crypto} from "../../../src/crypto";
import 'fake-indexeddb/auto';
import 'jest-localstorage-mock';
const Olm = global.Olm;
@@ -84,13 +79,6 @@ const BACKUP_INFO = {
},
};
const testKey = new Uint8Array([
0xda, 0x5a, 0x27, 0x60, 0xe3, 0x3a, 0xc5, 0x82,
0x9d, 0x12, 0xc3, 0xbe, 0xe8, 0xaa, 0xc2, 0xef,
0xae, 0xb1, 0x05, 0xc1, 0xe7, 0x62, 0x78, 0xa6,
0xd7, 0x1f, 0xf8, 0x2c, 0x51, 0x85, 0xf0, 0x1d,
]);
const keys = {};
function getCrossSigningKey(type) {
@@ -555,45 +543,3 @@ describe("MegolmBackup", function() {
});
});
});
describe.each([
["IndexedDBCryptoStore",
() => new IndexedDBCryptoStore(global.indexedDB, "tests")],
["LocalStorageCryptoStore",
() => new IndexedDBCryptoStore(undefined, "tests")],
["MemoryCryptoStore", () => {
const store = new IndexedDBCryptoStore(undefined, "tests");
store._backend = new MemoryCryptoStore();
store._backendPromise = Promise.resolve(store._backend);
return store;
}],
])("Crypto store backup key cache functions [%s]", function(name, dbFactory) {
let store;
beforeAll(async () => {
store = dbFactory();
await store.startup();
});
it("Stores and retrieves a backup key", async () => {
expect(store._backend).not.toBeNull();
await store.doTxn(
'readwrite',
[IndexedDBCryptoStore.STORE_ACCOUNT],
(txn) => {
store.storeBackupKey(txn, "m.megolm_backup.v1", testKey);
},
);
const result = await new Promise((resolve) => {
store.doTxn(
'readonly',
[IndexedDBCryptoStore.STORE_ACCOUNT],
(txn) => {
store.getBackupKey(txn, resolve, "m.megolm_backup.v1");
},
);
});
expect(result).toEqual(testKey);
});
});

View File

@@ -585,7 +585,7 @@ export function createCryptoStoreCacheCallbacks(store) {
'readonly',
[IndexedDBCryptoStore.STORE_ACCOUNT],
(txn) => {
store.getCrossSigningPrivateKey(txn, resolve, type);
store.getSecretStorePrivateKey(txn, resolve, type);
},
);
});
@@ -595,7 +595,7 @@ export function createCryptoStoreCacheCallbacks(store) {
'readwrite',
[IndexedDBCryptoStore.STORE_ACCOUNT],
(txn) => {
store.storeCrossSigningPrivateKey(txn, type, key);
store.storeSecretStorePrivateKey(txn, type, key);
},
);
},

View File

@@ -341,7 +341,7 @@ export class Backend {
};
}
getCrossSigningPrivateKey(txn, func, type) {
getSecretStorePrivateKey(txn, func, type) {
const objectStore = txn.objectStore("account");
const getReq = objectStore.get(`ssss_cache:${type}`);
getReq.onsuccess = function() {
@@ -358,28 +358,11 @@ export class Backend {
objectStore.put(keys, "crossSigningKeys");
}
storeCrossSigningPrivateKey(txn, type, key) {
storeSecretStorePrivateKey(txn, type, key) {
const objectStore = txn.objectStore("account");
objectStore.put(key, `ssss_cache:${type}`);
}
getBackupKey(txn, func, type) {
const objectStore = txn.objectStore("account");
const getReq = objectStore.get(`backup_key_cache:${type}`);
getReq.onsuccess = function() {
try {
func(getReq.result || null);
} catch (e) {
abortWithException(txn, e);
}
};
}
storeBackupKey(txn, type, key) {
const objectStore = txn.objectStore("account");
objectStore.put(key, `backup_key_cache:${type}`);
}
// Olm Sessions
countEndToEndSessions(txn, func) {

View File

@@ -313,8 +313,8 @@ export class IndexedDBCryptoStore {
* @param {function(string)} func Called with the private key
* @param {string} type A key type
*/
getCrossSigningPrivateKey(txn, func, type) {
this._backend.getCrossSigningPrivateKey(txn, func, type);
getSecretStorePrivateKey(txn, func, type) {
this._backend.getSecretStorePrivateKey(txn, func, type);
}
/**
@@ -334,28 +334,8 @@ export class IndexedDBCryptoStore {
* @param {string} type The type of cross-signing private key to store
* @param {string} key keys object as getCrossSigningKeys()
*/
storeCrossSigningPrivateKey(txn, type, key) {
this._backend.storeCrossSigningPrivateKey(txn, type, key);
}
/**
* Retrieve session backup keys from the store
* @param {*} txn An active transaction. See doTxn().
* @param {function(Uint8Array)} func Called with the private key
* @param {string} type A key type
*/
getBackupKey(txn, func, type) {
this._backend.getBackupKey(txn, func, type);
}
/**
*
* @param {*} txn
* @param {string} type A key type
* @param {Uint8Array} key A private key
*/
storeBackupKey(txn, type, key) {
this._backend.storeBackupKey(txn, type, key);
storeSecretStorePrivateKey(txn, type, key) {
this._backend.storeSecretStorePrivateKey(txn, type, key);
}
// Olm sessions

View File

@@ -367,7 +367,7 @@ export class LocalStorageCryptoStore extends MemoryCryptoStore {
func(keys);
}
getCrossSigningPrivateKey(txn, func, type) {
getSecretStorePrivateKey(txn, func, type) {
const key = getJsonItem(this.store, E2E_PREFIX + `ssss_cache.${type}`);
func(key ? Uint8Array.from(key) : key);
}
@@ -378,23 +378,12 @@ export class LocalStorageCryptoStore extends MemoryCryptoStore {
);
}
storeCrossSigningPrivateKey(txn, type, key) {
storeSecretStorePrivateKey(txn, type, key) {
setJsonItem(
this.store, E2E_PREFIX + `ssss_cache.${type}`, Array.from(key),
);
}
getBackupKey(txn, func, type) {
const key = getJsonItem(this.store, `backup_key_cache.${type}`);
func(key ? Uint8Array.from(key) : key);
}
storeBackupKey(txn, type, key) {
setJsonItem(
this.store, `backup_key_cache.${type}`, Array.from(key),
);
}
doTxn(mode, stores, func) {
return Promise.resolve(func(null));
}

View File

@@ -257,7 +257,7 @@ export class MemoryCryptoStore {
func(this._crossSigningKeys);
}
getCrossSigningPrivateKey(txn, func, type) {
getSecretStorePrivateKey(txn, func, type) {
const result = this._privateKeys[type];
return func(result || null);
}
@@ -266,19 +266,10 @@ export class MemoryCryptoStore {
this._crossSigningKeys = keys;
}
storeCrossSigningPrivateKey(txn, type, key) {
storeSecretStorePrivateKey(txn, type, key) {
this._privateKeys[type] = key;
}
getBackupKey(txn, func, type) {
const result = this._backupKeys[type];
return func(result || null);
}
storeBackupKey(txn, type, key) {
this._backupKeys[type] = key;
}
// Olm Sessions
countEndToEndSessions(txn, func) {