import config from 'config' import addrparser from 'address-rfc2822' import debug from 'debug' var log = debug('webfinger') import { getUserByName } from '../db/Users' const baseUrl = config.get('service.baseURL') /** * WebFinger endpoint * @type {import('express').RequestParamHandler} */ export const webfinger = async (req, res) => { const resource = req.query.resource if (resource) { const acct = resource.substring(5) // drop "acct:" const addresses = parseAddress(acct) if (addresses && addresses.length == 1) { const address = addresses[0] const ourAddress = new URL(baseUrl) if (address.host() === ourAddress.hostname) { const name = addresses[0].user() const user = await getUserByName(name) if (user) { return res.json({ subject: resource, links: [ { rel: 'self', type: 'application/activity+json', href: `${baseUrl}/u/${user.nick}` } ] }) } else { log(`User not found: ${name}`) return res.status(404).end() } } else { log(`Address not found: ${address.host()}`) return res.status(404).end() } } else { log(`Invalid resource: ${resource}`) return res.status(400).end() } } log('Missing `resource` param') res.status(400) } const parseAddress = (address = '') => { try { return addrparser.parse(address, { startAt: 'address' }) } catch { return undefined } }