|
|
|
@@ -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);
|
|
|
|
|