mirror of
https://github.com/ShareX/ShareX.git
synced 2024-09-28 23:42:13 +12:00
Added OAuthListener
This commit is contained in:
parent
4d64901364
commit
a9782a5112
6 changed files with 192 additions and 0 deletions
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
93
ShareX.UploadersLib/OAuth/OAuthListener.cs
Normal file
93
ShareX.UploadersLib/OAuth/OAuthListener.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
28
ShareX.UploadersLib/Properties/Resources.Designer.cs
generated
28
ShareX.UploadersLib/Properties/Resources.Designer.cs
generated
|
@ -496,6 +496,34 @@ internal static System.Drawing.Bitmap navigation_270_button_white {
|
|||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 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]";.
|
||||
/// </summary>
|
||||
internal static string OAuthCallbackPage {
|
||||
get {
|
||||
return ResourceManager.GetString("OAuthCallbackPage", resourceCulture);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to Paste verification code here.
|
||||
/// </summary>
|
||||
|
|
|
@ -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>
|
48
ShareX.UploadersLib/Resources/OAuthCallbackPage.html
Normal file
48
ShareX.UploadersLib/Resources/OAuthCallbackPage.html
Normal 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>
|
|
@ -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>
|
||||
|
|
Loading…
Reference in a new issue