Skip to content

Commit

Permalink
Update Google auth user info endpoint #251
Browse files Browse the repository at this point in the history
  • Loading branch information
Tratcher committed Jan 3, 2019
1 parent d95d857 commit 110f17f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 81 deletions.
2 changes: 1 addition & 1 deletion src/Microsoft.Owin.Security.Google/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ internal static class Constants

internal const string AuthorizationEndpoint = "https://accounts.google.com/o/oauth2/v2/auth";
internal const string TokenEndpoint = "https://www.googleapis.com/oauth2/v4/token";
internal const string UserInformationEndpoint = "https://www.googleapis.com/plus/v1/people/me";
internal const string UserInformationEndpoint = "https://www.googleapis.com/oauth2/v2/userinfo";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ public GoogleOAuth2AuthenticatedContext(IOwinContext context, JObject user, stri
}

Id = TryGetValue(user, "id");
Name = TryGetValue(user, "displayName");
GivenName = TryGetValue(user, "name", "givenName");
FamilyName = TryGetValue(user, "name", "familyName");
Profile = TryGetValue(user, "url");
Email = TryGetFirstValue(user, "emails", "value"); // TODO:
Name = TryGetValue(user, "name");
GivenName = TryGetValue(user, "given_name");
FamilyName = TryGetValue(user, "family_name");
Profile = TryGetValue(user, "link");
Email = TryGetValue(user, "email");
}

/// <summary>
Expand All @@ -70,18 +70,18 @@ public GoogleOAuth2AuthenticatedContext(IOwinContext context, JObject user, JObj
}

Id = TryGetValue(user, "id");
Name = TryGetValue(user, "displayName");
GivenName = TryGetValue(user, "name", "givenName");
FamilyName = TryGetValue(user, "name", "familyName");
Profile = TryGetValue(user, "url");
Email = TryGetFirstValue(user, "emails", "value"); // TODO:
Name = TryGetValue(user, "name");
GivenName = TryGetValue(user, "given_name");
FamilyName = TryGetValue(user, "family_name");
Profile = TryGetValue(user, "link");
Email = TryGetValue(user, "email");
}

/// <summary>
/// Gets the JSON-serialized user
/// </summary>
/// <remarks>
/// Contains the Google user obtained from the endpoint https://www.googleapis.com/oauth2/v3/userinfo
/// Contains the Google user obtained from the UserInformationEndpoint
/// </remarks>
public JObject User { get; private set; }

Expand Down Expand Up @@ -153,42 +153,5 @@ private static string TryGetValue(JObject user, string propertyName)
JToken value;
return user.TryGetValue(propertyName, out value) ? value.ToString() : null;
}

// Get the given subProperty from a property.
private static string TryGetValue(JObject user, string propertyName, string subProperty)
{
JToken value;
if (user.TryGetValue(propertyName, out value))
{
var subObject = JObject.Parse(value.ToString());
if (subObject != null && subObject.TryGetValue(subProperty, out value))
{
return value.ToString();
}
}
return null;
}

// Get the given subProperty from a list property.
private static string TryGetFirstValue(JObject user, string propertyName, string subProperty)
{
JToken value;
if (user.TryGetValue(propertyName, out value))
{
var array = JArray.Parse(value.ToString());
if (array != null && array.Count > 0)
{
var subObject = JObject.Parse(array.First.ToString());
if (subObject != null)
{
if (subObject.TryGetValue(subProperty, out value))
{
return value.ToString();
}
}
}
}
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -239,26 +239,16 @@ public async Task ReplyPathWillAuthenticateValidAuthorizeCodeAndState()
token_type = "Bearer"
});
}
else if (req.RequestUri.GetLeftPart(UriPartial.Path) == "https://www.googleapis.com/plus/v1/people/me")
else if (req.RequestUri.GetLeftPart(UriPartial.Path) == "https://www.googleapis.com/oauth2/v2/userinfo")
{
return await ReturnJsonResponse(new
{
id = "Test User ID",
displayName = "Test Name",
name = new
{
familyName = "Test Family Name",
givenName = "Test Given Name"
},
url = "Profile link",
emails = new[]
{
new
{
value = "Test email",
type = "account"
}
}
name = "Test Name",
given_name = "Test Given Name",
family_name = "Test Family Name",
link = "Profile link",
email = "Test email",
});
}
Expand Down Expand Up @@ -371,26 +361,16 @@ public async Task AuthenticatedEventCanGetRefreshToken()
refresh_token = "Test Refresh Token"
});
}
else if (req.RequestUri.GetLeftPart(UriPartial.Path) == "https://www.googleapis.com/plus/v1/people/me")
else if (req.RequestUri.GetLeftPart(UriPartial.Path) == "https://www.googleapis.com/oauth2/v2/userinfo")
{
return await ReturnJsonResponse(new
{
id = "Test User ID",
displayName = "Test Name",
name = new
{
familyName = "Test Family Name",
givenName = "Test Given Name"
},
url = "Profile link",
emails = new[]
{
new
{
value = "Test email",
type = "account"
}
}
name = "Test Name",
given_name = "Test Given Name",
family_name = "Test Family Name",
link = "Profile link",
email = "Test email",
});
}
Expand Down

0 comments on commit 110f17f

Please sign in to comment.