Simone Taeggi 26d9f6c8e0 add a specific error on the BE when the Token Salt token is missing
Co-authored-by: Christian <christiancp100@gmail.com>
2023-03-09 14:44:20 +01:00

30 lines
766 B
JavaScript

'use strict';
const { getService } = require('../utils');
module.exports = () => async (ctx, next) => {
const transferUtils = getService('transfer').utils;
const { hasValidTokenSalt, isDataTransferEnabled, isDisabledFromEnv } = transferUtils;
if (isDataTransferEnabled()) {
return next();
}
if (!hasValidTokenSalt()) {
return ctx.badRequest(
'The server configuration for data transfer is invalid. Please contact your server administrator.',
{
code: 'INVALID_TOKEN_SALT',
}
);
}
if (isDisabledFromEnv()) {
return ctx.notFound();
}
// This should never happen as long as we're handling individual scenarios above
throw new Error('Unexpected error while trying to access a data transfer route');
};