using FastReport.Utils; using System.Text; using System; using FastReport.Auth; namespace FastReport.Cloud.FastReport { public sealed class FRCloudOptions { public static FRCloudOptions Instance { get; } = new FRCloudOptions(); private const string DefaultBackendHost = "https://fastreport.cloud"; internal string BackendHost { get; set; } = DefaultBackendHost; public void ResetBackendHostAndApiKey() { XmlItem xi = Config.Root.FindItem("Auth").FindItem("ConnectionInfo"); string serverFromConfig = xi.GetProp("Server"); string apiKeyFromConfig = GetDecodedString(xi.GetProp("ApiKey")); if (serverFromConfig != string.Empty && apiKeyFromConfig != string.Empty) { Instance.BackendHost = serverFromConfig; AuthService.Instance.User.ApiKey = apiKeyFromConfig; } else { SetDefaultBackendHost(); } AuthService.Instance.Settings.Host = Res.Get("Forms,AccountWindow,AuthServer"); } internal void SaveCustomServerInfo() { XmlItem xi = Config.Root.FindItem("Auth").FindItem("ConnectionInfo"); var server = BackendHost; var encodedApi = GetEncodedString(AuthService.Instance.User.ApiKey); xi.SetProp("Server", server); xi.SetProp("ApiKey", encodedApi); } internal void DeleteCustomServerInfo() { XmlItem xi = Config.Root.FindItem("Auth").FindItem("ConnectionInfo"); xi.ClearProps(); xi.Clear(); } internal string GetEncodedString(string str) { var encoded = Convert.ToBase64String(Encoding.UTF8.GetBytes(str)); return encoded; } internal string GetDecodedString(string str) { var decoded = Encoding.UTF8.GetString(Convert.FromBase64String(str)); return decoded; } internal void SetDefaultBackendHost() { BackendHost = Res.Get("Forms,ServerWindow,DefaultServerHost"); } } }