From 9749ec4fdc84d5199e32fb32a68a94acd92243eb Mon Sep 17 00:00:00 2001 From: brianc Date: Sat, 23 Oct 2010 19:21:01 -0500 Subject: [PATCH] md5 password authentication using connect --- lib/client.js | 20 +++++---- test/unit/client/authentication-tests.js | 45 -------------------- test/unit/client/cleartext-password-tests.js | 15 +++++++ test/unit/client/md5-password-tests.js | 20 +++++++++ 4 files changed, 47 insertions(+), 53 deletions(-) delete mode 100644 test/unit/client/authentication-tests.js create mode 100644 test/unit/client/cleartext-password-tests.js create mode 100644 test/unit/client/md5-password-tests.js diff --git a/lib/client.js b/lib/client.js index 7f9b74e4..f1d7ce32 100644 --- a/lib/client.js +++ b/lib/client.js @@ -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)); diff --git a/test/unit/client/authentication-tests.js b/test/unit/client/authentication-tests.js deleted file mode 100644 index 85e43bd8..00000000 --- a/test/unit/client/authentication-tests.js +++ /dev/null @@ -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; - }); - }); - -}); diff --git a/test/unit/client/cleartext-password-tests.js b/test/unit/client/cleartext-password-tests.js new file mode 100644 index 00000000..0f147a75 --- /dev/null +++ b/test/unit/client/cleartext-password-tests.js @@ -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]); + }); + +}); diff --git a/test/unit/client/md5-password-tests.js b/test/unit/client/md5-password-tests.js new file mode 100644 index 00000000..70d3a18f --- /dev/null +++ b/test/unit/client/md5-password-tests.js @@ -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')) + }); + }); + +});