diff --git a/ShareX.HelpersLib/Helpers/URLHelpers.cs b/ShareX.HelpersLib/Helpers/URLHelpers.cs index 056205527..d01795781 100644 --- a/ShareX.HelpersLib/Helpers/URLHelpers.cs +++ b/ShareX.HelpersLib/Helpers/URLHelpers.cs @@ -32,6 +32,7 @@ You should have received a copy of the GNU General Public License using System.Linq; using System.Net; using System.Net.Cache; +using System.Net.Sockets; using System.Security; using System.Text; using System.Text.RegularExpressions; @@ -652,5 +653,20 @@ public static string DownloadString(string url, bool noCache = true) return response; } + + public static int GetRandomUnusedPort() + { + TcpListener listener = new TcpListener(IPAddress.Loopback, 0); + + try + { + listener.Start(); + return ((IPEndPoint)listener.LocalEndpoint).Port; + } + finally + { + listener.Stop(); + } + } } } \ No newline at end of file diff --git a/ShareX.UploadersLib/OAuth/OAuthListener.cs b/ShareX.UploadersLib/OAuth/OAuthListener.cs new file mode 100644 index 000000000..8423cf439 --- /dev/null +++ b/ShareX.UploadersLib/OAuth/OAuthListener.cs @@ -0,0 +1,93 @@ +#region License Information (GPL v3) + +/* + ShareX - A program that allows you to take screenshots and share any file type + Copyright (c) 2007-2022 ShareX Team + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + + Optionally you can also view the license at . +*/ + +#endregion License Information (GPL v3) + +using ShareX.HelpersLib; +using ShareX.UploadersLib.Properties; +using System; +using System.IO; +using System.Net; +using System.Text; +using System.Threading.Tasks; + +namespace ShareX.UploadersLib +{ + public class OAuthListener + { + public IOAuth2 OAuth { get; private set; } + + public async Task ConnectAsync() + { + IPAddress ip = IPAddress.Loopback; + int port = URLHelpers.GetRandomUnusedPort(); + string redirectURI = string.Format($"http://{ip}:{port}/"); + URLHelpers.OpenURL(redirectURI + "?code=test"); + + try + { + using (HttpListener listener = new HttpListener()) + { + listener.Prefixes.Add(redirectURI); + listener.Start(); + + HttpListenerContext context = await listener.GetContextAsync(); + string code = context.Request.QueryString.Get("code"); + + using (HttpListenerResponse response = context.Response) + { + string status; + + 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; + + using (Stream responseOutput = response.OutputStream) + { + await responseOutput.WriteAsync(buffer, 0, buffer.Length); + await responseOutput.FlushAsync(); + } + } + + return code; + } + } + catch (Exception e) + { + DebugHelper.WriteException(e); + } + + return null; + } + } +} \ No newline at end of file diff --git a/ShareX.UploadersLib/Properties/Resources.Designer.cs b/ShareX.UploadersLib/Properties/Resources.Designer.cs index 8aa945af3..705fadf3b 100644 --- a/ShareX.UploadersLib/Properties/Resources.Designer.cs +++ b/ShareX.UploadersLib/Properties/Resources.Designer.cs @@ -496,6 +496,34 @@ internal static System.Drawing.Bitmap navigation_270_button_white { } } + /// + /// Looks up a localized string similar to <!DOCTYPE html> + ///<html lang="en"> + /// + ///<head> + /// <meta charset="utf-8"> + /// <meta name="viewport" content="width=device-width, initial-scale=1"> + /// <title>Authorization - ShareX</title> + /// <style> + /// body { + /// background-color: #242424; + /// color: #fafafa; + /// font-family: Arial, sans-serif; + /// text-align: center; + /// } + /// + /// .container { + /// position: absolute; + /// top: 50%; + /// left: 50%; + /// transform: translat [rest of string was truncated]";. + /// + internal static string OAuthCallbackPage { + get { + return ResourceManager.GetString("OAuthCallbackPage", resourceCulture); + } + } + /// /// Looks up a localized string similar to Paste verification code here. /// diff --git a/ShareX.UploadersLib/Properties/Resources.resx b/ShareX.UploadersLib/Properties/Resources.resx index ee379e8d4..2da4f6fa8 100644 --- a/ShareX.UploadersLib/Properties/Resources.resx +++ b/ShareX.UploadersLib/Properties/Resources.resx @@ -446,4 +446,7 @@ Created folders: ..\Favicons\LobFile.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a + + ..\resources\oauthcallbackpage.html;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;Windows-1252 + \ No newline at end of file diff --git a/ShareX.UploadersLib/Resources/OAuthCallbackPage.html b/ShareX.UploadersLib/Resources/OAuthCallbackPage.html new file mode 100644 index 000000000..9e966ee3c --- /dev/null +++ b/ShareX.UploadersLib/Resources/OAuthCallbackPage.html @@ -0,0 +1,48 @@ + + + + + + + Authorization - ShareX + + + + +
+

ShareX

+

{0}

+

You can now close this page.

+
+ + + + + \ No newline at end of file diff --git a/ShareX.UploadersLib/ShareX.UploadersLib.csproj b/ShareX.UploadersLib/ShareX.UploadersLib.csproj index 24bffa065..79a381ed8 100644 --- a/ShareX.UploadersLib/ShareX.UploadersLib.csproj +++ b/ShareX.UploadersLib/ShareX.UploadersLib.csproj @@ -260,6 +260,7 @@ + True @@ -1185,6 +1186,9 @@ + + +