我正在开发Outlook Web加载项。
我正在尝试验证通过node.js
库传递给服务器端的令牌,但失败了,我不明白为什么。
这就是我检索用户身份令牌的方法。
Office.context.mailbox.getUserIdentityTokenAsync(function(result) {
result.value // contains the token.
// send this value to server side, which I can see that it's working.
})
在服务器端,我检索令牌并执行以下操作:
token; // contains the token passed from the web-app.
const jwt = require('jsonwebtoken');
const request = require('request');
let decoded = jwt.decode(token, {complete: true});
// Get the key, we'll need this later since we'll have to
// find the matching key from the config file.
let key = decoded.header.x5t;
let amurl = JSON.parse(decoded.payload.appctx).amurl;
// Make a request to the url to get the configuration json file.
request(amurl, {}, (err, response, body) => {
let keys = JSON.parse(body).keys;
// Filter the keys so we get the one which we can verify.
let s = keys.filter(t => t.keyinfo.x5t === key);
let cert = s[0].keyvalue.value;
// Fails the verification.
console.log(jwt.verify(token, cert));
});
请澄清一下,我正在正确检索令牌,并且此npm软件包对于其他jwt令牌似乎运行正常。 (因此,这实际上不是配置问题)
答案 0 :(得分:0)
我现在找到了这个问题的答案。
再次重申该问题是:
Office.context.mailbox.getUserIdentityToken
方法返回jwt令牌。amurl
字段,该字段指向公共证书作为文本。 jsonwebtoken.verify(token, certText)
时,失败,并显示消息invalid algorithm
(即使您从令牌的标题中指定了算法)问题是证书文本的格式。 jsonwebtoken
软件包正在寻找一种特殊的格式(每行以64个字符分隔,并带有证书开始和证书结束行,因此,使用下面的方法进行格式化时-它开始正常工作。
原始代码是从这里获取的:https://github.com/auth0/node-jsonwebtoken/issues/68,并略微格式化以适应需要。
/**
* @param {string} key - The certificate value retrieved from amurl property.
*/
formatKey: function(key) {
const beginKey = "-----BEGIN CERTIFICATE-----";
const endKey = "-----END CERTIFICATE-----";
const sanitizedKey = key.replace(beginKey, '').replace(endKey, '').replace('\n', '')
const keyArray = sanitizedKey.split('').map((l, i) => {
const position = i + 1
const isLastCharacter = sanitizedKey.length === position
if(position % 64 === 0 || isLastCharacter) {
return l + '\n'
}
return l
})
return `${beginKey}\n${keyArray.join('')}${endKey}\n`
}