trashposs/src/server/webfinger.ts

76 lines
1.7 KiB
TypeScript
Raw Normal View History

2018-04-18 12:24:09 +02:00
import * as mongo from 'mongodb';
2018-04-12 17:51:55 +02:00
import * as Router from 'koa-router';
2018-04-05 08:54:12 +02:00
2018-04-02 06:15:53 +02:00
import config from '../config';
2018-07-07 12:19:00 +02:00
import parseAcct from '../misc/acct/parse';
2018-04-18 12:24:09 +02:00
import User, { IUser } from '../models/user';
2018-04-01 07:12:07 +02:00
2018-04-12 17:51:55 +02:00
// Init router
const router = new Router();
2018-04-01 07:12:07 +02:00
2018-04-12 17:51:55 +02:00
router.get('/.well-known/webfinger', async ctx => {
if (typeof ctx.query.resource !== 'string') {
ctx.status = 400;
return;
2018-04-01 07:12:07 +02:00
}
2018-04-12 17:51:55 +02:00
const resourceLower = ctx.query.resource.toLowerCase();
2018-04-01 07:12:07 +02:00
let acctLower;
2018-04-18 12:24:09 +02:00
let id;
2018-04-01 07:12:07 +02:00
2018-04-18 12:24:09 +02:00
if (resourceLower.startsWith(config.url.toLowerCase() + '/@')) {
acctLower = resourceLower.split('/').pop();
} else if (resourceLower.startsWith(config.url.toLowerCase() + '/users/')) {
id = new mongo.ObjectID(resourceLower.split('/').pop());
2018-04-01 07:12:07 +02:00
} else if (resourceLower.startsWith('acct:')) {
acctLower = resourceLower.slice('acct:'.length);
} else {
acctLower = resourceLower;
}
2018-04-18 12:24:09 +02:00
let user: IUser;
if (acctLower) {
const parsedAcctLower = parseAcct(acctLower);
if (![null, config.host.toLowerCase()].includes(parsedAcctLower.host)) {
ctx.status = 422;
return;
}
2018-04-01 07:12:07 +02:00
2018-04-18 12:24:09 +02:00
user = await User.findOne({
usernameLower: parsedAcctLower.username,
host: null
});
} else {
user = await User.findOne({
_id: id,
host: null
});
}
2018-04-12 17:51:55 +02:00
2018-04-01 07:12:07 +02:00
if (user === null) {
2018-04-12 17:51:55 +02:00
ctx.status = 404;
return;
2018-04-01 07:12:07 +02:00
}
2018-04-12 17:51:55 +02:00
ctx.body = {
2018-04-01 07:12:07 +02:00
subject: `acct:${user.username}@${config.host}`,
2018-04-05 08:54:12 +02:00
links: [{
rel: 'self',
type: 'application/activity+json',
2018-04-08 08:25:17 +02:00
href: `${config.url}/users/${user._id}`
2018-04-05 08:54:12 +02:00
}, {
rel: 'http://webfinger.net/rel/profile-page',
type: 'text/html',
href: `${config.url}/@${user.username}`
2018-04-08 08:25:17 +02:00
}, {
rel: 'http://ostatus.org/schema/1.0/subscribe',
template: `${config.url}/authorize-follow?acct={uri}`
2018-04-05 08:54:12 +02:00
}]
2018-04-12 17:51:55 +02:00
};
2018-11-19 21:29:51 +01:00
ctx.set('Cache-Control', 'public, max-age=180');
2018-04-01 07:12:07 +02:00
});
2018-04-12 17:51:55 +02:00
export default router;