-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAPI.Common.cs
More file actions
326 lines (274 loc) · 11.3 KB
/
Copy pathAPI.Common.cs
File metadata and controls
326 lines (274 loc) · 11.3 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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
using Microsoft.AspNetCore.Http;
using Newtonsoft.Json;
using System.Collections.Specialized;
using System.DirectoryServices.AccountManagement;
using System.Dynamic;
using System.Net;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
namespace API
{
/// <summary>
/// Static implementation of the API Constants
/// </summary>
public class Common
{
/// <summary>
/// the type of middleware that the request is.
/// </summary>
public string middlewareType = null;
/// <summary>
/// HTTP POST Request
/// </summary>
internal string httpPOST = null;
/// <summary>
/// HTTP GET Request
/// </summary>
internal NameValueCollection httpGET = null;
/// <summary>
/// last time a request was sent for possible performance monitoring
/// </summary>
internal static DateTime lastRequestTime;
/// <summary>
/// number of requests
/// </summary>
internal static int requestCount = 0;
public Common()
{
}
/// <summary>
/// Get the HTTP request for the GET method
/// </summary>
/// <returns></returns>
internal static NameValueCollection GetHttpGET(HttpContext httpContext)
{
try
{
// Read the request from GET
return HttpUtility.ParseQueryString(httpContext.Request.QueryString.Value);
}
catch (Exception e)
{
Log.Instance.Info(e);
return null;
}
}
/// <summary>
/// Get the HTTP request for the POST method
/// </summary>
/// <returns></returns>
internal async Task<string> GetHttpPOST(HttpContext httpContext)
{
try
{
// Read the request from POST
//https://stackoverflow.com/questions/43403941/how-to-read-asp-net-core-response-body
string body;
using (var streamReader = new System.IO.StreamReader(
httpContext.Request.Body, System.Text.Encoding.UTF8, leaveOpen: true))
body = await streamReader.ReadToEndAsync();
httpContext.Request.Body.Position = 0;
return body;// httpContext.Request.Form.Keys.FirstOrDefault();
//new StreamReader(httpContext.Request.Body).ReadToEnd();
}
catch (Exception e)
{
Log.Instance.Info(e);
return null;
}
}
/// <summary>
/// Mask an input password
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public string MaskParameters(string input)
{
if (string.IsNullOrEmpty(input))
{
return "";
}
// Init the output
string output = input;
// Loop trough the parameters to mask API_MASK_PARAMETERS
foreach (var param in ApiServicesHelper.ApiConfiguration.Settings["API_MASK_PARAMETERS"].Split(','))
{
// https://stackoverflow.com/questions/171480/regex-grabbing-values-between-quotation-marks
Log.Instance.Info("Masked parameter: " + param);
output = Regex.Replace(output, "\"" + param + "\"\\s*:\\s*\"(.*?[^\\\\])\"", "\"" + param + "\": \"********\"", RegexOptions.IgnoreCase);
}
return output;
}
/// <summary>
/// manage responses to client
/// </summary>
/// <param name="context"></param>
/// <param name="message"></param>
/// <param name="sourceToken"></param>
/// <param name="statusCode"></param>
/// <param name="isFile"></param>
internal async Task returnResponseAsync(HttpContext context, string message, CancellationTokenSource sourceToken, HttpStatusCode statusCode, MemoryStream memoryStream = null)
{
Log.Instance.Info("Returning response");
//check if already cancelled
if (sourceToken.IsCancellationRequested)
{
throw new OperationCanceledException();
}
if (context.Response.HasStarted)
{
sourceToken.Cancel(true);
if (sourceToken.IsCancellationRequested)
{
throw new OperationCanceledException();
}
}
else
{
context.Response.StatusCode = context.Response.StatusCode == 0 ? (int)statusCode : context.Response.StatusCode;
if (memoryStream != null)
{
await memoryStream.CopyToAsync(context.Response.Body, sourceToken.Token);
}
else
{
await context.Response.WriteAsync(message);
}
await context.Response.CompleteAsync();
sourceToken.Cancel(true);
if (sourceToken.IsCancellationRequested)
{
throw new OperationCanceledException();
}
}
}
/// <summary>
/// Determines if cookie is not empty and adds name and value
/// </summary>
/// <param name="httpContext"></param>
internal Cookie CheckCookie(string SessionCookieName, HttpContext httpContext)
{
//add a cookie for testing
//httpContext.Request.Headers.Add("Cookie", "session=\"84c2f0b319460ee991924908198d46795049c83f1ebdfcaf90bd899c8d9d0bd2\";");
Cookie sessionCookie = new Cookie();
if (!string.IsNullOrEmpty(SessionCookieName))
{
//need to create a cookie using the value and the SessionCookieName
string testSessionCookieValue = httpContext.Request.Cookies[SessionCookieName];
if (!string.IsNullOrEmpty(testSessionCookieValue))
{
sessionCookie.Name = SessionCookieName;
sessionCookie.Value = testSessionCookieValue;
}
}
return sessionCookie;
}
internal void GatherTraceInformation(IRequest apiRequest, Trace trace)
{
if (ApiServicesHelper.ApiConfiguration.API_TRACE_ENABLED)
{
Type type = apiRequest.GetType();
if (type == typeof(JSONRPC_API))
{
trace.TrcParams = MaskParameters(apiRequest.parameters.ToString());
}
else if (type == typeof(RESTful_API) || type == typeof(Static_API))
{
//in non jsonrpc its a list of strings
trace.TrcParams = MaskParameters(apiRequest.parameters[0]);
}
//gather trace information
trace.TrcIp = apiRequest.ipAddress;
trace.TrcUseragent = apiRequest.userAgent;
trace.TrcMethod = apiRequest.method;
if (apiRequest.userDetail != null)
{
trace.TrcUsername = apiRequest.userDetail.Identity;
}
}
}
/// <summary>
/// method to check if the an api call is allowed and return the methodinfo if it is
/// </summary>
internal static MethodInfo? CheckAPICallsAllowed(string methodName, string methodPath, dynamic typeOfClassType)
{
//create key for the dictionary
dynamic jsonObj = new ExpandoObject();
jsonObj.methodName = methodName;
jsonObj.methodPath = methodPath;
jsonObj.methodType = typeOfClassType.Name; //Fixes bug where previous RESTful call breaks subsequent JSON-rpc calls and vice versa
string serializedAPIInfo = Utility.JsonSerialize_IgnoreLoopingReference(jsonObj);
//if already in dictionary no need to find again
if (AttributeDictionary.AllowedAPIDictionary.ContainsKey(serializedAPIInfo))
{
//return the methodInfo based on the methodInfo handle
MethodInfo m2 = MethodBase.GetMethodFromHandle(AttributeDictionary.AllowedAPIDictionary[serializedAPIInfo]) as MethodInfo;
return m2;
}else if(!EndpointCache.findApi(methodPath + "." + methodName))
{
//if not in the white list bounce
return null;
}
// Search in the Assemplies
var allAssemblies = EndpointCache.GetListAssemblies();// AppDomain.CurrentDomain.GetAssemblies();
var calledClass = allAssemblies.Select(y => y.GetType(methodPath, false, true)).Where(p => p != null).FirstOrDefault();
if (calledClass != null)
{
if (calledClass.FullName.Trim().Equals(methodPath.Trim()))
{
if (calledClass.CustomAttributes.Where(xx => xx.AttributeType.Name == "AllowAPICall").ToList().Count > 0)
{
MethodInfo methodInfo = null;
methodInfo = calledClass.GetMethod(methodName, new Type[] { typeOfClassType });
if (methodInfo == null)
{
return null;
}
else
{
//get the methods handle
RuntimeMethodHandle handle = methodInfo.MethodHandle;
//add handle to dictionary for future lookup
try
{
if (!AttributeDictionary.AllowedAPIDictionary.TryAdd(serializedAPIInfo, handle))
{
Log.Instance.Debug("Adding : " + serializedAPIInfo + " to dictionary 'CheckAPICallsAllowed' failed");
}
}
catch (Exception ex)
{
Log.Instance.Error(ex);
}
return methodInfo;
}
}
}
}
return null;
}
}
/// <summary>
/// Clone the UserPrincipal object structure for serialisation & deserialisation
/// This is required because of recursive loop
/// </summary>
public class API_UserPrincipal : UserPrincipal
{
/// <summary>
///
/// </summary>
/// <param name="context"></param>
public API_UserPrincipal(PrincipalContext context) : base(context) { }
/// <summary>
///
/// </summary>
/// <param name="context"></param>
/// <param name="samAccountName"></param>
/// <param name="password"></param>
/// <param name="enabled"></param>
[JsonConstructor]
public API_UserPrincipal(PrincipalContext context, string samAccountName, string password, bool enabled) : base(context, samAccountName, password, enabled) { }
}
}