Example #1
0
        private void Authenticate(Window parentView)
        {
            if (!_Model.IsValid())
            {
                return;
            }

            if (!File.Exists(_Model.ServerCertificate))
            {
                return;
            }

            using (_BusyScope.Start())
            {
                ConnectedStatus = "Connecting";

                string rootCertificate = File.ReadAllText(_Model.ServerCertificate);

                var     creds   = new SslCredentials(rootCertificate);
                Channel channel = new Channel(_Model.AuthenticationChannel, creds);

                Pswmgr.AuthenticationRequest request = new Pswmgr.AuthenticationRequest();
                var client = new Pswmgr.Authentication.AuthenticationClient(channel);

                if (string.IsNullOrEmpty(_Model.Username) || string.IsNullOrEmpty(_Model.Password))
                {
                    LoginView view = new LoginView()
                    {
                        Owner = parentView,
                        WindowStartupLocation = parentView == null ? WindowStartupLocation.CenterScreen : WindowStartupLocation.CenterOwner
                    };
                    if (view.ShowDialog() == true)
                    {
                        request.Username = _Model.Username;
                        request.Password = _Model.Password;
                    }
                }

                var result = client.Authenticate(request);

                bool cancelled = false;
                while (result.TokenNeededFor2Fa && !cancelled)
                {
                    LoginView view = new LoginView(true)
                    {
                        Owner = parentView,
                        WindowStartupLocation = parentView == null ? WindowStartupLocation.CenterScreen : WindowStartupLocation.CenterOwner
                    };
                    if (view.ShowDialog() == true)
                    {
                        request.Username = _Model.Username;
                        request.Password = _Model.Password;
                        request.TfaToken = int.Parse(_Model.TwoFactorAuthToken);
                    }
                    else
                    {
                        cancelled = true;
                    }
                    result = client.Authenticate(request);
                }

                _Token = result.Token;

                ConnectedStatus = "Authenticated";

                CallCredentials callCreds       = CallCredentials.FromInterceptor(CustomAuthProcessor);
                var             compositeCreds  = ChannelCredentials.Create(creds, callCreds);
                Channel         passwordChannel = new Channel(_Model.PasswordManagerChannel, compositeCreds);
                _Client = new Pswmgr.PasswordManager.PasswordManagerClient(passwordChannel);

                FetchPasswords();
            }
        }
Example #2
0
 public LoginViewModel(LoginView view, bool twoFactor = false)
 {
     _View  = view;
     _Model = App.Instance.Conf;
     _TwoFactorVisibility = twoFactor;
 }