Changing Consumer usernames

Hey all!
I have a node API that hits my kong layer whenever an actor in our system is updated. Im having trouble for the use case in which and actor is trying to update the username to a previously held name.

ie: user ‘Steve’ changes his name to ‘Steve1’, and then decides to change it back to ‘Steve’. But kong throws an error saying that it is unable to generate a new JWT for that consumer as the key ‘Steve’ is already in use. Im able to modify the consumer object itself with a new value, but once I try to return to an older, previously used but no longer active value, I am unable. Thoughts?

Here is a log of my authentication api during the above process. as you can see the ‘Steve’ consumer is created, and then once the username is updated to Steve1, it proceeds as normal, changing the value. But then a subsequent request to change the username back to ‘Steve’ results in this error being through of { key: 'already exists with value \'Steve\'' } and then the remainder of the promise chain involved with creating a JWT rejects.

Ive toyed with trying to delete that specific key at various points in the chain of my promise, but it always seems to end up deleting the consumer Im trying to update.

this is my request to kong to modify the user and is the point of failure in my patch route for modifying consumers.

  return new Promise(function(resolve,reject) {
    if (useKong === 'true') {
      request(
        {
          url: kongAdminUrl+'/consumers/'+obj.consumerId,
          method: 'PATCH',
          json: {
            'username': obj.userName,
            'custom_id': obj.spid
          }
        }, function(err,response,body) {
          if (err) reject(err);
          else {
            if (isDefined(body) && (typeof body) == 'string') body = JSON.parse(body);
            if (isDefined(body.id)) {
              obj.consumerId = body.id;
              console.log('Updated consumer \''+obj.userName+'\'');
              resolve(obj);
            } else {
              console.log('ERROR updating consumer \''+obj.userName+'\'');
              reject(new Error('failed to update kong consumer \''+obj.userName+'\''));
            }
          }
        }
      );
    } else {
      // Not using kong
      resolve(obj);
    }
  });
}```

Try using the “Update consumer associated to a specific plugin” in lieu of updating the consumer object directly

Okay so I think Ive figured out that the consumer is actually being updated, but the next step where Im trying to add a JWT is failing. Kong is finding the old JWT with the key of the old username thats trying to be added again, seeing that old JWT and not allowing two JWT’s with the same key to be added. So it seems like I need to delete the old JWT when updating the consumer.

function addConsumerJwtToken(obj) {
  return new Promise(function(resolve,reject) {
    if (useKong === 'true') {
      request(
        {
          url: kongAdminUrl+'/consumers/'+obj.consumerId+'/jwt',
          method: 'POST',
          json: {
            'algorithm':'HS256',
            'key': obj.userName,
            'secret': obj.secret
          }
        }, function(err,response,body) {
          if (err) reject(err);
          else {
            if (isDefined(body) && (typeof body) == 'string') body = JSON.parse(body);
            if (isDefined(body.id)) {
              obj.jwtId = body.id;
              console.log('Added JWT token for consumer \''+obj.userName+'\'');
              resolve(obj);
            } else {
              console.log('ERROR adding JWT token for consumer \''+obj.userName+'\'');
              //console.log(obj);
              //console.log(response);
              reject(new Error('failed to create kong jwt for \''+obj.userName+'\''));
            }
          }
        }
      );
    } else {
      // Not using kong
      resolve(obj);
    }
  });
}

I’ve thrown a deleteConsumerJWT in my promise chain to handle this before trying to add the new JWT, but to no avail. Still get the key already exists error, though my delete function seems to be functioning as expected and does not return any errors upon the delete request…

function deleteConsumerJwtToken(obj, oldJwt) {
  return new Promise(function(resolve,reject) {
    if (useKong === 'true') {
      request(
        {
          url: kongAdminUrl+'/consumers/'+obj.consumerId+'/jwt'/oldJwt,
          method: 'DELETE',
        }, function(err,response,body) {
          if (err) reject(err);
          else {
            console.log('Deleted consumer jwt ' + oldJwt);
            resolve(obj);
          }
        }
      );
    } else {
      // Not using kong
      resolve(obj);
    }
  });
}

© 2018 Kong Inc.    Terms  •  Privacy  •  FAQ