Skip to content

Commit 233a2e0

Browse files
refactor(NODE-5964): clean up prepareHandshakeDocument (#4001)
1 parent ff8b5f5 commit 233a2e0

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

src/cmap/connect.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import {
1616
MongoRuntimeError,
1717
needsRetryableWriteLabel
1818
} from '../error';
19-
import { type MongoClientAuthProviders } from '../mongo_client_auth_providers';
2019
import { HostAddress, ns, promiseWithResolvers } from '../utils';
2120
import { AuthContext } from './auth/auth_provider';
2221
import { AuthMechanism } from './auth/providers';
@@ -102,7 +101,7 @@ export async function performInitialHandshake(
102101
const authContext = new AuthContext(conn, credentials, options);
103102
conn.authContext = authContext;
104103

105-
const handshakeDoc = await prepareHandshakeDocument(authContext, options.authProviders);
104+
const handshakeDoc = await prepareHandshakeDocument(authContext);
106105

107106
// @ts-expect-error: TODO(NODE-5141): The options need to be filtered properly, Connection options differ from Command options
108107
const handshakeOptions: CommandOptions = { ...options };
@@ -196,8 +195,7 @@ export interface HandshakeDocument extends Document {
196195
* This function is only exposed for testing purposes.
197196
*/
198197
export async function prepareHandshakeDocument(
199-
authContext: AuthContext,
200-
authProviders: MongoClientAuthProviders
198+
authContext: AuthContext
201199
): Promise<HandshakeDocument> {
202200
const options = authContext.options;
203201
const compressors = options.compressors ? options.compressors : [];
@@ -219,7 +217,9 @@ export async function prepareHandshakeDocument(
219217
if (credentials.mechanism === AuthMechanism.MONGODB_DEFAULT && credentials.username) {
220218
handshakeDoc.saslSupportedMechs = `${credentials.source}.${credentials.username}`;
221219

222-
const provider = authProviders.getOrCreateProvider(AuthMechanism.MONGODB_SCRAM_SHA256);
220+
const provider = authContext.options.authProviders.getOrCreateProvider(
221+
AuthMechanism.MONGODB_SCRAM_SHA256
222+
);
223223
if (!provider) {
224224
// This auth mechanism is always present.
225225
throw new MongoInvalidArgumentError(
@@ -228,7 +228,7 @@ export async function prepareHandshakeDocument(
228228
}
229229
return provider.prepare(handshakeDoc, authContext);
230230
}
231-
const provider = authProviders.getOrCreateProvider(credentials.mechanism);
231+
const provider = authContext.options.authProviders.getOrCreateProvider(credentials.mechanism);
232232
if (!provider) {
233233
throw new MongoInvalidArgumentError(`No AuthProvider for ${credentials.mechanism} defined.`);
234234
}

test/unit/mongo_client.test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1196,7 +1196,9 @@ describe('MongoClient', function () {
11961196
client.s.authProviders.getOrCreateProvider('NOT_SUPPORTED');
11971197
} catch (error) {
11981198
expect(error).to.be.an.instanceof(MongoInvalidArgumentError);
1199+
return;
11991200
}
1201+
expect.fail('missed exception');
12001202
});
12011203
});
12021204
});

0 commit comments

Comments
 (0)