ApiAuthService.cs 8.5 KB

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