Skip to content

Commit

Permalink
refactor(NODE-5964): clean up prepareHandshakeDocument (#4001)
Browse files Browse the repository at this point in the history
  • Loading branch information
alenakhineika committed Feb 23, 2024
1 parent ff8b5f5 commit 233a2e0
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/cmap/connect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import {
MongoRuntimeError,
needsRetryableWriteLabel
} from '../error';
import { type MongoClientAuthProviders } from '../mongo_client_auth_providers';
import { HostAddress, ns, promiseWithResolvers } from '../utils';
import { AuthContext } from './auth/auth_provider';
import { AuthMechanism } from './auth/providers';
Expand Down Expand Up @@ -102,7 +101,7 @@ export async function performInitialHandshake(
const authContext = new AuthContext(conn, credentials, options);
conn.authContext = authContext;

const handshakeDoc = await prepareHandshakeDocument(authContext, options.authProviders);
const handshakeDoc = await prepareHandshakeDocument(authContext);

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

const provider = authProviders.getOrCreateProvider(AuthMechanism.MONGODB_SCRAM_SHA256);
const provider = authContext.options.authProviders.getOrCreateProvider(
AuthMechanism.MONGODB_SCRAM_SHA256
);
if (!provider) {
// This auth mechanism is always present.
throw new MongoInvalidArgumentError(
Expand All @@ -228,7 +228,7 @@ export async function prepareHandshakeDocument(
}
return provider.prepare(handshakeDoc, authContext);
}
const provider = authProviders.getOrCreateProvider(credentials.mechanism);
const provider = authContext.options.authProviders.getOrCreateProvider(credentials.mechanism);
if (!provider) {
throw new MongoInvalidArgumentError(`No AuthProvider for ${credentials.mechanism} defined.`);
}
Expand Down
2 changes: 2 additions & 0 deletions test/unit/mongo_client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1196,7 +1196,9 @@ describe('MongoClient', function () {
client.s.authProviders.getOrCreateProvider('NOT_SUPPORTED');
} catch (error) {
expect(error).to.be.an.instanceof(MongoInvalidArgumentError);
return;
}
expect.fail('missed exception');
});
});
});

0 comments on commit 233a2e0

Please sign in to comment.