|
6 | 6 | using GitCredentialManager.Authentication.Entra; |
7 | 7 | using GitCredentialManager.Tests; |
8 | 8 | using GitCredentialManager.Tests.Objects; |
| 9 | +using Microsoft.Identity.Client; |
9 | 10 | using Moq; |
10 | 11 | using Xunit; |
11 | 12 |
|
@@ -475,6 +476,161 @@ public async Task AzureReposProvider_GetCredentialAsync_JwtMode_NoCachedAuthorit |
475 | 476 | Assert.Equal(accessToken, credential.Password); |
476 | 477 | } |
477 | 478 |
|
| 479 | + [Theory] |
| 480 | + [InlineData(false, false, MsalError.InvalidClient, true)] |
| 481 | + [InlineData(true, false, MsalError.InvalidClient, true)] |
| 482 | + [InlineData(false, false, MsalError.UnauthorizedClient, true)] |
| 483 | + [InlineData(true, false, MsalError.UnauthorizedClient, true)] |
| 484 | + [InlineData(false, true, MsalError.InvalidClient, false)] |
| 485 | + [InlineData(true, true, MsalError.InvalidClient, false)] |
| 486 | + [InlineData(false, true, MsalError.UnauthorizedClient, false)] |
| 487 | + [InlineData(true, true, MsalError.UnauthorizedClient, false)] |
| 488 | + [InlineData(false, false, MsalError.AuthenticationCanceledError, false)] |
| 489 | + [InlineData(true, false, MsalError.AuthenticationCanceledError, false)] |
| 490 | + [InlineData(false, false, MsalError.AccessDenied, false)] |
| 491 | + [InlineData(true, false, MsalError.AccessDenied, false)] |
| 492 | + [InlineData(false, false, "authorization_declined", false)] |
| 493 | + [InlineData(true, false, "authorization_declined", false)] |
| 494 | + [InlineData(false, false, MsalError.InvalidRequest, false)] |
| 495 | + [InlineData(true, false, MsalError.InvalidRequest, false)] |
| 496 | + [InlineData(false, false, MsalError.InvalidGrantError, false)] |
| 497 | + [InlineData(true, false, MsalError.InvalidGrantError, false)] |
| 498 | + [InlineData(false, false, MsalError.InteractionRequired, false)] |
| 499 | + [InlineData(true, false, MsalError.InteractionRequired, false)] |
| 500 | + [InlineData(false, false, "invalid_scope", false)] |
| 501 | + [InlineData(true, false, "invalid_scope", false)] |
| 502 | + [InlineData(false, false, "temporarily_unavailable", false)] |
| 503 | + [InlineData(true, false, "temporarily_unavailable", false)] |
| 504 | + [InlineData(false, false, "test_error", false)] |
| 505 | + [InlineData(true, false, "test_error", false)] |
| 506 | + public async Task AzureReposProvider_GetCredentialAsync_MsalServiceFailure_WarnsOnlyForClientConfiguration( |
| 507 | + bool usePat, bool useLegacyClient, string errorCode, bool expectWarning) |
| 508 | + { |
| 509 | + var exception = new MsalServiceException(errorCode, "Test failure"); |
| 510 | + |
| 511 | + await AssertUserCredentialFailureAsync(usePat, useLegacyClient, exception, expectWarning); |
| 512 | + } |
| 513 | + |
| 514 | + [Theory] |
| 515 | + [InlineData(false, MsalError.InvalidClient)] |
| 516 | + [InlineData(true, MsalError.InvalidClient)] |
| 517 | + [InlineData(false, MsalError.UnauthorizedClient)] |
| 518 | + [InlineData(true, MsalError.UnauthorizedClient)] |
| 519 | + [InlineData(false, MsalError.AuthenticationCanceledError)] |
| 520 | + [InlineData(true, MsalError.AuthenticationCanceledError)] |
| 521 | + [InlineData(false, MsalError.RedirectUriValidationFailed)] |
| 522 | + [InlineData(true, MsalError.RedirectUriValidationFailed)] |
| 523 | + [InlineData(false, MsalError.CodeExpired)] |
| 524 | + [InlineData(true, MsalError.CodeExpired)] |
| 525 | + public async Task AzureReposProvider_GetCredentialAsync_MsalClientFailure_DoesNotWarn( |
| 526 | + bool usePat, string errorCode) |
| 527 | + { |
| 528 | + var exception = new MsalClientException(errorCode, "Test failure"); |
| 529 | + |
| 530 | + await AssertUserCredentialFailureAsync(usePat, false, exception, false); |
| 531 | + } |
| 532 | + |
| 533 | + [Theory] |
| 534 | + [InlineData(false, MsalError.InvalidClient)] |
| 535 | + [InlineData(true, MsalError.InvalidClient)] |
| 536 | + [InlineData(false, MsalError.UnauthorizedClient)] |
| 537 | + [InlineData(true, MsalError.UnauthorizedClient)] |
| 538 | + public async Task AzureReposProvider_GetCredentialAsync_MsalBaseFailure_DoesNotWarn( |
| 539 | + bool usePat, string errorCode) |
| 540 | + { |
| 541 | + var exception = new MsalException(errorCode, "Test failure"); |
| 542 | + |
| 543 | + await AssertUserCredentialFailureAsync(usePat, false, exception, false); |
| 544 | + } |
| 545 | + |
| 546 | + [Theory] |
| 547 | + [InlineData(false, MsalError.InvalidClient)] |
| 548 | + [InlineData(true, MsalError.InvalidClient)] |
| 549 | + [InlineData(false, MsalError.UnauthorizedClient)] |
| 550 | + [InlineData(true, MsalError.UnauthorizedClient)] |
| 551 | + public async Task AzureReposProvider_GetCredentialAsync_MsalUiRequiredFailure_DoesNotWarn( |
| 552 | + bool usePat, string errorCode) |
| 553 | + { |
| 554 | + var exception = new MsalUiRequiredException(errorCode, "Test failure"); |
| 555 | + |
| 556 | + await AssertUserCredentialFailureAsync(usePat, false, exception, false); |
| 557 | + } |
| 558 | + |
| 559 | + [Theory] |
| 560 | + [InlineData(false)] |
| 561 | + [InlineData(true)] |
| 562 | + public async Task AzureReposProvider_GetCredentialAsync_MsalPolicyFailure_DoesNotWarn(bool usePat) |
| 563 | + { |
| 564 | + var exception = new IntuneAppProtectionPolicyRequiredException( |
| 565 | + MsalError.UnauthorizedClient, "Test failure"); |
| 566 | + |
| 567 | + await AssertUserCredentialFailureAsync(usePat, false, exception, false); |
| 568 | + } |
| 569 | + |
| 570 | + [Theory] |
| 571 | + [InlineData(false)] |
| 572 | + [InlineData(true)] |
| 573 | + public async Task AzureReposProvider_GetCredentialAsync_MsalThrottledFailure_DoesNotWarn(bool usePat) |
| 574 | + { |
| 575 | + var exception = new MsalThrottledServiceException( |
| 576 | + new MsalServiceException(MsalError.InvalidClient, "Test failure")); |
| 577 | + |
| 578 | + await AssertUserCredentialFailureAsync(usePat, false, exception, false); |
| 579 | + } |
| 580 | + |
| 581 | + [Theory] |
| 582 | + [InlineData(false, 408)] |
| 583 | + [InlineData(true, 408)] |
| 584 | + [InlineData(false, 429)] |
| 585 | + [InlineData(true, 429)] |
| 586 | + [InlineData(false, 500)] |
| 587 | + [InlineData(true, 500)] |
| 588 | + public async Task AzureReposProvider_GetCredentialAsync_MsalRetryableFailure_DoesNotWarn( |
| 589 | + bool usePat, int statusCode) |
| 590 | + { |
| 591 | + var exception = new MsalServiceException(MsalError.InvalidClient, "Test failure", statusCode); |
| 592 | + |
| 593 | + await AssertUserCredentialFailureAsync(usePat, false, exception, false); |
| 594 | + } |
| 595 | + |
| 596 | + [Theory] |
| 597 | + [InlineData(false, null, true)] |
| 598 | + [InlineData(true, null, true)] |
| 599 | + [InlineData(false, "", true)] |
| 600 | + [InlineData(true, "", true)] |
| 601 | + [InlineData(false, " \t\r\n", true)] |
| 602 | + [InlineData(true, " \t\r\n", true)] |
| 603 | + [InlineData(false, "CLAIMS", false)] |
| 604 | + [InlineData(true, "CLAIMS", false)] |
| 605 | + public async Task AzureReposProvider_GetCredentialAsync_MsalClaimsFailure_WarnsOnlyWithoutClaims( |
| 606 | + bool usePat, string claims, bool expectWarning) |
| 607 | + { |
| 608 | + var exception = new MsalServiceException( |
| 609 | + MsalError.InvalidClient, "Test failure", 400, claims, null); |
| 610 | + |
| 611 | + await AssertUserCredentialFailureAsync(usePat, false, exception, expectWarning); |
| 612 | + } |
| 613 | + |
| 614 | + [Theory] |
| 615 | + [InlineData(false)] |
| 616 | + [InlineData(true)] |
| 617 | + public async Task AzureReposProvider_GetCredentialAsync_UserCancellation_DoesNotWarn(bool usePat) |
| 618 | + { |
| 619 | + var exception = new OperationCanceledException("Test failure"); |
| 620 | + |
| 621 | + await AssertUserCredentialFailureAsync(usePat, false, exception, false); |
| 622 | + } |
| 623 | + |
| 624 | + [Theory] |
| 625 | + [InlineData(false)] |
| 626 | + [InlineData(true)] |
| 627 | + public async Task AzureReposProvider_GetCredentialAsync_NonMsalFailure_DoesNotWarn(bool usePat) |
| 628 | + { |
| 629 | + var exception = new InvalidOperationException("Test failure"); |
| 630 | + |
| 631 | + await AssertUserCredentialFailureAsync(usePat, false, exception, false); |
| 632 | + } |
| 633 | + |
478 | 634 | [Fact] |
479 | 635 | public async Task AzureReposProvider_GetCredentialAsync_PatMode_OrgInUserName_NoExistingPat_GeneratesCredential() |
480 | 636 | { |
@@ -1158,6 +1314,83 @@ public void AzureReposHostProvider_TryGetAuthorityFromHeaders( |
1158 | 1314 | Assert.Equal(expectedAuthority, actualAuthority); |
1159 | 1315 | } |
1160 | 1316 |
|
| 1317 | + private static async Task AssertUserCredentialFailureAsync( |
| 1318 | + bool usePat, bool useLegacyClient, Exception failure, bool expectWarning) |
| 1319 | + { |
| 1320 | + var request = new GitRequest(new Dictionary<string, string> |
| 1321 | + { |
| 1322 | + ["protocol"] = "https", |
| 1323 | + ["host"] = "dev.azure.com", |
| 1324 | + ["path"] = "org/proj/_git/repo" |
| 1325 | + }); |
| 1326 | + |
| 1327 | + var expectedOrgUri = new Uri("https://dev.azure.com/org"); |
| 1328 | + var authorityUrl = "https://login.microsoftonline.com/common"; |
| 1329 | + var clientIds = new List<string>(); |
| 1330 | + |
| 1331 | + var context = new TestCommandContext(); |
| 1332 | + context.Environment.Variables[AzureDevOpsConstants.EnvironmentVariables.CredentialType] = |
| 1333 | + usePat ? AzureDevOpsConstants.PatCredentialType : AzureDevOpsConstants.OAuthCredentialType; |
| 1334 | + if (useLegacyClient) |
| 1335 | + { |
| 1336 | + context.Environment.Variables[AzureDevOpsConstants.EnvironmentVariables.UseLegacyClientId] = "true"; |
| 1337 | + } |
| 1338 | + |
| 1339 | + var azDevOpsMock = new Mock<IAzureDevOpsRestApi>(MockBehavior.Strict); |
| 1340 | + if (usePat) |
| 1341 | + { |
| 1342 | + azDevOpsMock.Setup(x => x.GetAuthorityAsync(expectedOrgUri)).ReturnsAsync(authorityUrl); |
| 1343 | + } |
| 1344 | + |
| 1345 | + var entraAuthMock = new Mock<IEntraAuthentication>(MockBehavior.Strict); |
| 1346 | + entraAuthMock.Setup(x => x.GetTokenForUserAsync( |
| 1347 | + AzureDevOpsConstants.AzureDevOpsDefaultScopes, authorityUrl, null, |
| 1348 | + InteractionMode.Auto, CancellationToken.None)) |
| 1349 | + .ThrowsAsync(failure); |
| 1350 | + |
| 1351 | + IEntraAuthentication EntraAuthFactory(PublicClientConfig config) |
| 1352 | + { |
| 1353 | + clientIds.Add(config.ClientId); |
| 1354 | + entraAuthMock.SetupGet(x => x.PublicClientConfig).Returns(config); |
| 1355 | + return entraAuthMock.Object; |
| 1356 | + } |
| 1357 | + |
| 1358 | + var authorityCacheMock = new Mock<IAzureDevOpsAuthorityCache>(MockBehavior.Strict); |
| 1359 | + authorityCacheMock.Setup(x => x.GetAuthority(OrgName)).Returns(authorityUrl); |
| 1360 | + |
| 1361 | + var userMgrMock = new Mock<IAzureReposBindingManager>(MockBehavior.Strict); |
| 1362 | + userMgrMock.Setup(x => x.GetBinding(OrgName)).Returns((AzureReposBinding)null); |
| 1363 | + |
| 1364 | + var provider = new AzureReposHostProvider(context, azDevOpsMock.Object, EntraAuthFactory, |
| 1365 | + authorityCacheMock.Object, userMgrMock.Object); |
| 1366 | + |
| 1367 | + Exception exception = await Record.ExceptionAsync(() => provider.GetCredentialAsync(request)); |
| 1368 | + |
| 1369 | + Assert.Same(failure, exception); |
| 1370 | + Assert.Equal( |
| 1371 | + new[] { useLegacyClient ? AzureDevOpsConstants.LegacyClientId : AzureDevOpsConstants.ClientId }, |
| 1372 | + clientIds); |
| 1373 | + entraAuthMock.Verify(x => x.GetTokenForUserAsync( |
| 1374 | + AzureDevOpsConstants.AzureDevOpsDefaultScopes, authorityUrl, null, |
| 1375 | + InteractionMode.Auto, CancellationToken.None), |
| 1376 | + Times.Once); |
| 1377 | + |
| 1378 | + if (expectWarning) |
| 1379 | + { |
| 1380 | + Assert.Equal(new[] |
| 1381 | + { |
| 1382 | + "Authentication using the new GCM Entra application failed. " + |
| 1383 | + "To retry this command with the legacy Entra application, " + |
| 1384 | + "set 'credential.azreposUseLegacyClientId' to 'true' in Git configuration.", |
| 1385 | + $"If that succeeds, please report the original failure at {Constants.HelpUrls.GcmNewIssue}" |
| 1386 | + }, context.Console.WrittenMessages); |
| 1387 | + } |
| 1388 | + else |
| 1389 | + { |
| 1390 | + Assert.Empty(context.Console.WrittenMessages); |
| 1391 | + } |
| 1392 | + } |
| 1393 | + |
1161 | 1394 | private static IEntraAuthenticationResult CreateAuthResult(string upn, string token) |
1162 | 1395 | { |
1163 | 1396 | return new MockEntraAuthResult |
|
0 commit comments