From 275f04bfbb7cbf262c20a1fc53729295803fedfb Mon Sep 17 00:00:00 2001 From: Simon Cropp Date: Sun, 14 Jan 2024 11:54:04 +1100 Subject: [PATCH 1/3] replace combo of where and FirstOrDefault with only FirstOrDefault --- .../Microsoft.Identity.Client/WsTrust/MexDocument.cs | 12 +++++------- .../PublicApiTests/ClientCredentialWithCertTest.cs | 2 +- .../ConfidentialClientApplicationTests.cs | 4 ++-- 3 files changed, 8 insertions(+), 10 deletions(-) diff --git a/src/client/Microsoft.Identity.Client/WsTrust/MexDocument.cs b/src/client/Microsoft.Identity.Client/WsTrust/MexDocument.cs index 93d7b780a3..d8a4e09262 100644 --- a/src/client/Microsoft.Identity.Client/WsTrust/MexDocument.cs +++ b/src/client/Microsoft.Identity.Client/WsTrust/MexDocument.cs @@ -68,13 +68,11 @@ private MexPolicy SelectPolicy(UserAuthType userAuthType) { //try ws-trust 1.3 first return _policies - .Values - .Where(p => p.Url != null && p.AuthType == userAuthType && p.Version == WsTrustVersion.WsTrust13) - .FirstOrDefault() ?? - _policies - .Values - .Where(p => p.Url != null && p.AuthType == userAuthType) - .FirstOrDefault(); + .Values + .FirstOrDefault(p => p.Url != null && p.AuthType == userAuthType && p.Version == WsTrustVersion.WsTrust13) ?? + _policies + .Values + .FirstOrDefault(p => p.Url != null && p.AuthType == userAuthType); } private void ReadPolicies(XContainer mexDocument) diff --git a/tests/Microsoft.Identity.Test.Unit/PublicApiTests/ClientCredentialWithCertTest.cs b/tests/Microsoft.Identity.Test.Unit/PublicApiTests/ClientCredentialWithCertTest.cs index 0aca8b35c8..da61c77f38 100644 --- a/tests/Microsoft.Identity.Test.Unit/PublicApiTests/ClientCredentialWithCertTest.cs +++ b/tests/Microsoft.Identity.Test.Unit/PublicApiTests/ClientCredentialWithCertTest.cs @@ -60,11 +60,11 @@ private static MockHttpMessageHandler CreateTokenResponseHttpHandlerWithX5CValid // Check presence and value of x5c cert claim. var handler = new JwtSecurityTokenHandler(); var jsonToken = handler.ReadJwtToken(encodedJwt); - var x5c = jsonToken.Header.Where(header => header.Key == "x5c").FirstOrDefault(); if (expectedX5C != null) { Assert.AreEqual("x5c", x5c.Key, "x5c should be present"); Assert.AreEqual(x5c.Value.ToString(), expectedX5C); + var x5c = jsonToken.Header.FirstOrDefault(header => header.Key == "x5c"); } else { diff --git a/tests/Microsoft.Identity.Test.Unit/PublicApiTests/ConfidentialClientApplicationTests.cs b/tests/Microsoft.Identity.Test.Unit/PublicApiTests/ConfidentialClientApplicationTests.cs index 09960315f9..11227892c4 100644 --- a/tests/Microsoft.Identity.Test.Unit/PublicApiTests/ConfidentialClientApplicationTests.cs +++ b/tests/Microsoft.Identity.Test.Unit/PublicApiTests/ConfidentialClientApplicationTests.cs @@ -656,8 +656,8 @@ public async Task ConfidentialClientUsingClientAssertionClaimsTestAsync() var jsonToken = handler.ReadJwtToken(actualAssertion); var claims = jsonToken.Claims; //checked if additional claim is in signed assertion - var audclaim = TestConstants.s_clientAssertionClaims.Where(x => x.Key == "aud").FirstOrDefault(); - var validClaim = claims.Where(x => x.Type == audclaim.Key && x.Value == audclaim.Value).FirstOrDefault(); + var audclaim = TestConstants.s_clientAssertionClaims.FirstOrDefault(x => x.Key == "aud"); + var validClaim = claims.FirstOrDefault(x => x.Type == audclaim.Key && x.Value == audclaim.Value); Assert.IsNotNull(validClaim); } } From d7336ffd3500f7447389910fd9cff3bdbd4a3bdd Mon Sep 17 00:00:00 2001 From: Simon Cropp Date: Thu, 18 Jan 2024 21:34:29 +1100 Subject: [PATCH 2/3] Update ClientCredentialWithCertTest.cs --- .../ClientCredentialWithCertTest.cs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/tests/Microsoft.Identity.Test.Unit/PublicApiTests/ClientCredentialWithCertTest.cs b/tests/Microsoft.Identity.Test.Unit/PublicApiTests/ClientCredentialWithCertTest.cs index da61c77f38..eb48ea25e8 100644 --- a/tests/Microsoft.Identity.Test.Unit/PublicApiTests/ClientCredentialWithCertTest.cs +++ b/tests/Microsoft.Identity.Test.Unit/PublicApiTests/ClientCredentialWithCertTest.cs @@ -60,16 +60,16 @@ private static MockHttpMessageHandler CreateTokenResponseHttpHandlerWithX5CValid // Check presence and value of x5c cert claim. var handler = new JwtSecurityTokenHandler(); var jsonToken = handler.ReadJwtToken(encodedJwt); - if (expectedX5C != null) - { - Assert.AreEqual("x5c", x5c.Key, "x5c should be present"); - Assert.AreEqual(x5c.Value.ToString(), expectedX5C); var x5c = jsonToken.Header.FirstOrDefault(header => header.Key == "x5c"); + if (expectedX5C != null) + { + Assert.AreEqual("x5c", x5c.Key, "x5c should be present"); + Assert.AreEqual(x5c.Value.ToString(), expectedX5C); } - else - { - Assert.IsNull(x5c.Key); - Assert.IsNull(x5c.Value); + else + { + Assert.IsNull(x5c.Key); + Assert.IsNull(x5c.Value); } } }; From 0e33f742e084fc3c182aaf538a77e56303310cfc Mon Sep 17 00:00:00 2001 From: Simon Cropp Date: Thu, 18 Jan 2024 23:39:10 +1100 Subject: [PATCH 3/3] Update DeviceAuthenticationTests.cs --- .../DeviceAuthenticationTests.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/Microsoft.Identity.Test.Integration.Win8/DeviceAuthenticationTests.cs b/tests/Microsoft.Identity.Test.Integration.Win8/DeviceAuthenticationTests.cs index e13c4bfe87..fe87087f31 100644 --- a/tests/Microsoft.Identity.Test.Integration.Win8/DeviceAuthenticationTests.cs +++ b/tests/Microsoft.Identity.Test.Integration.Win8/DeviceAuthenticationTests.cs @@ -48,9 +48,9 @@ public async Task PKeyAuthNonInteractiveTestAsync() //Assert that the PKeyAuth header is used and the token response is successful var (req, res) = factory.RequestsAndResponses - .Where(x => x.Item1.Headers.Authorization != null - && x.Item1.Headers.Authorization.Scheme.Contains(PKeyAuthConstants.PKeyAuthName) - && x.Item2.StatusCode == HttpStatusCode.OK).FirstOrDefault(); + .FirstOrDefault(x => x.Item1.Headers.Authorization != null + && x.Item1.Headers.Authorization.Scheme.Contains(PKeyAuthConstants.PKeyAuthName) + && x.Item2.StatusCode == HttpStatusCode.OK); Assert.IsNotNull(req); Assert.IsNotNull(res);