rubberguppe/routes/webfinger.js

41 lines
1.1 KiB
JavaScript
Raw Normal View History

'use strict'
const express = require('express')
const router = express.Router()
const pub = require('../pub')
const acctReg = /acct:[@~]?([^@]+)@?(.*)/
2018-09-15 07:01:19 +00:00
router.get('/', function (req, res) {
let resource = req.query.resource;
let acct = acctReg.exec(resource)
if (!acct || acct.length < 2) {
2018-09-15 07:01:19 +00:00
return res.status(400).send('Bad request. Please make sure "acct:USER@DOMAIN" is what you are sending as the "resource" query parameter.');
}
if (acct[2] && acct[2].toLowerCase() !== req.app.get('domain').toLowerCase()) {
return res.status(400).send('Requested user is not from this domain')
2018-09-15 07:01:19 +00:00
}
let db = req.app.get('db');
pub.actor.getOrCreateActor(acct[1], db)
.then(result => {
if (!result) {
return res.status(404).send(`${acct[1]}@${acct[2]} not found`)
}
const finger = {
'subject': resource,
'links': [
{
'rel': 'self',
'type': 'application/activity+json',
'href': result.id
}
]
}
return res.json(finger)
})
.catch(err => {
console.log(err);
res.status(500).send()
})
2018-09-15 07:01:19 +00:00
});
module.exports = router;