@overture-stack/lyric
Version:
Data Submission system
38 lines (37 loc) • 1.52 kB
TypeScript
import { NextFunction, Request, Response } from 'express';
export type UserSession = {
username: string;
isAdmin: boolean;
allowedWriteOrganizations: string[];
allowedReadOrganizations: string[];
};
export type UserSessionResult<TUser extends UserSession = UserSession> = {
user?: TUser;
errorCode?: number;
errorMessage?: string;
};
export type AuthConfig<TResult extends UserSessionResult = UserSessionResult> = {
enabled: boolean;
protectedMethods?: Array<'GET' | 'POST' | 'PUT' | 'DELETE'>;
customAuthHandler?: (req: Request) => TResult | Promise<TResult>;
};
export type RequestWithUser<TUser extends UserSession = UserSession> = Request & {
user?: TUser;
};
/**
* Determines whether the incoming request should bypass authentication,
* based on the application's authentication configuration.
* @param req
* @param authConfig
* @returns
*/
export declare const shouldBypassAuth: (req: Request, authConfig: AuthConfig) => boolean;
/**
* Middleware to handle authentication based on the provided auth configuration.
* It verifies the user's authentication implemented by the custom authentication handler
* If authentication is valid, it attaches the user information to the request object;
* Otherwise, it returns the appropriate error codes.
* @param authConfig
* @returns
*/
export declare const authMiddleware: (authConfig: AuthConfig) => (req: RequestWithUser, res: Response, next: NextFunction) => Promise<void | Response<any, Record<string, any>>>;