42
42
import java .util .Collections ;
43
43
import java .util .HashMap ;
44
44
import java .util .HashSet ;
45
- import java .util .List ;
46
45
import java .util .Map ;
47
46
import java .util .Set ;
48
47
import java .util .concurrent .atomic .AtomicLong ;
@@ -313,9 +312,9 @@ protected void close(){
313
312
314
313
CommandResult authenticate (Mongo mongo , final MongoCredential credentials ) {
315
314
Authenticator authenticator ;
316
- if (credentials .getMechanism () == MongoAuthenticationMechanism . MONGO_CR ) {
315
+ if (credentials .getMechanism (). equals ( MongoCredential . MONGO_CR_MECHANISM ) ) {
317
316
authenticator = new NativeAuthenticator (mongo , credentials );
318
- } else if (credentials .getMechanism ().equals (MongoAuthenticationMechanism . GSSAPI )) {
317
+ } else if (credentials .getMechanism ().equals (MongoCredential . GSSAPI_MECHANISM )) {
319
318
authenticator = new GSSAPIAuthenticator (mongo , credentials );
320
319
} else {
321
320
throw new IllegalArgumentException ("Unsupported authentication protocol: " + credentials .getMechanism ());
@@ -335,30 +334,6 @@ void checkAuth(Mongo mongo) throws IOException {
335
334
}
336
335
}
337
336
338
- private Authenticator getStrongestAuthenticator (final Mongo mongo , MongoCredential credentials ) {
339
- if (useCRAMAuthenticationProtocol == null ) {
340
- cacheStrongestAuthenticationProtocol (mongo );
341
- }
342
-
343
- if (useCRAMAuthenticationProtocol ) {
344
- return new GenericSaslAuthenticator (mongo , credentials , GenericSaslAuthenticator .CRAM_MD5 );
345
- } else {
346
- return new NativeAuthenticator (mongo , credentials );
347
- }
348
- }
349
-
350
- // Since the driver currently only support CRAM-MD5 as a generic SASL authenticator, simple determine whether
351
- // that is a supported mechanism.
352
- private void cacheStrongestAuthenticationProtocol (final Mongo mongo ) {
353
- try {
354
- CommandResult res = runCommand (mongo .getDB ("admin" ), new BasicDBObject ("saslStart" , 1 ).append ("mechanism" , "" ));
355
- useCRAMAuthenticationProtocol = res .get ("supportedMechanisms" ) != null &&
356
- ((List ) res .get ("supportedMechanisms" )).contains (GenericSaslAuthenticator .CRAM_MD5 );
357
- } catch (IOException e ) {
358
- throw new MongoException .Network ("IOException authenticating the connection" , e );
359
- }
360
- }
361
-
362
337
/**
363
338
* Gets the pool that this port belongs to.
364
339
* @return the pool that this port belongs to.
@@ -416,7 +391,7 @@ class GenericSaslAuthenticator extends SaslAuthenticator {
416
391
protected SaslClient createSaslClient () {
417
392
try {
418
393
return Sasl .createSaslClient (new String []{mechanism },
419
- credentials .getUserName (), MONGODB_PROTOCOL ,
394
+ credential .getUserName (), MONGODB_PROTOCOL ,
420
395
serverAddress ().getHost (), null , new CredentialsHandlingCallbackHandler ());
421
396
} catch (SaslException e ) {
422
397
throw new MongoException ("Exception initializing SASL client" , e );
@@ -425,7 +400,7 @@ protected SaslClient createSaslClient() {
425
400
426
401
@ Override
427
402
protected DB getDatabase () {
428
- return mongo .getDB (credentials .getSource ());
403
+ return mongo .getDB (credential .getSource ());
429
404
}
430
405
431
406
@ Override
@@ -439,12 +414,12 @@ public void handle(final Callback[] callbacks) throws IOException, UnsupportedCa
439
414
for (Callback callback : callbacks ) {
440
415
if (callback instanceof NameCallback ) {
441
416
NameCallback nameCallback = (NameCallback ) callback ;
442
- nameCallback .setName (credentials .getUserName ());
417
+ nameCallback .setName (credential .getUserName ());
443
418
}
444
419
if (callback instanceof PasswordCallback ) {
445
420
PasswordCallback passwordCallback = (PasswordCallback ) callback ;
446
- String hashedPassword = new String (NativeAuthenticationHelper .createHash (credentials . getUserName (),
447
- credentials .getPassword ()));
421
+ String hashedPassword = new String (NativeAuthenticationHelper .createHash (
422
+ credential . getUserName (), credential .getPassword ()));
448
423
passwordCallback .setPassword (hashedPassword .toCharArray ());
449
424
}
450
425
}
@@ -454,23 +429,23 @@ public void handle(final Callback[] callbacks) throws IOException, UnsupportedCa
454
429
455
430
class GSSAPIAuthenticator extends SaslAuthenticator {
456
431
public static final String GSSAPI_OID = "1.2.840.113554.1.2.2" ;
457
- public static final String GSSAPI_MECHANISM = "GSSAPI" ;
432
+ public static final String GSSAPI_MECHANISM = MongoCredential . GSSAPI_MECHANISM ;
458
433
459
434
GSSAPIAuthenticator (final Mongo mongo , final MongoCredential credentials ) {
460
435
super (mongo , credentials );
461
436
462
- if (!this .credentials .getMechanism ().equals (MongoAuthenticationMechanism . GSSAPI )) {
463
- throw new MongoException ("Incorrect mechanism: " + this .credentials .getMechanism ());
437
+ if (!this .credential .getMechanism ().equals (MongoCredential . GSSAPI_MECHANISM )) {
438
+ throw new MongoException ("Incorrect mechanism: " + this .credential .getMechanism ());
464
439
}
465
440
}
466
441
467
442
@ Override
468
443
protected SaslClient createSaslClient () {
469
444
try {
470
445
Map <String , Object > props = new HashMap <String , Object >();
471
- props .put (Sasl .CREDENTIALS , getGSSCredential (credentials .getUserName ()));
446
+ props .put (Sasl .CREDENTIALS , getGSSCredential (credential .getUserName ()));
472
447
473
- return Sasl .createSaslClient (new String []{GSSAPI_MECHANISM }, credentials .getUserName (), MONGODB_PROTOCOL ,
448
+ return Sasl .createSaslClient (new String []{GSSAPI_MECHANISM }, credential .getUserName (), MONGODB_PROTOCOL ,
474
449
serverAddress ().getHost (), props , null );
475
450
} catch (SaslException e ) {
476
451
throw new MongoException ("Exception initializing SASL client" , e );
@@ -481,7 +456,7 @@ protected SaslClient createSaslClient() {
481
456
482
457
@ Override
483
458
protected DB getDatabase () {
484
- return mongo .getDB (credentials .getSource ());
459
+ return mongo .getDB (credential .getSource ());
485
460
}
486
461
487
462
@ Override
@@ -565,12 +540,12 @@ class NativeAuthenticator extends Authenticator {
565
540
@ Override
566
541
public CommandResult authenticate () {
567
542
try {
568
- DB db = mongo .getDB (credentials .getSource ());
543
+ DB db = mongo .getDB (credential .getSource ());
569
544
CommandResult res = runCommand (db , NativeAuthenticationHelper .getNonceCommand ());
570
545
res .throwOnError ();
571
546
572
- res = runCommand (db , NativeAuthenticationHelper .getAuthCommand (credentials .getUserName (),
573
- credentials .getPassword (), res .getString ("nonce" )));
547
+ res = runCommand (db , NativeAuthenticationHelper .getAuthCommand (credential .getUserName (),
548
+ credential .getPassword (), res .getString ("nonce" )));
574
549
res .throwOnError ();
575
550
return res ;
576
551
} catch (IOException e ) {
@@ -581,11 +556,11 @@ public CommandResult authenticate() {
581
556
582
557
abstract class Authenticator {
583
558
protected final Mongo mongo ;
584
- protected final MongoCredential credentials ;
559
+ protected final MongoCredential credential ;
585
560
586
- Authenticator (Mongo mongo , MongoCredential credentials ) {
561
+ Authenticator (Mongo mongo , MongoCredential credential ) {
587
562
this .mongo = mongo ;
588
- this .credentials = credentials ;
563
+ this .credential = credential ;
589
564
}
590
565
591
566
abstract CommandResult authenticate ();
0 commit comments