Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more logging around client creds and claims #3707

Merged
merged 3 commits into from
Sep 30, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -176,37 +176,7 @@ public static byte[] GetKrbCred(KerberosSupplementalTicket ticket)

return null;
}

/// <summary>
/// Add Claims to body parameter for POST request.
/// </summary>
/// <param name="oAuth2Client"><see cref="OAuth2Client"/> object for Token request.</param>
/// <param name="requestParams"><see cref="AuthenticationRequestParameters"/> containing request parameters.</param>
internal static void AddKerberosTicketClaim(
OAuth2Client oAuth2Client,
AuthenticationRequestParameters requestParams)
{
string kerberosClaim = GetKerberosTicketClaim(
requestParams.RequestContext.ServiceBundle.Config.KerberosServicePrincipalName,
requestParams.RequestContext.ServiceBundle.Config.TicketContainer);

if (string.IsNullOrEmpty(kerberosClaim))
{
oAuth2Client.AddBodyParameter(OAuth2Parameter.Claims, requestParams.ClaimsAndClientCapabilities);
}
else if (string.IsNullOrEmpty(requestParams.ClaimsAndClientCapabilities))
{
oAuth2Client.AddBodyParameter(OAuth2Parameter.Claims, kerberosClaim);
}
else
{
var existingClaims = JsonHelper.ParseIntoJsonObject(requestParams.ClaimsAndClientCapabilities);
var mergedClaims = ClaimsHelper.MergeClaimsIntoCapabilityJson(kerberosClaim, existingClaims);

oAuth2Client.AddBodyParameter(OAuth2Parameter.Claims, JsonHelper.JsonObjectToString(mergedClaims));
}
}


/// <summary>
/// Generate a Kerberos Ticket Claim string.
/// </summary>
Expand Down
45 changes: 44 additions & 1 deletion src/client/Microsoft.Identity.Client/OAuth2/TokenClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ private async Task AddBodyParamsAndHeadersAsync(

if (_serviceBundle.Config.ClientCredential != null)
{
_requestParams.RequestContext.Logger.Verbose(
bgavrilMS marked this conversation as resolved.
Show resolved Hide resolved
"Before adding the client assertion / secret");

await _serviceBundle.Config.ClientCredential.AddConfidentialClientParametersAsync(
_oAuth2Client,
_requestParams.RequestContext.Logger,
Expand All @@ -138,14 +141,17 @@ await _serviceBundle.Config.ClientCredential.AddConfidentialClientParametersAsyn
_requestParams.Authority.GetTokenEndpoint(),
_requestParams.SendX5C,
cancellationToken).ConfigureAwait(false);

_requestParams.RequestContext.Logger.Verbose(
"After adding the client assertion / secret");
}

_oAuth2Client.AddBodyParameter(OAuth2Parameter.Scope, scopes);

// Add Kerberos Ticket claims if there's valid service principal name in Configuration.
// Kerberos Ticket claim is only allowed at token request due to security issue.
// It should not be included for authorize request.
KerberosSupplementalTicketManager.AddKerberosTicketClaim(_oAuth2Client, _requestParams);
AddClaims();
bgavrilMS marked this conversation as resolved.
Show resolved Hide resolved

foreach (var kvp in additionalBodyParameters)
{
Expand Down Expand Up @@ -180,6 +186,43 @@ await _serviceBundle.Config.ClientCredential.AddConfidentialClientParametersAsyn
AddExtraHttpHeaders();
}

/// <summary>
/// Add Claims, including ClientCapabilities, to body parameter for POST request.
/// </summary>
private void AddClaims()
{
string kerberosClaim = KerberosSupplementalTicketManager.GetKerberosTicketClaim(
_requestParams.RequestContext.ServiceBundle.Config.KerberosServicePrincipalName,
_requestParams.RequestContext.ServiceBundle.Config.TicketContainer);
string resolvedClaims;
if (string.IsNullOrEmpty(kerberosClaim))
{
resolvedClaims = _requestParams.ClaimsAndClientCapabilities;
}
else
{
if (!string.IsNullOrEmpty(_requestParams.ClaimsAndClientCapabilities))
{
var existingClaims = JsonHelper.ParseIntoJsonObject(_requestParams.ClaimsAndClientCapabilities);
var mergedClaims = ClaimsHelper.MergeClaimsIntoCapabilityJson(kerberosClaim, existingClaims);

resolvedClaims = JsonHelper.JsonObjectToString(mergedClaims);
_requestParams.RequestContext.Logger.Verbose(
$"Adding kerberos claim + Claims/ClientCapabilities to request: {resolvedClaims}");
}
else
{
resolvedClaims = kerberosClaim;
_requestParams.RequestContext.Logger.Verbose(
$"Adding kerberos claim to request: {resolvedClaims}");
}
}

// no-op if resolvedClaims is null
_oAuth2Client.AddBodyParameter(OAuth2Parameter.Claims, resolvedClaims);
}


private void AddExtraHttpHeaders()
{
if (_requestParams.ExtraHttpHeaders != null)
Expand Down