So, I'm using Node.js + Swagger + MongoDB.
I'm trying to validate that all requests come with a valid auth_token, which is a value stored on the user on mongo. My problem is that the validators supported by Swagger need to return true or false, but since I have to check against Mongo to verify the auth token, the whole validation becomes asynchronous.
Here is the code in case you need it:
swagger.addValidator(
function validate(req, path, httpMethod) {
var apiKey = req.headers["auth_token"];
if (!apiKey) {
apiKey = url.parse(req.url,true).query["auth_token"];
}
models.user.validateAuthToken(apiKey, function(err, valid) {
//Here is where I know if the auth token is valid and it checks against Mongo, so it's async
});
return [something]; //this is what the validation sequence is expecting this function to do: return true or false
});
How can I solve this issue?
You can't, You might want to open a ticket with swagger to enable async validations.
Are you using Swagger 1.2 or 2.0? If 2.0, you can use swagger-tools to connect middleware for wiring up security handlers for requests based on Swagger documentation. It supports all swagger security mechanism defined in Swagger Specification.
And returning the result of the async function does not work?
wagger.addValidator(
function validate(req, path, httpMethod) {
var apiKey = req.headers["auth_token"];
if (!apiKey) {
apiKey = url.parse(req.url,true).query["auth_token"];
}
return models.user.validateAuthToken(apiKey, function(err, valid) {
//check against mongo
return validation;
});
});