当我发布到服务器时,无论我给 auth 函数提供什么信息,它都会返回 true。我的预感是我正在尝试同步做一些事情,这本质上是异步的,但我不知道如何解决它。
auth = (username, api_key, device) ->
hashed_key = hash.sha256(username + api_key + device, salt)
winston.debug('Checking auth for ' + username)
redis_client.get hashed_key, (err, data) ->
if data == username
true
# Main Handler for posting data for a device.
server.post "/:customer/:site/:device", create = (req, res, next) ->
message = JSON.parse(req.body)
winston.info(server.name + ': Recieved event from ' + req.params.device)
authenticated = auth(message.username, message.api_key, message.device)
winston.debug('****' + authenticated)
if authenticated == true
winston.debug('Auth passed, got a valid user/device/api combination: ' + message.username)
redis_client.publish('device_events', req.body)
return next()
else
winston.debug('Auth failed, cant find device ' + message.device + ' for ' + message.username)
return next(restify.NotAuthorizedError)