
/**
 * This method imitates the ASAQC header-based
 * authorisation.
 *
 * The valid subscription token should be defined
 * in the $DOUGAL_ASAQC_SUBSCRIPTION_KEY environent
 * variable (e.g., by inclusion in .dougalrc).
 */
module.exports = async function (req, res, next) {

	try {
		const authSent = req.get("Ocp-Apim-Subscription-Key");
		const authExpected = process.env.DOUGAL_ASAQC_SUBSCRIPTION_KEY;

		if (authSent == authExpected || !authSent) {
			next()
		} else {
			throw {status: 401, message: "Not authorised"};
		}
	} catch (err) {
		next(err);
	}

};
