Sharkey/src/server/webfinger.ts

62 lines
1.4 KiB
TypeScript
Raw Normal View History

2018-04-12 08:51:55 -07:00
import * as Router from 'koa-router';
2018-04-04 23:54:12 -07:00
2018-04-01 21:15:53 -07:00
import config from '../config';
2018-04-01 21:44:32 -07:00
import parseAcct from '../acct/parse';
2018-03-31 22:12:07 -07:00
import User from '../models/user';
2018-04-12 08:51:55 -07:00
// Init router
const router = new Router();
2018-03-31 22:12:07 -07:00
2018-04-12 08:51:55 -07:00
router.get('/.well-known/webfinger', async ctx => {
if (typeof ctx.query.resource !== 'string') {
ctx.status = 400;
return;
2018-03-31 22:12:07 -07:00
}
2018-04-12 08:51:55 -07:00
const resourceLower = ctx.query.resource.toLowerCase();
2018-03-31 22:12:07 -07:00
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)) {
2018-04-12 08:51:55 -07:00
ctx.status = 422;
return;
2018-03-31 22:12:07 -07:00
}
2018-04-12 08:51:55 -07:00
const user = await User.findOne({
usernameLower: parsedAcctLower.username,
host: null
});
2018-03-31 22:12:07 -07:00
if (user === null) {
2018-04-12 08:51:55 -07:00
ctx.status = 404;
return;
2018-03-31 22:12:07 -07:00
}
2018-04-12 08:51:55 -07:00
ctx.body = {
2018-03-31 22:12:07 -07:00
subject: `acct:${user.username}@${config.host}`,
2018-04-04 23:54:12 -07:00
links: [{
rel: 'self',
type: 'application/activity+json',
2018-04-07 23:25:17 -07:00
href: `${config.url}/users/${user._id}`
2018-04-04 23:54:12 -07:00
}, {
rel: 'http://webfinger.net/rel/profile-page',
type: 'text/html',
href: `${config.url}/@${user.username}`
2018-04-07 23:25:17 -07:00
}, {
rel: 'http://ostatus.org/schema/1.0/subscribe',
template: `${config.url}/authorize-follow?acct={uri}`
2018-04-04 23:54:12 -07:00
}]
2018-04-12 08:51:55 -07:00
};
2018-03-31 22:12:07 -07:00
});
2018-04-12 08:51:55 -07:00
export default router;