@@ -41,16 +41,28 @@ import {
4141} from './wire_protocol/constants' ;
4242
4343/** @internal */
44- export const AUTH_PROVIDERS = new Map < AuthMechanism | string , AuthProvider > ( [
45- [ AuthMechanism . MONGODB_AWS , new MongoDBAWS ( ) ] ,
46- [ AuthMechanism . MONGODB_CR , new MongoCR ( ) ] ,
47- [ AuthMechanism . MONGODB_GSSAPI , new GSSAPI ( ) ] ,
48- [ AuthMechanism . MONGODB_OIDC , new MongoDBOIDC ( ) ] ,
49- [ AuthMechanism . MONGODB_PLAIN , new Plain ( ) ] ,
50- [ AuthMechanism . MONGODB_SCRAM_SHA1 , new ScramSHA1 ( ) ] ,
51- [ AuthMechanism . MONGODB_SCRAM_SHA256 , new ScramSHA256 ( ) ] ,
52- [ AuthMechanism . MONGODB_X509 , new X509 ( ) ]
53- ] ) ;
44+ export function getAuthProvider ( name : AuthMechanism | string ) : AuthProvider {
45+ switch ( name ) {
46+ case AuthMechanism . MONGODB_AWS :
47+ return new MongoDBAWS ( ) ;
48+ case AuthMechanism . MONGODB_CR :
49+ return new MongoCR ( ) ;
50+ case AuthMechanism . MONGODB_GSSAPI :
51+ return new GSSAPI ( ) ;
52+ case AuthMechanism . MONGODB_OIDC :
53+ return new MongoDBOIDC ( ) ;
54+ case AuthMechanism . MONGODB_PLAIN :
55+ return new Plain ( ) ;
56+ case AuthMechanism . MONGODB_SCRAM_SHA1 :
57+ return new ScramSHA1 ( ) ;
58+ case AuthMechanism . MONGODB_SCRAM_SHA256 :
59+ return new ScramSHA256 ( ) ;
60+ case AuthMechanism . MONGODB_X509 :
61+ return new X509 ( ) ;
62+ default :
63+ throw new MongoInvalidArgumentError ( `No auth provider found for type ${ name } ` ) ;
64+ }
65+ }
5466
5567/** @public */
5668export type Stream = Socket | TLSSocket ;
@@ -108,15 +120,6 @@ export async function performInitialHandshake(
108120) : Promise < void > {
109121 const credentials = options . credentials ;
110122
111- if ( credentials ) {
112- if (
113- ! ( credentials . mechanism === AuthMechanism . MONGODB_DEFAULT ) &&
114- ! AUTH_PROVIDERS . get ( credentials . mechanism )
115- ) {
116- throw new MongoInvalidArgumentError ( `AuthMechanism '${ credentials . mechanism } ' not supported` ) ;
117- }
118- }
119-
120123 const authContext = new AuthContext ( conn , credentials , options ) ;
121124 conn . authContext = authContext ;
122125
@@ -166,7 +169,7 @@ export async function performInitialHandshake(
166169 authContext . response = response ;
167170
168171 const resolvedCredentials = credentials . resolveAuthMechanism ( response ) ;
169- const provider = AUTH_PROVIDERS . get ( resolvedCredentials . mechanism ) ;
172+ const provider = getAuthProvider ( resolvedCredentials . mechanism ) ;
170173 if ( ! provider ) {
171174 throw new MongoInvalidArgumentError (
172175 `No AuthProvider for ${ resolvedCredentials . mechanism } defined.`
@@ -232,16 +235,10 @@ export async function prepareHandshakeDocument(
232235 if ( credentials . mechanism === AuthMechanism . MONGODB_DEFAULT && credentials . username ) {
233236 handshakeDoc . saslSupportedMechs = `${ credentials . source } .${ credentials . username } ` ;
234237
235- const provider = AUTH_PROVIDERS . get ( AuthMechanism . MONGODB_SCRAM_SHA256 ) ;
236- if ( ! provider ) {
237- // This auth mechanism is always present.
238- throw new MongoInvalidArgumentError (
239- `No AuthProvider for ${ AuthMechanism . MONGODB_SCRAM_SHA256 } defined.`
240- ) ;
241- }
238+ const provider = getAuthProvider ( AuthMechanism . MONGODB_SCRAM_SHA256 ) ;
242239 return provider . prepare ( handshakeDoc , authContext ) ;
243240 }
244- const provider = AUTH_PROVIDERS . get ( credentials . mechanism ) ;
241+ const provider = getAuthProvider ( credentials . mechanism ) ;
245242 if ( ! provider ) {
246243 throw new MongoInvalidArgumentError ( `No AuthProvider for ${ credentials . mechanism } defined.` ) ;
247244 }
0 commit comments