From 406e8a56a92da8ac650a95bca47928d6014f4854 Mon Sep 17 00:00:00 2001 From: brianc Date: Tue, 19 Oct 2010 23:29:23 -0500 Subject: [PATCH] got basic password authentication working --- lib/client.js | 14 ++++++++++ lib/parser.js | 12 ++++---- test/integration/password-connection-tests.js | 22 +++++++++++++++ test/unit/authentication-tests.js | 28 +++++++++++++++++++ 4 files changed, 69 insertions(+), 7 deletions(-) create mode 100644 test/integration/password-connection-tests.js create mode 100644 test/unit/authentication-tests.js diff --git a/lib/client.js b/lib/client.js index cc570f3d..ed8ac769 100644 --- a/lib/client.js +++ b/lib/client.js @@ -14,6 +14,7 @@ var Client = function(config) { this.queryQueue = []; this.stream = config.stream || new net.Stream(); this.queryQueue = []; + this.password = config.password || ''; }; sys.inherits(Client, EventEmitter); @@ -51,6 +52,19 @@ p.connect = function() { } }); + this.on('authenticationCleartextPassword', function() { + console.log(self); + var stringBuffer = new Buffer(self.password + '\0', 'utf8'); + var buffer = Buffer(stringBuffer.length + 5); + buffer[0] = 0x70; + buffer[1] = 0; + buffer[2] = 0; + buffer[3] = 0; + buffer[4] = buffer.length - 1; + buffer.write(self.password, 5) + self.stream.write(buffer); + }); + this.on('readyForQuery', function() { self.readyForQuery = true; self.pulseQueryQueue(); diff --git a/lib/parser.js b/lib/parser.js index f94032ee..a9921845 100644 --- a/lib/parser.js +++ b/lib/parser.js @@ -53,14 +53,12 @@ p.parseMessage = function() { }; p.parseR = function(msg) { + var code = 0; if(msg.length == 8) { - msg.code = this.parseInt32(); - } - if(msg.code == 0) { - return msg; - } - if(msg.code == 3) { - msg.name = 'authenticationCleartextPassword'; + code = this.parseInt32(); + if(code == 3) { + msg.name = 'authenticationCleartextPassword'; + } return msg; } throw new Error("Unknown authenticatinOk message type" + sys.inspect(msg)); diff --git a/test/integration/password-connection-tests.js b/test/integration/password-connection-tests.js new file mode 100644 index 00000000..b2270b1d --- /dev/null +++ b/test/integration/password-connection-tests.js @@ -0,0 +1,22 @@ +require(__dirname + '/test-helper'); +var client = new Client({ + database: 'postgres', + user: 'user_pw', + password: 'pass' +}); + + +client.connect(); +client.on('message', function(msg) { + console.log('message: ' + msg.name); +}); + +var query = client.query('select * from pg_type'); + +query.on('row', function() { + console.log('row'); +}); + +query.on('end', function() { + client.disconnect(); +}); diff --git a/test/unit/authentication-tests.js b/test/unit/authentication-tests.js new file mode 100644 index 00000000..8439456b --- /dev/null +++ b/test/unit/authentication-tests.js @@ -0,0 +1,28 @@ +require(__dirname+'/test-helper'); + +test('password authentication', function(){ + + var client = createClient(); + client.password = "!"; + + var clearTextPasswordBuffer = Buffer([0x52, 0, 0, 0, 8, 0, 0, 0, 3]); + + var raised = false; + + client.on('authenticationCleartextPassword', function() { + raised = true; + }); + + client.stream.emit('data', clearTextPasswordBuffer); + + test('raises event', function() { + assert.ok(raised); + }); + + 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]); + }); + +});