Skip to content

Commit e8683b8

Browse files
Lanning, MarkLanning, Mark
authored andcommitted
added auth updates and claims principal toItemDto()
1 parent 85a7a33 commit e8683b8

File tree

3 files changed

+216
-57
lines changed

3 files changed

+216
-57
lines changed

Base/src/ThingsLibrary.Base/DataType/Extensions/ClaimsPrincipal.cs

Lines changed: 108 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public static class ClaimsPrincipalExtensions
1717
/// <param name="claimsPrincipal"><see cref="ClaimsPrincipal"/></param>
1818
/// <param name="key">Claim Key</param>
1919
/// <returns>Claim string</returns>
20-
public static string GetClaim(this ClaimsPrincipal claimsPrincipal, string key)
20+
public static string? GetClaim(this ClaimsPrincipal claimsPrincipal, string key)
2121
{
2222
return claimsPrincipal.Claims.GetClaim(key);
2323
}
@@ -30,7 +30,7 @@ public static string GetClaim(this ClaimsPrincipal claimsPrincipal, string key)
3030
/// <param name="key">Claim Key</param>
3131
/// <param name="defaultValue">Default value if key is not found.</param>
3232
/// <returns>Claim as requested data type, or DefaultValue if key not found."/></returns>
33-
public static T GetClaim<T>(this ClaimsPrincipal claimsPrincipal, string key, T defaultValue)
33+
public static T? GetClaim<T>(this ClaimsPrincipal claimsPrincipal, string key, T defaultValue)
3434
{
3535
return claimsPrincipal.Claims.GetClaim<T>(key, defaultValue);
3636
}
@@ -43,7 +43,7 @@ public static T GetClaim<T>(this ClaimsPrincipal claimsPrincipal, string key, T
4343
/// <param name="key">Claim Key</param>
4444
/// <param name="defaultValue">Default value if key is not found.</param>
4545
/// <returns>Claim as requested data type, or DefaultValue if key not found."/></returns>
46-
public static T GetClaim<T>(this IEnumerable<Claim> claims, string key, T defaultValue)
46+
public static T? GetClaim<T>(this IEnumerable<Claim> claims, string key, T defaultValue)
4747
{
4848
var claim = GetClaim(claims, key);
4949
if (claim == null) { return defaultValue; }
@@ -57,11 +57,11 @@ public static T GetClaim<T>(this IEnumerable<Claim> claims, string key, T defaul
5757
/// <param name="claims"><see cref="IEnumerable{Claim}"/></param>
5858
/// <param name="key">Claim Key</param>
5959
/// <returns>Claim string</returns>
60-
public static string GetClaim(this IEnumerable<Claim> claims, string key)
60+
public static string? GetClaim(this IEnumerable<Claim> claims, string key)
6161
{
6262
if (string.IsNullOrEmpty(key)) { return string.Empty; }
6363

64-
return claims.FirstOrDefault(c => c.Type == key)?.Value ?? string.Empty;
64+
return claims.FirstOrDefault(c => c.Type == key)?.Value;
6565
}
6666

6767
/// <summary>
@@ -70,7 +70,7 @@ public static string GetClaim(this IEnumerable<Claim> claims, string key)
7070
/// <param name="claimsPrincipal"><see cref="ClaimsPrincipal"/></param>
7171
/// <param name="keys">Claim Keys (in order of importance)</param>
7272
/// <returns>Claim string</returns>
73-
public static string GetClaim(this ClaimsPrincipal claimsPrincipal, params string[] keys)
73+
public static string? GetClaim(this ClaimsPrincipal claimsPrincipal, params string[] keys)
7474
{
7575
return claimsPrincipal.Claims.GetClaim(keys);
7676
}
@@ -81,7 +81,7 @@ public static string GetClaim(this ClaimsPrincipal claimsPrincipal, params strin
8181
/// <param name="claims"><see cref="IEnumerable{Claim}"/></param>
8282
/// <param name="keys">Claim Keys (in order of importance)</param>
8383
/// <returns>Claim string</returns>
84-
public static string GetClaim(this IEnumerable<Claim> claims, params string[] keys)
84+
public static string? GetClaim(this IEnumerable<Claim> claims, params string[] keys)
8585
{
8686
if (keys == null || !keys.Any()) { return string.Empty; }
8787

@@ -92,7 +92,7 @@ public static string GetClaim(this IEnumerable<Claim> claims, params string[] ke
9292
if (claim != default) { return claim.Value; }
9393
}
9494

95-
return string.Empty;
95+
return null;
9696
}
9797

9898
/// <summary>
@@ -126,42 +126,64 @@ public static IEnumerable<string> GetClaims(this IEnumerable<Claim> claims, stri
126126
/// </summary>
127127
/// <param name="claimsPrincipal">Claims Principal</param>
128128
/// <returns></returns>
129-
public static string GetUserId(this ClaimsPrincipal claimsPrincipal)
129+
public static string? GetUserId(this ClaimsPrincipal claimsPrincipal)
130+
{
131+
return claimsPrincipal.Claims.GetUserId();
132+
}
133+
134+
/// <summary>
135+
/// Get User ID from claims
136+
/// </summary>
137+
/// <param name="claimsPrincipal">Claims Principal</param>
138+
/// <returns></returns>
139+
public static string? GetUserId(this IEnumerable<Claim> claims)
130140
{
131141
// try to find the user based on the object id (if exists)
132-
var userId = claimsPrincipal.Claims.SingleOrDefault(c => c.Type == "sub")?.Value.ToLower();
133-
if (userId == null) { userId = claimsPrincipal.Claims.SingleOrDefault(c => c.Type == "oid")?.Value.ToLower(); }
142+
var userId = claims.SingleOrDefault(c => c.Type == "sub")?.Value.ToLower();
143+
if (userId == null) { userId = claims.SingleOrDefault(c => c.Type == "oid")?.Value.ToLower(); }
134144

135145
userId = userId?.Trim().ToLower();
136146

137-
return userId ?? string.Empty;
147+
return userId;
138148
}
139149

150+
140151
/// <summary>
141152
/// Get Username from claims
142153
/// </summary>
143154
/// <param name="claimsPrincipal">Claims Principal</param>
144155
/// <returns></returns>
145-
public static string GetUsername(this ClaimsPrincipal claimsPrincipal)
156+
public static string? GetUsername(this ClaimsPrincipal claimsPrincipal)
157+
{
158+
return claimsPrincipal.Claims.GetUsername();
159+
}
160+
161+
/// <summary>
162+
/// Get Username from claims
163+
/// </summary>
164+
/// <param name="claimsPrincipal">Claims Principal</param>
165+
/// <returns></returns>
166+
public static string? GetUsername(this IEnumerable<Claim> claims)
146167
{
147168
// find a unique username to use
148-
var username = claimsPrincipal.Claims.FirstOrDefault(x => x.Type == "unique_name")?.Value;
149-
if (username == default) { username = claimsPrincipal.Claims.FirstOrDefault(x => x.Type == "preferred_username")?.Value; }
150-
if (username == default) { username = claimsPrincipal.Claims.FirstOrDefault(x => x.Type == "upn")?.Value; }
151-
if (username == default) { username = claimsPrincipal.Claims.FirstOrDefault(x => x.Type == "username")?.Value; }
152-
if (username == default) { username = claimsPrincipal.Claims.FirstOrDefault(x => x.Type == "email")?.Value; }
169+
var username = claims.FirstOrDefault(x => x.Type == "unique_name")?.Value;
170+
if (username == default) { username = claims.FirstOrDefault(x => x.Type == "preferred_username")?.Value; }
171+
if (username == default) { username = claims.FirstOrDefault(x => x.Type == "upn")?.Value; }
172+
if (username == default) { username = claims.FirstOrDefault(x => x.Type == "username")?.Value; }
173+
if (username == default) { username = claims.FirstOrDefault(x => x.Type == "email")?.Value; }
153174

154175
username = username?.Trim().ToLower();
155176

156-
return username ?? string.Empty;
177+
return username;
157178
}
179+
158180

159181
/// <summary>
160182
/// Get email address from claims
161183
/// </summary>
162184
/// <param name="claimsPrincipal">Claims Principal</param>
163185
/// <returns></returns>
164-
public static string GetEmailAddress(this ClaimsPrincipal claimsPrincipal)
186+
public static string? GetEmailAddress(this ClaimsPrincipal claimsPrincipal)
165187
{
166188
// find a unique username to use
167189
var email = claimsPrincipal.Claims.FirstOrDefault(x => x.Type == "email")?.Value;
@@ -173,22 +195,48 @@ public static string GetEmailAddress(this ClaimsPrincipal claimsPrincipal)
173195

174196
email = email?.Trim().ToLower();
175197

176-
return email ?? string.Empty;
198+
return email;
199+
}
200+
201+
public static string? GetEmailAddress(this IEnumerable<Claim> claims)
202+
{
203+
// find a unique username to use
204+
var email = claims.FirstOrDefault(x => x.Type == "email")?.Value;
205+
if (email == default)
206+
{
207+
var username = claims.GetUsername();
208+
if (IsValidEmail(username)) { email = username; }
209+
}
210+
211+
email = email?.Trim().ToLower();
212+
213+
return email;
177214
}
178215

216+
179217
/// <summary>
180218
/// Get first and last name
181219
/// </summary>
182220
/// <param name="claimsPrincipal">Claims Principal</param>
183221
/// <returns></returns>
184-
public static (string, string) GetNames(this ClaimsPrincipal claimsPrincipal)
222+
public static (string?, string?) GetNames(this ClaimsPrincipal claimsPrincipal)
185223
{
186-
var givenName = claimsPrincipal.Claims.FirstOrDefault(x => x.Type == "given_name")?.Value;
187-
var familyName = claimsPrincipal.Claims.FirstOrDefault(x => x.Type == "family_name")?.Value;
224+
return claimsPrincipal.Claims.GetNames();
225+
}
226+
227+
/// <summary>
228+
/// Get first and last name
229+
/// </summary>
230+
/// <param name="claimsPrincipal">Claims Principal</param>
231+
/// <returns></returns>
232+
public static (string?, string?) GetNames(this IEnumerable<Claim> claims)
233+
{
234+
var givenName = claims.FirstOrDefault(x => x.Type == "given_name")?.Value;
235+
var familyName = claims.FirstOrDefault(x => x.Type == "family_name")?.Value;
188236

189237
if (string.IsNullOrEmpty(givenName) && string.IsNullOrEmpty(familyName))
190238
{
191-
var fullName = claimsPrincipal.Claims.FirstOrDefault(x => x.Type == "name")?.Value;
239+
var fullName = claims.FirstOrDefault(x => x.Type == "name")?.Value;
192240
if (fullName != default)
193241
{
194242
int spacePos = fullName.IndexOf(' ');
@@ -204,20 +252,32 @@ public static (string, string) GetNames(this ClaimsPrincipal claimsPrincipal)
204252
givenName = givenName?.Trim();
205253
familyName = familyName?.Trim();
206254

207-
return (
208-
givenName ?? string.Empty,
209-
familyName ?? string.Empty
210-
);
255+
return (givenName, familyName);
256+
}
257+
258+
259+
/// <summary>
260+
/// Get a display name based on given_name and family_name
261+
/// </summary>
262+
/// <param name="claimsPrincipal">Claims Principal</param>
263+
/// <returns></returns>
264+
public static string? GetDisplayName(this ClaimsPrincipal claimsPrincipal)
265+
{
266+
return claimsPrincipal.Claims.GetDisplayName();
211267
}
212268

213269
/// <summary>
214270
/// Get a display name based on given_name and family_name
215271
/// </summary>
216272
/// <param name="claimsPrincipal">Claims Principal</param>
217273
/// <returns></returns>
218-
public static string GetDisplayName(this ClaimsPrincipal claimsPrincipal)
274+
public static string? GetDisplayName(this IEnumerable<Claim> claims)
219275
{
220-
var (givenName, familyName) = claimsPrincipal.GetNames();
276+
var name = claims.GetClaim("name");
277+
if (!string.IsNullOrEmpty(name)) { return name; }
278+
279+
var (givenName, familyName) = claims.GetNames();
280+
if (givenName == null && familyName == null) { return null; }
221281

222282
return $"{givenName} {familyName}".Trim();
223283
}
@@ -228,24 +288,37 @@ public static string GetDisplayName(this ClaimsPrincipal claimsPrincipal)
228288
/// </summary>
229289
/// <param name="claimsPrincipal">Claims Principal</param>
230290
/// <returns></returns>
231-
public static string GetPhoneNumber(this ClaimsPrincipal claimsPrincipal)
291+
public static string? GetPhoneNumber(this ClaimsPrincipal claimsPrincipal)
292+
{
293+
return claimsPrincipal.Claims.GetPhoneNumber();
294+
}
295+
296+
/// <summary>
297+
/// Get Phone Number from claims
298+
/// </summary>
299+
/// <param name="claimsPrincipal">Claims Principal</param>
300+
/// <returns></returns>
301+
public static string? GetPhoneNumber(this IEnumerable<Claim> claims)
232302
{
233-
var phoneNumber = claimsPrincipal.Claims.FirstOrDefault(x => x.Type == "phoneNumber")?.Value;
234-
if (phoneNumber == default) { phoneNumber = claimsPrincipal.Claims.FirstOrDefault(x => x.Type == "phone")?.Value; }
303+
var phoneNumber = claims.FirstOrDefault(x => x.Type == "phoneNumber")?.Value;
304+
if (phoneNumber == default) { phoneNumber = claims.FirstOrDefault(x => x.Type == "phone")?.Value; }
235305

236306
// we don't want to deal with nulls
237307
phoneNumber = phoneNumber?.Trim();
238308

239-
return phoneNumber ?? string.Empty;
309+
return phoneNumber;
240310
}
241311

312+
242313
/// <summary>
243314
/// Check if the email address is valid
244315
/// </summary>
245316
/// <param name="email"></param>
246317
/// <returns></returns>
247318
private static bool IsValidEmail(string email)
248319
{
320+
if (string.IsNullOrEmpty(email)) { return false; }
321+
249322
var trimmedEmail = email.Trim();
250323

251324
if (trimmedEmail.EndsWith(".")) { return false; }

0 commit comments

Comments
 (0)