ApiValidationMiddleware.cs 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using AipGateway.API.Application.Modules;
  2. using Microsoft.AspNetCore.Http;
  3. using Microsoft.Extensions.Logging;
  4. namespace AipGateway.API.Application.Pipeline.Middlewares
  5. {
  6. public class ApiValidationMiddleware
  7. {
  8. private readonly ILogger<ApiValidationMiddleware> _log;
  9. private readonly RequestDelegate _next;
  10. public ApiValidationMiddleware(ILogger<ApiValidationMiddleware> logger, RequestDelegate next)
  11. {
  12. _log = logger;
  13. _next = next;
  14. _log.LogError("wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww: ApiValidationMiddleware");
  15. }
  16. public async Task InvokeAsync(HttpContext context)
  17. {
  18. _log.LogError("wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww: ApiValidationMiddleware, {0}, {1}",
  19. context.Connection.RemoteIpAddress, context.Request.Path);
  20. //request.Method == HttpMethod.Get
  21. string requestUrl = context.Request.Path;
  22. if (requestUrl.Contains(GlobalConstants.API_ENDPOINT))
  23. {
  24. using (var reader = new StreamReader(context.Request.Body))
  25. {
  26. //var guid = context.Items[GlobalConstants.API_GUID];
  27. //int result = 9;
  28. //string errMsg = "API KEY를 찾을 수 없습니다.";
  29. //string? ipAddress = context.Connection.RemoteIpAddress?.ToString();
  30. //var requestBody = await reader.ReadToEndAsync();
  31. //var requestModel = JsonConvert.DeserializeObject<RequestModel>(requestBody);
  32. //if (requestModel != null)
  33. //{
  34. // result = ValidationApiKey(requestModel.apiKey, ipAddress, out errMsg);
  35. // result = 0;
  36. //}
  37. await _next(context);
  38. await Task.FromResult(Task.CompletedTask);
  39. }
  40. }
  41. else
  42. {
  43. await _next(context);
  44. await Task.FromResult(Task.CompletedTask);
  45. }
  46. return;
  47. }
  48. #if false
  49. private int ValidationApiKey(string key, string? ipAddress, out string errMsg)
  50. {
  51. ipAddress = string.IsNullOrEmpty(ipAddress) ? "" : ipAddress;
  52. try
  53. {
  54. if (ContainerService.apiKeyMap.ContainsKey(key)) {
  55. LinkedApiKey? apiKey = ContainerService.apiKeyMap[key] as LinkedApiKey;
  56. if (apiKey == null)
  57. {
  58. errMsg = "등록되어 있지 않은 API KEY 입니다.";
  59. return 1; // 등록되어 있지 않은 키 정보입니다.
  60. }
  61. if (DateTime.Compare(apiKey.ExpiredAt, DateTime.Now) < 0)
  62. {
  63. errMsg = " API KEY 유효기간이 지났습니다.";
  64. return 2; // 등록된 키의 유효기간이 지났습니다.
  65. }
  66. if (apiKey.serverMap.Contains(ipAddress))
  67. {
  68. errMsg = "성공.";
  69. return 0; // 유효한 키값과 IP Address
  70. }
  71. errMsg = "등록되어 있지 않은 IP Address에서 호출된 API KEY 입니다.";
  72. return 3; // 유효한 키값이나 등록되지 않은 IP Address
  73. }
  74. else
  75. {
  76. errMsg = "등록되어 있지 않은 API KEY 입니다.";
  77. return 1; // 등록되어 있지 않은 키 정보입니다.
  78. }
  79. }
  80. catch (Exception ex)
  81. {
  82. _log.LogError("wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww: ApiValidationMiddleware: AFTER {0}", ex.Message);
  83. errMsg = ex.Message;
  84. return 10;
  85. }
  86. }
  87. #endif
  88. }
  89. }