@@ -41,16 +41,28 @@ import {
41
41
} from './wire_protocol/constants' ;
42
42
43
43
/** @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
+ }
54
66
55
67
/** @public */
56
68
export type Stream = Socket | TLSSocket ;
@@ -109,15 +121,6 @@ async function performInitialHandshake(
109
121
) : Promise < void > {
110
122
const credentials = options . credentials ;
111
123
112
- if ( credentials ) {
113
- if (
114
- ! ( credentials . mechanism === AuthMechanism . MONGODB_DEFAULT ) &&
115
- ! AUTH_PROVIDERS . get ( credentials . mechanism )
116
- ) {
117
- throw new MongoInvalidArgumentError ( `AuthMechanism '${ credentials . mechanism } ' not supported` ) ;
118
- }
119
- }
120
-
121
124
const authContext = new AuthContext ( conn , credentials , options ) ;
122
125
conn . authContext = authContext ;
123
126
@@ -167,7 +170,7 @@ async function performInitialHandshake(
167
170
authContext . response = response ;
168
171
169
172
const resolvedCredentials = credentials . resolveAuthMechanism ( response ) ;
170
- const provider = AUTH_PROVIDERS . get ( resolvedCredentials . mechanism ) ;
173
+ const provider = getAuthProvider ( resolvedCredentials . mechanism ) ;
171
174
if ( ! provider ) {
172
175
throw new MongoInvalidArgumentError (
173
176
`No AuthProvider for ${ resolvedCredentials . mechanism } defined.`
@@ -229,16 +232,10 @@ export async function prepareHandshakeDocument(
229
232
if ( credentials . mechanism === AuthMechanism . MONGODB_DEFAULT && credentials . username ) {
230
233
handshakeDoc . saslSupportedMechs = `${ credentials . source } .${ credentials . username } ` ;
231
234
232
- const provider = AUTH_PROVIDERS . get ( AuthMechanism . MONGODB_SCRAM_SHA256 ) ;
233
- if ( ! provider ) {
234
- // This auth mechanism is always present.
235
- throw new MongoInvalidArgumentError (
236
- `No AuthProvider for ${ AuthMechanism . MONGODB_SCRAM_SHA256 } defined.`
237
- ) ;
238
- }
235
+ const provider = getAuthProvider ( AuthMechanism . MONGODB_SCRAM_SHA256 ) ;
239
236
return provider . prepare ( handshakeDoc , authContext ) ;
240
237
}
241
- const provider = AUTH_PROVIDERS . get ( credentials . mechanism ) ;
238
+ const provider = getAuthProvider ( credentials . mechanism ) ;
242
239
if ( ! provider ) {
243
240
throw new MongoInvalidArgumentError ( `No AuthProvider for ${ credentials . mechanism } defined.` ) ;
244
241
}
0 commit comments