3030import java .util .Set ;
3131import java .util .concurrent .TimeUnit ;
3232
33- import static com .mongodb .AuthenticationMechanism .GSSAPI ;
34- import static com .mongodb .AuthenticationMechanism .MONGODB_CR ;
35- import static com .mongodb .AuthenticationMechanism .MONGODB_X509 ;
36- import static com .mongodb .AuthenticationMechanism .PLAIN ;
37- import static com .mongodb .AuthenticationMechanism .SCRAM_SHA_1 ;
3833import static java .lang .String .format ;
3934import static java .util .Arrays .asList ;
4035import static java .util .Collections .singletonList ;
@@ -474,10 +469,6 @@ private ReadPreference createReadPreference(final Map<String, List<String>> opti
474469
475470 private MongoCredential createCredentials (final Map <String , List <String >> optionsMap , final String userName ,
476471 final char [] password ) {
477- if (userName == null ) {
478- return null ;
479- }
480-
481472 AuthenticationMechanism mechanism = null ;
482473 String authSource = (database == null ) ? "admin" : database ;
483474 String gssapiServiceName = null ;
@@ -501,28 +492,38 @@ private MongoCredential createCredentials(final Map<String, List<String>> option
501492 }
502493 }
503494
504- MongoCredential credential ;
505- if (mechanism == GSSAPI ) {
506- credential = MongoCredential .createGSSAPICredential (userName );
507- if (gssapiServiceName != null ) {
508- credential = credential .withMechanismProperty ("SERVICE_NAME" , gssapiServiceName );
495+
496+ MongoCredential credential = null ;
497+ if (mechanism != null ) {
498+ switch (mechanism ) {
499+ case GSSAPI :
500+ credential = MongoCredential .createGSSAPICredential (userName );
501+ if (gssapiServiceName != null ) {
502+ credential = credential .withMechanismProperty ("SERVICE_NAME" , gssapiServiceName );
503+ }
504+ break ;
505+ case PLAIN :
506+ credential = MongoCredential .createPlainCredential (userName , authSource , password );
507+ break ;
508+ case MONGODB_CR :
509+ credential = MongoCredential .createMongoCRCredential (userName , authSource , password );
510+ break ;
511+ case MONGODB_X509 :
512+ credential = MongoCredential .createMongoX509Credential (userName );
513+ break ;
514+ case SCRAM_SHA_1 :
515+ credential = MongoCredential .createScramSha1Credential (userName , authSource , password );
516+ break ;
517+ default :
518+ throw new UnsupportedOperationException (format ("The connection string contains an invalid authentication mechanism'. "
519+ + "'%s' is not a supported authentication mechanism" ,
520+ mechanism ));
509521 }
510- } else if (mechanism == PLAIN ) {
511- credential = MongoCredential .createPlainCredential (userName , authSource , password );
512- } else if (mechanism == MONGODB_CR ) {
513- credential = MongoCredential .createMongoCRCredential (userName , authSource , password );
514- } else if (mechanism == MONGODB_X509 ) {
515- credential = MongoCredential .createMongoX509Credential (userName );
516- } else if (mechanism == SCRAM_SHA_1 ) {
517- credential = MongoCredential .createScramSha1Credential (userName , authSource , password );
518- } else if (mechanism == null ) {
522+ } else if (userName != null ) {
519523 credential = MongoCredential .createCredential (userName , authSource , password );
520- } else {
521- throw new UnsupportedOperationException (format ("The connection string contains an invalid authentication mechanism'. "
522- + "'%s' is not a supported authentication mechanism" , mechanism ));
523524 }
524525
525- if (authMechanismProperties != null ) {
526+ if (credential != null && authMechanismProperties != null ) {
526527 for (String part : authMechanismProperties .split ("," )) {
527528 String [] mechanismPropertyKeyValue = part .split (":" );
528529 if (mechanismPropertyKeyValue .length != 2 ) {
0 commit comments