clear password using connection

This commit is contained in:
brianc 2010-10-23 19:02:13 -05:00
parent 34a17fbac8
commit 3662e6f4af
4 changed files with 15 additions and 40 deletions

View File

@ -5,6 +5,7 @@ var EventEmitter = require('events').EventEmitter;
var utils = require(__dirname + '/utils');
var BufferList = require(__dirname + '/buffer-list');
var Connection = require(__dirname + '/connection');
var Client = function(config) {
EventEmitter.call(this);
@ -14,6 +15,7 @@ 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.queryQueue = [];
this.password = config.password || '';
@ -29,33 +31,10 @@ sys.inherits(Client, EventEmitter);
var p = Client.prototype;
p.connect = function() {
if(this.stream.readyState === 'closed'){
this.stream.connect(this.port, this.host);
}
var self = this;
this.stream.on('connect', function() {
var data = ['user',self.user,'database', self.database, '\0'].join('\0');
var byteLength = Buffer.byteLength(data);
var fullBuffer = new Buffer(byteLength + 4);
fullBuffer[0] = 0;
fullBuffer[1] = 3;
fullBuffer[2] = 0;
fullBuffer[3] = 0;
fullBuffer.write(data, 4);
self.send(null, fullBuffer);
});
this.stream.on('data', function(buffer) {
self.setBuffer(buffer);
var msg;
while(msg = self.parseMessage()) {
self.emit('message', msg);
self.emit(msg.name, msg);
}
});
this.on('authenticationCleartextPassword', function() {
self.send('p', new Buffer(self.password + '\0', self.encoding));
var con = this.connection;
con.on('authenticationCleartextPassword', function() {
con.passwordMessage(this.password);
});
this.on('authenticationMD5Password', function(msg) {

View File

@ -56,6 +56,10 @@ p.startupMessage = function(config) {
this.send(false, buffer.join());
};
p.passwordMessage = function(password) {
this.send('p', Buffer(password + '\0', this.encoding));
};
p.send = function(code, bodyBuffer) {
var length = bodyBuffer.length + 4;
var buffer = Buffer(length + (code ? 1 : 0));

View File

@ -6,20 +6,7 @@ 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);
});
client.connection.emit('authenticationCleartextPassword');
test('responds with password', function() {
assert.length(client.stream.packets, 1);
var packet = client.stream.packets[0];

View File

@ -25,6 +25,11 @@ test("sends startup message", function() {
.addCString('').join(true))
});
test('sends passwordMessage', function() {
con.passwordMessage("!");
assert.recieved(stream, new BufferList().addCString("!").join(true,'p'));
});
test('sends query message', function() {
var txt = 'select * from boom';
con.query(txt);