1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- using AipGateway.API.Application.Modules;
- using Microsoft.AspNetCore.Http;
- using Microsoft.Extensions.Logging;
- namespace AipGateway.API.Application.Pipeline.Middlewares
- {
- public class ApiValidationMiddleware
- {
- private readonly ILogger<ApiValidationMiddleware> _log;
- private readonly RequestDelegate _next;
- public ApiValidationMiddleware(ILogger<ApiValidationMiddleware> logger, RequestDelegate next)
- {
- _log = logger;
- _next = next;
- _log.LogError("wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww: ApiValidationMiddleware");
- }
- public async Task InvokeAsync(HttpContext context)
- {
- _log.LogError("wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww: ApiValidationMiddleware, {0}, {1}",
- context.Connection.RemoteIpAddress, context.Request.Path);
- //request.Method == HttpMethod.Get
- string requestUrl = context.Request.Path;
- if (requestUrl.Contains(GlobalConstants.API_ENDPOINT))
- {
- using (var reader = new StreamReader(context.Request.Body))
- {
- //var guid = context.Items[GlobalConstants.API_GUID];
- //int result = 9;
- //string errMsg = "API KEY를 찾을 수 없습니다.";
- //string? ipAddress = context.Connection.RemoteIpAddress?.ToString();
- //var requestBody = await reader.ReadToEndAsync();
- //var requestModel = JsonConvert.DeserializeObject<RequestModel>(requestBody);
- //if (requestModel != null)
- //{
- // result = ValidationApiKey(requestModel.apiKey, ipAddress, out errMsg);
- // result = 0;
- //}
- await _next(context);
- await Task.FromResult(Task.CompletedTask);
- }
- }
- else
- {
- await _next(context);
- await Task.FromResult(Task.CompletedTask);
- }
- return;
- }
- #if false
- private int ValidationApiKey(string key, string? ipAddress, out string errMsg)
- {
- ipAddress = string.IsNullOrEmpty(ipAddress) ? "" : ipAddress;
- try
- {
- if (ContainerService.apiKeyMap.ContainsKey(key)) {
- LinkedApiKey? apiKey = ContainerService.apiKeyMap[key] as LinkedApiKey;
- if (apiKey == null)
- {
- errMsg = "등록되어 있지 않은 API KEY 입니다.";
- return 1; // 등록되어 있지 않은 키 정보입니다.
- }
- if (DateTime.Compare(apiKey.ExpiredAt, DateTime.Now) < 0)
- {
- errMsg = " API KEY 유효기간이 지났습니다.";
- return 2; // 등록된 키의 유효기간이 지났습니다.
- }
- if (apiKey.serverMap.Contains(ipAddress))
- {
- errMsg = "성공.";
- return 0; // 유효한 키값과 IP Address
- }
- errMsg = "등록되어 있지 않은 IP Address에서 호출된 API KEY 입니다.";
- return 3; // 유효한 키값이나 등록되지 않은 IP Address
- }
- else
- {
- errMsg = "등록되어 있지 않은 API KEY 입니다.";
- return 1; // 등록되어 있지 않은 키 정보입니다.
- }
- }
- catch (Exception ex)
- {
- _log.LogError("wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww: ApiValidationMiddleware: AFTER {0}", ex.Message);
- errMsg = ex.Message;
- return 10;
- }
- }
- #endif
- }
- }
|