예제 #1
0
        /// <summary>
        /// Starts the authentication flow
        /// </summary>
        /// <param name="name">Authenticator name from server.</param>
        /// <exception cref="AuthenticationException" />
        private Task <Response> StartAuthenticationFlow(string name)
        {
            //Determine which authentication flow to use.
            //Check if its using a C* 1.2 with authentication patched version (like DSE 3.1)
            var protocolVersion  = _serializer.ProtocolVersion;
            var isPatchedVersion = protocolVersion == ProtocolVersion.V1 &&
                                   !(Configuration.AuthProvider is NoneAuthProvider) && Configuration.AuthInfoProvider == null;

            if (protocolVersion == ProtocolVersion.V1 && !isPatchedVersion)
            {
                //Use protocol v1 authentication flow
                if (Configuration.AuthInfoProvider == null)
                {
                    throw new AuthenticationException(
                              String.Format("Host {0} requires authentication, but no credentials provided in Cluster configuration", Address),
                              Address);
                }
                var credentialsProvider = Configuration.AuthInfoProvider;
                var credentials         = credentialsProvider.GetAuthInfos(Address);
                var request             = new CredentialsRequest(credentials);
                return(Send(request)
                       .ContinueSync(response =>
                {
                    if (!(response is ReadyResponse))
                    {
                        //If Cassandra replied with a auth response error
                        //The task already is faulted and the exception was already thrown.
                        throw new ProtocolErrorException("Expected SASL response, obtained " + response.GetType().Name);
                    }
                    return response;
                }));
            }
            //Use protocol v2+ authentication flow
            if (Configuration.AuthProvider is IAuthProviderNamed)
            {
                //Provide name when required
                ((IAuthProviderNamed)Configuration.AuthProvider).SetName(name);
            }
            //NewAuthenticator will throw AuthenticationException when NoneAuthProvider
            var authenticator = Configuration.AuthProvider.NewAuthenticator(Address);

            var initialResponse = authenticator.InitialResponse() ?? new byte[0];

            return(Authenticate(initialResponse, authenticator));
        }
예제 #2
0
        /// <summary>
        /// Starts the authentication flow
        /// </summary>
        /// <exception cref="AuthenticationException" />
        private void Authenticate()
        {
            //Determine which authentication flow to use.
            //Check if its using a C* 1.2 with authentication patched version (like DSE 3.1)
            var isPatchedVersion = ProtocolVersion == 1 && !(Configuration.AuthProvider is NoneAuthProvider) && Configuration.AuthInfoProvider == null;

            if (ProtocolVersion >= 2 || isPatchedVersion)
            {
                //Use protocol v2+ authentication flow

                //NewAuthenticator will throw AuthenticationException when NoneAuthProvider
                var authenticator = Configuration.AuthProvider.NewAuthenticator(Address);

                var initialResponse = authenticator.InitialResponse() ?? new byte[0];
                Authenticate(initialResponse, authenticator);
            }
            else
            {
                //Use protocol v1 authentication flow
                if (Configuration.AuthInfoProvider == null)
                {
                    throw new AuthenticationException(
                              String.Format("Host {0} requires authentication, but no credentials provided in Cluster configuration", Address),
                              Address);
                }
                var credentialsProvider = Configuration.AuthInfoProvider;
                var credentials         = credentialsProvider.GetAuthInfos(Address);
                var request             = new CredentialsRequest(ProtocolVersion, credentials);
                var response            = TaskHelper.WaitToComplete(this.Send(request), Configuration.SocketOptions.ConnectTimeoutMillis);
                //If Cassandra replied with a auth response error
                //The task already is faulted and the exception was already thrown.
                if (response is ReadyResponse)
                {
                    return;
                }
                throw new ProtocolErrorException("Expected SASL response, obtained " + response.GetType().Name);
            }
        }
예제 #3
0
        /// <summary>
        /// Starts the authentication flow
        /// </summary>
        /// <exception cref="AuthenticationException" />
        private void Authenticate()
        {
            var provider = Configuration.AuthProvider;
            if (ProtocolVersion == 1 && provider == NoneAuthProvider.Instance)
            {
                //Use protocol v1 authentication flow
                var credentialsProvider = Configuration.AuthInfoProvider;
                var credentials = credentialsProvider.GetAuthInfos(HostAddress);
                var request = new CredentialsRequest(ProtocolVersion, credentials);
                var response = TaskHelper.WaitToComplete(this.Send(request), Configuration.SocketOptions.ConnectTimeoutMillis);
                //If Cassandra replied with a auth response error
                //The task already is faulted and the exception was already thrown.
                if (response is ReadyResponse)
                {
                    return;
                }
                throw new ProtocolErrorException("Expected SASL response, obtained " + response.GetType().Name);
            }
            else
            {
                //Use protocol v2+ authentication flow
                var authenticator = provider.NewAuthenticator(HostAddress);

                byte[] initialResponse = authenticator.InitialResponse();
                if (null == initialResponse)
                {
                    initialResponse = new byte[0];
                }
                Authenticate(initialResponse, authenticator);
            }
        }
예제 #4
0
        /// <summary>
        /// Starts the authentication flow
        /// </summary>
        /// <exception cref="AuthenticationException" />
        private Task<AbstractResponse> Authenticate()
        {
            //Determine which authentication flow to use.
            //Check if its using a C* 1.2 with authentication patched version (like DSE 3.1)
            var isPatchedVersion = ProtocolVersion == 1 && !(Configuration.AuthProvider is NoneAuthProvider) && Configuration.AuthInfoProvider == null;
            if (ProtocolVersion < 2 && !isPatchedVersion)
            {
                //Use protocol v1 authentication flow
                if (Configuration.AuthInfoProvider == null)
                {
                    throw new AuthenticationException(
                        String.Format("Host {0} requires authentication, but no credentials provided in Cluster configuration", Address),
                        Address);
                }
                var credentialsProvider = Configuration.AuthInfoProvider;
                var credentials = credentialsProvider.GetAuthInfos(Address);
                var request = new CredentialsRequest(ProtocolVersion, credentials);
                return Send(request)
                    .ContinueSync(response =>
                    {
                        if (!(response is ReadyResponse))
                        {
                            //If Cassandra replied with a auth response error
                            //The task already is faulted and the exception was already thrown.
                            throw new ProtocolErrorException("Expected SASL response, obtained " + response.GetType().Name);
                        }
                        return response;
                    });
            }
            //Use protocol v2+ authentication flow

            //NewAuthenticator will throw AuthenticationException when NoneAuthProvider
            var authenticator = Configuration.AuthProvider.NewAuthenticator(Address);

            var initialResponse = authenticator.InitialResponse() ?? new byte[0];
            return Authenticate(initialResponse, authenticator);
        }
예제 #5
0
        /// <summary>
        /// Starts the authentication flow
        /// </summary>
        /// <exception cref="AuthenticationException" />
        private void Authenticate()
        {
            //Determine which authentication flow to use.
            //Check if its using a C* 1.2 with authentication patched version (like DSE 3.1)
            var isPatchedVersion = ProtocolVersion == 1 && !(Configuration.AuthProvider is NoneAuthProvider) && Configuration.AuthInfoProvider == null;
            if (ProtocolVersion >= 2 || isPatchedVersion)
            {
                //Use protocol v2+ authentication flow

                //NewAuthenticator will throw AuthenticationException when NoneAuthProvider
                var authenticator = Configuration.AuthProvider.NewAuthenticator(Address);

                var initialResponse = authenticator.InitialResponse() ?? new byte[0];
                Authenticate(initialResponse, authenticator);
            }
            else
            {
                //Use protocol v1 authentication flow
                if (Configuration.AuthInfoProvider == null)
                {
                    throw new AuthenticationException(
                        String.Format("Host {0} requires authentication, but no credentials provided in Cluster configuration", Address),
                        Address);
                }
                var credentialsProvider = Configuration.AuthInfoProvider;
                var credentials = credentialsProvider.GetAuthInfos(Address);
                var request = new CredentialsRequest(ProtocolVersion, credentials);
                var response = TaskHelper.WaitToComplete(this.Send(request), Configuration.SocketOptions.ConnectTimeoutMillis);
                //If Cassandra replied with a auth response error
                //The task already is faulted and the exception was already thrown.
                if (response is ReadyResponse)
                {
                    return;
                }
                throw new ProtocolErrorException("Expected SASL response, obtained " + response.GetType().Name);
            }
        }