Begin work on refactoring every single route to use new subsystems

This commit is contained in:
Jesse Wierzbinski 2024-03-10 12:48:14 -10:00
parent 3b75f5f0a5
commit 05140f0d6f
No known key found for this signature in database
19 changed files with 128 additions and 183 deletions

View file

@ -1,7 +1,5 @@
import { applyConfig } from "@api";
import { getConfig } from "~classes/configmanager";
import { apiRoute, applyConfig } from "@api";
import { oauthRedirectUri } from "@constants";
import type { MatchedRoute } from "bun";
import {
calculatePKCECodeChallenge,
discoveryRequest,
@ -25,11 +23,7 @@ export const meta = applyConfig({
/**
* Redirects the user to the external OAuth provider
*/
export default async (
req: Request,
matchedRoute: MatchedRoute
// eslint-disable-next-line @typescript-eslint/require-await
): Promise<Response> => {
export default apiRoute(async (req, matchedRoute, extraData) => {
const redirectToLogin = (error: string) =>
Response.redirect(
`/oauth/authorize?` +
@ -49,7 +43,7 @@ export default async (
return redirectToLogin("Missing client_id");
}
const config = getConfig();
const config = await extraData.configManager.getConfig();
const issuer = config.oidc.providers.find(
provider => provider.id === issuerId
@ -98,4 +92,4 @@ export default async (
}).toString(),
302
);
};
});

View file

@ -1,7 +1,5 @@
import { applyConfig } from "@api";
import { getConfig } from "~classes/configmanager";
import { apiRoute, applyConfig } from "@api";
import { oauthRedirectUri } from "@constants";
import type { MatchedRoute } from "bun";
import { randomBytes } from "crypto";
import {
authorizationCodeGrantRequest,
@ -33,10 +31,7 @@ export const meta = applyConfig({
/**
* Redirects the user to the external OAuth provider
*/
export default async (
req: Request,
matchedRoute: MatchedRoute
): Promise<Response> => {
export default apiRoute(async (req, matchedRoute, extraData) => {
const redirectToLogin = (error: string) =>
Response.redirect(
`/oauth/authorize?` +
@ -65,7 +60,7 @@ export default async (
return redirectToLogin("Invalid flow");
}
const config = getConfig();
const config = await extraData.configManager.getConfig();
const issuer = config.oidc.providers.find(
provider => provider.id === issuerParam
@ -192,4 +187,4 @@ export default async (
`${flow.application.redirect_uris}?code=${code}`,
302
);
};
});

View file

@ -1,5 +1,4 @@
import { applyConfig } from "@api";
import { getConfig } from "~classes/configmanager";
import { apiRoute, applyConfig } from "@api";
import { jsonResponse } from "@response";
export const meta = applyConfig({
@ -17,9 +16,8 @@ export const meta = applyConfig({
/**
* Lists available OAuth providers
*/
// eslint-disable-next-line @typescript-eslint/require-await
export default async (): Promise<Response> => {
const config = getConfig();
export default apiRoute(async (req, matchedRoute, extraData) => {
const config = await extraData.configManager.getConfig();
return jsonResponse(
config.oidc.providers.map(p => ({
@ -28,4 +26,4 @@ export default async (): Promise<Response> => {
id: p.id,
}))
);
};
});

View file

@ -1,5 +1,4 @@
import { applyConfig } from "@api";
import { parseRequest } from "@request";
import { apiRoute, applyConfig } from "@api";
import { errorResponse, jsonResponse } from "@response";
import { client } from "~database/datasource";
@ -18,16 +17,16 @@ export const meta = applyConfig({
/**
* Allows getting token from OAuth code
*/
export default async (req: Request): Promise<Response> => {
export default apiRoute<{
grant_type: string;
code: string;
redirect_uri: string;
client_id: string;
client_secret: string;
scope: string;
}>(async (req, matchedRoute, extraData) => {
const { grant_type, code, redirect_uri, client_id, client_secret, scope } =
await parseRequest<{
grant_type: string;
code: string;
redirect_uri: string;
client_id: string;
client_secret: string;
scope: string;
}>(req);
extraData.parsedRequest;
if (grant_type !== "authorization_code")
return errorResponse(
@ -61,4 +60,4 @@ export default async (req: Request): Promise<Response> => {
scope: token.scope,
created_at: token.created_at,
});
};
});