From 63a5d8dfd3ae6708839debcddbbfa3a89a94dd64 Mon Sep 17 00:00:00 2001 From: Jaex Date: Wed, 7 Dec 2022 18:29:41 +0300 Subject: [PATCH] Code refactoring --- .../Forms/UploadersConfigFormHelper.cs | 26 +++-- ShareX.UploadersLib/OAuth/OAuthListener.cs | 108 ++++++++---------- 2 files changed, 68 insertions(+), 66 deletions(-) diff --git a/ShareX.UploadersLib/Forms/UploadersConfigFormHelper.cs b/ShareX.UploadersLib/Forms/UploadersConfigFormHelper.cs index 0c0b96261..80cacf7ec 100644 --- a/ShareX.UploadersLib/Forms/UploadersConfigFormHelper.cs +++ b/ShareX.UploadersLib/Forms/UploadersConfigFormHelper.cs @@ -1086,15 +1086,25 @@ private bool OAuth2Complete(IOAuth2Basic uploader, string code, OAuthControl con private async Task OAuth2Loopback(IOAuth2Loopback oauth) { - OAuthListener listener = new OAuthListener(oauth); - bool result = await listener.ConnectAsync(); - - this.ForceActivate(); - ConfigureOAuthStatus(oauth2YouTube, result); - - if (result) + try { - return listener.OAuth.AuthInfo; + using (OAuthListener listener = new OAuthListener(oauth)) + { + bool result = await listener.ConnectAsync(); + + this.ForceActivate(); + ConfigureOAuthStatus(oauth2YouTube, result); + + if (result) + { + return listener.OAuth.AuthInfo; + } + } + } + catch (Exception e) + { + DebugHelper.WriteException(e); + e.ShowError(); } return null; diff --git a/ShareX.UploadersLib/OAuth/OAuthListener.cs b/ShareX.UploadersLib/OAuth/OAuthListener.cs index 2fd3efa57..bae594ab8 100644 --- a/ShareX.UploadersLib/OAuth/OAuthListener.cs +++ b/ShareX.UploadersLib/OAuth/OAuthListener.cs @@ -53,77 +53,69 @@ public void Dispose() public async Task ConnectAsync() { + Dispose(); + Code = null; + + IPAddress ip = IPAddress.Loopback; + int port = URLHelpers.GetRandomUnusedPort(); + string redirectURI = string.Format($"http://{ip}:{port}/"); + + OAuth.RedirectURI = redirectURI; + string url = OAuth.GetAuthorizationURL(); + + if (!string.IsNullOrEmpty(url)) + { + URLHelpers.OpenURL(url); + DebugHelper.WriteLine("Authorization URL is opened: " + url); + } + else + { + DebugHelper.WriteLine("Authorization URL is empty."); + return false; + } + try { - Dispose(); - Code = null; + listener = new HttpListener(); + listener.Prefixes.Add(redirectURI); + listener.Start(); - IPAddress ip = IPAddress.Loopback; - int port = URLHelpers.GetRandomUnusedPort(); - string redirectURI = string.Format($"http://{ip}:{port}/"); + HttpListenerContext context = await listener.GetContextAsync(); + Code = context.Request.QueryString.Get("code"); - OAuth.RedirectURI = redirectURI; - string url = OAuth.GetAuthorizationURL(); - - if (!string.IsNullOrEmpty(url)) + using (HttpListenerResponse response = context.Response) { - URLHelpers.OpenURL(url); - DebugHelper.WriteLine("Authorization URL is opened: " + url); - } - else - { - DebugHelper.WriteLine("Authorization URL is empty."); - return false; - } + string status; - try - { - listener = new HttpListener(); - listener.Prefixes.Add(redirectURI); - listener.Start(); - - HttpListenerContext context = await listener.GetContextAsync(); - Code = context.Request.QueryString.Get("code"); - - using (HttpListenerResponse response = context.Response) + if (!string.IsNullOrEmpty(Code)) { - string status; + status = "Authorization completed successfully."; + } + else + { + status = "Authorization did not succeed."; + } - if (!string.IsNullOrEmpty(Code)) - { - status = "Authorization completed successfully."; - } - else - { - status = "Authorization did not succeed."; - } + string responseText = Resources.OAuthCallbackPage.Replace("{0}", status); + byte[] buffer = Encoding.UTF8.GetBytes(responseText); + response.ContentLength64 = buffer.Length; + response.KeepAlive = false; - string responseText = Resources.OAuthCallbackPage.Replace("{0}", status); - byte[] buffer = Encoding.UTF8.GetBytes(responseText); - response.ContentLength64 = buffer.Length; - response.KeepAlive = false; - - using (Stream responseOutput = response.OutputStream) - { - await responseOutput.WriteAsync(buffer, 0, buffer.Length); - await responseOutput.FlushAsync(); - } + using (Stream responseOutput = response.OutputStream) + { + await responseOutput.WriteAsync(buffer, 0, buffer.Length); + await responseOutput.FlushAsync(); } } - finally - { - Dispose(); - } - - if (!string.IsNullOrEmpty(Code)) - { - return await Task.Run(() => OAuth.GetAccessToken(Code)); - } } - catch (Exception e) + finally { - DebugHelper.WriteException(e); - e.ShowError(); + Dispose(); + } + + if (!string.IsNullOrEmpty(Code)) + { + return await Task.Run(() => OAuth.GetAccessToken(Code)); } return false;