Search in sources :

Example 1 with MongoSecurityException

use of com.mongodb.MongoSecurityException in project mongo-java-driver by mongodb.

the class X509Authenticator method authenticate.

@Override
void authenticate(final InternalConnection connection, final ConnectionDescription connectionDescription) {
    try {
        validateUserName(connectionDescription);
        BsonDocument authCommand = getAuthCommand(getCredential().getUserName());
        executeCommand(getCredential().getSource(), authCommand, connection);
    } catch (MongoCommandException e) {
        throw new MongoSecurityException(getCredential(), "Exception authenticating", e);
    }
}
Also used : MongoSecurityException(com.mongodb.MongoSecurityException) BsonDocument(org.bson.BsonDocument) MongoCommandException(com.mongodb.MongoCommandException)

Example 2 with MongoSecurityException

use of com.mongodb.MongoSecurityException in project mongo-java-driver by mongodb.

the class NativeAuthenticator method authenticate.

@Override
public void authenticate(final InternalConnection connection, final ConnectionDescription connectionDescription) {
    try {
        BsonDocument nonceResponse = executeCommand(getCredential().getSource(), getNonceCommand(), connection);
        BsonDocument authCommand = getAuthCommand(getCredential().getUserName(), getCredential().getPassword(), ((BsonString) nonceResponse.get("nonce")).getValue());
        executeCommand(getCredential().getSource(), authCommand, connection);
    } catch (MongoCommandException e) {
        throw new MongoSecurityException(getCredential(), "Exception authenticating", e);
    }
}
Also used : MongoSecurityException(com.mongodb.MongoSecurityException) BsonDocument(org.bson.BsonDocument) MongoCommandException(com.mongodb.MongoCommandException)

Example 3 with MongoSecurityException

use of com.mongodb.MongoSecurityException 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);
    }
}
Also used : CallbackHandler(javax.security.auth.callback.CallbackHandler) MongoSecurityException(com.mongodb.MongoSecurityException) PasswordCallback(javax.security.auth.callback.PasswordCallback) NameCallback(javax.security.auth.callback.NameCallback) Callback(javax.security.auth.callback.Callback) NameCallback(javax.security.auth.callback.NameCallback) MongoCredential(com.mongodb.MongoCredential) PasswordCallback(javax.security.auth.callback.PasswordCallback) IOException(java.io.IOException) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException) SaslException(javax.security.sasl.SaslException)

Example 4 with MongoSecurityException

use of com.mongodb.MongoSecurityException 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);
    }
}
Also used : MongoSecurityException(com.mongodb.MongoSecurityException) GSSException(org.ietf.jgss.GSSException) UnknownHostException(java.net.UnknownHostException) MongoCredential(com.mongodb.MongoCredential) SaslException(javax.security.sasl.SaslException) SaslClient(javax.security.sasl.SaslClient)

Aggregations

MongoSecurityException (com.mongodb.MongoSecurityException)4 MongoCommandException (com.mongodb.MongoCommandException)2 MongoCredential (com.mongodb.MongoCredential)2 SaslException (javax.security.sasl.SaslException)2 BsonDocument (org.bson.BsonDocument)2 IOException (java.io.IOException)1 UnknownHostException (java.net.UnknownHostException)1 Callback (javax.security.auth.callback.Callback)1 CallbackHandler (javax.security.auth.callback.CallbackHandler)1 NameCallback (javax.security.auth.callback.NameCallback)1 PasswordCallback (javax.security.auth.callback.PasswordCallback)1 UnsupportedCallbackException (javax.security.auth.callback.UnsupportedCallbackException)1 SaslClient (javax.security.sasl.SaslClient)1 GSSException (org.ietf.jgss.GSSException)1