Esempio n. 1
0
        public void Confirm()
        {
            if (_password == null)
            {
                return;
            }
            if (!CanConfirm)
            {
                return;
            }

            var currentSalt      = _password.CurrentSalt;
            var sha              = new SHA256Managed();
            var passwordHashData = sha.ComputeHash(TLUtils.Combine(currentSalt.Data, new TLString(Code).Data, currentSalt.Data));
            var passwordHash     = TLString.FromBigEndianData(passwordHashData);

            IsWorking = true;
#if LOG_REGISTRATION
            TLUtils.WriteLog("auth.checkPassword");
#endif
            MTProtoService.CheckPasswordAsync(passwordHash,
                                              auth => BeginOnUIThread(() =>
            {
#if LOG_REGISTRATION
                TLUtils.WriteLog("auth.checkPassword result " + auth);
                TLUtils.WriteLog("TLUtils.IsLogEnabled=false");
#endif

                TLUtils.IsLogEnabled = false;
                TLUtils.LogItems.Clear();

                var result = MessageBox.Show(
                    AppResources.ConfirmPushMessage,
                    AppResources.ConfirmPushTitle,
                    MessageBoxButton.OKCancel);

                if (result != MessageBoxResult.OK)
                {
                    StateService.GetNotifySettingsAsync(settings =>
                    {
                        var s                   = settings ?? new Settings();
                        s.ContactAlert          = false;
                        s.ContactMessagePreview = true;
                        s.ContactSound          = StateService.Sounds[0];
                        s.GroupAlert            = false;
                        s.GroupMessagePreview   = true;
                        s.GroupSound            = StateService.Sounds[0];

                        s.InAppMessagePreview = true;
                        s.InAppSound          = true;
                        s.InAppVibration      = true;

                        StateService.SaveNotifySettingsAsync(s);
                    });

                    MTProtoService.UpdateNotifySettingsAsync(
                        new TLInputNotifyUsers(),
                        new TLInputPeerNotifySettings
                    {
                        EventsMask   = new TLInt(1),
                        MuteUntil    = new TLInt(int.MaxValue),
                        ShowPreviews = new TLBool(true),
                        Sound        = new TLString(StateService.Sounds[0])
                    },
                        r => { });

                    MTProtoService.UpdateNotifySettingsAsync(
                        new TLInputNotifyChats(),
                        new TLInputPeerNotifySettings
                    {
                        EventsMask   = new TLInt(1),
                        MuteUntil    = new TLInt(int.MaxValue),
                        ShowPreviews = new TLBool(true),
                        Sound        = new TLString(StateService.Sounds[0])
                    },
                        r => { });
                }
                else
                {
                    StateService.GetNotifySettingsAsync(settings =>
                    {
                        var s                   = settings ?? new Settings();
                        s.ContactAlert          = true;
                        s.ContactMessagePreview = true;
                        s.ContactSound          = StateService.Sounds[0];
                        s.GroupAlert            = true;
                        s.GroupMessagePreview   = true;
                        s.GroupSound            = StateService.Sounds[0];

                        s.InAppMessagePreview = true;
                        s.InAppSound          = true;
                        s.InAppVibration      = true;

                        StateService.SaveNotifySettingsAsync(s);
                    });

                    MTProtoService.UpdateNotifySettingsAsync(
                        new TLInputNotifyUsers(),
                        new TLInputPeerNotifySettings
                    {
                        EventsMask   = new TLInt(1),
                        MuteUntil    = new TLInt(0),
                        ShowPreviews = new TLBool(true),
                        Sound        = new TLString(StateService.Sounds[0])
                    },
                        r => { });

                    MTProtoService.UpdateNotifySettingsAsync(
                        new TLInputNotifyChats(),
                        new TLInputPeerNotifySettings
                    {
                        EventsMask   = new TLInt(1),
                        MuteUntil    = new TLInt(0),
                        ShowPreviews = new TLBool(true),
                        Sound        = new TLString(StateService.Sounds[0])
                    },
                        r => { });
                }

                MTProtoService.SetInitState();
                StateService.CurrentUserId        = auth.User.Index;
                StateService.ClearNavigationStack = true;
                StateService.FirstRun             = true;
                SettingsHelper.SetValue(Constants.IsAuthorizedKey, true);
                NavigationService.UriFor <ShellViewModel>().Navigate();
                IsWorking = false;
            }),
                                              error => BeginOnUIThread(() =>
            {
#if LOG_REGISTRATION
                TLUtils.WriteLog("auth.checkPassword error " + error);
#endif
                IsWorking = false;
                if (error.TypeEquals(ErrorType.PASSWORD_HASH_INVALID))
                {
                    MessageBox.Show(AppResources.PasswordInvalidString, AppResources.Error, MessageBoxButton.OK);
                }
                else if (error.CodeEquals(ErrorCode.FLOOD))
                {
                    MessageBox.Show(AppResources.FloodWaitString + Environment.NewLine + "(" + error.Message + ")", AppResources.Error, MessageBoxButton.OK);
                }
                else
                {
                    Execute.ShowDebugMessage("account.checkPassword error " + error);
                }
            }));
        }
        public void Confirm()
        {
            if (_password == null)
            {
                return;
            }
            if (!CanConfirm)
            {
                return;
            }

            var password84 = _password as TLPassword84;

            if (password84 == null)
            {
                return;
            }

            var algo = password84.CurrentAlgo as TLPasswordKdfAlgoSHA256SHA256PBKDF2HMACSHA512iter100000SHA256ModPow;

            if (algo == null)
            {
                return;
            }

            IsWorking = true;
#if LOG_REGISTRATION
            TLUtils.WriteLog("auth.checkPassword");
#endif

            BeginOnThreadPool(() =>
            {
                //var passwordHash = Utils.Password.GetHash(_password.CurrentSalt, new TLString(Code));
                var passwordHash = SRP.GetX(new TLString(Code), algo);
                var password     = SRP.GetCheck(passwordHash, password84.SRPId, password84.SRPB, algo);

                MTProtoService.CheckPasswordAsync(
                    password,
                    auth => BeginOnUIThread(() =>
                {
#if LOG_REGISTRATION
                    TLUtils.WriteLog("auth.checkPassword result " + auth);
                    TLUtils.WriteLog("TLUtils.IsLogEnabled=false");
#endif

                    TLUtils.IsLogEnabled = false;
                    TLUtils.LogItems.Clear();

                    var result = MessageBox.Show(
                        AppResources.ConfirmPushMessage,
                        AppResources.ConfirmPushTitle,
                        MessageBoxButton.OKCancel);

                    if (result != MessageBoxResult.OK)
                    {
                        Notifications.Disable();
                    }
                    else
                    {
                        Notifications.Enable();
                    }

                    ConfirmViewModel.UpdateNotificationsAsync(MTProtoService, StateService);

                    MTProtoService.SetInitState();
                    StateService.CurrentUserId = auth.User.Index;
                    StateService.FirstRun      = true;
                    SettingsHelper.SetValue(Constants.IsAuthorizedKey, true);

                    ShellViewModel.Navigate(NavigationService);

                    IsWorking = false;
                }),
                    error => BeginOnUIThread(() =>
                {
#if LOG_REGISTRATION
                    TLUtils.WriteLog("auth.checkPassword error " + error);
#endif
                    IsWorking = false;
                    if (error.TypeEquals(ErrorType.PASSWORD_HASH_INVALID))
                    {
                        MTProtoService.GetPasswordAsync(
                            result1 => BeginOnUIThread(() =>
                        {
                            _password = result1 as TLPassword84;
                            MessageBox.Show(AppResources.PasswordInvalidString, AppResources.Error, MessageBoxButton.OK);
                        }),
                            error1 => BeginOnUIThread(() =>
                        {
                            MessageBox.Show(AppResources.PasswordInvalidString, AppResources.Error, MessageBoxButton.OK);
                        }));
                    }
                    else if (error.CodeEquals(ErrorCode.FLOOD))
                    {
                        MessageBox.Show(AppResources.FloodWaitString + Environment.NewLine + "(" + error.Message + ")", AppResources.Error, MessageBoxButton.OK);
                    }
                    else if (error.TypeEquals(ErrorType.SRP_ID_INVALID))
                    {
                        IsWorking = true;
                        MTProtoService.GetPasswordAsync(
                            result1 => BeginOnUIThread(() =>
                        {
                            IsWorking = false;
                            _password = result1 as TLPassword84;
                            Confirm();
                        }),
                            error1 => BeginOnUIThread(() =>
                        {
                            IsWorking = false;
                        }));
                    }
                    else
                    {
                        Execute.ShowDebugMessage("account.checkPassword error " + error);
                    }
                }));
            });
        }