private void ProcessAuth(XmppStream stream, Auth auth, XmppHandlerContext context)
        {
            AuthData authStep;
            lock (authData)
            {
                authData.TryGetValue(stream.Id, out authStep);
            }

            if (auth.MechanismType != MechanismType.DIGEST_MD5)
            {
                context.Sender.SendToAndClose(stream, XmppFailureError.InvalidMechanism);
            }
            else if (authStep != null)
            {
                context.Sender.SendToAndClose(stream, XmppFailureError.TemporaryAuthFailure);
            }
            else
            {
                lock (authData)
                {
                    authData[stream.Id] = new AuthData();
                }
                var challenge = GetChallenge(stream.Domain);
                context.Sender.SendTo(stream, challenge);
            }
        }
        private void ProcessAuth(XmppStream stream, Auth auth, XmppHandlerContext context)
        {
            AuthData authStep;
            lock (authData)
            {
                authData.TryGetValue(stream.Id, out authStep);
            }

            if (auth.MechanismType == MechanismType.DIGEST_MD5)
            {
                if (authStep != null)
                {
                    context.Sender.SendToAndClose(stream, XmppFailureError.TemporaryAuthFailure);
                }
                else
                {
                    lock (authData)
                    {
                        authData[stream.Id] = new AuthData();
                    }
                    var challenge = GetChallenge(stream.Domain);
                    context.Sender.SendTo(stream, challenge);
                }
            }
            else if (auth.MechanismType == MechanismType.PLAIN)
            {
                if (auth.TextBase64 == null)
                {
                    context.Sender.SendToAndClose(stream, XmppFailureError.TemporaryAuthFailure);
                }
                else
                {
                    string[] array = auth.TextBase64.Split('\0');
                    if (array.Length == 3)
                    {
                        string userName = array[1];
                        string password = array[2];
                        var storage = new DbLdapSettingsStore();
                        storage.GetLdapSettings(stream.Domain);
                        User user = context.UserManager.GetUser(new Jid(userName, stream.Domain, null));
                        if (user != null)
                        {
                            if (user.Sid != null)
                            {
                                var accountName = storage.getAccountNameBySid(user.Sid);
                                if (accountName != null && storage.CheckCredentials(accountName, password))
                                {
                                    // ldap user
                                    lock (authData)
                                    {
                                        authData[stream.Id] = new AuthData(true);
                                        authData[stream.Id].UserName = userName;
                                        authData[stream.Id].IsAuth = true;
                                    }
                                }
                            }
                            else if (user.Password == password)
                            {
                                // usual user
                                lock (authData)
                                {
                                    authData[stream.Id] = new AuthData(true);
                                    authData[stream.Id].UserName = userName;
                                    authData[stream.Id].IsAuth = true;
                                }
                            }
                        }
                    }
                    lock (authData)
                    {
                        if (!authData.ContainsKey(stream.Id))
                        {
                            authData[stream.Id] = new AuthData(true);
                        }
                    }
                    context.Sender.SendTo(stream, new Challenge());
                }
            }
            else
            {
                context.Sender.SendToAndClose(stream, XmppFailureError.InvalidMechanism);
            }
        }