-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAppendClaimsToMessageHeaders.cs
105 lines (85 loc) · 3.01 KB
/
AppendClaimsToMessageHeaders.cs
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
using Microsoft.AspNetCore.Http;
using Newtonsoft.Json;
// ReSharper disable UnusedVariable
public class AppendClaimsToMessageHeaders
{
public static void WithPrefix(IServiceCollection services)
{
#region AppendClaimsToMessageHeaders_WithPrefix
var configuration = new PassthroughConfiguration(
connectionFunc: OpenConnection,
callback: Callback,
dedupCriticalError: exception =>
Environment.FailFast("Dedup cleanup failure", exception));
configuration.AppendClaimsToMessageHeaders(headerPrefix: "Claim.");
services.AddSqlHttpPassthrough(configuration);
#endregion
}
public static void Default(IServiceCollection services)
{
#region AppendClaimsToMessageHeaders
var configuration = new PassthroughConfiguration(
connectionFunc: OpenConnection,
callback: Callback,
dedupCriticalError: exception =>
{
Environment.FailFast("Dedup cleanup failure", exception);
});
configuration.AppendClaimsToMessageHeaders();
services.AddSqlHttpPassthrough(configuration);
#endregion
}
public static void AppendClaimsToDictionary(Dictionary<string, string> headerDictionary)
{
#region AppendClaimsToDictionary
var claims = new List<Claim>
{
new(ClaimTypes.Email, "User@foo.bar"),
new(ClaimTypes.NameIdentifier, "User1"),
new(ClaimTypes.NameIdentifier, "User2")
};
ClaimsAppender.Append(claims, headerDictionary, "prefix.");
#endregion
}
public static void ExtractClaimsFromDictionary(Dictionary<string, string> headerDictionary)
{
#region ExtractClaimsFromDictionary
var claimsList = ClaimsAppender.Extract(headerDictionary, "prefix.");
#endregion
}
#region ClaimsRaw
public static void Append(
IEnumerable<Claim> claims,
IDictionary<string, string> headers, string prefix)
{
foreach (var claim in claims.GroupBy(_ => _.Type))
{
var items = claim.Select(_ => _.Value);
headers.Add(prefix + claim.Key, JsonConvert.SerializeObject(items));
}
}
public static IEnumerable<Claim> Extract(
IDictionary<string, string> headers,
string prefix)
{
foreach (var header in headers)
{
var key = header.Key;
if (!key.StartsWith(prefix))
{
continue;
}
key = key.Substring(prefix.Length, key.Length - prefix.Length);
var list = JsonConvert.DeserializeObject<List<string>>(header.Value)!;
foreach (var value in list)
{
yield return new(key, value);
}
}
}
#endregion
static Task<Table> Callback(HttpContext httpContext, PassthroughMessage passthroughMessage) =>
null!;
static Task<SqlConnection> OpenConnection(Cancel cancel) =>
null!;
}