Class that represents the current active gcloud config. The active gcloud config is the config that is listed when running "gcloud config list". This active config can be changed if the user run commands like "gcloud config set account" or if certain CLOUDSDK_* environment variables are changed.
Пример #1
0
        /// <summary>
        /// Gets the current active config. This value will be cached for the next call.
        /// If refreshConfig is true, however, we will refresh the cache. Everytime the cache is refreshed,
        /// a new access token will be generated for the current active config (even if there is no change
        /// in the current active config).
        /// </summary>
        public async static Task <ActiveUserConfig> GetActiveUserConfig(
            CancellationToken cancellationToken = default(CancellationToken),
            bool refreshConfig = false)
        {
            if (ActiveConfig != null && !refreshConfig)
            {
                // Check that finger print has not changed.
                string newFingerPrint = ActiveConfig.GetCurrentConfigurationFingerPrint();
                if (ActiveConfig.CachedFingerPrint == newFingerPrint)
                {
                    return(ActiveConfig);
                }
            }

            // Otherwise, we have to create a new active config.
            await s_lock.WaitAsync();

            try
            {
                string activeConfigJson = await GCloudWrapper.GetActiveConfig();

                cancellationToken.ThrowIfCancellationRequested();
                ActiveConfig = new ActiveUserConfig(activeConfigJson);
                return(ActiveConfig);
            }
            finally
            {
                s_lock.Release();
            }
        }
        /// <summary>
        /// Gets the current active config. This value will be cached for the next call.
        /// If refreshConfig is true, however, we will refresh the cache. Everytime the cache is refreshed,
        /// a new access token will be generated for the current active config (even if there is no change
        /// in the current active config).
        /// </summary>
        public async static Task<ActiveUserConfig> GetActiveUserConfig(
            CancellationToken cancellationToken = default(CancellationToken),
            bool refreshConfig = false)
        {
            if (s_activeUserConfig != null && !refreshConfig)
            {
                // Check that finger print has not changed.
                string newFingerPrint = s_activeUserConfig.GetCurrentConfigurationFingerPrint();
                if (s_activeUserConfig.CachedFingerPrint == newFingerPrint)
                {
                    return s_activeUserConfig;
                }
            }

            // Otherwise, we have to create a new active config.
            await s_lock.WaitAsync();
            try
            {
                string activeConfigJson = await GCloudWrapper.GetActiveConfig();
                cancellationToken.ThrowIfCancellationRequested();
                s_activeUserConfig = new ActiveUserConfig(activeConfigJson);
                return s_activeUserConfig;
            }
            finally
            {
                s_lock.Release();
            }
        }
Пример #3
0
        /// <summary>
        /// Asynchronously revokes the token by calling
        /// <see cref="Google.Apis.Auth.OAuth2.Flows.IAuthorizationCodeFlow.RevokeTokenAsync"/>.
        /// </summary>
        /// <param name="taskCancellationToken">Cancellation token to cancel an operation.</param>
        /// <returns><c>true</c> if the token was revoked successfully.</returns>
        public async Task <bool> RevokeTokenAsync(CancellationToken taskCancellationToken)
        {
            TokenResponse userToken = await ActiveUserConfig.GetActiveUserToken(taskCancellationToken);

            await flow.RevokeTokenAsync("userId", userToken.AccessToken, taskCancellationToken).ConfigureAwait(false);

            // We don't set the token to null, cause we want that the next request (without reauthorizing) will fail).
            return(true);
        }
Пример #4
0
        /// <summary>
        /// Refreshes the token by calling to ActiveUserConfig.GetActiveUserToken
        /// </summary>
        public async Task <bool> RefreshTokenAsync(CancellationToken taskCancellationToken)
        {
            TokenResponse userToken = await ActiveUserConfig.GetActiveUserToken(taskCancellationToken, refresh : true);

            if (userToken != null && userToken.AccessToken != null)
            {
                return(true);
            }
            return(false);
        }
Пример #5
0
        /// <summary>
        /// Returns a property of the configuration based on the given key.
        /// </summary>
        public async static Task <string> GetPropertyValue(string key)
        {
            string           result       = null;
            ActiveUserConfig activeConfig = await GetActiveUserConfig();

            if (activeConfig != null && activeConfig.PropertiesJson.TryGetPropertyValue(key, ref result))
            {
                return(result);
            }
            return(result);
        }
Пример #6
0
        /// <summary>
        /// Gets the token that belongs to the current active config.
        /// This value will be normally be cached as the current active user config is normally cached.
        /// If refresh is true or if the current token already expired, however, we will refresh
        /// the active user config to get a new token.
        /// </summary>
        public async static Task <TokenResponse> GetActiveUserToken(CancellationToken cancellationToken, bool refresh = false)
        {
            ActiveUserConfig userConfig = null;

            if (!refresh)
            {
                userConfig = await GetActiveUserConfig(cancellationToken);

                if (!userConfig.UserToken.IsExpired)
                {
                    return(userConfig.UserToken);
                }
            }
            userConfig = await GetActiveUserConfig(cancellationToken, refreshConfig : true);

            return(userConfig.UserToken);
        }
 /// <summary>
 /// Returns the setting with the given name from the currently active gcloud configuration.
 /// </summary>
 public static string GetSettingsValue(string settingName)
 {
     try
     {
         return(ActiveUserConfig.GetPropertyValue(settingName).Result);
     }
     catch (AggregateException aggEx)
     {
         if (aggEx.InnerExceptions != null)
         {
             // Exception thrown by GCloudWrapper is InvalidOperationException.
             Exception invalidOpEx = aggEx.InnerExceptions.FirstOrDefault(ex => ex is InvalidOperationException);
             if (invalidOpEx != null)
             {
                 throw invalidOpEx;
             }
         }
         throw aggEx;
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }
Пример #8
0
        public virtual async Task <string> GetAccessTokenForRequestAsync(string authUri = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            TokenResponse token = await ActiveUserConfig.GetActiveUserToken(cancellationToken);

            return(token.AccessToken);
        }