use of com.mongodb.MongoCredential in project mongo-java-driver by mongodb.
the class ConnectionStringTest method testValidAuth.
private void testValidAuth() {
ConnectionString connectionString = null;
try {
connectionString = new ConnectionString(input);
} catch (Throwable t) {
if (description.contains("without password")) {
// We don't allow null passwords without setting the authentication mechanism.
return;
} else {
assertTrue(String.format("Connection string '%s' should not have throw an exception: %s", input, t.toString()), false);
}
}
assertString("auth.db", getAuthDB(connectionString));
assertString("auth.username", connectionString.getUsername());
// Passwords for certain auth mechanisms are ignored.
String password = null;
if (connectionString.getPassword() != null) {
password = new String(connectionString.getPassword());
}
List<MongoCredential> credentials = connectionString.getCredentialList();
if (credentials.size() > 0) {
AuthenticationMechanism mechanism = credentials.get(0).getAuthenticationMechanism();
if (mechanism == null) {
assertString("auth.password", password);
} else {
switch(mechanism) {
case PLAIN:
case MONGODB_CR:
case SCRAM_SHA_1:
assertString("auth.password", password);
break;
default:
}
}
} else {
assertString("auth.password", password);
}
}
use of com.mongodb.MongoCredential in project mongo-java-driver by mongodb.
the class PlainAuthenticator method createSaslClient.
@Override
protected SaslClient createSaslClient(final ServerAddress serverAddress) {
final MongoCredential credential = getCredential();
isTrue("mechanism is PLAIN", credential.getAuthenticationMechanism() == PLAIN);
try {
return Sasl.createSaslClient(new String[] { PLAIN.getMechanismName() }, credential.getUserName(), DEFAULT_PROTOCOL, serverAddress.getHost(), null, new CallbackHandler() {
@Override
public void handle(final Callback[] callbacks) throws IOException, UnsupportedCallbackException {
for (final Callback callback : callbacks) {
if (callback instanceof PasswordCallback) {
((PasswordCallback) callback).setPassword(credential.getPassword());
} else if (callback instanceof NameCallback) {
((NameCallback) callback).setName(credential.getUserName());
}
}
}
});
} catch (SaslException e) {
throw new MongoSecurityException(credential, "Exception initializing SASL client", e);
}
}
use of com.mongodb.MongoCredential in project mongo-java-driver by mongodb.
the class GSSAPIAuthenticator method createSaslClient.
@Override
protected SaslClient createSaslClient(final ServerAddress serverAddress) {
MongoCredential credential = getCredential();
try {
Map<String, Object> saslClientProperties = getCredential().getMechanismProperty(JAVA_SASL_CLIENT_PROPERTIES_KEY, null);
if (saslClientProperties == null) {
saslClientProperties = new HashMap<String, Object>();
saslClientProperties.put(Sasl.MAX_BUFFER, "0");
saslClientProperties.put(Sasl.CREDENTIALS, getGSSCredential(credential.getUserName()));
}
SaslClient saslClient = Sasl.createSaslClient(new String[] { GSSAPI.getMechanismName() }, credential.getUserName(), credential.getMechanismProperty(SERVICE_NAME_KEY, SERVICE_NAME_DEFAULT_VALUE), getHostName(serverAddress), saslClientProperties, null);
if (saslClient == null) {
throw new MongoSecurityException(credential, String.format("No platform support for %s mechanism", GSSAPI));
}
return saslClient;
} catch (SaslException e) {
throw new MongoSecurityException(credential, "Exception initializing SASL client", e);
} catch (GSSException e) {
throw new MongoSecurityException(credential, "Exception initializing GSSAPI credentials", e);
} catch (UnknownHostException e) {
throw new MongoSecurityException(credential, "Unable to canonicalize host name + " + serverAddress);
}
}
use of com.mongodb.MongoCredential in project spring-boot by spring-projects.
the class MongoClientFactoryTests method uriCanBeCustomized.
@Test
public void uriCanBeCustomized() {
MongoProperties properties = new MongoProperties();
properties.setUri("mongodb://user:secret@mongo1.example.com:12345," + "mongo2.example.com:23456/test");
MongoClient client = createMongoClient(properties);
List<ServerAddress> allAddresses = extractServerAddresses(client);
assertThat(allAddresses).hasSize(2);
assertServerAddress(allAddresses.get(0), "mongo1.example.com", 12345);
assertServerAddress(allAddresses.get(1), "mongo2.example.com", 23456);
List<MongoCredential> credentialsList = client.getCredentialsList();
assertThat(credentialsList).hasSize(1);
assertMongoCredential(credentialsList.get(0), "user", "secret", "test");
}
use of com.mongodb.MongoCredential in project spring-boot by spring-projects.
the class ReactiveMongoClientFactoryTests method uriCanBeCustomized.
@Test
public void uriCanBeCustomized() throws UnknownHostException {
MongoProperties properties = new MongoProperties();
properties.setUri("mongodb://user:secret@mongo1.example.com:12345," + "mongo2.example.com:23456/test");
MongoClient client = createMongoClient(properties);
List<ServerAddress> allAddresses = extractServerAddresses(client);
assertThat(allAddresses).hasSize(2);
assertServerAddress(allAddresses.get(0), "mongo1.example.com", 12345);
assertServerAddress(allAddresses.get(1), "mongo2.example.com", 23456);
List<MongoCredential> credentialsList = extractMongoCredentials(client);
assertThat(credentialsList).hasSize(1);
assertMongoCredential(credentialsList.get(0), "user", "secret", "test");
}
Aggregations