jormungandr-bite/src/server/webfinger.ts

54 lines
1.4 KiB
TypeScript
Raw Normal View History

2018-04-05 00:54:12 -06:00
import * as express from 'express';
2018-04-01 22:15:53 -06:00
import config from '../config';
2018-04-01 22:44:32 -06:00
import parseAcct from '../acct/parse';
2018-03-31 23:12:07 -06:00
import User from '../models/user';
2018-04-08 00:25:17 -06:00
const app = express.Router();
2018-03-31 23:12:07 -06:00
2018-04-08 00:25:17 -06:00
app.get('/.well-known/webfinger', async (req, res) => {
2018-03-31 23:12:07 -06:00
if (typeof req.query.resource !== 'string') {
return res.sendStatus(400);
}
const resourceLower = req.query.resource.toLowerCase();
const webPrefix = config.url.toLowerCase() + '/@';
let acctLower;
if (resourceLower.startsWith(webPrefix)) {
acctLower = resourceLower.slice(webPrefix.length);
} else if (resourceLower.startsWith('acct:')) {
acctLower = resourceLower.slice('acct:'.length);
} else {
acctLower = resourceLower;
}
const parsedAcctLower = parseAcct(acctLower);
if (![null, config.host.toLowerCase()].includes(parsedAcctLower.host)) {
return res.sendStatus(422);
}
const user = await User.findOne({ usernameLower: parsedAcctLower.username, host: null });
if (user === null) {
return res.sendStatus(404);
}
return res.json({
subject: `acct:${user.username}@${config.host}`,
2018-04-05 00:54:12 -06:00
links: [{
rel: 'self',
type: 'application/activity+json',
2018-04-08 00:25:17 -06:00
href: `${config.url}/users/${user._id}`
2018-04-05 00:54:12 -06:00
}, {
rel: 'http://webfinger.net/rel/profile-page',
type: 'text/html',
href: `${config.url}/@${user.username}`
2018-04-08 00:25:17 -06:00
}, {
rel: 'http://ostatus.org/schema/1.0/subscribe',
template: `${config.url}/authorize-follow?acct={uri}`
2018-04-05 00:54:12 -06:00
}]
2018-03-31 23:12:07 -06:00
});
});
export default app;