* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* fix

* Update SignupApiService.ts

* wip

* wip

* Update ClientServerService.ts

* wip

* wip

* wip

* Update WellKnownServerService.ts

* wip

* wip

* update des

* wip

* Update ApiServerService.ts

* wip

* update deps

* Update WellKnownServerService.ts

* wip

* update deps

* Update ApiCallService.ts

* Update ApiCallService.ts

* Update ApiServerService.ts
This commit is contained in:
syuilo 2022-12-03 19:42:05 +09:00 committed by GitHub
parent 2db9f6efe7
commit 3a7182bfb5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
40 changed files with 1651 additions and 1977 deletions

View file

@ -1,6 +1,7 @@
import { Inject, Injectable } from '@nestjs/common';
import Router from '@koa/router';
import { FastifyInstance, FastifyPluginOptions, FastifyReply, FastifyRequest } from 'fastify';
import { IsNull, MoreThan } from 'typeorm';
import vary from 'vary';
import { DI } from '@/di-symbols.js';
import type { UsersRepository } from '@/models/index.js';
import type { Config } from '@/config.js';
@ -21,11 +22,10 @@ export class WellKnownServerService {
private nodeinfoServerService: NodeinfoServerService,
) {
this.createServer = this.createServer.bind(this);
}
public createRouter() {
const router = new Router();
public createServer(fastify: FastifyInstance, options: FastifyPluginOptions, done: (err?: Error) => void) {
const XRD = (...x: { element: string, value?: string, attributes?: Record<string, string> }[]) =>
`<?xml version="1.0" encoding="UTF-8"?><XRD xmlns="http://docs.oasis-open.org/ns/xri/xrd-1.0">${x.map(({ element, value, attributes }) =>
`<${
@ -34,37 +34,35 @@ export class WellKnownServerService {
typeof value === 'string' ? `>${escapeValue(value)}</${element}` : '/'
}>`).reduce((a, c) => a + c, '')}</XRD>`;
const allPath = '/.well-known/(.*)';
const allPath = '/.well-known/*';
const webFingerPath = '/.well-known/webfinger';
const jrd = 'application/jrd+json';
const xrd = 'application/xrd+xml';
router.use(allPath, async (ctx, next) => {
ctx.set({
'Access-Control-Allow-Headers': 'Accept',
'Access-Control-Allow-Methods': 'GET, OPTIONS',
'Access-Control-Allow-Origin': '*',
'Access-Control-Expose-Headers': 'Vary',
});
await next();
fastify.addHook('onRequest', (request, reply, done) => {
reply.header('Access-Control-Allow-Headers', 'Accept');
reply.header('Access-Control-Allow-Methods', 'GET, OPTIONS');
reply.header('Access-Control-Allow-Origin', '*');
reply.header('Access-Control-Expose-Headers', 'Vary');
done();
});
router.options(allPath, async ctx => {
ctx.status = 204;
fastify.options(allPath, async (request, reply) => {
reply.code(204);
});
router.get('/.well-known/host-meta', async ctx => {
ctx.set('Content-Type', xrd);
ctx.body = XRD({ element: 'Link', attributes: {
fastify.get('/.well-known/host-meta', async (request, reply) => {
reply.header('Content-Type', xrd);
return XRD({ element: 'Link', attributes: {
rel: 'lrdd',
type: xrd,
template: `${this.config.url}${webFingerPath}?resource={uri}`,
} });
});
router.get('/.well-known/host-meta.json', async ctx => {
ctx.set('Content-Type', jrd);
ctx.body = {
fastify.get('/.well-known/host-meta.json', async (request, reply) => {
reply.header('Content-Type', jrd);
return {
links: [{
rel: 'lrdd',
type: jrd,
@ -73,16 +71,16 @@ export class WellKnownServerService {
};
});
router.get('/.well-known/nodeinfo', async ctx => {
ctx.body = { links: this.nodeinfoServerService.getLinks() };
fastify.get('/.well-known/nodeinfo', async (request, reply) => {
return { links: this.nodeinfoServerService.getLinks() };
});
/* TODO
router.get('/.well-known/change-password', async ctx => {
fastify.get('/.well-known/change-password', async (request, reply) => {
});
*/
router.get(webFingerPath, async ctx => {
fastify.get<{ Querystring: { resource: string } }>(webFingerPath, async (request, reply) => {
const fromId = (id: User['id']): FindOptionsWhere<User> => ({
id,
host: IsNull(),
@ -104,22 +102,22 @@ router.get('/.well-known/change-password', async ctx => {
isSuspended: false,
} : 422;
if (typeof ctx.query.resource !== 'string') {
ctx.status = 400;
if (typeof request.query.resource !== 'string') {
reply.code(400);
return;
}
const query = generateQuery(ctx.query.resource.toLowerCase());
const query = generateQuery(request.query.resource.toLowerCase());
if (typeof query === 'number') {
ctx.status = query;
reply.code(query);
return;
}
const user = await this.usersRepository.findOneBy(query);
if (user == null) {
ctx.status = 404;
reply.code(404);
return;
}
@ -139,30 +137,25 @@ router.get('/.well-known/change-password', async ctx => {
template: `${this.config.url}/authorize-follow?acct={uri}`,
};
if (ctx.accepts(jrd, xrd) === xrd) {
ctx.body = XRD(
vary(reply.raw, 'Accept');
reply.header('Cache-Control', 'public, max-age=180');
if (request.accepts().type([jrd, xrd]) === xrd) {
reply.type(xrd);
return XRD(
{ element: 'Subject', value: subject },
{ element: 'Link', attributes: self },
{ element: 'Link', attributes: profilePage },
{ element: 'Link', attributes: subscribe });
ctx.type = xrd;
} else {
ctx.body = {
reply.type(jrd);
return {
subject,
links: [self, profilePage, subscribe],
};
ctx.type = jrd;
}
ctx.vary('Accept');
ctx.set('Cache-Control', 'public, max-age=180');
});
// Return 404 for other .well-known
router.all(allPath, async ctx => {
ctx.status = 404;
});
return router;
done();
}
}