mirror of
https://codeberg.org/keyoxide/keyoxide-web.git
synced 2024-12-22 23:09:29 -07:00
Add experimental caching of keys
This commit is contained in:
parent
ac2a103b9d
commit
271f2d0bee
4 changed files with 64 additions and 21 deletions
|
@ -8,6 +8,7 @@
|
||||||
"ajv": "^8.6.3",
|
"ajv": "^8.6.3",
|
||||||
"bent": "^7.3.12",
|
"bent": "^7.3.12",
|
||||||
"body-parser": "^1.19.0",
|
"body-parser": "^1.19.0",
|
||||||
|
"cache": "^3.0.0",
|
||||||
"dialog-polyfill": "^0.5.6",
|
"dialog-polyfill": "^0.5.6",
|
||||||
"doipjs": "^0.16.3",
|
"doipjs": "^0.16.3",
|
||||||
"dotenv": "^8.2.0",
|
"dotenv": "^8.2.0",
|
||||||
|
|
|
@ -31,6 +31,10 @@ import got from 'got'
|
||||||
import * as doipjs from 'doipjs'
|
import * as doipjs from 'doipjs'
|
||||||
import { readKey, readCleartextMessage, verify } from 'openpgp'
|
import { readKey, readCleartextMessage, verify } from 'openpgp'
|
||||||
import { computeWKDLocalPart } from './utils.js'
|
import { computeWKDLocalPart } from './utils.js'
|
||||||
|
import { createHash } from 'crypto'
|
||||||
|
import cache from 'cache'
|
||||||
|
|
||||||
|
let c = process.env.ENABLE_EXPERIMENTAL_CACHE ? new cache(60 * 1000) : null
|
||||||
|
|
||||||
const fetchWKD = (id) => {
|
const fetchWKD = (id) => {
|
||||||
return new Promise(async (resolve, reject) => {
|
return new Promise(async (resolve, reject) => {
|
||||||
|
@ -52,32 +56,44 @@ const fetchWKD = (id) => {
|
||||||
const urlDirect = `https://${domain}/.well-known/openpgpkey/hu/${localEncoded}`
|
const urlDirect = `https://${domain}/.well-known/openpgpkey/hu/${localEncoded}`
|
||||||
let plaintext
|
let plaintext
|
||||||
|
|
||||||
try {
|
const hash = createHash('md5').update(id).digest('hex')
|
||||||
plaintext = await got(urlAdvanced).then((response) => {
|
|
||||||
if (response.statusCode === 200) {
|
if (c && c.get(hash)) {
|
||||||
output.fetchURL = urlAdvanced
|
plaintext = c.get(hash)
|
||||||
return new Uint8Array(response.rawBody)
|
}
|
||||||
} else {
|
|
||||||
return null
|
if (!plaintext) {
|
||||||
}
|
|
||||||
})
|
|
||||||
} catch (e) {
|
|
||||||
try {
|
try {
|
||||||
plaintext = await got(urlDirect).then((response) => {
|
plaintext = await got(urlAdvanced).then((response) => {
|
||||||
if (response.statusCode === 200) {
|
if (response.statusCode === 200) {
|
||||||
output.fetchURL = urlDirect
|
output.fetchURL = urlAdvanced
|
||||||
return new Uint8Array(response.rawBody)
|
return new Uint8Array(response.rawBody)
|
||||||
} else {
|
} else {
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
} catch (error) {
|
} catch (e) {
|
||||||
|
try {
|
||||||
|
plaintext = await got(urlDirect).then((response) => {
|
||||||
|
if (response.statusCode === 200) {
|
||||||
|
output.fetchURL = urlDirect
|
||||||
|
return new Uint8Array(response.rawBody)
|
||||||
|
} else {
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
})
|
||||||
|
} catch (error) {
|
||||||
|
reject(new Error(`No public keys could be fetched using WKD`))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!plaintext) {
|
||||||
reject(new Error(`No public keys could be fetched using WKD`))
|
reject(new Error(`No public keys could be fetched using WKD`))
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (!plaintext) {
|
if (c) {
|
||||||
reject(new Error(`No public keys could be fetched using WKD`))
|
c.put(hash, plaintext)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
@ -112,17 +128,27 @@ const fetchHKP = (id, keyserverDomain) => {
|
||||||
query = `0x${id}`
|
query = `0x${id}`
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
const hash = createHash('md5').update(`${id}__${keyserverDomain}`).digest('hex')
|
||||||
output.publicKey = await doipjs.keys.fetchHKP(id, keyserverDomain)
|
|
||||||
output.fetchURL = `https://${keyserverDomain}/pks/lookup?op=get&options=mr&search=${query}`
|
if (c && c.get(hash)) {
|
||||||
} catch(error) {
|
output = c.get(hash)
|
||||||
reject(new Error(`No public keys could be fetched using HKP`))
|
} else {
|
||||||
|
try {
|
||||||
|
output.publicKey = await doipjs.keys.fetchHKP(id, keyserverDomain)
|
||||||
|
output.fetchURL = `https://${keyserverDomain}/pks/lookup?op=get&options=mr&search=${query}`
|
||||||
|
} catch(error) {
|
||||||
|
reject(new Error(`No public keys could be fetched using HKP`))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!output.publicKey) {
|
if (!output.publicKey) {
|
||||||
reject(new Error(`No public keys could be fetched using HKP`))
|
reject(new Error(`No public keys could be fetched using HKP`))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (c) {
|
||||||
|
c.put(hash, output)
|
||||||
|
}
|
||||||
|
|
||||||
resolve(output)
|
resolve(output)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -33,3 +33,7 @@
|
||||||
#KX_HIGHLIGHTS_3_NAME=
|
#KX_HIGHLIGHTS_3_NAME=
|
||||||
#KX_HIGHLIGHTS_3_DESCRIPTION=
|
#KX_HIGHLIGHTS_3_DESCRIPTION=
|
||||||
#KX_HIGHLIGHTS_3_FINGERPRINT=
|
#KX_HIGHLIGHTS_3_FINGERPRINT=
|
||||||
|
|
||||||
|
# Enable caching of keys (experimental)
|
||||||
|
# Opt-in; to disable, omit the environment variable
|
||||||
|
#ENABLE_EXPERIMENTAL_CACHE=
|
||||||
|
|
12
yarn.lock
12
yarn.lock
|
@ -1272,6 +1272,13 @@ bytesish@^0.4.1:
|
||||||
resolved "https://registry.npmjs.org/bytesish/-/bytesish-0.4.4.tgz"
|
resolved "https://registry.npmjs.org/bytesish/-/bytesish-0.4.4.tgz"
|
||||||
integrity sha512-i4uu6M4zuMUiyfZN4RU2+i9+peJh//pXhd9x1oSe1LBkZ3LEbCoygu8W0bXTukU1Jme2txKuotpCZRaC3FLxcQ==
|
integrity sha512-i4uu6M4zuMUiyfZN4RU2+i9+peJh//pXhd9x1oSe1LBkZ3LEbCoygu8W0bXTukU1Jme2txKuotpCZRaC3FLxcQ==
|
||||||
|
|
||||||
|
cache@^3.0.0:
|
||||||
|
version "3.0.0"
|
||||||
|
resolved "https://registry.yarnpkg.com/cache/-/cache-3.0.0.tgz#1c5857e874f7064be641114a605c7e2ae8a80880"
|
||||||
|
integrity sha512-sNoM5jithfalxIceo/uFFm5bOlGjux2y8jEvjNb0F/cACWQaMmWuEPTLl6GzLHdFcNsbWBBdqkBd9NyefZ5UQQ==
|
||||||
|
dependencies:
|
||||||
|
ds "^1.4.2"
|
||||||
|
|
||||||
cacheable-lookup@^5.0.3:
|
cacheable-lookup@^5.0.3:
|
||||||
version "5.0.4"
|
version "5.0.4"
|
||||||
resolved "https://registry.npmjs.org/cacheable-lookup/-/cacheable-lookup-5.0.4.tgz"
|
resolved "https://registry.npmjs.org/cacheable-lookup/-/cacheable-lookup-5.0.4.tgz"
|
||||||
|
@ -1816,6 +1823,11 @@ dotenv@^8.2.0:
|
||||||
resolved "https://registry.npmjs.org/dotenv/-/dotenv-8.2.0.tgz"
|
resolved "https://registry.npmjs.org/dotenv/-/dotenv-8.2.0.tgz"
|
||||||
integrity sha512-8sJ78ElpbDJBHNeBzUbUVLsqKdccaa/BXF1uPTw3GrvQTBgrQrtObr2mUrE38vzYd8cEv+m/JBfDLioYcfXoaw==
|
integrity sha512-8sJ78ElpbDJBHNeBzUbUVLsqKdccaa/BXF1uPTw3GrvQTBgrQrtObr2mUrE38vzYd8cEv+m/JBfDLioYcfXoaw==
|
||||||
|
|
||||||
|
ds@^1.4.2:
|
||||||
|
version "1.4.2"
|
||||||
|
resolved "https://registry.yarnpkg.com/ds/-/ds-1.4.2.tgz#0857aa213790a4fb3abb365b9cec0e9ba8569393"
|
||||||
|
integrity sha512-d5nMCjfod+srvE/1Bnt/u+L++6N8KJx3ZAi95AGp0g6RtfuGDNlGciWL/iiwKHsFVBVnA3/HEFUq5SW1NgTQ3Q==
|
||||||
|
|
||||||
duplexer@^0.1.2:
|
duplexer@^0.1.2:
|
||||||
version "0.1.2"
|
version "0.1.2"
|
||||||
resolved "https://registry.yarnpkg.com/duplexer/-/duplexer-0.1.2.tgz#3abe43aef3835f8ae077d136ddce0f276b0400e6"
|
resolved "https://registry.yarnpkg.com/duplexer/-/duplexer-0.1.2.tgz#3abe43aef3835f8ae077d136ddce0f276b0400e6"
|
||||||
|
|
Loading…
Reference in a new issue