Skip to content

refactor(NODE-5964): clean up prepareHandshakeDocument #4001

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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');
});
});
});