|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Globalization; |
| 7 | +using System.IdentityModel.Tokens.Jwt; |
| 8 | +using System.Linq; |
| 9 | +using System.Net.Http; |
| 10 | +using System.Security.Cryptography.X509Certificates; |
| 11 | +using System.Threading; |
| 12 | +using System.Threading.Tasks; |
| 13 | +using Microsoft.Identity.Client; |
| 14 | +using Microsoft.Identity.Client.RP; |
| 15 | +using Microsoft.Identity.Client.Cache; |
| 16 | +using Microsoft.Identity.Client.Internal; |
| 17 | +using Microsoft.Identity.Client.Internal.ClientCredential; |
| 18 | +using Microsoft.Identity.Client.OAuth2; |
| 19 | +using Microsoft.Identity.Client.PlatformsCommon.Shared; |
| 20 | +using Microsoft.Identity.Client.Utils; |
| 21 | +using Microsoft.Identity.Test.Common; |
| 22 | +using Microsoft.Identity.Test.Common.Core.Helpers; |
| 23 | +using Microsoft.Identity.Test.Common.Core.Mocks; |
| 24 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 25 | +using NSubstitute; |
| 26 | +using Microsoft.Identity.Client.Extensibility; |
| 27 | +using System.Net; |
| 28 | +using System.Security.Cryptography; |
| 29 | +using System.Text; |
| 30 | + |
| 31 | +namespace Microsoft.Identity.Test.Unit.PublicApiTests |
| 32 | +{ |
| 33 | + [TestClass] |
| 34 | + public class InstanceDiscoveryTests : TestBase |
| 35 | + { |
| 36 | + [TestMethod] |
| 37 | + public async Task ConfidentialClientUsingSecretNoInstanceDiscoveryTestAsync() |
| 38 | + { |
| 39 | + using (var httpManager = new MockHttpManager()) |
| 40 | + { |
| 41 | + |
| 42 | + var app = ConfidentialClientApplicationBuilder.Create(TestConstants.ClientId) |
| 43 | + .WithAuthority(new Uri(ClientApplicationBase.DefaultAuthority), true) |
| 44 | + .WithRedirectUri(TestConstants.RedirectUri) |
| 45 | + .WithClientSecret(TestConstants.ClientSecret) |
| 46 | + .WithHttpManager(httpManager) |
| 47 | + .WithInstanceDiscovery(false) |
| 48 | + .BuildConcrete(); |
| 49 | + |
| 50 | + var appCacheAccess = app.AppTokenCache.RecordAccess(); |
| 51 | + var userCacheAccess = app.UserTokenCache.RecordAccess(); |
| 52 | + |
| 53 | + httpManager.AddMockHandlerSuccessfulClientCredentialTokenResponseMessage(); |
| 54 | + |
| 55 | + var result = await app.AcquireTokenForClient(TestConstants.s_scope.ToArray()).ExecuteAsync(CancellationToken.None).ConfigureAwait(false); |
| 56 | + Assert.IsNotNull(result); |
| 57 | + Assert.IsNotNull("header.payload.signature", result.AccessToken); |
| 58 | + Assert.AreEqual(TestConstants.s_scope.AsSingleString(), result.Scopes.AsSingleString()); |
| 59 | + |
| 60 | + Assert.IsNotNull(app.UserTokenCache); |
| 61 | + Assert.IsNotNull(app.AppTokenCache); |
| 62 | + |
| 63 | + appCacheAccess.AssertAccessCounts(1, 1); |
| 64 | + userCacheAccess.AssertAccessCounts(0, 0); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + [TestMethod] |
| 69 | + [WorkItem(5545)] // https://github.com/AzureAD/microsoft-authentication-library-for-dotnet/issues/5545 |
| 70 | + public async Task NoInstanceDiscovery_AirgappedCould_TestAsync() |
| 71 | + { |
| 72 | + using (var httpManager = new MockHttpManager(disableInternalRetries: true)) |
| 73 | + using (new EnvVariableContext()) |
| 74 | + { |
| 75 | + Environment.SetEnvironmentVariable("REGION_NAME", TestConstants.Region); |
| 76 | + |
| 77 | + // Instance discovery explicitly disabled |
| 78 | + var app = ConfidentialClientApplicationBuilder.Create(TestConstants.ClientId) |
| 79 | + .WithAuthority(TestConstants.AuthorityNotKnownTenanted) |
| 80 | + .WithClientSecret(TestConstants.ClientSecret) |
| 81 | + .WithHttpManager(httpManager) |
| 82 | + .WithAzureRegion(ConfidentialClientApplication.AttemptRegionDiscovery) |
| 83 | + .WithInstanceDiscovery(false) |
| 84 | + .Build(); |
| 85 | + |
| 86 | + // Direct token request (no instance discovery mock!) |
| 87 | + httpManager.AddMockHandlerSuccessfulClientCredentialTokenResponseMessage(); |
| 88 | + |
| 89 | + // Act |
| 90 | + var result = await app.AcquireTokenForClient(TestConstants.s_scope.ToArray()) |
| 91 | + .ExecuteAsync(CancellationToken.None) |
| 92 | + .ConfigureAwait(false); |
| 93 | + |
| 94 | + // Assert happens when httpManager disposes and checks for unconsumed handlers |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + [TestMethod] |
| 99 | + [WorkItem(5546)] // https://github.com/AzureAD/microsoft-authentication-library-for-dotnet/issues/5546 |
| 100 | + public async Task HttpErrorsInDiscoveryShouldBeIgnored_AirgappedCould_TestAsync() |
| 101 | + { |
| 102 | + using (var httpManager = new MockHttpManager(disableInternalRetries: true)) |
| 103 | + using (new EnvVariableContext()) |
| 104 | + { |
| 105 | + Environment.SetEnvironmentVariable("REGION_NAME", TestConstants.Region); |
| 106 | + |
| 107 | + // Instance discovery explicitly disabled |
| 108 | + var app = ConfidentialClientApplicationBuilder.Create(TestConstants.ClientId) |
| 109 | + .WithAuthority(TestConstants.AuthorityNotKnownTenanted) |
| 110 | + .WithClientSecret(TestConstants.ClientSecret) |
| 111 | + .WithHttpManager(httpManager) |
| 112 | + .WithAzureRegion(ConfidentialClientApplication.AttemptRegionDiscovery) |
| 113 | + .Build(); |
| 114 | + |
| 115 | + httpManager.AddMockHandler( |
| 116 | + new MockHttpMessageHandler() |
| 117 | + { |
| 118 | + ExceptionToThrow = new HttpRequestException("Simulated network error") |
| 119 | + }); |
| 120 | + httpManager.AddMockHandlerSuccessfulClientCredentialTokenResponseMessage(); |
| 121 | + |
| 122 | + // Act |
| 123 | + var result = await app.AcquireTokenForClient(TestConstants.s_scope.ToArray()) |
| 124 | + .ExecuteAsync(CancellationToken.None) |
| 125 | + .ConfigureAwait(false); |
| 126 | + |
| 127 | + // Assert happens when httpManager disposes and checks for unconsumed handlers |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | +} |
0 commit comments