Ejemplo n.º 1
0
        public async Task WhenCalled_ThenQueryOpenIdConfigurationAsyncReturnsInfo()
        {
            var config = await GoogleAuthAdapter.QueryOpenIdConfigurationAsync(
                CancellationToken.None);

            Assert.IsNotNull(config.UserInfoEndpoint);
        }
Ejemplo n.º 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
            }
        }