Skip to content

Commit 7fb7918

Browse files
Add the missing claims in SignedAssertion With AssertionRequestOptions Delegate (#5145)
* initial * pr comments --------- Co-authored-by: Gladwin Johnson <[email protected]>
1 parent 651b71c commit 7fb7918

File tree

2 files changed

+102
-5
lines changed

2 files changed

+102
-5
lines changed

src/client/Microsoft.Identity.Client/Internal/ClientCredential/SignedAssertionDelegateClientCredential.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,18 +55,18 @@ public async Task AddConfidentialClientParametersAsync(
5555
TokenEndpoint = tokenEndpoint
5656
};
5757

58-
// Only set client capabilities if they exist and are not empty
58+
// Set client capabilities
5959
var configuredCapabilities = requestParameters
6060
.RequestContext
6161
.ServiceBundle
6262
.Config
6363
.ClientCapabilities;
6464

65-
if (configuredCapabilities != null && configuredCapabilities.Any())
66-
{
67-
assertionOptions.ClientCapabilities = configuredCapabilities;
68-
}
65+
assertionOptions.ClientCapabilities = configuredCapabilities;
6966

67+
//Set claims
68+
assertionOptions.Claims = requestParameters.Claims;
69+
7070
// Delegate that uses AssertionRequestOptions
7171
string signedAssertion = await _signedAssertionWithInfoDelegate(assertionOptions).ConfigureAwait(false);
7272

tests/Microsoft.Identity.Test.Unit/PublicApiTests/ConfidentialClientApplicationTests.cs

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2019,6 +2019,103 @@ public async Task ConfidentialClient_WithClaims_TestAsync()
20192019
}
20202020
}
20212021

2022+
[TestMethod]
2023+
public async Task SignedAssertionDelegateClientCredential_Claims_TestAsync()
2024+
{
2025+
using (var httpManager = new MockHttpManager())
2026+
{
2027+
httpManager.AddInstanceDiscoveryMockHandler();
2028+
2029+
// Mock the expected response and ensure the claims parameter is included in the request
2030+
var handler = httpManager.AddMockHandlerSuccessfulClientCredentialTokenResponseMessage();
2031+
handler.ExpectedPostData = new Dictionary<string, string>()
2032+
{
2033+
{ "claims", "{\"extra_claim\":\"value\"}" }
2034+
};
2035+
2036+
// Create ConfidentialClientApplication with a SignedAssertion delegate
2037+
var app = ConfidentialClientApplicationBuilder
2038+
.Create(TestConstants.ClientId)
2039+
.WithHttpManager(httpManager)
2040+
.WithClientAssertion(async (AssertionRequestOptions options) =>
2041+
{
2042+
// Ensure that the claims were properly passed to the assertion options
2043+
Assert.AreEqual("{\"extra_claim\":\"value\"}", options.Claims);
2044+
return await Task.FromResult("dummy_assertion").ConfigureAwait(false);
2045+
})
2046+
.BuildConcrete();
2047+
2048+
// Act: Acquire token with claims
2049+
var result = await app.AcquireTokenForClient(TestConstants.s_scope)
2050+
.WithClaims("{\"extra_claim\":\"value\"}")
2051+
.ExecuteAsync()
2052+
.ConfigureAwait(false);
2053+
2054+
// Assert: Ensure we got a valid token
2055+
Assert.IsNotNull(result);
2056+
}
2057+
}
2058+
2059+
[TestMethod]
2060+
public async Task SignedAssertionDelegateClientCredential_NoClaims_TestAsync()
2061+
{
2062+
using (var httpManager = new MockHttpManager())
2063+
{
2064+
httpManager.AddInstanceDiscoveryMockHandler();
2065+
2066+
var handler = httpManager.AddMockHandlerSuccessfulClientCredentialTokenResponseMessage();
2067+
handler.ExpectedPostData = new Dictionary<string, string>();
2068+
2069+
var app = ConfidentialClientApplicationBuilder
2070+
.Create(TestConstants.ClientId)
2071+
.WithHttpManager(httpManager)
2072+
.WithClientAssertion(async (AssertionRequestOptions options) =>
2073+
{
2074+
// Ensure claims are set when WithClaims is called
2075+
Assert.IsNull(options.Claims);
2076+
return await Task.FromResult("dummy_assertion").ConfigureAwait(false);
2077+
})
2078+
.BuildConcrete();
2079+
2080+
var result = await app.AcquireTokenForClient(TestConstants.s_scope)
2081+
.ExecuteAsync()
2082+
.ConfigureAwait(false);
2083+
2084+
Assert.IsNotNull(result);
2085+
Assert.IsFalse(handler.ActualRequestPostData.ContainsKey("claims"));
2086+
}
2087+
}
2088+
2089+
[TestMethod]
2090+
public async Task SignedAssertionDelegateClientCredential_WithClaims_TestAsync()
2091+
{
2092+
using (var httpManager = new MockHttpManager())
2093+
{
2094+
httpManager.AddInstanceDiscoveryMockHandler();
2095+
2096+
var handler = httpManager.AddMockHandlerSuccessfulClientCredentialTokenResponseMessage();
2097+
handler.ExpectedPostData = new Dictionary<string, string>();
2098+
2099+
var app = ConfidentialClientApplicationBuilder
2100+
.Create(TestConstants.ClientId)
2101+
.WithHttpManager(httpManager)
2102+
.WithClientAssertion(async (AssertionRequestOptions options) =>
2103+
{
2104+
// Ensure claims are NOT set when WithClaims is not called
2105+
Assert.IsNull(options.Claims);
2106+
return await Task.FromResult("dummy_assertion").ConfigureAwait(false);
2107+
})
2108+
.BuildConcrete();
2109+
2110+
var result = await app.AcquireTokenForClient(TestConstants.s_scope)
2111+
.ExecuteAsync()
2112+
.ConfigureAwait(false);
2113+
2114+
Assert.IsNotNull(result);
2115+
Assert.IsFalse(handler.ActualRequestPostData.ContainsKey("claims"));
2116+
}
2117+
}
2118+
20222119
[TestMethod]
20232120
public async Task AcquireTokenByAuthorizationCode_NullOrEmptyCode_ThrowsAsync()
20242121
{

0 commit comments

Comments
 (0)