Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

JAVA-691: Replacing authentication mechanism enum with String, so tha…

…t it's extensible in the future
  • Loading branch information...
commit a12d953a9d525f883565a361bc342c97d852d580 1 parent 2ed798f
@jyemin jyemin authored
View
3  examples/GSSAPICredentialsExample.java
@@ -16,7 +16,6 @@
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
-import com.mongodb.MongoAuthenticationMechanism;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.MongoCredential;
@@ -69,7 +68,7 @@ public static void main(String[] args) throws UnknownHostException, InterruptedE
System.out.println();
MongoClient mongoClient = new MongoClient(new ServerAddress(server),
- Arrays.asList(new MongoCredential(user, MongoAuthenticationMechanism.GSSAPI)),
+ Arrays.asList(MongoCredential.createGSSAPICredential(user)),
new MongoClientOptions.Builder().socketKeepAlive(true).socketTimeout(30000).build());
DB testDB = mongoClient.getDB(databaseName);
View
3  examples/MongoCredentialsExample.java
@@ -17,7 +17,6 @@
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
-import com.mongodb.MongoAuthenticationMechanism;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.MongoCredential;
@@ -40,7 +39,7 @@ public static void main(String[] args) throws UnknownHostException {
System.out.println();
MongoClient mongoClient = new MongoClient(new ServerAddress(server),
- Arrays.asList(new MongoCredential(user, password.toCharArray(), MongoAuthenticationMechanism.MONGO_CR, "test")),
+ Arrays.asList(MongoCredential.createMongoCRCredential(user, "test", password.toCharArray())),
new MongoClientOptions.Builder().socketKeepAlive(true).socketTimeout(30000).build());
DB testDB = mongoClient.getDB(databaseName);
View
4 src/main/com/mongodb/DB.java
@@ -614,8 +614,8 @@ public synchronized CommandResult authenticateCommand(String username, char[] pa
}
private CommandResultPair authenticateCommandHelper(String username, char[] password) {
- MongoCredential credentials = new MongoCredential(username, password, MongoAuthenticationMechanism.MONGO_CR, getName());
-
+ MongoCredential credentials =
+ MongoCredential.createMongoCRCredential(username, getName(), password);
if (getAuthenticationCredentials() != null) {
if (getAuthenticationCredentials().equals(credentials)) {
if (authenticationTestCommandResult != null) {
View
63 src/main/com/mongodb/DBPort.java
@@ -42,7 +42,6 @@
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
-import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicLong;
@@ -313,9 +312,9 @@ protected void close(){
CommandResult authenticate(Mongo mongo, final MongoCredential credentials) {
Authenticator authenticator;
- if (credentials.getMechanism() == MongoAuthenticationMechanism.MONGO_CR) {
+ if (credentials.getMechanism().equals(MongoCredential.MONGO_CR_MECHANISM)) {
authenticator = new NativeAuthenticator(mongo, credentials);
- } else if (credentials.getMechanism().equals(MongoAuthenticationMechanism.GSSAPI)) {
+ } else if (credentials.getMechanism().equals(MongoCredential.GSSAPI_MECHANISM)) {
authenticator = new GSSAPIAuthenticator(mongo, credentials);
} else {
throw new IllegalArgumentException("Unsupported authentication protocol: " + credentials.getMechanism());
@@ -335,30 +334,6 @@ void checkAuth(Mongo mongo) throws IOException {
}
}
- private Authenticator getStrongestAuthenticator(final Mongo mongo, MongoCredential credentials) {
- if (useCRAMAuthenticationProtocol == null) {
- cacheStrongestAuthenticationProtocol(mongo);
- }
-
- if (useCRAMAuthenticationProtocol) {
- return new GenericSaslAuthenticator(mongo, credentials, GenericSaslAuthenticator.CRAM_MD5);
- } else {
- return new NativeAuthenticator(mongo, credentials);
- }
- }
-
- // Since the driver currently only support CRAM-MD5 as a generic SASL authenticator, simple determine whether
- // that is a supported mechanism.
- private void cacheStrongestAuthenticationProtocol(final Mongo mongo) {
- try {
- CommandResult res = runCommand(mongo.getDB("admin"), new BasicDBObject("saslStart", 1).append("mechanism", ""));
- useCRAMAuthenticationProtocol = res.get("supportedMechanisms") != null &&
- ((List) res.get("supportedMechanisms")).contains(GenericSaslAuthenticator.CRAM_MD5);
- } catch (IOException e) {
- throw new MongoException.Network("IOException authenticating the connection", e);
- }
- }
-
/**
* Gets the pool that this port belongs to.
* @return the pool that this port belongs to.
@@ -416,7 +391,7 @@ public DBPortPool getPool() {
protected SaslClient createSaslClient() {
try {
return Sasl.createSaslClient(new String[]{mechanism},
- credentials.getUserName(), MONGODB_PROTOCOL,
+ credential.getUserName(), MONGODB_PROTOCOL,
serverAddress().getHost(), null, new CredentialsHandlingCallbackHandler());
} catch (SaslException e) {
throw new MongoException("Exception initializing SASL client", e);
@@ -425,7 +400,7 @@ protected SaslClient createSaslClient() {
@Override
protected DB getDatabase() {
- return mongo.getDB(credentials.getSource());
+ return mongo.getDB(credential.getSource());
}
@Override
@@ -439,12 +414,12 @@ public void handle(final Callback[] callbacks) throws IOException, UnsupportedCa
for (Callback callback : callbacks) {
if (callback instanceof NameCallback) {
NameCallback nameCallback = (NameCallback) callback;
- nameCallback.setName(credentials.getUserName());
+ nameCallback.setName(credential.getUserName());
}
if (callback instanceof PasswordCallback) {
PasswordCallback passwordCallback = (PasswordCallback) callback;
- String hashedPassword = new String(NativeAuthenticationHelper.createHash(credentials.getUserName(),
- credentials.getPassword()));
+ String hashedPassword = new String(NativeAuthenticationHelper.createHash(
+ credential.getUserName(), credential.getPassword()));
passwordCallback.setPassword(hashedPassword.toCharArray());
}
}
@@ -454,13 +429,13 @@ public void handle(final Callback[] callbacks) throws IOException, UnsupportedCa
class GSSAPIAuthenticator extends SaslAuthenticator {
public static final String GSSAPI_OID = "1.2.840.113554.1.2.2";
- public static final String GSSAPI_MECHANISM = "GSSAPI";
+ public static final String GSSAPI_MECHANISM = MongoCredential.GSSAPI_MECHANISM;
GSSAPIAuthenticator(final Mongo mongo, final MongoCredential credentials) {
super(mongo, credentials);
- if (!this.credentials.getMechanism().equals(MongoAuthenticationMechanism.GSSAPI)) {
- throw new MongoException("Incorrect mechanism: " + this.credentials.getMechanism());
+ if (!this.credential.getMechanism().equals(MongoCredential.GSSAPI_MECHANISM)) {
+ throw new MongoException("Incorrect mechanism: " + this.credential.getMechanism());
}
}
@@ -468,9 +443,9 @@ public void handle(final Callback[] callbacks) throws IOException, UnsupportedCa
protected SaslClient createSaslClient() {
try {
Map<String, Object> props = new HashMap<String, Object>();
- props.put(Sasl.CREDENTIALS, getGSSCredential(credentials.getUserName()));
+ props.put(Sasl.CREDENTIALS, getGSSCredential(credential.getUserName()));
- return Sasl.createSaslClient(new String[]{GSSAPI_MECHANISM}, credentials.getUserName(), MONGODB_PROTOCOL,
+ return Sasl.createSaslClient(new String[]{GSSAPI_MECHANISM}, credential.getUserName(), MONGODB_PROTOCOL,
serverAddress().getHost(), props, null);
} catch (SaslException e) {
throw new MongoException("Exception initializing SASL client", e);
@@ -481,7 +456,7 @@ protected SaslClient createSaslClient() {
@Override
protected DB getDatabase() {
- return mongo.getDB(credentials.getSource());
+ return mongo.getDB(credential.getSource());
}
@Override
@@ -565,12 +540,12 @@ private CommandResult sendSaslContinue(final int conversationId, final byte[] ou
@Override
public CommandResult authenticate() {
try {
- DB db = mongo.getDB(credentials.getSource());
+ DB db = mongo.getDB(credential.getSource());
CommandResult res = runCommand(db, NativeAuthenticationHelper.getNonceCommand());
res.throwOnError();
- res = runCommand(db, NativeAuthenticationHelper.getAuthCommand(credentials.getUserName(),
- credentials.getPassword(), res.getString("nonce")));
+ res = runCommand(db, NativeAuthenticationHelper.getAuthCommand(credential.getUserName(),
+ credential.getPassword(), res.getString("nonce")));
res.throwOnError();
return res;
} catch (IOException e) {
@@ -581,11 +556,11 @@ public CommandResult authenticate() {
abstract class Authenticator {
protected final Mongo mongo;
- protected final MongoCredential credentials;
+ protected final MongoCredential credential;
- Authenticator(Mongo mongo, MongoCredential credentials) {
+ Authenticator(Mongo mongo, MongoCredential credential) {
this.mongo = mongo;
- this.credentials = credentials;
+ this.credential = credential;
}
abstract CommandResult authenticate();
View
83 src/main/com/mongodb/MongoAuthenticationMechanism.java
@@ -1,83 +0,0 @@
-/*
- * Copyright (c) 2008 - 2013 10gen, Inc. <http://10gen.com>
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *
- */
-
-package com.mongodb;
-
-/**
- * An enumeration of the supported authentication mechanisms.
- */
-public enum MongoAuthenticationMechanism {
- /**
- * The GSSAPI protocol, to support Kerberos v5 via a SASL-based authentication protocol
- */
- GSSAPI {
- /**
- * The default source for GSSAPI is a reserved name that doesn't correspond to any database.
- * @return the default source.
- */
- @Override
- public String getDefaultSource() {
- return "$external";
- }
-
- @Override
- public String getMechanismName() {
- return "GSSAPI";
- }
- },
- /**
- * The native MongoDB authentication mechanism. This is an abbreviation for MongoDB Challenge Response.
- */
- MONGO_CR {
- /**
- * The default source is the "admin" database.
- * @return the "admin" database
- */
- @Override
- public String getDefaultSource() {
- return "admin";
- }
-
- @Override
- public String getMechanismName() {
- return "MONGO-CR";
- }
- };
-
- /**
- * Gets the default source for this mechanism, usually a database name.
- *
- * @return the default database for this protocol
- */
- public abstract String getDefaultSource();
-
- /**
- * Gets the mechanism name.
- * @return the name
- */
- public abstract String getMechanismName();
-
- public static MongoAuthenticationMechanism byMechanismName(String name) {
- if (name.equals(GSSAPI.getMechanismName())) {
- return GSSAPI;
- }
- if (name.equals(MONGO_CR.getMechanismName())) {
- return MONGO_CR;
- }
- throw new IllegalArgumentException("Invalid authentication mechanism name: " + name);
- }
-}
View
15 src/main/com/mongodb/MongoClientURI.java
@@ -389,7 +389,11 @@ private MongoCredential createCredentials(Map<String, List<String>> optionsMap,
return null;
}
- MongoAuthenticationMechanism protocol = MongoAuthenticationMechanism.MONGO_CR;
+ if (database == null) {
+ database = "admin";
+ }
+
+ String mechanism = MongoCredential.MONGO_CR_MECHANISM;
String authSource = database;
for (String key : authKeys) {
@@ -400,13 +404,18 @@ private MongoCredential createCredentials(Map<String, List<String>> optionsMap,
}
if (key.equals("authprotocol")) {
- protocol = MongoAuthenticationMechanism.byMechanismName(value);
+ mechanism = value;
} else if (key.equals("authsource")) {
authSource = value;
}
}
- return new MongoCredential(userName, password, protocol, authSource);
+ if (mechanism.equals(MongoCredential.GSSAPI_MECHANISM)) {
+ return MongoCredential.createGSSAPICredential(userName);
+ }
+ else {
+ return MongoCredential.createMongoCRCredential(userName, authSource, password);
+ }
}
private String getLastValue(final Map<String, List<String>> optionsMap, final String key) {
View
127 src/main/com/mongodb/MongoCredential.java
@@ -28,85 +28,76 @@
* @since 2.11.0
*/
@Immutable
-public class MongoCredential {
-
- private final MongoAuthenticationMechanism mechanism;
- private final String userName;
- private final char[] password;
- private final String source;
+public final class MongoCredential {
/**
- * Constructs a new instance using the given user name and password and the default mechanism and source.
- *
- * @param userName the user name
- * @param password the password
+ * The GSSAPI mechanism. See the <a href="http://tools.ietf.org/html/rfc4752">RFC</a>.
*/
- public MongoCredential(final String userName, final char[] password) {
- this(userName, password, MongoAuthenticationMechanism.MONGO_CR);
- }
+ public static final String GSSAPI_MECHANISM = "GSSAPI";
/**
- * Constructs a new instance using the given user name, password and source and the default mechanism.
- *
- * @param userName the user name
- * @param password the password
- * @param source the source of the credentials
+ * The MongoDB Challenge Response mechanism.
*/
- public MongoCredential(final String userName, final char[] password, String source) {
- this(userName, password, MongoAuthenticationMechanism.MONGO_CR, source);
- }
+ public static final String MONGO_CR_MECHANISM = "MONGO-CR";
+
+
+ private final String mechanism;
+ private final String userName;
+ private final String source;
+ private final char[] password;
/**
- * Constructs a new instance using the given user name, password and mechanism and the default source for that mechanism.
+ * Creates a MongoCredential instance for the MongoDB Challenge Response protocol.
*
* @param userName the user name
- * @param password the password
- * @param mechanism the mechanism to use for authentication
+ * @param database the database where the user is defined
+ * @param password the user's password
+ * @return the credential
*/
- public MongoCredential(final String userName, final char[] password, MongoAuthenticationMechanism mechanism) {
- this(userName, password, mechanism, null);
+ public static MongoCredential createMongoCRCredential(String userName, String database, char[] password) {
+ return new MongoCredential(MONGO_CR_MECHANISM, userName, database, password);
}
/**
- * Constructs a new instance using the given user name and mechanism. This really only applies to the GSSAPI
- * mechanism, since it's the only one that doesn't require a password.
+ * Creates a MongoCredential instance for the GSSAPI SASL mechanism.
*
* @param userName the user name
- * @param mechanism the mechanism to use for authentication
+ * @return the credential
*/
- public MongoCredential(final String userName, final MongoAuthenticationMechanism mechanism) {
- this(userName, null, mechanism);
+ public static MongoCredential createGSSAPICredential(String userName) {
+ return new MongoCredential(GSSAPI_MECHANISM, userName, "$external", null);
}
/**
- /**
- * Constructs a new instance using the given user name, password, mechanism, and source.
*
+ * Constructs a new instance using the given mechanism, userName, source, and password
+ *
+ * @param mechanism the authentication mechanism
* @param userName the user name
+ * @param source the source of the user name, typically a database name
* @param password the password
- * @param mechanism the mechanism
- * @param source the source of the credentials
*/
- public MongoCredential(final String userName, final char[] password, MongoAuthenticationMechanism mechanism, String source) {
- if (userName == null) {
- throw new IllegalArgumentException();
- }
+ MongoCredential(final String mechanism, final String userName, final String source, final char[] password) {
if (mechanism == null) {
- throw new IllegalArgumentException();
+ throw new IllegalArgumentException("mechanism can not be null");
}
- if (mechanism == MongoAuthenticationMechanism.MONGO_CR && password == null) {
- throw new IllegalArgumentException("password can not be null for " + MongoAuthenticationMechanism.MONGO_CR);
+ if (userName == null) {
+ throw new IllegalArgumentException("username can not be null");
}
- if (mechanism == MongoAuthenticationMechanism.GSSAPI && password != null) {
- throw new IllegalArgumentException("password must be null for " + MongoAuthenticationMechanism.GSSAPI);
+ if (mechanism.equals(MONGO_CR_MECHANISM) && password == null) {
+ throw new IllegalArgumentException("Password can not be null for " + MONGO_CR_MECHANISM + " mechanism");
+ }
+
+ if (mechanism.equals(GSSAPI_MECHANISM) && password != null) {
+ throw new IllegalArgumentException("Password must be null for the " + GSSAPI_MECHANISM + " mechanism");
}
- this.userName = userName;
- this.password = password;
- this.source = source != null ? source : getDefaultDatabase(mechanism);
this.mechanism = mechanism;
+ this.userName = userName;
+ this.source = source;
+ this.password = password != null ? password.clone() : null;
}
/**
@@ -114,7 +105,7 @@ public MongoCredential(final String userName, final char[] password, MongoAuthen
*
* @return the mechanism.
*/
- public MongoAuthenticationMechanism getMechanism() {
+ public String getMechanism() {
return mechanism;
}
@@ -128,6 +119,15 @@ public String getUserName() {
}
/**
+ * Gets the source of the user name, typically the name of the database where the user is defined.
+ *
+ * @return the user name. Can never be null.
+ */
+ public String getSource() {
+ return source;
+ }
+
+ /**
* Gets the password.
*
* @return the password. Can be null for some mechanisms.
@@ -139,15 +139,6 @@ public String getUserName() {
return password.clone();
}
- /**
- * Gets the source, which is usually the name of the database that the credentials are stored in.
- *
- * @return the source.
- */
- public String getSource() {
- return source;
- }
-
@Override
public boolean equals(final Object o) {
if (this == o) return true;
@@ -155,8 +146,8 @@ public boolean equals(final Object o) {
final MongoCredential that = (MongoCredential) o;
+ if (!mechanism.equals(that.mechanism)) return false;
if (!Arrays.equals(password, that.password)) return false;
- if (mechanism != that.mechanism) return false;
if (!source.equals(that.source)) return false;
if (!userName.equals(that.userName)) return false;
@@ -167,26 +158,18 @@ public boolean equals(final Object o) {
public int hashCode() {
int result = mechanism.hashCode();
result = 31 * result + userName.hashCode();
- result = 31 * result + (password != null ? Arrays.hashCode(password) : 0);
result = 31 * result + source.hashCode();
+ result = 31 * result + (password != null ? Arrays.hashCode(password) : 0);
return result;
}
@Override
public String toString() {
- return "MongoCredentials{" +
- "mechanism=" + mechanism +
- ", userName='" + userName +
- ", password=" + "<hidden>" +
- ", source='" + source +
+ return "MongoCredential{" +
+ "mechanism='" + mechanism + '\'' +
+ ", userName='" + userName + '\'' +
+ ", source='" + source + '\'' +
+ ", password=<hidden>" +
'}';
}
-
- private String getDefaultDatabase(final MongoAuthenticationMechanism mechanism) {
- if (mechanism == null) {
- return "admin";
- } else {
- return mechanism.getDefaultSource();
- }
- }
}
View
6 src/test/com/mongodb/DBPortTest.java
@@ -44,8 +44,8 @@ public void testAuthentication() throws IOException {
assertEquals(expected, port.authenticatedDatabases);
- m.getAuthority().getCredentialsStore().add(new MongoCredential("u1", "e".toCharArray(), "DBPortTest1"));
- m.getAuthority().getCredentialsStore().add(new MongoCredential("u2", "e".toCharArray(), "DBPortTest2"));
+ m.getAuthority().getCredentialsStore().add(MongoCredential.createMongoCRCredential("u1", "DBPortTest1", "e".toCharArray()));
+ m.getAuthority().getCredentialsStore().add(MongoCredential.createMongoCRCredential("u2", "DBPortTest2", "e".toCharArray()));
port.checkAuth(m);
@@ -53,7 +53,7 @@ public void testAuthentication() throws IOException {
expected.add("DBPortTest2");
assertEquals(expected, port.authenticatedDatabases);
- m.getAuthority().getCredentialsStore().add(new MongoCredential("u2", "e".toCharArray(), "DBPortTest3"));
+ m.getAuthority().getCredentialsStore().add(MongoCredential.createMongoCRCredential("u2", "DBPortTest3", "e".toCharArray()));
try {
port.checkAuth(m);
View
2  src/test/com/mongodb/JavaClientTest.java
@@ -661,7 +661,7 @@ public void testAuthenticate() throws UnknownHostException {
assertNull(db.getAuthenticationCredentials());
assertNull(_mongo.getAuthority().getCredentialsStore().get(db.getName()));
assertEquals(true, db.authenticate("xx", "e".toCharArray()));
- assertEquals(new MongoCredential("xx", "e".toCharArray(), db.getName()), db.getAuthenticationCredentials());
+ assertEquals(MongoCredential.createMongoCRCredential("xx", db.getName(), "e".toCharArray()), db.getAuthenticationCredentials());
assertEquals(db.getAuthenticationCredentials(), m.getAuthority().getCredentialsStore().get(db.getName()));
assertEquals(true, db.authenticate( "xx" , "e".toCharArray() ) );
View
3  src/test/com/mongodb/MongoClientTest.java
@@ -63,7 +63,8 @@ public void testConstructors() throws UnknownHostException {
Assert.assertEquals(emptyCredentials, mc.getCredentialsList());
mc.close();
- final List<MongoCredential> credentialsList = Arrays.asList(new MongoCredential("user1", "pwd".toCharArray(), "test"));
+ final List<MongoCredential> credentialsList = Arrays.asList(
+ MongoCredential.createMongoCRCredential("user1", "test", "pwd".toCharArray()));
mc = new MongoClient(new ServerAddress("127.0.0.1"), credentialsList);
Assert.assertEquals(new ServerAddress("127.0.0.1"), mc.getAddress());
Assert.assertEquals(defaultOptions, mc.getMongoOptions());
View
20 src/test/com/mongodb/MongoClientURITest.java
@@ -81,30 +81,34 @@ public void testBasic2() {
@Test()
public void testUserPass() {
+ final String userName = "user";
+ final char[] password = "pass".toCharArray();
+
MongoClientURI u = new MongoClientURI("mongodb://user:pass@host/bar");
assertEquals(1, u.getHosts().size());
assertEquals("host", u.getHosts().get(0));
- assertEquals("user", u.getUsername());
- assertEquals("pass", new String(u.getPassword()));
- assertEquals(new MongoCredential("user", "pass".toCharArray(), MongoAuthenticationMechanism.MONGO_CR, "bar"), u.getCredentials());
+ assertEquals(userName, u.getUsername());
+ assertArrayEquals(password, u.getPassword());
+
+ assertEquals(MongoCredential.createMongoCRCredential(userName, "bar", password), u.getCredentials());
u = new MongoClientURI("mongodb://user@host/?authProtocol=GSSAPI");
- assertEquals(new MongoCredential("user", MongoAuthenticationMechanism.GSSAPI), u.getCredentials());
+ assertEquals(MongoCredential.createGSSAPICredential(userName), u.getCredentials());
u = new MongoClientURI("mongodb://user:pass@host/?authProtocol=MONGO-CR");
- assertEquals(new MongoCredential("user", "pass".toCharArray(), MongoAuthenticationMechanism.MONGO_CR), u.getCredentials());
+ assertEquals(MongoCredential.createMongoCRCredential(userName, "admin", password), u.getCredentials());
u = new MongoClientURI("mongodb://user:pass@host/?authSource=test");
- assertEquals(new MongoCredential("user", "pass".toCharArray(), "test"), u.getCredentials());
+ assertEquals(MongoCredential.createMongoCRCredential(userName, "test", password), u.getCredentials());
u = new MongoClientURI("mongodb://user:pass@host");
- assertEquals(new MongoCredential("user", "pass".toCharArray(), "admin"), u.getCredentials());
+ assertEquals(MongoCredential.createMongoCRCredential(userName, "admin", password), u.getCredentials());
}
@Test
public void testURIEncoding() {
MongoClientURI u = new MongoClientURI("mongodb://use%24:he%21%21o@localhost");
- assertEquals(new MongoCredential("use$", "he!!o".toCharArray()), u.getCredentials());
+ assertEquals(MongoCredential.createMongoCRCredential("use$", "admin", "he!!o".toCharArray()), u.getCredentials());
}
@Test()
View
57 src/test/com/mongodb/MongoCredentialTest.java
@@ -31,49 +31,24 @@
public void testCredentials() {
MongoCredential credentials;
- credentials = new MongoCredential("user", "pwd".toCharArray());
- assertEquals("user", credentials.getUserName());
- assertArrayEquals("pwd".toCharArray(), credentials.getPassword());
- assertEquals(MongoAuthenticationMechanism.MONGO_CR, credentials.getMechanism());
- assertEquals("admin", credentials.getSource());
-
- credentials = new MongoCredential("user", "pwd".toCharArray(), "test");
- assertEquals("user", credentials.getUserName());
- assertArrayEquals("pwd".toCharArray(), credentials.getPassword());
- assertEquals(MongoAuthenticationMechanism.MONGO_CR, credentials.getMechanism());
- assertEquals("test", credentials.getSource());
-
- credentials = new MongoCredential("user", "pwd".toCharArray(), MongoAuthenticationMechanism.MONGO_CR);
- assertEquals("user", credentials.getUserName());
- assertArrayEquals("pwd".toCharArray(), credentials.getPassword());
- assertEquals(MongoAuthenticationMechanism.MONGO_CR, credentials.getMechanism());
- assertEquals("admin", credentials.getSource());
-
- credentials = new MongoCredential("user", MongoAuthenticationMechanism.GSSAPI);
- assertEquals("user", credentials.getUserName());
- assertNull(credentials.getPassword());
- assertEquals(MongoAuthenticationMechanism.GSSAPI, credentials.getMechanism());
- assertEquals("$external", credentials.getSource());
-
- credentials = new MongoCredential("user", "pwd".toCharArray(), MongoAuthenticationMechanism.MONGO_CR, "test");
- assertEquals("user", credentials.getUserName());
- assertArrayEquals("pwd".toCharArray(), credentials.getPassword());
- assertEquals(MongoAuthenticationMechanism.MONGO_CR, credentials.getMechanism());
- assertEquals("test", credentials.getSource());
+ final String mechanism = MongoCredential.MONGO_CR_MECHANISM;
+ final String userName = "user";
+ final String database = "test";
+ final char[] password = "pwd".toCharArray();
+ credentials = MongoCredential.createMongoCRCredential(userName, database, password);
+
+ assertEquals(mechanism, credentials.getMechanism());
+ assertEquals(userName, credentials.getUserName());
+ assertEquals(database, credentials.getSource());
+ assertArrayEquals(password, credentials.getPassword());
+ assertEquals(MongoCredential.MONGO_CR_MECHANISM, credentials.getMechanism());
try {
- new MongoCredential("user", null, MongoAuthenticationMechanism.MONGO_CR, "test");
+ MongoCredential.createMongoCRCredential(userName, database, null);
fail("MONGO-CR must have a password");
} catch (IllegalArgumentException e) {
// all good
}
-
- try {
- new MongoCredential("user", "a".toCharArray(), MongoAuthenticationMechanism.GSSAPI);
- fail("GSSAPI must not have a password");
- } catch (IllegalArgumentException e) {
- // all good
- }
}
@Test
@@ -89,7 +64,7 @@ public void testCredentialsStore() {
assertTrue(store.getDatabases().isEmpty());
assertNull(store.get("test"));
- MongoCredential credentials = new MongoCredential("user", password);
+ MongoCredential credentials = MongoCredential.createMongoCRCredential("user", "admin", password);
store = new MongoCredentialsStore(credentials);
Set<String> expected;
expected = new HashSet<String>();
@@ -100,8 +75,8 @@ public void testCredentialsStore() {
List<MongoCredential> credentialsList;
- final MongoCredential credentials1 = new MongoCredential("user", password, "db1");
- final MongoCredential credentials2 = new MongoCredential("user", "pwd".toCharArray(), "db2");
+ final MongoCredential credentials1 = MongoCredential.createMongoCRCredential("user", "db1", password);
+ final MongoCredential credentials2 = MongoCredential.createMongoCRCredential("user", "db2", password);
credentialsList = Arrays.asList(credentials1, credentials2);
store = new MongoCredentialsStore(credentialsList);
expected = new HashSet<String>();
@@ -113,7 +88,7 @@ public void testCredentialsStore() {
assertNull(store.get("db3"));
assertEquals(credentialsList, store.asList());
- credentialsList = Arrays.asList(credentials1, new MongoCredential("user2", password, "db1"));
+ credentialsList = Arrays.asList(credentials1, MongoCredential.createMongoCRCredential("user2", "db1", password));
try {
new MongoCredentialsStore(credentialsList);
fail("should throw");
View
2  src/test/com/mongodb/MongoURITest.java
@@ -32,7 +32,7 @@ public void testGetters() {
MongoURI mongoURI = new MongoURI( "mongodb://user:pwd@localhost/test.mongoURITest?safe=false");
assertEquals("user", mongoURI.getUsername());
assertEquals("pwd", new String(mongoURI.getPassword()));
- assertEquals(new MongoCredential("user", "pwd".toCharArray(), MongoAuthenticationMechanism.MONGO_CR, "test"), mongoURI.getCredentials());
+ assertEquals(MongoCredential.createMongoCRCredential("user", "test", "pwd".toCharArray()), mongoURI.getCredentials());
assertEquals(Arrays.asList("localhost"), mongoURI.getHosts());
assertEquals("test", mongoURI.getDatabase());
assertEquals("mongoURITest", mongoURI.getCollection());
Please sign in to comment.
Something went wrong with that request. Please try again.