mirror of
https://github.com/Unleash/unleash.git
synced 2025-01-31 00:16:47 +01:00
af3944bd75
When using PATs if the user that the PAT is for has been removed, we currently log the missing user at ERROR level. Since this is not something our SREs can fix, this PR downgrades the NotFoundError to WARN, instead of ERROR.
39 lines
1.2 KiB
TypeScript
39 lines
1.2 KiB
TypeScript
import { IUnleashConfig } from '../types';
|
|
import { IAuthRequest } from '../routes/unleash-types';
|
|
import NotFoundError from '../error/notfound-error';
|
|
|
|
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
|
|
const patMiddleware = (
|
|
{ getLogger }: Pick<IUnleashConfig, 'getLogger'>,
|
|
{ accountService }: any,
|
|
): any => {
|
|
const logger = getLogger('/middleware/pat-middleware.ts');
|
|
logger.debug('Enabling PAT middleware');
|
|
|
|
return async (req: IAuthRequest, res, next) => {
|
|
try {
|
|
const apiToken = req.header('authorization');
|
|
if (apiToken?.startsWith('user:')) {
|
|
const user =
|
|
await accountService.getAccountByPersonalAccessToken(
|
|
apiToken,
|
|
);
|
|
req.user = user;
|
|
accountService.addPATSeen(apiToken);
|
|
}
|
|
} catch (error) {
|
|
if (error instanceof NotFoundError) {
|
|
logger.warn(
|
|
'Tried to use a PAT token for user that no longer existed',
|
|
error,
|
|
);
|
|
} else {
|
|
logger.error(error);
|
|
}
|
|
}
|
|
next();
|
|
};
|
|
};
|
|
|
|
export default patMiddleware;
|