Permalink
Browse files

JAVA-691: Renamed MongoCredentials to MongoCredential and moved inner…

… Protocol enum to top level enum MongoAuthenticationProtocol
  • Loading branch information...
1 parent 021bc1d commit f5150708b4f8bb9fe0eadf94f2b2501cc8c1f951 @jyemin jyemin committed Jan 29, 2013
@@ -15,9 +15,10 @@
*/
import com.mongodb.DB;
+import com.mongodb.MongoAuthenticationProtocol;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
-import com.mongodb.MongoCredentials;
+import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import java.net.UnknownHostException;
@@ -67,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 MongoCredentials(user, MongoCredentials.Protocol.GSSAPI)),
+ Arrays.asList(new MongoCredential(user, MongoAuthenticationProtocol.GSSAPI)),
new MongoClientOptions.Builder().socketKeepAlive(true).socketTimeout(30000).build());
DB testDB = mongoClient.getDB(databaseName);
@@ -16,9 +16,10 @@
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
+import com.mongodb.MongoAuthenticationProtocol;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
-import com.mongodb.MongoCredentials;
+import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import java.net.UnknownHostException;
@@ -40,7 +41,7 @@ public static void main(String[] args) throws UnknownHostException, InterruptedE
String pwd = args[2];
String db = args[3];
- MongoCredentials credentials = new MongoCredentials(user, pwd.toCharArray(), MongoCredentials.Protocol.NEGOTIATE, db);
+ MongoCredential credentials = new MongoCredential(user, pwd.toCharArray(), MongoAuthenticationProtocol.NEGOTIATE, db);
MongoClient mongoClient = new MongoClient(new ServerAddress(server), Arrays.asList(credentials), new MongoClientOptions.Builder().build());
@@ -614,7 +614,7 @@ public synchronized CommandResult authenticateCommand(String username, char[] pa
}
private CommandResultPair authenticateCommandHelper(String username, char[] password) {
- MongoCredentials credentials = new MongoCredentials(username, password, MongoCredentials.Protocol.NEGOTIATE, getName());
+ MongoCredential credentials = new MongoCredential(username, password, MongoAuthenticationProtocol.NEGOTIATE, getName());
if (getAuthenticationCredentials() != null) {
if (getAuthenticationCredentials().equals(credentials)) {
@@ -647,7 +647,7 @@ public CommandResultPair(final CommandResult.CommandFailure failure) {
}
}
- abstract CommandResult doAuthenticate(MongoCredentials credentials);
+ abstract CommandResult doAuthenticate(MongoCredential credentials);
/**
* Adds a new user for this db
@@ -804,7 +804,7 @@ public int getOptions(){
public abstract void cleanCursors( boolean force );
- MongoCredentials getAuthenticationCredentials() {
+ MongoCredential getAuthenticationCredentials() {
return getMongo().getAuthority().getCredentialsStore().get(getName());
}
@@ -176,7 +176,7 @@ void killCursors( ServerAddress addr , List<Long> all ){
}
@Override
- CommandResult doAuthenticate(MongoCredentials credentials) {
+ CommandResult doAuthenticate(MongoCredential credentials) {
return _connector.authenticate(credentials);
}
@@ -112,5 +112,5 @@ public Response call( DB db , DBCollection coll , OutMessage m ,
* @throws com.mongodb.CommandResult.CommandFailure if the authentication failed
* @since 2.11.0
*/
- public CommandResult authenticate(MongoCredentials credentials);
+ public CommandResult authenticate(MongoCredential credentials);
}
@@ -311,11 +311,11 @@ protected void close(){
_socket = null;
}
- CommandResult authenticate(Mongo mongo, final MongoCredentials credentials) {
+ CommandResult authenticate(Mongo mongo, final MongoCredential credentials) {
Authenticator authenticator;
- if (credentials.getProtocol() == MongoCredentials.Protocol.NEGOTIATE) {
+ if (credentials.getProtocol() == MongoAuthenticationProtocol.NEGOTIATE) {
authenticator = getStrongestAuthenticator(mongo, credentials);
- } else if (credentials.getProtocol().equals(MongoCredentials.Protocol.GSSAPI)) {
+ } else if (credentials.getProtocol().equals(MongoAuthenticationProtocol.GSSAPI)) {
authenticator = new GSSAPIAuthenticator(mongo, credentials);
} else {
throw new IllegalArgumentException("Unsupported authentication protocol: " + credentials.getProtocol());
@@ -335,7 +335,7 @@ void checkAuth(Mongo mongo) throws IOException {
}
}
- private Authenticator getStrongestAuthenticator(final Mongo mongo, MongoCredentials credentials) {
+ private Authenticator getStrongestAuthenticator(final Mongo mongo, MongoCredential credentials) {
if (useCRAMAuthenticationProtocol == null) {
cacheStrongestAuthenticationProtocol(mongo);
}
@@ -407,7 +407,7 @@ public DBPortPool getPool() {
private final String mechanism;
- GenericSaslAuthenticator(final Mongo mongo, MongoCredentials credentials, String mechanism) {
+ GenericSaslAuthenticator(final Mongo mongo, MongoCredential credentials, String mechanism) {
super(mongo, credentials);
this.mechanism = mechanism;
}
@@ -456,10 +456,10 @@ public void handle(final Callback[] callbacks) throws IOException, UnsupportedCa
public static final String GSSAPI_OID = "1.2.840.113554.1.2.2";
public static final String GSSAPI_MECHANISM = "GSSAPI";
- GSSAPIAuthenticator(final Mongo mongo, final MongoCredentials credentials) {
+ GSSAPIAuthenticator(final Mongo mongo, final MongoCredential credentials) {
super(mongo, credentials);
- if (!this.credentials.getProtocol().equals(MongoCredentials.Protocol.GSSAPI)) {
+ if (!this.credentials.getProtocol().equals(MongoAuthenticationProtocol.GSSAPI)) {
throw new MongoException("Incorrect mechanism: " + this.credentials.getProtocol());
}
}
@@ -501,7 +501,7 @@ private GSSCredential getGSSCredential(String userName) throws GSSException {
abstract class SaslAuthenticator extends Authenticator {
public static final String MONGODB_PROTOCOL = "mongodb";
- SaslAuthenticator(final Mongo mongo, MongoCredentials credentials) {
+ SaslAuthenticator(final Mongo mongo, MongoCredential credentials) {
super(mongo, credentials);
}
@@ -558,7 +558,7 @@ private CommandResult sendSaslContinue(final int conversationId, final byte[] ou
}
class NativeAuthenticator extends Authenticator {
- NativeAuthenticator(Mongo mongo, MongoCredentials credentials) {
+ NativeAuthenticator(Mongo mongo, MongoCredential credentials) {
super(mongo, credentials);
}
@@ -581,9 +581,9 @@ public CommandResult authenticate() {
abstract class Authenticator {
protected final Mongo mongo;
- protected final MongoCredentials credentials;
+ protected final MongoCredential credentials;
- Authenticator(Mongo mongo, MongoCredentials credentials) {
+ Authenticator(Mongo mongo, MongoCredential credentials) {
this.mongo = mongo;
this.credentials = credentials;
}
@@ -597,7 +597,7 @@ public boolean isOpen(){
}
@Override
- public CommandResult authenticate(MongoCredentials credentials) {
+ public CommandResult authenticate(MongoCredential credentials) {
final MyPort mp = _myPort.get();
final DBPort port = mp.get(false, ReadPreference.primaryPreferred(), null);
@@ -0,0 +1,57 @@
+/*
+ * 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 protocols.
+ */
+public enum MongoAuthenticationProtocol {
+ /**
+ * 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";
+ }
+ },
+ /**
+ * Negotiate the strongest available protocol available. This is the default protocol.
+ */
+ NEGOTIATE {
+ /**
+ * The default source is the "admin" database.
+ * @return
+ */
+ @Override
+ public String getDefaultSource() {
+ return "admin";
+ }
+ };
+
+ /**
+ * Gets the default source for this mechanism, usually a database name.
+ *
+ * @return the default database for this protocol
+ */
+ public abstract String getDefaultSource();
+}
@@ -52,7 +52,7 @@
* @return
*/
public static MongoAuthority direct(ServerAddress serverAddress) {
- return direct(serverAddress, (MongoCredentials) null);
+ return direct(serverAddress, (MongoCredential) null);
}
/**
@@ -61,7 +61,7 @@ public static MongoAuthority direct(ServerAddress serverAddress) {
* @param credentials
* @return
*/
- public static MongoAuthority direct(ServerAddress serverAddress, MongoCredentials credentials) {
+ public static MongoAuthority direct(ServerAddress serverAddress, MongoCredential credentials) {
return direct(serverAddress, new MongoCredentialsStore(credentials));
}
@@ -81,7 +81,7 @@ public static MongoAuthority direct(ServerAddress serverAddress, MongoCredential
* @return
*/
public static MongoAuthority dynamicSet(List<ServerAddress> serverAddresses) {
- return dynamicSet(serverAddresses, (MongoCredentials) null);
+ return dynamicSet(serverAddresses, (MongoCredential) null);
}
/**
@@ -90,7 +90,7 @@ public static MongoAuthority dynamicSet(List<ServerAddress> serverAddresses) {
* @param credentials
* @return
*/
- public static MongoAuthority dynamicSet(List<ServerAddress> serverAddresses, MongoCredentials credentials) {
+ public static MongoAuthority dynamicSet(List<ServerAddress> serverAddresses, MongoCredential credentials) {
return dynamicSet(serverAddresses, new MongoCredentialsStore(credentials));
}
@@ -135,7 +135,7 @@ public MongoClient(ServerAddress addr) {
* @see com.mongodb.ServerAddress
* @since 2.11.0
*/
- public MongoClient(ServerAddress addr, List<MongoCredentials> credentialsList) {
+ public MongoClient(ServerAddress addr, List<MongoCredential> credentialsList) {
this(addr, credentialsList, new MongoClientOptions.Builder().build());
}
@@ -161,7 +161,7 @@ public MongoClient(ServerAddress addr, MongoClientOptions options) {
* @see com.mongodb.ServerAddress
* @since 2.11.0
*/
- public MongoClient(ServerAddress addr, List<MongoCredentials> credentialsList, MongoClientOptions options) {
+ public MongoClient(ServerAddress addr, List<MongoCredential> credentialsList, MongoClientOptions options) {
super(MongoAuthority.direct(addr, new MongoCredentialsStore(credentialsList)), new MongoOptions(options));
}
@@ -201,7 +201,7 @@ public MongoClient(List<ServerAddress> seeds) {
* @see com.mongodb.ServerAddress
* @since 2.11.0
*/
- public MongoClient(List<ServerAddress> seeds, List<MongoCredentials> credentialsList) {
+ public MongoClient(List<ServerAddress> seeds, List<MongoCredential> credentialsList) {
this(seeds, credentialsList, new MongoClientOptions.Builder().build());
}
@@ -244,7 +244,7 @@ public MongoClient(List<ServerAddress> seeds, MongoClientOptions options) {
* @see com.mongodb.ServerAddress
* @since 2.11.0
*/
- public MongoClient(List<ServerAddress> seeds, List<MongoCredentials> credentialsList, MongoClientOptions options) {
+ public MongoClient(List<ServerAddress> seeds, List<MongoCredential> credentialsList, MongoClientOptions options) {
super(MongoAuthority.dynamicSet(seeds, new MongoCredentialsStore(credentialsList)), new MongoOptions(options));
}
@@ -268,7 +268,7 @@ public MongoClient(MongoClientURI uri) throws UnknownHostException {
* @return the list of credentials
* @since 2.11.0
*/
- public List<MongoCredentials> getCredentialsList() {
+ public List<MongoCredential> getCredentialsList() {
return getAuthority().getCredentialsStore().asList();
}
}
@@ -375,13 +375,13 @@ private ReadPreference createReadPreference(final Map<String, List<String>> opti
return buildReadPreference(readPreferenceType, firstTagSet, remainingTagSets, slaveOk);
}
- private MongoCredentials createCredentials(Map<String, List<String>> optionsMap, final String userName,
+ private MongoCredential createCredentials(Map<String, List<String>> optionsMap, final String userName,
final char[] password, String database) {
if (userName == null) {
return null;
}
- MongoCredentials.Protocol protocol = MongoCredentials.Protocol.NEGOTIATE;
+ MongoAuthenticationProtocol protocol = MongoAuthenticationProtocol.NEGOTIATE;
String authSource = database;
for (String key : authKeys) {
@@ -392,13 +392,13 @@ private MongoCredentials createCredentials(Map<String, List<String>> optionsMap,
}
if (key.equals("authprotocol")) {
- protocol = MongoCredentials.Protocol.valueOf(value);
+ protocol = MongoAuthenticationProtocol.valueOf(value);
} else if (key.equals("authsource")) {
authSource = value;
}
}
- return new MongoCredentials(userName, password, protocol, authSource);
+ return new MongoCredential(userName, password, protocol, authSource);
}
private String getLastValue(final Map<String, List<String>> optionsMap, final String key) {
@@ -551,7 +551,7 @@ public String getURI() {
*
* @return the credentials
*/
- public MongoCredentials getCredentials() {
+ public MongoCredential getCredentials() {
return credentials;
}
@@ -567,7 +567,7 @@ public MongoClientOptions getOptions() {
// ---------------------------------
private final MongoClientOptions options;
- private final MongoCredentials credentials;
+ private final MongoCredential credentials;
private final List<String> hosts;
private final String database;
private final String collection;
Oops, something went wrong.

0 comments on commit f515070

Please sign in to comment.