mirror of
https://iceshrimp.dev/limepotato/jormungandr-bite.git
synced 2024-11-14 14:07:38 -07:00
[mastodon-client] Code cleanup and refactor for /oauth/token
This commit is contained in:
parent
4b76d0ce6f
commit
a3d2330f26
2 changed files with 13 additions and 17 deletions
|
@ -3,6 +3,7 @@ import { AuthHelpers } from "@/server/api/mastodon/helpers/auth.js";
|
||||||
import { convertId, IdType } from "@/misc/convert-id.js";
|
import { convertId, IdType } from "@/misc/convert-id.js";
|
||||||
import { AuthConverter } from "@/server/api/mastodon/converters/auth.js";
|
import { AuthConverter } from "@/server/api/mastodon/converters/auth.js";
|
||||||
import { v4 as uuid } from "uuid";
|
import { v4 as uuid } from "uuid";
|
||||||
|
import { MastoApiError } from "@/server/api/mastodon/middleware/catch-errors.js";
|
||||||
|
|
||||||
export function setupEndpointsAuth(router: Router): void {
|
export function setupEndpointsAuth(router: Router): void {
|
||||||
router.post("/v1/apps", async (ctx) => {
|
router.post("/v1/apps", async (ctx) => {
|
||||||
|
@ -30,9 +31,7 @@ export function setupEndpointsAuthRoot(router: Router): void {
|
||||||
if (state) param += `&state=${state}`;
|
if (state) param += `&state=${state}`;
|
||||||
if (redirect_uri) param += `&redirect_uri=${redirect_uri}`;
|
if (redirect_uri) param += `&redirect_uri=${redirect_uri}`;
|
||||||
const client = client_id ? client_id : "";
|
const client = client_id ? client_id : "";
|
||||||
ctx.redirect(
|
ctx.redirect(`${Buffer.from(client.toString(), "base64").toString()}?${param}`);
|
||||||
`${Buffer.from(client.toString(), "base64").toString()}?${param}`,
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
router.post("/oauth/token", async (ctx) => {
|
router.post("/oauth/token", async (ctx) => {
|
||||||
|
@ -50,19 +49,14 @@ export function setupEndpointsAuthRoot(router: Router): void {
|
||||||
if (body.code) {
|
if (body.code) {
|
||||||
token = body.code;
|
token = body.code;
|
||||||
}
|
}
|
||||||
try {
|
const accessToken = await AuthHelpers.getAuthToken(body.client_secret, token ? token : "").catch(_ => {
|
||||||
const accessToken = await AuthHelpers.getAuthToken(body.client_secret, token ? token : "");
|
throw new MastoApiError(401);
|
||||||
const ret = {
|
});
|
||||||
|
ctx.body = {
|
||||||
access_token: accessToken,
|
access_token: accessToken,
|
||||||
token_type: "Bearer",
|
token_type: "Bearer",
|
||||||
scope: body.scope || "read write follow push",
|
scope: body.scope || "read write follow push",
|
||||||
created_at: Math.floor(new Date().getTime() / 1000),
|
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;
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -32,6 +32,7 @@ import { initializeStreamingServer } from "./api/streaming.js";
|
||||||
import removeTrailingSlash from "koa-remove-trailing-slashes";
|
import removeTrailingSlash from "koa-remove-trailing-slashes";
|
||||||
import { koaBody } from "koa-body";
|
import { koaBody } from "koa-body";
|
||||||
import { setupEndpointsAuthRoot } from "@/server/api/mastodon/endpoints/auth.js";
|
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);
|
export const serverLogger = new Logger("server", "gray", false);
|
||||||
|
|
||||||
// Init app
|
// Init app
|
||||||
|
@ -134,6 +135,7 @@ mastoRouter.use(async (ctx, next) => {
|
||||||
await next();
|
await next();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
mastoRouter.use(CatchErrorsMiddleware);
|
||||||
setupEndpointsAuthRoot(mastoRouter);
|
setupEndpointsAuthRoot(mastoRouter);
|
||||||
|
|
||||||
// Register router
|
// Register router
|
||||||
|
|
Loading…
Reference in a new issue