Skip to content

Commit

Permalink
remove duplicate dictionary lookups (#4548)
Browse files Browse the repository at this point in the history
* remove duplicate dictionary lookups

* .
  • Loading branch information
SimonCropp authored Jan 23, 2024
1 parent 6edd250 commit 040e860
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,33 +96,33 @@ public IDictionary<string, JToken> Deserialize(byte[] bytes, bool clearExistingC
return null;
}

if (cacheDict.ContainsKey(AccessTokenKey))
if (cacheDict.TryGetValue(AccessTokenKey, out IEnumerable<string> accessTokenKeys))
{
foreach (var atItem in cacheDict[AccessTokenKey])
foreach (var atItem in accessTokenKeys)
{
_accessor.SaveAccessToken(MsalAccessTokenCacheItem.FromJsonString(atItem));
}
}

if (cacheDict.ContainsKey(RefreshTokenKey))
if (cacheDict.TryGetValue(RefreshTokenKey, out IEnumerable<string> refreshTokenKeys))
{
foreach (var rtItem in cacheDict[RefreshTokenKey])
foreach (var rtItem in refreshTokenKeys)
{
_accessor.SaveRefreshToken(MsalRefreshTokenCacheItem.FromJsonString(rtItem));
}
}

if (cacheDict.ContainsKey(IdTokenKey))
if (cacheDict.TryGetValue(IdTokenKey, out IEnumerable<string> idTokenKeys))
{
foreach (var idItem in cacheDict[IdTokenKey])
foreach (var idItem in idTokenKeys)
{
_accessor.SaveIdToken(MsalIdTokenCacheItem.FromJsonString(idItem));
}
}

if (cacheDict.ContainsKey(AccountKey))
if (cacheDict.TryGetValue(AccountKey, out IEnumerable<string> accountKeys))
{
foreach (var account in cacheDict[AccountKey])
foreach (var account in accountKeys)
{
_accessor.SaveAccount(MsalAccountCacheItem.FromJsonString(account));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -448,9 +448,9 @@ protected Task<MsalTokenResponse> SendTokenRequestAsync(
}
}

if (additionalBodyParameters.ContainsKey(OAuth2Parameter.Username))
if (additionalBodyParameters.TryGetValue(OAuth2Parameter.Username, out string username))
{
return GetCcsUpnHeader(additionalBodyParameters[OAuth2Parameter.Username]);
return GetCcsUpnHeader(username);
}

if (!String.IsNullOrEmpty(AuthenticationRequestParameters.LoginHint))
Expand Down
20 changes: 10 additions & 10 deletions src/client/Microsoft.Identity.Client/OAuth2/MsalTokenResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ internal static MsalTokenResponse CreateFromiOSBrokerResponse(Dictionary<string,
{
if (responseDictionary.TryGetValue(BrokerResponseConst.BrokerErrorCode, out string errorCode))
{
string metadataOriginal = responseDictionary.ContainsKey(MsalTokenResponse.iOSBrokerErrorMetadata) ? responseDictionary[MsalTokenResponse.iOSBrokerErrorMetadata] : null;
string metadataOriginal = responseDictionary.TryGetValue(MsalTokenResponse.iOSBrokerErrorMetadata, out string iOSBrokerErrorMetadata) ? iOSBrokerErrorMetadata : null;
Dictionary<string, string> metadataDictionary = null;

if (metadataOriginal != null)
Expand All @@ -209,20 +209,20 @@ internal static MsalTokenResponse CreateFromiOSBrokerResponse(Dictionary<string,
return new MsalTokenResponse
{
Error = errorCode,
ErrorDescription = responseDictionary.ContainsKey(BrokerResponseConst.BrokerErrorDescription) ? CoreHelpers.UrlDecode(responseDictionary[BrokerResponseConst.BrokerErrorDescription]) : string.Empty,
SubError = responseDictionary.ContainsKey(OAuth2ResponseBaseClaim.SubError) ? responseDictionary[OAuth2ResponseBaseClaim.SubError] : string.Empty,
ErrorDescription = responseDictionary.TryGetValue(BrokerResponseConst.BrokerErrorDescription, out string brokerErrorDescription) ? CoreHelpers.UrlDecode(brokerErrorDescription) : string.Empty,
SubError = responseDictionary.TryGetValue(OAuth2ResponseBaseClaim.SubError, out string subError) ? subError : string.Empty,
AccountUserId = homeAcctId != null ? AccountId.ParseFromString(homeAcctId).ObjectId : null,
TenantId = homeAcctId != null ? AccountId.ParseFromString(homeAcctId).TenantId : null,
Upn = (metadataDictionary?.ContainsKey(TokenResponseClaim.Upn) ?? false) ? metadataDictionary[TokenResponseClaim.Upn] : null,
CorrelationId = responseDictionary.ContainsKey(BrokerResponseConst.CorrelationId) ? responseDictionary[BrokerResponseConst.CorrelationId] : null,
CorrelationId = responseDictionary.TryGetValue(BrokerResponseConst.CorrelationId, out string correlationId) ? correlationId : null,
};
}

var response = new MsalTokenResponse
{
AccessToken = responseDictionary[BrokerResponseConst.AccessToken],
RefreshToken = responseDictionary.ContainsKey(BrokerResponseConst.RefreshToken)
? responseDictionary[BrokerResponseConst.RefreshToken]
RefreshToken = responseDictionary.TryGetValue(BrokerResponseConst.RefreshToken, out string refreshToken)
? refreshToken
: null,
IdToken = responseDictionary[BrokerResponseConst.IdToken],
TokenType = BrokerResponseConst.Bearer,
Expand All @@ -231,16 +231,16 @@ internal static MsalTokenResponse CreateFromiOSBrokerResponse(Dictionary<string,
ExpiresIn = responseDictionary.TryGetValue(BrokerResponseConst.ExpiresOn, out string expiresOn) ?
DateTimeHelpers.GetDurationFromNowInSeconds(expiresOn) :
0,
ClientInfo = responseDictionary.ContainsKey(BrokerResponseConst.ClientInfo)
? responseDictionary[BrokerResponseConst.ClientInfo]
ClientInfo = responseDictionary.TryGetValue(BrokerResponseConst.ClientInfo, out string clientInfo)
? clientInfo
: null,
TokenSource = TokenSource.Broker
};

if (responseDictionary.ContainsKey(TokenResponseClaim.RefreshIn))
if (responseDictionary.TryGetValue(TokenResponseClaim.RefreshIn, out string refreshIn))
{
response.RefreshIn = long.Parse(
responseDictionary[TokenResponseClaim.RefreshIn],
refreshIn,
CultureInfo.InvariantCulture);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -396,10 +396,10 @@ protected override void Dispose(bool disposing)
/// </summary>
protected MsalClientException CreateExceptionForAuthenticationUiFailed(int statusCode)
{
if (NavigateErrorStatus.Messages.ContainsKey(statusCode))
if (NavigateErrorStatus.Messages.TryGetValue(statusCode, out string statusCodeMessages))
{
string format = "The browser based authentication dialog failed to complete. Reason: {0}";
string message = string.Format(CultureInfo.InvariantCulture, format, NavigateErrorStatus.Messages[statusCode]);
string message = string.Format(CultureInfo.InvariantCulture, format, statusCodeMessages);
return new MsalClientException(MsalError.AuthenticationUiFailedError, message);
}

Expand Down
68 changes: 34 additions & 34 deletions src/client/Microsoft.Identity.Client/UI/AuthorizationResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
using System.Collections.Generic;
using Microsoft.Identity.Client.OAuth2;
using Microsoft.Identity.Client.Utils;
#if SUPPORTS_SYSTEM_TEXT_JSON
using Microsoft.Identity.Client.Platforms.net6;
#else
using Microsoft.Identity.Json;
#if SUPPORTS_SYSTEM_TEXT_JSON
using Microsoft.Identity.Client.Platforms.net6;
#else
using Microsoft.Identity.Json;
#endif
namespace Microsoft.Identity.Client.UI
{
Expand All @@ -21,7 +21,7 @@ internal enum AuthorizationStatus
UnknownError
}

[JsonObject]
[JsonObject]
[Preserve(AllMembers = true)]
internal class AuthorizationResult
{
Expand Down Expand Up @@ -74,20 +74,20 @@ public static AuthorizationResult FromPostData(byte[] postData)

private static AuthorizationResult FromParsedValues(Dictionary<string, string> parameters, string url = null)
{
if (parameters.ContainsKey(TokenResponseClaim.Error))
if (parameters.TryGetValue(TokenResponseClaim.Error, out string error))
{
if (parameters.TryGetValue(TokenResponseClaim.ErrorSubcode, out string subcode))
{
if (TokenResponseClaim.ErrorSubcodeCancel.Equals(subcode, StringComparison.OrdinalIgnoreCase))
{
return FromStatus(AuthorizationStatus.UserCancel);
if (TokenResponseClaim.ErrorSubcodeCancel.Equals(subcode, StringComparison.OrdinalIgnoreCase))
{
return FromStatus(AuthorizationStatus.UserCancel);
}
}

return FromStatus(AuthorizationStatus.ProtocolError,
parameters[TokenResponseClaim.Error],
parameters.ContainsKey(TokenResponseClaim.ErrorDescription)
? parameters[TokenResponseClaim.ErrorDescription]
error,
parameters.TryGetValue(TokenResponseClaim.ErrorDescription, out string errorDescription)
? errorDescription
: null);
}

Expand All @@ -96,24 +96,24 @@ private static AuthorizationResult FromParsedValues(Dictionary<string, string> p
Status = AuthorizationStatus.Success
};

if (parameters.ContainsKey(OAuth2Parameter.State))
if (parameters.TryGetValue(OAuth2Parameter.State, out string state))
{
authResult.State = parameters[OAuth2Parameter.State];
authResult.State = state;
}

if (parameters.ContainsKey(TokenResponseClaim.CloudInstanceHost))
if (parameters.TryGetValue(TokenResponseClaim.CloudInstanceHost, out string cloudInstanceHost))
{
authResult.CloudInstanceHost = parameters[TokenResponseClaim.CloudInstanceHost];
authResult.CloudInstanceHost = cloudInstanceHost;
}

if (parameters.ContainsKey(TokenResponseClaim.ClientInfo))
if (parameters.TryGetValue(TokenResponseClaim.ClientInfo, out string clientInfo))
{
authResult.ClientInfo = parameters[TokenResponseClaim.ClientInfo];
authResult.ClientInfo = clientInfo;
}

if (parameters.ContainsKey(TokenResponseClaim.Code))
if (parameters.TryGetValue(TokenResponseClaim.Code, out string code))
{
authResult.Code = parameters[TokenResponseClaim.Code];
authResult.Code = code;
}
else if (!string.IsNullOrEmpty(url) && url.StartsWith("msauth://", StringComparison.OrdinalIgnoreCase))
{
Expand Down Expand Up @@ -167,26 +167,26 @@ public static AuthorizationResult FromStatus(AuthorizationStatus status, string
};
}

public AuthorizationStatus Status { get; private set; }

#if !SUPPORTS_SYSTEM_TEXT_JSON
[JsonProperty]
#endif
public AuthorizationStatus Status { get; private set; }

#if !SUPPORTS_SYSTEM_TEXT_JSON
[JsonProperty]
#endif
public string Code { get; set; }

#if !SUPPORTS_SYSTEM_TEXT_JSON
[JsonProperty]
#endif
#if !SUPPORTS_SYSTEM_TEXT_JSON
[JsonProperty]
#endif
public string Error { get; set; }

#if !SUPPORTS_SYSTEM_TEXT_JSON
[JsonProperty]
#endif
#if !SUPPORTS_SYSTEM_TEXT_JSON
[JsonProperty]
#endif
public string ErrorDescription { get; set; }

#if !SUPPORTS_SYSTEM_TEXT_JSON
[JsonProperty]
#endif
#if !SUPPORTS_SYSTEM_TEXT_JSON
[JsonProperty]
#endif
public string CloudInstanceHost { get; set; }

public string ClientInfo { get; set; }
Expand Down

0 comments on commit 040e860

Please sign in to comment.