/** * Creates a new MongoCredential as a copy of this instance, with the specified mechanism property added. * * @param key the key to the property, which is treated as case-insensitive * @param value the value of the property * @param <T> the property type * @return the credential * @since 2.12 */ public <T> MongoCredential withMechanismProperty(final String key, final T value) { return new MongoCredential(this, key, value); }
/** * Creates a MongoCredential instance for the MongoDB X.509 protocol. * * @param userName the user name * @return the credential * * @since 2.12 * @mongodb.server.release 2.6 * @mongodb.driver.manual core/authentication/#x-509-certificate-authentication X-509 */ public static MongoCredential createMongoX509Credential(final String userName) { return new MongoCredential(MONGODB_X509, userName, "$external", null); }
/** * Creates a MongoCredential instance for the PLAIN SASL mechanism. * * @param userName the non-null user name * @param source the source where the user is defined. This can be either {@code "$external"} or the name of a database. * @param password the non-null user password * @return the credential * * @since 2.12 * @mongodb.server.release 2.6 * @mongodb.driver.manual core/authentication/#ldap-proxy-authority-authentication PLAIN */ public static MongoCredential createPlainCredential(final String userName, final String source, final char[] password) { return new MongoCredential(PLAIN, userName, source, password); }
/** * Creates a MongoCredential instance for the MongoDB X.509 protocol where the distinguished subject name of the client certificate * acts as the userName. * <p> * Available on MongoDB server versions >= 3.4. * </p> * @return the credential * * @since 3.4 * @mongodb.server.release 3.4 * @mongodb.driver.manual core/authentication/#x-509-certificate-authentication X-509 */ public static MongoCredential createMongoX509Credential() { return new MongoCredential(MONGODB_X509, null, "$external", null); }
/** * Creates a MongoCredential instance for the SCRAM-SHA-256 SASL mechanism. * * @param userName the non-null user name * @param source the source where the user is defined. * @param password the non-null user password * @return the credential * @see #createCredential(String, String, char[]) * * @since 3.8 * @mongodb.server.release 4.0 * @mongodb.driver.manual core/authentication/#authentication-scram-sha-256 SCRAM-SHA-256 */ public static MongoCredential createScramSha256Credential(final String userName, final String source, final char[] password) { return new MongoCredential(SCRAM_SHA_256, userName, source, password); }
/** * Creates a MongoCredential instance with an unspecified mechanism. The client will negotiate the best mechanism based on the * version of the server that the client is authenticating to. * * <p>If the server version is 4.0 or higher, the driver will negotiate with the server preferring the SCRAM-SHA-256 mechanism. 3.x * servers will authenticate using SCRAM-SHA-1, older servers will authenticate using the MONGODB_CR mechanism.</p> * * @param userName the user name * @param database the database where the user is defined * @param password the user's password * @return the credential * * @since 2.13 * @mongodb.driver.manual core/authentication/#authentication-scram-sha-256 SCRAM-SHA-256 * @mongodb.driver.manual core/authentication/#authentication-scram-sha-1 SCRAM-SHA-1 * @mongodb.driver.manual core/authentication/#mongodb-cr-authentication MONGODB-CR */ public static MongoCredential createCredential(final String userName, final String database, final char[] password) { return new MongoCredential(null, userName, database, password); }
/** * Creates a MongoCredential instance for the SCRAM-SHA-1 SASL mechanism. Use this method only if you want to ensure that * the driver uses the SCRAM-SHA-1 mechanism regardless of whether the server you are connecting to supports the * authentication mechanism. Otherwise use the {@link #createCredential(String, String, char[])} method to allow the driver to * negotiate the best mechanism based on the server version. * * * @param userName the non-null user name * @param source the source where the user is defined. * @param password the non-null user password * @return the credential * @see #createCredential(String, String, char[]) * * @since 2.13 * @mongodb.server.release 3.0 * @mongodb.driver.manual core/authentication/#authentication-scram-sha-1 SCRAM-SHA-1 */ public static MongoCredential createScramSha1Credential(final String userName, final String source, final char[] password) { return new MongoCredential(SCRAM_SHA_1, userName, source, password); }
/** * Creates a new MongoCredential with the set mechanism. The existing mechanism must be null. * * @param mechanism the mechanism to set * @return the credential * @since 3.8 */ public MongoCredential withMechanism(final AuthenticationMechanism mechanism) { if (this.mechanism != null) { throw new IllegalArgumentException("Mechanism already set"); } return new MongoCredential(mechanism, userName, source, password, mechanismProperties); }
/** * Creates a MongoCredential instance for the GSSAPI SASL mechanism. * <p> * To override the default service name of {@code "mongodb"}, add a mechanism property with the name {@code "SERVICE_NAME"}. * <p> * To force canonicalization of the host name prior to authentication, add a mechanism property with the name * {@code "CANONICALIZE_HOST_NAME"} with the value{@code true}. * <p> * To override the {@link javax.security.auth.Subject} with which the authentication executes, add a mechanism property with the name * {@code "JAVA_SUBJECT"} with the value of a {@code Subject} instance. * <p> * To override the properties of the {@link javax.security.sasl.SaslClient} with which the authentication executes, add a mechanism * property with the name {@code "JAVA_SASL_CLIENT_PROPERTIES"} with the value of a {@code Map<String, Object} instance containing the * necessary properties. This can be useful if the application is customizing the default * {@link javax.security.sasl.SaslClientFactory}. * * @param userName the non-null user name * @return the credential * @mongodb.server.release 2.4 * @mongodb.driver.manual core/authentication/#kerberos-authentication GSSAPI * @see #withMechanismProperty(String, Object) * @see #SERVICE_NAME_KEY * @see #CANONICALIZE_HOST_NAME_KEY * @see #JAVA_SUBJECT_KEY * @see #JAVA_SASL_CLIENT_PROPERTIES_KEY */ public static MongoCredential createGSSAPICredential(final String userName) { return new MongoCredential(GSSAPI, userName, "$external", null); }
/** * Creates a MongoCredential instance for the MongoDB Challenge Response protocol. Use this method only if you want to ensure that * the driver uses the MONGODB_CR mechanism regardless of whether the server you are connecting to supports a more secure * authentication mechanism. Otherwise use the {@link #createCredential(String, String, char[])} method to allow the driver to * negotiate the best mechanism based on the server version. * * @param userName the user name * @param database the database where the user is defined * @param password the user's password * @return the credential * @see #createCredential(String, String, char[]) * @mongodb.driver.manual core/authentication/#mongodb-cr-authentication MONGODB-CR * @deprecated MONGODB-CR was replaced by SCRAM-SHA-1 in MongoDB 3.0, and is now deprecated. Use * the {@link #createCredential(String, String, char[])} factory method instead. */ @SuppressWarnings("deprecation") @Deprecated public static MongoCredential createMongoCRCredential(final String userName, final String database, final char[] password) { return new MongoCredential(MONGODB_CR, userName, database, password); }
/** * Creates a new MongoCredential as a copy of this instance, with the specified mechanism property added. * * @param key the key to the property, which is treated as case-insensitive * @param value the value of the property * @param <T> the property type * @return the credential * @since 2.12 */ public <T> MongoCredential withMechanismProperty(final String key, final T value) { return new MongoCredential(this, key, value); }
/** * Creates a MongoCredential instance for the MongoDB X.509 protocol. * * @param userName the user name * @return the credential * * @since 2.12 * @mongodb.server.release 2.6 * @mongodb.driver.manual core/authentication/#x-509-certificate-authentication X-509 */ public static MongoCredential createMongoX509Credential(final String userName) { return new MongoCredential(MONGODB_X509, userName, "$external", null); }
/** * Creates a MongoCredential instance for the PLAIN SASL mechanism. * * @param userName the non-null user name * @param source the source where the user is defined. This can be either {@code "$external"} or the name of a database. * @param password the non-null user password * @return the credential * * @since 2.12 * @mongodb.server.release 2.6 * @mongodb.driver.manual core/authentication/#ldap-proxy-authority-authentication PLAIN */ public static MongoCredential createPlainCredential(final String userName, final String source, final char[] password) { return new MongoCredential(PLAIN, userName, source, password); }
/** * Creates a MongoCredential instance for the MongoDB X.509 protocol where the distinguished subject name of the client certificate * acts as the userName. * <p> * Available on MongoDB server versions >= 3.4. * </p> * @return the credential * * @since 3.4 * @mongodb.server.release 3.4 * @mongodb.driver.manual core/authentication/#x-509-certificate-authentication X-509 */ public static MongoCredential createMongoX509Credential() { return new MongoCredential(MONGODB_X509, null, "$external", null); }
/** * Creates a MongoCredential instance for the SCRAM-SHA-256 SASL mechanism. * * @param userName the non-null user name * @param source the source where the user is defined. * @param password the non-null user password * @return the credential * @see #createCredential(String, String, char[]) * * @since 3.8 * @mongodb.server.release 4.0 * @mongodb.driver.manual core/authentication/#authentication-scram-sha-256 SCRAM-SHA-256 */ public static MongoCredential createScramSha256Credential(final String userName, final String source, final char[] password) { return new MongoCredential(SCRAM_SHA_256, userName, source, password); }
/** * Creates a MongoCredential instance for the SCRAM-SHA-1 SASL mechanism. Use this method only if you want to ensure that * the driver uses the SCRAM-SHA-1 mechanism regardless of whether the server you are connecting to supports the * authentication mechanism. Otherwise use the {@link #createCredential(String, String, char[])} method to allow the driver to * negotiate the best mechanism based on the server version. * * * @param userName the non-null user name * @param source the source where the user is defined. * @param password the non-null user password * @return the credential * @see #createCredential(String, String, char[]) * * @since 2.13 * @mongodb.server.release 3.0 * @mongodb.driver.manual core/authentication/#authentication-scram-sha-1 SCRAM-SHA-1 */ public static MongoCredential createScramSha1Credential(final String userName, final String source, final char[] password) { return new MongoCredential(SCRAM_SHA_1, userName, source, password); }
/** * Creates a MongoCredential instance with an unspecified mechanism. The client will negotiate the best mechanism based on the * version of the server that the client is authenticating to. * * <p>If the server version is 4.0 or higher, the driver will negotiate with the server preferring the SCRAM-SHA-256 mechanism. 3.x * servers will authenticate using SCRAM-SHA-1, older servers will authenticate using the MONGODB_CR mechanism.</p> * * @param userName the user name * @param database the database where the user is defined * @param password the user's password * @return the credential * * @since 2.13 * @mongodb.driver.manual core/authentication/#authentication-scram-sha-256 SCRAM-SHA-256 * @mongodb.driver.manual core/authentication/#authentication-scram-sha-1 SCRAM-SHA-1 * @mongodb.driver.manual core/authentication/#mongodb-cr-authentication MONGODB-CR */ public static MongoCredential createCredential(final String userName, final String database, final char[] password) { return new MongoCredential(null, userName, database, password); }
/** * Creates a new MongoCredential with the set mechanism. The existing mechanism must be null. * * @param mechanism the mechanism to set * @return the credential * @since 3.8 */ public MongoCredential withMechanism(final AuthenticationMechanism mechanism) { if (this.mechanism != null) { throw new IllegalArgumentException("Mechanism already set"); } return new MongoCredential(mechanism, userName, source, password, mechanismProperties); }
/** * Creates a MongoCredential instance for the GSSAPI SASL mechanism. * <p> * To override the default service name of {@code "mongodb"}, add a mechanism property with the name {@code "SERVICE_NAME"}. * <p> * To force canonicalization of the host name prior to authentication, add a mechanism property with the name * {@code "CANONICALIZE_HOST_NAME"} with the value{@code true}. * <p> * To override the {@link javax.security.auth.Subject} with which the authentication executes, add a mechanism property with the name * {@code "JAVA_SUBJECT"} with the value of a {@code Subject} instance. * <p> * To override the properties of the {@link javax.security.sasl.SaslClient} with which the authentication executes, add a mechanism * property with the name {@code "JAVA_SASL_CLIENT_PROPERTIES"} with the value of a {@code Map<String, Object} instance containing the * necessary properties. This can be useful if the application is customizing the default * {@link javax.security.sasl.SaslClientFactory}. * * @param userName the non-null user name * @return the credential * @mongodb.server.release 2.4 * @mongodb.driver.manual core/authentication/#kerberos-authentication GSSAPI * @see #withMechanismProperty(String, Object) * @see #SERVICE_NAME_KEY * @see #CANONICALIZE_HOST_NAME_KEY * @see #JAVA_SUBJECT_KEY * @see #JAVA_SASL_CLIENT_PROPERTIES_KEY */ public static MongoCredential createGSSAPICredential(final String userName) { return new MongoCredential(GSSAPI, userName, "$external", null); }
/** * Creates a MongoCredential instance for the MongoDB Challenge Response protocol. Use this method only if you want to ensure that * the driver uses the MONGODB_CR mechanism regardless of whether the server you are connecting to supports a more secure * authentication mechanism. Otherwise use the {@link #createCredential(String, String, char[])} method to allow the driver to * negotiate the best mechanism based on the server version. * * @param userName the user name * @param database the database where the user is defined * @param password the user's password * @return the credential * @see #createCredential(String, String, char[]) * @mongodb.driver.manual core/authentication/#mongodb-cr-authentication MONGODB-CR * @deprecated MONGODB-CR was replaced by SCRAM-SHA-1 in MongoDB 3.0, and is now deprecated. Use * the {@link #createCredential(String, String, char[])} factory method instead. */ @SuppressWarnings("deprecation") @Deprecated public static MongoCredential createMongoCRCredential(final String userName, final String database, final char[] password) { return new MongoCredential(MONGODB_CR, userName, database, password); }