gancio-upstream/server/federation/webfinger.js

159 lines
4.8 KiB
JavaScript
Raw Normal View History

2019-07-29 01:27:47 +02:00
const express = require('express')
const router = express.Router()
2023-01-10 18:16:11 +01:00
const { Event, Resource, User } = require('../api/models/models')
2020-06-27 02:10:10 +02:00
2019-08-02 13:43:28 +02:00
const cors = require('cors')
const settingsController = require('../api/controller/settings')
const version = require('../../package.json').version
const url = require('url')
2021-03-05 14:17:10 +01:00
const log = require('../log')
2019-07-29 01:27:47 +02:00
2019-08-02 13:43:28 +02:00
router.use(cors())
2022-02-26 21:27:40 +01:00
function allowFederation (req, res, next) {
// is federation enabled ?
2022-02-26 21:27:40 +01:00
if (settingsController.settings.enable_federation) {
2021-03-05 14:17:10 +01:00
return next()
}
log.debug('Federation disabled')
res.status(404).send('Federation disabled')
}
router.get('/webfinger', allowFederation, (req, res) => {
2022-02-26 21:27:40 +01:00
const settings = settingsController.settings
2019-09-19 23:47:01 +02:00
if (!req.query || !req.query.resource || !req.query.resource.includes('acct:')) {
2021-03-05 14:17:10 +01:00
log.debug('Bad webfinger request => ', req.query && req.query.resource)
2019-07-29 01:27:47 +02:00
return res.status(400).send('Bad request. Please make sure "acct:USER@DOMAIN" is what you are sending as the "resource" query parameter.')
}
2019-09-20 00:01:15 +02:00
const resource = req.query.resource
2022-05-20 13:03:46 +02:00
const domain = (new url.URL(res.locals.settings.baseurl)).host
2019-09-11 19:12:24 +02:00
const [, name, req_domain] = resource.match(/acct:(.*)@(.*)/)
if (domain !== req_domain) {
2021-03-05 14:17:10 +01:00
log.warn(`Bad webfinger request, requested domain "${req_domain}" instead of "${domain}"`)
2019-10-28 17:33:20 +01:00
return res.status(400).send('Bad request. Please make sure "acct:USER@DOMAIN" is what you are sending as the "resource" query parameter.')
2019-09-11 19:12:24 +02:00
}
2022-02-26 21:27:40 +01:00
if (name !== settings.instance_name) {
2021-03-05 14:17:10 +01:00
log.warn(`User not found: ${name}`)
2019-09-11 19:12:24 +02:00
return res.status(404).send(`No record found for ${name}`)
}
2021-03-05 14:17:10 +01:00
log.info(`webfinger ${resource} ${domain}`)
2019-07-29 01:27:47 +02:00
const ret = {
2019-07-30 00:57:45 +02:00
subject: `acct:${name}@${domain}`,
2019-07-29 01:27:47 +02:00
links: [
{
rel: 'self',
type: 'application/activity+json',
2022-02-26 21:27:40 +01:00
href: `${settings.baseurl}/federation/u/${name}`
2019-07-29 01:27:47 +02:00
}
]
}
2023-12-22 21:23:36 +01:00
res.set('Content-Type', 'application/json; charset=utf-8')
2019-07-29 01:27:47 +02:00
res.json(ret)
})
2019-08-02 13:43:28 +02:00
router.get('/nodeinfo/:nodeinfo_version', async (req, res) => {
2022-02-26 21:27:40 +01:00
const settings = settingsController.settings
2019-10-22 00:58:38 +02:00
const usersCount = (await User.findAndCountAll()).count
const eventsCount = (await Event.findAndCountAll()).count
2019-12-04 11:58:47 +01:00
const resourcesCount = (await Resource.findAndCountAll()).count
2019-08-02 13:43:28 +02:00
const ret = {
metadata: {
2022-02-26 21:27:40 +01:00
nodeDescription: settings.description,
nodeName: settings.title,
2023-02-03 15:12:12 +01:00
nodeTimezone: settings.instance_timezone,
nodeActor: settings.instance_name
2019-08-02 13:43:28 +02:00
},
2022-02-26 21:27:40 +01:00
openRegistrations: settings.allow_registration,
2019-09-11 19:12:24 +02:00
protocols: ['activitypub'],
services: { inbound: [], outbound: ['rss2.0'] },
2019-08-02 13:43:28 +02:00
software: {
name: 'gancio',
version
},
2019-11-09 15:02:16 +01:00
version: req.params.nodeinfo_version,
2019-09-11 19:12:24 +02:00
usage: {
2019-12-04 11:58:47 +01:00
localComments: resourcesCount,
2019-10-22 00:58:38 +02:00
localPosts: eventsCount,
2019-08-02 13:43:28 +02:00
users: {
2019-10-22 00:58:38 +02:00
total: usersCount
2019-08-02 13:43:28 +02:00
}
2019-11-09 15:02:16 +01:00
}
2019-08-02 13:43:28 +02:00
}
2019-09-11 19:12:24 +02:00
if (req.params.nodeinfo_version === '2.1') {
2019-10-20 14:01:38 +02:00
ret.software.repository = 'https://framagit.org/les/gancio'
2019-08-02 13:43:28 +02:00
}
res.json(ret)
})
router.get('/x-nodeinfo2', async (req, res) => {
2022-02-26 21:27:40 +01:00
const settings = settingsController.settings
2019-10-22 00:58:38 +02:00
const usersCount = (await User.findAndCountAll()).count
const eventsCount = (await Event.findAndCountAll()).count
2019-12-04 11:58:47 +01:00
const resourcesCount = (await Resource.findAndCountAll()).count
2019-08-02 13:43:28 +02:00
const ret = {
version: '1.0',
server: {
2022-02-26 21:27:40 +01:00
baseUrl: settings.baseurl,
name: settings.title,
2019-08-02 13:43:28 +02:00
software: 'Gancio',
version
},
protocols: ['activitypub'],
openRegistrations: settingsController.settings.allow_registration,
2019-09-11 19:12:24 +02:00
usage: {
2019-08-02 13:43:28 +02:00
users: {
2019-10-22 00:58:38 +02:00
total: usersCount
},
2019-11-09 15:02:16 +01:00
localPosts: eventsCount,
2019-12-04 11:58:47 +01:00
localComments: resourcesCount
}
2019-08-02 13:43:28 +02:00
}
res.json(ret)
})
router.get('/nodeinfo', (req, res) => {
const settings = res.locals.settings
2019-08-02 13:43:28 +02:00
const ret = {
links: [
2022-02-26 21:27:40 +01:00
{ href: `${settings.baseurl}/.well-known/nodeinfo/2.0`, rel: 'http://nodeinfo.diaspora.software/ns/schema/2.0' },
{ href: `${settings.baseurl}/.well-known/nodeinfo/2.1`, rel: 'http://nodeinfo.diaspora.software/ns/schema/2.1' },
... settings.enable_federation ? [{
"rel": "https://www.w3.org/ns/activitystreams#Application",
"href": `${settings.baseurl}/federation/u/${settings?.instance_name ?? 'relay' }`
}] : []
2019-08-02 13:43:28 +02:00
]
}
res.json(ret)
})
2019-08-25 14:34:26 +02:00
router.use('/host-meta', (req, res) => {
2022-02-26 21:27:40 +01:00
const settings = settingsController.settings
2021-03-05 14:17:10 +01:00
log.debug('host-meta')
res.type('application/xrd+xml')
2019-08-25 14:34:26 +02:00
res.send(`<?xml version="1.0" encoding="UTF-8"?>
<XRD xmlns="http://docs.oasis-open.org/ns/xri/xrd-1.0">
2022-02-26 21:27:40 +01:00
<Link rel="lrdd" type="application/xrd+xml" template="${settings.baseurl}/.well-known/webfinger?resource={uri}"/>
2019-08-25 14:34:26 +02:00
</XRD>`)
})
2019-08-02 13:43:28 +02:00
// Handle 404
2019-09-11 19:12:24 +02:00
router.use((req, res) => {
2021-03-05 14:17:10 +01:00
log.error('404 Page not found: ', req.path)
2019-08-09 14:37:51 +02:00
res.status(404).send('404: Page not Found')
2019-08-02 13:43:28 +02:00
})
// Handle 500
2019-09-11 19:12:24 +02:00
router.use((error, req, res, next) => {
2021-07-08 20:41:56 +02:00
log.error('[WEBFINGER]', error)
2019-08-09 14:37:51 +02:00
res.status(500).send('500: Internal Server Error')
2019-08-02 13:43:28 +02:00
})
2019-07-29 01:27:47 +02:00
module.exports = router