mirror of
https://github.com/brianc/node-postgres.git
synced 2025-12-08 20:16:25 +00:00
got basic password authentication working
This commit is contained in:
parent
f72f996705
commit
406e8a56a9
@ -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();
|
||||
|
||||
@ -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));
|
||||
|
||||
22
test/integration/password-connection-tests.js
Normal file
22
test/integration/password-connection-tests.js
Normal 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();
|
||||
});
|
||||
28
test/unit/authentication-tests.js
Normal file
28
test/unit/authentication-tests.js
Normal 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]);
|
||||
});
|
||||
|
||||
});
|
||||
Loading…
x
Reference in New Issue
Block a user