summaryrefslogtreecommitdiff
path: root/src/server/webfinger.ts
blob: e72592351bcaa7d3d86c7b861322788b1a148747 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import * as Router from 'koa-router';

import config from '../config';
import parseAcct from '../acct/parse';
import User from '../models/user';

// Init router
const router = new Router();

router.get('/.well-known/webfinger', async ctx => {
	if (typeof ctx.query.resource !== 'string') {
		ctx.status = 400;
		return;
	}

	const resourceLower = ctx.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)) {
		ctx.status = 422;
		return;
	}

	const user = await User.findOne({
		usernameLower: parsedAcctLower.username,
		host: null
	});

	if (user === null) {
		ctx.status = 404;
		return;
	}

	ctx.body = {
		subject: `acct:${user.username}@${config.host}`,
		links: [{
			rel: 'self',
			type: 'application/activity+json',
			href: `${config.url}/users/${user._id}`
		}, {
			rel: 'http://webfinger.net/rel/profile-page',
			type: 'text/html',
			href: `${config.url}/@${user.username}`
		}, {
			rel: 'http://ostatus.org/schema/1.0/subscribe',
			template: `${config.url}/authorize-follow?acct={uri}`
		}]
	};
});

export default router;