Skip to content

Commit d647ccd

Browse files
committed
JAVA-691: Changed MONGO-CR to MONGODB-CR and authProtocol to authMechanism
1 parent a12d953 commit d647ccd

File tree

5 files changed

+28
-15
lines changed

5 files changed

+28
-15
lines changed

src/main/com/mongodb/DBPort.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ protected void close(){
312312

313313
CommandResult authenticate(Mongo mongo, final MongoCredential credentials) {
314314
Authenticator authenticator;
315-
if (credentials.getMechanism().equals(MongoCredential.MONGO_CR_MECHANISM)) {
315+
if (credentials.getMechanism().equals(MongoCredential.MONGODB_CR_MECHANISM)) {
316316
authenticator = new NativeAuthenticator(mongo, credentials);
317317
} else if (credentials.getMechanism().equals(MongoCredential.GSSAPI_MECHANISM)) {
318318
authenticator = new GSSAPIAuthenticator(mongo, credentials);

src/main/com/mongodb/MongoClientURI.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,8 @@
127127
* </ul>
128128
* <p>Authentication configuration:</p>
129129
* <ul>
130-
* <li>{@code authProtocol=MONGO-CR|GSSAPI}: The authentication protocol to use if a credential was supplied.
131-
* The default is MONGO-CR, which is the native MongoDB Challenge Response mechanism.
130+
* <li>{@code authMechanism=MONGO-CR|GSSAPI}: The authentication mechanism to use if a credential was supplied.
131+
* The default is MONGODB-CR, which is the native MongoDB Challenge Response mechanism.
132132
* </li>
133133
* <li>{@code authSource=string}: The source of the authentication credentials. This is typically the database that
134134
* the credentials have been created. The value defaults to the database specified in the path portion of the URI.
@@ -269,7 +269,7 @@ public MongoClientURI(final String uri) {
269269
writeConcernKeys.add("fsync");
270270
writeConcernKeys.add("j");
271271

272-
authKeys.add("authprotocol");
272+
authKeys.add("authmechanism");
273273
authKeys.add("authsource");
274274

275275
allKeys.addAll(generalOptionsKeys);
@@ -393,7 +393,7 @@ private MongoCredential createCredentials(Map<String, List<String>> optionsMap,
393393
database = "admin";
394394
}
395395

396-
String mechanism = MongoCredential.MONGO_CR_MECHANISM;
396+
String mechanism = MongoCredential.MONGODB_CR_MECHANISM;
397397
String authSource = database;
398398

399399
for (String key : authKeys) {
@@ -403,7 +403,7 @@ private MongoCredential createCredentials(Map<String, List<String>> optionsMap,
403403
continue;
404404
}
405405

406-
if (key.equals("authprotocol")) {
406+
if (key.equals("authmechanism")) {
407407
mechanism = value;
408408
} else if (key.equals("authsource")) {
409409
authSource = value;
@@ -413,9 +413,12 @@ private MongoCredential createCredentials(Map<String, List<String>> optionsMap,
413413
if (mechanism.equals(MongoCredential.GSSAPI_MECHANISM)) {
414414
return MongoCredential.createGSSAPICredential(userName);
415415
}
416-
else {
416+
else if (mechanism.equals(MongoCredential.MONGODB_CR_MECHANISM)) {
417417
return MongoCredential.createMongoCRCredential(userName, authSource, password);
418418
}
419+
else {
420+
throw new IllegalArgumentException("Unsupported authMechanism: " + mechanism);
421+
}
419422
}
420423

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

src/main/com/mongodb/MongoCredential.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public final class MongoCredential {
3838
/**
3939
* The MongoDB Challenge Response mechanism.
4040
*/
41-
public static final String MONGO_CR_MECHANISM = "MONGO-CR";
41+
public static final String MONGODB_CR_MECHANISM = "MONGODB-CR";
4242

4343

4444
private final String mechanism;
@@ -55,7 +55,7 @@ public final class MongoCredential {
5555
* @return the credential
5656
*/
5757
public static MongoCredential createMongoCRCredential(String userName, String database, char[] password) {
58-
return new MongoCredential(MONGO_CR_MECHANISM, userName, database, password);
58+
return new MongoCredential(MONGODB_CR_MECHANISM, userName, database, password);
5959
}
6060

6161
/**
@@ -86,8 +86,8 @@ public static MongoCredential createGSSAPICredential(String userName) {
8686
throw new IllegalArgumentException("username can not be null");
8787
}
8888

89-
if (mechanism.equals(MONGO_CR_MECHANISM) && password == null) {
90-
throw new IllegalArgumentException("Password can not be null for " + MONGO_CR_MECHANISM + " mechanism");
89+
if (mechanism.equals(MONGODB_CR_MECHANISM) && password == null) {
90+
throw new IllegalArgumentException("Password can not be null for " + MONGODB_CR_MECHANISM + " mechanism");
9191
}
9292

9393
if (mechanism.equals(GSSAPI_MECHANISM) && password != null) {

src/test/com/mongodb/MongoClientURITest.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,10 @@ public void testUserPass() {
9292

9393
assertEquals(MongoCredential.createMongoCRCredential(userName, "bar", password), u.getCredentials());
9494

95-
u = new MongoClientURI("mongodb://user@host/?authProtocol=GSSAPI");
95+
u = new MongoClientURI("mongodb://user@host/?authMechanism=GSSAPI");
9696
assertEquals(MongoCredential.createGSSAPICredential(userName), u.getCredentials());
9797

98-
u = new MongoClientURI("mongodb://user:pass@host/?authProtocol=MONGO-CR");
98+
u = new MongoClientURI("mongodb://user:pass@host/?authMechanism=MONGODB-CR");
9999
assertEquals(MongoCredential.createMongoCRCredential(userName, "admin", password), u.getCredentials());
100100

101101
u = new MongoClientURI("mongodb://user:pass@host/?authSource=test");
@@ -105,6 +105,16 @@ public void testUserPass() {
105105
assertEquals(MongoCredential.createMongoCRCredential(userName, "admin", password), u.getCredentials());
106106
}
107107

108+
@Test
109+
public void testUnsupportedAuthMechanism() {
110+
try {
111+
new MongoClientURI("mongodb://user:pass@host/?authMechanism=UNKNOWN");
112+
fail("Should fail due to unknown authMechanism");
113+
} catch (IllegalArgumentException e) {
114+
// expected
115+
}
116+
}
117+
108118
@Test
109119
public void testURIEncoding() {
110120
MongoClientURI u = new MongoClientURI("mongodb://use%24:he%21%21o@localhost");

src/test/com/mongodb/MongoCredentialTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public class MongoCredentialTest extends TestCase {
3131
public void testCredentials() {
3232
MongoCredential credentials;
3333

34-
final String mechanism = MongoCredential.MONGO_CR_MECHANISM;
34+
final String mechanism = MongoCredential.MONGODB_CR_MECHANISM;
3535
final String userName = "user";
3636
final String database = "test";
3737
final char[] password = "pwd".toCharArray();
@@ -41,7 +41,7 @@ public void testCredentials() {
4141
assertEquals(userName, credentials.getUserName());
4242
assertEquals(database, credentials.getSource());
4343
assertArrayEquals(password, credentials.getPassword());
44-
assertEquals(MongoCredential.MONGO_CR_MECHANISM, credentials.getMechanism());
44+
assertEquals(MongoCredential.MONGODB_CR_MECHANISM, credentials.getMechanism());
4545

4646
try {
4747
MongoCredential.createMongoCRCredential(userName, database, null);

0 commit comments

Comments
 (0)