Skip to content

Commit 6ab6478

Browse files
committed
JAVA-691: Removed authentication mechanism negotiation. Renamed MongoAuthenticationProtocol to MongoAuthenticationMechanism
1 parent 3784566 commit 6ab6478

File tree

10 files changed

+124
-92
lines changed

10 files changed

+124
-92
lines changed

examples/GSSAPICredentialsExample.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@
1414
* limitations under the License.
1515
*/
1616

17+
import com.mongodb.BasicDBObject;
1718
import com.mongodb.DB;
18-
import com.mongodb.MongoAuthenticationProtocol;
19+
import com.mongodb.MongoAuthenticationMechanism;
1920
import com.mongodb.MongoClient;
2021
import com.mongodb.MongoClientOptions;
2122
import com.mongodb.MongoCredential;
@@ -45,7 +46,7 @@ public class GSSAPICredentialsExample {
4546
// auth.login.defaultCallbackHandler=name of class that implements javax.security.auth.callback.CallbackHandler
4647
public static void main(String[] args) throws UnknownHostException, InterruptedException {
4748
// Set this property to avoid the default behavior where the program prompts on the command line for username/password
48-
Security.setProperty("auth.login.defaultCallbackHandler", "DefaultSecurityCallbackHandler");
49+
// Security.setProperty("auth.login.defaultCallbackHandler", "DefaultSecurityCallbackHandler");
4950

5051
String server = args[0];
5152
String user = args[1];
@@ -68,10 +69,11 @@ public static void main(String[] args) throws UnknownHostException, InterruptedE
6869
System.out.println();
6970

7071
MongoClient mongoClient = new MongoClient(new ServerAddress(server),
71-
Arrays.asList(new MongoCredential(user, MongoAuthenticationProtocol.GSSAPI)),
72+
Arrays.asList(new MongoCredential(user, MongoAuthenticationMechanism.GSSAPI)),
7273
new MongoClientOptions.Builder().socketKeepAlive(true).socketTimeout(30000).build());
7374
DB testDB = mongoClient.getDB(databaseName);
7475

76+
System.out.println("Insert result: " + testDB.getCollection("test").insert(new BasicDBObject()));
7577
System.out.println("Count: " + testDB.getCollection("test").count());
7678
}
7779
}
Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
1-
/**
2-
* Copyright (c) 2008 - 2012 10gen, Inc. <http://10gen.com>
3-
* <p/>
1+
/*
2+
* Copyright (c) 2008 - 2013 10gen, Inc. <http://10gen.com>
3+
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
7-
* <p/>
8-
* http://www.apache.org/licenses/LICENSE-2.0
9-
* <p/>
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
1212
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
15+
*
1516
*/
1617

1718
import com.mongodb.BasicDBObject;
1819
import com.mongodb.DB;
19-
import com.mongodb.MongoAuthenticationProtocol;
20+
import com.mongodb.MongoAuthenticationMechanism;
2021
import com.mongodb.MongoClient;
2122
import com.mongodb.MongoClientOptions;
2223
import com.mongodb.MongoCredential;
@@ -25,28 +26,27 @@
2526
import java.net.UnknownHostException;
2627
import java.util.Arrays;
2728

28-
/**
29-
* Example usage of NEGOTIATE authentication protocol.
30-
* <p>
31-
* Usage:
32-
* </p>
33-
* <pre>
34-
* java NegotiatedAuthenticationProtocolExample server userName password databaseName
35-
* </pre>
36-
*/
37-
public class NegotiatedAuthenticationProtocolExample {
38-
public static void main(String[] args) throws UnknownHostException, InterruptedException {
29+
public class MongoCredentialsExample {
30+
public static void main(String[] args) throws UnknownHostException {
3931
String server = args[0];
4032
String user = args[1];
41-
String pwd = args[2];
42-
String db = args[3];
33+
String password = args[2];
34+
String databaseName = args[3];
35+
36+
System.out.println("server: " + server);
37+
System.out.println("user: " + user);
38+
System.out.println("database: " + databaseName);
4339

44-
MongoCredential credentials = new MongoCredential(user, pwd.toCharArray(), MongoAuthenticationProtocol.NEGOTIATE, db);
40+
System.out.println();
4541

46-
MongoClient mongoClient = new MongoClient(new ServerAddress(server), Arrays.asList(credentials), new MongoClientOptions.Builder().build());
42+
MongoClient mongoClient = new MongoClient(new ServerAddress(server),
43+
Arrays.asList(new MongoCredential(user, password.toCharArray(), MongoAuthenticationMechanism.MONGO_CR, "test")),
44+
new MongoClientOptions.Builder().socketKeepAlive(true).socketTimeout(30000).build());
45+
DB testDB = mongoClient.getDB(databaseName);
4746

48-
DB testDB = mongoClient.getDB(db);
49-
testDB.getCollection("test").insert(new BasicDBObject());
5047
System.out.println("Count: " + testDB.getCollection("test").count());
48+
49+
System.out.println("Insert result: " + testDB.getCollection("test").insert(new BasicDBObject()));
50+
5151
}
5252
}

src/main/com/mongodb/DB.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,7 @@ 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, MongoAuthenticationProtocol.NEGOTIATE, getName());
617+
MongoCredential credentials = new MongoCredential(username, password, MongoAuthenticationMechanism.MONGO_CR, getName());
618618

619619
if (getAuthenticationCredentials() != null) {
620620
if (getAuthenticationCredentials().equals(credentials)) {

src/main/com/mongodb/DBPort.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -313,12 +313,12 @@ protected void close(){
313313

314314
CommandResult authenticate(Mongo mongo, final MongoCredential credentials) {
315315
Authenticator authenticator;
316-
if (credentials.getProtocol() == MongoAuthenticationProtocol.NEGOTIATE) {
317-
authenticator = getStrongestAuthenticator(mongo, credentials);
318-
} else if (credentials.getProtocol().equals(MongoAuthenticationProtocol.GSSAPI)) {
316+
if (credentials.getMechanism() == MongoAuthenticationMechanism.MONGO_CR) {
317+
authenticator = new NativeAuthenticator(mongo, credentials);
318+
} else if (credentials.getMechanism().equals(MongoAuthenticationMechanism.GSSAPI)) {
319319
authenticator = new GSSAPIAuthenticator(mongo, credentials);
320320
} else {
321-
throw new IllegalArgumentException("Unsupported authentication protocol: " + credentials.getProtocol());
321+
throw new IllegalArgumentException("Unsupported authentication protocol: " + credentials.getMechanism());
322322
}
323323
CommandResult res = authenticator.authenticate();
324324
authenticatedDatabases.add(credentials.getSource());
@@ -459,8 +459,8 @@ class GSSAPIAuthenticator extends SaslAuthenticator {
459459
GSSAPIAuthenticator(final Mongo mongo, final MongoCredential credentials) {
460460
super(mongo, credentials);
461461

462-
if (!this.credentials.getProtocol().equals(MongoAuthenticationProtocol.GSSAPI)) {
463-
throw new MongoException("Incorrect mechanism: " + this.credentials.getProtocol());
462+
if (!this.credentials.getMechanism().equals(MongoAuthenticationMechanism.GSSAPI)) {
463+
throw new MongoException("Incorrect mechanism: " + this.credentials.getMechanism());
464464
}
465465
}
466466

src/main/com/mongodb/MongoAuthenticationProtocol.java renamed to src/main/com/mongodb/MongoAuthenticationMechanism.java

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
package com.mongodb;
1919

2020
/**
21-
* An enumeration of the supported authentication protocols.
21+
* An enumeration of the supported authentication mechanisms.
2222
*/
23-
public enum MongoAuthenticationProtocol {
23+
public enum MongoAuthenticationMechanism {
2424
/**
2525
* The GSSAPI protocol, to support Kerberos v5 via a SASL-based authentication protocol
2626
*/
@@ -33,19 +33,29 @@ public enum MongoAuthenticationProtocol {
3333
public String getDefaultSource() {
3434
return "$external";
3535
}
36+
37+
@Override
38+
public String getMechanismName() {
39+
return "GSSAPI";
40+
}
3641
},
3742
/**
38-
* Negotiate the strongest available protocol available. This is the default protocol.
43+
* The native MongoDB authentication mechanism. This is an abbreviation for MongoDB Challenge Response.
3944
*/
40-
NEGOTIATE {
45+
MONGO_CR {
4146
/**
4247
* The default source is the "admin" database.
43-
* @return
48+
* @return the "admin" database
4449
*/
4550
@Override
4651
public String getDefaultSource() {
4752
return "admin";
4853
}
54+
55+
@Override
56+
public String getMechanismName() {
57+
return "MONGO-CR";
58+
}
4959
};
5060

5161
/**
@@ -54,4 +64,20 @@ public String getDefaultSource() {
5464
* @return the default database for this protocol
5565
*/
5666
public abstract String getDefaultSource();
67+
68+
/**
69+
* Gets the mechanism name.
70+
* @return the name
71+
*/
72+
public abstract String getMechanismName();
73+
74+
public static MongoAuthenticationMechanism byMechanismName(String name) {
75+
if (name.equals(GSSAPI.getMechanismName())) {
76+
return GSSAPI;
77+
}
78+
if (name.equals(MONGO_CR.getMechanismName())) {
79+
return MONGO_CR;
80+
}
81+
throw new IllegalArgumentException("Invalid authentication mechanism name: " + name);
82+
}
5783
}

src/main/com/mongodb/MongoClientURI.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,8 @@
127127
* </ul>
128128
* <p>Authentication configuration:</p>
129129
* <ul>
130-
* <li>{@code authProtocol=NEGOTIATE|GSSAPI}: The authentication protocol to use. The default is NEGOTIATE.
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.
131132
* </li>
132133
* <li>{@code authSource=string}: The source of the authentication credentials. This is typically the database that
133134
* the credentials have been created. The value defaults to the database specified in the path portion of the URI.
@@ -388,7 +389,7 @@ private MongoCredential createCredentials(Map<String, List<String>> optionsMap,
388389
return null;
389390
}
390391

391-
MongoAuthenticationProtocol protocol = MongoAuthenticationProtocol.NEGOTIATE;
392+
MongoAuthenticationMechanism protocol = MongoAuthenticationMechanism.MONGO_CR;
392393
String authSource = database;
393394

394395
for (String key : authKeys) {
@@ -399,7 +400,7 @@ private MongoCredential createCredentials(Map<String, List<String>> optionsMap,
399400
}
400401

401402
if (key.equals("authprotocol")) {
402-
protocol = MongoAuthenticationProtocol.valueOf(value);
403+
protocol = MongoAuthenticationMechanism.byMechanismName(value);
403404
} else if (key.equals("authsource")) {
404405
authSource = value;
405406
}

0 commit comments

Comments
 (0)