@@ -19,7 +19,7 @@ type CryptoMethod = 'sha1' | 'sha256';
19
19
class ScramSHA extends AuthProvider {
20
20
cryptoMethod : CryptoMethod ;
21
21
randomBytesAsync : ( size : number ) => Promise < Buffer > ;
22
- constructor ( cryptoMethod : CryptoMethod ) {
22
+ constructor ( cryptoMethod : CryptoMethod , private saslPrep : typeof saslprep = saslprep ) {
23
23
super ( ) ;
24
24
this . cryptoMethod = cryptoMethod || 'sha1' ;
25
25
this . randomBytesAsync = promisify ( crypto . randomBytes ) ;
@@ -34,7 +34,7 @@ class ScramSHA extends AuthProvider {
34
34
if ( ! credentials ) {
35
35
throw new MongoMissingCredentialsError ( 'AuthContext must provide credentials.' ) ;
36
36
}
37
- if ( cryptoMethod === 'sha256' && saslprep == null ) {
37
+ if ( cryptoMethod === 'sha256' && this . saslPrep == null ) {
38
38
emitWarning ( 'Warning: no saslprep library specified. Passwords will not be sanitized' ) ;
39
39
}
40
40
@@ -59,10 +59,11 @@ class ScramSHA extends AuthProvider {
59
59
return continueScramConversation (
60
60
this . cryptoMethod ,
61
61
response . speculativeAuthenticate ,
62
- authContext
62
+ authContext ,
63
+ this . saslPrep
63
64
) ;
64
65
}
65
- return executeScram ( this . cryptoMethod , authContext ) ;
66
+ return executeScram ( this . cryptoMethod , authContext , this . saslPrep ) ;
66
67
}
67
68
}
68
69
@@ -103,7 +104,11 @@ function makeFirstMessage(
103
104
} ;
104
105
}
105
106
106
- async function executeScram ( cryptoMethod : CryptoMethod , authContext : AuthContext ) : Promise < void > {
107
+ async function executeScram (
108
+ cryptoMethod : CryptoMethod ,
109
+ authContext : AuthContext ,
110
+ saslPrep : typeof saslprep
111
+ ) : Promise < void > {
107
112
const { connection, credentials } = authContext ;
108
113
if ( ! credentials ) {
109
114
throw new MongoMissingCredentialsError ( 'AuthContext must provide credentials.' ) ;
@@ -116,13 +121,14 @@ async function executeScram(cryptoMethod: CryptoMethod, authContext: AuthContext
116
121
117
122
const saslStartCmd = makeFirstMessage ( cryptoMethod , credentials , nonce ) ;
118
123
const response = await connection . commandAsync ( ns ( `${ db } .$cmd` ) , saslStartCmd , undefined ) ;
119
- await continueScramConversation ( cryptoMethod , response , authContext ) ;
124
+ await continueScramConversation ( cryptoMethod , response , authContext , saslPrep ) ;
120
125
}
121
126
122
127
async function continueScramConversation (
123
128
cryptoMethod : CryptoMethod ,
124
129
response : Document ,
125
- authContext : AuthContext
130
+ authContext : AuthContext ,
131
+ saslPrep : typeof saslprep
126
132
) : Promise < void > {
127
133
const connection = authContext . connection ;
128
134
const credentials = authContext . credentials ;
@@ -140,7 +146,8 @@ async function continueScramConversation(
140
146
141
147
let processedPassword ;
142
148
if ( cryptoMethod === 'sha256' ) {
143
- processedPassword = 'kModuleError' in saslprep ? password : saslprep ( password ) ;
149
+ processedPassword =
150
+ 'kModuleError' in saslPrep || typeof saslPrep !== 'function' ? password : saslPrep ( password ) ;
144
151
} else {
145
152
processedPassword = passwordDigest ( username , password ) ;
146
153
}
0 commit comments