Java 类com.mongodb.AuthenticationMechanism 实例源码
项目:calcite
文件:MongoSchemaFactory.java
private MongoCredential createCredentials(Map<String, Object> map) {
final String authMechanismName = (String) map.get("authMechanism");
final AuthenticationMechanism authenticationMechanism =
AuthenticationMechanism.fromMechanismName(authMechanismName);
final String username = (String) map.get("username");
final String authDatabase = (String) map.get("authDatabase");
final String password = (String) map.get("password");
switch (authenticationMechanism) {
case PLAIN:
return MongoCredential.createPlainCredential(username, authDatabase,
password.toCharArray());
case SCRAM_SHA_1:
return MongoCredential.createScramSha1Credential(username, authDatabase,
password.toCharArray());
case GSSAPI:
return MongoCredential.createGSSAPICredential(username);
case MONGODB_CR:
return MongoCredential.createMongoCRCredential(username, authDatabase,
password.toCharArray());
case MONGODB_X509:
return MongoCredential.createMongoX509Credential(username);
}
throw new IllegalArgumentException("Unsupported authentication mechanism "
+ authMechanismName);
}
项目:vertx-mongo-client
文件:CredentialListParserTest.java
@Test
public void testAuth_GSSAPI() {
JsonObject config = new JsonObject();
String username = TestUtils.randomAlphaString(8);
String authSource = TestUtils.randomAlphaString(10);
config.put("username", username);
config.put("authSource", authSource);
config.put("authMechanism", "GSSAPI");
List<MongoCredential> credentials = new CredentialListParser(null, config).credentials();
assertEquals(1, credentials.size());
MongoCredential credential = credentials.get(0);
assertEquals(username, credential.getUserName());
assertNotEquals(authSource, credential.getSource()); // It should ignore the source we pass in
assertEquals(AuthenticationMechanism.GSSAPI, credential.getAuthenticationMechanism());
}
项目:vertx-mongo-client
文件:CredentialListParserTest.java
@Test
public void testAuth_GSSAPI_WithServiceName() {
JsonObject config = new JsonObject();
String username = TestUtils.randomAlphaString(8);
String authSource = TestUtils.randomAlphaString(10);
String serviceName = TestUtils.randomAlphaString(11);
config.put("username", username);
config.put("authSource", authSource);
config.put("authMechanism", "GSSAPI");
config.put("gssapiServiceName", serviceName);
List<MongoCredential> credentials = new CredentialListParser(null, config).credentials();
assertEquals(1, credentials.size());
MongoCredential credential = credentials.get(0);
assertEquals(username, credential.getUserName());
assertNotEquals(authSource, credential.getSource()); // It should ignore the source we pass in
assertEquals(AuthenticationMechanism.GSSAPI, credential.getAuthenticationMechanism());
assertEquals(serviceName, credential.getMechanismProperty("SERVICE_NAME", null));
}
项目:vertx-mongo-client
文件:CredentialListParserTest.java
@Test
public void testAuth_PLAIN() {
JsonObject config = new JsonObject();
String username = TestUtils.randomAlphaString(8);
String password = TestUtils.randomAlphaString(20);
String authSource = TestUtils.randomAlphaString(10);
config.put("username", username);
config.put("password", password);
config.put("authSource", authSource);
config.put("authMechanism", "PLAIN");
List<MongoCredential> credentials = new CredentialListParser(null, config).credentials();
assertEquals(1, credentials.size());
MongoCredential credential = credentials.get(0);
assertEquals(username, credential.getUserName());
assertArrayEquals(password.toCharArray(), credential.getPassword());
assertEquals(authSource, credential.getSource());
assertEquals(AuthenticationMechanism.PLAIN, credential.getAuthenticationMechanism());
}
项目:vertx-mongo-client
文件:CredentialListParserTest.java
@Test
public void testAuth_MONGODB_CR() {
JsonObject config = new JsonObject();
String username = TestUtils.randomAlphaString(8);
String password = TestUtils.randomAlphaString(20);
String authSource = TestUtils.randomAlphaString(10);
config.put("username", username);
config.put("password", password);
config.put("authSource", authSource);
config.put("authMechanism", "MONGODB-CR");
List<MongoCredential> credentials = new CredentialListParser(null, config).credentials();
assertEquals(1, credentials.size());
MongoCredential credential = credentials.get(0);
assertEquals(username, credential.getUserName());
assertArrayEquals(password.toCharArray(), credential.getPassword());
assertEquals(authSource, credential.getSource());
assertEquals(AuthenticationMechanism.MONGODB_CR, credential.getAuthenticationMechanism());
}
项目:vertx-mongo-client
文件:CredentialListParserTest.java
@Test
public void testAuth_MONGODB_X509() {
JsonObject config = new JsonObject();
String username = TestUtils.randomAlphaString(8);
String authSource = TestUtils.randomAlphaString(10);
config.put("username", username);
config.put("authSource", authSource);
config.put("authMechanism", "MONGODB-X509");
List<MongoCredential> credentials = new CredentialListParser(null, config).credentials();
assertEquals(1, credentials.size());
MongoCredential credential = credentials.get(0);
assertEquals(username, credential.getUserName());
assertNotEquals(authSource, credential.getSource()); // It should ignore the source we pass in
assertEquals(AuthenticationMechanism.MONGODB_X509, credential.getAuthenticationMechanism());
}
项目:vertx-mongo-client
文件:CredentialListParserTest.java
@Test
public void testAuth_SCRAM_SHA_1() {
JsonObject config = new JsonObject();
String username = TestUtils.randomAlphaString(8);
String password = TestUtils.randomAlphaString(20);
String authSource = TestUtils.randomAlphaString(10);
config.put("username", username);
config.put("password", password);
config.put("authSource", authSource);
config.put("authMechanism", "SCRAM-SHA-1");
List<MongoCredential> credentials = new CredentialListParser(null, config).credentials();
assertEquals(1, credentials.size());
MongoCredential credential = credentials.get(0);
assertEquals(username, credential.getUserName());
assertArrayEquals(password.toCharArray(), credential.getPassword());
assertEquals(authSource, credential.getSource());
assertEquals(AuthenticationMechanism.SCRAM_SHA_1, credential.getAuthenticationMechanism());
}
项目:nosql4idea
文件:MongoAuthenticationPanel.java
@Override
public void load(AuthenticationSettings settings) {
usernameField.setText(settings.getUsername());
passwordField.setText(settings.getPassword());
MongoExtraSettings mongoExtraSettings = new MongoExtraSettings(settings.getExtras());
authenticationDatabaseField.setText(mongoExtraSettings.getAuthenticationDatabase());
sslConnectionField.setSelected(mongoExtraSettings.isSsl());
AuthenticationMechanism authentificationMethod = mongoExtraSettings.getAuthenticationMechanism();
if (AuthenticationMechanism.MONGODB_CR.equals(authentificationMethod)) {
mongoCRAuthRadioButton.setSelected(true);
} else if (AuthenticationMechanism.SCRAM_SHA_1.equals(authentificationMethod)) {
scramSHA1AuthRadioButton.setSelected(true);
} else {
defaultAuthMethodRadioButton.setSelected(true);
}
}
项目:jpa-unit
文件:KunderaConfigurationTest.java
@Test
public void testMongoCredentials() {
// GIVEN
final Map<String, Object> properties = new HashMap<>();
when(descriptor.getProperties()).thenReturn(properties);
properties.put("kundera.keyspace", "foo");
properties.put("kundera.username", "user");
properties.put("kundera.password", "pass");
final ConfigurationFactory factory = new ConfigurationFactoryImpl();
// WHEN
final Configuration configuration = factory.createConfiguration(descriptor);
// THEN
assertThat(configuration, notNullValue());
final List<MongoCredential> credentials = configuration.getCredentials();
assertThat(credentials, notNullValue());
assertThat(credentials.size(), equalTo(1));
final MongoCredential mongoCredential = credentials.get(0);
assertThat(mongoCredential, notNullValue());
assertThat(mongoCredential.getUserName(), equalTo("user"));
assertThat(mongoCredential.getPassword(), equalTo("pass".toCharArray()));
assertThat(mongoCredential.getSource(), equalTo("admin"));
assertThat(mongoCredential.getAuthenticationMechanism(), equalTo(AuthenticationMechanism.PLAIN));
}
项目:jpa-unit
文件:DataNucleusConfigurationTest.java
@Test
public void testMongoCredentials() {
// GIVEN
final Map<String, Object> properties = new HashMap<>();
when(descriptor.getProperties()).thenReturn(properties);
properties.put("datanucleus.ConnectionURL", "mongodb:/foo");
properties.put("datanucleus.ConnectionUserName", "user");
properties.put("datanucleus.ConnectionPassword", "pass");
final ConfigurationFactory factory = new ConfigurationFactoryImpl();
// WHEN
final Configuration configuration = factory.createConfiguration(descriptor);
// THEN
assertThat(configuration, notNullValue());
final List<MongoCredential> credentials = configuration.getCredentials();
assertThat(credentials, notNullValue());
assertThat(credentials.size(), equalTo(1));
final MongoCredential mongoCredential = credentials.get(0);
assertThat(mongoCredential, notNullValue());
assertThat(mongoCredential.getUserName(), equalTo("user"));
assertThat(mongoCredential.getPassword(), equalTo("pass".toCharArray()));
assertThat(mongoCredential.getSource(), equalTo("admin"));
assertThat(mongoCredential.getAuthenticationMechanism(), equalTo(AuthenticationMechanism.PLAIN));
}
项目:jpa-unit
文件:HibernateOgmConfigurationTest.java
@Test
public void testMongoCredentialsWithoutSpecifyingAuthenticationDatabase() {
// GIVEN
final Map<String, Object> properties = new HashMap<>();
when(descriptor.getProperties()).thenReturn(properties);
properties.put("hibernate.ogm.datastore.database", "foo");
properties.put("hibernate.ogm.datastore.username", "user");
properties.put("hibernate.ogm.datastore.password", "pass");
final ConfigurationFactory factory = new ConfigurationFactoryImpl();
// WHEN
final Configuration configuration = factory.createConfiguration(descriptor);
// THEN
assertThat(configuration, notNullValue());
final List<MongoCredential> credentials = configuration.getCredentials();
assertThat(credentials, notNullValue());
assertThat(credentials.size(), equalTo(1));
final MongoCredential mongoCredential = credentials.get(0);
assertThat(mongoCredential, notNullValue());
assertThat(mongoCredential.getUserName(), equalTo("user"));
assertThat(mongoCredential.getPassword(), equalTo("pass".toCharArray()));
assertThat(mongoCredential.getSource(), equalTo("admin"));
assertThat(mongoCredential.getAuthenticationMechanism(), equalTo(AuthenticationMechanism.PLAIN));
}
项目:jpa-unit
文件:HibernateOgmConfigurationTest.java
@Test
public void testMongoCredentialsWithAuthenticationDatabaseSet() {
// GIVEN
final Map<String, Object> properties = new HashMap<>();
when(descriptor.getProperties()).thenReturn(properties);
properties.put("hibernate.ogm.datastore.database", "foo");
properties.put("hibernate.ogm.datastore.username", "user");
properties.put("hibernate.ogm.datastore.password", "pass");
properties.put("hibernate.ogm.mongodb.authentication_database", "auth");
final ConfigurationFactory factory = new ConfigurationFactoryImpl();
// WHEN
final Configuration configuration = factory.createConfiguration(descriptor);
// THEN
assertThat(configuration, notNullValue());
final List<MongoCredential> credentials = configuration.getCredentials();
assertThat(credentials, notNullValue());
assertThat(credentials.size(), equalTo(1));
final MongoCredential mongoCredential = credentials.get(0);
assertThat(mongoCredential, notNullValue());
assertThat(mongoCredential.getUserName(), equalTo("user"));
assertThat(mongoCredential.getPassword(), equalTo("pass".toCharArray()));
assertThat(mongoCredential.getSource(), equalTo("auth"));
assertThat(mongoCredential.getAuthenticationMechanism(), equalTo(AuthenticationMechanism.PLAIN));
}
项目:jpa-unit
文件:EclipseLinkConfigurationTest.java
@Test
public void testMongoCredentials() {
// GIVEN
final Map<String, Object> properties = new HashMap<>();
when(descriptor.getProperties()).thenReturn(properties);
properties.put("eclipselink.nosql.property.mongo.db", "foo");
properties.put("eclipselink.nosql.property.user", "user");
properties.put("eclipselink.nosql.property.password", "pass");
final ConfigurationFactory factory = new ConfigurationFactoryImpl();
// WHEN
final Configuration configuration = factory.createConfiguration(descriptor);
// THEN
assertThat(configuration, notNullValue());
final List<MongoCredential> credentials = configuration.getCredentials();
assertThat(credentials, notNullValue());
assertThat(credentials.size(), equalTo(1));
final MongoCredential mongoCredential = credentials.get(0);
assertThat(mongoCredential, notNullValue());
assertThat(mongoCredential.getUserName(), equalTo("user"));
assertThat(mongoCredential.getPassword(), equalTo("pass".toCharArray()));
assertThat(mongoCredential.getSource(), equalTo("admin"));
assertThat(mongoCredential.getAuthenticationMechanism(), equalTo(AuthenticationMechanism.PLAIN));
}
项目:vertx-mongo-client
文件:CredentialListParser.java
private AuthenticationMechanism getAuthenticationMechanism(String authMechanism) {
AuthenticationMechanism mechanism;
try {
mechanism = AuthenticationMechanism.fromMechanismName(authMechanism);
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException("Invalid authMechanism '" + authMechanism + "'");
}
return mechanism;
}
项目:nosql4idea
文件:MongoAuthenticationPanel.java
private AuthenticationMechanism getAuthenticationMechanism() {
if (mongoCRAuthRadioButton.isSelected()) {
return AuthenticationMechanism.MONGODB_CR;
} else if (scramSHA1AuthRadioButton.isSelected()) {
return AuthenticationMechanism.SCRAM_SHA_1;
}
return null;
}
项目:nosql4idea
文件:ServerConfigurationPanelTest.java
@Test
public void createMongoConfiguration() throws Exception {
frameFixture.textBox("labelField").setText("Localhost");
frameFixture.label("databaseVendorLabel").requireText("MongoDB");
frameFixture.label("databaseTipsLabel").requireText("format: host:port. If replicat set: host:port1,host:port2,...");
frameFixture.textBox("serverUrlField").setText("localhost:25");
frameFixture.textBox("usernameField").setText("john");
frameFixture.textBox("passwordField").setText("johnpassword");
frameFixture.textBox("userDatabaseField").setText("mydatabase");
frameFixture.textBox("authenticationDatabaseField").setText("admin");
frameFixture.radioButton("defaultAuthMethod").requireSelected();
frameFixture.radioButton("mongoCRAuthField").click();
frameFixture.checkBox("sslConnectionField").check();
frameFixture.checkBox("autoConnectField").check();
ServerConfiguration configuration = new ServerConfiguration();
configurationPanel.applyConfigurationData(configuration);
assertEquals("Localhost", configuration.getLabel());
assertEquals(DatabaseVendor.MONGO, configuration.getDatabaseVendor());
assertEquals("localhost:25", configuration.getServerUrl());
AuthenticationSettings authenticationSettings = configuration.getAuthenticationSettings();
assertEquals("john", authenticationSettings.getUsername());
assertEquals("johnpassword", authenticationSettings.getPassword());
MongoExtraSettings mongoExtraSettings = new MongoExtraSettings(authenticationSettings.getExtras());
assertEquals("admin", mongoExtraSettings.getAuthenticationDatabase());
assertEquals(AuthenticationMechanism.MONGODB_CR, mongoExtraSettings.getAuthenticationMechanism());
assertEquals("mydatabase", configuration.getUserDatabase());
assertTrue(configuration.isConnectOnIdeStartup());
}
项目:nosql4idea
文件:ServerConfigurationPanelTest.java
@Test
public void loadMongoConfiguration() throws Exception {
ServerConfiguration configuration = new ServerConfiguration();
configuration.setLabel("Localhost");
configuration.setDatabaseVendor(DatabaseVendor.MONGO);
configuration.setServerUrl("localhost:25");
AuthenticationSettings authenticationSettings = new AuthenticationSettings();
authenticationSettings.setUsername("john");
authenticationSettings.setPassword("johnpassword");
MongoExtraSettings mongoExtraSettings = new MongoExtraSettings();
mongoExtraSettings.setAuthenticationDatabase("admin");
mongoExtraSettings.setAuthenticationMechanism(AuthenticationMechanism.SCRAM_SHA_1);
mongoExtraSettings.setSsl(true);
authenticationSettings.setExtras(mongoExtraSettings.get());
configuration.setAuthenticationSettings(authenticationSettings);
configuration.setUserDatabase("mydatabase");
configurationPanel.loadConfigurationData(configuration);
frameFixture.textBox("labelField").requireText("Localhost");
frameFixture.textBox("serverUrlField").requireText("localhost:25");
frameFixture.textBox("usernameField").requireText("john");
frameFixture.textBox("passwordField").requireText("johnpassword");
frameFixture.textBox("authenticationDatabaseField").requireText("admin");
frameFixture.checkBox("sslConnectionField").requireSelected();
frameFixture.radioButton("scramSHA1AuthField").requireSelected();
}
项目:vertx-mongo-client
文件:CredentialListParser.java
public CredentialListParser(ConnectionString connectionString, JsonObject config) {
List<MongoCredential> connStringCredentials = null;
if (connectionString != null) {
connStringCredentials = connectionString.getCredentialList();
}
if (connStringCredentials != null && !connStringCredentials.isEmpty()) {
credentials = connStringCredentials;
} else {
String username = config.getString("username");
if (username == null) {
credentials = Collections.emptyList();
} else {
credentials = new ArrayList<>();
String passwd = config.getString("password");
char[] password = (passwd == null) ? null : passwd.toCharArray();
// See https://github.com/vert-x3/vertx-mongo-client/issues/46 - 'admin' as default is a security
// concern, use the 'db_name' if none is set.
String authSource = config.getString("authSource",
config.getString("db_name", MongoClientImpl.DEFAULT_DB_NAME));
// AuthMechanism
AuthenticationMechanism mechanism = null;
String authMechanism = config.getString("authMechanism");
if (authMechanism != null) {
mechanism = getAuthenticationMechanism(authMechanism);
}
// MongoCredential
String gssapiServiceName = config.getString("gssapiServiceName");
MongoCredential credential;
if (mechanism == GSSAPI) {
credential = MongoCredential.createGSSAPICredential(username);
credential = getMongoCredential(gssapiServiceName, credential);
} else if (mechanism == PLAIN) {
credential = MongoCredential.createPlainCredential(username, authSource, password);
} else if (mechanism == MONGODB_CR) {
credential = MongoCredential.createMongoCRCredential(username, authSource, password);
} else if (mechanism == MONGODB_X509) {
credential = MongoCredential.createMongoX509Credential(username);
} else if (mechanism == SCRAM_SHA_1) {
credential = MongoCredential.createScramSha1Credential(username, authSource, password);
} else if (mechanism == null) {
credential = MongoCredential.createCredential(username, authSource, password);
} else {
throw new IllegalArgumentException("Unsupported authentication mechanism " + mechanism);
}
credentials.add(credential);
}
}
}
项目:nosql4idea
文件:MongoConsoleRunner.java
@Nullable
@Override
protected Process createProcess() throws ExecutionException {
NoSqlConfiguration noSqlConfiguration = NoSqlConfiguration.getInstance(getProject());
String shellPath = noSqlConfiguration.getShellPath(DatabaseVendor.MONGO);
final GeneralCommandLine commandLine = new GeneralCommandLine();
commandLine.setExePath(shellPath);
commandLine.addParameter(MongoUtils.buildMongoUrl(serverConfiguration, database));
String shellWorkingDir = serverConfiguration.getShellWorkingDir();
if (StringUtils.isNotBlank(shellWorkingDir)) {
commandLine.withWorkDirectory(shellWorkingDir);
}
AuthenticationSettings authenticationSettings = serverConfiguration.getAuthenticationSettings();
String username = authenticationSettings.getUsername();
if (StringUtils.isNotBlank(username)) {
commandLine.addParameter("--username");
commandLine.addParameter(username);
}
String password = authenticationSettings.getPassword();
if (StringUtils.isNotBlank(password)) {
commandLine.addParameter("--password");
commandLine.addParameter(password);
}
MongoExtraSettings mongoExtraSettings = new MongoExtraSettings(authenticationSettings.getExtras());
String authenticationDatabase = mongoExtraSettings.getAuthenticationDatabase();
if (StringUtils.isNotBlank(authenticationDatabase)) {
commandLine.addParameter("--authenticationDatabase");
commandLine.addParameter(authenticationDatabase);
}
AuthenticationMechanism authenticationMecanism = mongoExtraSettings.getAuthenticationMechanism();
if (authenticationMecanism != null) {
commandLine.addParameter("--authenticationMecanism");
commandLine.addParameter(authenticationMecanism.getMechanismName());
}
String shellArgumentsLine = serverConfiguration.getShellArgumentsLine();
if (StringUtils.isNotBlank(shellArgumentsLine)) {
commandLine.addParameters(shellArgumentsLine.split(" "));
}
return commandLine.createProcess();
}
项目:nosql4idea
文件:MongoExtraSettings.java
public AuthenticationMechanism getAuthenticationMechanism() {
String authMecanism = extras.get(AUTH_MECHANISM);
return authMecanism == null ? null : AuthenticationMechanism.valueOf(authMecanism);
}
项目:nosql4idea
文件:MongoExtraSettings.java
public void setAuthenticationMechanism(AuthenticationMechanism authenticationMechanism) {
if (authenticationMechanism != null) {
extras.put(AUTH_MECHANISM, authenticationMechanism.name());
}
}
项目:nosql4idea
文件:MongoClientURIBuilder.java
public MongoClientURIBuilder setAuthenticationMecanism(@NotNull AuthenticationMechanism authenticationMecanism) {
this.authenticationMecanism = authenticationMecanism;
return this;
}