use openConfig instead of useConfig

This commit is contained in:
Riccardo Balbo 2024-10-16 17:11:45 +02:00 committed by k00b
parent eeef7039b9
commit 86994c4c46
1 changed files with 58 additions and 39 deletions

View File

@ -4,7 +4,7 @@ import { useMutation, useApolloClient } from '@apollo/client'
import { SET_ENTRY, UNSET_ENTRY, GET_ENTRY, CLEAR_VAULT, SET_VAULT_KEY_HASH } from '@/fragments/vault' import { SET_ENTRY, UNSET_ENTRY, GET_ENTRY, CLEAR_VAULT, SET_VAULT_KEY_HASH } from '@/fragments/vault'
import { E_VAULT_KEY_EXISTS } from '@/lib/error' import { E_VAULT_KEY_EXISTS } from '@/lib/error'
import { useToast } from '@/components/toast' import { useToast } from '@/components/toast'
import useLocalStorage, { openLocalStorage, listLocalStorages } from '@/components/use-local-storage' import { openLocalStorage, listLocalStorages } from '@/components/use-local-storage'
import { toHex, fromHex } from '@/lib/hex' import { toHex, fromHex } from '@/lib/hex'
import createTaskQueue from '@/lib/task-queue' import createTaskQueue from '@/lib/task-queue'
@ -17,61 +17,84 @@ export function useVaultConfigurator () {
const [setVaultKeyHash] = useMutation(SET_VAULT_KEY_HASH) const [setVaultKeyHash] = useMutation(SET_VAULT_KEY_HASH)
const [vaultKey, innerSetVaultKey] = useState(null) const [vaultKey, innerSetVaultKey] = useState(null)
const [config, configError] = useConfig()
const [vaultKeyHash, setVaultKeyHashLocal] = useState(null) const [vaultKeyHash, setVaultKeyHashLocal] = useState(null)
useEffect(() => { useEffect(() => {
if (!me) return if (!me) return
if (configError) {
toaster.danger('error loading vault configuration ' + configError.message)
return
}
(async () => { (async () => {
let localVaultKey = await config.get('key') const config = await openConfig(me.id)
const keyHash = me?.privates?.vaultKeyHash || vaultKeyHash try {
if ((!keyHash && localVaultKey?.hash) || (localVaultKey?.hash !== keyHash)) { let localVaultKey = await config.get('key')
// If the hash stored in the server does not match the hash of the local key, const keyHash = me?.privates?.vaultKeyHash || vaultKeyHash
// we can tell that the key is outdated (reset by another device or other reasons) if ((!keyHash && localVaultKey?.hash) || (localVaultKey?.hash && keyHash && localVaultKey?.hash !== keyHash)) {
// in this case we clear the local key and let the user re-enter the passphrase // If the hash stored in the server does not match the hash of the local key,
console.log('vault key hash mismatch, clearing local key', localVaultKey?.hash, '!=', keyHash) // we can tell that the key is outdated (reset by another device or other reasons)
localVaultKey = null // in this case we clear the local key and let the user re-enter the passphrase
await config.unset('key') console.log('vault key hash mismatch, clearing local key', localVaultKey?.hash, '!=', keyHash)
localVaultKey = null
await config.unset('key')
}
innerSetVaultKey(localVaultKey)
} catch (e) {
toaster.danger('error loading vault configuration ' + e.message)
} finally {
await config.close()
} }
innerSetVaultKey(localVaultKey)
})() })()
}, [me?.privates?.vaultKeyHash, config, configError]) }, [me?.privates?.vaultKeyHash])
// clear vault: remove everything and reset the key // clear vault: remove everything and reset the key
const [clearVault] = useMutation(CLEAR_VAULT, { const [clearVault] = useMutation(CLEAR_VAULT, {
onCompleted: async () => { onCompleted: async () => {
await config.unset('key') const config = await openConfig(me.id)
innerSetVaultKey(null) try {
await config.unset('key')
innerSetVaultKey(null)
} catch (e) {
toaster.danger('error clearing vault ' + e.message)
} finally {
await config.close()
}
} }
}) })
// initialize the vault and set a vault key // initialize the vault and set a vault key
const setVaultKey = useCallback(async (passphrase) => { const setVaultKey = useCallback(async (passphrase) => {
const vaultKey = await deriveKey(me.id, passphrase) const config = await openConfig(me.id)
await setVaultKeyHash({ try {
variables: { hash: vaultKey.hash }, const vaultKey = await deriveKey(me.id, passphrase)
onError: (error) => { await setVaultKeyHash({
const errorCode = error.graphQLErrors[0]?.extensions?.code variables: { hash: vaultKey.hash },
if (errorCode === E_VAULT_KEY_EXISTS) { onError: (error) => {
throw new Error('wrong passphrase') const errorCode = error.graphQLErrors[0]?.extensions?.code
if (errorCode === E_VAULT_KEY_EXISTS) {
throw new Error('wrong passphrase')
}
toaster.danger(error.graphQLErrors[0].message)
} }
toaster.danger(error.graphQLErrors[0].message) })
} innerSetVaultKey(vaultKey)
}) setVaultKeyHashLocal(vaultKey.hash)
innerSetVaultKey(vaultKey) await config.set('key', vaultKey)
setVaultKeyHashLocal(vaultKey.hash) } catch (e) {
await config.set('key', vaultKey) toaster.danger('error setting vault key ' + e.message)
} finally {
await config.close()
}
}, [setVaultKeyHash]) }, [setVaultKeyHash])
// disconnect the user from the vault (will not clear or reset the passphrase, use clearVault for that) // disconnect the user from the vault (will not clear or reset the passphrase, use clearVault for that)
const disconnectVault = useCallback(async () => { const disconnectVault = useCallback(async () => {
await config.unset('key') const config = await openConfig(me.id)
innerSetVaultKey(null) try {
}, [innerSetVaultKey, config]) await config.unset('key')
innerSetVaultKey(null)
} catch (e) {
toaster.danger('error disconnecting vault ' + e.message)
} finally {
await config.close()
}
}, [innerSetVaultKey])
return [vaultKey, setVaultKey, clearVault, disconnectVault] return [vaultKey, setVaultKey, clearVault, disconnectVault]
} }
@ -332,10 +355,6 @@ export function openVault (apollo, user, owner) {
return { get: getValue, set: setValue, clear: clearValue, close } return { get: getValue, set: setValue, clear: clearValue, close }
} }
function useConfig () {
return useLocalStorage({ database: 'vault-config', namespace: ['settings'], supportLegacy: false })
}
async function openConfig (userId) { async function openConfig (userId) {
return await openLocalStorage({ userId, database: 'vault-config', namespace: ['settings'] }) return await openLocalStorage({ userId, database: 'vault-config', namespace: ['settings'] })
} }