yavsc/Yavsc/AuthorizationServer/GoogleHelper.cs

144 lines
4.3 KiB
C#

using System;
using Newtonsoft.Json.Linq;
/// <summary>
/// Contains static methods that allow to extract user's information from a <see cref="JObject"/>
/// instance retrieved from Google after a successful authentication process.
/// </summary>
public static class GoogleHelper
{
/// <summary>
/// Gets the Google user ID.
/// </summary>
public static string GetId(JObject user)
{
if (user == null)
{
throw new ArgumentNullException(nameof(user));
}
return user.Value<string>("id");
}
/// <summary>
/// Gets the user's name.
/// </summary>
public static string GetName(JObject user)
{
if (user == null)
{
throw new ArgumentNullException(nameof(user));
}
return user.Value<string>("displayName");
}
/// <summary>
/// Gets the user's given name.
/// </summary>
public static string GetGivenName(JObject user)
{
if (user == null)
{
throw new ArgumentNullException(nameof(user));
}
return TryGetValue(user, "name", "givenName");
}
/// <summary>
/// Gets the user's family name.
/// </summary>
public static string GetFamilyName(JObject user)
{
if (user == null)
{
throw new ArgumentNullException(nameof(user));
}
return TryGetValue(user, "name", "familyName");
}
/// <summary>
/// Gets the user's profile link.
/// </summary>
public static string GetProfile(JObject user)
{
if (user == null)
{
throw new ArgumentNullException(nameof(user));
}
return user.Value<string>("url");
}
/// <summary>
/// Gets the user's email.
/// </summary>
public static string GetEmail(JObject user)
{
if (user == null)
{
throw new ArgumentNullException(nameof(user));
}
return TryGetFirstValue(user, "emails", "value");
}
// 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;
}
7 years ago
#if GoogleApisAuthOAuth2
public static ServiceAccountCredential GetGoogleApiCredentials (string[] scopes)
{
String serviceAccountEmail = "SERVICE_ACCOUNT_EMAIL_HERE";
string private_key = Startup.GoogleSettings.Account.private_key;
string secret = Startup.GoogleSettings.ClientSecret;
var certificate = new X509Certificate2(@"key.p12", secret, X509KeyStorageFlags.Exportable);
return new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(serviceAccountEmail)
{
Scopes = scopes
}.FromCertificate(certificate));
}
#endif
// 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;
}
}