|
28 | 28 | using Xunit;
|
29 | 29 | using Xunit.Abstractions;
|
30 | 30 | using System.Text.RegularExpressions;
|
| 31 | +using System.Net.Http; |
| 32 | +using System.Threading; |
31 | 33 |
|
32 | 34 | namespace Common.Authentication.Test
|
33 | 35 | {
|
@@ -561,5 +563,51 @@ private string GetFunctionsResourceId(string resourceIdOrEndpointName, IAzureEnv
|
561 | 563 |
|
562 | 564 | return resourceId;
|
563 | 565 | }
|
| 566 | + |
| 567 | + [Fact] |
| 568 | + [Trait(Category.AcceptanceType, Category.CheckIn)] |
| 569 | + public void CanGetServiceClientCredentialsWithAccessToken() |
| 570 | + { |
| 571 | + AzureSessionInitializer.InitializeAzureSession(); |
| 572 | + IAuthenticatorBuilder authenticatorBuilder = new DefaultAuthenticatorBuilder(); |
| 573 | + AzureSession.Instance.RegisterComponent(AuthenticatorBuilder.AuthenticatorBuilderKey, () => authenticatorBuilder); |
| 574 | + PowerShellTokenCacheProvider factory = new InMemoryTokenCacheProvider(); |
| 575 | + AzureSession.Instance.RegisterComponent(PowerShellTokenCacheProvider.PowerShellTokenCacheProviderKey, () => factory); |
| 576 | + string tenant = Guid.NewGuid().ToString(); |
| 577 | + string userId = "[email protected]"; |
| 578 | + var armToken = Guid.NewGuid().ToString(); |
| 579 | + var graphToken = Guid.NewGuid().ToString(); |
| 580 | + var kvToken = Guid.NewGuid().ToString(); |
| 581 | + var account = new AzureAccount |
| 582 | + { |
| 583 | + Id = userId, |
| 584 | + Type = AzureAccount.AccountType.AccessToken |
| 585 | + }; |
| 586 | + account.SetTenants(tenant); |
| 587 | + account.SetAccessToken(armToken); |
| 588 | + account.SetProperty(AzureAccount.Property.GraphAccessToken, graphToken); |
| 589 | + account.SetProperty(AzureAccount.Property.KeyVaultAccessToken, kvToken); |
| 590 | + var authFactory = new AuthenticationFactory(); |
| 591 | + var environment = AzureEnvironment.PublicEnvironments.Values.First(); |
| 592 | + var mockContext = new AzureContext() |
| 593 | + { |
| 594 | + Account = account |
| 595 | + }; |
| 596 | + var credentials = authFactory.GetServiceClientCredentials(mockContext); |
| 597 | + VerifyAccessTokenInServiceClientCredentials(credentials, armToken); |
| 598 | + credentials = authFactory.GetServiceClientCredentials(mockContext, AzureEnvironment.Endpoint.Graph); |
| 599 | + VerifyAccessTokenInServiceClientCredentials(credentials, graphToken); |
| 600 | + credentials = authFactory.GetServiceClientCredentials(mockContext, AzureEnvironment.Endpoint.AzureKeyVaultServiceEndpointResourceId); |
| 601 | + VerifyAccessTokenInServiceClientCredentials(credentials, kvToken); |
| 602 | + } |
| 603 | + |
| 604 | + private void VerifyAccessTokenInServiceClientCredentials(Microsoft.Rest.ServiceClientCredentials cred, string expected) |
| 605 | + { |
| 606 | + using (var request = new HttpRequestMessage()) |
| 607 | + { |
| 608 | + cred.ProcessHttpRequestAsync(request, new CancellationToken()).ConfigureAwait(false).GetAwaiter().GetResult(); |
| 609 | + Assert.Equal(expected, request.Headers.Authorization.Parameter); |
| 610 | + } |
| 611 | + } |
564 | 612 | }
|
565 | 613 | }
|
0 commit comments