Skip to content

Commit 71a66dd

Browse files
committed
Pass connection parameters to password callback
1 parent d8c5bf6 commit 71a66dd

4 files changed

Lines changed: 70 additions & 3 deletions

File tree

packages/pg/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ bench:
2727
@find benchmark -name "*-bench.js" | $(node-command)
2828

2929
test-unit:
30+
@chmod 600 test/unit/client/pgpass.file
3031
@find test/unit -name "*-tests.js" | $(node-command)
3132

3233
test-connection:

packages/pg/lib/client.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ class Client extends EventEmitter {
249249
if (typeof this.password === 'function') {
250250
this._Promise
251251
.resolve()
252-
.then(() => this.password())
252+
.then(() => this.password(this.connectionParameters))
253253
.then((pass) => {
254254
if (pass !== undefined) {
255255
if (typeof pass !== 'string') {
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
'use strict'
2+
const helper = require('./test-helper')
3+
const assert = require('assert')
4+
const suite = new helper.Suite()
5+
const pgpass = require('pgpass')
6+
7+
class Wait {
8+
constructor() {
9+
this.promise = new Promise((resolve) => {
10+
this.resolve = resolve
11+
})
12+
}
13+
14+
until() {
15+
return this.promise
16+
}
17+
18+
done(time) {
19+
if (time) {
20+
setTimeout(this.resolve.bind(this), time)
21+
} else {
22+
this.resolve()
23+
}
24+
}
25+
}
26+
27+
suite.test('password callback is called with conenction params', async function () {
28+
const wait = new Wait()
29+
const client = helper.client({
30+
user: 'foo',
31+
database: 'bar',
32+
host: 'baz',
33+
password: async () => {
34+
wait.done(1)
35+
return 'password'
36+
},
37+
})
38+
client.connection.emit('authenticationCleartextPassword')
39+
await wait.until()
40+
assert.equal(client.user, 'foo')
41+
assert.equal(client.database, 'bar')
42+
assert.equal(client.host, 'baz')
43+
assert.equal(client.connectionParameters.password, 'password')
44+
})
45+
46+
suite.test('cleartext password auth does not crash with null password using pg-pass', async function () {
47+
process.env.PGPASSFILE = `${__dirname}/pgpass.file`
48+
const wait = new Wait()
49+
const client = helper.client({
50+
host: 'foo',
51+
port: 5432,
52+
database: 'bar',
53+
user: 'baz',
54+
password: (params) => {
55+
return new Promise((resolve) => {
56+
pgpass(params, (pass) => {
57+
wait.done(1)
58+
resolve(pass)
59+
})
60+
})
61+
},
62+
})
63+
client.connection.emit('authenticationCleartextPassword')
64+
await wait.until()
65+
assert.equal(client.password, 'quz')
66+
})

packages/pg/test/unit/client/test-helper.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ const helper = require('../test-helper')
33
const Connection = require('../../../lib/connection')
44
const { Client } = helper
55

6-
const makeClient = function () {
6+
const makeClient = function (config) {
77
const connection = new Connection({ stream: 'no' })
88
connection.startup = function () {}
99
connection.connect = function () {}
1010
connection.query = function (text) {
1111
this.queries.push(text)
1212
}
1313
connection.queries = []
14-
const client = new Client({ connection: connection })
14+
const client = new Client({ connection: connection, ...config })
1515
client.connect()
1616
client.connection.emit('connect')
1717
return client

0 commit comments

Comments
 (0)