Ejemplo n.º 1
0
        private void Login_v1(string user, string password, bool allowLoginProcessVersion2Fallback)
        {
            //Get login hash
            string responseHash;

            try
            {
                ApiCommand readLoginHashCommand = new ApiCommand(this, "/login");
                responseHash = readLoginHashCommand.ExecuteScalar();
            }
            catch (TikCommandException) //TODO catch specific exception / message
            {
                if (allowLoginProcessVersion2Fallback)
                {
                    Login_v2(user, password); // try it via newer login process
                    return;
                }
                else
                {
                    throw;
                }
            }

            //login connection
            string     hashedPass   = ApiConnectionHelper.EncodePassword(password, responseHash);
            ApiCommand loginCommand = new ApiCommand(this, "/login", TikCommandParameterFormat.NameValue,
                                                     new ApiCommandParameter("name", user), new ApiCommandParameter("response", hashedPass));

            loginCommand.ExecuteNonQuery();
        }
Ejemplo n.º 2
0
        private void WriteCommand(IEnumerable <string> commandRows)
        {
            try
            {
                foreach (string row in commandRows)
                {
                    byte[] bytes  = _encoding.GetBytes(row.ToCharArray());
                    byte[] length = ApiConnectionHelper.EncodeLength(bytes.Length);

                    _tcpConnectionStream.Write(length, 0, length.Length); //write length of comming sentence
                    _tcpConnectionStream.Write(bytes, 0, bytes.Length);   //write sentence body

                    if (OnWriteRow != null)
                    {
                        OnWriteRow(this, new TikConnectionCommCallbackEventArgs(row));
                    }
                    if (DebugEnabled)
                    {
                        System.Diagnostics.Debug.WriteLine("> " + row);
                    }
                }

                _tcpConnectionStream.WriteByte(0); //final zero byte
            }
            catch (IOException)
            {
                _isOpened = _tcpConnection.Connected;
                throw;
            }
        }
Ejemplo n.º 3
0
        //private void LoginInternal(string user, string password)
        //{
        //    switch (_loginProcessVersion)
        //    {
        //        case LoginProcessVersion.Version1:
        //            Login_v1(user, password, true);
        //            break;
        //        case LoginProcessVersion.Version2:
        //            Login_v2(user, password);
        //            break;
        //        default:
        //            throw new NotImplementedException(string.Format("Unsuported login process version {0}", _loginProcessVersion));
        //    }
        //}

        //private void Login_v1(string user, string password, bool allowLoginProcessVersion2Fallback)
        //{
        //    //Get login hash
        //    string responseHash;
        //    try
        //    {
        //        ApiCommand readLoginHashCommand = new ApiCommand(this, "/login");
        //        responseHash = readLoginHashCommand.ExecuteScalar();
        //    }
        //    catch(TikCommandException) //TODO catch specific exception / message
        //    {
        //        if (allowLoginProcessVersion2Fallback)
        //        {
        //            Login_v2(user, password); // try it via newer login process
        //            return;
        //        }
        //        else
        //            throw;
        //    }

        //    //login connection
        //    string hashedPass = ApiConnectionHelper.EncodePassword(password, responseHash);
        //    ApiCommand loginCommand = new ApiCommand(this, "/login", TikCommandParameterFormat.NameValue,
        //        new ApiCommandParameter("name", user), new ApiCommandParameter("response", hashedPass));
        //    loginCommand.ExecuteNonQuery();
        //}

        //private void Login_v2(string user, string password)
        //{
        //    //login connection
        //    ApiCommand loginCommand = new ApiCommand(this, "/login", TikCommandParameterFormat.NameValue,
        //        new ApiCommandParameter("name", user), new ApiCommandParameter("password", password));
        //    loginCommand.ExecuteNonQuery();
        //}

        private void Login_v3(string user, string password)
        {
            try
            {
                ApiCommand loginCommand = new ApiCommand(this, "/login", TikCommandParameterFormat.NameValue,
                                                         new ApiCommandParameter("name", user), new ApiCommandParameter("password", password)); //parameters will be ignored with old login protocol

                var responseHashOrNull = loginCommand.ExecuteScalarOrDefault();

                //old login protocol
                if (!string.IsNullOrEmpty(responseHashOrNull))
                {
                    //login connection
                    string     hashedPass    = ApiConnectionHelper.EncodePassword(password, responseHashOrNull);
                    ApiCommand loginCommand2 = new ApiCommand(this, "/login", TikCommandParameterFormat.NameValue,
                                                              new ApiCommandParameter("name", user), new ApiCommandParameter("response", hashedPass));
                    loginCommand2.ExecuteNonQuery();
                }
            }
            catch (TikCommandTrapException ex)
            {
                if (ex.Message == "cannot log in")
                {
                    throw new TikConnectionLoginException();
                }
                else
                {
                    throw;
                }
            }
        }
Ejemplo n.º 4
0
        private void LoginOld(string user, string password)
        {
            //Get login hash
            ApiCommand readLoginHashCommand = new ApiCommand(this, "/login");
            string     responseHash         = readLoginHashCommand.ExecuteScalar();

            //login connection
            string     hashedPass   = ApiConnectionHelper.EncodePassword(password, responseHash);
            ApiCommand loginCommand = new ApiCommand(this, "/login", TikCommandParameterFormat.NameValue,
                                                     new ApiCommandParameter("name", user), new ApiCommandParameter("response", hashedPass));

            loginCommand.ExecuteNonQuery();
        }
Ejemplo n.º 5
0
        private void WriteCommand(IEnumerable <string> commandRows)
        {
            foreach (string row in commandRows)
            {
                byte[] bytes  = Encoding.ASCII.GetBytes(row.ToCharArray());
                byte[] length = ApiConnectionHelper.EncodeLength(bytes.Length);

                _tcpConnectionStream.Write(length, 0, length.Length); //write length of comming sentence
                _tcpConnectionStream.Write(bytes, 0, bytes.Length);   //write sentence body
                if (OnWriteRow != null)
                {
                    OnWriteRow(this, row);
                }
            }

            _tcpConnectionStream.WriteByte(0); //final zero byte
        }
Ejemplo n.º 6
0
        public void Open(string host, int port, string user, string password)
        {
            //open connection
            _tcpConnection = new TcpClient();
            _tcpConnection.Connect(host, port);
            _tcpConnectionStream = _tcpConnection.GetStream();

            //Get login hash
            ApiCommand readLoginHashCommand = new ApiCommand(this, "/login");
            string     responseHash         = readLoginHashCommand.ExecuteScalar();

            //login connection
            string     hashedPass   = ApiConnectionHelper.EncodePassword(password, responseHash);
            ApiCommand loginCommand = new ApiCommand(this, "/login", TikCommandParameterFormat.NameValue,
                                                     new ApiCommandParameter("name", user), new ApiCommandParameter("response", hashedPass));

            loginCommand.ExecuteNonQuery();

            _isOpened = true;
        }