Skip to content

Commit

Permalink
Minor fixes. Update tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
pmaytak committed Feb 12, 2021
1 parent 618b099 commit a1b77bc
Show file tree
Hide file tree
Showing 3 changed files with 220 additions and 143 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ internal class AdfsWebFingerResponse : OAuth2ResponseBase, IJsonSerializable<Adf

Subject = jObject[AdfsWebFingerResponseClaim.Subject]?.ToString();
Links = ((JArray)jObject[AdfsWebFingerResponseClaim.Links]).Select(c => new LinksList().DeserializeFromJson(c.ToString())).ToList();

base.DeserializeFromJson(json);

return this;
Expand Down
53 changes: 50 additions & 3 deletions src/client/Microsoft.Identity.Client/OAuth2/MsalTokenResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Microsoft.Identity.Client.Http;
using Microsoft.Identity.Client.Core;
using System.Text;
using System.Net;

namespace Microsoft.Identity.Client.OAuth2
{
Expand All @@ -36,6 +37,13 @@ internal class TokenResponseClaim : OAuth2ResponseBaseClaim
[Preserve(AllMembers = true)]
internal class MsalTokenResponse : OAuth2ResponseBase, IJsonSerializable<MsalTokenResponse>
{
private const string WamAccountIdPropertyName = "WamAccountId";
private const string AccessTokenExpiresOnPropertyName = "AccessTokenExpiresOn";
private const string AccessTokenExtendedExpiresOnPropertyName = "AccessTokenExtendedExpiresOn";
private const string AccessTokenRefreshOnPropertyName = "AccessTokenRefreshOn";
private const string TokenSourcePropertyName = "TokenSource";
private const string HttpResponsePropertyName = "HttpResponse";

private long _expiresIn;
private long _extendedExpiresIn;
private long _refreshIn;
Expand Down Expand Up @@ -122,8 +130,30 @@ public long RefreshIn
ExtendedExpiresIn = long.Parse(jObject[TokenResponseClaim.ExtendedExpiresIn]?.ToString(), CultureInfo.InvariantCulture);
RefreshIn = long.Parse(jObject[TokenResponseClaim.RefreshIn]?.ToString(), CultureInfo.InvariantCulture);
FamilyId = jObject[TokenResponseClaim.FamilyId]?.ToString();
WamAccountId = jObject[WamAccountIdPropertyName]?.ToString();
AccessTokenExpiresOn = DateTime.Parse(jObject[AccessTokenExpiresOnPropertyName]?.ToString(), CultureInfo.CurrentCulture);
AccessTokenExtendedExpiresOn = DateTime.Parse(jObject[AccessTokenExtendedExpiresOnPropertyName]?.ToString(), CultureInfo.CurrentCulture);
AccessTokenRefreshOn = DateTime.Parse(jObject[AccessTokenRefreshOnPropertyName]?.ToString(), CultureInfo.CurrentCulture);
TokenSource = (TokenSource)Convert.ToInt32(jObject[TokenSourcePropertyName]?.ToString(), CultureInfo.InvariantCulture);
HttpResponse = DeserializeHttpResponse(jObject[HttpResponsePropertyName]);
base.DeserializeFromJson(json);

HttpResponse DeserializeHttpResponse(JToken httpResponseJson)
{
if (httpResponseJson != null)
{
HttpResponse = new HttpResponse()
{
Headers = null,
StatusCode = (HttpStatusCode)int.Parse(httpResponseJson["StatusCode"]?.ToString(), CultureInfo.InvariantCulture),
UserAgent = jObject["UserAgent"]?.ToString(),
Body = jObject["Body"]?.ToString()
};
}

return HttpResponse;
}

return this;
}

Expand All @@ -140,14 +170,31 @@ public long RefreshIn
new JProperty(TokenResponseClaim.ExtendedExpiresIn, ExtendedExpiresIn),
new JProperty(TokenResponseClaim.RefreshIn, RefreshIn),
new JProperty(TokenResponseClaim.FamilyId, FamilyId),
new JProperty(WamAccountIdPropertyName, WamAccountId),
new JProperty(AccessTokenExpiresOnPropertyName, AccessTokenExpiresOn),
new JProperty(AccessTokenExtendedExpiresOnPropertyName, AccessTokenExtendedExpiresOn),
new JProperty(AccessTokenRefreshOnPropertyName, AccessTokenRefreshOn),
new JProperty(TokenSourcePropertyName, TokenSource),
new JProperty(HttpResponsePropertyName, SerializeHttpResponse()),
JObject.Parse(base.SerializeToJson()).Properties());

JObject SerializeHttpResponse()
{
return new JObject(
new JProperty("Headers", new JArray()),
new JProperty("HeadersAsDictionary", new JObject()),
new JProperty("StatusCode", (int)HttpResponse.StatusCode),
new JProperty("UserAgent", HttpResponse.UserAgent),
new JProperty("Body", HttpResponse.Body)
);
}

return jObject.ToString(Formatting.None);
}

internal static MsalTokenResponse CreateFromiOSBrokerResponse(Dictionary<string, string> responseDictionary)
{
if (responseDictionary.TryGetValue(BrokerResponseConst.BrokerErrorCode, out string errorCode))
if (responseDictionary.TryGetValue(BrokerResponseConst.BrokerErrorCode, out string errorCode))
{
return new MsalTokenResponse
{
Expand Down Expand Up @@ -178,7 +225,7 @@ internal static MsalTokenResponse CreateFromiOSBrokerResponse(Dictionary<string,
if (responseDictionary.ContainsKey(TokenResponseClaim.RefreshIn))
{
response.RefreshIn = long.Parse(
responseDictionary[TokenResponseClaim.RefreshIn],
responseDictionary[TokenResponseClaim.RefreshIn],
CultureInfo.InvariantCulture);
}

Expand Down Expand Up @@ -258,5 +305,5 @@ public void Log(ICoreLogger logger, LogLevel logLevel)
logger.Log(logLevel, withPii.ToString(), withoutPii.ToString());
}
}
}
}
}
Loading

0 comments on commit a1b77bc

Please sign in to comment.