gancio-upstream/server/federation/webfinger.js

141 lines
4.3 KiB
JavaScript
Raw Normal View History

2019-07-29 01:27:47 +02:00
const express = require('express')
const router = express.Router()
2019-12-04 11:58:47 +01:00
const { event: Event, user: User, resource: Resource } = require('../api/models')
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')
2019-09-11 19:12:24 +02:00
const debug = require('debug')('webfinger')
2019-07-29 01:27:47 +02:00
2019-08-02 13:43:28 +02:00
router.use(cors())
router.use((req, res, next) => {
// is federation enabled ?
2019-10-28 17:33:20 +01:00
if (req.settings.enable_federation) { return next() }
debug('Federation disabled')
res.status(404).send('Federation disabled')
})
router.get('/webfinger', (req, res) => {
2019-09-19 23:47:01 +02:00
if (!req.query || !req.query.resource || !req.query.resource.includes('acct:')) {
debug('Bad webfinger request => %s', 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
const domain = (new url.URL(req.settings.baseurl)).host
2019-09-11 19:12:24 +02:00
const [, name, req_domain] = resource.match(/acct:(.*)@(.*)/)
if (domain !== req_domain) {
debug('Bad webfinger request, requested domain "%s" instead of "%s"', req_domain, 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
}
if (name !== req.settings.instance_name) {
2019-09-11 19:12:24 +02:00
debug('User not found: %s', name)
return res.status(404).send(`No record found for ${name}`)
}
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',
2019-10-22 00:58:38 +02:00
href: `${req.settings.baseurl}/federation/u/${name}`
2019-07-29 01:27:47 +02:00
}
]
}
2019-08-01 15:18:45 +02:00
res.set('Content-Type', 'application/jrd+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) => {
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: {
2019-10-22 00:58:38 +02:00
nodeDescription: req.settings.description,
2020-03-18 10:11:24 +01:00
nodeName: req.settings.title,
nodeLabel: req.settings.instance_place
2019-08-02 13:43:28 +02:00
},
2019-09-11 19:12:24 +02:00
openRegistrations: settingsController.settings.allow_registration,
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) => {
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: {
2019-10-22 00:58:38 +02:00
baseUrl: req.settings.baseurl,
name: req.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) => {
2019-08-02 13:43:28 +02:00
const ret = {
links: [
2020-03-18 10:11:24 +01:00
{ href: `${req.settings.baseurl}/.well-known/nodeinfo/2.0`, rel: 'http://nodeinfo.diaspora.software/ns/schema/2.0' },
{ href: `${req.settings.baseurl}/.well-known/nodeinfo/2.1`, rel: 'http://nodeinfo.diaspora.software/ns/schema/2.1' }
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) => {
res.type('application/xml')
res.send(`<?xml version="1.0" encoding="UTF-8"?>
<XRD xmlns="http://docs.oasis-open.org/ns/xri/xrd-1.0">
2019-10-22 00:58:38 +02:00
<Link rel="lrdd" type="application/xrd+xml" template="${req.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) => {
debug('404 Page not found: %s', 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) => {
debug(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