greenshot/GreenshotPlugin/Controls/OAuthLoginForm.cs

105 lines
3.8 KiB
C#

/*
* Greenshot - a free and open source screenshot tool
* Copyright (C) 2007-2016 Thomas Braun, Jens Klingen, Robin Krom
*
* For more information see: http://getgreenshot.org/
* The Greenshot project is hosted on GitHub https://github.com/greenshot/greenshot
*
* 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 1 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, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using GreenshotPlugin.Core;
using log4net;
namespace GreenshotPlugin.Controls {
/// <summary>
/// The OAuthLoginForm is used to allow the user to authorize Greenshot with an "Oauth" application
/// </summary>
public sealed partial class OAuthLoginForm : Form {
private static readonly ILog LOG = LogManager.GetLogger(typeof(OAuthLoginForm));
private readonly string _callbackUrl;
private IDictionary<string, string> _callbackParameters;
public IDictionary<string, string> CallbackParameters => _callbackParameters;
public bool IsOk => DialogResult == DialogResult.OK;
public OAuthLoginForm(string browserTitle, Size size, string authorizationLink, string callbackUrl) {
_callbackUrl = callbackUrl;
// Fix for BUG-2071
if (callbackUrl.EndsWith("/"))
{
_callbackUrl = callbackUrl.Substring(0, callbackUrl.Length - 1);
}
InitializeComponent();
ClientSize = size;
Icon = GreenshotResources.getGreenshotIcon();
Text = browserTitle;
_addressTextBox.Text = authorizationLink;
// The script errors are suppressed by using the ExtendedWebBrowser
_browser.ScriptErrorsSuppressed = false;
_browser.DocumentCompleted += Browser_DocumentCompleted;
_browser.Navigated += Browser_Navigated;
_browser.Navigating += Browser_Navigating;
_browser.Navigate(new Uri(authorizationLink));
}
/// <summary>
/// Make sure the form is visible
/// </summary>
/// <param name="e">EventArgs</param>
protected override void OnShown(EventArgs e) {
base.OnShown(e);
WindowDetails.ToForeground(Handle);
}
private void Browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) {
LOG.DebugFormat("document completed with url: {0}", _browser.Url);
CheckUrl();
}
private void Browser_Navigating(object sender, WebBrowserNavigatingEventArgs e) {
LOG.DebugFormat("Navigating to url: {0}", _browser.Url);
_addressTextBox.Text = e.Url.ToString();
}
private void Browser_Navigated(object sender, WebBrowserNavigatedEventArgs e) {
LOG.DebugFormat("Navigated to url: {0}", _browser.Url);
CheckUrl();
}
private void CheckUrl() {
if (_browser.Url.ToString().StartsWith(_callbackUrl)) {
var correctedUri = new Uri(_browser.Url.AbsoluteUri.Replace("#", "&"));
string queryParams = correctedUri.Query;
if (queryParams.Length > 0) {
queryParams = NetworkHelper.UrlDecode(queryParams);
//Store the Token and Token Secret
_callbackParameters = NetworkHelper.ParseQueryString(queryParams);
}
DialogResult = DialogResult.OK;
}
}
private void AddressTextBox_KeyPress(object sender, KeyPressEventArgs e) {
//Cancel the key press so the user can't enter a new url
e.Handled = true;
}
}
}