This repository was archived by the owner on Nov 13, 2019. It is now read-only.
File tree 4 files changed +11
-10
lines changed
4 files changed +11
-10
lines changed Original file line number Diff line number Diff line change @@ -106,10 +106,7 @@ Client.prototype.connect = function (callback) {
106
106
107
107
// password request handling
108
108
con . on ( 'authenticationMD5Password' , checkPgPass ( function ( msg ) {
109
- var inner = utils . md5 ( self . password + self . user )
110
- var outer = utils . md5 ( Buffer . concat ( [ Buffer . from ( inner ) , msg . salt ] ) )
111
- var md5password = 'md5' + outer
112
- con . password ( md5password )
109
+ con . password ( utils . postgresMd5PasswordHash ( self . user , self . password , msg . salt ) )
113
110
} ) )
114
111
115
112
con . once ( 'backendKeyData' , function ( msg ) {
Original file line number Diff line number Diff line change @@ -138,12 +138,20 @@ const md5 = function (string) {
138
138
return crypto . createHash ( 'md5' ) . update ( string , 'utf-8' ) . digest ( 'hex' )
139
139
}
140
140
141
+ // See AuthenticationMD5Password at https://www.postgresql.org/docs/current/static/protocol-flow.html
142
+ const postgresMd5PasswordHash = function ( user , password , salt ) {
143
+ var inner = md5 ( password + user )
144
+ var outer = md5 ( Buffer . concat ( [ Buffer . from ( inner ) , salt ] ) )
145
+ return 'md5' + outer
146
+ }
147
+
141
148
module . exports = {
142
149
prepareValue : function prepareValueWrapper ( value ) {
143
150
// this ensures that extra arguments do not get passed into prepareValue
144
151
// by accident, eg: from calling values.map(utils.prepareValue)
145
152
return prepareValue ( value )
146
153
} ,
147
154
normalizeQueryConfig : normalizeQueryConfig ,
155
+ postgresMd5PasswordHash : postgresMd5PasswordHash ,
148
156
md5 : md5
149
157
}
Original file line number Diff line number Diff line change @@ -21,9 +21,7 @@ var connect = function (callback) {
21
21
con . password ( helper . args . password )
22
22
} )
23
23
con . once ( 'authenticationMD5Password' , function ( msg ) {
24
- var inner = utils . md5 ( helper . args . password + helper . args . user )
25
- var outer = utils . md5 ( Buffer . concat ( [ Buffer . from ( inner ) , msg . salt ] ) )
26
- con . password ( 'md5' + outer )
24
+ con . password ( utils . postgresMd5PasswordHash ( helper . args . user , helper . args . password , msg . salt ) ) ;
27
25
} )
28
26
con . once ( 'readyForQuery' , function ( ) {
29
27
con . query ( 'create temp table ids(id integer)' )
Original file line number Diff line number Diff line change @@ -11,9 +11,7 @@ test('md5 authentication', function () {
11
11
test ( 'responds' , function ( ) {
12
12
assert . lengthIs ( client . connection . stream . packets , 1 )
13
13
test ( 'should have correct encrypted data' , function ( ) {
14
- var encrypted = utils . md5 ( client . password + client . user )
15
- encrypted = utils . md5 ( encrypted + salt . toString ( 'binary' ) )
16
- var password = 'md5' + encrypted
14
+ var password = utils . postgresMd5PasswordHash ( client . user , client . password , salt )
17
15
// how do we want to test this?
18
16
assert . equalBuffers ( client . connection . stream . packets [ 0 ] , new BufferList ( )
19
17
. addCString ( password ) . join ( true , 'p' ) )
You can’t perform that action at this time.
0 commit comments