mirror of https://github.com/vector-im/riot-web
Re-add the secret storage key cache
parent
c2cd97fab3
commit
c5099b9b96
|
@ -20,12 +20,25 @@ import MatrixClientPeg from './MatrixClientPeg';
|
||||||
import { deriveKey } from 'matrix-js-sdk/lib/crypto/key_passphrase';
|
import { deriveKey } from 'matrix-js-sdk/lib/crypto/key_passphrase';
|
||||||
import { decodeRecoveryKey } from 'matrix-js-sdk/lib/crypto/recoverykey';
|
import { decodeRecoveryKey } from 'matrix-js-sdk/lib/crypto/recoverykey';
|
||||||
|
|
||||||
|
// This stores the secret storage private keys in memory for the JS SDK. This is
|
||||||
|
// only meant to act as a cache to avoid prompting the user multiple times
|
||||||
|
// during the same session. It is considered unsafe to persist this to normal
|
||||||
|
// web storage. For platforms with a secure enclave, we will store this key
|
||||||
|
// there.
|
||||||
|
const secretStorageKeys = {};
|
||||||
|
|
||||||
export const getSecretStorageKey = async ({ keys: keyInfos }) => {
|
export const getSecretStorageKey = async ({ keys: keyInfos }) => {
|
||||||
const keyInfoEntries = Object.entries(keyInfos);
|
const keyInfoEntries = Object.entries(keyInfos);
|
||||||
if (keyInfoEntries.length > 1) {
|
if (keyInfoEntries.length > 1) {
|
||||||
throw new Error("Multiple storage key requests not implemented");
|
throw new Error("Multiple storage key requests not implemented");
|
||||||
}
|
}
|
||||||
const [name, info] = keyInfoEntries[0];
|
const [name, info] = keyInfoEntries[0];
|
||||||
|
|
||||||
|
// Check the in-memory cache
|
||||||
|
if (secretStorageKeys[name]) {
|
||||||
|
return [name, secretStorageKeys[name]];
|
||||||
|
}
|
||||||
|
|
||||||
const inputToKey = async ({ passphrase, recoveryKey }) => {
|
const inputToKey = async ({ passphrase, recoveryKey }) => {
|
||||||
if (passphrase) {
|
if (passphrase) {
|
||||||
return deriveKey(
|
return deriveKey(
|
||||||
|
@ -54,5 +67,9 @@ export const getSecretStorageKey = async ({ keys: keyInfos }) => {
|
||||||
throw new Error("Secret storage access canceled");
|
throw new Error("Secret storage access canceled");
|
||||||
}
|
}
|
||||||
const key = await inputToKey(input);
|
const key = await inputToKey(input);
|
||||||
|
|
||||||
|
// Save to cache to avoid future prompts in the current session
|
||||||
|
secretStorageKeys[name] = key;
|
||||||
|
|
||||||
return [name, key];
|
return [name, key];
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue