forked from NpgsqlRest/NpgsqlRest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtensions.cs
More file actions
287 lines (262 loc) · 8.86 KB
/
Copy pathExtensions.cs
File metadata and controls
287 lines (262 loc) · 8.86 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
using System.Security.Claims;
using System.Text;
using Npgsql;
using NpgsqlRest.Auth;
namespace NpgsqlRest;
public static class Ext
{
public static T Get<T>(this NpgsqlDataReader reader, int ordinal)
{
object? value;
if (typeof(T) == typeof(short?[]))
{
value = reader.GetFieldValue<short?[]>(ordinal);
}
else
{
value = reader[ordinal];
}
if (value == DBNull.Value)
{
return default!;
}
// strange bug single char representing as string on older pg versions when using functions
if (typeof(T) == typeof(char) && value.GetType() == typeof(string))
{
if (value is null)
{
return default!;
}
object c = ((string)value)[0];
return (T)c;
}
return (T)value;
}
public static T GetEnum<T>(this string? value) where T : struct
{
Enum.TryParse<T>(value, true, out var result);
// return the first enum (Other) when no match
return result;
}
public static bool IsTypeOf(this Claim claim, string type)
{
return string.Equals(claim.Type, type, StringComparison.OrdinalIgnoreCase);
}
public static object GetClaimDbParam(this Dictionary<string, object> dict, string key)
{
if (dict.TryGetValue(key, out var value))
{
if (value is null)
{
return DBNull.Value;
}
return value;
}
return DBNull.Value;
}
public static object GetClaimDbContextParam(this Dictionary<string, object> dict, string key)
{
object value = dict.GetClaimDbParam(key);
if (value == DBNull.Value || value is string)
{
return value;
}
var list = value as List<string>;
StringBuilder sb = new(100);
sb.Append('{');
for (int i = 0; i < list?.Count; i++)
{
if (i > 0)
{
sb.Append(',');
}
sb.Append(PgConverters.SerializeString(list[i]));
}
sb.Append('}');
return sb.ToString();
}
public static Dictionary<string, object> BuildClaimsDictionary(this ClaimsPrincipal? user, NpgsqlRestAuthenticationOptions options)
{
Dictionary<string, object> claimValues = [];
if (user is null || user.Claims is null)
{
return claimValues;
}
foreach (var claim in user.Claims)
{
if (claimValues.TryGetValue(claim.Type, out var existing))
{
if (existing is List<string> list)
{
list.Add(claim.Value);
}
else
{
var newList = new List<string>(4) { (string)existing, claim.Value };
claimValues[claim.Type] = newList;
}
}
else
{
if (claim.IsTypeOf(options.DefaultRoleClaimType))
{
claimValues[claim.Type] = new List<string> { claim.Value };
}
else
{
claimValues[claim.Type] = claim.Value;
}
}
}
return claimValues;
}
public static object GetUserClaimsDbParam(this ClaimsPrincipal user, Dictionary<string, object> claimValues)
{
if (user is null || claimValues is null || claimValues.Count == 0)
{
return "{}";
}
int estimatedCapacity = 2 + (claimValues.Count * 10);
foreach (var entry in claimValues)
{
estimatedCapacity += entry.Key.Length * 2;
if (entry.Value is List<string> list)
{
estimatedCapacity += 2;
foreach (var value in list)
{
estimatedCapacity += value.Length * 2 + 3;
}
}
else
{
estimatedCapacity += ((string)entry.Value).Length * 2 + 2;
}
}
StringBuilder sb = new(estimatedCapacity);
sb.Append('{');
int i = 0;
foreach (var entry in claimValues)
{
if (i > 0)
{
sb.Append(',');
}
sb.Append(PgConverters.SerializeString(entry.Key));
sb.Append(':');
if (entry.Value is List<string> values)
{
sb.Append('[');
for (int j = 0; j < values.Count; j++)
{
if (j > 0)
{
sb.Append(',');
}
sb.Append(PgConverters.SerializeString(values[j]));
}
sb.Append(']');
}
else
{
sb.Append(PgConverters.SerializeString((string)entry.Value));
}
i++;
}
sb.Append('}');
return sb.ToString();
}
public static string? GetClientIpAddress(this HttpRequest request)
{
// Check X-Forwarded-For header
var forwardedIp = request.Headers["X-Forwarded-For"].FirstOrDefault();
if (!string.IsNullOrEmpty(forwardedIp))
{
int commaIndex = forwardedIp.IndexOf(',');
return commaIndex > 0 ? forwardedIp[..commaIndex].Trim() : forwardedIp.Trim();
}
// Check other headers with null-coalescing operator
var ip = request.Headers["X-Real-IP"].FirstOrDefault()
?? request.Headers["HTTP_X_FORWARDED_FOR"].FirstOrDefault()
?? request.Headers["REMOTE_ADDR"].FirstOrDefault();
return !string.IsNullOrEmpty(ip) ? ip : request.HttpContext.Connection.RemoteIpAddress?.ToString();
}
public static object GetClientIpAddressDbParam(this HttpRequest request)
{
return request.GetClientIpAddress() as object ?? DBNull.Value;
}
private const string Info = "INFO";
private const string Notice = "NOTICE";
private const string Warning = "WARNING";
public static bool IsInfo(this PostgresNotice notice)
{
return string.Equals(notice.Severity, Info, StringComparison.OrdinalIgnoreCase);
}
public static bool IsNotice(this PostgresNotice notice)
{
return string.Equals(notice.Severity, Notice, StringComparison.OrdinalIgnoreCase);
}
public static bool IsWarning(this PostgresNotice notice)
{
return string.Equals(notice.Severity, Warning, StringComparison.OrdinalIgnoreCase);
}
public static bool? ParameterEnabled(this Dictionary<string, string>? parameters, string key)
{
if (parameters is null || parameters.Count == 0)
{
return null;
}
if (parameters.TryGetValue(key, out var value))
{
// Check for "off" values
if (string.Equals(value, "false", StringComparison.OrdinalIgnoreCase) ||
string.Equals(value, "off", StringComparison.OrdinalIgnoreCase) ||
string.Equals(value, "disabled", StringComparison.OrdinalIgnoreCase) ||
string.Equals(value, "disable", StringComparison.OrdinalIgnoreCase) ||
string.Equals(value, "0", StringComparison.OrdinalIgnoreCase))
{
return false;
}
// Check for "on" values
if (string.Equals(value, "true", StringComparison.OrdinalIgnoreCase) ||
string.Equals(value, "on", StringComparison.OrdinalIgnoreCase) ||
string.Equals(value, "enabled", StringComparison.OrdinalIgnoreCase) ||
string.Equals(value, "enable", StringComparison.OrdinalIgnoreCase) ||
string.Equals(value, "1", StringComparison.OrdinalIgnoreCase))
{
return true;
}
return null;
}
return null;
}
public static void TraceCommand(this ILogger? logger, NpgsqlCommand command, string name)
{
if (logger?.IsEnabled(LogLevel.Trace) is true && logger is not null)
{
StringBuilder sb = new();
for (int i = 0; i < command.Parameters.Count; i++)
{
sb.Append('$');
sb.Append(i+1);
sb.Append("=");
sb.Append(PgConverters.SerializeDatbaseObject(command.Parameters[i].Value));
sb.Append('\n');
}
sb.Append(command.CommandText);
logger?.LogTrace("{name}:\n{query}", name, sb.ToString());
}
}
public static bool IsSsl(this HttpRequest request)
{
if (request.IsHttps)
{
return true;
}
if (string.Equals(request.Scheme, "https", StringComparison.OrdinalIgnoreCase))
{
return true;
}
return false;
}
}