Skip to content

Commit

Permalink
Merge pull request redis#848 from fintura/new_auth
Browse files Browse the repository at this point in the history
Fix auth callback being called more than once
  • Loading branch information
BridgeAR committed Sep 17, 2015
2 parents 13635c9 + d61d97e commit 2578aba
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 16 deletions.
27 changes: 11 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,38 +50,30 @@ function RedisClient(stream, options) {
this.options.socket_keepalive = true;
}
this.should_buffer = false;
this.command_queue_high_water = this.options.command_queue_high_water || 1000;
this.command_queue_low_water = this.options.command_queue_low_water || 0;
this.command_queue_high_water = options.command_queue_high_water || 1000;
this.command_queue_low_water = options.command_queue_low_water || 0;
this.max_attempts = +options.max_attempts || 0;
this.command_queue = new Queue(); // holds sent commands to de-pipeline them
this.offline_queue = new Queue(); // holds commands issued but not able to be sent
this.commands_sent = 0;
this.connect_timeout = +options.connect_timeout || 86400000; // 24 * 60 * 60 * 1000 ms
this.enable_offline_queue = true;
if (this.options.enable_offline_queue === false) {
if (options.enable_offline_queue === false) {
this.enable_offline_queue = false;
}
this.retry_max_delay = null;
if (options.retry_max_delay && options.retry_max_delay > 0) {
this.retry_max_delay = +options.retry_max_delay;
}
this.retry_max_delay = +options.retry_max_delay || null;
this.initialize_retry_vars();
this.pub_sub_mode = false;
this.subscription_set = {};
this.monitoring = false;
this.closing = false;
this.server_info = {};
this.auth_pass = null;
if (options.auth_pass !== undefined) {
this.auth_pass = options.auth_pass;
}
this.auth_pass = options.auth_pass;
this.parser_module = null;
this.selected_db = null; // save the selected db here, used when reconnecting

this.selected_db = null; // save the selected db here, used when reconnecting
this.old_state = null;

this.install_stream_listeners();

events.EventEmitter.call(this);
}
util.inherits(RedisClient, events.EventEmitter);
Expand Down Expand Up @@ -199,6 +191,7 @@ RedisClient.prototype.do_auth = function () {
} else if (self.auth_callback) {
self.auth_callback(err);
self.auth_callback = null;
return;
} else {
self.emit("error", err);
return;
Expand Down Expand Up @@ -928,10 +921,12 @@ RedisClient.prototype.auth = RedisClient.prototype.AUTH = function (pass, callba
return;
}
this.auth_pass = pass;
this.auth_callback = callback;
debug("Saving auth as " + this.auth_pass);
// Only run the callback once. So do not safe it if already connected
if (this.connected) {
this.send_command("auth", pass, callback);
this.send_command("auth", [this.auth_pass], callback);
} else {
this.auth_callback = callback;
}
};

Expand Down
12 changes: 12 additions & 0 deletions test/auth.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,18 @@ describe("client authentication", function () {
});
client.auth(234567);
});

it('allows auth to be provided post-hoc with auth method again', function (done) {
if (helper.redisProcess().spawnFailed()) this.skip();

var args = config.configureClient(parser, ip, {
auth_pass: auth
});
client = redis.createClient.apply(redis.createClient, args);
client.on("ready", function () {
client.auth(auth, helper.isString('OK', done));
});
});
});
});

Expand Down

0 comments on commit 2578aba

Please sign in to comment.