From 239e7a1f151d00ef6615d7bc290c5c3b0970b77f Mon Sep 17 00:00:00 2001 From: Guy Korland Date: Tue, 31 Mar 2020 02:18:49 +0300 Subject: [PATCH 01/11] Add support for `auth pass user` --- index.js | 3 ++- lib/createClient.js | 6 +++++- lib/individualCommands.js | 38 ++++++++++++++++++++++++++++++++------ 3 files changed, 39 insertions(+), 8 deletions(-) diff --git a/index.js b/index.js index 439c784c8d9..1b63055995e 100644 --- a/index.js +++ b/index.js @@ -109,6 +109,7 @@ function RedisClient (options, stream) { this.closing = false; this.server_info = {}; this.auth_pass = options.auth_pass || options.password; + this.auth_user = options.auth_user; this.selected_db = options.db; // Save the selected db here, used when reconnecting this.fire_strings = true; // Determine if strings or buffers should be written to the stream this.pipeline = false; @@ -240,7 +241,7 @@ RedisClient.prototype.create_stream = function () { if (this.auth_pass !== undefined) { this.ready = true; // Fail silently as we might not be able to connect - this.auth(this.auth_pass, function (err) { + this.auth(this.auth_pass, this.auth_user, function (err) { if (err && err.code !== 'UNCERTAIN_STATE') { self.emit('error', err); } diff --git a/lib/createClient.js b/lib/createClient.js index 1533d73d1b7..304e243c203 100644 --- a/lib/createClient.js +++ b/lib/createClient.js @@ -29,7 +29,11 @@ module.exports = function createClient (port_arg, host_arg, options) { // [redis:]//[[user][:password]@][host][:port][/db-number][?db=db-number[&password=bar[&option=value]]] if (parsed.slashes) { // We require slashes if (parsed.auth) { - options.password = parsed.auth.slice(parsed.auth.indexOf(':') + 1); + var columnIndex = parsed.auth.indexOf(':'); + options.password = parsed.auth.slice(columnIndex + 1); + if(columnIndex>0){ + options.user = parsed.auth.slice(0, columnIndex); + } } if (parsed.protocol) { if (parsed.protocol === 'rediss:') { diff --git a/lib/individualCommands.js b/lib/individualCommands.js index d366b642502..fc52536a21c 100644 --- a/lib/individualCommands.js +++ b/lib/individualCommands.js @@ -180,7 +180,12 @@ Multi.prototype.info = Multi.prototype.INFO = function info (section, callback) return this; }; -function auth_callback (self, pass, callback) { +function auth_callback (self, pass, user, callback) { + // Backward compatibility support for auth with password only + if (user instanceof Function){ + callback = user; + user = null; + } return function (err, res) { if (err) { if (no_password_is_set.test(err.message)) { @@ -191,7 +196,7 @@ function auth_callback (self, pass, callback) { // If redis is still loading the db, it will not authenticate and everything else will fail debug('Redis still loading, trying to authenticate later'); setTimeout(function () { - self.auth(pass, callback); + self.auth(pass, user, callback); }, 100); return; } @@ -200,25 +205,46 @@ function auth_callback (self, pass, callback) { }; } -RedisClient.prototype.auth = RedisClient.prototype.AUTH = function auth (pass, callback) { +RedisClient.prototype.auth = RedisClient.prototype.AUTH = function auth (pass, user, callback) { debug('Sending auth to ' + this.address + ' id ' + this.connection_id); + // Backward compatibility support for auth with password only + if (user instanceof Function){ + callback = user; + user = null; + } // Stash auth for connect and reconnect. this.auth_pass = pass; + this.auth_user = user; var ready = this.ready; this.ready = ready || this.offline_queue.length === 0; - var tmp = this.internal_send_command(new Command('auth', [pass], auth_callback(this, pass, callback))); + var tmp; + if(user){ + tmp = this.internal_send_command(new Command('auth', [pass, user], auth_callback(this, pass, user, callback))); + } else { + tmp = this.internal_send_command(new Command('auth', [pass], auth_callback(this, pass, callback))); + } this.ready = ready; return tmp; }; // Only works with batch, not in a transaction -Multi.prototype.auth = Multi.prototype.AUTH = function auth (pass, callback) { +Multi.prototype.auth = Multi.prototype.AUTH = function auth (pass, user, callback) { debug('Sending auth to ' + this.address + ' id ' + this.connection_id); + // Backward compatibility support for auth with password only + if (user instanceof Function){ + callback = user; + user = null; + } // Stash auth for connect and reconnect. this.auth_pass = pass; - this.queue.push(new Command('auth', [pass], auth_callback(this._client, callback))); + this.auth_user = user; + if(user){ + this.queue.push(new Command('auth', [pass, user], auth_callback(this._client, callback))); + } else { + this.queue.push(new Command('auth', [pass], auth_callback(this._client, callback))); + } return this; }; From 11adc29549b7529e7293ffa2f17d6d776f0a819d Mon Sep 17 00:00:00 2001 From: Guy Korland Date: Tue, 31 Mar 2020 17:33:17 +0300 Subject: [PATCH 02/11] fix lint issues --- lib/createClient.js | 10 +++++----- lib/individualCommands.js | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/createClient.js b/lib/createClient.js index 304e243c203..b969bc2149f 100644 --- a/lib/createClient.js +++ b/lib/createClient.js @@ -29,11 +29,11 @@ module.exports = function createClient (port_arg, host_arg, options) { // [redis:]//[[user][:password]@][host][:port][/db-number][?db=db-number[&password=bar[&option=value]]] if (parsed.slashes) { // We require slashes if (parsed.auth) { - var columnIndex = parsed.auth.indexOf(':'); - options.password = parsed.auth.slice(columnIndex + 1); - if(columnIndex>0){ - options.user = parsed.auth.slice(0, columnIndex); - } + var columnIndex = parsed.auth.indexOf(':'); + options.password = parsed.auth.slice(columnIndex + 1); + if (columnIndex > 0){ + options.user = parsed.auth.slice(0, columnIndex); + } } if (parsed.protocol) { if (parsed.protocol === 'rediss:') { diff --git a/lib/individualCommands.js b/lib/individualCommands.js index fc52536a21c..7d5fb2a69b9 100644 --- a/lib/individualCommands.js +++ b/lib/individualCommands.js @@ -181,8 +181,8 @@ Multi.prototype.info = Multi.prototype.INFO = function info (section, callback) }; function auth_callback (self, pass, user, callback) { - // Backward compatibility support for auth with password only - if (user instanceof Function){ + // Backward compatibility support for auth with password on + if (user instanceof Function){ callback = user; user = null; } From 27195ef0e534cb1702d49074cbd35eb79765a24d Mon Sep 17 00:00:00 2001 From: Guy Korland Date: Tue, 31 Mar 2020 17:33:40 +0300 Subject: [PATCH 03/11] fix typo --- lib/individualCommands.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/individualCommands.js b/lib/individualCommands.js index 7d5fb2a69b9..543f648a727 100644 --- a/lib/individualCommands.js +++ b/lib/individualCommands.js @@ -181,7 +181,7 @@ Multi.prototype.info = Multi.prototype.INFO = function info (section, callback) }; function auth_callback (self, pass, user, callback) { - // Backward compatibility support for auth with password on + // Backward compatibility support for auth with password only if (user instanceof Function){ callback = user; user = null; From f142ecc389476b263f220aa34e5dba70e5fda61e Mon Sep 17 00:00:00 2001 From: Guy Korland Date: Tue, 31 Mar 2020 17:37:09 +0300 Subject: [PATCH 04/11] fix more lint issues --- lib/individualCommands.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/individualCommands.js b/lib/individualCommands.js index 543f648a727..4af63f47cf5 100644 --- a/lib/individualCommands.js +++ b/lib/individualCommands.js @@ -182,9 +182,9 @@ Multi.prototype.info = Multi.prototype.INFO = function info (section, callback) function auth_callback (self, pass, user, callback) { // Backward compatibility support for auth with password only - if (user instanceof Function){ - callback = user; - user = null; + if (user instanceof Function) { + callback = user; + user = null; } return function (err, res) { if (err) { From f946e13af0f5cb6655c5fb21e07bb03226ee4803 Mon Sep 17 00:00:00 2001 From: Guy Korland Date: Tue, 31 Mar 2020 17:43:02 +0300 Subject: [PATCH 05/11] more lints fixes --- lib/createClient.js | 2 +- lib/individualCommands.js | 36 ++++++++++++++++++------------------ 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/lib/createClient.js b/lib/createClient.js index b969bc2149f..b03bb575399 100644 --- a/lib/createClient.js +++ b/lib/createClient.js @@ -31,7 +31,7 @@ module.exports = function createClient (port_arg, host_arg, options) { if (parsed.auth) { var columnIndex = parsed.auth.indexOf(':'); options.password = parsed.auth.slice(columnIndex + 1); - if (columnIndex > 0){ + if (columnIndex > 0) { options.user = parsed.auth.slice(0, columnIndex); } } diff --git a/lib/individualCommands.js b/lib/individualCommands.js index 4af63f47cf5..43fd619a2b8 100644 --- a/lib/individualCommands.js +++ b/lib/individualCommands.js @@ -185,7 +185,7 @@ function auth_callback (self, pass, user, callback) { if (user instanceof Function) { callback = user; user = null; - } + } return function (err, res) { if (err) { if (no_password_is_set.test(err.message)) { @@ -208,21 +208,21 @@ function auth_callback (self, pass, user, callback) { RedisClient.prototype.auth = RedisClient.prototype.AUTH = function auth (pass, user, callback) { debug('Sending auth to ' + this.address + ' id ' + this.connection_id); - // Backward compatibility support for auth with password only - if (user instanceof Function){ - callback = user; - user = null; - } + // Backward compatibility support for auth with password only + if (user instanceof Function) { + callback = user; + user = null; + } // Stash auth for connect and reconnect. this.auth_pass = pass; this.auth_user = user; var ready = this.ready; this.ready = ready || this.offline_queue.length === 0; var tmp; - if(user){ - tmp = this.internal_send_command(new Command('auth', [pass, user], auth_callback(this, pass, user, callback))); + if (user) { + tmp = this.internal_send_command(new Command('auth', [pass, user], auth_callback(this, pass, user, callback))); } else { - tmp = this.internal_send_command(new Command('auth', [pass], auth_callback(this, pass, callback))); + tmp = this.internal_send_command(new Command('auth', [pass], auth_callback(this, pass, callback))); } this.ready = ready; return tmp; @@ -232,18 +232,18 @@ RedisClient.prototype.auth = RedisClient.prototype.AUTH = function auth (pass, u Multi.prototype.auth = Multi.prototype.AUTH = function auth (pass, user, callback) { debug('Sending auth to ' + this.address + ' id ' + this.connection_id); - // Backward compatibility support for auth with password only - if (user instanceof Function){ - callback = user; - user = null; - } + // Backward compatibility support for auth with password only + if (user instanceof Function) { + callback = user; + user = null; + } // Stash auth for connect and reconnect. this.auth_pass = pass; - this.auth_user = user; - if(user){ - this.queue.push(new Command('auth', [pass, user], auth_callback(this._client, callback))); + this.auth_user = user; + if (user) { + this.queue.push(new Command('auth', [pass, user], auth_callback(this._client, callback))); } else { - this.queue.push(new Command('auth', [pass], auth_callback(this._client, callback))); + this.queue.push(new Command('auth', [pass], auth_callback(this._client, callback))); } return this; }; From 1aa87e6048c58c2d77b00ded878fb9c3861a748b Mon Sep 17 00:00:00 2001 From: Guy Korland Date: Fri, 17 Apr 2020 01:01:30 +0300 Subject: [PATCH 06/11] reverse password user order --- index.js | 4 ++-- lib/individualCommands.js | 12 ++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/index.js b/index.js index 1b63055995e..f1adacefbc5 100644 --- a/index.js +++ b/index.js @@ -109,7 +109,7 @@ function RedisClient (options, stream) { this.closing = false; this.server_info = {}; this.auth_pass = options.auth_pass || options.password; - this.auth_user = options.auth_user; + this.auth_user = options.auth_user || options.user; this.selected_db = options.db; // Save the selected db here, used when reconnecting this.fire_strings = true; // Determine if strings or buffers should be written to the stream this.pipeline = false; @@ -241,7 +241,7 @@ RedisClient.prototype.create_stream = function () { if (this.auth_pass !== undefined) { this.ready = true; // Fail silently as we might not be able to connect - this.auth(this.auth_pass, this.auth_user, function (err) { + this.auth(this.auth_user, this.auth_pass, function (err) { if (err && err.code !== 'UNCERTAIN_STATE') { self.emit('error', err); } diff --git a/lib/individualCommands.js b/lib/individualCommands.js index 43fd619a2b8..47a5f991d4a 100644 --- a/lib/individualCommands.js +++ b/lib/individualCommands.js @@ -180,7 +180,7 @@ Multi.prototype.info = Multi.prototype.INFO = function info (section, callback) return this; }; -function auth_callback (self, pass, user, callback) { +function auth_callback (self, user, pass, callback) { // Backward compatibility support for auth with password only if (user instanceof Function) { callback = user; @@ -196,7 +196,7 @@ function auth_callback (self, pass, user, callback) { // If redis is still loading the db, it will not authenticate and everything else will fail debug('Redis still loading, trying to authenticate later'); setTimeout(function () { - self.auth(pass, user, callback); + self.auth(user, pass, callback); }, 100); return; } @@ -205,7 +205,7 @@ function auth_callback (self, pass, user, callback) { }; } -RedisClient.prototype.auth = RedisClient.prototype.AUTH = function auth (pass, user, callback) { +RedisClient.prototype.auth = RedisClient.prototype.AUTH = function auth (user, pass, callback) { debug('Sending auth to ' + this.address + ' id ' + this.connection_id); // Backward compatibility support for auth with password only @@ -220,7 +220,7 @@ RedisClient.prototype.auth = RedisClient.prototype.AUTH = function auth (pass, u this.ready = ready || this.offline_queue.length === 0; var tmp; if (user) { - tmp = this.internal_send_command(new Command('auth', [pass, user], auth_callback(this, pass, user, callback))); + tmp = this.internal_send_command(new Command('auth', [user, pass], auth_callback(this, user, pass, callback))); } else { tmp = this.internal_send_command(new Command('auth', [pass], auth_callback(this, pass, callback))); } @@ -229,7 +229,7 @@ RedisClient.prototype.auth = RedisClient.prototype.AUTH = function auth (pass, u }; // Only works with batch, not in a transaction -Multi.prototype.auth = Multi.prototype.AUTH = function auth (pass, user, callback) { +Multi.prototype.auth = Multi.prototype.AUTH = function auth (user, pass, callback) { debug('Sending auth to ' + this.address + ' id ' + this.connection_id); // Backward compatibility support for auth with password only @@ -241,7 +241,7 @@ Multi.prototype.auth = Multi.prototype.AUTH = function auth (pass, user, callbac this.auth_pass = pass; this.auth_user = user; if (user) { - this.queue.push(new Command('auth', [pass, user], auth_callback(this._client, callback))); + this.queue.push(new Command('auth', [user, pass], auth_callback(this._client, callback))); } else { this.queue.push(new Command('auth', [pass], auth_callback(this._client, callback))); } From 20fb4ffe9a0a45f924c33e9f00104b1623d1b078 Mon Sep 17 00:00:00 2001 From: Guy Korland Date: Sat, 19 Sep 2020 21:45:54 +0300 Subject: [PATCH 07/11] update redis-commands --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 75489c3ec44..c9d17647f50 100644 --- a/package.json +++ b/package.json @@ -38,7 +38,7 @@ }, "dependencies": { "denque": "^1.4.1", - "redis-commands": "^1.5.0", + "redis-commands": "^1.6.0", "redis-errors": "^1.2.0", "redis-parser": "^3.0.0" }, From 8c898fbef1d6efa1e590ea13c0a62ecf337ea574 Mon Sep 17 00:00:00 2001 From: Leibale Eidelman Date: Mon, 8 Mar 2021 16:49:10 -0500 Subject: [PATCH 08/11] Update individualCommands.js Clean code --- lib/individualCommands.js | 21 ++++++--------------- 1 file changed, 6 insertions(+), 15 deletions(-) diff --git a/lib/individualCommands.js b/lib/individualCommands.js index 47a5f991d4a..091f63131d8 100644 --- a/lib/individualCommands.js +++ b/lib/individualCommands.js @@ -180,12 +180,7 @@ Multi.prototype.info = Multi.prototype.INFO = function info (section, callback) return this; }; -function auth_callback (self, user, pass, callback) { - // Backward compatibility support for auth with password only - if (user instanceof Function) { - callback = user; - user = null; - } +function auth_callback (self, pass, user, callback) { return function (err, res) { if (err) { if (no_password_is_set.test(err.message)) { @@ -205,7 +200,7 @@ function auth_callback (self, user, pass, callback) { }; } -RedisClient.prototype.auth = RedisClient.prototype.AUTH = function auth (user, pass, callback) { +RedisClient.prototype.auth = RedisClient.prototype.AUTH = function auth (pass, user, callback) { debug('Sending auth to ' + this.address + ' id ' + this.connection_id); // Backward compatibility support for auth with password only @@ -220,16 +215,16 @@ RedisClient.prototype.auth = RedisClient.prototype.AUTH = function auth (user, p this.ready = ready || this.offline_queue.length === 0; var tmp; if (user) { - tmp = this.internal_send_command(new Command('auth', [user, pass], auth_callback(this, user, pass, callback))); + tmp = this.internal_send_command(new Command('auth', [user, pass], auth_callback(this, pass, user, callback))); } else { - tmp = this.internal_send_command(new Command('auth', [pass], auth_callback(this, pass, callback))); + tmp = this.internal_send_command(new Command('auth', [pass], auth_callback(this, pass, user, callback))); } this.ready = ready; return tmp; }; // Only works with batch, not in a transaction -Multi.prototype.auth = Multi.prototype.AUTH = function auth (user, pass, callback) { +Multi.prototype.auth = Multi.prototype.AUTH = function auth (pass, user, callback) { debug('Sending auth to ' + this.address + ' id ' + this.connection_id); // Backward compatibility support for auth with password only @@ -240,11 +235,7 @@ Multi.prototype.auth = Multi.prototype.AUTH = function auth (user, pass, callbac // Stash auth for connect and reconnect. this.auth_pass = pass; this.auth_user = user; - if (user) { - this.queue.push(new Command('auth', [user, pass], auth_callback(this._client, callback))); - } else { - this.queue.push(new Command('auth', [pass], auth_callback(this._client, callback))); - } + this.queue.push(new Command('auth', user ? [user, pass] : [pass], auth_callback(this._client, pass, user, callback))); return this; }; From 61fb85caf058e91eace20a590a08351ec51b8d31 Mon Sep 17 00:00:00 2001 From: Leibale Eidelman Date: Mon, 8 Mar 2021 16:55:35 -0500 Subject: [PATCH 09/11] Update individualCommands.js --- lib/individualCommands.js | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/lib/individualCommands.js b/lib/individualCommands.js index 34a6ee9a2ba..4d6f0a70942 100644 --- a/lib/individualCommands.js +++ b/lib/individualCommands.js @@ -213,12 +213,7 @@ RedisClient.prototype.auth = RedisClient.prototype.AUTH = function auth (pass, u this.auth_user = user; var ready = this.ready; this.ready = ready || this.offline_queue.length === 0; - var tmp; - if (user) { - tmp = this.internal_send_command(new Command('auth', [user, pass], auth_callback(this, pass, user, callback))); - } else { - tmp = this.internal_send_command(new Command('auth', [pass], auth_callback(this, pass, user, callback))); - } + var tmp = this.internal_send_command(new Command('auth', user ? [user, pass] : [pass], auth_callback(this, pass, user, callback))); this.ready = ready; return tmp; }; From fc67ed3c396a9ea081bb45ecee7cd863b6fefdff Mon Sep 17 00:00:00 2001 From: Leibale Eidelman Date: Mon, 8 Mar 2021 16:56:18 -0500 Subject: [PATCH 10/11] Update auth.spec.js --- test/auth.spec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/auth.spec.js b/test/auth.spec.js index 995d98136b3..d1b596e5ae3 100644 --- a/test/auth.spec.js +++ b/test/auth.spec.js @@ -61,7 +61,7 @@ describe('client authentication', function () { }); var tmp = client.command_queue.get(0).callback; client.command_queue.get(0).callback = function (err, res) { - client.auth = function (pass, callback) { + client.auth = function (pass, user, callback) { callback(null, 'retry worked'); }; tmp(new Error('ERR redis is still LOADING')); From ff84f690817770df749ac67fcd003ff13bb5c6fc Mon Sep 17 00:00:00 2001 From: Leibale Eidelman Date: Wed, 17 Mar 2021 19:18:44 -0400 Subject: [PATCH 11/11] Update index.js --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index f1adacefbc5..fe79c5f3935 100644 --- a/index.js +++ b/index.js @@ -241,7 +241,7 @@ RedisClient.prototype.create_stream = function () { if (this.auth_pass !== undefined) { this.ready = true; // Fail silently as we might not be able to connect - this.auth(this.auth_user, this.auth_pass, function (err) { + this.auth(this.auth_pass, this.auth_user, function (err) { if (err && err.code !== 'UNCERTAIN_STATE') { self.emit('error', err); }