Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ import {
handleWebhookSiteChanged,
} from './http/api';
import { AccountFollowsView } from './http/api/views/account.follows.view';
import { createWebFingerHandler } from './http/handler/webfinger';
import { spanWrapper } from './instrumentation';
import { KnexKvStore } from './knex.kvstore';
import { scopeKvStore } from './kv-helpers';
Expand Down Expand Up @@ -247,7 +248,7 @@ if (process.env.MANUALLY_START_QUEUE === 'true') {
});
}

const flagService = new FlagService([]);
const flagService = new FlagService(['custom-webfinger']);

const events = new AsyncEvents();
const fedifyContextFactory = new FedifyContextFactory();
Expand Down Expand Up @@ -902,6 +903,13 @@ function requireRole(...roles: GhostRole[]) {
};
}

app.get(
'/.well-known/webfinger',
spanWrapper(
createWebFingerHandler(accountRepository, siteService, flagService),
),
);

app.get(
'/.ghost/activitypub/inbox/:handle',
requireRole(GhostRole.Owner, GhostRole.Administrator),
Expand Down
4 changes: 4 additions & 0 deletions src/flag/flag.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ export class FlagService {
return store.has(flag);
}

public isDisabled(flag: string) {
return !this.isEnabled(flag);
}

public getRegistered() {
return Array.from(this.flags);
}
Expand Down
13 changes: 13 additions & 0 deletions src/flag/flag.service.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,17 @@ describe('FlagService', () => {
expect(flagService.isEnabled('bar')).toBe(true);
});
});

it('should be able to check if a flag is disabled', () => {
const flagService = new FlagService(['foo', 'bar']);

flagService.runInContext(async () => {
expect(flagService.isDisabled('foo')).toBe(true); // registered but not enabled

expect(flagService.isDisabled('baz')).toBe(true); // not registered

flagService.enable('bar');
expect(flagService.isDisabled('bar')).toBe(false);
});
});
});
88 changes: 88 additions & 0 deletions src/http/handler/webfinger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import type { Context as HonoContext, Next } from 'hono';

import type { Account } from 'account/account.entity';
import type { KnexAccountRepository } from 'account/account.repository.knex';
import type { FlagService } from 'flag/flag.service';
import type { SiteService } from 'site/site.service';

const ACCOUNT_RESOURCE_PREFIX = 'acct:';
const HOST_REGEX = /^([a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]+)$/;

export function createWebFingerHandler(
accountRepository: KnexAccountRepository,
siteService: SiteService,
flagService: FlagService,
) {
/**
* Custom webfinger implementation to allow resources hosted on the www
* version of a host to resolve to the non-www version of the host
*
* @see https://github.com/fedify-dev/fedify/blob/main/src/webfinger/handler.ts
*/
return async function handleWebFinger(ctx: HonoContext, next: Next) {
if (flagService.isDisabled('custom-webfinger')) {
return next();
}

const resource = ctx.req.query('resource');

// We only support custom handling of `acct:` resources - If the
// resource is not an `acct:` resource, fallback to the default
// webfinger implementation
if (!resource || !resource.startsWith(ACCOUNT_RESOURCE_PREFIX)) {
return next();
}

const [_, resourceHost] = resource
.slice(ACCOUNT_RESOURCE_PREFIX.length)
.split('@');
if (!resourceHost || !HOST_REGEX.test(resourceHost)) {
return new Response(null, {
status: 400,
});
}

const site =
(await siteService.getSiteByHost(resourceHost)) ||
(await siteService.getSiteByHost(`www.${resourceHost}`));

if (!site) {
return new Response(null, {
status: 404,
});
}

let account: Account;

try {
account = await accountRepository.getBySite(site);
} catch (error) {
return new Response(null, {
status: 404,
});
}

const webfingerData = {
subject: `acct:${account.username}@${site.host.replace('www.', '')}`,
aliases: [account.apId.toString()],
links: [
{
rel: 'self',
href: account.apId.toString(),
type: 'application/activity+json',
},
{
rel: 'http://webfinger.net/rel/profile-page',
href: account.url.toString(),
},
],
};

return new Response(JSON.stringify(webfingerData), {
status: 200,
headers: {
'Content-Type': 'application/jrd+json',
},
});
};
}
Loading