diff --git a/routes/profile.js b/routes/profile.js index 6a5d139..7794d0b 100644 --- a/routes/profile.js +++ b/routes/profile.js @@ -59,7 +59,11 @@ router.get('/hkp/:server/:id', async (req, res) => { }) router.get('/keybase/:username/:fingerprint', async (req, res) => { - res.render('profile', { mode: 'keybase', uid: `${req.params.username}/${req.params.fingerprint}` }) + const data = await kx.generateKeybaseProfile(req.params.username, req.params.fingerprint) + if (data.errors.length > 0) { + return res.render('profile-failed', { data: data }) + } + res.render('profile', { data: data }) }) router.get('/:id', async (req, res) => { @@ -69,6 +73,9 @@ router.get('/:id', async (req, res) => { } else { data = await kx.generateHKPProfile(req.params.id) } + if (data.errors.length > 0) { + return res.render('profile-failed', { data: data }) + } res.render('profile', { data: data }) }) diff --git a/server/index.js b/server/index.js index d4e2ad1..89966be 100644 --- a/server/index.js +++ b/server/index.js @@ -81,6 +81,31 @@ const generateHKPProfile = async (id, keyserverDomain) => { }) } +const generateKeybaseProfile = async (username, fingerprint) => { + return keys.fetchKeybase(id, keyserverDomain) + .then(async key => { + let keyData = await doip.keys.process(key.publicKey) + keyData.key.fetchMethod = 'hkp' + keyData.key.uri = key.fetchURL + keyData = processKeyData(keyData) + + return { + key: key, + keyData: keyData, + extra: await computeExtraData(key, keyData), + errors: [] + } + }) + .catch(err => { + return { + key: null, + keyData: null, + extra: null, + errors: [err.message] + } + }) +} + const processKeyData = (keyData) => { keyData.users.forEach(user => { // Match claims @@ -120,3 +145,4 @@ const computeExtraData = async (key, keyData) => { exports.generateWKDProfile = generateWKDProfile exports.generateHKPProfile = generateHKPProfile +exports.generateKeybaseProfile = generateKeybaseProfile diff --git a/server/keys.js b/server/keys.js index d5dac21..311d6ac 100644 --- a/server/keys.js +++ b/server/keys.js @@ -118,5 +118,28 @@ const fetchHKP = (id, keyserverDomain) => { }) } +const fetchKeybase = (username, fingerprint) => { + return new Promise(async (resolve, reject) => { + let output = { + publicKey: null, + fetchURL: null + } + + try { + output.publicKey = await doip.keys.fetchKeybase(username, fingerprint) + output.fetchURL = `https://keybase.io/${username}` + } catch(error) { + reject(new Error("No public keys could be fetched from Keybase")) + } + + if (!output.publicKey) { + reject(new Error("No public keys could be fetched from Keybase")) + } + + resolve(output) + }) +} + exports.fetchWKD = fetchWKD exports.fetchHKP = fetchHKP +exports.fetchKeybase = fetchKeybase