2021-03-29 19:58:11 +02:00
|
|
|
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
|
2021-09-15 20:28:10 +02:00
|
|
|
import { ApiTokenType } from '../types/models/api-token';
|
2021-04-22 10:07:10 +02:00
|
|
|
import { IUnleashConfig } from '../types/option';
|
2022-09-28 15:53:56 +02:00
|
|
|
import { IAuthRequest } from '../routes/unleash-types';
|
2021-03-29 19:58:11 +02:00
|
|
|
|
2021-09-15 20:28:10 +02:00
|
|
|
const isClientApi = ({ path }) => {
|
2023-05-27 16:29:54 +02:00
|
|
|
return path && path.indexOf('/api/client') > -1;
|
2021-09-15 20:28:10 +02:00
|
|
|
};
|
|
|
|
|
2022-08-16 15:33:33 +02:00
|
|
|
const isProxyApi = ({ path }) => {
|
2022-08-26 11:44:12 +02:00
|
|
|
if (!path) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle all our current proxy paths which will redirect to the new
|
|
|
|
// embedded proxy endpoint
|
|
|
|
return (
|
2023-05-27 16:29:54 +02:00
|
|
|
path.indexOf('/api/proxy') > -1 ||
|
|
|
|
path.indexOf('/api/development/proxy') > -1 ||
|
|
|
|
path.indexOf('/api/production/proxy') > -1 ||
|
|
|
|
path.indexOf('/api/frontend') > -1
|
2022-08-26 11:44:12 +02:00
|
|
|
);
|
2022-08-16 15:33:33 +02:00
|
|
|
};
|
|
|
|
|
2022-06-17 09:00:13 +02:00
|
|
|
export const TOKEN_TYPE_ERROR_MESSAGE =
|
2022-08-16 15:33:33 +02:00
|
|
|
'invalid token: expected a different token type for this endpoint';
|
2022-06-17 09:00:13 +02:00
|
|
|
|
2023-05-27 16:29:54 +02:00
|
|
|
export const NO_TOKEN_WHERE_TOKEN_WAS_REQUIRED =
|
|
|
|
'This endpoint requires an API token. Please add an authorization header to your request with a valid token';
|
2021-03-29 19:58:11 +02:00
|
|
|
const apiAccessMiddleware = (
|
2021-04-22 10:07:10 +02:00
|
|
|
{
|
|
|
|
getLogger,
|
|
|
|
authentication,
|
2022-08-26 15:16:29 +02:00
|
|
|
flagResolver,
|
|
|
|
}: Pick<IUnleashConfig, 'getLogger' | 'authentication' | 'flagResolver'>,
|
2021-03-29 19:58:11 +02:00
|
|
|
{ apiTokenService }: any,
|
|
|
|
): any => {
|
2021-04-22 10:07:10 +02:00
|
|
|
const logger = getLogger('/middleware/api-token.ts');
|
2021-09-15 20:28:10 +02:00
|
|
|
logger.debug('Enabling api-token middleware');
|
2021-03-29 19:58:11 +02:00
|
|
|
|
2021-04-22 10:07:10 +02:00
|
|
|
if (!authentication.enableApiToken) {
|
2021-03-29 19:58:11 +02:00
|
|
|
return (req, res, next) => next();
|
|
|
|
}
|
|
|
|
|
2022-09-28 15:53:56 +02:00
|
|
|
return (req: IAuthRequest, res, next) => {
|
2021-09-15 20:28:10 +02:00
|
|
|
if (req.user) {
|
2021-03-29 19:58:11 +02:00
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2021-04-22 23:40:52 +02:00
|
|
|
const apiToken = req.header('authorization');
|
2022-09-28 15:53:56 +02:00
|
|
|
if (!apiToken?.startsWith('user:')) {
|
|
|
|
const apiUser = apiTokenService.getUserForToken(apiToken);
|
|
|
|
const { CLIENT, FRONTEND } = ApiTokenType;
|
2022-06-17 09:00:13 +02:00
|
|
|
|
2022-09-28 15:53:56 +02:00
|
|
|
if (apiUser) {
|
|
|
|
if (
|
|
|
|
(apiUser.type === CLIENT && !isClientApi(req)) ||
|
|
|
|
(apiUser.type === FRONTEND && !isProxyApi(req)) ||
|
|
|
|
(apiUser.type === FRONTEND &&
|
|
|
|
!flagResolver.isEnabled('embedProxy'))
|
|
|
|
) {
|
|
|
|
res.status(403).send({
|
|
|
|
message: TOKEN_TYPE_ERROR_MESSAGE,
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
req.user = apiUser;
|
2023-05-27 16:29:54 +02:00
|
|
|
} else if (isClientApi(req) || isProxyApi(req)) {
|
|
|
|
// If we're here, we know that api token middleware was enabled, otherwise we'd returned a no-op middleware
|
|
|
|
// We explicitly only protect client and proxy apis, since admin apis are protected by our permission checker
|
|
|
|
// Reject with 401
|
|
|
|
res.status(401).send({
|
|
|
|
message: NO_TOKEN_WHERE_TOKEN_WAS_REQUIRED,
|
|
|
|
});
|
|
|
|
return;
|
2021-09-15 20:28:10 +02:00
|
|
|
}
|
2021-03-29 19:58:11 +02:00
|
|
|
}
|
|
|
|
} catch (error) {
|
2023-07-11 12:33:02 +02:00
|
|
|
logger.warn(error);
|
2021-03-29 19:58:11 +02:00
|
|
|
}
|
|
|
|
|
2022-06-17 09:00:13 +02:00
|
|
|
next();
|
2021-03-29 19:58:11 +02:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export default apiAccessMiddleware;
|