-
Notifications
You must be signed in to change notification settings - Fork 403
Expand file tree
/
Copy pathExtraBodyParametersTests.cs
More file actions
297 lines (254 loc) · 15.6 KB
/
ExtraBodyParametersTests.cs
File metadata and controls
297 lines (254 loc) · 15.6 KB
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Identity.Client;
using Microsoft.Identity.Test.Common.Core.Mocks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.Identity.Test.Common.Core.Helpers;
using Microsoft.Identity.Client.Extensibility;
#pragma warning disable CS0618 // Tests intentionally exercise the obsolete AcquireTokenForClientBuilderExtensions.WithExtraBodyParameters overload.
namespace Microsoft.Identity.Test.Unit.PublicApiTests
{
[TestClass]
public class ExtraBodyParametersTests : TestBase
{
private string _clientId = "4df2cbbb-8612-49c1-87c8-f334d6d065ad";
private string _scope = "api://AzureFMITokenExchange/.default";
private string _tenantId = "tenantid";
private string _expectedParameterHash = "";
string _expectedExternalCacheKey => $"{_clientId}_{_tenantId}_{_expectedParameterHash}_AppTokenCache";
[TestMethod]
public async Task ValidateExtraBodyParameters()
{
using (var httpManager = new MockHttpManager())
{
//Arrange
var extraBodyParams = new Dictionary<string, Func<CancellationToken, Task<string>>>
{
{ "attributetoken", (CancellationToken ct) => GetComputedValue() },
{ "attributetoken2", (CancellationToken ct) => GetComputedValue() }
};
var extraBodyParams2 = new Dictionary<string, Func<CancellationToken, Task<string>>>
{
{ "attributetoken", (CancellationToken ct) => GetComputedValue() },
{ "attributetoken2", (CancellationToken ct) => GetComputedValue() },
{ "attributetoken3", (CancellationToken ct) => GetComputedValue() }
};
//Act
//Create application
var confidentialApp = ConfidentialClientApplicationBuilder
.Create(_clientId)
.WithAuthority("https://login.microsoftonline.com/", _tenantId)
.WithClientSecret("ClientSecret")
.WithHttpManager(httpManager)
.WithExperimentalFeatures(true) // Enable experimental features to use WithExtraBodyParameters
.BuildConcrete();
//Recording test data for Asserts
_expectedParameterHash = "8cY9AFTXo3uSqueI1A_HPiX0j66dJXB-3c3BTDcJVxE";
var appCacheAccess = confidentialApp.AppTokenCache.RecordAccess(
(args) =>
{
Assert.AreEqual(_expectedExternalCacheKey, args.SuggestedCacheKey); }
);
//Acquire AuthN
httpManager.AddInstanceDiscoveryMockHandler();
httpManager.AddMockHandlerSuccessfulClientCredentialTokenResponseMessage(
expectedPostData: new Dictionary<string, string>
{
{ "attributetoken", "AttributeToken" },
{ "attributetoken2", "AttributeToken" }
});
var authResult = await confidentialApp.AcquireTokenForClient(new[] { _scope })
.WithFmiPath("SomeFmiPath/FmiCredentialPath") //Sets fmi path in client credential request.
.WithExtraBodyParameters(extraBodyParams) //Sets attributes in client credential request.
.ExecuteAsync()
.ConfigureAwait(false);
//Assert
Assert.IsNotNull(authResult);
Assert.AreEqual(TokenSource.IdentityProvider, authResult.AuthenticationResultMetadata.TokenSource);
//Ensure the extra body parameters are present in the cache key
authResult = await confidentialApp.AcquireTokenForClient(new[] { _scope })
.WithFmiPath("SomeFmiPath/FmiCredentialPath") //Sets fmi path in client credential request.
.WithExtraBodyParameters(extraBodyParams) //Sets attributes in client credential request.
.ExecuteAsync()
.ConfigureAwait(false);
//Assert
Assert.IsNotNull(authResult);
Assert.AreEqual(TokenSource.Cache, authResult.AuthenticationResultMetadata.TokenSource);
//Ensure the same extra body parameters are needed get the cache key
httpManager.AddMockHandlerSuccessfulClientCredentialTokenResponseMessage(
expectedPostData: new Dictionary<string, string>
{
{ "attributetoken", "AttributeToken" },
{ "attributetoken2", "AttributeToken" },
{ "attributetoken3", "AttributeToken" }
});
_expectedParameterHash = "aPnz3SdIoSMmI5yKcFs9h2vMKdZB_vahvt61jBrsCIE";
authResult = await confidentialApp.AcquireTokenForClient(new[] { _scope })
.WithFmiPath("SomeFmiPath/FmiCredentialPath") //Sets fmi path in client credential request.
.WithExtraBodyParameters(extraBodyParams2) //Sets attributes in client credential request.
.ExecuteAsync()
.ConfigureAwait(false);
//Assert
Assert.IsNotNull(authResult);
Assert.AreEqual(TokenSource.IdentityProvider, authResult.AuthenticationResultMetadata.TokenSource);
//Ensure the first token can still be retrieved from the cache
_expectedParameterHash = "8cY9AFTXo3uSqueI1A_HPiX0j66dJXB-3c3BTDcJVxE";
authResult = await confidentialApp.AcquireTokenForClient(new[] { _scope })
.WithFmiPath("SomeFmiPath/FmiCredentialPath") //Sets fmi path in client credential request.
.WithExtraBodyParameters(extraBodyParams) //Sets attributes in client credential request.
.ExecuteAsync()
.ConfigureAwait(false);
//Assert
Assert.IsNotNull(authResult);
Assert.AreEqual(TokenSource.Cache, authResult.AuthenticationResultMetadata.TokenSource);
}
}
[TestMethod]
public async Task ValidateExtraBodyParametersAreCombined()
{
using (var httpManager = new MockHttpManager())
{
//Arrange
var clientId = "4df2cbbb-8612-49c1-87c8-f334d6d065ad";
var scope = "api://AzureFMITokenExchange/.default";
var tenantId = "tenantid";
//Act
//Create application
var confidentialApp = ConfidentialClientApplicationBuilder
.Create(clientId)
.WithAuthority("https://login.microsoftonline.com/", tenantId)
.WithClientSecret("ClientSecret")
.WithHttpManager(httpManager)
.WithExperimentalFeatures(true) // Enable experimental features to use WithExtraBodyParameters
.BuildConcrete();
//Recording test data for Asserts
_expectedParameterHash = "zl6sDTLdSw06EytxoYRBItblGzbgi4qzQ8gvIyRywxc";
var appCacheAccess = confidentialApp.AppTokenCache.RecordAccess(
(args) =>
{
Assert.AreEqual(_expectedExternalCacheKey, args.SuggestedCacheKey);
}
);
//Acquire AuthN
httpManager.AddInstanceDiscoveryMockHandler();
httpManager.AddMockHandlerSuccessfulClientCredentialTokenResponseMessage(
expectedPostData: new Dictionary<string, string>
{
{ "attributetoken1", "AttributeToken" },
{ "attributetoken2", "AttributeToken" },
{ "attributetoken3", "AttributeToken" },
{ "attributetoken4", "AttributeToken" },
{ "attributetoken5", "AttributeToken" }
});
var extraBodyParams = new Dictionary<string, Func<CancellationToken, Task<string>>>
{
{ "attributetoken1", (CancellationToken ct) => GetComputedValue() },
{ "attributetoken2", (CancellationToken ct) => GetComputedValue() }
};
var extraBodyParams2 = new Dictionary<string, Func<CancellationToken, Task<string>>>
{
{ "attributetoken3", (CancellationToken ct) => GetComputedValue() },
{ "attributetoken4", (CancellationToken ct) => GetComputedValue() },
{ "attributetoken5", (CancellationToken ct) => GetComputedValue() }
};
var authResult = await confidentialApp.AcquireTokenForClient(new[] { scope })
.WithFmiPath("SomeFmiPath/FmiCredentialPath") //Sets fmi path in client credential request.
.WithExtraBodyParameters(extraBodyParams) //Sets attributes in client credential request.
.WithExtraBodyParameters(extraBodyParams2)
.ExecuteAsync()
.ConfigureAwait(false);
//Assert
Assert.IsNotNull(authResult);
Assert.AreEqual(TokenSource.IdentityProvider, authResult.AuthenticationResultMetadata.TokenSource);
//Ensure the extra body parameters are present in the cache key
authResult = await confidentialApp.AcquireTokenForClient(new[] { scope })
.WithFmiPath("SomeFmiPath/FmiCredentialPath") //Sets fmi path in client credential request.
.WithExtraBodyParameters(extraBodyParams) //Sets attributes in client credential request.
.WithExtraBodyParameters(extraBodyParams2)
.ExecuteAsync()
.ConfigureAwait(false);
//Assert
Assert.IsNotNull(authResult);
Assert.AreEqual(TokenSource.Cache, authResult.AuthenticationResultMetadata.TokenSource);
//Ensure the same extra body parameters are needed get the cache key
httpManager.AddMockHandlerSuccessfulClientCredentialTokenResponseMessage(
expectedPostData: new Dictionary<string, string>
{
{ "attributetoken3", "AttributeToken" },
{ "attributetoken4", "AttributeToken" },
{ "attributetoken5", "AttributeToken" }
});
_expectedParameterHash = "y6I4j3oaWfbZglcRJJsyBj7ROXrfqSMdYoglx8Fdp4A";
authResult = await confidentialApp.AcquireTokenForClient(new[] { scope })
.WithFmiPath("SomeFmiPath/FmiCredentialPath") //Sets fmi path in client credential request.
.WithExtraBodyParameters(extraBodyParams2) //Sets attributes in client credential request.
.ExecuteAsync()
.ConfigureAwait(false);
//Assert
Assert.IsNotNull(authResult);
Assert.AreEqual(TokenSource.IdentityProvider, authResult.AuthenticationResultMetadata.TokenSource);
httpManager.AddMockHandlerSuccessfulClientCredentialTokenResponseMessage(
expectedPostData: new Dictionary<string, string>
{
{ "attributetoken1", "AttributeToken" },
{ "attributetoken2", "AttributeToken" }
});
_expectedParameterHash = "9VQZ-ObNKwbMeTs51ehDhjCE2mB4n5N_KoAy85Sr3yQ";
authResult = await confidentialApp.AcquireTokenForClient(new[] { scope })
.WithFmiPath("SomeFmiPath/FmiCredentialPath") //Sets fmi path in client credential request.
.WithExtraBodyParameters(extraBodyParams) //Sets attributes in client credential request.
.ExecuteAsync()
.ConfigureAwait(false);
//Assert
Assert.IsNotNull(authResult);
Assert.AreEqual(TokenSource.IdentityProvider, authResult.AuthenticationResultMetadata.TokenSource);
}
}
[TestMethod]
public async Task WithExtraBodyParameters_NullInput_ReturnToken()
{
using (var httpManager = new MockHttpManager())
{
// Arrange
httpManager.AddInstanceDiscoveryMockHandler();
httpManager.AddMockHandlerSuccessfulClientCredentialTokenResponseMessage();
var confidentialApp = ConfidentialClientApplicationBuilder
.Create(_clientId)
.WithAuthority("https://login.microsoftonline.com/", _tenantId)
.WithClientSecret("ClientSecret")
.WithHttpManager(httpManager)
.WithExperimentalFeatures(true) // Enable experimental features to use WithExtraBodyParameters
.BuildConcrete();
// Act & Assert
// Ensure that the token is returned even when no extra body parameters are provided
var authResult = await confidentialApp.AcquireTokenForClient(new[] { _scope })
.WithExtraBodyParameters(null)
.ExecuteAsync()
.ConfigureAwait(false);
Assert.IsNotNull(authResult);
Assert.AreEqual(TokenSource.IdentityProvider, authResult.AuthenticationResultMetadata.TokenSource);
authResult = await confidentialApp.AcquireTokenForClient(new[] { _scope })
.WithExtraBodyParameters(null)
.ExecuteAsync()
.ConfigureAwait(false);
Assert.IsNotNull(authResult);
Assert.AreEqual(TokenSource.Cache, authResult.AuthenticationResultMetadata.TokenSource);
//Ensure that the token can still be retrieved from the cache when the input is an empty dictionary
authResult = await confidentialApp.AcquireTokenForClient(new[] { _scope })
.WithExtraBodyParameters(new Dictionary<string, Func<CancellationToken, Task<string>>>())
.ExecuteAsync()
.ConfigureAwait(false);
Assert.IsNotNull(authResult);
Assert.AreEqual(TokenSource.Cache, authResult.AuthenticationResultMetadata.TokenSource);
}
}
private Task<string> GetComputedValue()
{
return Task.FromResult("AttributeToken");
}
}
}