我使用Mosca作为嵌入式MQTT经纪人与蚊子后端。我主要使用Mosca作为auth机制,它可以读取jwt令牌并从中提取一些值。我创建了自己的授权程序,在mqtt密码字段中读取JWT令牌并解码/验证它。解码后,我将解码后的字段添加到客户端对象中,如下所示
authenticate(client, username, password, next) {
try {
const decoded_token = jwt.verify(password.toString(), process.env.SECRET_KEY_BASE);
client.decoded_token = decoded_token;
next(null, true);
} catch(error) {
console.log('name: %s, error: %s', error.name, error.message);
console.log('unauthorized!');
next(null, false);
}
}
我想访问decoded_token
回调中的published
值,如下所示
broker.on('published', (packet, client) => {
console.log(client.decoded_token);
console.log('Published', packet.topic, packet.payload);
});
不知何故,上面的代码不能按预期工作。我收到以下错误:
TypeError:无法读取未定义的属性'decoding_token'
然而,如果我使用console.log(client.decoded_token)
代替console.log(client)
,它可以工作,我可以在客户端对象中看到decoded_token
值。
我有两个问题。
我是MQTT的新手所以如果问题太天真,请原谅我:)
client.decoded_token
如果您使用vs代码或Webstorm进行开发,则可以使用内置调试器来调试和打印客户端对象。