Пример #1
0
        /// <summary>
        /// Returns user's subscriptions
        /// </summary>
        public async Task <IEnumerable <IAzureUserAccountSubscriptionContext> > GetSubscriptionsAsync()
        {
            var  result = Enumerable.Empty <IAzureUserAccountSubscriptionContext>();
            bool userNeedsAuthentication = await GetUserNeedsReauthenticationAsync();

            if (!userNeedsAuthentication)
            {
                AzureUserAccount currentUser = await GetCurrentAccountInternalAsync();

                if (currentUser != null)
                {
                    try
                    {
                        result = await GetSubscriptionsFromCacheAsync(currentUser);
                    }
                    catch (ServiceExceptionBase)
                    {
                        throw;
                    }
                    catch (Exception ex)
                    {
                        throw new ServiceFailedException(SR.FailedToGetAzureSubscriptionsErrorMessage, ex);
                    }
                }
                result = result ?? Enumerable.Empty <IAzureUserAccountSubscriptionContext>();
            }
            return(result);
        }
 private void CopyFrom(IAzureUserAccount azureUserAccount)
 {
     this.DisplayInfo           = new AzureUserAccountDisplayInfo(azureUserAccount.DisplayInfo);
     this.NeedsReauthentication = azureUserAccount.NeedsReauthentication;
     this.TenantId   = azureUserAccount.TenantId;
     this.AllTenants = azureUserAccount.AllTenants;
     this.UniqueId   = azureUserAccount.UniqueId;
     AzureUserAccount account = azureUserAccount as AzureUserAccount;
 }
Пример #3
0
        private Task <AzureUserAccount> GetCurrentAccountInternalAsync()
        {
            AzureUserAccount account = null;

            if (currentAccountId != null &&
                accountsMap.TryGetValue(currentAccountId, out account))
            {
                // TODO is there more needed here?
            }
            return(Task.FromResult <AzureUserAccount>(account));
        }
Пример #4
0
        /// <summary>
        /// Public for testing purposes. Creates an Azure account with the correct set of mappings for tenants etc.
        /// </summary>
        /// <param name="accountTokenWrapper"></param>
        /// <returns></returns>
        public AzureUserAccount CreateUserAccount(AccountTokenWrapper accountTokenWrapper)
        {
            Account account = accountTokenWrapper.Account;

            CommonUtil.CheckForNull(accountTokenWrapper, nameof(accountTokenWrapper));
            CommonUtil.CheckForNull(account, nameof(account));
            CommonUtil.CheckForNull(account.Key, nameof(account) + ".Key");
            CommonUtil.CheckForNull(accountTokenWrapper.SecurityTokenMappings, nameof(account) + ".SecurityTokenMappings");
            AzureUserAccount userAccount = new AzureUserAccount();

            userAccount.UniqueId              = account.Key.AccountId;
            userAccount.DisplayInfo           = ToDisplayInfo(account);
            userAccount.NeedsReauthentication = account.IsStale;
            userAccount.AllTenants            = ProcessTenants(accountTokenWrapper, account);
            return(userAccount);
        }
Пример #5
0
        /// <summary>
        /// Set current logged in user
        /// </summary>
        public async Task <IUserAccount> SetCurrentAccountAsync(object account)
        {
            CommonUtil.CheckForNull(account, nameof(account));
            AccountTokenWrapper accountTokenWrapper = account as AccountTokenWrapper;

            if (accountTokenWrapper != null)
            {
                AzureUserAccount userAccount = CreateUserAccount(accountTokenWrapper);
                accountsMap[userAccount.UniqueId] = userAccount;
                currentAccountId = userAccount.UniqueId;
            }
            else
            {
                throw new ServiceFailedException(string.Format(CultureInfo.CurrentCulture, SR.UnsupportedAuthType, account.GetType().Name));
            }
            OnCurrentAccountChanged();
            return(await GetCurrentAccountAsync());
        }
Пример #6
0
        private async Task <IEnumerable <IAzureUserAccountSubscriptionContext> > GetSubscriptionFromServiceAsync(AzureUserAccount userAccount)
        {
            CommonUtil.CheckForNull(userAccount, nameof(userAccount));
            List <IAzureUserAccountSubscriptionContext> subscriptionList = new List <IAzureUserAccountSubscriptionContext>();

            if (userAccount.NeedsReauthentication)
            {
                throw new ExpiredTokenException(SR.UserNeedsAuthenticationError);
            }
            try
            {
                IAzureResourceManager resourceManager = ServiceProvider.GetService <IAzureResourceManager>();
                IEnumerable <IAzureUserAccountSubscriptionContext> contexts = await resourceManager.GetSubscriptionContextsAsync(userAccount);

                subscriptionList = contexts.ToList();
            }
            catch (ServiceExceptionBase)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw new ServiceFailedException(SR.FailedToGetAzureSubscriptionsErrorMessage, ex);
            }
            return(subscriptionList);
        }
Пример #7
0
        private async Task <IEnumerable <IAzureUserAccountSubscriptionContext> > GetSubscriptionsFromCacheAsync(AzureUserAccount user)
        {
            var result = Enumerable.Empty <IAzureUserAccountSubscriptionContext>();

            if (user != null)
            {
                result = _subscriptionCache.Get(user.UniqueId);
                if (result == null)
                {
                    result = await GetSubscriptionFromServiceAsync(user);

                    _subscriptionCache.UpdateCache(user.UniqueId, result);
                }
            }
            result = result ?? Enumerable.Empty <IAzureUserAccountSubscriptionContext>();
            return(result);
        }