improve compatibility with multipart/form-data mastodon API requests
This commit is contained in:
parent
1a19301c90
commit
cd4fbc851b
11 changed files with 202 additions and 241 deletions
|
|
@ -3,15 +3,14 @@
|
|||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
import querystring from 'querystring';
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import { v4 as uuid } from 'uuid';
|
||||
import multer from 'fastify-multer';
|
||||
import { bindThis } from '@/decorators.js';
|
||||
import type { Config } from '@/config.js';
|
||||
import { DI } from '@/di-symbols.js';
|
||||
import { MastodonClientService } from '@/server/api/mastodon/MastodonClientService.js';
|
||||
import { getErrorData } from '@/server/api/mastodon/MastodonLogger.js';
|
||||
import { ServerUtilityService } from '@/server/ServerUtilityService.js';
|
||||
import type { FastifyInstance } from 'fastify';
|
||||
|
||||
const kinds = [
|
||||
|
|
@ -56,6 +55,7 @@ export class OAuth2ProviderService {
|
|||
private config: Config,
|
||||
|
||||
private readonly mastodonClientService: MastodonClientService,
|
||||
private readonly serverUtilityService: ServerUtilityService,
|
||||
) { }
|
||||
|
||||
// https://datatracker.ietf.org/doc/html/rfc8414.html
|
||||
|
|
@ -92,36 +92,10 @@ export class OAuth2ProviderService {
|
|||
});
|
||||
}); */
|
||||
|
||||
const upload = multer({
|
||||
storage: multer.diskStorage({}),
|
||||
limits: {
|
||||
fileSize: this.config.maxFileSize || 262144000,
|
||||
files: 1,
|
||||
},
|
||||
});
|
||||
|
||||
fastify.addHook('onRequest', (request, reply, done) => {
|
||||
reply.header('Access-Control-Allow-Origin', '*');
|
||||
done();
|
||||
});
|
||||
|
||||
fastify.addContentTypeParser('application/x-www-form-urlencoded', (request, payload, done) => {
|
||||
let body = '';
|
||||
payload.on('data', (data) => {
|
||||
body += data;
|
||||
});
|
||||
payload.on('end', () => {
|
||||
try {
|
||||
const parsed = querystring.parse(body);
|
||||
done(null, parsed);
|
||||
} catch (e: unknown) {
|
||||
done(e instanceof Error ? e : new Error(String(e)));
|
||||
}
|
||||
});
|
||||
payload.on('error', done);
|
||||
});
|
||||
|
||||
fastify.register(multer.contentParser);
|
||||
this.serverUtilityService.addMultipartFormDataContentType(fastify);
|
||||
this.serverUtilityService.addFormUrlEncodedContentType(fastify);
|
||||
this.serverUtilityService.addCORS(fastify);
|
||||
this.serverUtilityService.addFlattenedQueryType(fastify);
|
||||
|
||||
for (const url of ['/authorize', '/authorize/']) {
|
||||
fastify.get<{ Querystring: Record<string, string | string[] | undefined> }>(url, async (request, reply) => {
|
||||
|
|
@ -136,7 +110,7 @@ export class OAuth2ProviderService {
|
|||
});
|
||||
}
|
||||
|
||||
fastify.post<{ Body?: Record<string, string | string[] | undefined>, Querystring: Record<string, string | string[] | undefined> }>('/token', { preHandler: upload.none() }, async (request, reply) => {
|
||||
fastify.post<{ Body?: Record<string, string | string[] | undefined>, Querystring: Record<string, string | string[] | undefined> }>('/token', async (request, reply) => {
|
||||
const body = request.body ?? request.query;
|
||||
|
||||
if (body.grant_type === 'client_credentials') {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue