30
30
import java .util .Set ;
31
31
import java .util .concurrent .TimeUnit ;
32
32
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 ;
38
33
import static java .lang .String .format ;
39
34
import static java .util .Arrays .asList ;
40
35
import static java .util .Collections .singletonList ;
@@ -474,10 +469,6 @@ private ReadPreference createReadPreference(final Map<String, List<String>> opti
474
469
475
470
private MongoCredential createCredentials (final Map <String , List <String >> optionsMap , final String userName ,
476
471
final char [] password ) {
477
- if (userName == null ) {
478
- return null ;
479
- }
480
-
481
472
AuthenticationMechanism mechanism = null ;
482
473
String authSource = (database == null ) ? "admin" : database ;
483
474
String gssapiServiceName = null ;
@@ -501,28 +492,38 @@ private MongoCredential createCredentials(final Map<String, List<String>> option
501
492
}
502
493
}
503
494
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 ));
509
521
}
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 ) {
519
523
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 ));
523
524
}
524
525
525
- if (authMechanismProperties != null ) {
526
+ if (credential != null && authMechanismProperties != null ) {
526
527
for (String part : authMechanismProperties .split ("," )) {
527
528
String [] mechanismPropertyKeyValue = part .split (":" );
528
529
if (mechanismPropertyKeyValue .length != 2 ) {
0 commit comments