2024-10-14 15:49:06 +00:00
|
|
|
import { E_VAULT_KEY_EXISTS, GqlAuthenticationError, GqlInputError } from '@/lib/error'
|
|
|
|
|
|
|
|
export default {
|
|
|
|
Query: {
|
2024-10-23 00:53:56 +00:00
|
|
|
getVaultEntry: async (parent, { key }, { me, models }, info) => {
|
2024-10-14 15:49:06 +00:00
|
|
|
if (!me) throw new GqlAuthenticationError()
|
|
|
|
if (!key) throw new GqlInputError('must have key')
|
|
|
|
|
|
|
|
const k = await models.vault.findUnique({
|
|
|
|
where: {
|
2024-10-27 07:43:45 +00:00
|
|
|
key,
|
|
|
|
userId: me.id
|
2024-10-14 15:49:06 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
return k
|
|
|
|
},
|
2024-10-23 00:53:56 +00:00
|
|
|
getVaultEntries: async (parent, { keysFilter }, { me, models }, info) => {
|
2024-10-14 15:49:06 +00:00
|
|
|
if (!me) throw new GqlAuthenticationError()
|
|
|
|
|
2024-10-27 07:43:45 +00:00
|
|
|
const entries = await models.vaultEntry.findMany({
|
2024-10-14 15:49:06 +00:00
|
|
|
where: {
|
|
|
|
userId: me.id,
|
|
|
|
key: keysFilter?.length
|
|
|
|
? {
|
|
|
|
in: keysFilter
|
|
|
|
}
|
|
|
|
: undefined
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return entries
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Mutation: {
|
2024-10-23 00:53:56 +00:00
|
|
|
// atomic vault migration
|
|
|
|
updateVaultKey: async (parent, { entries, hash }, { me, models }) => {
|
2024-10-14 15:49:06 +00:00
|
|
|
if (!me) throw new GqlAuthenticationError()
|
|
|
|
if (!hash) throw new GqlInputError('hash required')
|
2024-10-23 00:53:56 +00:00
|
|
|
const txs = []
|
2024-10-14 15:49:06 +00:00
|
|
|
|
|
|
|
const { vaultKeyHash: oldKeyHash } = await models.user.findUnique({ where: { id: me.id } })
|
|
|
|
if (oldKeyHash) {
|
|
|
|
if (oldKeyHash !== hash) {
|
|
|
|
throw new GqlInputError('vault key already set', E_VAULT_KEY_EXISTS)
|
|
|
|
} else {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
} else {
|
2024-10-23 00:53:56 +00:00
|
|
|
txs.push(models.user.update({
|
2024-10-14 15:49:06 +00:00
|
|
|
where: { id: me.id },
|
|
|
|
data: { vaultKeyHash: hash }
|
2024-10-23 00:53:56 +00:00
|
|
|
}))
|
2024-10-14 15:49:06 +00:00
|
|
|
}
|
2024-10-23 00:53:56 +00:00
|
|
|
|
|
|
|
for (const entry of entries) {
|
2024-10-23 17:42:34 +00:00
|
|
|
txs.push(models.vaultEntry.update({
|
2024-10-24 20:30:56 +00:00
|
|
|
where: { userId_key: { userId: me.id, key: entry.key } },
|
2024-10-27 07:43:45 +00:00
|
|
|
data: { value: entry.value, iv: entry.iv }
|
2024-10-23 00:53:56 +00:00
|
|
|
}))
|
|
|
|
}
|
2024-10-27 07:43:45 +00:00
|
|
|
await models.$transaction(txs)
|
2024-10-23 00:53:56 +00:00
|
|
|
return true
|
|
|
|
},
|
|
|
|
clearVault: async (parent, args, { me, models }) => {
|
|
|
|
if (!me) throw new GqlAuthenticationError()
|
|
|
|
const txs = []
|
|
|
|
txs.push(models.user.update({
|
|
|
|
where: { id: me.id },
|
|
|
|
data: { vaultKeyHash: '' }
|
|
|
|
}))
|
|
|
|
txs.push(models.vaultEntry.deleteMany({ where: { userId: me.id } }))
|
2024-10-27 07:43:45 +00:00
|
|
|
await models.$transaction(txs)
|
2024-10-14 15:49:06 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|