diff --git a/Greenshot/Forms/BugReportForm.cs b/Greenshot/Forms/BugReportForm.cs index 77cc8929f..2f6fb0247 100644 --- a/Greenshot/Forms/BugReportForm.cs +++ b/Greenshot/Forms/BugReportForm.cs @@ -31,7 +31,7 @@ namespace Greenshot.Forms { // The InitializeComponent() call is required for Windows Forms designer support. // InitializeComponent(); - BringToFront = true; + ToFront = true; } public BugReportForm(string bugText) : this() { diff --git a/Greenshot/Forms/CaptureForm.cs b/Greenshot/Forms/CaptureForm.cs index 1347e215b..528cd6515 100644 --- a/Greenshot/Forms/CaptureForm.cs +++ b/Greenshot/Forms/CaptureForm.cs @@ -177,7 +177,7 @@ namespace Greenshot.Forms { ResumeLayout(); // Fix missing focus - BringToFront = true; + ToFront = true; TopMost = true; } diff --git a/Greenshot/Help/HelpFileLoader.cs b/Greenshot/Help/HelpFileLoader.cs index fe79de7b4..93759a850 100644 --- a/Greenshot/Help/HelpFileLoader.cs +++ b/Greenshot/Help/HelpFileLoader.cs @@ -1,10 +1,22 @@ /* - * Created by SharpDevelop. - * User: jens - * Date: 09.04.2012 - * Time: 19:24 + * Greenshot - a free and open source screenshot tool + * Copyright (C) 2007-2015 Thomas Braun, Jens Klingen, Robin Krom * - * To change this template use Tools | Options | Coding | Edit Standard Headers. + * For more information see: http://getgreenshot.org/ + * The Greenshot project is hosted on Sourceforge: http://sourceforge.net/projects/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 . */ using GreenshotPlugin.Core; @@ -28,7 +40,7 @@ namespace Greenshot.Help } public static void LoadHelp() { - string uri = findOnlineHelpUrl(Language.CurrentLanguage); + string uri = FindOnlineHelpUrl(Language.CurrentLanguage); if(uri == null) { uri = Language.HelpFilePath; } @@ -36,7 +48,7 @@ namespace Greenshot.Help } /// URL of help file in selected ietf, or (if not present) default ietf, or null (if not present, too. probably indicating that there is no internet connection) - private static string findOnlineHelpUrl(string currentIETF) { + private static string FindOnlineHelpUrl(string currentIETF) { string ret = null; string extHelpUrlForCurrrentIETF = EXT_HELP_URL; @@ -45,12 +57,12 @@ namespace Greenshot.Help extHelpUrlForCurrrentIETF += currentIETF.ToLower() + "/"; } - HttpStatusCode? httpStatusCode = getHttpStatus(extHelpUrlForCurrrentIETF); + HttpStatusCode? httpStatusCode = GetHttpStatus(extHelpUrlForCurrrentIETF); if(httpStatusCode == HttpStatusCode.OK) { ret = extHelpUrlForCurrrentIETF; } else if(httpStatusCode != null && !extHelpUrlForCurrrentIETF.Equals(EXT_HELP_URL)) { LOG.DebugFormat("Localized online help not found at {0}, will try {1} as fallback", extHelpUrlForCurrrentIETF, EXT_HELP_URL); - httpStatusCode = getHttpStatus(EXT_HELP_URL); + httpStatusCode = GetHttpStatus(EXT_HELP_URL); if(httpStatusCode == HttpStatusCode.OK) { ret = EXT_HELP_URL; } else { @@ -68,9 +80,9 @@ namespace Greenshot.Help /// /// URL for which the HTTP status is to be checked /// An HTTP status code, or null if there is none (probably indicating that there is no internet connection available - private static HttpStatusCode? getHttpStatus(string url) { + private static HttpStatusCode? GetHttpStatus(string url) { try { - HttpWebRequest req = (HttpWebRequest)NetworkHelper.CreateWebRequest(url); + HttpWebRequest req = NetworkHelper.CreateWebRequest(url); HttpWebResponse res = (HttpWebResponse)req.GetResponse(); return res.StatusCode; } catch(WebException e) { diff --git a/GreenshotBoxPlugin/BoxConfiguration.cs b/GreenshotBoxPlugin/BoxConfiguration.cs index e82773acf..b7632f865 100644 --- a/GreenshotBoxPlugin/BoxConfiguration.cs +++ b/GreenshotBoxPlugin/BoxConfiguration.cs @@ -18,9 +18,11 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ + using System.Windows.Forms; using Greenshot.IniFile; using GreenshotPlugin.Core; +using System; namespace GreenshotBoxPlugin { /// @@ -38,10 +40,43 @@ namespace GreenshotBoxPlugin { public bool AfterUploadLinkToClipBoard; [IniProperty("UseSharedLink", Description = "Use the shared link, instead of the private, on the clipboard", DefaultValue = "True")] - public bool UseSharedLink; + public bool UseSharedLink { + get; + set; + } + [IniProperty("FolderId", Description = "Folder ID to upload to, only change if you know what you are doing!", DefaultValue = "0")] + public string FolderId { + get; + set; + } - [IniProperty("BoxToken", Description = "Token.", DefaultValue = "")] - public string BoxToken; + [IniProperty("AddFilename", Description = "Is the filename passed on to Box", DefaultValue = "False")] + public bool AddFilename { + get; + set; + } + + [IniProperty("RefreshToken", Description = "Box authorization refresh Token", Encrypted = true)] + public string RefreshToken { + get; + set; + } + + /// + /// Not stored + /// + public string AccessToken { + get; + set; + } + + /// + /// Not stored + /// + public DateTimeOffset AccessTokenExpires { + get; + set; + } /// /// A form for token diff --git a/GreenshotBoxPlugin/BoxUtils.cs b/GreenshotBoxPlugin/BoxUtils.cs index bca46b0bf..2cb343c06 100644 --- a/GreenshotBoxPlugin/BoxUtils.cs +++ b/GreenshotBoxPlugin/BoxUtils.cs @@ -18,16 +18,14 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -using System; + +using Greenshot.IniFile; +using GreenshotPlugin.Core; using System.Collections.Generic; using System.Drawing; -using System.Net; -using System.Text; -using Greenshot.IniFile; -using GreenshotPlugin.Controls; -using GreenshotPlugin.Core; -using System.Runtime.Serialization.Json; using System.IO; +using System.Runtime.Serialization.Json; +using System.Text; namespace GreenshotBoxPlugin { @@ -37,101 +35,24 @@ namespace GreenshotBoxPlugin { public static class BoxUtils { private static readonly log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(BoxUtils)); private static readonly BoxConfiguration Config = IniConfig.GetIniSection(); - private const string RedirectUri = "https://www.box.com/home/"; private const string UploadFileUri = "https://upload.box.com/api/2.0/files/content"; - private const string AuthorizeUri = "https://www.box.com/api/oauth2/authorize"; - private const string TokenUri = "https://www.box.com/api/oauth2/token"; private const string FilesUri = "https://www.box.com/api/2.0/files/{0}"; - private static bool Authorize() { - string authorizeUrl = string.Format("{0}?client_id={1}&response_type=code&state=dropboxplugin&redirect_uri={2}", AuthorizeUri, BoxCredentials.ClientId, RedirectUri); - - OAuthLoginForm loginForm = new OAuthLoginForm("Box Authorize", new Size(1060, 600), authorizeUrl, RedirectUri); - loginForm.ShowDialog(); - if (!loginForm.isOk) { - return false; - } - var callbackParameters = loginForm.CallbackParameters; - if (callbackParameters == null || !callbackParameters.ContainsKey("code")) { - return false; - } - - string authorizationResponse = PostAndReturn(new Uri(TokenUri), string.Format("grant_type=authorization_code&code={0}&client_id={1}&client_secret={2}", callbackParameters["code"], BoxCredentials.ClientId, BoxCredentials.ClientSecret)); - var authorization = JsonSerializer.Deserialize(authorizationResponse); - - Config.BoxToken = authorization.AccessToken; - IniConfig.Save(); - return true; - } - /// - /// Download a url response as string - /// - /// An Uri to specify the download location - /// - /// string with the file content - public static string PostAndReturn(Uri url, string postMessage) { - HttpWebRequest webRequest = NetworkHelper.CreateWebRequest(url); - webRequest.Method = "POST"; - webRequest.KeepAlive = true; - webRequest.Credentials = CredentialCache.DefaultCredentials; - webRequest.ContentType = "application/x-www-form-urlencoded"; - byte[] data = Encoding.UTF8.GetBytes(postMessage); - using (var requestStream = webRequest.GetRequestStream()) { - requestStream.Write(data, 0, data.Length); - } - return NetworkHelper.GetResponse(webRequest); - } - - /// - /// Upload parameters by post - /// - /// - /// - /// response - public static string HttpPost(string url, IDictionary parameters) { - var webRequest = (HttpWebRequest)NetworkHelper.CreateWebRequest(url); - webRequest.Method = "POST"; - webRequest.KeepAlive = true; - webRequest.Credentials = CredentialCache.DefaultCredentials; - webRequest.Headers.Add("Authorization", "Bearer " + Config.BoxToken); - NetworkHelper.WriteMultipartFormData(webRequest, parameters); - - return NetworkHelper.GetResponse(webRequest); - } - - /// - /// Upload file by PUT + /// Put string /// /// /// + /// OAuth2Settings /// response - public static string HttpPut(string url, string content) { - var webRequest = (HttpWebRequest)NetworkHelper.CreateWebRequest(url); - webRequest.Method = "PUT"; - webRequest.KeepAlive = true; - webRequest.Credentials = CredentialCache.DefaultCredentials; - webRequest.Headers.Add("Authorization", "Bearer " + Config.BoxToken); + public static string HttpPut(string url, string content, OAuth2Settings settings) { + var webRequest= OAuth2Helper.CreateOAuth2WebRequest(HTTPMethod.PUT, url, settings); + byte[] data = Encoding.UTF8.GetBytes(content); using (var requestStream = webRequest.GetRequestStream()) { requestStream.Write(data, 0, data.Length); } - return NetworkHelper.GetResponse(webRequest); - } - - - /// - /// Get REST request - /// - /// - /// response - public static string HttpGet(string url) { - var webRequest = (HttpWebRequest)NetworkHelper.CreateWebRequest(url); - webRequest.Method = "GET"; - webRequest.KeepAlive = true; - webRequest.Credentials = CredentialCache.DefaultCredentials; - webRequest.Headers.Add("Authorization", "Bearer " + Config.BoxToken); - return NetworkHelper.GetResponse(webRequest); + return NetworkHelper.GetResponseAsString(webRequest); } /// @@ -143,45 +64,53 @@ namespace GreenshotBoxPlugin { /// Filename of box upload /// url to uploaded image public static string UploadToBox(SurfaceContainer image, string title, string filename) { - while (true) { - const string folderId = "0"; - if (string.IsNullOrEmpty(Config.BoxToken)) { - if (!Authorize()) { - return null; - } - } + // Fill the OAuth2Settings + OAuth2Settings settings = new OAuth2Settings(); + + settings.AuthUrlPattern = "https://www.box.com/api/oauth2/authorize?client_id={ClientId}&response_type={response_type}&state{State}&redirect_uri={RedirectUrl}"; + settings.TokenUrlPattern = "https://www.box.com/api/oauth2/token"; + settings.CloudServiceName = "Box"; + settings.ClientId = BoxCredentials.ClientId; + settings.ClientSecret = BoxCredentials.ClientSecret; + settings.RedirectUrl = "https://www.box.com/home/"; + settings.BrowserSize = new Size(1060, 600); + settings.AuthorizeMode = OAuth2AuthorizeMode.EmbeddedBrowser; + + // Copy the settings from the config, which is kept in memory and on the disk + settings.RefreshToken = Config.RefreshToken; + settings.AccessToken = Config.AccessToken; + settings.AccessTokenExpires = Config.AccessTokenExpires; + + try { + var webRequest = OAuth2Helper.CreateOAuth2WebRequest(HTTPMethod.POST, FilesUri, settings); IDictionary parameters = new Dictionary(); - parameters.Add("filename", image); - parameters.Add("parent_id", folderId); - - var response = ""; - try { - response = HttpPost(UploadFileUri, parameters); - } catch (WebException ex) { - if (ex.Status == WebExceptionStatus.ProtocolError) { - Config.BoxToken = null; - continue; - } + if (Config.AddFilename) { + parameters.Add("filename", image); } + parameters.Add("parent_id", Config.FolderId); + + NetworkHelper.WriteMultipartFormData(webRequest, parameters); + + var response = NetworkHelper.GetResponseAsString(webRequest); LOG.DebugFormat("Box response: {0}", response); - // Check if the token is wrong - if ("wrong auth token".Equals(response)) { - Config.BoxToken = null; - IniConfig.Save(); - continue; - } var upload = JsonSerializer.Deserialize(response); if (upload == null || upload.Entries == null || upload.Entries.Count == 0) return null; if (Config.UseSharedLink) { - string filesResponse = HttpPut(string.Format(FilesUri, upload.Entries[0].Id), "{\"shared_link\": {\"access\": \"open\"}}"); + string filesResponse = HttpPut(string.Format(FilesUri, upload.Entries[0].Id), "{\"shared_link\": {\"access\": \"open\"}}", settings); var file = JsonSerializer.Deserialize(filesResponse); return file.SharedLink.Url; } return string.Format("http://www.box.com/files/0/f/0/1/f_{0}", upload.Entries[0].Id); + } finally { + // Copy the settings back to the config, so they are stored. + Config.RefreshToken = settings.RefreshToken; + Config.AccessToken = settings.AccessToken; + Config.AccessTokenExpires = settings.AccessTokenExpires; + Config.IsDirty = true; } } } diff --git a/GreenshotImgurPlugin/ImgurUtils.cs b/GreenshotImgurPlugin/ImgurUtils.cs index ca03e84fe..833657964 100644 --- a/GreenshotImgurPlugin/ImgurUtils.cs +++ b/GreenshotImgurPlugin/ImgurUtils.cs @@ -122,8 +122,7 @@ namespace GreenshotImgurPlugin { if (config.AnonymousAccess) { // add key, we only use the other parameters for the AnonymousAccess otherParameters.Add("key", IMGUR_ANONYMOUS_API_KEY); - HttpWebRequest webRequest = (HttpWebRequest)NetworkHelper.CreateWebRequest(config.ImgurApiUrl + "/upload.xml?" + NetworkHelper.GenerateQueryParameters(otherParameters)); - webRequest.Method = "POST"; + HttpWebRequest webRequest = NetworkHelper.CreateWebRequest(config.ImgurApiUrl + "/upload.xml?" + NetworkHelper.GenerateQueryParameters(otherParameters), HTTPMethod.POST); webRequest.ContentType = "image/" + outputSettings.Format.ToString(); webRequest.ServicePoint.Expect100Continue = false; @@ -194,8 +193,7 @@ namespace GreenshotImgurPlugin { return; } LOG.InfoFormat("Retrieving Imgur image for {0} with url {1}", imgurInfo.Hash, imgurInfo.SmallSquare); - HttpWebRequest webRequest = (HttpWebRequest)NetworkHelper.CreateWebRequest(imgurInfo.SmallSquare); - webRequest.Method = "GET"; + HttpWebRequest webRequest = NetworkHelper.CreateWebRequest(imgurInfo.SmallSquare, HTTPMethod.GET); webRequest.ServicePoint.Expect100Continue = false; SetClientId(webRequest); using (WebResponse response = webRequest.GetResponse()) { @@ -215,8 +213,7 @@ namespace GreenshotImgurPlugin { public static ImgurInfo RetrieveImgurInfo(string hash, string deleteHash) { string url = config.ImgurApiUrl + "/image/" + hash; LOG.InfoFormat("Retrieving Imgur info for {0} with url {1}", hash, url); - HttpWebRequest webRequest = (HttpWebRequest)NetworkHelper.CreateWebRequest(url); - webRequest.Method = "GET"; + HttpWebRequest webRequest = NetworkHelper.CreateWebRequest(url, HTTPMethod.GET); webRequest.ServicePoint.Expect100Continue = false; SetClientId(webRequest); string responseString; @@ -250,10 +247,7 @@ namespace GreenshotImgurPlugin { try { string url = config.ImgurApiUrl + "/delete/" + imgurInfo.DeleteHash; - HttpWebRequest webRequest = (HttpWebRequest)NetworkHelper.CreateWebRequest(url); - - //webRequest.Method = "DELETE"; - webRequest.Method = "GET"; + HttpWebRequest webRequest = NetworkHelper.CreateWebRequest(url, HTTPMethod.GET); webRequest.ServicePoint.Expect100Continue = false; SetClientId(webRequest); string responseString; diff --git a/GreenshotPicasaPlugin/PicasaConfiguration.cs b/GreenshotPicasaPlugin/PicasaConfiguration.cs index c392dce2b..c7b81e3c2 100644 --- a/GreenshotPicasaPlugin/PicasaConfiguration.cs +++ b/GreenshotPicasaPlugin/PicasaConfiguration.cs @@ -37,12 +37,6 @@ namespace GreenshotPicasaPlugin { [IniProperty("AfterUploadLinkToClipBoard", Description = "After upload send Picasa link to clipboard.", DefaultValue = "true")] public bool AfterUploadLinkToClipBoard; - [IniProperty("RefreshToken", Description = "Picasa refresh Token", Encrypted = true)] - public string RefreshToken { - get; - set; - } - [IniProperty("AddFilename", Description = "Is the filename passed on to Picasa", DefaultValue = "False")] public bool AddFilename { get; @@ -61,6 +55,12 @@ namespace GreenshotPicasaPlugin { set; } + [IniProperty("RefreshToken", Description = "Picasa authorization refresh Token", Encrypted = true)] + public string RefreshToken { + get; + set; + } + /// /// Not stored /// diff --git a/GreenshotPicasaPlugin/PicasaUtils.cs b/GreenshotPicasaPlugin/PicasaUtils.cs index 04be91926..8ea4d57c6 100644 --- a/GreenshotPicasaPlugin/PicasaUtils.cs +++ b/GreenshotPicasaPlugin/PicasaUtils.cs @@ -50,10 +50,12 @@ namespace GreenshotPicasaPlugin { OAuth2Settings settings = new OAuth2Settings(); settings.AuthUrlPattern = AuthUrl; settings.TokenUrlPattern = TokenUrl; + settings.CloudServiceName = "Picasa"; settings.AdditionalAttributes.Add("response_type", "code"); settings.AdditionalAttributes.Add("scope", PicasaScope); settings.ClientId = PicasaCredentials.ClientId; settings.ClientSecret = PicasaCredentials.ClientSecret; + settings.AuthorizeMode = OAuth2AuthorizeMode.LocalServer; // Copy the settings from the config, which is kept in memory and on the disk settings.RefreshToken = Config.RefreshToken; @@ -61,27 +63,14 @@ namespace GreenshotPicasaPlugin { settings.AccessTokenExpires = Config.AccessTokenExpires; try { - // Get Refresh / Access token - if (string.IsNullOrEmpty(settings.RefreshToken)) { - OAuth2Helper.AuthenticateViaLocalServer(settings); - } - - if (settings.IsAccessTokenExpired) { - OAuth2Helper.GenerateAccessToken(settings); - } - - var webRequest = (HttpWebRequest)NetworkHelper.CreateWebRequest(string.Format(UploadUrl, Config.UploadUser, Config.UploadAlbum)); - webRequest.Method = "POST"; - webRequest.KeepAlive = true; - webRequest.Credentials = CredentialCache.DefaultCredentials; - OAuth2Helper.AddOAuth2Credentials(webRequest, settings); + var webRequest = OAuth2Helper.CreateOAuth2WebRequest(HTTPMethod.POST, string.Format(UploadUrl, Config.UploadUser, Config.UploadAlbum), settings); if (Config.AddFilename) { webRequest.Headers.Add("Slug", NetworkHelper.EscapeDataString(filename)); } SurfaceContainer container = new SurfaceContainer(surfaceToUpload, outputSettings, filename); container.Upload(webRequest); - string response = NetworkHelper.GetResponse(webRequest); + string response = NetworkHelper.GetResponseAsString(webRequest); return ParseResponse(response); } finally { @@ -89,6 +78,7 @@ namespace GreenshotPicasaPlugin { Config.RefreshToken = settings.RefreshToken; Config.AccessToken = settings.AccessToken; Config.AccessTokenExpires = settings.AccessTokenExpires; + Config.IsDirty = true; } } diff --git a/GreenshotPlugin/Controls/GreenshotForm.cs b/GreenshotPlugin/Controls/GreenshotForm.cs index ef15c0d76..ab151675a 100644 --- a/GreenshotPlugin/Controls/GreenshotForm.cs +++ b/GreenshotPlugin/Controls/GreenshotForm.cs @@ -87,7 +87,7 @@ namespace GreenshotPlugin.Controls { /// /// When this is set, the form will be brought to the foreground as soon as it is shown. /// - protected bool BringToFront { + protected bool ToFront { get; set; } @@ -162,7 +162,7 @@ namespace GreenshotPlugin.Controls { /// EventArgs protected override void OnShown(EventArgs e) { base.OnShown(e); - if (BringToFront) { + if (ToFront) { WindowDetails.ToForeground(Handle); } } diff --git a/GreenshotPlugin/Controls/OAuthLoginForm.Designer.cs b/GreenshotPlugin/Controls/OAuthLoginForm.Designer.cs index d81bb942e..f7d47f4c5 100644 --- a/GreenshotPlugin/Controls/OAuthLoginForm.Designer.cs +++ b/GreenshotPlugin/Controls/OAuthLoginForm.Designer.cs @@ -44,37 +44,37 @@ namespace GreenshotPlugin.Controls { /// the contents of this method with the code editor. /// private void InitializeComponent() { - this.addressTextBox = new System.Windows.Forms.TextBox(); - this.browser = new ExtendedWebBrowser(); + this._addressTextBox = new System.Windows.Forms.TextBox(); + this._browser = new ExtendedWebBrowser(); this.SuspendLayout(); // - // addressTextBox + // _addressTextBox // - this.addressTextBox.Cursor = System.Windows.Forms.Cursors.Arrow; - this.addressTextBox.Dock = System.Windows.Forms.DockStyle.Top; - this.addressTextBox.Enabled = false; - this.addressTextBox.Location = new System.Drawing.Point(0, 0); - this.addressTextBox.Name = "addressTextBox"; - this.addressTextBox.Size = new System.Drawing.Size(595, 20); - this.addressTextBox.TabIndex = 3; - this.addressTextBox.TabStop = false; + this._addressTextBox.Cursor = System.Windows.Forms.Cursors.Arrow; + this._addressTextBox.Dock = System.Windows.Forms.DockStyle.Top; + this._addressTextBox.Enabled = false; + this._addressTextBox.Location = new System.Drawing.Point(0, 0); + this._addressTextBox.Name = "addressTextBox"; + this._addressTextBox.Size = new System.Drawing.Size(595, 20); + this._addressTextBox.TabIndex = 3; + this._addressTextBox.TabStop = false; // - // browser + // _browser // - this.browser.Dock = System.Windows.Forms.DockStyle.Fill; - this.browser.Location = new System.Drawing.Point(0, 20); - this.browser.MinimumSize = new System.Drawing.Size(100, 100); - this.browser.Name = "browser"; - this.browser.Size = new System.Drawing.Size(595, 295); - this.browser.TabIndex = 4; + this._browser.Dock = System.Windows.Forms.DockStyle.Fill; + this._browser.Location = new System.Drawing.Point(0, 20); + this._browser.MinimumSize = new System.Drawing.Size(100, 100); + this._browser.Name = "browser"; + this._browser.Size = new System.Drawing.Size(595, 295); + this._browser.TabIndex = 4; // // OAuthLoginForm // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(595, 315); - this.Controls.Add(this.browser); - this.Controls.Add(this.addressTextBox); + this.Controls.Add(this._browser); + this.Controls.Add(this._addressTextBox); this.MaximizeBox = false; this.MinimizeBox = false; this.Name = "OAuthLoginForm"; @@ -85,8 +85,8 @@ namespace GreenshotPlugin.Controls { #endregion - private System.Windows.Forms.TextBox addressTextBox; - private ExtendedWebBrowser browser; + private System.Windows.Forms.TextBox _addressTextBox; + private ExtendedWebBrowser _browser; } } diff --git a/GreenshotPlugin/Controls/OAuthLoginForm.cs b/GreenshotPlugin/Controls/OAuthLoginForm.cs index f619927e9..ce721def5 100644 --- a/GreenshotPlugin/Controls/OAuthLoginForm.cs +++ b/GreenshotPlugin/Controls/OAuthLoginForm.cs @@ -35,31 +35,33 @@ namespace GreenshotPlugin.Controls { /// public partial class OAuthLoginForm : Form { private static readonly ILog LOG = LogManager.GetLogger(typeof(OAuthLoginForm)); - private string callbackUrl = null; - private IDictionary callbackParameters = null; + private string _callbackUrl = null; + private IDictionary _callbackParameters = null; public IDictionary CallbackParameters { - get { return callbackParameters; } + get { + return _callbackParameters; + } } - public bool isOk { + public bool IsOk { get { return DialogResult == DialogResult.OK; } } public OAuthLoginForm(string browserTitle, Size size, string authorizationLink, string callbackUrl) { - this.callbackUrl = callbackUrl; + _callbackUrl = callbackUrl; InitializeComponent(); ClientSize = size; Icon = GreenshotResources.getGreenshotIcon(); Text = browserTitle; - addressTextBox.Text = authorizationLink; + _addressTextBox.Text = authorizationLink; // The script errors are suppressed by using the ExtendedWebBrowser - browser.ScriptErrorsSuppressed = false; - browser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(browser_DocumentCompleted); - browser.Navigate(new Uri(authorizationLink)); + _browser.ScriptErrorsSuppressed = false; + _browser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(Browser_DocumentCompleted); + _browser.Navigate(new Uri(authorizationLink)); } /// @@ -71,33 +73,24 @@ namespace GreenshotPlugin.Controls { WindowDetails.ToForeground(Handle); } - private void browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) { - LOG.DebugFormat("document completed with url: {0}", browser.Url); - checkUrl(); - } - private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e) { - LOG.DebugFormat("Navigating to url: {0}", browser.Url); - addressTextBox.Text = e.Url.ToString(); + private void Browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) { + LOG.DebugFormat("document completed with url: {0}", _browser.Url); + CheckUrl(); } - 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)) { - string queryParams = browser.Url.Query; + private void CheckUrl() { + if (_browser.Url.ToString().StartsWith(_callbackUrl)) { + string queryParams = _browser.Url.Query; if (queryParams.Length > 0) { queryParams = NetworkHelper.UrlDecode(queryParams); //Store the Token and Token Secret - callbackParameters = NetworkHelper.ParseQueryString(queryParams); + _callbackParameters = NetworkHelper.ParseQueryString(queryParams); } DialogResult = DialogResult.OK; } } - private void addressTextBox_KeyPress(object sender, KeyPressEventArgs e) { + private void AddressTextBox_KeyPress(object sender, KeyPressEventArgs e) { //Cancel the key press so the user can't enter a new url e.Handled = true; } diff --git a/GreenshotPlugin/Controls/QualityDialog.cs b/GreenshotPlugin/Controls/QualityDialog.cs index bba47e895..c601c8e88 100644 --- a/GreenshotPlugin/Controls/QualityDialog.cs +++ b/GreenshotPlugin/Controls/QualityDialog.cs @@ -47,7 +47,7 @@ namespace GreenshotPlugin.Controls { trackBarJpegQuality.Value = Settings.JPGQuality; textBoxJpegQuality.Enabled = OutputFormat.jpg.Equals(outputSettings.Format); textBoxJpegQuality.Text = Settings.JPGQuality.ToString(); - BringToFront = true; + ToFront = true; } void Button_okClick(object sender, EventArgs e) { diff --git a/GreenshotPlugin/Core/NetworkHelper.cs b/GreenshotPlugin/Core/NetworkHelper.cs index fcd258e30..4ff398158 100644 --- a/GreenshotPlugin/Core/NetworkHelper.cs +++ b/GreenshotPlugin/Core/NetworkHelper.cs @@ -33,6 +33,17 @@ using System.Text; using System.Text.RegularExpressions; namespace GreenshotPlugin.Core { + /// + /// HTTP Method to make sure we have the correct method + /// + public enum HTTPMethod { + GET, + POST, + PUT, + DELETE, + HEAD + }; + /// /// Description of NetworkHelper. /// @@ -55,10 +66,7 @@ namespace GreenshotPlugin.Core { /// string with the file content public static string GetAsString(Uri uri) { HttpWebRequest webRequest = CreateWebRequest(uri); - webRequest.Method = "GET"; - webRequest.KeepAlive = true; - webRequest.Credentials = CredentialCache.DefaultCredentials; - return GetResponse(webRequest); + return GetResponseAsString(CreateWebRequest(uri)); } /// @@ -143,14 +151,36 @@ namespace GreenshotPlugin.Core { } /// - /// Helper method to create a web request, eventually with proxy + /// Helper method to create a web request with a lot of default settings /// /// string with uri to connect to /// WebRequest public static HttpWebRequest CreateWebRequest(string uri) { return CreateWebRequest(new Uri(uri)); } + + /// + /// Helper method to create a web request with a lot of default settings + /// + /// string with uri to connect to + /// /// Method to use + /// WebRequest + public static HttpWebRequest CreateWebRequest(string uri, HTTPMethod method) { + return CreateWebRequest(new Uri(uri), method); + } + /// + /// Helper method to create a web request with a lot of default settings + /// + /// Uri with uri to connect to + /// Method to use + /// WebRequest + public static HttpWebRequest CreateWebRequest(Uri uri, HTTPMethod method) { + HttpWebRequest webRequest = CreateWebRequest(uri); + webRequest.Method = method.ToString(); + return webRequest; + } + /// /// Helper method to create a web request, eventually with proxy /// @@ -164,6 +194,10 @@ namespace GreenshotPlugin.Core { // BUG-1655: Fix that Greenshot always uses the default proxy even if the "use default proxy" checkbox is unset webRequest.Proxy = null; } + // Make sure the default credentials are available + webRequest.Credentials = CredentialCache.DefaultCredentials; + + // Allow redirect, this is usually needed so that we don't get a problem when a service moves webRequest.AllowAutoRedirect = true; // Set default timeouts webRequest.Timeout = Config.WebRequestTimeout*1000; @@ -365,7 +399,7 @@ namespace GreenshotPlugin.Core { using (var streamWriter = new StreamWriter(requestStream, Encoding.UTF8)) { streamWriter.Write(urlEncoded); } - return GetResponse(webRequest); + return GetResponseAsString(webRequest); } /// @@ -388,7 +422,7 @@ namespace GreenshotPlugin.Core { /// The request object. /// The response data. /// TODO: This method should handle the StatusCode better! - public static string GetResponse(HttpWebRequest webRequest) { + public static string GetResponseAsString(HttpWebRequest webRequest) { string responseData = null; try { HttpWebResponse response = (HttpWebResponse) webRequest.GetResponse(); @@ -429,7 +463,7 @@ namespace GreenshotPlugin.Core { public static DateTime GetLastModified(Uri uri) { try { HttpWebRequest webRequest = CreateWebRequest(uri); - webRequest.Method = "HEAD"; + webRequest.Method = HTTPMethod.HEAD.ToString(); HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse(); LOG.DebugFormat("RSS feed was updated at {0}", webResponse.LastModified); return webResponse.LastModified; diff --git a/GreenshotPlugin/Core/OAuthHelper.cs b/GreenshotPlugin/Core/OAuthHelper.cs index a327264a7..2a8995131 100644 --- a/GreenshotPlugin/Core/OAuthHelper.cs +++ b/GreenshotPlugin/Core/OAuthHelper.cs @@ -46,9 +46,15 @@ namespace GreenshotPlugin.Core { } /// - /// Used HTTP Method, this is for the OAuth 1.x protocol + /// Specify the autorize mode that is used to get the token from the cloud service. /// - public enum HTTPMethod { GET, POST, PUT, DELETE }; + public enum OAuth2AuthorizeMode { + Unknown, // Will give an exception, caller needs to specify another value + LocalServer, // Will specify a redirect URL to http://localhost:port/authorize, while having a HttpListener + MonitorTitle, // Not implemented yet: Will monitor for title changes + Pin, // Not implemented yet: Will ask the user to enter the shown PIN + EmbeddedBrowser // Will open into an embedded _browser (OAuthLoginForm), and catch the redirect + } /// /// Settings for the OAuth 2 protocol @@ -58,6 +64,28 @@ namespace GreenshotPlugin.Core { AdditionalAttributes = new Dictionary(); // Create a default state State = Guid.NewGuid().ToString(); + AuthorizeMode = OAuth2AuthorizeMode.Unknown; + } + + public OAuth2AuthorizeMode AuthorizeMode { + get; + set; + } + + /// + /// Specify the name of the cloud service, so it can be used in window titles, logs etc + /// + public string CloudServiceName { + get; + set; + } + + /// + /// Specify the size of the embedded Browser, if using this + /// + public Size BrowserSize { + get; + set; } /// @@ -242,7 +270,7 @@ namespace GreenshotPlugin.Core { private readonly string _consumerKey; private readonly string _consumerSecret; - // default browser size + // default _browser size private Size _browserSize = new Size(864, 587); private string _loginTitle = "Authorize Greenshot access"; @@ -479,7 +507,7 @@ namespace GreenshotPlugin.Core { LOG.DebugFormat("Opening AuthorizationLink: {0}", AuthorizationLink); OAuthLoginForm oAuthLoginForm = new OAuthLoginForm(LoginTitle, BrowserSize, AuthorizationLink, CallbackUrl); oAuthLoginForm.ShowDialog(); - if (oAuthLoginForm.isOk) { + if (oAuthLoginForm.IsOk) { if (oAuthLoginForm.CallbackParameters != null) { string tokenValue; if (oAuthLoginForm.CallbackParameters.TryGetValue(OAUTH_TOKEN_KEY, out tokenValue)) { @@ -812,11 +840,9 @@ namespace GreenshotPlugin.Core { } } // Create webrequest - HttpWebRequest webRequest = NetworkHelper.CreateWebRequest(requestURL); - webRequest.Method = method.ToString(); + HttpWebRequest webRequest = NetworkHelper.CreateWebRequest(requestURL, method); webRequest.ServicePoint.Expect100Continue = false; webRequest.UserAgent = _userAgent; - webRequest.Timeout = 100000; if (UseHTTPHeadersForAuthorization && authHeader != null) { LOG.DebugFormat("Authorization: OAuth {0}", authHeader); @@ -860,7 +886,7 @@ namespace GreenshotPlugin.Core { string responseData; try { - responseData = NetworkHelper.GetResponse(webRequest); + responseData = NetworkHelper.GetResponseAsString(webRequest); LOG.DebugFormat("Response: {0}", responseData); } catch (Exception ex) { LOG.Error("Couldn't retrieve response: ", ex); @@ -879,6 +905,7 @@ namespace GreenshotPlugin.Core { /// public class LocalServerCodeReceiver { private static readonly ILog LOG = LogManager.GetLogger(typeof(LocalServerCodeReceiver)); + private readonly ManualResetEvent _ready = new ManualResetEvent(true); private string _loopbackCallback = "http://localhost:{0}/authorize/"; /// @@ -896,9 +923,9 @@ namespace GreenshotPlugin.Core { private string _closePageResponse = @" -OAuth 2.0 Authentication Token Received +OAuth 2.0 Authentication CloudServiceName -Greenshot received verification code. You can close this browser / tab if it is not closed itself... +Greenshot received information from CloudServiceName. You can close this browser / tab if it is not closed itself...