Skip to content

Commit 354b19f

Browse files
committed
feat: Add dynamic retrieval for client password
Adds option to specify a function for a client password. When the client is connected, if the value of password is a function then it is invoked to get the password to use for that connection. The function must return either a string or a Promise that resolves to a string. If the function throws or rejects with an error then it will be bubbled up to the client.
1 parent 0acaf9d commit 354b19f

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

lib/client.js

+20-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,26 @@ Client.prototype._connect = function (callback) {
114114

115115
function checkPgPass (cb) {
116116
return function (msg) {
117-
if (self.password !== null) {
117+
if (typeof(self.password) === 'function') {
118+
try {
119+
self._Promise.resolve(self.password())
120+
.then(pass => {
121+
if (undefined !== pass) {
122+
if (typeof(pass) !== 'string') {
123+
con.emit('error', new Error('Password must be a string'));
124+
return
125+
}
126+
self.connectionParameters.password = self.password = pass
127+
}
128+
cb(msg)
129+
})
130+
.catch(err => {
131+
con.emit('error', err)
132+
})
133+
} catch (err) {
134+
con.emit('error', err)
135+
}
136+
} else if (self.password !== null) {
118137
cb(msg)
119138
} else {
120139
pgPass(self.connectionParameters, function (pass) {

0 commit comments

Comments
 (0)