ApiAuthService.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. using AipGateway.API.Application;
  2. using AipGateway.API.Domain.Entities;
  3. using AipGateway.API.Repositories;
  4. using AipGateway.API.Services.Interfaces;
  5. using System.Collections;
  6. namespace AipGateway.API.Services
  7. {
  8. public class ApiAuthService : IApiAuthService
  9. {
  10. private readonly ILogger<ApiAuthService> _log;
  11. private readonly IAipDbRepository _repo;
  12. private Hashtable _apiKeyMap = new Hashtable();
  13. private Hashtable _decryptKeyMap = new Hashtable();
  14. public ApiAuthService(ILogger<ApiAuthService> log, IAipDbRepository repo)
  15. {
  16. _log = log;
  17. _repo = repo;
  18. Inititialize();
  19. }
  20. private void Inititialize()
  21. {
  22. LoadLinkedApiKeys();
  23. LoadLinkedDecryptKeys();
  24. }
  25. private bool CheckApiRights(LinkedApiKey keyInfo, int apiId)
  26. {
  27. if (keyInfo == null) return false;
  28. // 레이블 조회 및 템플릿 조회 API 허용 여부
  29. // 파일 정보 조회 API 허용 여부
  30. // 레이블 적용 API 허용 여부
  31. // 레이블 해제 API 허용 여부
  32. // 파일 암호화 API 허용 여부
  33. // 파일 복호화 API 허용 여부
  34. if (apiId >= GlobalConstants.API_DB_RELOAD && apiId <= GlobalConstants.API_DB_LINKED_DECRYPT_KEYS)
  35. {
  36. return keyInfo.policyLookupYn;
  37. }
  38. if (apiId == GlobalConstants.API_AIP_DOWNLOAD ||
  39. apiId == GlobalConstants.API_AIP_LABELS ||
  40. apiId == GlobalConstants.API_AIP_POLICIES ||
  41. apiId == GlobalConstants.API_AIP_PROTECTIONS ||
  42. apiId == GlobalConstants.API_FILE_INFO ||
  43. apiId == GlobalConstants.API_STREAM_INFO)
  44. {
  45. return keyInfo.fileInfoLookupYn;
  46. }
  47. if (apiId == GlobalConstants.API_FILE_SET_LABEL ||
  48. apiId == GlobalConstants.API_FILE_SET_LABELS ||
  49. apiId == GlobalConstants.API_STREAM_SET_LABEL ||
  50. apiId == GlobalConstants.API_STREAM_SET_LABELS)
  51. {
  52. return keyInfo.applyLabelYn;
  53. }
  54. if (apiId == GlobalConstants.API_FILE_DELETE_LABEL ||
  55. apiId == GlobalConstants.API_FILE_DELETE_LABELS ||
  56. apiId == GlobalConstants.API_STREAM_DELETE_LABEL ||
  57. apiId == GlobalConstants.API_STREAM_DELETE_LABELS)
  58. {
  59. return keyInfo.releaseLabelYn;
  60. }
  61. if (apiId == GlobalConstants.API_FILE_SET_PROTECTION ||
  62. apiId == GlobalConstants.API_STREAM_SET_PROTECTION ||
  63. apiId == GlobalConstants.API_FILE_ENCRYPT ||
  64. apiId == GlobalConstants.API_STREAM_ENCRYPT)
  65. {
  66. return keyInfo.encryptionFileYn;
  67. }
  68. if (apiId == GlobalConstants.API_FILE_DELETE_PROTECTIN ||
  69. apiId == GlobalConstants.API_STREAM_DELETE_PROTECTIN ||
  70. apiId == GlobalConstants.API_FILE_DECRYPT ||
  71. apiId == GlobalConstants.API_STREAM_DECRYPT)
  72. {
  73. return keyInfo.decryptionFileYn;
  74. }
  75. if (apiId == GlobalConstants.API_FILE_SET_LABEL_PROTECTION ||
  76. apiId == GlobalConstants.API_STREAM_SET_LABEL_PROTECTION)
  77. {
  78. return keyInfo.applyLabelYn && keyInfo.encryptionFileYn;
  79. }
  80. if (apiId == GlobalConstants.API_FILE_DELETE_LABEL_PROTECTION ||
  81. apiId == GlobalConstants.API_STREAM_DELETE_LABEL_PROTECTION)
  82. {
  83. return keyInfo.releaseLabelYn && keyInfo.decryptionFileYn;
  84. }
  85. if (apiId == GlobalConstants.API_DUMMY)
  86. {
  87. return true;
  88. }
  89. return false;
  90. }
  91. public int CheckApiKeyValidation(HttpContext httpContext, string apiKey, int apiId)
  92. {
  93. string errMsg;
  94. string? ipAddress = httpContext.Connection.RemoteIpAddress?.ToString();
  95. ipAddress = string.IsNullOrEmpty(ipAddress) ? "xx.xx.xx.xx" : ipAddress;
  96. try
  97. {
  98. if (_apiKeyMap.ContainsKey(apiKey))
  99. {
  100. LinkedApiKey? apiKeyData = _apiKeyMap[apiKey] as LinkedApiKey;
  101. if (apiKeyData == null)
  102. {
  103. return 1; // 등록되어 있지 않은 키 정보입니다.
  104. }
  105. if (!CheckApiRights(apiKeyData, apiId))
  106. {
  107. return 4; // API 사용 권한이 없습니다.
  108. }
  109. LinkedApiKey? server = apiKeyData.serverMap[ipAddress] as LinkedApiKey;
  110. if (server == null)
  111. {
  112. return 2; // 등록되어 있지 않은 IP Address에서 호출된 API KEY 입니다.
  113. }
  114. if (DateTime.Compare(server.ExpiredAt, DateTime.Now) < 0)
  115. {
  116. return 3; // API KEY 유효기간이 지났습니다.
  117. }
  118. return 0; // OK
  119. }
  120. else
  121. {
  122. errMsg = "등록되어 있지 않은 API KEY 입니다.";
  123. return 1; // 등록되어 있지 않은 키 정보입니다.
  124. }
  125. }
  126. catch (Exception ex)
  127. {
  128. errMsg = ex.Message;
  129. return 4;
  130. }
  131. }
  132. public int LoadAuthInformation()
  133. {
  134. LoadLinkedApiKeys();
  135. LoadLinkedDecryptKeys();
  136. return _apiKeyMap.Count + _decryptKeyMap.Count;
  137. }
  138. public void LoadLinkedApiKeys()
  139. {
  140. Hashtable keyMap = new Hashtable();
  141. List<LinkedApiKey> result = _repo.LoadLinkedApiKeys().Result;
  142. if (result != null)
  143. {
  144. foreach (LinkedApiKey key in result)
  145. {
  146. LinkedApiKey? apiKey = keyMap[key.ApiKey] as LinkedApiKey;
  147. if (apiKey == null)
  148. {
  149. // SERVER 추가
  150. key.serverMap = new Hashtable();
  151. key.serverMap.Add(key.ServerIpAddr, key);
  152. // API KEY 추가
  153. keyMap.Add(key.ApiKey, key);
  154. }
  155. else
  156. {
  157. LinkedApiKey? server = apiKey.serverMap[key.ServerIpAddr] as LinkedApiKey;
  158. if (server == null)
  159. {
  160. apiKey.serverMap.Add(key.ServerIpAddr, key);
  161. }
  162. }
  163. }
  164. }
  165. _apiKeyMap = keyMap;
  166. foreach (string key in _apiKeyMap.Keys)
  167. {
  168. LinkedApiKey? apiKey = _apiKeyMap[key] as LinkedApiKey;
  169. if (apiKey != null)
  170. {
  171. _log.LogInformation(" API KEY: {0}", apiKey.ApiKey);
  172. foreach (string serverIp in apiKey.serverMap.Keys)
  173. {
  174. LinkedApiKey? server = apiKey.serverMap[serverIp] as LinkedApiKey;
  175. if (server != null)
  176. {
  177. _log.LogInformation("API KEY SERVER: {0}, {1}", server.ApiKey, server.ServerIpAddr);
  178. }
  179. }
  180. }
  181. }
  182. }
  183. public void LoadLinkedDecryptKeys()
  184. {
  185. Hashtable keyMap = new Hashtable();
  186. List<LinkedDecryptKey> result = _repo.LoadLinkedDecryptKeys().Result;
  187. if (result != null)
  188. {
  189. foreach (LinkedDecryptKey key in result)
  190. {
  191. LinkedDecryptKey? apiKey = keyMap[key.DecryptKey] as LinkedDecryptKey;
  192. if (apiKey == null)
  193. {
  194. // SERVER 추가
  195. key.serverMap = new Hashtable();
  196. key.serverMap.Add(key.ServerIpAddr, key);
  197. // DESCRYPT KEY 추가
  198. keyMap.Add(key.DecryptKey, key);
  199. }
  200. else
  201. {
  202. LinkedDecryptKey? server = apiKey.serverMap[key.ServerIpAddr] as LinkedDecryptKey;
  203. if (server == null)
  204. {
  205. apiKey.serverMap.Add(key.ServerIpAddr, key);
  206. }
  207. }
  208. }
  209. }
  210. _decryptKeyMap = keyMap;
  211. foreach (string key in _decryptKeyMap.Keys)
  212. {
  213. LinkedDecryptKey? apiKey = _decryptKeyMap[key] as LinkedDecryptKey;
  214. if (apiKey != null)
  215. {
  216. _log.LogInformation(" DESCRYPT KEY: {0}", apiKey.DecryptKey);
  217. foreach (string serverIp in apiKey.serverMap.Keys)
  218. {
  219. LinkedDecryptKey? server = apiKey.serverMap[serverIp] as LinkedDecryptKey;
  220. if (server != null)
  221. {
  222. _log.LogInformation("DESCRYPT KEY SERVER: {0}, {1}", server.DecryptKey, server.ServerIpAddr);
  223. }
  224. }
  225. }
  226. }
  227. }
  228. }
  229. }