Example #1
0
        private void MainForm_Shown(object sender, EventArgs _)
        {
            //
            // Authorize.
            //
            try
            {
                this.Authorization = AuthorizeDialog.Authorize(
                    this,
                    OAuthClient.Secrets,
                    new[] { IapTunnelingEndpoint.RequiredScope },
                    this.authSettings);
            }
            catch (Exception e)
            {
                this.serviceProvider
                .GetService <IExceptionDialog>()
                .Show(this, "Authorization failed", e);
            }

            if (this.Authorization == null)
            {
                // Not authorized -> close.
                Close();
            }

            //
            // Set up sub-windows.
            //
            SuspendLayout();

            this.dockPanel.Theme = this.vs2015LightTheme;
            this.vsToolStripExtender.SetStyle(
                this.mainMenu,
                VisualStudioToolStripExtender.VsVersion.Vs2015,
                this.vs2015LightTheme);
            this.vsToolStripExtender.SetStyle(
                this.statusStrip,
                VisualStudioToolStripExtender.VsVersion.Vs2015,
                this.vs2015LightTheme);

            // Show who is signed in.
            this.toolStripEmail.Text = this.Authorization.Email;

            ResumeLayout();

            this.serviceProvider.GetService <IProjectExplorer>().ShowWindow();

#if DEBUG
            this.serviceProvider.GetService <DebugWindow>().ShowWindow();
#endif
        }
Example #2
0
        public static IAuthorization Authorize(
            Control parent,
            ClientSecrets clientSecrets,
            string[] scopes,
            IDataStore dataStore)
        {
            // N.B. Do not dispose the adapter (and embedded GoogleAuthorizationCodeFlow)
            // as it might be needed for token refreshes later.
            var oauthAdapter = new GoogleAuthAdapter(
                new GoogleAuthorizationCodeFlow.Initializer
            {
                ClientSecrets = clientSecrets,
                Scopes        = scopes,
                DataStore     = dataStore
            },
                Resources.AuthorizationSuccessful);

            Exception caughtException = null;

            using (var dialog = new AuthorizeDialog())
            {
                Task.Run(async() =>
                {
                    try
                    {
                        // Try to authorize using OAuth.
                        dialog.authorization = await OAuthAuthorization.TryLoadExistingAuthorizationAsync(
                            oauthAdapter,
                            CancellationToken.None)
                                               .ConfigureAwait(true);

                        if (dialog.authorization != null)
                        {
                            // We have existing credentials, there is no need to even
                            // show the "Sign In" button.
                            parent.BeginInvoke((Action)(() => dialog.Close()));
                        }
                        else
                        {
                            // No valid credentials present, request user to authroize
                            // by showing the "Sign In" button.
                            parent.BeginInvoke((Action)(() => dialog.ToggleSignInButton()));
                        }
                    }
                    catch (Exception)
                    {
                        // Something went wrong trying to load existing credentials.
                        parent.BeginInvoke((Action)(() => dialog.ToggleSignInButton()));
                    }
                });

                dialog.signInButton.Click += async(sender, args) =>
                {
                    // Switch to showing spinner so that a user cannot click twice.
                    dialog.ToggleSignInButton();

                    try
                    {
                        dialog.authorization = await OAuthAuthorization.CreateAuthorizationAsync(
                            oauthAdapter,
                            CancellationToken.None)
                                               .ConfigureAwait(true);
                    }
                    catch (Exception e)
                    {
                        caughtException = e;
                    }

                    dialog.Close();
                };

                dialog.ShowDialog(parent);

#pragma warning disable CA1508 // Avoid dead conditional code
                if (caughtException != null)
                {
                    throw caughtException;
                }
                else
                {
                    return(dialog.authorization);
                }
#pragma warning restore CA1508 // Avoid dead conditional code
            }
        }