-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathDefaultResponseParser.cs
More file actions
79 lines (75 loc) · 2.7 KB
/
Copy pathDefaultResponseParser.cs
File metadata and controls
79 lines (75 loc) · 2.7 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
using Microsoft.AspNetCore.Antiforgery;
using NpgsqlRest;
using NpgsqlRest.Auth;
namespace NpgsqlRestClient;
public class DefaultResponseParser(
NpgsqlRestAuthenticationOptions options,
string? antiforgeryFieldNameTag,
string? antiforgeryTokenTag,
string[]? availableClaimTypes)
{
public ReadOnlySpan<char> Parse(
ReadOnlySpan<char> input,
HttpContext context,
AntiforgeryTokenSet? tokenSet)
{
Dictionary<string, string> replacements = [];
HashSet<string> arrayTypes = new(10)
{
options.DefaultRoleClaimType
};
foreach (var claim in context.User.Claims)
{
if (replacements.TryGetValue(claim.Type, out var existingValue))
{
replacements[claim.Type] = string.Concat(existingValue, ",", PgConverters.SerializeString(claim.Value));
if (arrayTypes.Contains(claim.Type) is false)
{
arrayTypes.Add(claim.Type);
}
}
else
{
replacements.Add(claim.Type, PgConverters.SerializeString(claim.Value));
}
}
foreach (var item in arrayTypes)
{
if (replacements.TryGetValue(item, out var existingValue) is true)
{
replacements[item] = string.Concat(Consts.OpenBracket, existingValue, Consts.CloseBracket);
}
else
{
replacements[item] = string.Concat(Consts.OpenBracket, Consts.CloseBracket);
}
}
if (replacements.ContainsKey(options.DefaultUserIdClaimType) is false)
{
replacements.Add(options.DefaultUserIdClaimType, Consts.Null);
}
if (replacements.ContainsKey(options.DefaultNameClaimType) is false)
{
replacements.Add(options.DefaultNameClaimType, Consts.Null);
}
if (tokenSet is not null && (antiforgeryFieldNameTag is not null || antiforgeryTokenTag is not null))
{
if (antiforgeryFieldNameTag is not null)
{
replacements.Add(antiforgeryFieldNameTag, tokenSet.FormFieldName);
}
if (antiforgeryTokenTag is not null && tokenSet.RequestToken is not null)
{
replacements.Add(antiforgeryTokenTag, tokenSet.RequestToken);
}
}
if (availableClaimTypes is not null && availableClaimTypes.Length > 0)
{
for (int i = 0; i < availableClaimTypes.Length; i++)
{
replacements.TryAdd(availableClaimTypes[i], Consts.Null);
}
}
return Formatter.FormatString(input, replacements);
}
}