got basic password authentication working

This commit is contained in:
brianc 2010-10-19 23:29:23 -05:00
parent f72f996705
commit 406e8a56a9
4 changed files with 69 additions and 7 deletions

View File

@ -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();

View File

@ -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));

View File

@ -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();
});

View File

@ -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]);
});
});