FRCloudOptions.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using FastReport.Utils;
  2. using System.Text;
  3. using System;
  4. using FastReport.Auth;
  5. namespace FastReport.Cloud.FastReport
  6. {
  7. public sealed class FRCloudOptions
  8. {
  9. public static FRCloudOptions Instance { get; } = new FRCloudOptions();
  10. private const string DefaultBackendHost = "https://fastreport.cloud";
  11. internal string BackendHost { get; set; } = DefaultBackendHost;
  12. public void ResetBackendHostAndApiKey()
  13. {
  14. XmlItem xi = Config.Root.FindItem("Auth").FindItem("ConnectionInfo");
  15. string serverFromConfig = xi.GetProp("Server");
  16. string apiKeyFromConfig = GetDecodedString(xi.GetProp("ApiKey"));
  17. if (serverFromConfig != string.Empty && apiKeyFromConfig != string.Empty)
  18. {
  19. Instance.BackendHost = serverFromConfig;
  20. AuthService.Instance.User.ApiKey = apiKeyFromConfig;
  21. }
  22. else
  23. {
  24. SetDefaultBackendHost();
  25. }
  26. AuthService.Instance.Settings.Host = Res.Get("Forms,AccountWindow,AuthServer");
  27. }
  28. internal void SaveCustomServerInfo()
  29. {
  30. XmlItem xi = Config.Root.FindItem("Auth").FindItem("ConnectionInfo");
  31. var server = BackendHost;
  32. var encodedApi = GetEncodedString(AuthService.Instance.User.ApiKey);
  33. xi.SetProp("Server", server);
  34. xi.SetProp("ApiKey", encodedApi);
  35. }
  36. internal void DeleteCustomServerInfo()
  37. {
  38. XmlItem xi = Config.Root.FindItem("Auth").FindItem("ConnectionInfo");
  39. xi.ClearProps();
  40. xi.Clear();
  41. }
  42. internal string GetEncodedString(string str)
  43. {
  44. var encoded = Convert.ToBase64String(Encoding.UTF8.GetBytes(str));
  45. return encoded;
  46. }
  47. internal string GetDecodedString(string str)
  48. {
  49. var decoded = Encoding.UTF8.GetString(Convert.FromBase64String(str));
  50. return decoded;
  51. }
  52. internal void SetDefaultBackendHost()
  53. {
  54. BackendHost = Res.Get("Forms,ServerWindow,DefaultServerHost");
  55. }
  56. }
  57. }