[英]Async validation function for Swagger + Node.js
So, I'm using Node.js + Swagger + MongoDB. 因此,我正在使用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. 我正在尝试验证所有请求都带有有效的auth_token,这是在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. 我的问题是Swagger支持的验证器需要返回true或false,但是由于我必须对Mongo进行检查以验证auth令牌,因此整个验证将变得异步。
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? 我该如何解决这个问题?
您不能,您可能想要张开一张票以启用异步验证。
Are you using Swagger 1.2 or 2.0? 您使用的是Swagger 1.2还是2.0? If 2.0, you can use swagger-tools to connect middleware for wiring up security handlers for requests based on Swagger documentation. 如果是2.0,则可以使用swagger-tools连接中间件,以为基于Swagger文档的请求连接安全处理程序。 It supports all swagger security mechanism defined in Swagger Specification . 它支持Swagger Specification中定义的所有swagger安全机制。
And returning the result of the async function does not work? 并且返回异步函数的结果不起作用?
swagger.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;
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.