Java Code Examples for com.mongodb.MongoCredential#createScramSha256Credential()

The following examples show how to use com.mongodb.MongoCredential#createScramSha256Credential() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MongoSchemaFactory.java    From calcite with Apache License 2.0 6 votes vote down vote up
private MongoCredential createCredential(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 SCRAM_SHA_256:
    return MongoCredential.createScramSha256Credential(username, authDatabase,
        password.toCharArray());
  case GSSAPI:
    return MongoCredential.createGSSAPICredential(username);
  case MONGODB_X509:
    return MongoCredential.createMongoX509Credential(username);
  }
  throw new IllegalArgumentException("Unsupported authentication mechanism "
      + authMechanismName);
}
 
Example 2
Source File: CredentialListParser.java    From vertx-mongo-client with Apache License 2.0 4 votes vote down vote up
public CredentialListParser(JsonObject config) {
  String username = config.getString("username");
  // AuthMechanism
  AuthenticationMechanism mechanism = null;
  String authMechanism = config.getString("authMechanism");
  if (authMechanism != null) {
    mechanism = getAuthenticationMechanism(authMechanism);
  }
  credentials = new ArrayList<>();
  if (username == null) {
    if (mechanism == MONGODB_X509) {
      credentials.add(MongoCredential.createMongoX509Credential());
    }
  } else {
    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));

    // 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_X509) {
      credential = MongoCredential.createMongoX509Credential(username);
    } else if (mechanism == SCRAM_SHA_1) {
      credential = MongoCredential.createScramSha1Credential(username, authSource, password);
    } else if (mechanism == SCRAM_SHA_256) {
      credential = MongoCredential.createScramSha256Credential(username, authSource, password);
    } else if (mechanism == null) {
      credential = MongoCredential.createCredential(username, authSource, password);
    } else {
      throw new IllegalArgumentException("Unsupported authentication mechanism " + mechanism);
    }

    credentials.add(credential);
  }
}