Minor tidy up

This commit is contained in:
jd
2026-02-13 22:12:02 +00:00
parent 387a9a36f3
commit eddba49893
5 changed files with 12 additions and 12 deletions

View File

@@ -1,5 +1,5 @@
{ {
"name": "bgApp", "name": "bgapp",
"version": "1.0.0", "version": "1.0.0",
"description": "", "description": "",
"main": "src/index.ts", "main": "src/index.ts",

View File

@@ -1,4 +1,3 @@
import {BunRequest as Request} from "bun";
import {orm} from "../orm/orm.ts"; import {orm} from "../orm/orm.ts";
import jwt from "jsonwebtoken"; import jwt from "jsonwebtoken";
import {UnwrappedRequest} from "../utilities/guard"; import {UnwrappedRequest} from "../utilities/guard";

View File

@@ -19,7 +19,7 @@ const server = Bun.serve({
}, },
// (optional) fallback for unmatched routes: // (optional) fallback for unmatched routes:
fetch(request: Request): Response { fetch(): Response {
return Response.json({message: "Not found"}, {status: 404}); return Response.json({message: "Not found"}, {status: 404});
}, },
}); });

View File

@@ -1,4 +1,4 @@
import {beforeAll, afterAll} from 'bun:test'; import {beforeAll} from 'bun:test';
import Bun from 'bun'; import Bun from 'bun';
import {sql} from "bun"; import {sql} from "bun";

View File

@@ -1,6 +1,8 @@
import {BunRequest as Request} from 'bun'; import {BunRequest as Request} from 'bun';
import jwt from 'jsonwebtoken'; import jwt from 'jsonwebtoken';
import {Claims} from "../orm/orm.ts"; import {ErrorResponse} from "./responseHelper";
import {UnauthorizedError} from "./errors";
import {Claims} from "../orm/claims";
export function guardRedirect(method: Function, redirectMethod: Function, guardedClaims: string[] | undefined = undefined) { export function guardRedirect(method: Function, redirectMethod: Function, guardedClaims: string[] | undefined = undefined) {
try { try {
@@ -10,18 +12,17 @@ export function guardRedirect(method: Function, redirectMethod: Function, guarde
} }
} }
export function guard(method: Function, guardedClaims: string[] | undefined = undefined):(r:Request)=>Promise<Response> { export function guard(method: Function, guardedClaims: string[] | undefined = undefined): (r: Request) => Promise<Response> {
return async (request: Request): Promise<Response> => { return async (request: Request): Promise<Response> => {
const authHeader: string | null = request.headers.get('Authorization')?.replace(/^Bearer /, '') as string ?? null; const authHeader: string | null = request.headers.get('Authorization')?.replace(/^Bearer /, '') as string ?? null;
try { try {
const userClaims: Claims = jwt.verify(authHeader as string, process.env.JWT_SECRET_KEY as string) as Claims; const userClaims: Claims = jwt.verify(authHeader as string, process.env.JWT_SECRET_KEY as string) as Claims;
console.log('Claims?', guardedClaims !== undefined, !userClaims.claims.some(x => guardedClaims?.includes(x))) if (guardedClaims !== undefined && !userClaims.claims.some((x: string): boolean => guardedClaims.includes(x))) {
if (guardedClaims !== undefined && !userClaims.claims.some(x => guardedClaims.includes(x))) { throw new UnauthorizedError('Unauthorized');
throw new Error('Unauthorized');
} }
return method(await unwrap(request, userClaims)); return method(await unwrap(request, userClaims));
} catch (e) { } catch (error: any) {
return Response.json({message: 'Authentication failed.'}, {status: 401}) return new ErrorResponse(error as Error);
} }
} }
} }
@@ -49,7 +50,7 @@ export async function unwrap(request: Request, claims: Claims | null = null) {
}) })
} }
export function unwrapMethod(methodToUnwrap:((r:UnwrappedRequest)=>Response)|((r:UnwrappedRequest)=>Promise<Response>)):(r:Request)=>Promise<Response> { export function unwrapMethod(methodToUnwrap: ((r: UnwrappedRequest) => Response) | ((r: UnwrappedRequest) => Promise<Response>)): (r: Request) => Promise<Response> {
return async (request: Request) => { return async (request: Request) => {
const unwrappedRequest = await unwrap(request); const unwrappedRequest = await unwrap(request);
return await methodToUnwrap(unwrappedRequest); return await methodToUnwrap(unwrappedRequest);