|
| 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.Linq; |
| 7 | +using System.Security.Claims; |
| 8 | +using System.Text; |
| 9 | +using System.Threading.Tasks; |
| 10 | +using Microsoft.Extensions.DependencyInjection; |
| 11 | +using Microsoft.Identity.Abstractions; |
| 12 | +using Microsoft.Identity.Client; |
| 13 | +using Microsoft.Identity.Web.Test.Common.Mocks; |
| 14 | +using Microsoft.Identity.Web.TestOnly; |
| 15 | +using Microsoft.IdentityModel.Tokens; |
| 16 | +using Xunit; |
| 17 | + |
| 18 | +namespace Microsoft.Identity.Web.Test |
| 19 | +{ |
| 20 | + public class AuthorizationHeaderProviderTests |
| 21 | + { |
| 22 | + [Fact] |
| 23 | + public async Task LongRunningSessionForDefaultAuthProviderForUserDefaultKeyTest() |
| 24 | + { |
| 25 | + // Arrange |
| 26 | + var tokenAcquirerFactory = InitTokenAcquirerFactoryForTest(); |
| 27 | + IServiceProvider serviceProvider = tokenAcquirerFactory.Build(); |
| 28 | + |
| 29 | + IAuthorizationHeaderProvider authorizationHeaderProvider = |
| 30 | + serviceProvider.GetRequiredService<IAuthorizationHeaderProvider>(); |
| 31 | + var mockHttpClient = serviceProvider.GetRequiredService<IMsalHttpClientFactory>() as MockHttpClientFactory; |
| 32 | + |
| 33 | + using (mockHttpClient) |
| 34 | + { |
| 35 | + // Create a test ClaimsPrincipal |
| 36 | + var claims = new List<Claim> |
| 37 | + { |
| 38 | + new Claim(ClaimTypes.Name, "[email protected]") |
| 39 | + }; |
| 40 | + |
| 41 | + var identity = new CaseSensitiveClaimsIdentity(claims, "TestAuth"); |
| 42 | + identity.BootstrapContext = CreateTestJwt(); |
| 43 | + var claimsPrincipal = new ClaimsPrincipal(identity); |
| 44 | + |
| 45 | + // Create options with LongRunningWebApiSessionKey |
| 46 | + var options = new AuthorizationHeaderProviderOptions |
| 47 | + { |
| 48 | + AcquireTokenOptions = new AcquireTokenOptions |
| 49 | + { |
| 50 | + //When this is set to Auto, the first call will set the LongRunningWebApiSessionKey in the options to |
| 51 | + //the hash of the ClaimsPrincipal's access token. |
| 52 | + LongRunningWebApiSessionKey = TokenAcquisitionOptions.LongRunningWebApiSessionKeyAuto |
| 53 | + } |
| 54 | + }; |
| 55 | + |
| 56 | + // Act & Assert |
| 57 | + |
| 58 | + // Step 3: First call with ClaimsPrincipal to initiate LR session |
| 59 | + var scopes = new[] { "User.Read" }; |
| 60 | + mockHttpClient!.AddMockHandler(MockHttpCreator.CreateLrOboTokenHandler("User.Read")); |
| 61 | + var result = await authorizationHeaderProvider.CreateAuthorizationHeaderForUserAsync( |
| 62 | + scopes, |
| 63 | + options, |
| 64 | + claimsPrincipal); |
| 65 | + |
| 66 | + Assert.NotNull(result); |
| 67 | + Assert.NotEqual(options.AcquireTokenOptions.LongRunningWebApiSessionKey, TokenAcquisitionOptions.LongRunningWebApiSessionKeyAuto); |
| 68 | + string key1 = options.AcquireTokenOptions.LongRunningWebApiSessionKey; |
| 69 | + |
| 70 | + // Step 4: Second call without ClaimsPrincipal should return the token from cache |
| 71 | + result = await authorizationHeaderProvider.CreateAuthorizationHeaderForUserAsync( |
| 72 | + scopes, |
| 73 | + options); |
| 74 | + |
| 75 | + Assert.NotNull(result); |
| 76 | + Assert.NotEqual(options.AcquireTokenOptions.LongRunningWebApiSessionKey, TokenAcquisitionOptions.LongRunningWebApiSessionKeyAuto); |
| 77 | + Assert.Equal(key1, options.AcquireTokenOptions.LongRunningWebApiSessionKey); |
| 78 | + |
| 79 | + // Step 5: First call with ClaimsPrincipal to initiate LR session for CreateAuthorizationHeaderAsync |
| 80 | + scopes = new[] { "User.Write" }; |
| 81 | + mockHttpClient!.AddMockHandler(MockHttpCreator.CreateLrOboTokenHandler("User.Write")); |
| 82 | + result = await authorizationHeaderProvider.CreateAuthorizationHeaderAsync( |
| 83 | + scopes, |
| 84 | + options, |
| 85 | + claimsPrincipal); |
| 86 | + |
| 87 | + Assert.NotNull(result); |
| 88 | + Assert.NotEqual(options.AcquireTokenOptions.LongRunningWebApiSessionKey, TokenAcquisitionOptions.LongRunningWebApiSessionKeyAuto); |
| 89 | + key1 = options.AcquireTokenOptions.LongRunningWebApiSessionKey; |
| 90 | + |
| 91 | + // Step 6: Second call without ClaimsPrincipal should return the token from cache for CreateAuthorizationHeaderAsync |
| 92 | + result = await authorizationHeaderProvider.CreateAuthorizationHeaderAsync( |
| 93 | + scopes, |
| 94 | + options); |
| 95 | + |
| 96 | + Assert.NotNull(result); |
| 97 | + Assert.NotEqual(options.AcquireTokenOptions.LongRunningWebApiSessionKey, TokenAcquisitionOptions.LongRunningWebApiSessionKeyAuto); |
| 98 | + Assert.Equal(key1, options.AcquireTokenOptions.LongRunningWebApiSessionKey); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + [Fact] |
| 103 | + public async Task LongRunningSessionForDefaultAuthProviderForUserTest() |
| 104 | + { |
| 105 | + // Arrange |
| 106 | + var tokenAcquirerFactory = InitTokenAcquirerFactoryForTest(); |
| 107 | + IServiceProvider serviceProvider = tokenAcquirerFactory.Build(); |
| 108 | + |
| 109 | + IAuthorizationHeaderProvider authorizationHeaderProvider = |
| 110 | + serviceProvider.GetRequiredService<IAuthorizationHeaderProvider>(); |
| 111 | + var mockHttpClient = serviceProvider.GetRequiredService<IMsalHttpClientFactory>() as MockHttpClientFactory; |
| 112 | + |
| 113 | + using (mockHttpClient) |
| 114 | + { |
| 115 | + // Create a test ClaimsPrincipal |
| 116 | + var claims = new List<Claim> |
| 117 | + { |
| 118 | + new Claim(ClaimTypes.Name, "[email protected]") |
| 119 | + }; |
| 120 | + |
| 121 | + var identity = new CaseSensitiveClaimsIdentity(claims, "TestAuth"); |
| 122 | + identity.BootstrapContext = CreateTestJwt(); |
| 123 | + var claimsPrincipal = new ClaimsPrincipal(identity); |
| 124 | + |
| 125 | + // Create options with LongRunningWebApiSessionKey |
| 126 | + var options = new AuthorizationHeaderProviderOptions |
| 127 | + { |
| 128 | + AcquireTokenOptions = new AcquireTokenOptions |
| 129 | + { |
| 130 | + LongRunningWebApiSessionKey = "oboKey1" |
| 131 | + } |
| 132 | + }; |
| 133 | + |
| 134 | + // Act & Assert |
| 135 | + |
| 136 | + // Step 3: First call with ClaimsPrincipal to initiate LR session |
| 137 | + var scopes = new[] { "User.Read" }; |
| 138 | + mockHttpClient!.AddMockHandler(MockHttpCreator.CreateLrOboTokenHandler("User.Read")); |
| 139 | + var result = await authorizationHeaderProvider.CreateAuthorizationHeaderForUserAsync( |
| 140 | + scopes, |
| 141 | + options, |
| 142 | + claimsPrincipal); |
| 143 | + |
| 144 | + Assert.NotNull(result); |
| 145 | + Assert.Equal("oboKey1", options.AcquireTokenOptions.LongRunningWebApiSessionKey); |
| 146 | + |
| 147 | + // Step 4: Second call without ClaimsPrincipal should return the token from cache |
| 148 | + result = await authorizationHeaderProvider.CreateAuthorizationHeaderForUserAsync( |
| 149 | + scopes, |
| 150 | + options); |
| 151 | + |
| 152 | + Assert.NotNull(result); |
| 153 | + Assert.Equal("oboKey1", options.AcquireTokenOptions.LongRunningWebApiSessionKey); |
| 154 | + |
| 155 | + options = new AuthorizationHeaderProviderOptions |
| 156 | + { |
| 157 | + AcquireTokenOptions = new AcquireTokenOptions |
| 158 | + { |
| 159 | + LongRunningWebApiSessionKey = "oboKey2" |
| 160 | + } |
| 161 | + }; |
| 162 | + |
| 163 | + // Step 5: First call with ClaimsPrincipal to initiate LR session for CreateAuthorizationHeaderAsync |
| 164 | + scopes = new[] { "User.Write" }; |
| 165 | + mockHttpClient!.AddMockHandler(MockHttpCreator.CreateLrOboTokenHandler("User.Write")); |
| 166 | + result = await authorizationHeaderProvider.CreateAuthorizationHeaderAsync( |
| 167 | + scopes, |
| 168 | + options, |
| 169 | + claimsPrincipal); |
| 170 | + |
| 171 | + Assert.NotNull(result); |
| 172 | + Assert.Equal("oboKey2", options.AcquireTokenOptions.LongRunningWebApiSessionKey); |
| 173 | + |
| 174 | + // Step 6: Second call without ClaimsPrincipal should return the token from cache for CreateAuthorizationHeaderAsync |
| 175 | + result = await authorizationHeaderProvider.CreateAuthorizationHeaderAsync( |
| 176 | + scopes, |
| 177 | + options); |
| 178 | + |
| 179 | + Assert.NotNull(result); |
| 180 | + Assert.Equal("oboKey2", options.AcquireTokenOptions.LongRunningWebApiSessionKey); |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + private static string CreateTestJwt() |
| 185 | + { |
| 186 | + var header = new Dictionary<string, object> |
| 187 | + { |
| 188 | + { "alg", "HS256" }, |
| 189 | + { "typ", "JWT" } |
| 190 | + }; |
| 191 | + |
| 192 | + var payload = new Dictionary<string, object> |
| 193 | + { |
| 194 | + { "iss", "https://login.microsoftonline.com/test-tenant-id/v2.0" } |
| 195 | + }; |
| 196 | + |
| 197 | + string headerJson = System.Text.Json.JsonSerializer.Serialize(header); |
| 198 | + string payloadJson = System.Text.Json.JsonSerializer.Serialize(payload); |
| 199 | + |
| 200 | + string headerBase64 = Base64UrlEncoder.Encode(headerJson); |
| 201 | + string payloadBase64 = Base64UrlEncoder.Encode(payloadJson); |
| 202 | + |
| 203 | + // For testing purposes, we're using a fixed signature |
| 204 | + const string signature = "test_signature"; |
| 205 | + string signatureBase64 = Base64UrlEncoder.Encode(signature); |
| 206 | + |
| 207 | + return $"{headerBase64}.{payloadBase64}.{signatureBase64}"; |
| 208 | + } |
| 209 | + |
| 210 | + private TokenAcquirerFactory InitTokenAcquirerFactoryForTest() |
| 211 | + { |
| 212 | + TokenAcquirerFactoryTesting.ResetTokenAcquirerFactoryInTest(); |
| 213 | + TokenAcquirerFactory tokenAcquirerFactory = TokenAcquirerFactory.GetDefaultInstance(); |
| 214 | + tokenAcquirerFactory.Services.Configure<MicrosoftIdentityApplicationOptions>(options => |
| 215 | + { |
| 216 | + options.Instance = "https://login.microsoftonline.com/"; |
| 217 | + options.TenantId = "testTenantId"; |
| 218 | + options.ClientId = "testClientId"; |
| 219 | + options.ClientCredentials = [ new CredentialDescription() { |
| 220 | + SourceType = CredentialSource.ClientSecret, |
| 221 | + ClientSecret = "test-secret" |
| 222 | + }]; |
| 223 | + }); |
| 224 | + |
| 225 | + // Add required services |
| 226 | + tokenAcquirerFactory.Services.AddSingleton<IMsalHttpClientFactory, MockHttpClientFactory>(); |
| 227 | + tokenAcquirerFactory.Services.AddScoped<IAuthorizationHeaderProvider, DefaultAuthorizationHeaderProvider>(); |
| 228 | + |
| 229 | + return tokenAcquirerFactory; |
| 230 | + } |
| 231 | + } |
| 232 | +} |
0 commit comments