Repository navigation
Expand file tree
/
Copy pathGoogleHelper.cs
More file actions
131 lines (116 loc) · 3.84 KB
/
Copy pathGoogleHelper.cs
File metadata and controls
131 lines (116 loc) · 3.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// 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 Microsoft.Extensions.Internal;
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 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;
}
// 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;
}
}
}