1
1
var crypto = require ( 'crypto' ) ;
2
2
var EventEmitter = require ( 'events' ) . EventEmitter ;
3
3
var util = require ( 'util' ) ;
4
+ var pgPass = require ( 'pgpass' ) ;
4
5
5
6
var ConnectionParameters = require ( __dirname + '/connection-parameters' ) ;
6
7
var Query = require ( __dirname + '/query' ) ;
@@ -38,6 +39,7 @@ util.inherits(Client, EventEmitter);
38
39
Client . prototype . connect = function ( callback ) {
39
40
var self = this ;
40
41
var con = this . connection ;
42
+
41
43
if ( this . host && this . host . indexOf ( '/' ) === 0 ) {
42
44
con . connect ( this . host + '/.s.PGSQL.' + this . port ) ;
43
45
} else {
@@ -64,18 +66,33 @@ Client.prototype.connect = function(callback) {
64
66
} ) ;
65
67
} ) ;
66
68
69
+ function checkPgPass ( cb ) {
70
+ return function ( msg ) {
71
+ if ( null !== self . password ) {
72
+ cb ( msg ) ;
73
+ } else {
74
+ pgPass ( self . connectionParameters , function ( pass ) {
75
+ if ( undefined !== pass ) {
76
+ self . connectionParameters . password = self . password = pass ;
77
+ }
78
+ cb ( msg ) ;
79
+ } ) ;
80
+ }
81
+ } ;
82
+ }
83
+
67
84
//password request handling
68
- con . on ( 'authenticationCleartextPassword' , function ( ) {
85
+ con . on ( 'authenticationCleartextPassword' , checkPgPass ( function ( ) {
69
86
con . password ( self . password ) ;
70
- } ) ;
87
+ } ) ) ;
71
88
72
89
//password request handling
73
- con . on ( 'authenticationMD5Password' , function ( msg ) {
90
+ con . on ( 'authenticationMD5Password' , checkPgPass ( function ( msg ) {
74
91
var inner = Client . md5 ( self . password + self . user ) ;
75
92
var outer = Client . md5 ( inner + msg . salt . toString ( 'binary' ) ) ;
76
93
var md5password = "md5" + outer ;
77
94
con . password ( md5password ) ;
78
- } ) ;
95
+ } ) ) ;
79
96
80
97
con . once ( 'backendKeyData' , function ( msg ) {
81
98
self . processID = msg . processID ;
0 commit comments