Ejemplo n.º 1
0
 /// <summary>
 /// Raises event LoginResultEvent.
 /// </summary>
 /// <param name="groups"></param>
 public static void RaiseLoginResultEvent(LoginResponse response)
 {
     if (LoginResultEvent != null)
     {
         LoginResultEvent.Invoke(response);
     }
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Gets verification code from mozilla-vpn:// redirect after the user logs in through the browser.
        /// </summary>
        /// <param name="codeVerifier">The code sent in the PKCE auth request.</param>
        /// <param name="code">Verification code from the localhost redirect after logging in.</param>
        public void VerifyUserLogin(string codeVerifier, string code)
        {
            // Make the POST request to the verify endpoint
            ApiRequest api = new ApiRequest(string.Empty, $"{ProductConstants.BaseUrl}{ApiVersion}/vpn/login/verify", RestSharp.Method.POST);

            // Create a new dictionary to hold the code and code_verifier.
            Dictionary <string, string> postBody = new Dictionary <string, string>();

            postBody.Add("code", code);
            postBody.Add("code_verifier", codeVerifier);
            api.AddPostBody(postBody);

            // Execute the request
            var response = api.SendRequest();

            if (response == null || response.StatusCode != HttpStatusCode.OK)
            {
                ErrorHandling.ErrorHandler.Handle(new ErrorHandling.UserFacingMessage("toast-login-url-retrieval-error"), ErrorHandling.UserFacingErrorType.None, ErrorHandling.LogLevel.Error);
                return;
            }

            // Check login credentials, add the session info to the settings, and add a new device
            bool processLoginResult = Manager.Account.ProcessLogin(response.Content);

            Manager.Account.LoginState = LoginState.LoggedIn;
            Manager.StartUIUpdaters();

            var maxDevicesReached = !processLoginResult && Manager.Account.Config.FxALogin.User.Devices.Count() >= Manager.Account.Config.FxALogin.User.MaxDevices;

            Cache.FxAServerList.RetrieveRemoteServerList();

            Application.Current.Dispatcher.Invoke(() =>
            {
                var owner = Application.Current.MainWindow;
                if (owner != null)
                {
                    if (!Manager.MustUpdate)
                    {
                        if (maxDevicesReached)
                        {
                            ((UI.MainWindow)owner).NavigateToView(new UI.DevicesView(deviceLimitReached: true, fxaJson: response.Content), UI.MainWindow.SlideDirection.Left);
                        }
                        else
                        {
                            ((UI.MainWindow)owner).NavigateToView(new UI.OnboardingView5(), UI.MainWindow.SlideDirection.Left);
                        }
                    }

                    ((UI.MainWindow)owner).Show();
                    ((UI.MainWindow)owner).WindowState = WindowState.Normal;
                    ((UI.MainWindow)owner).Activate();
                }
            });

            LoginResultEvent?.Invoke(this, this, Manager.Account.LoginState);
        }
Ejemplo n.º 3
0
 public void OnLoginResult(object sender, MyEvent e)
 {
     LoginResultEvent?.Invoke(sender, e);
 }
Ejemplo n.º 4
0
        /// <summary>
        /// Polls a verification URL periodically to confirm if the user has logged in.
        /// </summary>
        /// <param name="queryUri">Login verification URL.</param>
        /// <param name="timeoutSeconds">Timeout (secs) between verification query attempts.</param>
        /// <param name="expiresAt">Expiration date of the verification URL.</param>
        /// <param name="cancelToken">Token used to cancel the login thread.</param>
        private void QueryLoginThread(string queryUri, int timeoutSeconds, DateTime expiresAt, CancellationToken cancelToken)
        {
            while (Manager.Account.LoginState == FxA.LoginState.LoggingIn && DateTime.Compare(DateTime.UtcNow, expiresAt) < 0)
            {
                try
                {
                    var queryRawData = QueryRawLoginState(queryUri);

                    if (cancelToken.IsCancellationRequested)
                    {
                        break;
                    }

                    if (queryRawData == null)
                    {
                        Thread.Sleep(TimeSpan.FromSeconds(timeoutSeconds));
                        continue;
                    }

                    var queryData = ParseLoginState(queryRawData);

                    if (queryData.User == null || queryData.User.Subscriptions == null || queryData.User.Subscriptions.Vpn == null)
                    {
                        Manager.Account.LoginState = LoginState.LoggedOut;
                        break;
                    }

                    if (!queryData.User.Subscriptions.Vpn.Active)
                    {
                        Manager.Account.LoginState = LoginState.LoggedOut;
                        break;
                    }

                    var processLoginResult = Manager.Account.ProcessLogin(queryRawData);
                    Manager.Account.LoginState = LoginState.LoggedIn;
                    Manager.StartUIUpdaters();

                    var maxDevicesReached = !processLoginResult && Manager.Account.Config.FxALogin.User.Devices.Count() >= Manager.Account.Config.FxALogin.User.MaxDevices;

                    Cache.FxAServerList.RetrieveRemoteServerList();
                    Application.Current.Dispatcher.Invoke(() =>
                    {
                        var owner = Application.Current.MainWindow;
                        if (owner != null)
                        {
                            if (!Manager.MustUpdate)
                            {
                                if (maxDevicesReached)
                                {
                                    ((UI.MainWindow)owner).NavigateToView(new UI.DevicesView(deviceLimitReached: true, fxaJson: queryRawData), UI.MainWindow.SlideDirection.Left);
                                }
                                else
                                {
                                    ((UI.MainWindow)owner).NavigateToView(new UI.OnboardingView5(), UI.MainWindow.SlideDirection.Left);
                                }
                            }

                            ((UI.MainWindow)owner).Show();
                            ((UI.MainWindow)owner).WindowState = WindowState.Normal;
                            ((UI.MainWindow)owner).Activate();
                        }
                    });
                }
                catch (Exception e)
                {
                    ErrorHandling.ErrorHandler.Handle(e, ErrorHandling.LogLevel.Debug);
                }

                Thread.Sleep(TimeSpan.FromSeconds(5));
            }

            LoginResultEvent?.Invoke(this, this, Manager.Account.LoginState);
        }