2022-12-08 00:58:08 +13:00
|
|
|
|
#region License Information (GPL v3)
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
ShareX - A program that allows you to take screenshots and share any file type
|
2024-01-03 12:57:14 +13:00
|
|
|
|
Copyright (c) 2007-2024 ShareX Team
|
2022-12-08 00:58:08 +13:00
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
{
|
2022-12-08 03:26:58 +13:00
|
|
|
|
public class OAuthListener : IDisposable
|
2022-12-08 00:58:08 +13:00
|
|
|
|
{
|
2022-12-08 02:18:16 +13:00
|
|
|
|
public IOAuth2Loopback OAuth { get; private set; }
|
2022-12-08 03:26:58 +13:00
|
|
|
|
|
|
|
|
|
private HttpListener listener;
|
2022-12-08 00:58:08 +13:00
|
|
|
|
|
2022-12-08 02:18:16 +13:00
|
|
|
|
public OAuthListener(IOAuth2Loopback oauth)
|
|
|
|
|
{
|
|
|
|
|
OAuth = oauth;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-08 03:26:58 +13:00
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
2022-12-08 21:32:58 +13:00
|
|
|
|
if (listener != null)
|
|
|
|
|
{
|
|
|
|
|
listener.Close();
|
|
|
|
|
listener = null;
|
|
|
|
|
}
|
2022-12-08 03:26:58 +13:00
|
|
|
|
}
|
|
|
|
|
|
2022-12-08 02:18:16 +13:00
|
|
|
|
public async Task<bool> ConnectAsync()
|
2022-12-08 00:58:08 +13:00
|
|
|
|
{
|
2022-12-08 04:29:41 +13:00
|
|
|
|
Dispose();
|
|
|
|
|
|
|
|
|
|
IPAddress ip = IPAddress.Loopback;
|
2023-10-29 19:58:20 +13:00
|
|
|
|
int port = WebHelpers.GetRandomUnusedPort();
|
2022-12-08 04:29:41 +13:00
|
|
|
|
string redirectURI = string.Format($"http://{ip}:{port}/");
|
2022-12-08 04:51:48 +13:00
|
|
|
|
string state = Helpers.GetRandomAlphanumeric(32);
|
2022-12-08 04:29:41 +13:00
|
|
|
|
|
|
|
|
|
OAuth.RedirectURI = redirectURI;
|
2022-12-08 04:51:48 +13:00
|
|
|
|
OAuth.State = state;
|
2022-12-08 04:29:41 +13:00
|
|
|
|
string url = OAuth.GetAuthorizationURL();
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(url))
|
2022-12-08 03:26:58 +13:00
|
|
|
|
{
|
2022-12-08 04:29:41 +13:00
|
|
|
|
URLHelpers.OpenURL(url);
|
|
|
|
|
DebugHelper.WriteLine("Authorization URL is opened: " + url);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
DebugHelper.WriteLine("Authorization URL is empty.");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2022-12-08 02:18:16 +13:00
|
|
|
|
|
2022-12-08 04:51:48 +13:00
|
|
|
|
string queryCode = null;
|
|
|
|
|
string queryState = null;
|
|
|
|
|
|
2022-12-08 04:29:41 +13:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
listener = new HttpListener();
|
|
|
|
|
listener.Prefixes.Add(redirectURI);
|
|
|
|
|
listener.Start();
|
2022-12-08 02:18:16 +13:00
|
|
|
|
|
2022-12-08 04:29:41 +13:00
|
|
|
|
HttpListenerContext context = await listener.GetContextAsync();
|
2022-12-08 04:51:48 +13:00
|
|
|
|
queryCode = context.Request.QueryString.Get("code");
|
|
|
|
|
queryState = context.Request.QueryString.Get("state");
|
2022-12-08 00:58:08 +13:00
|
|
|
|
|
2022-12-08 04:29:41 +13:00
|
|
|
|
using (HttpListenerResponse response = context.Response)
|
2022-12-08 00:58:08 +13:00
|
|
|
|
{
|
2022-12-08 04:29:41 +13:00
|
|
|
|
string status;
|
2022-12-08 03:26:58 +13:00
|
|
|
|
|
2022-12-10 22:51:49 +13:00
|
|
|
|
if (queryState != state)
|
2022-12-08 04:51:48 +13:00
|
|
|
|
{
|
|
|
|
|
status = "Invalid state parameter.";
|
|
|
|
|
}
|
|
|
|
|
else if (!string.IsNullOrEmpty(queryCode))
|
2022-12-08 04:29:41 +13:00
|
|
|
|
{
|
|
|
|
|
status = "Authorization completed successfully.";
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
status = "Authorization did not succeed.";
|
|
|
|
|
}
|
2022-12-08 00:58:08 +13:00
|
|
|
|
|
2022-12-08 04:29:41 +13:00
|
|
|
|
string responseText = Resources.OAuthCallbackPage.Replace("{0}", status);
|
|
|
|
|
byte[] buffer = Encoding.UTF8.GetBytes(responseText);
|
|
|
|
|
response.ContentLength64 = buffer.Length;
|
|
|
|
|
response.KeepAlive = false;
|
2022-12-08 00:58:08 +13:00
|
|
|
|
|
2022-12-08 04:29:41 +13:00
|
|
|
|
using (Stream responseOutput = response.OutputStream)
|
2022-12-08 00:58:08 +13:00
|
|
|
|
{
|
2022-12-08 04:29:41 +13:00
|
|
|
|
await responseOutput.WriteAsync(buffer, 0, buffer.Length);
|
|
|
|
|
await responseOutput.FlushAsync();
|
2022-12-08 00:58:08 +13:00
|
|
|
|
}
|
2022-12-08 03:26:58 +13:00
|
|
|
|
}
|
2022-12-08 00:58:08 +13:00
|
|
|
|
}
|
2022-12-08 21:32:58 +13:00
|
|
|
|
catch (ObjectDisposedException)
|
|
|
|
|
{
|
|
|
|
|
}
|
2022-12-08 04:29:41 +13:00
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
Dispose();
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-10 22:51:49 +13:00
|
|
|
|
if (queryState == state && !string.IsNullOrEmpty(queryCode))
|
2022-12-08 00:58:08 +13:00
|
|
|
|
{
|
2022-12-08 04:51:48 +13:00
|
|
|
|
return await Task.Run(() => OAuth.GetAccessToken(queryCode));
|
2022-12-08 00:58:08 +13:00
|
|
|
|
}
|
|
|
|
|
|
2022-12-08 02:18:16 +13:00
|
|
|
|
return false;
|
2022-12-08 00:58:08 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|