I'm using amqplib in my NodeJS project.
Instead of username:password
I'm using JWT authentication. Which is basically an empty username and JWT token as a password. The rest is handled by the MQ server.
Auth works fine but when the JWT token expires RabbitMQ server returns an error Uauthorized
and the token is expired message.
The problem is that I cannot use one open connection for a long time and neither I can extend it. The only option worked for me so far is to close open channels and reconnect again with the new token. It works but is quite tedious work since I need to subscribe to queues and events again.
I checked the lib and cannot find a way to pass dynamic credentials. Do you know any better approach for this?
This is how I connect to MQ
import * as RMQ from 'amqplib';
const options = { hostname: '127.0.0.1' };
const NO_USER = '';
const token = 'AAA.BBB.CCC';
const credentials = RMQ.credentials.plain(NO_USER, token);
const connection = {
value: null,
channel: null,
};
if (connection.value) connection.value.close();
connection.value = await RMQ.connect(options, { credentials });
What I'm looking for is to update credentials via method maybe? To use the same socket/channel but update credentials? like connection.updateSecret(...);
I know that such functionality exists in Ruby Bunny package as you can see it here https://github.com/ruby-amqp/bunny/blob/7ef5fbee17b383d39d4c31a15714ea893cf17d23/lib/bunny/session.rb#L354