import { AuthenticationInfo } from '../authentication-info.js'; import { AuthenticatedRequest } from '../authenticated-request.js'; import * as express from 'express'; import { RequestHandler } from 'express'; import { UnauthorizedException } from '../../../exceptions/unauthorized-exception.js'; import { ForbiddenException } from '../../../exceptions/forbidden-exception.js'; import { envVars, getEnvVar } from '../../../util/envVars.js'; import { AccountType } from '@dwengo-1/common/util/account-types'; /** * Middleware which rejects unauthenticated users (with HTTP 401) and authenticated users which do not fulfill * the given access condition. * @param accessCondition Predicate over the current AuthenticationInfo. Access is only granted when this evaluates * to true. */ export function authorize
>( accessCondition: (auth: AuthenticationInfo, req: AuthenticatedRequest
) => boolean | Promise {
// Bypass authentication during testing
if (getEnvVar(envVars.RunMode) === 'test') {
return async (
_req: AuthenticatedRequest ,
_res: express.Response,
next: express.NextFunction
): Promise ,
_res: express.Response,
next: express.NextFunction
): Promise