Skip to content

Commit

Permalink
reduce some string alloc on building (#4564)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp authored Jan 24, 2024
1 parent 4a0e782 commit 16827a2
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ internal static class ThrottleCommon
public const string ThrottleRetryAfterHeaderName = "x-ms-lib-capability";
public const string ThrottleRetryAfterHeaderValue = "retry-after, h429";

internal const string KeyDelimiter = ".";
internal const char KeyDelimiter = '.';

/// <summary>
/// The strict thumbprint is based on:
Expand All @@ -28,18 +28,22 @@ public static string GetRequestStrictThumbprint(
string authority,
string homeAccountId)
{
StringBuilder sb = new StringBuilder();
var sb = new StringBuilder();
if (bodyParams.TryGetValue(OAuth2Parameter.ClientId, out string clientId))
{
sb.Append((clientId ?? "") + KeyDelimiter);
sb.Append(clientId);
sb.Append(KeyDelimiter);
}
sb.Append(authority + KeyDelimiter);
sb.Append(authority);
sb.Append(KeyDelimiter);
if (bodyParams.TryGetValue(OAuth2Parameter.Scope, out string scopes))
{
sb.Append((scopes ?? "") + KeyDelimiter);
sb.Append(scopes);
sb.Append(KeyDelimiter);
}

sb.Append((homeAccountId ?? "") + KeyDelimiter);
sb.Append(homeAccountId);
sb.Append(KeyDelimiter);

return sb.ToString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ private void TryThrowException(string thumbprint, ILoggerAdapter logger)
$"[Throttling] Exception thrown because of throttling rule UiRequired - thumbprint: {thumbprint}",
$"[Throttling] Exception thrown because of throttling rule UiRequired ");

// mark the exception for logging purposes
// mark the exception for logging purposes
throw new MsalThrottledUiRequiredException(uiException);
}
}
Expand All @@ -85,7 +85,7 @@ private void TryThrowException(string thumbprint, ILoggerAdapter logger)
/// Currently, throttling will only apply to public client applications at first.
/// </summary>
private static bool IsRequestSupported(AuthenticationRequestParameters requestParams)
{
{
return !requestParams.AppConfig.IsConfidentialClient &&
requestParams.ApiId == TelemetryCore.Internal.Events.ApiEvent.ApiIds.AcquireTokenSilent;
}
Expand All @@ -102,29 +102,34 @@ private static string GetRequestStrictThumbprint(
string authority,
ICryptographyManager crypto)
{
StringBuilder sb = new StringBuilder();
var sb = new StringBuilder();
if (bodyParams.TryGetValue(OAuth2Parameter.ClientId, out string clientId))
{
sb.Append((clientId ?? "") + ThrottleCommon.KeyDelimiter);
sb.Append(clientId);
sb.Append(ThrottleCommon.KeyDelimiter);
}
sb.Append(authority + ThrottleCommon.KeyDelimiter);
sb.Append(authority);
sb.Append(ThrottleCommon.KeyDelimiter);
if (bodyParams.TryGetValue(OAuth2Parameter.Scope, out string scopes))
{
sb.Append((scopes ?? "") + ThrottleCommon.KeyDelimiter);
sb.Append(scopes);
sb.Append(ThrottleCommon.KeyDelimiter);
}

if (bodyParams.TryGetValue(OAuth2Parameter.RefreshToken, out string rt) &&
if (bodyParams.TryGetValue(OAuth2Parameter.RefreshToken, out string rt) &&
!string.IsNullOrEmpty(rt))
{
sb.Append(crypto.CreateSha256Hash(rt) + ThrottleCommon.KeyDelimiter);
sb.Append(crypto.CreateSha256Hash(rt));
sb.Append(ThrottleCommon.KeyDelimiter);
}

// check mam enrollment id
if (bodyParams.TryGetValue(SilentRequestHelper.MamEnrollmentIdKey, out string mamEnrollmentId))
{
sb.Append(crypto.CreateSha256Hash(mamEnrollmentId) + ThrottleCommon.KeyDelimiter);
sb.Append(crypto.CreateSha256Hash(mamEnrollmentId));
sb.Append(ThrottleCommon.KeyDelimiter);
}

return sb.ToString();
}
}
Expand Down

0 comments on commit 16827a2

Please sign in to comment.