Skip to content

Commit 2ce22ff

Browse files
User/mstannah/iwa update (#151)
* IWA refactor * logging changes * make other test names consistent * Update changelog Co-authored-by: Kyle Rader <kyrader@microsoft.com>
1 parent 733b867 commit 2ce22ff

File tree

3 files changed

+118
-148
lines changed

3 files changed

+118
-148
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77
## [Unreleased]
88
### Fixed
99
- Option `--resource` is not needed if option `--scope` is provided.
10+
- Refactoring IWA AuthFlow to call GetTokenIWA when we have a MsalUiRequiredException
1011

1112
## [0.5.1] - 2022-09-08
1213
### Fixed

src/MSALWrapper.Test/AuthFlow/IntegratedWindowsAuthenticationTest.cs

Lines changed: 69 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,10 @@ public void Setup()
7979
public AuthFlow.IntegratedWindowsAuthentication Subject() => this.serviceProvider.GetService<AuthFlow.IntegratedWindowsAuthentication>();
8080

8181
[Test]
82-
public async Task IntegratedWindowsAuthFlow_HappyPath()
82+
public async Task CachedAuthSuccess()
8383
{
84-
this.SilentAuthResult();
85-
8684
this.MockAccount();
85+
this.CachedAuthResult();
8786

8887
// Act
8988
AuthFlow.IntegratedWindowsAuthentication iwa = this.Subject();
@@ -98,29 +97,10 @@ public async Task IntegratedWindowsAuthFlow_HappyPath()
9897
}
9998

10099
[Test]
101-
public async Task IntegratedWindowsAuthFlow_GetTokenSilent_ReturnsNull()
102-
{
103-
this.SilentAuthReturnsNull();
104-
105-
this.MockAccount();
106-
107-
// Act
108-
AuthFlow.IntegratedWindowsAuthentication iwa = this.Subject();
109-
var authFlowResult = await iwa.GetTokenAsync();
110-
111-
// Assert
112-
this.pcaWrapperMock.VerifyAll();
113-
authFlowResult.TokenResult.Should().Be(null);
114-
authFlowResult.Errors.Should().BeEmpty();
115-
authFlowResult.AuthFlowName.Should().Be("IntegratedWindowsAuthentication");
116-
}
117-
118-
[Test]
119-
public async Task IntegratedWindowsAuthFlow_MsalUIException()
100+
public async Task GetCachedToken_ReturnsNull()
120101
{
121-
this.SilentAuthUIRequired();
122-
123102
this.MockAccount();
103+
this.CachedAuthReturnsNull();
124104

125105
// Act
126106
AuthFlow.IntegratedWindowsAuthentication iwa = this.Subject();
@@ -130,12 +110,11 @@ public async Task IntegratedWindowsAuthFlow_MsalUIException()
130110
this.pcaWrapperMock.VerifyAll();
131111
authFlowResult.TokenResult.Should().Be(null);
132112
authFlowResult.Errors.Should().HaveCount(1);
133-
authFlowResult.Errors[0].Should().BeOfType(typeof(MsalUiRequiredException));
134113
authFlowResult.AuthFlowName.Should().Be("IntegratedWindowsAuthentication");
135114
}
136115

137116
[Test]
138-
public void IntegratedWindowsAuthFlow_General_Exceptions_Are_ReThrown()
117+
public void General_Exceptions_Are_ReThrown()
139118
{
140119
var message = "Something somwhere has gone terribly wrong!";
141120
this.pcaWrapperMock
@@ -155,11 +134,10 @@ public void IntegratedWindowsAuthFlow_General_Exceptions_Are_ReThrown()
155134
}
156135

157136
[Test]
158-
public async Task IntegratedWindowsAuthFlow_GetTokenSilent_MsalServiceException()
137+
public async Task CachedAuth_Throws_ServiceException()
159138
{
160-
this.SilentAuthServiceException();
161-
162139
this.MockAccount();
140+
this.CachedAuthServiceException();
163141

164142
// Act
165143
AuthFlow.IntegratedWindowsAuthentication iwa = this.Subject();
@@ -174,11 +152,10 @@ public async Task IntegratedWindowsAuthFlow_GetTokenSilent_MsalServiceException(
174152
}
175153

176154
[Test]
177-
public async Task IntegratedWindowsAuthFlow_GetTokenSilent_OperationCanceledException()
155+
public async Task GetTokenSilent_OperationCanceledException()
178156
{
179-
this.SilentAuthTimeout();
180-
181157
this.MockAccount();
158+
this.CachedAuthTimeout();
182159

183160
// Act
184161
AuthFlow.IntegratedWindowsAuthentication iwa = this.Subject();
@@ -187,18 +164,18 @@ public async Task IntegratedWindowsAuthFlow_GetTokenSilent_OperationCanceledExce
187164
// Assert
188165
this.pcaWrapperMock.VerifyAll();
189166
authFlowResult.TokenResult.Should().Be(null);
190-
authFlowResult.Errors.Should().HaveCount(1);
167+
authFlowResult.Errors.Should().HaveCount(2);
191168
authFlowResult.Errors[0].Should().BeOfType(typeof(AuthenticationTimeoutException));
192169
authFlowResult.Errors[0].Message.Should().Be("Get Token Silent timed out after 00:00:06");
170+
authFlowResult.Errors[1].Should().BeOfType(typeof(NullTokenResultException));
193171
authFlowResult.AuthFlowName.Should().Be("IntegratedWindowsAuthentication");
194172
}
195173

196174
[Test]
197-
public async Task IntegratedWindowsAuthFlow_GetTokenSilent_MsalClientException()
175+
public async Task GetTokenSilent_MsalClientException()
198176
{
199-
this.SilentAuthClientException();
200-
201177
this.MockAccount();
178+
this.CachedAuthClientException();
202179

203180
// Act
204181
AuthFlow.IntegratedWindowsAuthentication iwa = this.Subject();
@@ -213,11 +190,10 @@ public async Task IntegratedWindowsAuthFlow_GetTokenSilent_MsalClientException()
213190
}
214191

215192
[Test]
216-
public async Task IntegratedWindowsAuthFlow_GetTokenSilent_NullReferenceException()
193+
public async Task GetTokenSilent_NullReferenceException()
217194
{
218-
this.SilentAuthNullReferenceException();
219-
220195
this.MockAccount();
196+
this.CachedAuthNullReferenceException();
221197

222198
// Act
223199
AuthFlow.IntegratedWindowsAuthentication iwa = this.Subject();
@@ -232,11 +208,11 @@ public async Task IntegratedWindowsAuthFlow_GetTokenSilent_NullReferenceExceptio
232208
}
233209

234210
[Test]
235-
public async Task IntegratedWindowsAuthFlow_GetTokenIWA()
211+
public async Task NoCachedAccounts_IWASuccess()
236212
{
237-
this.IntegratedWindowsAuthenticationResult();
238-
239213
this.MockAccountReturnsNull();
214+
this.CachedAuthUIRequiredNoAccount();
215+
this.IWAReturnsResult();
240216

241217
// Act
242218
AuthFlow.IntegratedWindowsAuthentication iwa = this.Subject();
@@ -246,16 +222,16 @@ public async Task IntegratedWindowsAuthFlow_GetTokenIWA()
246222
this.pcaWrapperMock.VerifyAll();
247223
authFlowResult.TokenResult.Should().Be(this.tokenResult);
248224
authFlowResult.TokenResult.IsSilent.Should().BeTrue();
249-
authFlowResult.Errors.Should().BeEmpty();
225+
authFlowResult.Errors.Should().HaveCount(1);
250226
authFlowResult.AuthFlowName.Should().Be("IntegratedWindowsAuthentication");
251227
}
252228

253229
[Test]
254-
public async Task IntegratedWindowsAuthFlow_GetTokenIWA_ReturnsNull()
230+
public async Task GetTokenIWA_ReturnsNull()
255231
{
256-
this.IntegratedWindowsAuthenticationReturnsNull();
257-
258232
this.MockAccountReturnsNull();
233+
this.CachedAuthUIRequired();
234+
this.IWAReturnsNull();
259235

260236
// Act
261237
AuthFlow.IntegratedWindowsAuthentication iwa = this.Subject();
@@ -264,54 +240,57 @@ public async Task IntegratedWindowsAuthFlow_GetTokenIWA_ReturnsNull()
264240
// Assert
265241
this.pcaWrapperMock.VerifyAll();
266242
authFlowResult.TokenResult.Should().Be(null);
267-
authFlowResult.Errors.Should().BeEmpty();
243+
authFlowResult.Errors.Should().HaveCount(1);
244+
authFlowResult.Errors[0].Should().BeOfType(typeof(MsalUiRequiredException));
268245
authFlowResult.AuthFlowName.Should().Be("IntegratedWindowsAuthentication");
269246
}
270247

271248
[Test]
272-
public async Task IntegratedWindowsAuthFlow_GetTokenIWA_MsalUIRequired_2FA()
249+
public async Task GetTokenIWA_MsalUIRequired_2FA()
273250
{
274-
this.IntegratedWindowsAuthenticationUIRequiredFor2FA();
275-
276251
this.MockAccountReturnsNull();
252+
this.CachedAuthUIRequired();
253+
this.IWAUIRequiredFor2FA();
277254

278255
// Act
279256
AuthFlow.IntegratedWindowsAuthentication iwa = this.Subject();
280257
var authFlowResult = await iwa.GetTokenAsync();
281258

282259
this.pcaWrapperMock.VerifyAll();
283260
authFlowResult.TokenResult.Should().Be(null);
284-
authFlowResult.Errors.Should().HaveCount(1);
261+
authFlowResult.Errors.Should().HaveCount(2);
285262
authFlowResult.Errors[0].Should().BeOfType(typeof(MsalUiRequiredException));
286-
authFlowResult.Errors[0].Message.Should().Be("AADSTS50076 MSAL UI Required Exception!");
263+
authFlowResult.Errors[1].Should().BeOfType(typeof(MsalUiRequiredException));
264+
authFlowResult.Errors[1].Message.Should().Be("AADSTS50076 MSAL UI Required Exception!");
287265
authFlowResult.AuthFlowName.Should().Be("IntegratedWindowsAuthentication");
288266
}
289267

290268
[Test]
291-
public async Task IntegratedWindowsAuthFlow_GetTokenIWA_MsalUIRequired_AADBrokeIWA()
269+
public async Task GetTokenIWA_GenericMsalUIRequired()
292270
{
293-
this.IntegratedWindowsAuthenticationUIRequiredForAADBrokeIWA();
294-
295271
this.MockAccountReturnsNull();
272+
this.CachedAuthUIRequired();
273+
this.IWAGenericUIRequiredException();
296274

297275
// Act
298276
AuthFlow.IntegratedWindowsAuthentication iwa = this.Subject();
299277
var authFlowResult = await iwa.GetTokenAsync();
300278

301279
this.pcaWrapperMock.VerifyAll();
302280
authFlowResult.TokenResult.Should().Be(null);
303-
authFlowResult.Errors.Should().HaveCount(1);
281+
authFlowResult.Errors.Should().HaveCount(2);
304282
authFlowResult.Errors[0].Should().BeOfType(typeof(MsalUiRequiredException));
305-
authFlowResult.Errors[0].Message.Should().Be("MSAL UI Required Exception!");
283+
authFlowResult.Errors[1].Should().BeOfType(typeof(MsalUiRequiredException));
284+
authFlowResult.Errors[1].Message.Should().Be("MSAL UI Required Exception!");
306285
authFlowResult.AuthFlowName.Should().Be("IntegratedWindowsAuthentication");
307286
}
308287

309288
[Test]
310-
public async Task IntegratedWindowsAuthFlow_GetTokenIWA_MsalServiceException()
289+
public async Task GetTokenIWA_MsalServiceException()
311290
{
312-
this.IntegratedWindowsAuthenticationServiceException();
313-
314291
this.MockAccountReturnsNull();
292+
this.CachedAuthUIRequired();
293+
this.IWAServiceException();
315294

316295
// Act
317296
AuthFlow.IntegratedWindowsAuthentication iwa = this.Subject();
@@ -321,17 +300,18 @@ public async Task IntegratedWindowsAuthFlow_GetTokenIWA_MsalServiceException()
321300
// our caller can retry auth another way.
322301
this.pcaWrapperMock.VerifyAll();
323302
authFlowResult.TokenResult.Should().Be(null);
324-
authFlowResult.Errors.Should().HaveCount(1);
325-
authFlowResult.Errors[0].Should().BeOfType(typeof(MsalServiceException));
303+
authFlowResult.Errors.Should().HaveCount(2);
304+
authFlowResult.Errors[0].Should().BeOfType(typeof(MsalUiRequiredException));
305+
authFlowResult.Errors[1].Should().BeOfType(typeof(MsalServiceException));
326306
authFlowResult.AuthFlowName.Should().Be("IntegratedWindowsAuthentication");
327307
}
328308

329309
[Test]
330-
public async Task IntegratedWindowsAuthFlow_GetTokenIWA_MsalClientException()
310+
public async Task GetTokenIWA_MsalClientException()
331311
{
332-
this.IntegratedWindowsAuthenticationClientException();
333-
334312
this.MockAccountReturnsNull();
313+
this.CachedAuthUIRequired();
314+
this.IWAClientException();
335315

336316
// Act
337317
AuthFlow.IntegratedWindowsAuthentication iwa = this.Subject();
@@ -340,96 +320,104 @@ public async Task IntegratedWindowsAuthFlow_GetTokenIWA_MsalClientException()
340320
// Assert
341321
this.pcaWrapperMock.VerifyAll();
342322
authFlowResult.TokenResult.Should().Be(null);
343-
authFlowResult.Errors.Should().HaveCount(1);
344-
authFlowResult.Errors[0].Should().BeOfType(typeof(MsalClientException));
323+
authFlowResult.Errors.Should().HaveCount(2);
324+
authFlowResult.Errors[0].Should().BeOfType(typeof(MsalUiRequiredException));
325+
authFlowResult.Errors[1].Should().BeOfType(typeof(MsalClientException));
345326
authFlowResult.AuthFlowName.Should().Be("IntegratedWindowsAuthentication");
346327
}
347328

348-
private void SilentAuthResult()
329+
private void CachedAuthResult()
349330
{
350331
this.pcaWrapperMock
351332
.Setup((pca) => pca.GetTokenSilentAsync(this.scopes, this.testAccount.Object, It.IsAny<CancellationToken>()))
352333
.ReturnsAsync(this.tokenResult);
353334
}
354335

355-
private void SilentAuthReturnsNull()
336+
private void CachedAuthReturnsNull()
356337
{
357338
this.pcaWrapperMock
358339
.Setup((pca) => pca.GetTokenSilentAsync(this.scopes, this.testAccount.Object, It.IsAny<CancellationToken>()))
359340
.ReturnsAsync((TokenResult)null);
360341
}
361342

362-
private void SilentAuthUIRequired()
343+
private void CachedAuthUIRequired()
363344
{
364345
this.pcaWrapperMock
365-
.Setup((pca) => pca.GetTokenSilentAsync(this.scopes, this.testAccount.Object, It.IsAny<CancellationToken>()))
346+
.Setup((pca) => pca.GetTokenSilentAsync(this.scopes, null, It.IsAny<CancellationToken>()))
366347
.Throws(new MsalUiRequiredException("1", "UI is required"));
367348
}
368349

369-
private void SilentAuthServiceException()
350+
private void CachedAuthServiceException()
370351
{
371352
this.pcaWrapperMock
372353
.Setup((pca) => pca.GetTokenSilentAsync(this.scopes, this.testAccount.Object, It.IsAny<CancellationToken>()))
373354
.Throws(new MsalServiceException(MsalServiceExceptionErrorCode, MsalServiceExceptionMessage));
374355
}
375356

376-
private void SilentAuthTimeout()
357+
private void CachedAuthTimeout()
377358
{
378359
this.pcaWrapperMock
379360
.Setup((pca) => pca.GetTokenSilentAsync(this.scopes, this.testAccount.Object, It.IsAny<CancellationToken>()))
380361
.Throws(new OperationCanceledException());
381362
}
382363

383-
private void SilentAuthClientException()
364+
private void CachedAuthClientException()
384365
{
385366
this.pcaWrapperMock
386367
.Setup((pca) => pca.GetTokenSilentAsync(this.scopes, this.testAccount.Object, It.IsAny<CancellationToken>()))
387368
.Throws(new MsalClientException("1", "Could not find a WAM account for the silent request."));
388369
}
389370

390-
private void SilentAuthNullReferenceException()
371+
private void CachedAuthNullReferenceException()
391372
{
392373
this.pcaWrapperMock
393374
.Setup((pca) => pca.GetTokenSilentAsync(this.scopes, this.testAccount.Object, It.IsAny<CancellationToken>()))
394375
.Throws(new NullReferenceException("There was a null reference excpetion. This should absolutly never happen and if it does it is a bug."));
395376
}
396377

397-
private void IntegratedWindowsAuthenticationResult()
378+
private void IWAReturnsResult()
398379
{
399380
this.pcaWrapperMock
400381
.Setup((pca) => pca.GetTokenIntegratedWindowsAuthenticationAsync(this.scopes, It.IsAny<CancellationToken>()))
401382
.ReturnsAsync(this.tokenResult);
402383
}
403384

404-
private void IntegratedWindowsAuthenticationReturnsNull()
385+
private void IWAReturnsNull()
405386
{
406387
this.pcaWrapperMock
407388
.Setup((pca) => pca.GetTokenIntegratedWindowsAuthenticationAsync(this.scopes, It.IsAny<CancellationToken>()))
408389
.ReturnsAsync((TokenResult)null);
409390
}
410391

411-
private void IntegratedWindowsAuthenticationUIRequiredFor2FA()
392+
private void CachedAuthUIRequiredNoAccount()
393+
{
394+
this.pcaWrapperMock
395+
.Setup((pca) => pca.GetTokenSilentAsync(this.scopes, null, It.IsAny<CancellationToken>()))
396+
.Throws(new MsalUiRequiredException("1", "No account hint given!"));
397+
}
398+
399+
private void IWAUIRequiredFor2FA()
412400
{
413401
this.pcaWrapperMock
414402
.Setup((pca) => pca.GetTokenIntegratedWindowsAuthenticationAsync(this.scopes, It.IsAny<CancellationToken>()))
415403
.Throws(new MsalUiRequiredException("1", "AADSTS50076 MSAL UI Required Exception!"));
416404
}
417405

418-
private void IntegratedWindowsAuthenticationUIRequiredForAADBrokeIWA()
406+
private void IWAGenericUIRequiredException()
419407
{
420408
this.pcaWrapperMock
421409
.Setup((pca) => pca.GetTokenIntegratedWindowsAuthenticationAsync(this.scopes, It.IsAny<CancellationToken>()))
422410
.Throws(new MsalUiRequiredException("2", "MSAL UI Required Exception!"));
423411
}
424412

425-
private void IntegratedWindowsAuthenticationServiceException()
413+
private void IWAServiceException()
426414
{
427415
this.pcaWrapperMock
428416
.Setup((pca) => pca.GetTokenIntegratedWindowsAuthenticationAsync(this.scopes, It.IsAny<CancellationToken>()))
429417
.Throws(new MsalServiceException(MsalServiceExceptionErrorCode, MsalServiceExceptionMessage));
430418
}
431419

432-
private void IntegratedWindowsAuthenticationClientException()
420+
private void IWAClientException()
433421
{
434422
this.pcaWrapperMock
435423
.Setup((pca) => pca.GetTokenIntegratedWindowsAuthenticationAsync(this.scopes, It.IsAny<CancellationToken>()))

0 commit comments

Comments
 (0)