From b25e19384563ddc2ad870b1b4d839d169a16fba5 Mon Sep 17 00:00:00 2001 From: Mar0xy Date: Sun, 24 Sep 2023 00:41:13 +0200 Subject: [PATCH] upd: create auth endpoint --- .../src/server/api/mastodon/endpoints/auth.ts | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 packages/backend/src/server/api/mastodon/endpoints/auth.ts diff --git a/packages/backend/src/server/api/mastodon/endpoints/auth.ts b/packages/backend/src/server/api/mastodon/endpoints/auth.ts new file mode 100644 index 0000000000..48b5ec55c9 --- /dev/null +++ b/packages/backend/src/server/api/mastodon/endpoints/auth.ts @@ -0,0 +1,74 @@ +import type { MegalodonInterface } from "megalodon"; +import type { FastifyRequest } from 'fastify'; + +const readScope = [ + "read:account", + "read:drive", + "read:blocks", + "read:favorites", + "read:following", + "read:messaging", + "read:mutes", + "read:notifications", + "read:reactions", + "read:pages", + "read:page-likes", + "read:user-groups", + "read:channels", + "read:gallery", + "read:gallery-likes", +]; + +const writeScope = [ + "write:account", + "write:drive", + "write:blocks", + "write:favorites", + "write:following", + "write:messaging", + "write:mutes", + "write:notes", + "write:notifications", + "write:reactions", + "write:votes", + "write:pages", + "write:page-likes", + "write:user-groups", + "write:channels", + "write:gallery", + "write:gallery-likes", +]; + +export async function apiAuthMastodon(request: FastifyRequest, client: MegalodonInterface) { + const body: any = request.body || request.query; + try { + let scope = body.scopes; + if (typeof scope === "string") scope = scope.split(" "); + const pushScope = new Set(); + for (const s of scope) { + if (s.match(/^read/)) for (const r of readScope) pushScope.add(r); + if (s.match(/^write/)) for (const r of writeScope) pushScope.add(r); + } + const scopeArr = Array.from(pushScope); + + const red = body.redirect_uris; + const appData = await client.registerApp(body.client_name, { + scopes: scopeArr, + redirect_uris: red, + website: body.website, + }); + const returns = { + id: Math.floor(Math.random() * 100).toString(), + name: appData.name, + website: body.website, + redirect_uri: red, + client_id: Buffer.from(appData.url || "").toString("base64"), + client_secret: appData.clientSecret, + }; + + return returns; + } catch (e: any) { + console.error(e); + return e.response.data; + } +}