Skip to content

Commit 239e7a1

Browse files
committed
Add support for auth pass user
1 parent 0041e3e commit 239e7a1

File tree

3 files changed

+39
-8
lines changed

3 files changed

+39
-8
lines changed

index.js

Lines changed: 2 additions & 1 deletion
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;
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
}

lib/createClient.js

Lines changed: 5 additions & 1 deletion
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:') {

lib/individualCommands.js

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,12 @@ 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) {
184+
// Backward compatibility support for auth with password only
185+
if (user instanceof Function){
186+
callback = user;
187+
user = null;
188+
}
184189
return function (err, res) {
185190
if (err) {
186191
if (no_password_is_set.test(err.message)) {
@@ -191,7 +196,7 @@ function auth_callback (self, pass, callback) {
191196
// If redis is still loading the db, it will not authenticate and everything else will fail
192197
debug('Redis still loading, trying to authenticate later');
193198
setTimeout(function () {
194-
self.auth(pass, callback);
199+
self.auth(pass, user, callback);
195200
}, 100);
196201
return;
197202
}
@@ -200,25 +205,46 @@ function auth_callback (self, pass, callback) {
200205
};
201206
}
202207

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

211+
// Backward compatibility support for auth with password only
212+
if (user instanceof Function){
213+
callback = user;
214+
user = null;
215+
}
206216
// Stash auth for connect and reconnect.
207217
this.auth_pass = pass;
218+
this.auth_user = user;
208219
var ready = this.ready;
209220
this.ready = ready || this.offline_queue.length === 0;
210-
var tmp = this.internal_send_command(new Command('auth', [pass], auth_callback(this, pass, callback)));
221+
var tmp;
222+
if(user){
223+
tmp = this.internal_send_command(new Command('auth', [pass, user], auth_callback(this, pass, user, callback)));
224+
} else {
225+
tmp = this.internal_send_command(new Command('auth', [pass], auth_callback(this, pass, callback)));
226+
}
211227
this.ready = ready;
212228
return tmp;
213229
};
214230

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

235+
// Backward compatibility support for auth with password only
236+
if (user instanceof Function){
237+
callback = user;
238+
user = null;
239+
}
219240
// Stash auth for connect and reconnect.
220241
this.auth_pass = pass;
221-
this.queue.push(new Command('auth', [pass], auth_callback(this._client, callback)));
242+
this.auth_user = user;
243+
if(user){
244+
this.queue.push(new Command('auth', [pass, user], auth_callback(this._client, callback)));
245+
} else {
246+
this.queue.push(new Command('auth', [pass], auth_callback(this._client, callback)));
247+
}
222248
return this;
223249
};
224250

0 commit comments

Comments
 (0)