예제 #1
0
        private void ClearPassword()
        {
            var password = _password as TLPassword84;

            if (password == null)
            {
                return;
            }

            IsWorking = true;
            BeginOnThreadPool(() =>
            {
                var newSettings = new TLPasswordInputSettings83
                {
                    Flags           = new TLInt(0),
                    NewAlgo         = new TLPasswordKdfAlgoUnknown(),
                    NewPasswordHash = TLString.Empty,
                    Hint            = TLString.Empty,
                    Email           = TLString.Empty
                };

                MTProtoService.GetPasswordAsync(
                    result1 =>
                {
                    var srpParams = result1 as IPasswordSRPParams;
                    if (srpParams == null)
                    {
                        BeginOnUIThread(() => IsWorking = false);
                        return;
                    }

                    var currentPasswordHash = password.CurrentPasswordHash ?? TLString.Empty;
                    MTProtoService.UpdatePasswordSettingsAsync(SRP.GetCheck(currentPasswordHash, srpParams.SRPId, srpParams.SRPB, srpParams.CurrentAlgo), newSettings,
                                                               result => BeginOnUIThread(() =>
                    {
                        StopTimer();

                        IsWorking      = false;
                        var noPassword = new TLPassword84
                        {
                            Flags = new TLInt(0),
                            EmailUnconfirmedPattern = TLString.Empty,
                            NewSecureSalt           = TLString.Empty,
                            SecretRandom            = TLString.Empty
                        };

                        var password81 = _password as TLPassword84;
                        if (password81 != null)
                        {
                            noPassword.NewAlgo       = password81.NewAlgo;
                            noPassword.NewSecureSalt = password81.NewSecureSalt;
                            noPassword.SecretRandom  = password81.SecretRandom;
                        }

                        _password = noPassword;
                        if (_password != null)
                        {
                            PasswordEnabled = _password.IsAvailable;
                            RecoveryEmailUnconfirmedVisibility = !TLString.IsNullOrEmpty(_password.EmailUnconfirmedPattern) || (_password is TLPassword && ((TLPassword)_password).HasRecovery.Value)
                                        ? Visibility.Visible
                                        : Visibility.Collapsed;
                            NotifyOfPropertyChange(() => RecoveryEmailLabel);
                            NotifyOfPropertyChange(() => CompletePasswordLabel);
                            NotifyOfPropertyChange(() => ChangePasswordVisibility);
                            NotifyOfPropertyChange(() => CompletePasswordVisibility);
                        }
                        EventAggregator.Publish(_password);
                    }),
                                                               error => BeginOnUIThread(() =>
                    {
                        IsWorking          = false;
                        var messageBuilder = new StringBuilder();
                        //messageBuilder.AppendLine(AppResources.ServerErrorMessage);
                        //messageBuilder.AppendLine();
                        messageBuilder.AppendLine("Method: account.updatePasswordSettings");
                        messageBuilder.AppendLine("Result: " + error);

                        if (TLRPCError.CodeEquals(error, ErrorCode.FLOOD))
                        {
                            Execute.BeginOnUIThread(() => MessageBox.Show(AppResources.FloodWaitString, AppResources.Error, MessageBoxButton.OK));
                        }
                        else if (TLRPCError.CodeEquals(error, ErrorCode.INTERNAL))
                        {
                            Execute.BeginOnUIThread(() => MessageBox.Show(messageBuilder.ToString(), AppResources.ServerError, MessageBoxButton.OK));
                        }
                        else if (TLRPCError.CodeEquals(error, ErrorCode.BAD_REQUEST))
                        {
                            if (TLRPCError.TypeEquals(error, ErrorType.PASSWORD_HASH_INVALID))
                            {
                                Execute.BeginOnUIThread(() => MessageBox.Show(messageBuilder.ToString(), AppResources.Error, MessageBoxButton.OK));
                            }
                            else if (TLRPCError.TypeEquals(error, ErrorType.NEW_PASSWORD_BAD))
                            {
                                Execute.BeginOnUIThread(() => MessageBox.Show(messageBuilder.ToString(), AppResources.Error, MessageBoxButton.OK));
                            }
                            else if (TLRPCError.TypeEquals(error, ErrorType.NEW_SALT_INVALID))
                            {
                                Execute.BeginOnUIThread(() => MessageBox.Show(messageBuilder.ToString(), AppResources.Error, MessageBoxButton.OK));
                            }
                            else if (TLRPCError.TypeEquals(error, ErrorType.EMAIL_INVALID))
                            {
                                Execute.BeginOnUIThread(() => MessageBox.Show(AppResources.EmailInvalid, AppResources.Error, MessageBoxButton.OK));
                            }
                            else if (TLRPCError.TypeEquals(error, ErrorType.EMAIL_UNCONFIRMED))
                            {
                                Execute.BeginOnUIThread(() => MessageBox.Show(messageBuilder.ToString(), AppResources.Error, MessageBoxButton.OK));
                            }
                            else
                            {
                                Execute.BeginOnUIThread(() => MessageBox.Show(messageBuilder.ToString(), AppResources.Error, MessageBoxButton.OK));
                            }
                        }
                        else
                        {
                            Execute.ShowDebugMessage("account.updatePasswordSettings error " + error);
                        }
                    }));
                },
                    error1 => BeginOnUIThread(() =>
                {
                    IsWorking = false;
                }));
            });
        }
        protected void UpdatePasswordSettings(TLPassword84 password, TLPasswordInputSettings newSettings)
        {
            IsWorking = true;
            BeginOnThreadPool(() =>
            {
                MTProtoService.GetPasswordAsync(
                    result1 =>
                {
                    var srpParams = result1 as IPasswordSRPParams;
                    if (srpParams == null)
                    {
                        BeginOnUIThread(() => IsWorking = false);
                        return;
                    }

                    var newSettings83 = newSettings as TLPasswordInputSettings83;
                    if (newSettings83 == null)
                    {
                        return;
                    }

                    // calculate new password hash if password will be changed
                    TLString newPasswordHash = null;
                    var newAlgo = newSettings83.NewAlgo as TLPasswordKdfAlgoSHA256SHA256PBKDF2HMACSHA512iter100000SHA256ModPow;
                    if (newAlgo != null)
                    {
                        if (string.IsNullOrEmpty(newSettings83.NewPassword))
                        {
                            return;
                        }

                        newPasswordHash = SRP.GetX(new TLString(newSettings83.NewPassword), newAlgo);

                        newSettings83.NewPasswordHash = SRP.GetVBytes(new TLString(newSettings83.NewPassword), newAlgo);
                    }

                    var currentPasswordHash = password.CurrentPasswordHash ?? TLString.Empty;
                    MTProtoService.UpdatePasswordSettingsAsync(SRP.GetCheck(currentPasswordHash, srpParams.SRPId, srpParams.SRPB, srpParams.CurrentAlgo), newSettings,
                                                               result2 =>
                    {
                        IsWorking = false;
                        MTProtoService.GetPasswordAsync(
                            result3 => BeginOnUIThread(() =>
                        {
                            EventAggregator.Publish(result3);

                            var password84 = result3 as TLPassword84;
                            if (password84 != null)
                            {
                                password84.CurrentPasswordHash = newPasswordHash ?? password84.CurrentPasswordHash;
                            }

                            MessageBox.Show(AppResources.PasswordActive, AppResources.Success, MessageBoxButton.OK);

                            if (password84 != null && (_authorizationForm != null || _secureValues != null))
                            {
                                MTProtoService.GetPasswordSettingsAsync(SRP.GetCheck(password84.CurrentPasswordHash, password84.SRPId, password84.SRPB, password84.CurrentAlgo),
                                                                        result4 => BeginOnUIThread(() =>
                                {
                                    Passport.EnterPasswordViewModel.NavigateToPassportCommon(
                                        result4, password84, new TLString(PasswordViewModel.TempNewPassword),
                                        _authorizationForm, _secureValues,
                                        MTProtoService, StateService, NavigationService);
                                }),
                                                                        error4 => BeginOnUIThread(() =>
                                {
                                    if (error4.TypeEquals(ErrorType.PASSWORD_HASH_INVALID))
                                    {
                                        StateService.AuthorizationForm = _authorizationForm;
                                        StateService.Password          = password84;
                                        NavigationService.UriFor <Passport.EnterPasswordViewModel>().WithParam(x => x.RandomParam, Guid.NewGuid().ToString()).Navigate();
                                    }
                                }));
                            }
                            else
                            {
                                StateService.Password = result3;
                                NavigationService.GoBack();
                            }
                        }),
                            error3 => BeginOnUIThread(() =>
                        {
                            Execute.ShowDebugMessage("account.getPassword error " + error3);
                        }));
                    },
                                                               error2 => BeginOnUIThread(() =>
                    {
                        IsWorking          = false;
                        var messageBuilder = new StringBuilder();
                        //messageBuilder.AppendLine(AppResources.Error);
                        //messageBuilder.AppendLine();
                        messageBuilder.AppendLine("Method: account.updatePasswordSettings");
                        messageBuilder.AppendLine("Result: " + error2);

                        if (TLRPCError.CodeEquals(error2, ErrorCode.FLOOD))
                        {
                            HasError = true;
                            Error    = AppResources.FloodWaitString;
                            Telegram.Api.Helpers.Execute.BeginOnUIThread(() => MessageBox.Show(AppResources.FloodWaitString, AppResources.Error, MessageBoxButton.OK));
                        }
                        else if (TLRPCError.CodeEquals(error2, ErrorCode.INTERNAL))
                        {
                            HasError = true;
                            Error    = AppResources.ServerError;
                            Telegram.Api.Helpers.Execute.BeginOnUIThread(() => MessageBox.Show(messageBuilder.ToString(), AppResources.ServerError, MessageBoxButton.OK));
                        }
                        else if (TLRPCError.CodeEquals(error2, ErrorCode.BAD_REQUEST))
                        {
                            if (TLRPCError.TypeEquals(error2, ErrorType.PASSWORD_HASH_INVALID))
                            {
                                HasError = true;
                                Error    = string.Format("{0} {1}", error2.Code, error2.Message);
                                Telegram.Api.Helpers.Execute.BeginOnUIThread(() => MessageBox.Show(messageBuilder.ToString(), AppResources.Error, MessageBoxButton.OK));
                            }
                            else if (TLRPCError.TypeEquals(error2, ErrorType.NEW_PASSWORD_BAD))
                            {
                                HasError = true;
                                Error    = string.Format("{0} {1}", error2.Code, error2.Message);
                                Telegram.Api.Helpers.Execute.BeginOnUIThread(() => MessageBox.Show(messageBuilder.ToString(), AppResources.Error, MessageBoxButton.OK));
                            }
                            else if (TLRPCError.TypeEquals(error2, ErrorType.NEW_SALT_INVALID))
                            {
                                HasError = true;
                                Error    = string.Format("{0} {1}", error2.Code, error2.Message);
                                Telegram.Api.Helpers.Execute.BeginOnUIThread(() => MessageBox.Show(messageBuilder.ToString(), AppResources.Error, MessageBoxButton.OK));
                            }
                            else if (TLRPCError.TypeEquals(error2, ErrorType.EMAIL_INVALID))
                            {
                                HasError = true;
                                Error    = AppResources.EmailInvalid;
                                Telegram.Api.Helpers.Execute.BeginOnUIThread(() => MessageBox.Show(AppResources.EmailInvalid, AppResources.Error, MessageBoxButton.OK));
                            }
                            else if (TLRPCError.TypeEquals(error2, ErrorType.EMAIL_UNCONFIRMED))
                            {
                                HasError = false;
                                Error    = string.Empty;
                                MTProtoService.GetPasswordAsync(
                                    result3 => BeginOnUIThread(() =>
                                {
                                    IsWorking = false;
                                    password  = result3 as TLPassword84;
                                    if (password != null)
                                    {
                                        password.CurrentPasswordHash = currentPasswordHash;             //EMAIL_UNCONFIRMED - new settings are not active yet
                                    }

                                    MessageBox.Show(AppResources.CompletePasswordHint, AppResources.AlmostThere, MessageBoxButton.OK);

                                    if (_authorizationForm != null || _secureValues != null)
                                    {
                                        StateService.AuthorizationForm = _authorizationForm;
                                        StateService.SecureValues      = _secureValues;
                                        StateService.Password          = result3;
                                        StateService.RemoveBackEntry   = true;
                                        NavigationService.UriFor <PasswordViewModel>().Navigate();
                                    }
                                    else
                                    {
                                        StateService.Password = result3;
                                        NavigationService.GoBack();
                                    }
                                }),
                                    error3 => Execute.BeginOnUIThread(() =>
                                {
                                    IsWorking = false;
                                    Execute.ShowDebugMessage("account.getPassword error " + error3);
                                }));
                            }
                            else
                            {
                                HasError = true;
                                Error    = string.Format("{0} {1}", error2.Code, error2.Message);
                                Execute.BeginOnUIThread(() => MessageBox.Show(messageBuilder.ToString(), AppResources.Error, MessageBoxButton.OK));
                            }
                        }
                        else
                        {
                            HasError = true;
                            Error    = string.Empty;
                            Execute.ShowDebugMessage("account.updatePasswordSettings error " + error2);
                        }
                    }));
                },
                    error1 => BeginOnUIThread(() =>
                {
                    IsWorking = false;
                }));
            });
        }