2024-04-18 10:13:40 +02:00
|
|
|
import type { IUnleashConfig } from '../types';
|
|
|
|
import type { IApiRequest, IAuthRequest } from '../routes/unleash-types';
|
|
|
|
import { extractAuditInfo } from '../util';
|
|
|
|
|
|
|
|
export const auditAccessMiddleware = ({
|
|
|
|
getLogger,
|
|
|
|
}: Pick<IUnleashConfig, 'getLogger'>): any => {
|
|
|
|
const logger = getLogger('/middleware/audit-middleware.ts');
|
|
|
|
return (req: IAuthRequest | IApiRequest, _res, next) => {
|
|
|
|
if (!req.user) {
|
|
|
|
logger.info('Could not find user');
|
|
|
|
} else {
|
2024-04-18 16:32:35 +02:00
|
|
|
try {
|
|
|
|
req.audit = extractAuditInfo(req);
|
|
|
|
} catch (e) {
|
|
|
|
logger.warn('Could not find audit info in request');
|
|
|
|
}
|
2024-04-18 10:13:40 +02:00
|
|
|
}
|
|
|
|
next();
|
|
|
|
};
|
|
|
|
};
|