-
Notifications
You must be signed in to change notification settings - Fork 343
/
AuthorityTests.cs
280 lines (241 loc) · 13.2 KB
/
AuthorityTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Threading.Tasks;
using Microsoft.Identity.Client;
using Microsoft.Identity.Client.Instance;
using Microsoft.Identity.Client.Internal;
using Microsoft.Identity.Client.Utils;
using Microsoft.Identity.Test.Common;
using Microsoft.Identity.Test.Common.Core.Helpers;
using Microsoft.Identity.Test.Common.Core.Mocks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NSubstitute.ReceivedExtensions;
namespace Microsoft.Identity.Test.Unit.ApiConfigTests
{
[TestClass]
public class AuthorityTests : TestBase
{
private static readonly Authority s_commonAuthority = Authority.CreateAuthority(TestConstants.AuthorityCommonTenant, true);
private static readonly string s_ppeCommonUri = $@"https://{TestConstants.PpeEnvironment}/{TestConstants.TenantId}";
private static readonly Authority s_ppeAuthority = Authority.CreateAuthority(s_ppeCommonUri, true);
private static readonly string s_ppeOrgCommonUri = $@"https://{TestConstants.PpeOrgEnvironment}/{TestConstants.TenantId}";
private static readonly Authority s_ppeOrgAuthority = Authority.CreateAuthority(s_ppeOrgCommonUri, true);
private static readonly Authority s_utidAuthority = Authority.CreateAuthority(TestConstants.AuthorityUtidTenant, true);
private static readonly Authority s_utid2Authority = Authority.CreateAuthority(TestConstants.AuthorityUtid2Tenant, true);
private static readonly Authority s_b2cAuthority = Authority.CreateAuthority(TestConstants.B2CAuthority, true);
private static readonly Authority s_commonNetAuthority = Authority.CreateAuthority(TestConstants.PrefCacheAuthorityCommonTenant, true);
private MockHttpAndServiceBundle _harness;
private RequestContext _testRequestContext;
[TestInitialize]
public override void TestInitialize()
{
TestCommon.ResetInternalStaticCaches();
base.TestInitialize();
_harness = base.CreateTestHarness();
_testRequestContext = new RequestContext(
_harness.ServiceBundle,
Guid.NewGuid());
}
[TestCleanup]
public override void TestCleanup()
{
_harness.Dispose();
base.TestCleanup();
}
[TestMethod]
public void WithTenantIdExceptions()
{
var app1 = ConfidentialClientApplicationBuilder
.Create(TestConstants.ClientId)
.WithAdfsAuthority(TestConstants.ADFSAuthority)
.WithClientSecret("secret")
.Build();
var ex1 = AssertException.Throws<MsalClientException>(() =>
app1
.AcquireTokenByAuthorizationCode(TestConstants.s_scope, "code")
.WithTenantId(TestConstants.TenantId));
var app2 = ConfidentialClientApplicationBuilder
.Create(TestConstants.ClientId)
.WithB2CAuthority(TestConstants.B2CAuthority)
.WithClientSecret("secret")
.Build();
var ex2 = AssertException.Throws<MsalClientException>(() =>
app2
.AcquireTokenByAuthorizationCode(TestConstants.s_scope, "code")
.WithTenantId(TestConstants.TenantId));
Assert.AreEqual(ex1.ErrorCode, MsalError.TenantOverrideNonAad);
Assert.AreEqual(ex2.ErrorCode, MsalError.TenantOverrideNonAad);
}
[DataTestMethod]
[DynamicData(nameof(TestData.GetAuthorityWithExpectedTenantId), typeof(TestData), DynamicDataSourceType.Method)]
public void WithTenantId_Success(Uri authorityValue, string tenantId)
{
// Ignore authorityValue, it's just that we don't need to create another TestData method
var app = ConfidentialClientApplicationBuilder
.Create(TestConstants.ClientId)
.WithAuthority(TestConstants.AuthorityCommonTenant)
.WithClientSecret("secret")
.Build();
var parameterBuilder = app
.AcquireTokenByAuthorizationCode(TestConstants.s_scope, "code")
.WithTenantId(tenantId);
// Verify Host still matches the original Authority
Assert.AreEqual(new Uri(TestConstants.AuthorityCommonTenant).Host, parameterBuilder.CommonParameters.AuthorityOverride.Host);
// Verify the Tenant Id matches
Assert.AreEqual(tenantId, AuthorityHelpers.GetTenantId(parameterBuilder.CommonParameters.AuthorityOverride.CanonicalAuthority));
}
[DataTestMethod]
[DynamicData(nameof(TestData.GetAuthorityWithExpectedTenantId), typeof(TestData), DynamicDataSourceType.Method)]
public void WithTenantIdFromAuthority_Success(Uri authorityValue, string expectedTenantId)
{
var app = ConfidentialClientApplicationBuilder
.Create(TestConstants.ClientId)
.WithAuthority(TestConstants.AuthorityCommonTenant)
.WithClientSecret("secret")
.Build();
var parameterBuilder = app
.AcquireTokenByAuthorizationCode(TestConstants.s_scope, "code")
.WithTenantIdFromAuthority(authorityValue);
// Verify Host still matches the original Authority
Assert.AreEqual(new Uri(TestConstants.AuthorityCommonTenant).Host, parameterBuilder.CommonParameters.AuthorityOverride.Host);
// Verify the Tenant Id matches
Assert.AreEqual(expectedTenantId, AuthorityHelpers.GetTenantId(parameterBuilder.CommonParameters.AuthorityOverride.CanonicalAuthority));
}
[DataTestMethod]
[DataRow(null)]
public void WithTenantIdFromAuthority_NullUriAuthority_Failure(Uri authorityValue)
{
var app = ConfidentialClientApplicationBuilder
.Create(TestConstants.ClientId)
.WithAuthority(TestConstants.AuthorityCommonTenant)
.WithClientSecret("secret")
.Build();
AssertException.Throws<ArgumentNullException>(() =>
app
.AcquireTokenByAuthorizationCode(TestConstants.s_scope, "code")
.WithTenantIdFromAuthority(authorityValue));
}
[TestMethod]
public void VerifyAuthorityTest()
{
VerifyAuthority(
configAuthority: s_commonAuthority,
requestAuthority: null,
account: null,
expectedTenantId: "common",
_testRequestContext);
VerifyAuthority(
configAuthority: s_commonAuthority,
requestAuthority: s_commonAuthority,
account: null,
expectedTenantId: "common",
_testRequestContext);
VerifyAuthority(
configAuthority: s_commonAuthority,
requestAuthority: s_commonAuthority,
account: new Account(TestConstants.s_userIdentifier, "username", s_commonAuthority.AuthorityInfo.Host),
expectedTenantId: TestConstants.Utid,
_testRequestContext);
VerifyAuthority(
configAuthority: s_commonAuthority,
requestAuthority: s_utidAuthority,
account: null,
expectedTenantId: TestConstants.Utid,
_testRequestContext);
VerifyAuthority(
configAuthority: s_commonAuthority,
requestAuthority: s_utid2Authority,
account: new Account(TestConstants.s_userIdentifier, "username", s_utid2Authority.AuthorityInfo.Host),
expectedTenantId: TestConstants.Utid2,
_testRequestContext);
VerifyAuthority(
configAuthority: s_commonAuthority,
requestAuthority: null,
account: PublicClientApplication.OperatingSystemAccount,
expectedTenantId: "common",
_testRequestContext,
multiCloudSupport: true);
}
[TestMethod]
public async Task AuthorityMismatchTestAsync()
{
_testRequestContext.ServiceBundle.Config.Authority = s_utidAuthority;
var ex = await AssertException.TaskThrowsAsync<MsalClientException>(
() => Authority.CreateAuthorityForRequestAsync(_testRequestContext, s_b2cAuthority.AuthorityInfo, null))
.ConfigureAwait(false);
Assert.AreEqual(MsalError.AuthorityTypeMismatch, ex.ErrorCode);
}
[TestMethod]
public async Task DefaultAuthorityDifferentTypeTestAsync()
{
_testRequestContext.ServiceBundle.Config.Authority = s_commonAuthority;
var ex = await Assert.ThrowsExceptionAsync<MsalClientException>(
() => Authority.CreateAuthorityForRequestAsync(_testRequestContext, s_b2cAuthority.AuthorityInfo, null)).ConfigureAwait(false);
Assert.AreEqual(MsalError.B2CAuthorityHostMismatch, ex.ErrorCode);
}
[TestMethod]
public async Task DifferentHostsAsync()
{
_harness.HttpManager.AddInstanceDiscoveryMockHandler();
_testRequestContext.ServiceBundle.Config.HttpManager = _harness.HttpManager;
_testRequestContext.ServiceBundle.Config.Authority = s_commonAuthority;
var ex = await Assert.ThrowsExceptionAsync<MsalClientException>(
() => Authority.CreateAuthorityForRequestAsync(_testRequestContext, s_ppeOrgAuthority.AuthorityInfo, null)).ConfigureAwait(false);
Assert.AreEqual(MsalError.AuthorityHostMismatch, ex.ErrorCode);
_harness.HttpManager.AddInstanceDiscoveryMockHandler();
_testRequestContext.ServiceBundle.Config.Authority = s_ppeOrgAuthority;
var ex2 = await Assert.ThrowsExceptionAsync<MsalClientException>(
() => Authority.CreateAuthorityForRequestAsync(_testRequestContext, s_commonAuthority.AuthorityInfo, null)).ConfigureAwait(false);
Assert.AreEqual(MsalError.AuthorityHostMismatch, ex2.ErrorCode);
_testRequestContext.ServiceBundle.Config.Authority = Authority.CreateAuthority(TestConstants.ADFSAuthority, true);
var ex3 = await Assert.ThrowsExceptionAsync<MsalClientException>(
() => Authority.CreateAuthorityForRequestAsync(
_testRequestContext,
AuthorityInfo.FromAdfsAuthority(TestConstants.ADFSAuthority2, true),
null)).ConfigureAwait(false);
Assert.AreEqual(MsalError.AuthorityHostMismatch, ex3.ErrorCode);
_testRequestContext.ServiceBundle.Config.Authority = Authority.CreateAuthority(TestConstants.B2CAuthority, true);
var ex4 = await Assert.ThrowsExceptionAsync<MsalClientException>(
() => Authority.CreateAuthorityForRequestAsync(
_testRequestContext,
AuthorityInfo.FromAuthorityUri(TestConstants.B2CCustomDomain, true),
null)).ConfigureAwait(false);
Assert.AreEqual(MsalError.B2CAuthorityHostMismatch, ex4.ErrorCode);
}
[TestMethod]
public async Task DifferentHostsWithAliasedAuthorityAsync()
{
//Checking for aliased authority. Should not throw exception when a developer configures an authority on the application
//but uses a different authority that is a known alias of the previously configured one.
//See https://github.com/AzureAD/microsoft-authentication-library-for-dotnet/issues/2736
_harness.HttpManager.AddInstanceDiscoveryMockHandler(TestConstants.PrefCacheAuthorityCommonTenant);
_testRequestContext.ServiceBundle.Config.HttpManager = _harness.HttpManager;
_testRequestContext.ServiceBundle.Config.Authority = s_commonNetAuthority;
var authority = await Authority.CreateAuthorityForRequestAsync(_testRequestContext, s_commonAuthority.AuthorityInfo).ConfigureAwait(false);
Assert.AreEqual(s_commonNetAuthority.AuthorityInfo.CanonicalAuthority, authority.AuthorityInfo.CanonicalAuthority);
}
[TestMethod]
public void IsDefaultAuthorityTest()
{
Assert.IsTrue(
Authority.CreateAuthority(ClientApplicationBase.DefaultAuthority)
.AuthorityInfo.IsDefaultAuthority);
Assert.IsFalse(s_utidAuthority.AuthorityInfo.IsDefaultAuthority);
Assert.IsFalse(s_b2cAuthority.AuthorityInfo.IsDefaultAuthority);
}
private static void VerifyAuthority(
Authority configAuthority,
Authority requestAuthority,
IAccount account,
string expectedTenantId,
RequestContext requestContext,
bool multiCloudSupport = false)
{
requestContext.ServiceBundle.Config.Authority = configAuthority;
requestContext.ServiceBundle.Config.MultiCloudSupportEnabled = multiCloudSupport;
var resultAuthority = Authority.CreateAuthorityForRequestAsync(requestContext, requestAuthority?.AuthorityInfo, account).Result;
Assert.AreEqual(expectedTenantId, resultAuthority.TenantId);
}
}
}