Skip to content

Commit cc4e838

Browse files
committed
Refactor lab api.
1 parent 431b732 commit cc4e838

22 files changed

+187
-166
lines changed

tests/CacheCompat/CommonCache.Test.Unit/CacheExecutionTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ public class CacheExecutionTests
1919

2020
private static async Task<LabUserData> GetPublicAadUserDataAsync()
2121
{
22-
var api = new LabServiceApi();
23-
LabResponse labResponse = (await api.GetLabResponseFromApiAsync(UserQuery.PublicAadUserQuery).ConfigureAwait(false));
22+
LabResponse labResponse = await LabUserHelper.GetDefaultUserAsync().ConfigureAwait(false);
23+
2424
return new LabUserData(
2525
labResponse.User.Upn,
2626
labResponse.User.GetOrFetchPassword(),
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Security.Cryptography;
7+
using System.Security.Cryptography.X509Certificates;
8+
9+
namespace Microsoft.Identity.Test.Common.Core.Helpers
10+
{
11+
public static class CertificateFinder
12+
{
13+
/// <summary>
14+
/// Try and locate a certificate matching the given <paramref name="subjectName"/> by searching in
15+
/// the <see cref="StoreName.My"/> store subjectName for all available <see cref="StoreLocation"/>s.
16+
/// </summary>
17+
/// <param name="subjectName">Thumbprint of certificate to locate</param>
18+
/// <returns><see cref="X509Certificate2"/> with <paramref subjectName="subjectName"/>, or null if no matching certificate was found</returns>
19+
public static X509Certificate2 FindCertificateByName(string subjectName)
20+
{
21+
foreach (StoreLocation storeLocation in Enum.GetValues(typeof(StoreLocation)))
22+
{
23+
var certificate = FindCertificateByName(subjectName, storeLocation, StoreName.My);
24+
if (certificate != null)
25+
{
26+
return certificate;
27+
}
28+
}
29+
30+
return null;
31+
}
32+
/// <summary>
33+
/// Try and locate a certificate matching the given <paramref name="certName"/> by searching in
34+
/// the in the given <see cref="StoreName"/> and <see cref="StoreLocation"/>.
35+
/// </summary>
36+
/// <param subjectName="certName">Thumbprint of certificate to locate</param>
37+
/// <param subjectName="location"><see cref="StoreLocation"/> in which to search for a matching certificate</param>
38+
/// <param subjectName="name"><see cref="StoreName"/> in which to search for a matching certificate</param>
39+
/// <returns><see cref="X509Certificate2"/> with <paramref subjectName="certName"/>, or null if no matching certificate was found</returns>
40+
public static X509Certificate2 FindCertificateByName(string certName, StoreLocation location, StoreName name)
41+
{
42+
// Don't validate certs, since the test root isn't installed.
43+
const bool validateCerts = false;
44+
45+
using (var store = new X509Store(name, location))
46+
{
47+
store.Open(OpenFlags.ReadOnly);
48+
X509Certificate2Collection collection = store.Certificates.Find(X509FindType.FindBySubjectName, certName, validateCerts);
49+
50+
X509Certificate2 certToUse = null;
51+
52+
// select the "freshest" certificate
53+
foreach (X509Certificate2 cert in collection)
54+
{
55+
if (certToUse == null || cert.NotBefore > certToUse.NotBefore)
56+
{
57+
certToUse = cert;
58+
}
59+
}
60+
61+
return certToUse;
62+
63+
}
64+
}
65+
}
66+
67+
public enum KnownTestCertType
68+
{
69+
RSA,
70+
ECD
71+
}
72+
}

tests/Microsoft.Identity.Test.Integration.netcore/HeadlessTests/CiamIntegrationTests.cs

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,7 @@ public async Task ROPC_Ciam_StandardDomains_CompletesSuccessfully()
3232
{
3333
string authority;
3434
//Get lab details
35-
var labResponse = await LabUserHelper.GetLabUserDataAsync(new UserQuery()
36-
{
37-
FederationProvider = FederationProvider.CIAMCUD,
38-
SignInAudience = SignInAudience.AzureAdMyOrg
39-
}).ConfigureAwait(false);
35+
var labResponse = await LabUserHelper.GetCiamUserAync().ConfigureAwait(false);
4036

4137
//https://tenantName.ciamlogin.com/
4238
authority = string.Format("https://{0}.ciamlogin.com/", labResponse.User.LabName);
@@ -87,12 +83,7 @@ public async Task ClientCredentialCiam_WithClientCredentials_ReturnsValidTokens(
8783
{
8884
string authority;
8985
//Get lab details
90-
var labResponse = await LabUserHelper.GetLabUserDataAsync(new UserQuery()
91-
{
92-
FederationProvider = FederationProvider.CIAMCUD,
93-
SignInAudience = SignInAudience.AzureAdMyOrg
94-
}).ConfigureAwait(false);
95-
86+
var labResponse = await LabUserHelper.GetCiamUserAync().ConfigureAwait(false);
9687

9788
//https://tenantName.ciamlogin.com/
9889
authority = string.Format("https://{0}.ciamlogin.com/", labResponse.User.LabName);
@@ -117,7 +108,7 @@ private async Task RunCiamCCATest(string authority, string appId)
117108
//Acquire tokens
118109
var msalConfidentialClientBuilder = ConfidentialClientApplicationBuilder
119110
.Create(appId)
120-
.WithCertificate(CertificateHelper.FindCertificateByName(TestConstants.AutomationTestCertName))
111+
.WithCertificate(CertificateFinder.FindCertificateByName(TestConstants.AutomationTestCertName))
121112
.WithExperimentalFeatures();
122113

123114
if (authority.Contains(Constants.CiamAuthorityHostSuffix))
@@ -157,11 +148,7 @@ public async Task OBOCiam_CustomDomain_ReturnsValidTokens()
157148
string ciamWebApi = "634de702-3173-4a71-b336-a4fab786a479";
158149

159150
//Get lab details
160-
LabResponse labResponse = await LabUserHelper.GetLabUserDataAsync(new UserQuery()
161-
{
162-
FederationProvider = FederationProvider.CIAMCUD,
163-
SignInAudience = SignInAudience.AzureAdMyOrg
164-
}).ConfigureAwait(false);
151+
var labResponse = await LabUserHelper.GetCiamUserAync().ConfigureAwait(false);
165152

166153
//Acquire tokens
167154
var msalPublicClient = PublicClientApplicationBuilder
@@ -184,7 +171,7 @@ public async Task OBOCiam_CustomDomain_ReturnsValidTokens()
184171
//Acquire tokens for OBO
185172
var msalConfidentialClient = ConfidentialClientApplicationBuilder
186173
.Create(ciamWebApi)
187-
.WithCertificate(CertificateHelper.FindCertificateByName(TestConstants.AutomationTestCertName))
174+
.WithCertificate(CertificateFinder.FindCertificateByName(TestConstants.AutomationTestCertName))
188175
.WithAuthority(authorityCud, false)
189176
.WithRedirectUri(_ciamRedirectUri)
190177
.BuildConcrete();

tests/Microsoft.Identity.Test.Integration.netcore/HeadlessTests/ClientCredentialsTests.NetFwk.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,19 +64,19 @@ public async Task RefreshOnIsEnabled(bool useRegional)
6464
Assert.Inconclusive("Can't run regional on local devbox.");
6565
}
6666

67-
var cert = CertificateHelper.FindCertificateByName(TestConstants.AutomationTestCertName);
67+
var cert = CertificateFinder.FindCertificateByName(TestConstants.AutomationTestCertName);
6868

69-
var builder = ConfidentialClientApplicationBuilder.Create(LabAuthenticationHelper.LabAccessConfidentialClientId)
69+
var builder = ConfidentialClientApplicationBuilder.Create(LabApiConstants.LabClientId)
7070
.WithCertificate(cert, sendX5C: true)
71-
.WithAuthority(LabAuthenticationHelper.LabClientInstance, LabAuthenticationHelper.LabClientTenantId);
71+
.WithAuthority(LabApiConstants.LabClientInstance, LabApiConstants.LabClientTenantId);
7272

7373
// auto-detect should work on Azure DevOps build
7474
if (useRegional)
7575
builder = builder.WithAzureRegion();
7676

7777
var cca = builder.Build();
7878

79-
var result = await cca.AcquireTokenForClient([LabAuthenticationHelper.LabScope]).ExecuteAsync().ConfigureAwait(false);
79+
var result = await cca.AcquireTokenForClient([LabApiConstants.LabScope]).ExecuteAsync().ConfigureAwait(false);
8080

8181
Assert.AreEqual(TokenSource.IdentityProvider, result.AuthenticationResultMetadata.TokenSource);
8282
Assert.IsTrue(result.AuthenticationResultMetadata.RefreshOn.HasValue, "refresh_in was not issued - did the MSAL SKU value change?");

tests/Microsoft.Identity.Test.Integration.netcore/HeadlessTests/PoPTests.NetFwk.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -770,7 +770,7 @@ public void CheckPopRuntimeBrokerSupportTest()
770770

771771
private static X509Certificate2 GetCertificate()
772772
{
773-
X509Certificate2 cert = CertificateHelper.FindCertificateByName(TestConstants.AutomationTestCertName);
773+
X509Certificate2 cert = CertificateFinder.FindCertificateByName(TestConstants.AutomationTestCertName);
774774

775775
if (cert == null)
776776
{

tests/Microsoft.Identity.Test.Integration.netcore/Infrastructure/ConfidentialAppSettings.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using System.Security.Cryptography.X509Certificates;
6+
using Microsoft.Identity.Test.Common.Core.Helpers;
67
using Microsoft.Identity.Test.LabInfrastructure;
78
using Microsoft.Identity.Test.Unit;
89

@@ -196,7 +197,7 @@ public static IConfidentialAppSettings GetSettings(Cloud cloud)
196197

197198
public static Lazy<X509Certificate2> GetCertificateLazy(string certName) => new Lazy<X509Certificate2>(() =>
198199
{
199-
X509Certificate2 cert = CertificateHelper.FindCertificateByName(certName);
200+
X509Certificate2 cert = CertificateFinder.FindCertificateByName(certName);
200201
if (cert == null)
201202
{
202203
throw new InvalidOperationException(

tests/Microsoft.Identity.Test.Integration.netcore/Infrastructure/MsiProxyHttpManager.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,12 @@
33

44
using System;
55
using System.Collections.Generic;
6-
using System.Linq;
76
using System.Net;
87
using System.Net.Http;
98
using System.Net.Http.Headers;
109
using System.Security.Cryptography.X509Certificates;
11-
using System.Text;
1210
using System.Threading;
1311
using System.Threading.Tasks;
14-
using System.Web;
15-
using Microsoft.Identity.Client;
1612
using Microsoft.Identity.Client.Core;
1713
using Microsoft.Identity.Client.Http;
1814
using Microsoft.Identity.Test.LabInfrastructure;

tests/Microsoft.Identity.Test.LabInfrastructure/CertificateHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
namespace Microsoft.Identity.Test.LabInfrastructure
88
{
9-
public static class CertificateHelper
9+
internal static class CertificateHelper
1010
{
1111
/// <summary>
1212
/// Try and locate a certificate matching the given <paramref name="subjectName"/> by searching in

tests/Microsoft.Identity.Test.LabInfrastructure/KeyVaultConfiguration.cs

Lines changed: 0 additions & 38 deletions
This file was deleted.

tests/Microsoft.Identity.Test.LabInfrastructure/KeyVaultSecretsProvider.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,7 @@ public async Task<X509Certificate2> GetCertificateWithPrivateMaterialAsync(strin
8686

8787
private async Task<TokenCredential> GetKeyVaultCredentialAsync()
8888
{
89-
var accessToken = await LabAuthenticationHelper.GetLabAccessTokenAsync(
90-
"https://login.microsoftonline.com/72f988bf-86f1-41af-91ab-2d7cd011db47/",
91-
new[] { "https://vault.azure.net/.default" }).ConfigureAwait(false);
89+
var accessToken = await LabAuthenticationHelper.GetKeyVaultAccessToken().ConfigureAwait(false);
9290
return DelegatedTokenCredential.Create((_, __) => accessToken);
9391
}
9492

0 commit comments

Comments
 (0)