iceshrimp/packages/backend/src/server/api/mastodon/endpoints/auth.ts

73 lines
1.8 KiB
TypeScript
Raw Normal View History

import Router from "@koa/router";
2023-09-29 00:49:01 +02:00
import { AuthHelpers } from "@/server/api/mastodon/helpers/auth.js";
import { convertId, IdType } from "@/misc/convert-id.js";
const readScope = [
2023-02-11 00:41:19 +01:00
"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 = [
2023-02-11 00:41:19 +01:00
"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 function apiAuthMastodon(router: Router): void {
2023-02-11 00:41:19 +01:00
router.post("/v1/apps", async (ctx) => {
2023-02-28 17:23:04 +01:00
const body: any = ctx.request.body || ctx.request.query;
try {
2023-02-11 00:41:19 +01:00
let scope = body.scopes;
if (typeof scope === "string") scope = scope.split(" ");
const pushScope = new Set<string>();
for (const s of scope) {
2023-02-11 00:41:19 +01:00
if (s.match(/^read/)) for (const r of readScope) pushScope.add(r);
if (s.match(/^write/)) for (const r of writeScope) pushScope.add(r);
}
2023-02-11 00:41:19 +01:00
const scopeArr = Array.from(pushScope);
const red = body.redirect_uris;
2023-09-29 00:49:01 +02:00
const appData = await AuthHelpers.registerApp(body['client_name'], scopeArr, red, body['website']);
2023-02-28 17:23:04 +01:00
const returns = {
2023-09-29 00:49:01 +02:00
id: convertId(appData.id, IdType.MastodonId),
name: appData.name,
2023-02-28 17:23:04 +01:00
website: body.website,
redirect_uri: red,
2023-09-29 00:49:01 +02:00
client_id: Buffer.from(appData.url ?? "").toString("base64"),
2023-03-31 04:10:03 +02:00
client_secret: appData.clientSecret,
2023-02-11 00:41:19 +01:00
};
2023-02-28 17:23:04 +01:00
ctx.body = returns;
} catch (e: any) {
2023-02-11 00:41:19 +01:00
console.error(e);
ctx.status = 401;
ctx.body = e.response.data;
}
});
}