Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

JAVA-691: Changed MONGO-CR to MONGODB-CR and authProtocol to authMech…

…anism
  • Loading branch information...
commit d647ccd0a59b23bed469053142ea7e2974fd44c0 1 parent a12d953
Jeff Yemin jyemin authored
2  src/main/com/mongodb/DBPort.java
View
@@ -312,7 +312,7 @@ protected void close(){
CommandResult authenticate(Mongo mongo, final MongoCredential credentials) {
Authenticator authenticator;
- if (credentials.getMechanism().equals(MongoCredential.MONGO_CR_MECHANISM)) {
+ if (credentials.getMechanism().equals(MongoCredential.MONGODB_CR_MECHANISM)) {
authenticator = new NativeAuthenticator(mongo, credentials);
} else if (credentials.getMechanism().equals(MongoCredential.GSSAPI_MECHANISM)) {
authenticator = new GSSAPIAuthenticator(mongo, credentials);
15 src/main/com/mongodb/MongoClientURI.java
View
@@ -127,8 +127,8 @@
* </ul>
* <p>Authentication configuration:</p>
* <ul>
- * <li>{@code authProtocol=MONGO-CR|GSSAPI}: The authentication protocol to use if a credential was supplied.
- * The default is MONGO-CR, which is the native MongoDB Challenge Response mechanism.
+ * <li>{@code authMechanism=MONGO-CR|GSSAPI}: The authentication mechanism to use if a credential was supplied.
+ * The default is MONGODB-CR, which is the native MongoDB Challenge Response mechanism.
* </li>
* <li>{@code authSource=string}: The source of the authentication credentials. This is typically the database that
* 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) {
writeConcernKeys.add("fsync");
writeConcernKeys.add("j");
- authKeys.add("authprotocol");
+ authKeys.add("authmechanism");
authKeys.add("authsource");
allKeys.addAll(generalOptionsKeys);
@@ -393,7 +393,7 @@ private MongoCredential createCredentials(Map<String, List<String>> optionsMap,
database = "admin";
}
- String mechanism = MongoCredential.MONGO_CR_MECHANISM;
+ String mechanism = MongoCredential.MONGODB_CR_MECHANISM;
String authSource = database;
for (String key : authKeys) {
@@ -403,7 +403,7 @@ private MongoCredential createCredentials(Map<String, List<String>> optionsMap,
continue;
}
- if (key.equals("authprotocol")) {
+ if (key.equals("authmechanism")) {
mechanism = value;
} else if (key.equals("authsource")) {
authSource = value;
@@ -413,9 +413,12 @@ private MongoCredential createCredentials(Map<String, List<String>> optionsMap,
if (mechanism.equals(MongoCredential.GSSAPI_MECHANISM)) {
return MongoCredential.createGSSAPICredential(userName);
}
- else {
+ else if (mechanism.equals(MongoCredential.MONGODB_CR_MECHANISM)) {
return MongoCredential.createMongoCRCredential(userName, authSource, password);
}
+ else {
+ throw new IllegalArgumentException("Unsupported authMechanism: " + mechanism);
+ }
}
private String getLastValue(final Map<String, List<String>> optionsMap, final String key) {
8 src/main/com/mongodb/MongoCredential.java
View
@@ -38,7 +38,7 @@
/**
* The MongoDB Challenge Response mechanism.
*/
- public static final String MONGO_CR_MECHANISM = "MONGO-CR";
+ public static final String MONGODB_CR_MECHANISM = "MONGODB-CR";
private final String mechanism;
@@ -55,7 +55,7 @@
* @return the credential
*/
public static MongoCredential createMongoCRCredential(String userName, String database, char[] password) {
- return new MongoCredential(MONGO_CR_MECHANISM, userName, database, password);
+ return new MongoCredential(MONGODB_CR_MECHANISM, userName, database, password);
}
/**
@@ -86,8 +86,8 @@ public static MongoCredential createGSSAPICredential(String userName) {
throw new IllegalArgumentException("username can not be null");
}
- if (mechanism.equals(MONGO_CR_MECHANISM) && password == null) {
- throw new IllegalArgumentException("Password can not be null for " + MONGO_CR_MECHANISM + " mechanism");
+ if (mechanism.equals(MONGODB_CR_MECHANISM) && password == null) {
+ throw new IllegalArgumentException("Password can not be null for " + MONGODB_CR_MECHANISM + " mechanism");
}
if (mechanism.equals(GSSAPI_MECHANISM) && password != null) {
14 src/test/com/mongodb/MongoClientURITest.java
View
@@ -92,10 +92,10 @@ public void testUserPass() {
assertEquals(MongoCredential.createMongoCRCredential(userName, "bar", password), u.getCredentials());
- u = new MongoClientURI("mongodb://user@host/?authProtocol=GSSAPI");
+ u = new MongoClientURI("mongodb://user@host/?authMechanism=GSSAPI");
assertEquals(MongoCredential.createGSSAPICredential(userName), u.getCredentials());
- u = new MongoClientURI("mongodb://user:pass@host/?authProtocol=MONGO-CR");
+ u = new MongoClientURI("mongodb://user:pass@host/?authMechanism=MONGODB-CR");
assertEquals(MongoCredential.createMongoCRCredential(userName, "admin", password), u.getCredentials());
u = new MongoClientURI("mongodb://user:pass@host/?authSource=test");
@@ -106,6 +106,16 @@ public void testUserPass() {
}
@Test
+ public void testUnsupportedAuthMechanism() {
+ try {
+ new MongoClientURI("mongodb://user:pass@host/?authMechanism=UNKNOWN");
+ fail("Should fail due to unknown authMechanism");
+ } catch (IllegalArgumentException e) {
+ // expected
+ }
+ }
+
+ @Test
public void testURIEncoding() {
MongoClientURI u = new MongoClientURI("mongodb://use%24:he%21%21o@localhost");
assertEquals(MongoCredential.createMongoCRCredential("use$", "admin", "he!!o".toCharArray()), u.getCredentials());
4 src/test/com/mongodb/MongoCredentialTest.java
View
@@ -31,7 +31,7 @@
public void testCredentials() {
MongoCredential credentials;
- final String mechanism = MongoCredential.MONGO_CR_MECHANISM;
+ final String mechanism = MongoCredential.MONGODB_CR_MECHANISM;
final String userName = "user";
final String database = "test";
final char[] password = "pwd".toCharArray();
@@ -41,7 +41,7 @@ public void testCredentials() {
assertEquals(userName, credentials.getUserName());
assertEquals(database, credentials.getSource());
assertArrayEquals(password, credentials.getPassword());
- assertEquals(MongoCredential.MONGO_CR_MECHANISM, credentials.getMechanism());
+ assertEquals(MongoCredential.MONGODB_CR_MECHANISM, credentials.getMechanism());
try {
MongoCredential.createMongoCRCredential(userName, database, null);
Please sign in to comment.
Something went wrong with that request. Please try again.