未处理的拒绝TypeError:无法读取未定义的属性'toString'

时间:2016-01-09 18:08:14

标签: javascript node.js error-handling bluebird

我正在向我的API发送base64编码的令牌。这是谷歌的id令牌。当字符串有效时,一切正常,但我实际上是在尝试设置正确的错误处理。在字符串中添加一些额外的字符会引发TypeError。

我已经从编码和缓冲区记录了结果,并且全部都已定义。任何人都能说出我做错了什么?

function(payload) {
  var config = request.getAsync('https://accounts.google.com/.well-known/openid-configuration')
    .then(function(result) {
      var response = JSON.parse(result.body);
      return request.getAsync(response.jwks_uri);
    })
    .then(function(result) {
      return JSON.parse(result.body);
    })
    .caught(SyntaxError, function(e) {
      throw e;
    })

  var header = new Promise(function(resolve, reject) {
      var segments = payload.id_token.split('.');
      try {
        resolve(JSON.parse(base64urlDecode(segments[0])));
      } catch (e) {
        reject(e);
      }

    })
    .caught(SyntaxError, function(e) {
      throw e;
    })

  return Promise.join(config, header, function(response, header) {
    return new Promise(function(resolve, reject) {
      var key = '';
      for (var i = 0; i < response.keys.length; i++) {
        if (response.keys[i].kid === header.kid) {
          key = response.keys[i];
          break;
        }
      }

      if (jws.verify(payload.id_token, key)) {
        resolve();
      } else {
        reject();
      }
    });
  });
}

function base64urlDecode(str) {
  return new Buffer(base64urlUnescape(str), 'base64').toString();
};

function base64urlUnescape(str) {
  str += Array(5 - str.length % 4).join('=');
  return str.replace(/\-/g, '+').replace(/_/g, '/');
}

0 个答案:

没有答案