Skip to content

Commit

Permalink
feat: Add dynamic retrieval for client password
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
sehrope committed Jul 17, 2019
1 parent 0acaf9d commit 354b19f
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion lib/client.js
Expand Up @@ -114,7 +114,26 @@ Client.prototype._connect = function (callback) {

function checkPgPass (cb) {
return function (msg) {
if (self.password !== null) {
if (typeof(self.password) === 'function') {
try {
self._Promise.resolve(self.password())
.then(pass => {
if (undefined !== pass) {
if (typeof(pass) !== 'string') {
con.emit('error', new Error('Password must be a string'));
return
}
self.connectionParameters.password = self.password = pass
}
cb(msg)
})
.catch(err => {
con.emit('error', err)
})
} catch (err) {
con.emit('error', err)
}
} else if (self.password !== null) {
cb(msg)
} else {
pgPass(self.connectionParameters, function (pass) {
Expand Down

0 comments on commit 354b19f

Please sign in to comment.