jsonwebtoken包未能验证用户Office.context.mailbox.getUserIdentityToken结果

问题描述 投票:0回答:1

我开发一个的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令牌罚款。 (所以它不是一个真正的配置问题)

jwt office-js office-addins outlook-web-addins office-js-helpers
1个回答
0
投票

现在我已经找到了这个问题的答案。

只是再次重申的问题是:

  1. Office.context.mailbox.getUserIdentityToken方法返回一个JWT令牌。
  2. 当解码该令牌包含一个指向公共证书作为文本的amurl场。
  3. 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`
  }
© www.soinside.com 2019 - 2024. All rights reserved.