External.cshtml.cs 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using Application.Abstractions.Crypto;
  2. using Application.Abstractions.Messaging;
  3. using Microsoft.AspNetCore.Mvc;
  4. using Microsoft.AspNetCore.Mvc.RazorPages;
  5. namespace Admin.Pages.Config;
  6. public class ExternalModel(IMediator mediator, IFieldEncryptor encryptor) : PageModel
  7. {
  8. [BindProperty]
  9. public InputModel Input { get; set; } = new();
  10. public async Task OnGetAsync(CancellationToken ct)
  11. {
  12. var config = await mediator.Send(new GetConfig.Query(), ct);
  13. if (config is not null)
  14. {
  15. Input = InputModel.From(config);
  16. // 공개(/api/config) 응답엔 암호문(enc) 그대로 — Admin 화면에서만 복호화해 실제 키 표시
  17. Input.DataCollection.DataGoKrServiceKey = Dec(config.DataCollection.DataGoKrServiceKey);
  18. Input.DataCollection.KrxApiKey = Dec(config.DataCollection.KrxApiKey);
  19. Input.DataCollection.OpenDartApiKey = Dec(config.DataCollection.OpenDartApiKey);
  20. Input.DataCollection.KoreaEximExchangeKey = Dec(config.DataCollection.KoreaEximExchangeKey);
  21. Input.DataCollection.KoreaEximLoanRateKey = Dec(config.DataCollection.KoreaEximLoanRateKey);
  22. Input.DataCollection.KoreaEximIntlRateKey = Dec(config.DataCollection.KoreaEximIntlRateKey);
  23. Input.DataCollection.SeibroApiKey = Dec(config.DataCollection.SeibroApiKey);
  24. Input.DataCollection.KosisApiKey = Dec(config.DataCollection.KosisApiKey);
  25. Input.External.NaverClientSecret = Dec(config.External.NaverClientSecret);
  26. Input.External.KakaoRestApiKey = Dec(config.External.KakaoRestApiKey);
  27. Input.External.KakaoAdminKey = Dec(config.External.KakaoAdminKey);
  28. Input.External.NaverSearchClientSecret = Dec(config.External.NaverSearchClientSecret);
  29. }
  30. }
  31. /// <summary>암호문(enc:)이면 복호화, 아니면 그대로/빈 값 반환 — Admin 화면 표시 전용.</summary>
  32. private string? Dec(string? enc)
  33. {
  34. return string.IsNullOrWhiteSpace(enc) ? null : EncryptedConfigValue.Unprotect(encryptor, enc);
  35. }
  36. public async Task<IActionResult> OnPostAsync(CancellationToken ct)
  37. {
  38. if (!ModelState.IsValid)
  39. {
  40. return Page();
  41. }
  42. await mediator.Send(Input.ToCommand(), ct);
  43. TempData["SuccessMessage"] = "저장되었습니다.";
  44. return RedirectToPage();
  45. }
  46. public sealed class InputModel
  47. {
  48. public UpdateConfig.Request.ExternalApiConfigDto External { get; set; } = new();
  49. public UpdateConfig.Request.DataCollectionConfigDto DataCollection { get; set; } = new();
  50. public static InputModel From(GetConfig.Response config)
  51. {
  52. var req = UpdateConfig.Request.From(config);
  53. return new()
  54. {
  55. External = req.External,
  56. DataCollection = req.DataCollection ?? new()
  57. };
  58. }
  59. public UpdateConfig.Command ToCommand()
  60. {
  61. return new(
  62. External: External,
  63. DataCollection: DataCollection
  64. );
  65. }
  66. }
  67. }