calckey/src/server/webfinger.ts

51 lines
1.3 KiB
TypeScript
Raw Normal View History

2018-04-05 06:54:12 +00:00
import * as express from 'express';
2018-04-02 04:15:53 +00:00
import config from '../config';
2018-04-02 04:44:32 +00:00
import parseAcct from '../acct/parse';
2018-04-01 05:12:07 +00:00
import User from '../models/user';
const app = express();
2018-04-05 06:54:12 +00:00
app.get('/.well-known/webfinger', async (req: express.Request, res: express.Response) => {
2018-04-01 05:12:07 +00: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 06:54:12 +00:00
links: [{
rel: 'self',
type: 'application/activity+json',
href: `${config.url}/@${user.username}`
}, {
rel: 'http://webfinger.net/rel/profile-page',
type: 'text/html',
href: `${config.url}/@${user.username}`
}]
2018-04-01 05:12:07 +00:00
});
});
export default app;