ApiAuthService.cs 8.4 KB

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