// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; using Newtonsoft.Json.Linq; namespace Microsoft.AspNetCore.Authentication.Google { /// /// Contains static methods that allow to extract user's information from a /// instance retrieved from Google after a successful authentication process. /// public static class GoogleHelper { /// /// Gets the user's email. /// 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 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; } } }