fix: only require metrics password if set (#1715)
This commit is contained in:
@@ -138,34 +138,27 @@ export const defineMetricsHandler = <
|
|||||||
handler: MetricsHandler<TReq, TRes>
|
handler: MetricsHandler<TReq, TRes>
|
||||||
) => {
|
) => {
|
||||||
return defineEventHandler(async (event) => {
|
return defineEventHandler(async (event) => {
|
||||||
const auth = getHeader(event, 'Authorization');
|
|
||||||
|
|
||||||
if (!auth) {
|
|
||||||
throw createError({
|
|
||||||
statusCode: 401,
|
|
||||||
statusMessage: 'Unauthorized',
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
const [method, value] = auth.split(' ');
|
|
||||||
|
|
||||||
if (method !== 'Bearer' || !value) {
|
|
||||||
throw createError({
|
|
||||||
statusCode: 401,
|
|
||||||
statusMessage: 'Bearer Auth required',
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
const metricsConfig = await Database.general.getMetricsConfig();
|
const metricsConfig = await Database.general.getMetricsConfig();
|
||||||
|
|
||||||
if (metricsConfig[type] !== true) {
|
|
||||||
throw createError({
|
|
||||||
statusCode: 400,
|
|
||||||
statusMessage: 'Metrics not enabled',
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
if (metricsConfig.password) {
|
if (metricsConfig.password) {
|
||||||
|
const auth = getHeader(event, 'Authorization');
|
||||||
|
|
||||||
|
if (!auth) {
|
||||||
|
throw createError({
|
||||||
|
statusCode: 401,
|
||||||
|
statusMessage: 'Unauthorized',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const [method, value] = auth.split(' ');
|
||||||
|
|
||||||
|
if (method !== 'Bearer' || !value) {
|
||||||
|
throw createError({
|
||||||
|
statusCode: 401,
|
||||||
|
statusMessage: 'Bearer Auth required',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
const tokenValid = await isPasswordValid(value, metricsConfig.password);
|
const tokenValid = await isPasswordValid(value, metricsConfig.password);
|
||||||
|
|
||||||
if (!tokenValid) {
|
if (!tokenValid) {
|
||||||
@@ -176,6 +169,13 @@ export const defineMetricsHandler = <
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (metricsConfig[type] !== true) {
|
||||||
|
throw createError({
|
||||||
|
statusCode: 400,
|
||||||
|
statusMessage: 'Metrics not enabled',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
return await handler({ event });
|
return await handler({ event });
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user