1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-10-18 20:09:08 +02:00
unleash.unleash/src/lib/middleware/api-token-middleware.ts
2021-04-22 23:40:52 +02:00

39 lines
1001 B
TypeScript

/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
import { IUnleashConfig } from '../types/option';
const apiAccessMiddleware = (
{
getLogger,
authentication,
}: Pick<IUnleashConfig, 'getLogger' | 'authentication'>,
{ apiTokenService }: any,
): any => {
const logger = getLogger('/middleware/api-token.ts');
logger.info('Enabling api-token middleware');
if (!authentication.enableApiToken) {
return (req, res, next) => next();
}
return (req, res, next) => {
if (req.apiUser) {
return next();
}
try {
const apiToken = req.header('authorization');
const apiUser = apiTokenService.getUserForToken(apiToken);
if (apiUser) {
req.user = apiUser;
}
} catch (error) {
logger.error(error);
}
return next();
};
};
module.exports = apiAccessMiddleware;
export default apiAccessMiddleware;