summaryrefslogtreecommitdiff
path: root/packages/backend/src/server/oauth
diff options
context:
space:
mode:
authorHazelnoot <acomputerdog@gmail.com>2025-05-06 12:55:51 -0400
committerHazelnoot <acomputerdog@gmail.com>2025-05-08 11:23:20 -0400
commitcd4fbc851b0fc766c93552971cb916e4ccd1ef55 (patch)
tree4ff9de42a5f63e66496833e7684b5437411876c7 /packages/backend/src/server/oauth
parentmerge: Add missing paused state (!992) (diff)
downloadsharkey-cd4fbc851b0fc766c93552971cb916e4ccd1ef55.tar.gz
sharkey-cd4fbc851b0fc766c93552971cb916e4ccd1ef55.tar.bz2
sharkey-cd4fbc851b0fc766c93552971cb916e4ccd1ef55.zip
improve compatibility with multipart/form-data mastodon API requests
Diffstat (limited to 'packages/backend/src/server/oauth')
-rw-r--r--packages/backend/src/server/oauth/OAuth2ProviderService.ts40
1 files changed, 7 insertions, 33 deletions
diff --git a/packages/backend/src/server/oauth/OAuth2ProviderService.ts b/packages/backend/src/server/oauth/OAuth2ProviderService.ts
index a65acb7c9b..e1f39dd9b6 100644
--- a/packages/backend/src/server/oauth/OAuth2ProviderService.ts
+++ b/packages/backend/src/server/oauth/OAuth2ProviderService.ts
@@ -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') {