Skip to content

Commit 4edd2ae

Browse files
Fix up API usage changes for Google
1 parent 1301c6c commit 4edd2ae

File tree

3 files changed

+23
-41
lines changed

3 files changed

+23
-41
lines changed

src/Owin.Security.Providers.Google/GoogleAuthenticationHandler.cs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ public class GoogleAuthenticationHandler : AuthenticationHandler<GoogleAuthentic
1717
{
1818
private const string XmlSchemaString = "http://www.w3.org/2001/XMLSchema#string";
1919
private const string TokenEndpoint = "https://accounts.google.com/o/oauth2/token";
20+
// TODO: This url should come from here: https://accounts.google.com/.well-known/openid-configuration
21+
// TODO: as described by https://developers.google.com/identity/protocols/OpenIDConnect#discovery
2022
private const string UserInfoEndpoint = "https://www.googleapis.com/oauth2/v3/userinfo";
21-
private const string GooglePlusUserEndpoint = "https://www.googleapis.com/plus/v1/people/me";
2223

2324
private readonly ILogger _logger;
2425
private readonly HttpClient _httpClient;
@@ -94,16 +95,9 @@ protected override async Task<AuthenticationTicket> AuthenticateCoreAsync()
9495
UserInfoEndpoint + "?access_token=" + Uri.EscapeDataString(accessToken), Request.CallCancelled);
9596
graphResponse.EnsureSuccessStatusCode();
9697
text = await graphResponse.Content.ReadAsStringAsync();
97-
var user = JObject.Parse(text);
98+
var userInfo = JObject.Parse(text);
9899

99-
// Get the Google+ Person Info
100-
graphResponse = await _httpClient.GetAsync(
101-
GooglePlusUserEndpoint + "?access_token=" + Uri.EscapeDataString(accessToken), Request.CallCancelled);
102-
graphResponse.EnsureSuccessStatusCode();
103-
text = await graphResponse.Content.ReadAsStringAsync();
104-
var person = JObject.Parse(text);
105-
106-
var context = new GoogleAuthenticatedContext(Context, user, person, accessToken, expires, refreshToken)
100+
var context = new GoogleAuthenticatedContext(Context, userInfo, accessToken, expires, refreshToken)
107101
{
108102
Identity = new ClaimsIdentity(
109103
Options.AuthenticationType,
@@ -124,11 +118,11 @@ protected override async Task<AuthenticationTicket> AuthenticateCoreAsync()
124118
}
125119
if (!string.IsNullOrEmpty(context.Name))
126120
{
127-
context.Identity.AddClaim(new Claim("urn:googleplus:name", context.Name, XmlSchemaString, Options.AuthenticationType));
121+
context.Identity.AddClaim(new Claim("urn:google:name", context.Name, XmlSchemaString, Options.AuthenticationType));
128122
}
129123
if (!string.IsNullOrEmpty(context.Link))
130124
{
131-
context.Identity.AddClaim(new Claim("urn:googleplus:url", context.Link, XmlSchemaString, Options.AuthenticationType));
125+
context.Identity.AddClaim(new Claim("urn:google:url", context.Link, XmlSchemaString, Options.AuthenticationType));
132126
}
133127
context.Properties = properties;
134128

src/Owin.Security.Providers.Google/GoogleAuthenticationOptions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ public GoogleAuthenticationOptions()
100100
AuthenticationMode = AuthenticationMode.Passive;
101101
Scope = new List<string>
102102
{
103+
"openid",
103104
"profile",
104105
"email"
105106
};

src/Owin.Security.Providers.Google/Provider/GoogleAuthenticatedContext.cs

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
using System;
44
using System.Globalization;
5-
using System.Linq;
65
using System.Security.Claims;
76
using Microsoft.Owin;
87
using Microsoft.Owin.Security;
@@ -20,16 +19,14 @@ public class GoogleAuthenticatedContext : BaseContext
2019
/// Initializes a <see cref="GoogleAuthenticatedContext"/>
2120
/// </summary>
2221
/// <param name="context">The OWIN environment</param>
23-
/// <param name="user">The JSON-serialized user</param>
24-
/// <param name="person"></param>
25-
/// <param name="accessToken">Google+ Access token</param>
22+
/// <param name="userInfo">The JSON-serialized user_info. Format described here: https://openid.net/specs/openid-connect-core-1_0.html#StandardClaims</param>
23+
/// <param name="accessToken">Google Access token</param>
2624
/// <param name="expires">Seconds until expiration</param>
2725
/// <param name="refreshToken"></param>
28-
public GoogleAuthenticatedContext(IOwinContext context, JObject user, JObject person, string accessToken, string expires, string refreshToken)
26+
public GoogleAuthenticatedContext(IOwinContext context, JObject userInfo, string accessToken, string expires, string refreshToken)
2927
: base(context)
3028
{
31-
User = user;
32-
Person = person;
29+
UserInfo = userInfo;
3330
AccessToken = accessToken;
3431
RefreshToken = refreshToken;
3532

@@ -39,16 +36,15 @@ public GoogleAuthenticatedContext(IOwinContext context, JObject user, JObject pe
3936
ExpiresIn = TimeSpan.FromSeconds(expiresValue);
4037
}
4138

42-
Id = TryGetValue(person, "id");
43-
Name = TryGetValue(person, "displayName");
44-
Link = TryGetValue(person, "url");
45-
UserName = TryGetValue(person, "displayName").Replace(" ", "");
39+
// See https://openid.net/specs/openid-connect-core-1_0.html#StandardClaims for a list of properties
40+
Id = TryGetValue(userInfo, "sub");
41+
Name = TryGetValue(userInfo, "name");
42+
Link = TryGetValue(userInfo, "profile");
43+
UserName = TryGetValue(userInfo, "name").Replace(" ", "");
4644

47-
var email = (from e in person["emails"]
48-
where e["type"].ToString() == "account"
49-
select e).FirstOrDefault();
45+
var email = TryGetValue(userInfo, "email");
5046
if (email != null)
51-
Email = email["value"].ToString();
47+
Email = email;
5248
}
5349

5450
/// <summary>
@@ -57,16 +53,7 @@ public GoogleAuthenticatedContext(IOwinContext context, JObject user, JObject pe
5753
/// <remarks>
5854
/// Contains the Google user obtained from the endpoint https://www.googleapis.com/oauth2/v3/userinfo
5955
/// </remarks>
60-
public JObject User { get; private set; }
61-
62-
/// <summary>
63-
/// Gets the JSON-serialized person
64-
/// </summary>
65-
/// <remarks>
66-
/// Contains the Google+ person obtained from the endpoint https://www.googleapis.com/plus/v1/people/me. For more information
67-
/// see https://developers.google.com/+/api/latest/people
68-
/// </remarks>
69-
public JObject Person { get; private set; }
56+
public JObject UserInfo { get; private set; }
7057

7158
/// <summary>
7259
/// Gets the Google OAuth access token
@@ -79,12 +66,12 @@ public GoogleAuthenticatedContext(IOwinContext context, JObject user, JObject pe
7966
public string RefreshToken { get; private set; }
8067

8168
/// <summary>
82-
/// Gets the Google+ access token expiration time
69+
/// Gets the Google access token expiration time
8370
/// </summary>
8471
public TimeSpan? ExpiresIn { get; set; }
8572

8673
/// <summary>
87-
/// Gets the Google+ user ID
74+
/// Gets the Google user ID
8875
/// </summary>
8976
public string Id { get; private set; }
9077

@@ -96,12 +83,12 @@ public GoogleAuthenticatedContext(IOwinContext context, JObject user, JObject pe
9683
public string Link { get; private set; }
9784

9885
/// <summary>
99-
/// Gets the Google+ username
86+
/// Gets the Google username
10087
/// </summary>
10188
public string UserName { get; private set; }
10289

10390
/// <summary>
104-
/// Gets the Google+ email address for the account
91+
/// Gets the Google email address for the account
10592
/// </summary>
10693
public string Email { get; private set; }
10794

0 commit comments

Comments
 (0)