using FastReport.Forms; using FastReport.Utils; using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; using FastReport.Cloud.FastReport; namespace FastReport.Auth { public partial class AuthForm : BaseDialogForm { #region Private Fields private string expiresFormat; private static int selectedIndex = 0; private static string customAPIkey = ""; private static string customServerName = ""; private CancellationTokenSource tokenSource; #endregion Private Fields public static bool isFirstLoad = true; public AuthForm() { InitializeComponent(); Localize(); AuthService.Instance.Refresh(); UpdateInfo(); UIUtils.CheckRTL(this); UpdateDpiDependencies(); tokenSource = new CancellationTokenSource(); } public override void UpdateDpiDependencies() { base.UpdateDpiDependencies(); lbCaption.Font = lbCaptionServer.Font = this.LogicalToDevice(new Font("Calibri Light", 19.5f), true); if (RightToLeft == RightToLeft.Yes) { lbCaption.Left = pbAvatar.Right - lbCaption.Width; lbCaptionServer.Left = pbAvatar.Right - lbCaptionServer.Width; } } public override void Localize() { base.Localize(); MyRes res = new MyRes("Forms,AccountWindow"); Text = res.Get(""); lbCaption.Text = res.Get("Title"); btnSignIn.Text = res.Get("SignIn"); lbNoAccount.Text = res.Get("NoAccount"); lbCreateOne.Text = res.Get("Create"); lbMessage.Text = res.Get("Intro") + res.Get("Testing"); expiresFormat = res.Get("Expires"); lbManage.Text = res.Get("Profile"); lbSignOut.Text = res.Get("SignOut"); lbText.Text = res.Get("Message"); lbUpdate.Text = res.Get("SessionExpired"); MyRes res2 = new MyRes("Forms,ServerWindow"); pgServer.Text = res2.Get(""); label1Server.Text = res2.Get("Server"); label2Server.Text = res2.Get("ApiKeyword"); lbCaptionServer.Text = res2.Get("Caption"); comboBoxServerServer.Items[0] = res2.Get("comboBoxServer"); comboBoxServerServer.Items[1] = res2.Get("comboBoxCustom"); } protected override void OnFormClosed(FormClosedEventArgs e) { base.OnFormClosed(e); if (DialogResult == DialogResult.OK) { if (!string.IsNullOrEmpty(AuthService.Instance.User.ApiKey) && !string.IsNullOrEmpty(FRCloudOptions.Instance.BackendHost)) FRCloudOptions.Instance.SaveCustomServerInfo(); else FRCloudOptions.Instance.DeleteCustomServerInfo(); } tokenSource.Cancel(); tokenSource.Dispose(); } private async void btnSignIn_Click(object sender, EventArgs e) { await Task.Run(async () => { await AuthService.Instance.SignIn(tokenSource.Token); }, tokenSource.Token); UpdateInfo(); } private void lbCreateOne_Click(object sender, EventArgs e) { ProcessHelper.StartProcess("https://id.fast-report.com/"); } private void lbManage_Click(object sender, EventArgs e) { ProcessHelper.StartProcess("https://id.fast-report.com/"); } private async void lbSignOut_Click(object sender, EventArgs e) { await Task.Run(async () => { await AuthService.Instance.SignOut(tokenSource.Token); }, tokenSource.Token); UpdateInfo(); } private async void lbUpdate_Click(object sender, EventArgs e) { await Task.Run(async () => { if (!AuthService.Instance.Refresh()) await AuthService.Instance.SignIn(tokenSource.Token); }, tokenSource.Token); UpdateInfo(); } //server private void comboBoxServerServer_SelectedValueChanged(object sender, EventArgs e) { var comboBox = sender as ComboBox; var value = comboBox.SelectedItem as string; if (value == Res.Get("Forms,ServerWindow,comboBoxServer")) // pre-written server { textBoxCustomHostServer.Text = Res.Get("Forms,ServerWindow,DefaultServerHost"); textBoxCustomHostServer.Enabled = false; customServerName = ""; } else // Custom { textBoxCustomHostServer.Enabled = true; textBoxCustomHostServer.Text = ""; } if (isFirstLoad) { textBoxApiKeyServer.Text = customAPIkey; } else { textBoxApiKeyServer.Text = String.Empty; } } private void textBoxCustomHostServer_TextChanged(object sender, EventArgs e) { var textBox = sender as TextBox; if (!textBox.Enabled) return; var requestHost = textBox.Text; if (string.IsNullOrEmpty(requestHost)) { } else { AuthService.Instance.User.ApiKey = AuthService.Instance.User.ApiKey == String.Empty ? String.Empty : AuthService.Instance.User.ApiKey; FRCloudOptions.Instance.BackendHost = requestHost; customServerName = requestHost; } } private void textBoxApiKeyServer_TextChanged(object sender, EventArgs e) { var textBox = sender as TextBox; var text = textBox.Text; if (text != null) { var apiKey = text.Trim(); AuthService.Instance.User.ApiKey = apiKey; customAPIkey = apiKey; } } private Image RoundCorners(Image fromImage, float cornerRadius, Color backgroundColor) { cornerRadius *= 2; Bitmap result = new Bitmap(fromImage.Width, fromImage.Height); using (Graphics g = Graphics.FromImage(result)) { g.Clear(backgroundColor); g.SmoothingMode = SmoothingMode.AntiAlias; using (Brush brush = new TextureBrush(fromImage)) { using (GraphicsPath gp = new GraphicsPath()) { gp.AddArc(0, 0, cornerRadius, cornerRadius, 180, 90); gp.AddArc(0 + result.Width - cornerRadius - 1, 0, cornerRadius, cornerRadius, 270, 90); gp.AddArc(0 + result.Width - cornerRadius - 1, 0 + result.Height - cornerRadius - 1, cornerRadius, cornerRadius, 0, 90); gp.AddArc(0, 0 + result.Height - cornerRadius, cornerRadius - 1, cornerRadius, 90, 90); g.FillPath(brush, gp); return result; } } } } private void UpdateInfo() { AuthService auth = AuthService.Instance; if (isFirstLoad) { XmlItem xi = Config.Root.FindItem("Auth").FindItem("ConnectionInfo"); string serverFromConfig = xi.GetProp("Server"); string apiKeyFromConfig = FRCloudOptions.Instance.GetDecodedString(xi.GetProp("ApiKey")); if (serverFromConfig != string.Empty && serverFromConfig != Res.Get("Forms,ServerWindow,DefaultServerHost") && apiKeyFromConfig != string.Empty) { comboBoxServerServer.SelectedIndex = 1; FRCloudOptions.Instance.BackendHost = serverFromConfig; AuthService.Instance.User.ApiKey = apiKeyFromConfig; customAPIkey = apiKeyFromConfig; customServerName = serverFromConfig; textBoxApiKeyServer.Text = apiKeyFromConfig; textBoxCustomHostServer.Text = serverFromConfig; } else { customAPIkey = apiKeyFromConfig; comboBoxServerServer.SelectedIndex = 0; textBoxApiKeyServer.Text = apiKeyFromConfig; } isFirstLoad = false; } else { comboBoxServerServer.SelectedIndex = selectedIndex; if (selectedIndex == 1) { textBoxCustomHostServer.Text = customServerName; } textBoxApiKeyServer.Text = customAPIkey; } if (auth.User.IsAuthenticated) { BeforeSignIn(false); AfterSignIn(true); lbUsername.Text = auth.User.DisplayName; lbEmail.Text = auth.User.DisplayEmail; pbAvatar.Image = RoundCorners(auth.User.DisplayAvatar, 15, Color.Transparent); if (auth.CanRefresh) { if (auth.User.ExpiresIn < DateTime.Now) { lbTime.Visible = false; lbUpdate.Visible = true; } else { lbUpdate.Visible = false; lbTime.Visible = true; lbTime.Text = String.Format(expiresFormat, auth.User.ExpiresIn.ToShortTimeString()); } } else { // do not overlap with lbUpdate which is visible too lbTime.Visible = false; } } else { BeforeSignIn(true); AfterSignIn(false); } } private void BeforeSignIn(bool visible) { pgFrId.SuspendLayout(); lbMessage.Visible = visible; btnSignIn.Visible = visible; lbCreateOne.Visible = visible; lbNoAccount.Visible = visible; pgFrId.PerformLayout(); } private void AfterSignIn(bool visible) { pgFrId.SuspendLayout(); lbUpdate.Visible = visible; lbTime.Visible = visible; pbAvatar.Visible = visible; lbText.Visible = visible; lbSignOut.Visible = visible; lbManage.Visible = visible; lbUsername.Visible = visible; lbEmail.Visible = visible; pgFrId.ResumeLayout(true); } private void comboBoxServerServer_SelectedIndexChanged(object sender, EventArgs e) { selectedIndex = comboBoxServerServer.SelectedIndex; } } }