md5 password authentication using connect
This commit is contained in:
parent
3662e6f4af
commit
9749ec4fdc
@ -15,8 +15,9 @@ var Client = function(config) {
|
||||
this.port = config.port || 5432;
|
||||
this.host = config.host;
|
||||
this.queryQueue = [];
|
||||
this.connection = new Connection();
|
||||
|
||||
this.stream = config.stream || new net.Stream();
|
||||
this.connection = new Connection({stream: this.stream});
|
||||
this.queryQueue = [];
|
||||
this.password = config.password || '';
|
||||
this.lastBuffer = false;
|
||||
@ -34,15 +35,14 @@ p.connect = function() {
|
||||
var self = this;
|
||||
var con = this.connection;
|
||||
con.on('authenticationCleartextPassword', function() {
|
||||
con.passwordMessage(this.password);
|
||||
con.passwordMessage(self.password);
|
||||
});
|
||||
|
||||
this.on('authenticationMD5Password', function(msg) {
|
||||
var enc = function(string) {
|
||||
return crypto.createHash('md5').update(string).digest('hex');
|
||||
}
|
||||
var md5password = "md5" + enc(enc(self.password + self.user) + msg.salt.toString('binary')) + "\0";
|
||||
self.send('p', new Buffer(md5password, self.encoding));
|
||||
con.on('authenticationMD5Password', function(msg) {
|
||||
var inner = self.md5(self.password + self.user);
|
||||
var outer = self.md5(inner + msg.salt.toString('binary'));
|
||||
var md5password = "md5" + outer;
|
||||
con.passwordMessage(md5password);
|
||||
});
|
||||
|
||||
this.on('readyForQuery', function() {
|
||||
@ -59,6 +59,10 @@ p.connect = function() {
|
||||
});
|
||||
};
|
||||
|
||||
p.md5 = function(string) {
|
||||
return crypto.createHash('md5').update(string).digest('hex');
|
||||
};
|
||||
|
||||
p.send = function(code, bodyBuffer) {
|
||||
var length = bodyBuffer.length + 4;
|
||||
var buffer = Buffer(length + (code ? 1 : 0));
|
||||
|
@ -1,45 +0,0 @@
|
||||
|
||||
require(__dirname+'/test-helper');
|
||||
|
||||
test('password authentication', function(){
|
||||
|
||||
var client = createClient();
|
||||
client.password = "!";
|
||||
|
||||
client.connection.emit('authenticationCleartextPassword');
|
||||
test('responds with password', function() {
|
||||
assert.length(client.stream.packets, 1);
|
||||
var packet = client.stream.packets[0];
|
||||
assert.equalBuffers(packet, [0x70, 0, 0, 0, 6, 33, 0]);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
test('md5 authentication', function() {
|
||||
var client = createClient();
|
||||
client.password = "!";
|
||||
|
||||
var md5PasswordBuffer = Buffer([0x52, 0, 0, 0, 12, 0, 0, 0, 5, 1, 2, 3, 4]);
|
||||
|
||||
var raised = false;
|
||||
|
||||
client.on('authenticationMD5Password', function(msg) {
|
||||
raised = true;
|
||||
assert.equalBuffers(msg.salt, new Buffer([1,2,3,4]));
|
||||
});
|
||||
|
||||
client.stream.emit('data', md5PasswordBuffer);
|
||||
|
||||
test('raises event', function() {
|
||||
assert.ok(raised);
|
||||
});
|
||||
|
||||
test('responds', function() {
|
||||
assert.length(client.stream.packets, 1);
|
||||
test('should have correct encrypted data', function() {
|
||||
//how do we want to test this?
|
||||
return false;
|
||||
});
|
||||
});
|
||||
|
||||
});
|
15
test/unit/client/cleartext-password-tests.js
Normal file
15
test/unit/client/cleartext-password-tests.js
Normal file
@ -0,0 +1,15 @@
|
||||
require(__dirname+'/test-helper');
|
||||
|
||||
test('cleartext password authentication', function(){
|
||||
|
||||
var client = createClient();
|
||||
client.password = "!";
|
||||
client.stream.packets = [];
|
||||
client.connection.emit('authenticationCleartextPassword');
|
||||
test('responds with password', function() {
|
||||
assert.length(client.stream.packets, 1);
|
||||
var packet = client.stream.packets[0];
|
||||
assert.equalBuffers(packet, [0x70, 0, 0, 0, 6, 33, 0]);
|
||||
});
|
||||
|
||||
});
|
20
test/unit/client/md5-password-tests.js
Normal file
20
test/unit/client/md5-password-tests.js
Normal file
@ -0,0 +1,20 @@
|
||||
require(__dirname + '/test-helper')
|
||||
test('md5 authentication', function() {
|
||||
var client = createClient();
|
||||
client.password = "!";
|
||||
var salt = Buffer([1, 2, 3, 4]);
|
||||
client.connection.emit('authenticationMD5Password', {salt: salt});
|
||||
|
||||
test('responds', function() {
|
||||
assert.length(client.stream.packets, 1);
|
||||
test('should have correct encrypted data', function() {
|
||||
var encrypted = client.md5(client.password + client.user);
|
||||
encrypted = client.md5(encrypted + salt.toString('binary'));
|
||||
var password = "md5" + encrypted
|
||||
//how do we want to test this?
|
||||
assert.equalBuffers(client.stream.packets[0], new BufferList()
|
||||
.addCString(password).join(true,'p'))
|
||||
});
|
||||
});
|
||||
|
||||
});
|
Loading…
Reference in New Issue
Block a user