51 lines
1.6 KiB
C#
51 lines
1.6 KiB
C#
// 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
|
|
{
|
|
/// <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 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 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;
|
|
}
|
|
}
|
|
}
|