Skip to content

Commit 428e1c8

Browse files
gkorlandleibale
andauthored
Add support for Redis 6 auth pass [user] (#1508)
* Add support for `auth pass user` * fix lint issues * fix typo * fix more lint issues * more lints fixes * reverse password user order * update redis-commands * Update individualCommands.js Clean code * Update individualCommands.js * Update auth.spec.js * Update index.js Co-authored-by: Leibale Eidelman <[email protected]>
1 parent bb208d0 commit 428e1c8

File tree

4 files changed

+26
-9
lines changed

4 files changed

+26
-9
lines changed

Diff for: index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ function RedisClient (options, stream) {
109109
this.closing = false;
110110
this.server_info = {};
111111
this.auth_pass = options.auth_pass || options.password;
112+
this.auth_user = options.auth_user || options.user;
112113
this.selected_db = options.db; // Save the selected db here, used when reconnecting
113114
this.fire_strings = true; // Determine if strings or buffers should be written to the stream
114115
this.pipeline = false;
@@ -240,7 +241,7 @@ RedisClient.prototype.create_stream = function () {
240241
if (this.auth_pass !== undefined) {
241242
this.ready = true;
242243
// Fail silently as we might not be able to connect
243-
this.auth(this.auth_pass, function (err) {
244+
this.auth(this.auth_pass, this.auth_user, function (err) {
244245
if (err && err.code !== 'UNCERTAIN_STATE') {
245246
self.emit('error', err);
246247
}

Diff for: lib/createClient.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@ module.exports = function createClient (port_arg, host_arg, options) {
2929
// [redis:]//[[user][:password]@][host][:port][/db-number][?db=db-number[&password=bar[&option=value]]]
3030
if (parsed.slashes) { // We require slashes
3131
if (parsed.auth) {
32-
options.password = parsed.auth.slice(parsed.auth.indexOf(':') + 1);
32+
var columnIndex = parsed.auth.indexOf(':');
33+
options.password = parsed.auth.slice(columnIndex + 1);
34+
if (columnIndex > 0) {
35+
options.user = parsed.auth.slice(0, columnIndex);
36+
}
3337
}
3438
if (parsed.protocol) {
3539
if (parsed.protocol === 'rediss:') {

Diff for: lib/individualCommands.js

+18-6
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ Multi.prototype.info = Multi.prototype.INFO = function info (section, callback)
180180
return this;
181181
};
182182

183-
function auth_callback (self, pass, callback) {
183+
function auth_callback (self, pass, user, callback) {
184184
return function (err, res) {
185185
if (err) {
186186
if (no_password_is_set.test(err.message)) {
@@ -191,7 +191,7 @@ function auth_callback (self, pass, callback) {
191191
// If redis is still loading the db, it will not authenticate and everything else will fail
192192
debug('Redis still loading, trying to authenticate later');
193193
setTimeout(function () {
194-
self.auth(pass, callback);
194+
self.auth(user, pass, callback);
195195
}, 100);
196196
return;
197197
}
@@ -200,25 +200,37 @@ function auth_callback (self, pass, callback) {
200200
};
201201
}
202202

203-
RedisClient.prototype.auth = RedisClient.prototype.AUTH = function auth (pass, callback) {
203+
RedisClient.prototype.auth = RedisClient.prototype.AUTH = function auth (pass, user, callback) {
204204
debug('Sending auth to ' + this.address + ' id ' + this.connection_id);
205205

206+
// Backward compatibility support for auth with password only
207+
if (user instanceof Function) {
208+
callback = user;
209+
user = null;
210+
}
206211
// Stash auth for connect and reconnect.
207212
this.auth_pass = pass;
213+
this.auth_user = user;
208214
var ready = this.ready;
209215
this.ready = ready || this.offline_queue.length === 0;
210-
var tmp = this.internal_send_command(new Command('auth', [pass], auth_callback(this, pass, callback)));
216+
var tmp = this.internal_send_command(new Command('auth', user ? [user, pass] : [pass], auth_callback(this, pass, user, callback)));
211217
this.ready = ready;
212218
return tmp;
213219
};
214220

215221
// Only works with batch, not in a transaction
216-
Multi.prototype.auth = Multi.prototype.AUTH = function auth (pass, callback) {
222+
Multi.prototype.auth = Multi.prototype.AUTH = function auth (pass, user, callback) {
217223
debug('Sending auth to ' + this.address + ' id ' + this.connection_id);
218224

225+
// Backward compatibility support for auth with password only
226+
if (user instanceof Function) {
227+
callback = user;
228+
user = null;
229+
}
219230
// Stash auth for connect and reconnect.
220231
this.auth_pass = pass;
221-
this.queue.push(new Command('auth', [pass], auth_callback(this._client, callback)));
232+
this.auth_user = user;
233+
this.queue.push(new Command('auth', user ? [user, pass] : [pass], auth_callback(this._client, pass, user, callback)));
222234
return this;
223235
};
224236

Diff for: test/auth.spec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ describe('client authentication', function () {
6161
});
6262
var tmp = client.command_queue.get(0).callback;
6363
client.command_queue.get(0).callback = function (err, res) {
64-
client.auth = function (pass, callback) {
64+
client.auth = function (pass, user, callback) {
6565
callback(null, 'retry worked');
6666
};
6767
tmp(new Error('ERR redis is still LOADING'));

0 commit comments

Comments
 (0)