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

During the initial handshake in which the token and username are passed, I catch 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' }

      

wrong JWT? is the initial token malformed?

+3


source to share


4 answers


As far as I know, this error was the result of an uncaught exception when parsing a JWT that refers to a user no longer in the db - the more common scenario is when the bcrypt comparison or whatever you use finds the hash comparison to be false - this is what I assumed in attention - not find a user I didn't. When I explained this, the error was gone.



+6


source


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



//...

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

      

+3


source


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

// 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");
}

      

Should be:

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

      

0


source


Check if your token or ciphertext has three segments. For example

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

      

If the length of the segments is 3, then the token is correct. But if not, you should check that your token was changed between creation and validation.

0


source







All Articles