Added OAuthListener

This commit is contained in:
Jaex 2022-12-07 14:58:08 +03:00
parent 4d64901364
commit a9782a5112
6 changed files with 192 additions and 0 deletions

View file

@ -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();
}
}
}
}

View file

@ -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 <http://www.gnu.org/licenses/>.
*/
#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<string> 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;
}
}
}

View file

@ -496,6 +496,34 @@ internal static System.Drawing.Bitmap navigation_270_button_white {
}
}
/// <summary>
/// Looks up a localized string similar to &lt;!DOCTYPE html&gt;
///&lt;html lang=&quot;en&quot;&gt;
///
///&lt;head&gt;
/// &lt;meta charset=&quot;utf-8&quot;&gt;
/// &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1&quot;&gt;
/// &lt;title&gt;Authorization - ShareX&lt;/title&gt;
/// &lt;style&gt;
/// 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]&quot;;.
/// </summary>
internal static string OAuthCallbackPage {
get {
return ResourceManager.GetString("OAuthCallbackPage", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Paste verification code here.
/// </summary>

View file

@ -446,4 +446,7 @@ Created folders:</value>
<data name="LobFile" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Favicons\LobFile.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
<data name="OAuthCallbackPage" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\resources\oauthcallbackpage.html;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;Windows-1252</value>
</data>
</root>

View file

@ -0,0 +1,48 @@
<!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: translate(-50%, -50%);
}
.container h1 {
margin: 0px 0px 30px 0px;
font-size: 80px;
}
.container p {
font-size: 20px;
}
</style>
</head>
<body>
<div class="container">
<h1>ShareX</h1>
<p>{0}</p>
<p>You can now close this page.</p>
</div>
<script>
setTimeout(function() {
window.close();
}, 30000);
</script>
</body>
</html>

View file

@ -260,6 +260,7 @@
<Compile Include="OAuth\OAuth2ProofKey.cs" />
<Compile Include="Helpers\SSLBypassHelper.cs" />
<Compile Include="BaseServices\URLSharingService.cs" />
<Compile Include="OAuth\OAuthListener.cs" />
<Compile Include="OAuth\OAuthUserInfo.cs" />
<Compile Include="Properties\Resources.Designer.cs">
<AutoGen>True</AutoGen>
@ -1185,6 +1186,9 @@
<ItemGroup>
<None Include="Favicons\LobFile.png" />
</ItemGroup>
<ItemGroup>
<Content Include="Resources\OAuthCallbackPage.html" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<PropertyGroup>
<PreBuildEvent>