-
Notifications
You must be signed in to change notification settings - Fork 340
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix some static analyzer issues in tests around certs and TLS
- Loading branch information
Showing
15 changed files
with
152 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Security.Cryptography.X509Certificates; | ||
using System.Security.Cryptography; | ||
using System.Text; | ||
|
||
namespace Microsoft.Identity.Test.Common.Core.Helpers | ||
{ | ||
public static class CertHelper | ||
{ | ||
private static X509Certificate2 s_x509Certificate2 = null; | ||
|
||
public static X509Certificate2 GetOrCreateTestCert() | ||
{ | ||
// create the cert if it doesn't exist. use a lock to prevent multiple threads from creating the cert | ||
|
||
if (s_x509Certificate2 == null) | ||
{ | ||
lock (typeof(CertHelper)) | ||
{ | ||
if (s_x509Certificate2 == null) | ||
{ | ||
s_x509Certificate2 = CreateTestCert(); | ||
} | ||
} | ||
} | ||
|
||
return s_x509Certificate2; | ||
} | ||
|
||
private static X509Certificate2 CreateTestCert() | ||
{ | ||
using (RSA rsa = RSA.Create(4096)) | ||
{ | ||
CertificateRequest parentReq = new CertificateRequest( | ||
"CN=Test Cert", | ||
rsa, | ||
HashAlgorithmName.SHA256, | ||
RSASignaturePadding.Pkcs1); | ||
|
||
parentReq.CertificateExtensions.Add( | ||
new X509BasicConstraintsExtension(true, false, 0, true)); | ||
|
||
parentReq.CertificateExtensions.Add( | ||
new X509SubjectKeyIdentifierExtension(parentReq.PublicKey, false)); | ||
|
||
X509Certificate2 cert = parentReq.CreateSelfSigned( | ||
DateTimeOffset.UtcNow, | ||
DateTimeOffset.UtcNow.AddDays(1)); | ||
|
||
return cert; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,13 +4,15 @@ | |
using System; | ||
using System.Linq; | ||
using System.Net.Http; | ||
using System.Security.Claims; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.Identity.Client; | ||
using Microsoft.Identity.Client.UI; | ||
using Microsoft.Identity.Test.Common.Core.Mocks; | ||
using Microsoft.Identity.Test.Common.Mocks; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using NSubstitute; | ||
|
||
namespace Microsoft.Identity.Test.Unit | ||
{ | ||
|
@@ -52,7 +54,7 @@ public async Task MetricsUpdatedSucessfully_AcquireTokenForClient_Async() | |
Assert.IsTrue(result.AuthenticationResultMetadata.DurationInCacheInMs > 0); | ||
Assert.IsTrue(result.AuthenticationResultMetadata.DurationTotalInMs > 0); | ||
Assert.AreEqual( | ||
"https://login.microsoftonline.com/common/oauth2/v2.0/token", | ||
"https://login.microsoftonline.com/common/oauth2/v2.0/token", | ||
result.AuthenticationResultMetadata.TokenEndpoint); | ||
Assert.AreEqual(1, Metrics.TotalAccessTokensFromIdP); | ||
Assert.AreEqual(0, Metrics.TotalAccessTokensFromCache); | ||
|
@@ -160,6 +162,49 @@ public async Task RefreshReasonExpired_ConfidentialClient_Async() | |
} | ||
} | ||
|
||
[TestMethod] | ||
public void IdWebAccountExtension() | ||
{ | ||
// copied from https://github.com/AzureAD/microsoft-identity-web/blob/master/src/Microsoft.Identity.Web/AccountExtensions.cs#L13 | ||
static ClaimsPrincipal ToClaimsPrincipal(IAccount account) | ||
{ | ||
ClaimsIdentity identity = new ClaimsIdentity(new[] | ||
{ | ||
new Claim(ClaimTypes.Upn, account.Username), | ||
}); | ||
|
||
if (!string.IsNullOrEmpty(account.HomeAccountId?.ObjectId)) | ||
{ | ||
identity.AddClaim(new Claim("oid", account.HomeAccountId.ObjectId)); | ||
} | ||
|
||
if (!string.IsNullOrEmpty(account.HomeAccountId?.TenantId)) | ||
{ | ||
identity.AddClaim(new Claim("tid", account.HomeAccountId.TenantId)); | ||
} | ||
|
||
return new ClaimsPrincipal(identity); | ||
} | ||
|
||
var username = "[email protected]"; | ||
var oid = "objectId"; | ||
var tid = "tenantId"; | ||
|
||
IAccount account = Substitute.For<IAccount>(); | ||
account.Username.Returns(username); | ||
var accId = new AccountId("identifier", oid, tid); | ||
account.HomeAccountId.Returns(accId); | ||
|
||
var claimsIdentityResult = ToClaimsPrincipal(account).Identity as ClaimsIdentity; | ||
|
||
Assert.IsNotNull(claimsIdentityResult); | ||
Assert.AreEqual(3, claimsIdentityResult.Claims.Count()); | ||
Assert.AreEqual(username, claimsIdentityResult.FindFirst(ClaimTypes.Upn)?.Value); | ||
Assert.AreEqual(oid, claimsIdentityResult.FindFirst("oid")?.Value); | ||
Assert.AreEqual(tid, claimsIdentityResult.FindFirst("tid")?.Value); | ||
|
||
} | ||
|
||
[TestMethod] | ||
public async Task RefreshReasonExpired_AcquireTokenSilent_Async() | ||
{ | ||
|
@@ -255,7 +300,7 @@ private PublicClientApplication CreatePca(MockHttpManager httpManager, bool popu | |
.WithAuthority(new Uri(ClientApplicationBase.DefaultAuthority), false) | ||
.WithHttpManager(httpManager) | ||
.BuildConcrete(); | ||
|
||
if (populateUserCache) | ||
{ | ||
TokenCacheHelper.PopulateCache(pca.UserTokenCacheInternal.Accessor); | ||
|
Oops, something went wrong.