|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +using System.Runtime.InteropServices; |
| 6 | +using Microsoft.Identity.Client; |
| 7 | + |
| 8 | +namespace Microsoft.Data.SqlClient.Extensions.Azure.Test; |
| 9 | + |
| 10 | +/// <summary> |
| 11 | +/// Verifies missing-window diagnostics without opening UI or contacting Entra ID. |
| 12 | +/// </summary> |
| 13 | +[Collection(SqlAuthenticationProviderCollection.Name)] |
| 14 | +public class WamBrokerErrorTests |
| 15 | +{ |
| 16 | + /// <summary> |
| 17 | + /// Both first-party and custom applications using WAM receive actionable guidance on Windows. |
| 18 | + /// Other platforms retain the original MSAL diagnostic. |
| 19 | + /// </summary> |
| 20 | + [Theory] |
| 21 | + [InlineData(null)] |
| 22 | + [InlineData("11111111-2222-3333-4444-555555555555")] |
| 23 | + public async Task AcquireTokenAsync_MissingWamWindow_ExplainsSupportedAlternatives(string? applicationClientId) |
| 24 | + { |
| 25 | + var provider = new ActiveDirectoryAuthenticationProvider( |
| 26 | + new ActiveDirectoryAuthenticationProviderOptions |
| 27 | + { |
| 28 | + ApplicationClientId = applicationClientId, |
| 29 | + UseWamBroker = true, |
| 30 | + }); |
| 31 | + provider.SetParentActivityOrWindowFunc( |
| 32 | + () => throw new InvalidOperationException("Window discovery must not precede MSAL token acquisition.")); |
| 33 | + var cause = new MsalClientException("window_handle_required", "Original MSAL diagnostic."); |
| 34 | + |
| 35 | + AuthenticationException exception = await AcquireWithMsalExceptionAsync( |
| 36 | + provider, SqlAuthenticationMethod.ActiveDirectoryInteractive, cause); |
| 37 | + |
| 38 | + Assert.Equal(SqlAuthenticationMethod.ActiveDirectoryInteractive, exception.Method); |
| 39 | + Assert.Equal(cause.ErrorCode, exception.FailureCode); |
| 40 | + Assert.Same(cause, exception.InnerException); |
| 41 | + Assert.False(exception.ShouldRetry); |
| 42 | + Assert.Equal(0, exception.RetryPeriod); |
| 43 | + Assert.Contains(cause.Message, exception.Message); |
| 44 | + |
| 45 | + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) |
| 46 | + { |
| 47 | + Assert.Contains("parent window handle", exception.Message); |
| 48 | + Assert.Contains("SetParentActivityOrWindowFunc", exception.Message); |
| 49 | + Assert.Contains("Active Directory Default", exception.Message); |
| 50 | + Assert.Contains("ApplicationClientId", exception.Message); |
| 51 | + Assert.Contains("UseWamBroker", exception.Message); |
| 52 | + Assert.Contains("first-party", exception.Message); |
| 53 | + } |
| 54 | + else |
| 55 | + { |
| 56 | + Assert.Equal($"Failed to acquire access token for {exception.Method}: {cause.Message}", exception.Message); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + /// <summary> |
| 61 | + /// Non-broker flows, other authentication methods, and unrelated MSAL failures are not |
| 62 | + /// relabeled as missing WAM windows. |
| 63 | + /// </summary> |
| 64 | + [Theory] |
| 65 | + [InlineData(SqlAuthenticationMethod.ActiveDirectoryInteractive, false, "window_handle_required")] |
| 66 | + [InlineData(SqlAuthenticationMethod.ActiveDirectoryDeviceCodeFlow, true, "window_handle_required")] |
| 67 | + [InlineData(SqlAuthenticationMethod.ActiveDirectoryIntegrated, true, "window_handle_required")] |
| 68 | + [InlineData(SqlAuthenticationMethod.ActiveDirectoryInteractive, true, "authentication_canceled")] |
| 69 | + public async Task AcquireTokenAsync_OtherMsalFailures_PreservesDiagnostic( |
| 70 | + SqlAuthenticationMethod method, bool useWamBroker, string errorCode) |
| 71 | + { |
| 72 | + var provider = new ActiveDirectoryAuthenticationProvider( |
| 73 | + new ActiveDirectoryAuthenticationProviderOptions |
| 74 | + { |
| 75 | + ApplicationClientId = "11111111-2222-3333-4444-555555555555", |
| 76 | + UseWamBroker = useWamBroker, |
| 77 | + }); |
| 78 | + var cause = new MsalClientException(errorCode, "Original MSAL diagnostic."); |
| 79 | + |
| 80 | + AuthenticationException exception = await AcquireWithMsalExceptionAsync(provider, method, cause); |
| 81 | + |
| 82 | + Assert.Equal(method, exception.Method); |
| 83 | + Assert.Equal(errorCode, exception.FailureCode); |
| 84 | + Assert.Same(cause, exception.InnerException); |
| 85 | + Assert.False(exception.ShouldRetry); |
| 86 | + Assert.Equal($"Failed to acquire access token for {method}: {cause.Message}", exception.Message); |
| 87 | + } |
| 88 | + |
| 89 | + /// <summary> |
| 90 | + /// Installs an isolated MSAL client whose cache callback throws the supplied exception, |
| 91 | + /// exercising the provider's real exception translation without a network request. |
| 92 | + /// The cache entry is removed even if an assertion fails. |
| 93 | + /// </summary> |
| 94 | + /// <param name="provider">The provider configuration under test.</param> |
| 95 | + /// <param name="method">The authentication method under test.</param> |
| 96 | + /// <param name="cause">The MSAL failure to propagate through token acquisition.</param> |
| 97 | + /// <returns>The translated authentication failure.</returns> |
| 98 | + private static async Task<AuthenticationException> AcquireWithMsalExceptionAsync( |
| 99 | + ActiveDirectoryAuthenticationProvider provider, SqlAuthenticationMethod method, MsalClientException cause) |
| 100 | + { |
| 101 | + string authority = $"https://login.microsoftonline.com/{Guid.NewGuid()}"; |
| 102 | + string redirectUri = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) |
| 103 | + ? provider.UseWamBroker |
| 104 | + ? $"ms-appx-web://microsoft.aad.brokerplugin/{provider.ApplicationClientId}" |
| 105 | +#if NETFRAMEWORK |
| 106 | + : "https://login.microsoftonline.com/common/oauth2/nativeclient" |
| 107 | +#else |
| 108 | + : "http://localhost" |
| 109 | +#endif |
| 110 | + : "http://localhost"; |
| 111 | + |
| 112 | + var key = new ActiveDirectoryAuthenticationProvider.PublicClientAppKey( |
| 113 | + authority, redirectUri, provider.ApplicationClientId |
| 114 | +#if NETFRAMEWORK |
| 115 | + , null |
| 116 | +#endif |
| 117 | + ); |
| 118 | + IPublicClientApplication app = PublicClientApplicationBuilder.Create(provider.ApplicationClientId) |
| 119 | + .WithAuthority(authority) |
| 120 | + .WithRedirectUri(redirectUri) |
| 121 | + .Build(); |
| 122 | + app.UserTokenCache.SetBeforeAccess(_ => throw cause); |
| 123 | + |
| 124 | + var cache = ActiveDirectoryAuthenticationProvider.s_pcaMap; |
| 125 | + Assert.True(cache.TryAdd(key, app)); |
| 126 | + try |
| 127 | + { |
| 128 | + var parameters = new SqlAuthenticationParameters( |
| 129 | + method, "test-server", "test-database", "https://database.windows.net/", |
| 130 | + authority, userId: null, password: null, connectionId: Guid.NewGuid(), connectionTimeout: 30); |
| 131 | + return await Assert.ThrowsAsync<AuthenticationException>(() => provider.AcquireTokenAsync(parameters)); |
| 132 | + } |
| 133 | + finally |
| 134 | + { |
| 135 | + cache.TryRemove(key, out _); |
| 136 | + } |
| 137 | + } |
| 138 | +} |
0 commit comments