Search in sources :

Example 1 with SaslNettyClient

use of herddb.security.sasl.SaslNettyClient in project herddb by diennea.

the class RoutedClientSideConnection method performAuthentication.

private void performAuthentication(Channel _channel, String serverHostname) throws Exception {
    SaslNettyClient saslNettyClient = new SaslNettyClient(connection.getClient().getConfiguration().getString(ClientConfiguration.PROPERTY_CLIENT_USERNAME, ClientConfiguration.PROPERTY_CLIENT_USERNAME_DEFAULT), connection.getClient().getConfiguration().getString(ClientConfiguration.PROPERTY_CLIENT_PASSWORD, ClientConfiguration.PROPERTY_CLIENT_PASSWORD_DEFAULT), serverHostname);
    byte[] firstToken = new byte[0];
    if (saslNettyClient.hasInitialResponse()) {
        firstToken = saslNettyClient.evaluateChallenge(new byte[0]);
    }
    Message saslResponse = _channel.sendMessageWithReply(Message.SASL_TOKEN_MESSAGE_REQUEST(SaslUtils.AUTH_DIGEST_MD5, firstToken), timeout);
    for (int i = 0; i < 100; i++) {
        byte[] responseToSendToServer;
        switch(saslResponse.type) {
            case Message.TYPE_SASL_TOKEN_SERVER_RESPONSE:
                byte[] token = (byte[]) saslResponse.parameters.get("token");
                responseToSendToServer = saslNettyClient.evaluateChallenge(token);
                saslResponse = _channel.sendMessageWithReply(Message.SASL_TOKEN_MESSAGE_TOKEN(responseToSendToServer), timeout);
                if (saslNettyClient.isComplete()) {
                    LOGGER.finest("SASL auth completed with success");
                    return;
                }
                break;
            case Message.TYPE_ERROR:
                throw new Exception("Server returned ERROR during SASL negotiation, Maybe authentication failure (" + saslResponse.parameters + ")");
            default:
                throw new Exception("Unexpected server response during SASL negotiation (" + saslResponse + ")");
        }
    }
    throw new Exception("SASL negotiation took too many steps");
}
Also used : SaslNettyClient(herddb.security.sasl.SaslNettyClient) Message(herddb.network.Message) RetryRequestException(herddb.client.impl.RetryRequestException) TimeoutException(java.util.concurrent.TimeoutException) DataStorageManagerException(herddb.storage.DataStorageManagerException)

Aggregations

RetryRequestException (herddb.client.impl.RetryRequestException)1 Message (herddb.network.Message)1 SaslNettyClient (herddb.security.sasl.SaslNettyClient)1 DataStorageManagerException (herddb.storage.DataStorageManagerException)1 TimeoutException (java.util.concurrent.TimeoutException)1