You've already forked node-redis
mirror of
https://github.com/redis/node-redis.git
synced 2025-08-06 02:15:48 +03:00
Unify auth handling
This commit is contained in:
58
index.js
58
index.js
@@ -188,8 +188,8 @@ RedisClient.prototype.create_stream = function () {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Fire the command before redis is connected to be sure it's the first fired command
|
// Fire the command before redis is connected to be sure it's the first fired command
|
||||||
if (typeof this.auth_pass === 'string') {
|
if (this.auth_pass !== undefined) {
|
||||||
this.do_auth();
|
this.auth(this.auth_pass);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -274,29 +274,6 @@ RedisClient.prototype.on_error = function (err) {
|
|||||||
this.connection_gone('error');
|
this.connection_gone('error');
|
||||||
};
|
};
|
||||||
|
|
||||||
var noPasswordIsSet = /no password is set/;
|
|
||||||
|
|
||||||
RedisClient.prototype.do_auth = function () {
|
|
||||||
var self = this;
|
|
||||||
debug('Sending auth to ' + self.address + ' id ' + self.connection_id);
|
|
||||||
|
|
||||||
this.send_anyway = true;
|
|
||||||
this.send_command('auth', [this.auth_pass], function (err, res) {
|
|
||||||
if (err) {
|
|
||||||
if (noPasswordIsSet.test(err.message)) {
|
|
||||||
debug('Warning: Redis server does not require a password, but a password was supplied.');
|
|
||||||
err = null;
|
|
||||||
res = 'OK';
|
|
||||||
} else {
|
|
||||||
self.emit('error', err);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
debug('Auth succeeded ' + self.address + ' id ' + self.connection_id);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
this.send_anyway = false;
|
|
||||||
};
|
|
||||||
|
|
||||||
RedisClient.prototype.on_connect = function () {
|
RedisClient.prototype.on_connect = function () {
|
||||||
debug('Stream connected ' + this.address + ' id ' + this.connection_id);
|
debug('Stream connected ' + this.address + ' id ' + this.connection_id);
|
||||||
|
|
||||||
@@ -1034,18 +1011,29 @@ RedisClient.prototype.callback_emit_error = function (callback, err) {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Stash auth for connect and reconnect. Send immediately if already connected.
|
var noPasswordIsSet = /no password is set/;
|
||||||
RedisClient.prototype.auth = RedisClient.prototype.AUTH = function (pass, callback) {
|
|
||||||
if (typeof pass !== 'string') {
|
RedisClient.prototype.auth = function (pass, callback) {
|
||||||
var err = new Error('The password has to be of type "string"');
|
var self = this;
|
||||||
err.command = 'AUTH';
|
debug('Sending auth to ' + self.address + ' id ' + self.connection_id);
|
||||||
this.callback_emit_error(callback, err);
|
|
||||||
return true;
|
// Stash auth for connect and reconnect.
|
||||||
}
|
|
||||||
this.auth_pass = pass;
|
this.auth_pass = pass;
|
||||||
debug('Saving auth as ' + this.auth_pass);
|
|
||||||
this.send_anyway = true;
|
this.send_anyway = true;
|
||||||
var tmp = this.send_command('auth', [pass], callback);
|
var tmp = this.send_command('auth', [pass], function (err, res) {
|
||||||
|
if (err) {
|
||||||
|
if (noPasswordIsSet.test(err.message)) {
|
||||||
|
self.warn('Warning: Redis server does not require a password, but a password was supplied.');
|
||||||
|
err = null;
|
||||||
|
res = 'OK';
|
||||||
|
} else if (!callback) {
|
||||||
|
self.emit('error', err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (callback) {
|
||||||
|
callback(err, res);
|
||||||
|
}
|
||||||
|
});
|
||||||
this.send_anyway = false;
|
this.send_anyway = false;
|
||||||
return tmp;
|
return tmp;
|
||||||
};
|
};
|
||||||
|
@@ -161,7 +161,7 @@ describe("client authentication", function () {
|
|||||||
client = redis.createClient.apply(redis.createClient, args);
|
client = redis.createClient.apply(redis.createClient, args);
|
||||||
var async = true;
|
var async = true;
|
||||||
client.auth(undefined, function(err, res) {
|
client.auth(undefined, function(err, res) {
|
||||||
assert.strictEqual(err.message, 'The password has to be of type "string"');
|
assert.strictEqual(err.message, 'ERR invalid password');
|
||||||
assert.strictEqual(err.command, 'AUTH');
|
assert.strictEqual(err.command, 'AUTH');
|
||||||
assert.strictEqual(res, undefined);
|
assert.strictEqual(res, undefined);
|
||||||
async = false;
|
async = false;
|
||||||
@@ -175,7 +175,7 @@ describe("client authentication", function () {
|
|||||||
|
|
||||||
client = redis.createClient.apply(redis.createClient, args);
|
client = redis.createClient.apply(redis.createClient, args);
|
||||||
client.on('error', function (err) {
|
client.on('error', function (err) {
|
||||||
assert.strictEqual(err.message, 'The password has to be of type "string"');
|
assert.strictEqual(err.message, 'ERR invalid password');
|
||||||
assert.strictEqual(err.command, 'AUTH');
|
assert.strictEqual(err.command, 'AUTH');
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
|
Reference in New Issue
Block a user