Skip to content

Commit 110f17f

Browse files
committed
Update Google auth user info endpoint #251
1 parent d95d857 commit 110f17f

File tree

3 files changed

+24
-81
lines changed

3 files changed

+24
-81
lines changed

src/Microsoft.Owin.Security.Google/Constants.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ internal static class Constants
99

1010
internal const string AuthorizationEndpoint = "https://accounts.google.com/o/oauth2/v2/auth";
1111
internal const string TokenEndpoint = "https://www.googleapis.com/oauth2/v4/token";
12-
internal const string UserInformationEndpoint = "https://www.googleapis.com/plus/v1/people/me";
12+
internal const string UserInformationEndpoint = "https://www.googleapis.com/oauth2/v2/userinfo";
1313
}
1414
}

src/Microsoft.Owin.Security.Google/Provider/GoogleOAuth2AuthenticatedContext.cs

Lines changed: 11 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ public GoogleOAuth2AuthenticatedContext(IOwinContext context, JObject user, stri
3939
}
4040

4141
Id = TryGetValue(user, "id");
42-
Name = TryGetValue(user, "displayName");
43-
GivenName = TryGetValue(user, "name", "givenName");
44-
FamilyName = TryGetValue(user, "name", "familyName");
45-
Profile = TryGetValue(user, "url");
46-
Email = TryGetFirstValue(user, "emails", "value"); // TODO:
42+
Name = TryGetValue(user, "name");
43+
GivenName = TryGetValue(user, "given_name");
44+
FamilyName = TryGetValue(user, "family_name");
45+
Profile = TryGetValue(user, "link");
46+
Email = TryGetValue(user, "email");
4747
}
4848

4949
/// <summary>
@@ -70,18 +70,18 @@ public GoogleOAuth2AuthenticatedContext(IOwinContext context, JObject user, JObj
7070
}
7171

7272
Id = TryGetValue(user, "id");
73-
Name = TryGetValue(user, "displayName");
74-
GivenName = TryGetValue(user, "name", "givenName");
75-
FamilyName = TryGetValue(user, "name", "familyName");
76-
Profile = TryGetValue(user, "url");
77-
Email = TryGetFirstValue(user, "emails", "value"); // TODO:
73+
Name = TryGetValue(user, "name");
74+
GivenName = TryGetValue(user, "given_name");
75+
FamilyName = TryGetValue(user, "family_name");
76+
Profile = TryGetValue(user, "link");
77+
Email = TryGetValue(user, "email");
7878
}
7979

8080
/// <summary>
8181
/// Gets the JSON-serialized user
8282
/// </summary>
8383
/// <remarks>
84-
/// Contains the Google user obtained from the endpoint https://www.googleapis.com/oauth2/v3/userinfo
84+
/// Contains the Google user obtained from the UserInformationEndpoint
8585
/// </remarks>
8686
public JObject User { get; private set; }
8787

@@ -153,42 +153,5 @@ private static string TryGetValue(JObject user, string propertyName)
153153
JToken value;
154154
return user.TryGetValue(propertyName, out value) ? value.ToString() : null;
155155
}
156-
157-
// Get the given subProperty from a property.
158-
private static string TryGetValue(JObject user, string propertyName, string subProperty)
159-
{
160-
JToken value;
161-
if (user.TryGetValue(propertyName, out value))
162-
{
163-
var subObject = JObject.Parse(value.ToString());
164-
if (subObject != null && subObject.TryGetValue(subProperty, out value))
165-
{
166-
return value.ToString();
167-
}
168-
}
169-
return null;
170-
}
171-
172-
// Get the given subProperty from a list property.
173-
private static string TryGetFirstValue(JObject user, string propertyName, string subProperty)
174-
{
175-
JToken value;
176-
if (user.TryGetValue(propertyName, out value))
177-
{
178-
var array = JArray.Parse(value.ToString());
179-
if (array != null && array.Count > 0)
180-
{
181-
var subObject = JObject.Parse(array.First.ToString());
182-
if (subObject != null)
183-
{
184-
if (subObject.TryGetValue(subProperty, out value))
185-
{
186-
return value.ToString();
187-
}
188-
}
189-
}
190-
}
191-
return null;
192-
}
193156
}
194157
}

tests/Microsoft.Owin.Security.Tests/Google/GoogleOAuth2MiddlewareTests.cs

Lines changed: 12 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -239,26 +239,16 @@ public async Task ReplyPathWillAuthenticateValidAuthorizeCodeAndState()
239239
token_type = "Bearer"
240240
});
241241
}
242-
else if (req.RequestUri.GetLeftPart(UriPartial.Path) == "https://www.googleapis.com/plus/v1/people/me")
242+
else if (req.RequestUri.GetLeftPart(UriPartial.Path) == "https://www.googleapis.com/oauth2/v2/userinfo")
243243
{
244244
return await ReturnJsonResponse(new
245245
{
246246
id = "Test User ID",
247-
displayName = "Test Name",
248-
name = new
249-
{
250-
familyName = "Test Family Name",
251-
givenName = "Test Given Name"
252-
},
253-
url = "Profile link",
254-
emails = new[]
255-
{
256-
new
257-
{
258-
value = "Test email",
259-
type = "account"
260-
}
261-
}
247+
name = "Test Name",
248+
given_name = "Test Given Name",
249+
family_name = "Test Family Name",
250+
link = "Profile link",
251+
email = "Test email",
262252
});
263253
}
264254

@@ -371,26 +361,16 @@ public async Task AuthenticatedEventCanGetRefreshToken()
371361
refresh_token = "Test Refresh Token"
372362
});
373363
}
374-
else if (req.RequestUri.GetLeftPart(UriPartial.Path) == "https://www.googleapis.com/plus/v1/people/me")
364+
else if (req.RequestUri.GetLeftPart(UriPartial.Path) == "https://www.googleapis.com/oauth2/v2/userinfo")
375365
{
376366
return await ReturnJsonResponse(new
377367
{
378368
id = "Test User ID",
379-
displayName = "Test Name",
380-
name = new
381-
{
382-
familyName = "Test Family Name",
383-
givenName = "Test Given Name"
384-
},
385-
url = "Profile link",
386-
emails = new[]
387-
{
388-
new
389-
{
390-
value = "Test email",
391-
type = "account"
392-
}
393-
}
369+
name = "Test Name",
370+
given_name = "Test Given Name",
371+
family_name = "Test Family Name",
372+
link = "Profile link",
373+
email = "Test email",
394374
});
395375
}
396376

0 commit comments

Comments
 (0)