2023-07-27 14:31:52 +09:00
|
|
|
/*
|
2024-02-14 00:59:27 +09:00
|
|
|
* SPDX-FileCopyrightText: syuilo and misskey-project
|
2023-07-27 14:31:52 +09:00
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
2022-12-03 19:42:05 +09:00
|
|
|
import cors from '@fastify/cors';
|
|
|
|
import multipart from '@fastify/multipart';
|
2022-12-17 11:02:49 +09:00
|
|
|
import fastifyCookie from '@fastify/cookie';
|
2023-02-16 23:09:41 +09:00
|
|
|
import { ModuleRef } from '@nestjs/core';
|
2022-09-21 05:33:11 +09:00
|
|
|
import type { Config } from '@/config.js';
|
2023-09-15 14:28:29 +09:00
|
|
|
import type { InstancesRepository, AccessTokensRepository } from '@/models/_.js';
|
2022-09-18 03:27:08 +09:00
|
|
|
import { DI } from '@/di-symbols.js';
|
|
|
|
import { UserEntityService } from '@/core/entities/UserEntityService.js';
|
2022-12-17 11:02:49 +09:00
|
|
|
import { bindThis } from '@/decorators.js';
|
2023-02-16 23:09:41 +09:00
|
|
|
import endpoints from './endpoints.js';
|
2022-09-18 03:27:08 +09:00
|
|
|
import { ApiCallService } from './ApiCallService.js';
|
|
|
|
import { SignupApiService } from './SignupApiService.js';
|
|
|
|
import { SigninApiService } from './SigninApiService.js';
|
2022-12-14 00:01:45 +09:00
|
|
|
import type { FastifyInstance, FastifyPluginOptions } from 'fastify';
|
2022-09-18 03:27:08 +09:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class ApiServerService {
|
|
|
|
constructor(
|
|
|
|
private moduleRef: ModuleRef,
|
|
|
|
|
|
|
|
@Inject(DI.config)
|
|
|
|
private config: Config,
|
|
|
|
|
|
|
|
@Inject(DI.instancesRepository)
|
|
|
|
private instancesRepository: InstancesRepository,
|
|
|
|
|
|
|
|
@Inject(DI.accessTokensRepository)
|
|
|
|
private accessTokensRepository: AccessTokensRepository,
|
|
|
|
|
|
|
|
private userEntityService: UserEntityService,
|
|
|
|
private apiCallService: ApiCallService,
|
2023-01-13 17:49:05 +09:00
|
|
|
private signupApiService: SignupApiService,
|
|
|
|
private signinApiService: SigninApiService,
|
2022-09-18 03:27:08 +09:00
|
|
|
) {
|
2022-12-04 15:03:09 +09:00
|
|
|
//this.createServer = this.createServer.bind(this);
|
2022-09-18 03:27:08 +09:00
|
|
|
}
|
|
|
|
|
2022-12-04 15:03:09 +09:00
|
|
|
@bindThis
|
2022-12-03 19:42:05 +09:00
|
|
|
public createServer(fastify: FastifyInstance, options: FastifyPluginOptions, done: (err?: Error) => void) {
|
|
|
|
fastify.register(cors, {
|
2022-09-18 03:27:08 +09:00
|
|
|
origin: '*',
|
|
|
|
});
|
|
|
|
|
2022-12-03 19:42:05 +09:00
|
|
|
fastify.register(multipart, {
|
2022-09-18 03:27:08 +09:00
|
|
|
limits: {
|
|
|
|
fileSize: this.config.maxFileSize ?? 262144000,
|
|
|
|
files: 1,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2022-12-17 11:02:49 +09:00
|
|
|
fastify.register(fastifyCookie, {});
|
|
|
|
|
2022-12-03 19:42:05 +09:00
|
|
|
// Prevent cache
|
|
|
|
fastify.addHook('onRequest', (request, reply, done) => {
|
|
|
|
reply.header('Cache-Control', 'private, max-age=0, must-revalidate');
|
|
|
|
done();
|
|
|
|
});
|
2022-09-18 03:27:08 +09:00
|
|
|
|
|
|
|
for (const endpoint of endpoints) {
|
2022-12-03 19:42:05 +09:00
|
|
|
const ep = {
|
|
|
|
name: endpoint.name,
|
|
|
|
meta: endpoint.meta,
|
|
|
|
params: endpoint.params,
|
|
|
|
exec: this.moduleRef.get('ep:' + endpoint.name, { strict: false }).exec,
|
|
|
|
};
|
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
if (endpoint.meta.requireFile) {
|
2022-12-03 19:42:05 +09:00
|
|
|
fastify.all<{
|
|
|
|
Params: { endpoint: string; },
|
|
|
|
Body: Record<string, unknown>,
|
|
|
|
Querystring: Record<string, unknown>,
|
2023-03-03 11:13:12 +09:00
|
|
|
}>('/' + endpoint.name, async (request, reply) => {
|
2022-12-03 19:42:05 +09:00
|
|
|
if (request.method === 'GET' && !endpoint.meta.allowGet) {
|
|
|
|
reply.code(405);
|
2022-12-24 13:55:50 +09:00
|
|
|
reply.send();
|
2022-12-03 19:42:05 +09:00
|
|
|
return;
|
|
|
|
}
|
2023-02-27 18:01:43 +09:00
|
|
|
|
2023-03-03 11:13:12 +09:00
|
|
|
// Await so that any error can automatically be translated to HTTP 500
|
|
|
|
await this.apiCallService.handleMultipartRequest(ep, request, reply);
|
|
|
|
return reply;
|
2022-12-03 19:42:05 +09:00
|
|
|
});
|
2022-09-18 03:27:08 +09:00
|
|
|
} else {
|
2022-12-03 19:42:05 +09:00
|
|
|
fastify.all<{
|
|
|
|
Params: { endpoint: string; },
|
|
|
|
Body: Record<string, unknown>,
|
|
|
|
Querystring: Record<string, unknown>,
|
2023-04-11 14:28:40 +09:00
|
|
|
}>('/' + endpoint.name, { bodyLimit: 1024 * 1024 }, async (request, reply) => {
|
2022-12-03 19:42:05 +09:00
|
|
|
if (request.method === 'GET' && !endpoint.meta.allowGet) {
|
|
|
|
reply.code(405);
|
2022-12-24 13:55:50 +09:00
|
|
|
reply.send();
|
2022-12-03 19:42:05 +09:00
|
|
|
return;
|
2022-09-18 03:27:08 +09:00
|
|
|
}
|
2023-02-27 18:01:43 +09:00
|
|
|
|
2023-03-03 11:13:12 +09:00
|
|
|
// Await so that any error can automatically be translated to HTTP 500
|
|
|
|
await this.apiCallService.handleRequest(ep, request, reply);
|
|
|
|
return reply;
|
2022-12-03 19:42:05 +09:00
|
|
|
});
|
2022-09-18 03:27:08 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-03 19:42:05 +09:00
|
|
|
fastify.post<{
|
|
|
|
Body: {
|
|
|
|
username: string;
|
|
|
|
password: string;
|
|
|
|
host?: string;
|
|
|
|
invitationCode?: string;
|
|
|
|
emailAddress?: string;
|
|
|
|
'hcaptcha-response'?: string;
|
|
|
|
'g-recaptcha-response'?: string;
|
|
|
|
'turnstile-response'?: string;
|
|
|
|
}
|
2023-01-13 17:49:05 +09:00
|
|
|
}>('/signup', (request, reply) => this.signupApiService.signup(request, reply));
|
2022-12-03 19:42:05 +09:00
|
|
|
|
|
|
|
fastify.post<{
|
|
|
|
Body: {
|
|
|
|
username: string;
|
|
|
|
password: string;
|
|
|
|
token?: string;
|
|
|
|
signature?: string;
|
|
|
|
authenticatorData?: string;
|
|
|
|
clientDataJSON?: string;
|
|
|
|
credentialId?: string;
|
|
|
|
challengeId?: string;
|
|
|
|
};
|
2023-01-13 17:49:05 +09:00
|
|
|
}>('/signin', (request, reply) => this.signinApiService.signin(request, reply));
|
2022-12-03 19:42:05 +09:00
|
|
|
|
2023-01-13 17:49:05 +09:00
|
|
|
fastify.post<{ Body: { code: string; } }>('/signup-pending', (request, reply) => this.signupApiService.signupPending(request, reply));
|
2022-12-03 19:42:05 +09:00
|
|
|
|
|
|
|
fastify.get('/v1/instance/peers', async (request, reply) => {
|
2022-09-18 03:27:08 +09:00
|
|
|
const instances = await this.instancesRepository.find({
|
|
|
|
select: ['host'],
|
2022-12-19 09:00:13 +09:00
|
|
|
where: {
|
|
|
|
isSuspended: false,
|
|
|
|
},
|
2022-09-18 03:27:08 +09:00
|
|
|
});
|
|
|
|
|
2022-12-03 19:42:05 +09:00
|
|
|
return instances.map(instance => instance.host);
|
2022-09-18 03:27:08 +09:00
|
|
|
});
|
|
|
|
|
2022-12-03 19:42:05 +09:00
|
|
|
fastify.post<{ Params: { session: string; } }>('/miauth/:session/check', async (request, reply) => {
|
2022-09-18 03:27:08 +09:00
|
|
|
const token = await this.accessTokensRepository.findOneBy({
|
2022-12-03 19:42:05 +09:00
|
|
|
session: request.params.session,
|
2022-09-18 03:27:08 +09:00
|
|
|
});
|
|
|
|
|
|
|
|
if (token && token.session != null && !token.fetched) {
|
|
|
|
this.accessTokensRepository.update(token.id, {
|
|
|
|
fetched: true,
|
|
|
|
});
|
|
|
|
|
2022-12-03 19:42:05 +09:00
|
|
|
return {
|
2022-09-18 03:27:08 +09:00
|
|
|
ok: true,
|
|
|
|
token: token.token,
|
2024-01-31 15:45:35 +09:00
|
|
|
user: await this.userEntityService.pack(token.userId, null, { schema: 'UserDetailedNotMe' }),
|
2022-09-18 03:27:08 +09:00
|
|
|
};
|
|
|
|
} else {
|
2022-12-03 19:42:05 +09:00
|
|
|
return {
|
2022-09-18 03:27:08 +09:00
|
|
|
ok: false,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-02-27 18:01:43 +09:00
|
|
|
// Make sure any unknown path under /api returns HTTP 404 Not Found,
|
|
|
|
// because otherwise ClientServerService will return the base client HTML
|
|
|
|
// page with HTTP 200.
|
2023-03-10 02:37:44 +09:00
|
|
|
fastify.get('/*', (request, reply) => {
|
2023-02-27 18:01:43 +09:00
|
|
|
reply.code(404);
|
|
|
|
// Mock ApiCallService.send's error handling
|
|
|
|
reply.send({
|
|
|
|
error: {
|
|
|
|
message: 'Unknown API endpoint.',
|
|
|
|
code: 'UNKNOWN_API_ENDPOINT',
|
|
|
|
id: '2ca3b769-540a-4f08-9dd5-b5a825b6d0f1',
|
|
|
|
kind: 'client',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-12-03 19:42:05 +09:00
|
|
|
done();
|
2022-09-18 03:27:08 +09:00
|
|
|
}
|
|
|
|
}
|