Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 218
sjt003 Avatar asked May 27 '15 18:05

sjt003


3 Answers

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.

//...

var segments = token.split('.');
if (segments.length !== 3) {
  throw new Error('Not enough or too many segments');
}
like image 78
Pytth Avatar answered Nov 14 '22 21:11

Pytth


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.

like image 6
sjt003 Avatar answered Nov 14 '22 22:11

sjt003


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.

like image 1
Pooja-G Avatar answered Nov 14 '22 23:11

Pooja-G