From a3d2330f26edc33cd4f6ed11ed545a806f75661b Mon Sep 17 00:00:00 2001 From: Laura Hausmann Date: Fri, 6 Oct 2023 01:22:42 +0200 Subject: [PATCH] [mastodon-client] Code cleanup and refactor for /oauth/token --- .../src/server/api/mastodon/endpoints/auth.ts | 28 ++++++++----------- packages/backend/src/server/index.ts | 2 ++ 2 files changed, 13 insertions(+), 17 deletions(-) diff --git a/packages/backend/src/server/api/mastodon/endpoints/auth.ts b/packages/backend/src/server/api/mastodon/endpoints/auth.ts index c5e7589ce..267128a6a 100644 --- a/packages/backend/src/server/api/mastodon/endpoints/auth.ts +++ b/packages/backend/src/server/api/mastodon/endpoints/auth.ts @@ -3,6 +3,7 @@ import { AuthHelpers } from "@/server/api/mastodon/helpers/auth.js"; import { convertId, IdType } from "@/misc/convert-id.js"; import { AuthConverter } from "@/server/api/mastodon/converters/auth.js"; import { v4 as uuid } from "uuid"; +import { MastoApiError } from "@/server/api/mastodon/middleware/catch-errors.js"; export function setupEndpointsAuth(router: Router): void { router.post("/v1/apps", async (ctx) => { @@ -30,9 +31,7 @@ export function setupEndpointsAuthRoot(router: Router): void { if (state) param += `&state=${state}`; if (redirect_uri) param += `&redirect_uri=${redirect_uri}`; const client = client_id ? client_id : ""; - ctx.redirect( - `${Buffer.from(client.toString(), "base64").toString()}?${param}`, - ); + ctx.redirect(`${Buffer.from(client.toString(), "base64").toString()}?${param}`); }); router.post("/oauth/token", async (ctx) => { @@ -50,19 +49,14 @@ export function setupEndpointsAuthRoot(router: Router): void { if (body.code) { token = body.code; } - try { - const accessToken = await AuthHelpers.getAuthToken(body.client_secret, token ? token : ""); - const ret = { - access_token: accessToken, - token_type: "Bearer", - scope: body.scope || "read write follow push", - created_at: Math.floor(new Date().getTime() / 1000), - }; - ctx.body = ret; - } catch (err: any) { - console.error(err); - ctx.status = 401; - ctx.body = err.response.data; - } + const accessToken = await AuthHelpers.getAuthToken(body.client_secret, token ? token : "").catch(_ => { + throw new MastoApiError(401); + }); + ctx.body = { + access_token: accessToken, + token_type: "Bearer", + scope: body.scope || "read write follow push", + created_at: Math.floor(new Date().getTime() / 1000), + }; }); } diff --git a/packages/backend/src/server/index.ts b/packages/backend/src/server/index.ts index edf5ef1f4..929efd73b 100644 --- a/packages/backend/src/server/index.ts +++ b/packages/backend/src/server/index.ts @@ -32,6 +32,7 @@ import { initializeStreamingServer } from "./api/streaming.js"; import removeTrailingSlash from "koa-remove-trailing-slashes"; import { koaBody } from "koa-body"; import { setupEndpointsAuthRoot } from "@/server/api/mastodon/endpoints/auth.js"; +import { CatchErrorsMiddleware } from "@/server/api/mastodon/middleware/catch-errors.js"; export const serverLogger = new Logger("server", "gray", false); // Init app @@ -134,6 +135,7 @@ mastoRouter.use(async (ctx, next) => { await next(); }); +mastoRouter.use(CatchErrorsMiddleware); setupEndpointsAuthRoot(mastoRouter); // Register router