public ManagedIdenityEnvironment(IdentityTestEnvironment env)
 {
     _envVar =
         new TestEnvVar(
             new Dictionary <string, string>
     {
         { "IDENTITY_ENDPOINT", env.IdentityEndpoint },
         { "IMDS_ENDPOINT", env.ImdsEndpoint },
         { "MSI_ENDPOINT", env.MsiEndpoint },
         { "MSI_SECRET", env.MsiSecret }, { "IDENTITY_HEADER", env.IdentityHeader },
         { "IDENTITY_SERVER_THUMBPRINT", env.IdentityServerThumbprint }
     });
 }
        public override TokenCredential GetTokenCredential(TokenCredentialOptions options)
        {
            using var env = new TestEnvVar(new Dictionary <string, string> { { "TENANT_ID", TenantId } });
            var environment = new IdentityTestEnvironment();
            var vscOptions  = new VisualStudioCodeCredentialOptions
            {
                Diagnostics = { IsAccountIdentifierLoggingEnabled = options.Diagnostics.IsAccountIdentifierLoggingEnabled },
                TenantId    = environment.TenantId,
                Transport   = new MockTransport()
            };

            return(InstrumentClient(
                       new VisualStudioCodeCredential(
                           vscOptions,
                           null,
                           mockPublicMsalClient,
                           CredentialTestHelpers.CreateFileSystemForVisualStudioCode(environment),
                           new TestVscAdapter("VS Code Azure", "AzureCloud", expectedToken))));
        }
        public void CreateClientRespectsCaeConfig(
            [Values(true, false, null)] bool?setDisableSwitch,
            [Values(true, false, null)] bool?setDisableEnvVar)
        {
            TestAppContextSwitch ctx = null;
            TestEnvVar           env = null;

            try
            {
                if (setDisableSwitch != null)
                {
                    ctx = new TestAppContextSwitch(IdentityCompatSwitches.DisableCP1ExecutionSwitchName, setDisableSwitch.Value.ToString());
                }
                if (setDisableEnvVar != null)
                {
                    env = new TestEnvVar(IdentityCompatSwitches.DisableCP1ExecutionEnvVar, setDisableEnvVar.Value.ToString());
                }

                var mock = new MockMsalPublicClient();
                mock.PubClientAppFactory = (capabilities) =>
                {
                    bool IsCp1Set = cp1 == string.Join("", capabilities);
                    if (setDisableSwitch.HasValue)
                    {
                        Assert.AreEqual(setDisableSwitch.Value, !IsCp1Set);
                    }
                    else
                    {
                        Assert.AreEqual(setDisableEnvVar.HasValue && setDisableEnvVar.Value, !IsCp1Set);
                    }
                    return(Moq.Mock.Of <IPublicClientApplication>());
                };

                mock.CallCreateClientAsync(false, default);
            }
            finally
            {
                ctx?.Dispose();
                env?.Dispose();
            }
        }
Пример #4
0
        public async Task AuthenticateWithVsCodeCredential([Values(null, TenantIdHint)] string tenantId, [Values(true)] bool allowMultiTenantAuthentication)
        {
            using var env = new TestEnvVar(new Dictionary<string, string> {{"TENANT_ID", TenantId}});
            var environment = new IdentityTestEnvironment();
            var options = new VisualStudioCodeCredentialOptions { TenantId = environment.TenantId, Transport = new MockTransport() };
            var context = new TokenRequestContext(new[] { Scope }, tenantId: tenantId);
            expectedTenantId = TenantIdResolver.Resolve(environment.TenantId, context);

            VisualStudioCodeCredential credential = InstrumentClient(
                new VisualStudioCodeCredential(
                    options,
                    null,
                    mockPublicMsalClient,
                    CredentialTestHelpers.CreateFileSystemForVisualStudioCode(environment),
                    new TestVscAdapter("VS Code Azure", "AzureCloud", expectedToken)));

            var actualToken = await credential.GetTokenAsync(context, CancellationToken.None);

            Assert.AreEqual(expectedToken, actualToken.Token, "Token should match");
            Assert.AreEqual(expiresOn, actualToken.ExpiresOn, "expiresOn should match");
        }