javascript Express JWT 错误:socket.io 初始身份验证中的段不够或太多

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30490066/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 12:15:28  来源:igfitidea点击:

Express JWT Error: Not enough or too many segments in socket.io initial auth

javascriptauthenticationsocket.iojwtexpress-jwt

提问by sjt003

During the initial handshake where a token and username are passed, I am catching this strange error--

在传递令牌和用户名的初始握手期间,我发现了这个奇怪的错误——

    { handle: 10,
      type: 'error',
      className: 'Error',
      constructorFunction: { ref: 11 },
      protoObject: { ref: 12 },
      prototypeObject: { ref: 3 },
      properties: 
      [ { name: 'stack',
          attributes: 2,
          propertyType: 3,
          ref: 3 },
        { name: 'arguments',
          attributes: 2,
          propertyType: 1,
          ref: 3 },
        { name: 'type',
          attributes: 2,
          propertyType: 1,
          ref: 3 },
        { name: 'message',
          attributes: 2,
          propertyType: 1,
          ref: 13 } ],
        text: 'Error: Not enough or too many segments' }

malformed JWT? initial token malformed?

JWT 格式错误?初始令牌格式错误?

回答by Pytth

If you are using JWT-simple, by looking at the source code, we can see that this error is caused by the token having an incorrect form.

如果您使用的是 JWT-simple,通过查看源代码,我们可以看到此错误是由令牌格式不正确引起的。

//...

var segments = token.split('.');
if (segments.length !== 3) {
  throw new Error('Not enough or too many segments');
}

回答by sjt003

To the best of my knowledge this error was a result of an uncaught exception on parsing a JWT that references a user no longer in the db--the more common scenario is when bcrypt compare or whatever you are using finds the comparison of hash to be false--this I had taken into account--not finding a user I did not. When I accounted for this the error disappeared.

据我所知,此错误是由于解析 JWT 时出现未捕获的异常导致的,该异常引用不再存在于 db 中的用户——更常见的情况是当 bcrypt compare 或您使用的任何内容发现散列的比较为错误——这是我已经考虑到的——没有找到我没有找到的用户。当我考虑到这一点时,错误消失了。

回答by Gabriel Kunkel

This happened to me in my angular application when I passed a messed up callback into my "then" statement.

当我将一个混乱的回调传递到我的“then”语句中时,这发生在我的 angular 应用程序中。

// in my Auth Service

this.register = function (email, password) {
  return $http.post(API_URL + 'register', {
    email: email,
    password: password
  }).then(authSuccessful)
    .catch(authError);
};

function authSuccessful(res) {
  alert('success', 'Joy!', 'Welcome, ' + res.data.user.email + '.');
  // authToken.setToken just puts the token in local storage.
  authToken.setToken(res.token); // <- WRONG!!
  $state.go("connections");
}

It should have been:

本来应该是:

function authSuccessful(res) {
  alert('success', 'Joy!', 'Welcome, ' + res.data.user.email + '.');
  authToken.setToken(res.data.token); // <- Yay!
  $state.go("connections");
}

回答by Pooja-G

Check whether your token or encrypted text having three segment. For Ex.

检查您的令牌或加密文本是否具有三段。对于前。

var segments = token.split('.');

If segments length is 3 then token is proper. But If not you must check your token has been modified in between creation and validate.

如果段长度为 3,则令牌是正确的。但如果不是,您必须检查您的令牌在创建和验证之间是否已被修改。