Added OAuthListenerForm

This commit is contained in:
Jaex 2022-12-08 11:32:58 +03:00
parent dd8baa9cb6
commit c8adf33bd5
7 changed files with 378 additions and 30 deletions

View file

@ -3041,10 +3041,19 @@ private void txtGfycatTitle_TextChanged(object sender, EventArgs e)
#region YouTube
private async void oauth2YouTube_OpenButtonClicked()
private void oauth2YouTube_OpenButtonClicked()
{
OAuth2Info oauth = new OAuth2Info(APIKeys.GoogleClientID, APIKeys.GoogleClientSecret);
Config.YouTubeOAuth2Info = await OAuth2Loopback(new YouTube(oauth).OAuth2);
IOAuth2Loopback oauthLoopback = new YouTube(oauth).OAuth2;
using (OAuthListenerForm form = new OAuthListenerForm(oauthLoopback))
{
form.ShowDialog();
Config.YouTubeOAuth2Info = form.OAuth2Info;
}
this.ForceActivate();
ConfigureOAuthStatus(oauth2YouTube, Config.YouTubeOAuth2Info != null);
}
private void oauth2YouTube_CompleteButtonClicked(string code)

View file

@ -1084,32 +1084,6 @@ private bool OAuth2Complete(IOAuth2Basic uploader, string code, OAuthControl con
return false;
}
private async Task<OAuth2Info> OAuth2Loopback(IOAuth2Loopback oauth)
{
try
{
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;
}
private bool OAuth2Refresh(IOAuth2 uploader, OAuthControl oauth2)
{
try

View file

@ -46,8 +46,11 @@ public OAuthListener(IOAuth2Loopback oauth)
public void Dispose()
{
listener?.Close();
listener = null;
if (listener != null)
{
listener.Close();
listener = null;
}
}
public async Task<bool> ConnectAsync()
@ -118,6 +121,9 @@ public async Task<bool> ConnectAsync()
}
}
}
catch (ObjectDisposedException)
{
}
finally
{
Dispose();

View file

@ -0,0 +1,67 @@
namespace ShareX.UploadersLib
{
partial class OAuthListenerForm
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(OAuthListenerForm));
this.label1 = new System.Windows.Forms.Label();
this.btnCancel = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// label1
//
resources.ApplyResources(this.label1, "label1");
this.label1.Name = "label1";
//
// btnCancel
//
resources.ApplyResources(this.btnCancel, "btnCancel");
this.btnCancel.Name = "btnCancel";
this.btnCancel.UseVisualStyleBackColor = true;
this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
//
// OAuthListenerForm
//
resources.ApplyResources(this, "$this");
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi;
this.Controls.Add(this.btnCancel);
this.Controls.Add(this.label1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.Name = "OAuthListenerForm";
this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.OAuthListenerForm_FormClosed);
this.Load += new System.EventHandler(this.OAuthListenerForm_Load);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button btnCancel;
}
}

View file

@ -0,0 +1,88 @@
#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 System;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ShareX.UploadersLib
{
public partial class OAuthListenerForm : Form
{
public IOAuth2Loopback OAuth { get; private set; }
public OAuth2Info OAuth2Info { get; private set; }
private OAuthListener listener;
public OAuthListenerForm(IOAuth2Loopback oauth)
{
InitializeComponent();
ShareXResources.ApplyTheme(this);
OAuth = oauth;
}
private async void OAuthListenerForm_Load(object sender, EventArgs e)
{
OAuth2Info = await ConnectAsync(OAuth);
Close();
}
private void OAuthListenerForm_FormClosed(object sender, FormClosedEventArgs e)
{
listener?.Dispose();
}
private void btnCancel_Click(object sender, EventArgs e)
{
Close();
}
private async Task<OAuth2Info> ConnectAsync(IOAuth2Loopback oauth)
{
try
{
using (listener = new OAuthListener(oauth))
{
bool result = await listener.ConnectAsync();
if (result)
{
return listener.OAuth.AuthInfo;
}
}
}
catch (Exception e)
{
DebugHelper.WriteException(e);
e.ShowError();
}
return null;
}
}
}

View file

@ -0,0 +1,195 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<assembly alias="System.Drawing" name="System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<data name="label1.Location" type="System.Drawing.Point, System.Drawing">
<value>21, 24</value>
</data>
<data name="label1.Size" type="System.Drawing.Size, System.Drawing">
<value>424, 56</value>
</data>
<assembly alias="mscorlib" name="mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<data name="label1.TabIndex" type="System.Int32, mscorlib">
<value>0</value>
</data>
<data name="label1.Text" xml:space="preserve">
<value>Waiting for authorization in your browser to complete.</value>
</data>
<data name="&gt;&gt;label1.Name" xml:space="preserve">
<value>label1</value>
</data>
<data name="&gt;&gt;label1.Type" xml:space="preserve">
<value>System.Windows.Forms.Label, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</data>
<data name="&gt;&gt;label1.Parent" xml:space="preserve">
<value>$this</value>
</data>
<data name="&gt;&gt;label1.ZOrder" xml:space="preserve">
<value>1</value>
</data>
<data name="btnCancel.Location" type="System.Drawing.Point, System.Drawing">
<value>328, 88</value>
</data>
<data name="btnCancel.Size" type="System.Drawing.Size, System.Drawing">
<value>120, 40</value>
</data>
<data name="btnCancel.TabIndex" type="System.Int32, mscorlib">
<value>1</value>
</data>
<data name="btnCancel.Text" xml:space="preserve">
<value>Cancel</value>
</data>
<data name="&gt;&gt;btnCancel.Name" xml:space="preserve">
<value>btnCancel</value>
</data>
<data name="&gt;&gt;btnCancel.Type" xml:space="preserve">
<value>System.Windows.Forms.Button, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</data>
<data name="&gt;&gt;btnCancel.Parent" xml:space="preserve">
<value>$this</value>
</data>
<data name="&gt;&gt;btnCancel.ZOrder" xml:space="preserve">
<value>0</value>
</data>
<metadata name="$this.Localizable" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<data name="$this.AutoScaleDimensions" type="System.Drawing.SizeF, System.Drawing">
<value>96, 96</value>
</data>
<data name="$this.ClientSize" type="System.Drawing.Size, System.Drawing">
<value>465, 145</value>
</data>
<data name="$this.Font" type="System.Drawing.Font, System.Drawing">
<value>Segoe UI, 12pt</value>
</data>
<assembly alias="System.Windows.Forms" name="System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<data name="$this.StartPosition" type="System.Windows.Forms.FormStartPosition, System.Windows.Forms">
<value>CenterScreen</value>
</data>
<data name="$this.Text" xml:space="preserve">
<value>ShareX - Authorization</value>
</data>
<data name="&gt;&gt;$this.Name" xml:space="preserve">
<value>OAuthListenerForm</value>
</data>
<data name="&gt;&gt;$this.Type" xml:space="preserve">
<value>System.Windows.Forms.Form, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</data>
</root>

View file

@ -262,6 +262,12 @@
<Compile Include="Helpers\SSLBypassHelper.cs" />
<Compile Include="BaseServices\URLSharingService.cs" />
<Compile Include="OAuth\OAuthListener.cs" />
<Compile Include="OAuth\OAuthListenerForm.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="OAuth\OAuthListenerForm.Designer.cs">
<DependentUpon>OAuthListenerForm.cs</DependentUpon>
</Compile>
<Compile Include="OAuth\OAuthUserInfo.cs" />
<Compile Include="Properties\Resources.Designer.cs">
<AutoGen>True</AutoGen>
@ -1069,6 +1075,9 @@
<EmbeddedResource Include="Forms\YouTubeVideoOptionsForm.zh-CN.resx">
<DependentUpon>YouTubeVideoOptionsForm.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="OAuth\OAuthListenerForm.resx">
<DependentUpon>OAuthListenerForm.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="Properties\Resources.de.resx">
<SubType>Designer</SubType>
</EmbeddedResource>