This class stores the complete data for a user account. The class can be serialize to a .json string with an schema compatible with gcloud by design. The serialized form of this class can be consumed by gcloud via the --credential-file-override parameter. The serialize form can also be consumed as the "application default credentials" by apps using Google's client NuGet packages.
        public UserAccountViewModel(UserAccount userAccount)
        {
            UserAccount = userAccount;

            AccountName = userAccount.AccountName;

            var dataSource = new GPlusDataSource(userAccount.GetGoogleCredential(), GoogleCloudExtensionPackage.VersionedApplicationName);
            var personTask = dataSource.GetProfileAsync();

            // TODO: Show the default image while it is being loaded.
            ProfilePictureAsync = AsyncPropertyValueUtils.CreateAsyncProperty(personTask, x => x.Image.Url);
            NameAsync = AsyncPropertyValueUtils.CreateAsyncProperty(personTask, x => x.DisplayName, Resources.CloudExplorerLoadingMessage);
        }
        private static string GetFileName(UserAccount userAccount)
        {
            var serialized = JsonConvert.SerializeObject(userAccount);
            var sha1 = SHA1.Create();
            var hash = sha1.ComputeHash(Encoding.UTF8.GetBytes(serialized));

            StringBuilder sb = new StringBuilder();
            foreach (byte b in hash)
            {
                sb.AppendFormat("{0:x2}", b);
            }
            sb.Append(".json");
            return sb.ToString();
        }
 private static string SaveUserAccount(UserAccount userAccount)
 {
     var name = GetFileName(userAccount);
     var savePath = Path.Combine(s_credentialsStoreRoot, name);
     SaveUserAccount(userAccount, savePath);
     return name;
 }
 private static void SaveUserAccount(UserAccount userAccount, string path)
 {
     try
     {
         var serialized = JsonConvert.SerializeObject(userAccount);
         AtomicFileWrite(path, serialized);
     }
     catch (IOException ex)
     {
         Debug.WriteLine($"Failed to save user account to {path}: {ex.Message}");
         throw new CredentialsStoreException(ex.Message, ex);
     }
 }
 /// <summary>
 /// Resets the credentials state to the account with the given <paramref name="accountName"/> and the
 /// given <paramref name="projectId"/>. The <seealso cref="Reset"/> event will be raised to notify
 /// listeners on this.
 /// If <paramref name="accountName"/> cannot be found in the store then the credentials will be reset
 /// to empty.
 /// </summary>
 /// <param name="accountName">The name of the account to make current.</param>
 /// <param name="projectId">The projectId to make current.</param>
 public void ResetCredentials(string accountName, string projectId)
 {
     var newCurrentAccount = GetAccount(accountName);
     if (newCurrentAccount != null)
     {
         _currentAccount = newCurrentAccount;
         _currentProjectId = projectId;
     }
     else
     {
         Debug.WriteLine($"Unknown account: {accountName}");
         _currentAccount = null;
         _currentProjectId = null;
     }
     Reset?.Invoke(this, EventArgs.Empty);
 }
 /// <summary>
 /// Stores a new set of user credentials in the credentials store.
 /// </summary>
 /// <param name="userAccount"></param>
 /// <returns></returns>
 public void AddAccount(UserAccount userAccount)
 {
     EnsureCredentialsRootExist();
     var name = SaveUserAccount(userAccount);
     _cachedCredentials[userAccount.AccountName] = new StoredUserAccount
     {
         FileName = name,
         UserAccount = userAccount,
     };
 }
        /// <summary>
        /// Deletes the <paramref name="account"/> from the store. The account must exist in the store
        /// or it will throw.
        /// </summary>
        /// <param name="account">The accound to delete.</param>
        /// <returns>True if the current account was deleted, false otherwise.</returns>
        public void DeleteAccount(UserAccount account)
        {
            var accountFilePath = GetUserAccountPath(account.AccountName);
            if (accountFilePath == null)
            {
                Debug.WriteLine($"Should not be here, unknown account name: {account.AccountName}");
                throw new InvalidOperationException($"Unknown account name: {account.AccountName}");
            }

            File.Delete(accountFilePath);
            var isCurrentAccount = account.AccountName == CurrentAccount?.AccountName;
            _cachedCredentials = LoadAccounts();
            if (isCurrentAccount)
            {
                ResetCredentials(null, null);
            }
        }
コード例 #8
0
 private static async Task<UserAccount> GetUserAccountForRefreshToken(string refreshToken)
 {
     var result = new UserAccount
     {
         RefreshToken = refreshToken,
         ClientId = s_extensionCredentials.ClientId,
         ClientSecret = s_extensionCredentials.ClientSecret
     };
     var plusDataSource = new GPlusDataSource(result.GetGoogleCredential(), GoogleCloudExtensionPackage.VersionedApplicationName);
     var person = await plusDataSource.GetProfileAsync();
     result.AccountName = person.Emails.FirstOrDefault()?.Value;
     return result;
 }
コード例 #9
0
 /// <summary>
 /// Deletes the given <paramref name="userAccount"/> from the store.
 /// </summary>
 /// <param name="userAccount"></param>
 public static void DeleteAccount(UserAccount userAccount) => CredentialsStore.Default.DeleteAccount(userAccount);
コード例 #10
0
 /// <summary>
 /// Deletes the given <paramref name="userAccount"/> from the store.
 /// </summary>
 /// <param name="userAccount"></param>
 public static void DeleteAccount(UserAccount userAccount) => CredentialsStore.Default.DeleteAccount(userAccount);