forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClaimUtils.cs
More file actions
77 lines (65 loc) · 3.48 KB
/
Copy pathClaimUtils.cs
File metadata and controls
77 lines (65 loc) · 3.48 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
#nullable enable
using System;
using System.Linq;
using System.Security.Claims;
namespace ServiceStack;
public static class ClaimUtils
{
public static string Admin { get; set; } = nameof(Admin);
public static string PermissionType { get; set; } = JwtClaimTypes.Permission;
public static string Picture { get; set; } = JwtClaimTypes.Picture;
public static bool IsAuthenticated(this ClaimsPrincipal? principal) => principal?.Identity?.IsAuthenticated == true;
public static ClaimsPrincipal? AuthenticatedUser(this ClaimsPrincipal? principal) =>
principal?.Identity?.IsAuthenticated == true ? principal : null;
public static bool IsAdmin(this ClaimsPrincipal? principal) => principal?.IsInRole(Admin) == true;
public static string? GetUserName(this ClaimsPrincipal? principal) => principal?.Identity?.Name;
public static string? GetUserId(this ClaimsPrincipal? principal) => principal?.FindFirst(ClaimTypes.NameIdentifier)?.Value;
public static string? GetDisplayName(this ClaimsPrincipal? principal) =>
principal?.FindFirst(JwtClaimTypes.NickName)?.Value ??
principal?.FindFirst(principal.Identities.FirstOrDefault()?.NameClaimType ?? ClaimTypes.Name)?.Value ??
principal?.Identity?.Name;
public static string? GetEmail(this ClaimsPrincipal principal) => principal.FindFirst(ClaimTypes.Email)?.Value;
public static string[] GetRoles(this ClaimsPrincipal? principal) => principal?.Claims.Where(x => x.Type == ClaimTypes.Role)
.Select(x => x.Value).ToArray() ?? [];
public static string[] GetPermissions(this ClaimsPrincipal? principal) => principal?.Claims.Where(x => x.Type == PermissionType)
.Select(x => x.Value).ToArray() ?? [];
public static bool HasRole(this ClaimsPrincipal? principal, string roleName) => principal?.IsInRole(roleName) == true;
public static bool HasAllRoles(this ClaimsPrincipal? principal, params string[] roleNames) => principal?.GetRoles()
.All(roleNames.Contains) == true;
public static bool HasScope(this ClaimsPrincipal? principal, string scope) => principal?.HasClaim(JwtClaimTypes.Scope, scope) == true;
public static bool HasClaim(this ClaimsPrincipal? principal, string type, string value)
{
foreach (var claim in principal?.Claims ?? Array.Empty<Claim>())
{
if (claim.Type == type && claim.Value == value)
return true;
}
return false;
}
public static string? GetPicture(this ClaimsPrincipal? principal) =>
X.Map(principal?.FindFirst(Picture)?.Value, x => string.IsNullOrWhiteSpace(x) ? null : x)
?? JwtClaimTypes.DefaultProfileUrl;
public static AuthenticateResponse? ToAuthenticateResponse(this ClaimsPrincipal? user, Action<AuthenticateResponse>? configure=null)
{
if (user?.Identity is not { IsAuthenticated: true })
return null;
var sub = user.FindFirst(JwtClaimTypes.Subject)?.Value;
var id = user.FindFirst(ClaimTypes.NameIdentifier)?.Value;
var to = new AuthenticateResponse
{
UserId = sub ?? id, // sub can override Id
UserName = user.GetUserName(),
DisplayName = user.GetDisplayName(),
ProfileUrl = user.GetPicture(),
Roles = [..user.GetRoles()],
};
if (sub != null && id != null)
{
to.Meta = new() {
["Id"] = user.GetUserId(),
};
}
configure?.Invoke(to);
return to;
}
}