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';
|
2023-11-03 12:00:24 +01:00
|
|
|
import { IApiRequest, IAuthRequest } from '../routes/unleash-types';
|
2024-01-17 14:33:03 +01:00
|
|
|
import { IUnleashServices } from '../server-impl';
|
2024-02-27 16:08:44 +01:00
|
|
|
import { IFlagContext } from '../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
|
|
|
};
|
|
|
|
|
2024-01-02 09:51:01 +01:00
|
|
|
const isEdgeMetricsApi = ({ path }) => {
|
|
|
|
return path && path.indexOf('/edge/metrics') > -1;
|
|
|
|
};
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
2024-02-27 16:08:44 +01:00
|
|
|
const contextFrom = (
|
|
|
|
req: IAuthRequest<any, any, any, any> | IApiRequest<any, any, any, any>,
|
|
|
|
): IFlagContext | undefined => {
|
|
|
|
// this is what we'd get from edge:
|
|
|
|
// req_path: '/api/client/features',
|
|
|
|
// req_user_agent: 'unleash-edge-16.0.4'
|
|
|
|
return {
|
|
|
|
reqPath: req.path,
|
|
|
|
reqUserAgent: req.get ? req.get('User-Agent') ?? '' : '',
|
|
|
|
reqAppName:
|
|
|
|
req.headers?.['unleash-appname'] ?? req.query?.appName ?? '',
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
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'>,
|
2023-11-03 12:00:24 +01:00
|
|
|
{ apiTokenService }: Pick<IUnleashServices, 'apiTokenService'>,
|
2021-03-29 19:58:11 +02:00
|
|
|
): 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();
|
|
|
|
}
|
|
|
|
|
2024-02-27 16:08:44 +01:00
|
|
|
return async (req: IAuthRequest | IApiRequest, 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:')) {
|
2023-11-03 12:00:24 +01:00
|
|
|
const apiUser = apiToken
|
2024-02-27 16:08:44 +01:00
|
|
|
? await apiTokenService.getUserForToken(
|
|
|
|
apiToken,
|
|
|
|
contextFrom(req),
|
|
|
|
)
|
2023-11-03 12:00:24 +01:00
|
|
|
: undefined;
|
2022-09-28 15:53:56 +02:00
|
|
|
const { CLIENT, FRONTEND } = ApiTokenType;
|
2022-06-17 09:00:13 +02:00
|
|
|
|
2022-09-28 15:53:56 +02:00
|
|
|
if (apiUser) {
|
|
|
|
if (
|
2024-01-02 09:51:01 +01:00
|
|
|
(apiUser.type === CLIENT &&
|
|
|
|
!isClientApi(req) &&
|
|
|
|
!isEdgeMetricsApi(req)) ||
|
2022-09-28 15:53:56 +02:00
|
|
|
(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;
|