aboutsummaryrefslogtreecommitdiff
path: root/vnext/server/middleware
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2024-10-21 05:53:23 +0300
committerGravatar Vitaly Takmazov2024-10-21 05:53:23 +0300
commit6f7c755dca704ccf880538b7884473c9c829c2b7 (patch)
treeb3691287d72d6d0d7117b771e46944d778c93a21 /vnext/server/middleware
parent1eac455507b75c0795c169d92e1ce9bb36733209 (diff)
vnext: `webfinger` middleware
Diffstat (limited to 'vnext/server/middleware')
-rw-r--r--vnext/server/middleware/webfinger.js52
1 files changed, 52 insertions, 0 deletions
diff --git a/vnext/server/middleware/webfinger.js b/vnext/server/middleware/webfinger.js
new file mode 100644
index 00000000..9800fc01
--- /dev/null
+++ b/vnext/server/middleware/webfinger.js
@@ -0,0 +1,52 @@
+import config from 'config'
+import addrparser from 'address-rfc2822'
+
+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 user = await getUserByName(addresses[0].user())
+ if (user) {
+ return res.json({
+ subject: resource,
+ links: [
+ {
+ rel: 'self',
+ type: 'application/activity+json',
+ href: `${baseUrl}/u/${user.nick}`
+ }
+ ]
+ })
+ } else {
+ return res.status(404).send('User not found')
+ }
+ } else {
+ return res.status(404).send('Address not found')
+ }
+ } else {
+ return res.status(400).send('Invalid resource')
+ }
+ }
+ res.status(400).send('Missing `resource` param')
+}
+
+const parseAddress = (address = '') => {
+ try {
+ return addrparser.parse(address, { startAt: 'address' })
+ } catch {
+ return undefined
+ }
+}