public void CanStoreAndRetrieveSingleKeyCorrectly()
        {
            const string appId    = "myServiceKeyTest";
            string       tenantId = Guid.NewGuid().ToString();
            const string password = "******";

            using (SecureString secret = SecureStringFromString(password))
            {
                ServicePrincipalKeyStore.SaveKey(appId, tenantId, secret);
            }

            try
            {
                string retrievedPassword;
                using (SecureString secret = ServicePrincipalKeyStore.GetKey(appId, tenantId))
                {
                    retrievedPassword = StringFromSecureString(secret);
                }

                Assert.Equal(password, retrievedPassword);
            }
            finally
            {
                ServicePrincipalKeyStore.DeleteKey(appId, tenantId);
            }
        }
        public void CanStoreMultipleKeysIndependently()
        {
            const string appId   = "myKeyTest";
            string       tenant1 = Guid.NewGuid().ToString();
            string       tenant2 = Guid.NewGuid().ToString();
            const string pwd1    = "password one";
            const string pwd2    = "second password";

            using (SecureString ss = SecureStringFromString(pwd1))
            {
                ServicePrincipalKeyStore.SaveKey(appId, tenant1, ss);
            }

            using (SecureString ss = SecureStringFromString(pwd2))
            {
                ServicePrincipalKeyStore.SaveKey(appId, tenant2, ss);
            }
            try
            {
                using (SecureString ss = ServicePrincipalKeyStore.GetKey(appId, tenant1))
                {
                    Assert.Equal(pwd1, StringFromSecureString(ss));
                }

                using (SecureString ss = ServicePrincipalKeyStore.GetKey(appId, tenant2))
                {
                    Assert.Equal(pwd2, StringFromSecureString(ss));
                }
            }
            finally
            {
                ServicePrincipalKeyStore.DeleteKey(appId, tenant1);
                ServicePrincipalKeyStore.DeleteKey(appId, tenant2);
            }
        }
        public AuthenticationFactory()
        {
            _getKeyStore = () =>
            {
                IServicePrincipalKeyStore keyStore = null;
                if (!AzureSession.Instance.TryGetComponent(ServicePrincipalKeyStore.Name, out keyStore))
                {
                    keyStore = new ServicePrincipalKeyStore();
                }

                return(keyStore);
            };

            _getAuthenticator = () =>
            {
                IAuthenticatorBuilder builder = null;
                if (!AzureSession.Instance.TryGetComponent(AuthenticatorBuilder.AuthenticatorBuilderKey, out builder))
                {
                    builder = new AuthenticatorBuilder();
                }

                return(builder);
            };

            TokenProvider = new AdalTokenProvider(_getKeyStore);
        }
Ejemplo n.º 4
0
        public void RemoveUser(IAzureAccount account, IAzureTokenCache tokenCache)
        {
            TokenCache cache = tokenCache as TokenCache;

            if (cache != null && account != null && !string.IsNullOrEmpty(account.Id) && !string.IsNullOrWhiteSpace(account.Type))
            {
                switch (account.Type)
                {
                case AzureAccount.AccountType.AccessToken:
                    account.SetProperty(AzureAccount.Property.AccessToken, null);
                    account.SetProperty(AzureAccount.Property.GraphAccessToken, null);
                    account.SetProperty(AzureAccount.Property.KeyVaultAccessToken, null);
                    break;

                case AzureAccount.AccountType.ServicePrincipal:
                    try
                    {
                        ServicePrincipalKeyStore.DeleteKey(account.Id, account.GetTenants().FirstOrDefault());
                    }
                    catch
                    {
                        // make best effort to remove credentials
                    }

                    RemoveFromTokenCache(cache, account);
                    break;

                case AzureAccount.AccountType.User:
                    RemoveFromTokenCache(cache, account);
                    break;
                }
            }
        }
        public void RetrievingNonExistingKeyReturnsNull()
        {
            const string appId    = "myKeyTest";
            string       tenantId = Guid.NewGuid().ToString();

            SecureString ss = null;

            try
            {
                ss = ServicePrincipalKeyStore.GetKey(appId, tenantId);
                Assert.Null(ss);
            }
            finally
            {
                if (ss != null)
                {
                    ss.Dispose();
                }
            }
        }
        public void DeletingKeyWorks()
        {
            const string appId    = "myKeyTest";
            string       tenantId = Guid.NewGuid().ToString();

            using (SecureString ss = SecureStringFromString("sekret"))
            {
                ServicePrincipalKeyStore.SaveKey(appId, tenantId, ss);
            }

            using (SecureString ss = ServicePrincipalKeyStore.GetKey(appId, tenantId))
            {
                Assert.NotNull(ss);
            }

            ServicePrincipalKeyStore.DeleteKey(appId, tenantId);

            using (SecureString ss = ServicePrincipalKeyStore.GetKey(appId, tenantId))
            {
                Assert.Null(ss);
            }
        }
Ejemplo n.º 7
0
 private void StoreAppKey(string appId, string tenantId, SecureString appKey)
 {
     ServicePrincipalKeyStore.SaveKey(appId, tenantId, appKey);
 }
Ejemplo n.º 8
0
 private SecureString LoadAppKey(string appId, string tenantId)
 {
     return(ServicePrincipalKeyStore.GetKey(appId, tenantId));
 }