Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
return new Promise(resolve => {
// extract the x-forwarded-for header and the allowed cidrs from the params
const x = extractor(p1, p2, p3, p4, p5, p6);
const maybeClientIp = x.e1;
const cidrs = x.e2;
if (isNone(maybeClientIp)) {
return resolve(
ResponseErrorInternal(
"IP address cannot be extracted from the request"
)
);
}
if (
// either allowed CIDRs is empty or client IP is contained in allowed CIDRs
cidrs.size === 0 ||
isContainedInCidrs(maybeClientIp.value, cidrs)
) {
// forward request to handler
return resolve(handler(p1, p2, p3, p4, p5, p6));
} else {
// respond with Not Authorized
return resolve(ResponseErrorForbiddenNotAuthorized);
}
return left, IAzureUserAttributes>(
ResponseErrorInternal(
`Missing, empty or invalid ${HEADER_USER_EMAIL} header`
)
);
}
const userEmail = errorOrUserEmail.value;
const errorOrUserSubscriptionId = NonEmptyString.decode(
request.header(HEADER_USER_SUBSCRIPTION_KEY)
);
if (isLeft(errorOrUserSubscriptionId)) {
return left, IAzureUserAttributes>(
ResponseErrorInternal(
`Missing or empty ${HEADER_USER_SUBSCRIPTION_KEY} header`
)
);
}
const subscriptionId = errorOrUserSubscriptionId.value;
// serviceId equals subscriptionId
const errorOrMaybeService = await serviceModel.findOneByServiceId(
subscriptionId
);
if (isLeft(errorOrMaybeService)) {
winston.error(
`No service found for subscription|${subscriptionId}|${JSON.stringify(
errorOrMaybeService.value
() =>
// this should never happen since if the profile doesn't exist this function
// will never be called, but let's deal with this anyway, you never know
ResponseErrorInternal(
"Error while updating the existing profile, the profile does not exist!"
),
p => ResponseSuccessJson(toExtendedProfile(p))
() =>
resolve(
left>(
ResponseErrorInternal("Cannot get context from request")
)
),
context => resolve(right>(context))
e => {
ResponseErrorInternal(e).apply(response);
winston.log("debug", `wrapRequestHandler|ERROR|${request.url}|${e}`);
}
);
...currentService,
...serviceFromPayload,
serviceId
};
}
);
if (isLeft(errorOrMaybeUpdatedService)) {
return ResponseErrorQuery(
"Error while updating the existing service",
errorOrMaybeUpdatedService.value
);
}
const maybeUpdatedService = errorOrMaybeUpdatedService.value;
if (isNone(maybeUpdatedService)) {
return ResponseErrorInternal("Error while updating the existing service");
}
return ResponseSuccessJson(
retrievedServiceToPublic(maybeUpdatedService.value)
);
};
}