Skip to content

Commit a12d953

Browse files
committed
JAVA-691: Replacing authentication mechanism enum with String, so that it's extensible in the future
1 parent 2ed798f commit a12d953

13 files changed

+125
-263
lines changed

examples/GSSAPICredentialsExample.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
import com.mongodb.BasicDBObject;
1818
import com.mongodb.DB;
19-
import com.mongodb.MongoAuthenticationMechanism;
2019
import com.mongodb.MongoClient;
2120
import com.mongodb.MongoClientOptions;
2221
import com.mongodb.MongoCredential;
@@ -69,7 +68,7 @@ public static void main(String[] args) throws UnknownHostException, InterruptedE
6968
System.out.println();
7069

7170
MongoClient mongoClient = new MongoClient(new ServerAddress(server),
72-
Arrays.asList(new MongoCredential(user, MongoAuthenticationMechanism.GSSAPI)),
71+
Arrays.asList(MongoCredential.createGSSAPICredential(user)),
7372
new MongoClientOptions.Builder().socketKeepAlive(true).socketTimeout(30000).build());
7473
DB testDB = mongoClient.getDB(databaseName);
7574

examples/MongoCredentialsExample.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
import com.mongodb.BasicDBObject;
1919
import com.mongodb.DB;
20-
import com.mongodb.MongoAuthenticationMechanism;
2120
import com.mongodb.MongoClient;
2221
import com.mongodb.MongoClientOptions;
2322
import com.mongodb.MongoCredential;
@@ -40,7 +39,7 @@ public static void main(String[] args) throws UnknownHostException {
4039
System.out.println();
4140

4241
MongoClient mongoClient = new MongoClient(new ServerAddress(server),
43-
Arrays.asList(new MongoCredential(user, password.toCharArray(), MongoAuthenticationMechanism.MONGO_CR, "test")),
42+
Arrays.asList(MongoCredential.createMongoCRCredential(user, "test", password.toCharArray())),
4443
new MongoClientOptions.Builder().socketKeepAlive(true).socketTimeout(30000).build());
4544
DB testDB = mongoClient.getDB(databaseName);
4645

src/main/com/mongodb/DB.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -614,8 +614,8 @@ public synchronized CommandResult authenticateCommand(String username, char[] pa
614614
}
615615

616616
private CommandResultPair authenticateCommandHelper(String username, char[] password) {
617-
MongoCredential credentials = new MongoCredential(username, password, MongoAuthenticationMechanism.MONGO_CR, getName());
618-
617+
MongoCredential credentials =
618+
MongoCredential.createMongoCRCredential(username, getName(), password);
619619
if (getAuthenticationCredentials() != null) {
620620
if (getAuthenticationCredentials().equals(credentials)) {
621621
if (authenticationTestCommandResult != null) {

src/main/com/mongodb/DBPort.java

Lines changed: 19 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
import java.util.Collections;
4343
import java.util.HashMap;
4444
import java.util.HashSet;
45-
import java.util.List;
4645
import java.util.Map;
4746
import java.util.Set;
4847
import java.util.concurrent.atomic.AtomicLong;
@@ -313,9 +312,9 @@ protected void close(){
313312

314313
CommandResult authenticate(Mongo mongo, final MongoCredential credentials) {
315314
Authenticator authenticator;
316-
if (credentials.getMechanism() == MongoAuthenticationMechanism.MONGO_CR) {
315+
if (credentials.getMechanism().equals(MongoCredential.MONGO_CR_MECHANISM)) {
317316
authenticator = new NativeAuthenticator(mongo, credentials);
318-
} else if (credentials.getMechanism().equals(MongoAuthenticationMechanism.GSSAPI)) {
317+
} else if (credentials.getMechanism().equals(MongoCredential.GSSAPI_MECHANISM)) {
319318
authenticator = new GSSAPIAuthenticator(mongo, credentials);
320319
} else {
321320
throw new IllegalArgumentException("Unsupported authentication protocol: " + credentials.getMechanism());
@@ -335,30 +334,6 @@ void checkAuth(Mongo mongo) throws IOException {
335334
}
336335
}
337336

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-
362337
/**
363338
* Gets the pool that this port belongs to.
364339
* @return the pool that this port belongs to.
@@ -416,7 +391,7 @@ class GenericSaslAuthenticator extends SaslAuthenticator {
416391
protected SaslClient createSaslClient() {
417392
try {
418393
return Sasl.createSaslClient(new String[]{mechanism},
419-
credentials.getUserName(), MONGODB_PROTOCOL,
394+
credential.getUserName(), MONGODB_PROTOCOL,
420395
serverAddress().getHost(), null, new CredentialsHandlingCallbackHandler());
421396
} catch (SaslException e) {
422397
throw new MongoException("Exception initializing SASL client", e);
@@ -425,7 +400,7 @@ protected SaslClient createSaslClient() {
425400

426401
@Override
427402
protected DB getDatabase() {
428-
return mongo.getDB(credentials.getSource());
403+
return mongo.getDB(credential.getSource());
429404
}
430405

431406
@Override
@@ -439,12 +414,12 @@ public void handle(final Callback[] callbacks) throws IOException, UnsupportedCa
439414
for (Callback callback : callbacks) {
440415
if (callback instanceof NameCallback) {
441416
NameCallback nameCallback = (NameCallback) callback;
442-
nameCallback.setName(credentials.getUserName());
417+
nameCallback.setName(credential.getUserName());
443418
}
444419
if (callback instanceof PasswordCallback) {
445420
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()));
448423
passwordCallback.setPassword(hashedPassword.toCharArray());
449424
}
450425
}
@@ -454,23 +429,23 @@ public void handle(final Callback[] callbacks) throws IOException, UnsupportedCa
454429

455430
class GSSAPIAuthenticator extends SaslAuthenticator {
456431
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;
458433

459434
GSSAPIAuthenticator(final Mongo mongo, final MongoCredential credentials) {
460435
super(mongo, credentials);
461436

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());
464439
}
465440
}
466441

467442
@Override
468443
protected SaslClient createSaslClient() {
469444
try {
470445
Map<String, Object> props = new HashMap<String, Object>();
471-
props.put(Sasl.CREDENTIALS, getGSSCredential(credentials.getUserName()));
446+
props.put(Sasl.CREDENTIALS, getGSSCredential(credential.getUserName()));
472447

473-
return Sasl.createSaslClient(new String[]{GSSAPI_MECHANISM}, credentials.getUserName(), MONGODB_PROTOCOL,
448+
return Sasl.createSaslClient(new String[]{GSSAPI_MECHANISM}, credential.getUserName(), MONGODB_PROTOCOL,
474449
serverAddress().getHost(), props, null);
475450
} catch (SaslException e) {
476451
throw new MongoException("Exception initializing SASL client", e);
@@ -481,7 +456,7 @@ protected SaslClient createSaslClient() {
481456

482457
@Override
483458
protected DB getDatabase() {
484-
return mongo.getDB(credentials.getSource());
459+
return mongo.getDB(credential.getSource());
485460
}
486461

487462
@Override
@@ -565,12 +540,12 @@ class NativeAuthenticator extends Authenticator {
565540
@Override
566541
public CommandResult authenticate() {
567542
try {
568-
DB db = mongo.getDB(credentials.getSource());
543+
DB db = mongo.getDB(credential.getSource());
569544
CommandResult res = runCommand(db, NativeAuthenticationHelper.getNonceCommand());
570545
res.throwOnError();
571546

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")));
574549
res.throwOnError();
575550
return res;
576551
} catch (IOException e) {
@@ -581,11 +556,11 @@ public CommandResult authenticate() {
581556

582557
abstract class Authenticator {
583558
protected final Mongo mongo;
584-
protected final MongoCredential credentials;
559+
protected final MongoCredential credential;
585560

586-
Authenticator(Mongo mongo, MongoCredential credentials) {
561+
Authenticator(Mongo mongo, MongoCredential credential) {
587562
this.mongo = mongo;
588-
this.credentials = credentials;
563+
this.credential = credential;
589564
}
590565

591566
abstract CommandResult authenticate();

src/main/com/mongodb/MongoAuthenticationMechanism.java

Lines changed: 0 additions & 83 deletions
This file was deleted.

src/main/com/mongodb/MongoClientURI.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,11 @@ private MongoCredential createCredentials(Map<String, List<String>> optionsMap,
389389
return null;
390390
}
391391

392-
MongoAuthenticationMechanism protocol = MongoAuthenticationMechanism.MONGO_CR;
392+
if (database == null) {
393+
database = "admin";
394+
}
395+
396+
String mechanism = MongoCredential.MONGO_CR_MECHANISM;
393397
String authSource = database;
394398

395399
for (String key : authKeys) {
@@ -400,13 +404,18 @@ private MongoCredential createCredentials(Map<String, List<String>> optionsMap,
400404
}
401405

402406
if (key.equals("authprotocol")) {
403-
protocol = MongoAuthenticationMechanism.byMechanismName(value);
407+
mechanism = value;
404408
} else if (key.equals("authsource")) {
405409
authSource = value;
406410
}
407411
}
408412

409-
return new MongoCredential(userName, password, protocol, authSource);
413+
if (mechanism.equals(MongoCredential.GSSAPI_MECHANISM)) {
414+
return MongoCredential.createGSSAPICredential(userName);
415+
}
416+
else {
417+
return MongoCredential.createMongoCRCredential(userName, authSource, password);
418+
}
410419
}
411420

412421
private String getLastValue(final Map<String, List<String>> optionsMap, final String key) {

0 commit comments

Comments
 (0)