private MockResponse ProcessMockRequest(MockRequest mockRequest, string code, string token)
        {
            IdentityTestEnvironment testEnvironment = new IdentityTestEnvironment();
            string requestUrl = mockRequest.Uri.ToUri().AbsoluteUri;

            if (requestUrl.StartsWith(new Uri(new Uri(testEnvironment.AuthorityHostUrl), "common/discovery/instance").ToString()))
            {
                return(DiscoveryInstanceResponse);
            }

            if (requestUrl.StartsWith(new Uri(new Uri(testEnvironment.AuthorityHostUrl), "organizations/v2.0/.well-known/openid-configuration").ToString()))
            {
                return(OpenIdConfigurationResponse);
            }

            if (requestUrl.StartsWith("https://login.microsoftonline.com/organizations/oauth2/v2.0/devicecode"))
            {
                return(CreateDeviceCodeResponse(code));
            }

            if (requestUrl.StartsWith("https://login.microsoftonline.com/organizations/oauth2/v2.0/token"))
            {
                return(CreateTokenResponse(code, token));
            }

            throw new InvalidOperationException();
        }
 public ManagedIdenityEnvironment(IdentityTestEnvironment env)
 {
     _envVars = new TestEnvVar[]
     {
         new TestEnvVar("IDENTITY_ENDPOINT", env.IdentityEndpoint),
         new TestEnvVar("IMDS_ENDPOINT", env.ImdsEndpoint),
         new TestEnvVar("MSI_ENDPOINT", env.MsiEndpoint),
         new TestEnvVar("MSI_SECRET", env.MsiSecret),
         new TestEnvVar("IDENTITY_HEADER", env.IdentityHeader),
         new TestEnvVar("IDENTITY_SERVER_THUMBPRINT", env.IdentityServerThumbprint)
     };
 }
 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 void AuthenticateWithDeviceCodeCallbackThrowsAsync()
        {
            IdentityTestEnvironment testEnvironment = new IdentityTestEnvironment();
            var cancelSource = new CancellationTokenSource();
            var options      = new TokenCredentialOptions();

            var cred = InstrumentClient(new DeviceCodeCredential(ThrowingDeviceCodeCallback, null, ClientId, options, null, mockPublicMsalClient));

            var ex = Assert.ThrowsAsync <AuthenticationFailedException>(
                async() => await cred.GetTokenAsync(new TokenRequestContext(new[] { testEnvironment.KeyvaultScope }), cancelSource.Token));

            Assert.IsInstanceOf(typeof(MockException), ex.InnerException);
        }
        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))));
        }
예제 #6
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");
        }
        public void AuthenticateWithDeviceCodeCallbackThrowsAsync()
        {
            IdentityTestEnvironment testEnvironment = new IdentityTestEnvironment();
            var expectedCode = Guid.NewGuid().ToString();

            var expectedToken = Guid.NewGuid().ToString();

            var cancelSource = new CancellationTokenSource();

            var mockTransport = new MockTransport(request => ProcessMockRequest(request, expectedCode, expectedToken));

            var options = new TokenCredentialOptions()
            {
                Transport = mockTransport
            };

            var cred = InstrumentClient(new DeviceCodeCredential(ThrowingDeviceCodeCallback, ClientId, options: options));

            var ex = Assert.ThrowsAsync <AuthenticationFailedException>(async() => await cred.GetTokenAsync(new TokenRequestContext(new string[] { testEnvironment.KeyvaultScope }), cancelSource.Token));

            Assert.IsInstanceOf(typeof(MockException), ex.InnerException);
        }