mirror of
https://github.com/greenshot/greenshot
synced 2025-07-15 09:33:46 -07:00
BUG-1769: Added OAuth 2 for Picasa
This commit is contained in:
parent
29c7f466ec
commit
ea4631af3d
7 changed files with 660 additions and 139 deletions
|
@ -135,7 +135,7 @@ namespace Greenshot.Destinations {
|
||||||
string filepath = FilenameHelper.FillVariables(conf.OutputFilePath, false);
|
string filepath = FilenameHelper.FillVariables(conf.OutputFilePath, false);
|
||||||
try {
|
try {
|
||||||
fullPath = Path.Combine(filepath, filename);
|
fullPath = Path.Combine(filepath, filename);
|
||||||
} catch (ArgumentException ae) {
|
} catch (ArgumentException) {
|
||||||
// configured filename or path not valid, show error message...
|
// configured filename or path not valid, show error message...
|
||||||
LOG.InfoFormat("Generated path or filename not valid: {0}, {1}", filepath, filename);
|
LOG.InfoFormat("Generated path or filename not valid: {0}, {1}", filepath, filename);
|
||||||
|
|
||||||
|
|
|
@ -20,6 +20,7 @@
|
||||||
using System.Windows.Forms;
|
using System.Windows.Forms;
|
||||||
using Greenshot.IniFile;
|
using Greenshot.IniFile;
|
||||||
using GreenshotPlugin.Core;
|
using GreenshotPlugin.Core;
|
||||||
|
using System;
|
||||||
|
|
||||||
namespace GreenshotPicasaPlugin {
|
namespace GreenshotPicasaPlugin {
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -36,11 +37,45 @@ namespace GreenshotPicasaPlugin {
|
||||||
[IniProperty("AfterUploadLinkToClipBoard", Description = "After upload send Picasa link to clipboard.", DefaultValue = "true")]
|
[IniProperty("AfterUploadLinkToClipBoard", Description = "After upload send Picasa link to clipboard.", DefaultValue = "true")]
|
||||||
public bool AfterUploadLinkToClipBoard;
|
public bool AfterUploadLinkToClipBoard;
|
||||||
|
|
||||||
[IniProperty("PicasaToken", Description = "Picasa Token", Encrypted = true)]
|
[IniProperty("RefreshToken", Description = "Picasa refresh Token", Encrypted = true)]
|
||||||
public string PicasaToken;
|
public string RefreshToken {
|
||||||
|
get;
|
||||||
|
set;
|
||||||
|
}
|
||||||
|
|
||||||
[IniProperty("PicasaTokenSecret", Description = "PicasaTokenSecret", Encrypted = true)]
|
[IniProperty("AddFilename", Description = "Is the filename passed on to Picasa", DefaultValue = "False")]
|
||||||
public string PicasaTokenSecret;
|
public bool AddFilename {
|
||||||
|
get;
|
||||||
|
set;
|
||||||
|
}
|
||||||
|
|
||||||
|
[IniProperty("UploadUser", Description = "The picasa user to upload to", DefaultValue = "default")]
|
||||||
|
public string UploadUser {
|
||||||
|
get;
|
||||||
|
set;
|
||||||
|
}
|
||||||
|
|
||||||
|
[IniProperty("UploadAlbum", Description = "The picasa album to upload to", DefaultValue = "default")]
|
||||||
|
public string UploadAlbum {
|
||||||
|
get;
|
||||||
|
set;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Not stored
|
||||||
|
/// </summary>
|
||||||
|
public string AccessToken {
|
||||||
|
get;
|
||||||
|
set;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Not stored
|
||||||
|
/// </summary>
|
||||||
|
public DateTimeOffset AccessTokenExpires {
|
||||||
|
get;
|
||||||
|
set;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// A form for token
|
/// A form for token
|
||||||
|
|
|
@ -24,17 +24,102 @@ using System.Xml;
|
||||||
using Greenshot.IniFile;
|
using Greenshot.IniFile;
|
||||||
using Greenshot.Plugin;
|
using Greenshot.Plugin;
|
||||||
using GreenshotPlugin.Core;
|
using GreenshotPlugin.Core;
|
||||||
|
using System.Net;
|
||||||
|
|
||||||
namespace GreenshotPicasaPlugin {
|
namespace GreenshotPicasaPlugin {
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Description of PicasaUtils.
|
/// Description of PicasaUtils.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class PicasaUtils {
|
public static class PicasaUtils {
|
||||||
private const string GoogleAccountUri = "https://www.google.com/accounts/";
|
private const string PicasaScope = "https://picasaweb.google.com/data/";
|
||||||
private static readonly log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(PicasaUtils));
|
private static readonly log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(PicasaUtils));
|
||||||
private static readonly PicasaConfiguration Config = IniConfig.GetIniSection<PicasaConfiguration>();
|
private static readonly PicasaConfiguration Config = IniConfig.GetIniSection<PicasaConfiguration>();
|
||||||
|
private const string AuthUrl = "https://accounts.google.com/o/oauth2/auth?response_type={response_type}&client_id={ClientId}&redirect_uri={RedirectUrl}&state={State}&scope={scope}";
|
||||||
|
private const string TokenUrl = "https://www.googleapis.com/oauth2/v3/token";
|
||||||
|
private const string UploadUrl = "https://picasaweb.google.com/data/feed/api/user/{0}/albumid/{1}";
|
||||||
|
|
||||||
private PicasaUtils() {
|
/// <summary>
|
||||||
|
/// Authenticate by using the LocalServerCodeReceiver
|
||||||
|
/// If this works, generate a token
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="settings"></param>
|
||||||
|
private static void Authenticate(OAuth2Settings settings) {
|
||||||
|
var codeReceiver = new LocalServerCodeReceiver();
|
||||||
|
IDictionary<string, string> result = codeReceiver.ReceiveCode(settings);
|
||||||
|
|
||||||
|
string code;
|
||||||
|
if (result.TryGetValue("code", out code)) {
|
||||||
|
GenerateToken(code, settings);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Upload parameters by post
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="url"></param>
|
||||||
|
/// <param name="parameters"></param>
|
||||||
|
/// <returns>response</returns>
|
||||||
|
public static string HttpPost(string url, IDictionary<string, object> parameters, OAuth2Settings settings) {
|
||||||
|
var webRequest = (HttpWebRequest)NetworkHelper.CreateWebRequest(url);
|
||||||
|
webRequest.Method = "POST";
|
||||||
|
webRequest.KeepAlive = true;
|
||||||
|
webRequest.Credentials = CredentialCache.DefaultCredentials;
|
||||||
|
|
||||||
|
if (!string.IsNullOrEmpty(settings.AccessToken)) {
|
||||||
|
webRequest.Headers.Add("Authorization", "Bearer " + settings.AccessToken);
|
||||||
|
}
|
||||||
|
return NetworkHelper.UploadFormUrlEncoded(webRequest, parameters);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void GenerateToken(string code, OAuth2Settings settings) {
|
||||||
|
// Use the returned code to get a refresh code
|
||||||
|
IDictionary<string, object> data = new Dictionary<string, object>();
|
||||||
|
data.Add("code", code);
|
||||||
|
data.Add("client_id", settings.ClientId);
|
||||||
|
data.Add("redirect_uri", settings.RedirectUrl);
|
||||||
|
data.Add("client_secret", settings.ClientSecret);
|
||||||
|
data.Add("grant_type", "authorization_code");
|
||||||
|
|
||||||
|
var accessTokenJsonResult = HttpPost(settings.FormattedTokenUrl, data, settings);
|
||||||
|
IDictionary<string, object> refreshTokenResult = JSONHelper.JsonDecode(accessTokenJsonResult);
|
||||||
|
// gives as described here: https://developers.google.com/identity/protocols/OAuth2InstalledApp
|
||||||
|
// "access_token":"1/fFAGRNJru1FTz70BzhT3Zg",
|
||||||
|
// "expires_in":3920,
|
||||||
|
// "token_type":"Bearer",
|
||||||
|
// "refresh_token":"1/xEoDL4iW3cxlI7yDbSRFYNG01kVKM2C-259HOF2aQbI"
|
||||||
|
settings.AccessToken = (string)refreshTokenResult["access_token"] as string;
|
||||||
|
settings.RefreshToken = (string)refreshTokenResult["refresh_token"] as string;
|
||||||
|
|
||||||
|
object seconds = refreshTokenResult["expires_in"];
|
||||||
|
if (seconds != null) {
|
||||||
|
settings.AccessTokenExpires = DateTimeOffset.Now.AddSeconds((double)seconds);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Go out and retrieve a new access token via refresh-token with the TokenUrl in the settings
|
||||||
|
/// Will upate the access token, refresh token, expire date
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="settings"></param>
|
||||||
|
private static void GenerateAccessToken(OAuth2Settings settings) {
|
||||||
|
IDictionary<string, object> data = new Dictionary<string, object>();
|
||||||
|
data.Add("refresh_token", settings.RefreshToken);
|
||||||
|
data.Add("client_id", settings.ClientId);
|
||||||
|
data.Add("client_secret", settings.ClientSecret);
|
||||||
|
data.Add("grant_type", "refresh_token");
|
||||||
|
|
||||||
|
var accessTokenJsonResult = HttpPost(settings.FormattedTokenUrl, data, settings);
|
||||||
|
// gives as described here: https://developers.google.com/identity/protocols/OAuth2InstalledApp
|
||||||
|
// "access_token":"1/fFAGRNJru1FTz70BzhT3Zg",
|
||||||
|
// "expires_in":3920,
|
||||||
|
// "token_type":"Bearer",
|
||||||
|
|
||||||
|
IDictionary<string, object> accessTokenResult = JSONHelper.JsonDecode(accessTokenJsonResult);
|
||||||
|
settings.AccessToken = (string)accessTokenResult["access_token"] as string;
|
||||||
|
object seconds = accessTokenResult["expires_in"];
|
||||||
|
if (seconds != null) {
|
||||||
|
settings.AccessTokenExpires = DateTimeOffset.Now.AddSeconds((double)seconds);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -46,46 +131,57 @@ namespace GreenshotPicasaPlugin {
|
||||||
/// <param name="filename"></param>
|
/// <param name="filename"></param>
|
||||||
/// <returns>PicasaResponse</returns>
|
/// <returns>PicasaResponse</returns>
|
||||||
public static string UploadToPicasa(ISurface surfaceToUpload, SurfaceOutputSettings outputSettings, string title, string filename) {
|
public static string UploadToPicasa(ISurface surfaceToUpload, SurfaceOutputSettings outputSettings, string title, string filename) {
|
||||||
OAuthSession oAuth = new OAuthSession(PicasaCredentials.ConsumerKey, PicasaCredentials.ConsumerSecret);
|
// Fill the OAuth2Settings
|
||||||
oAuth.BrowserSize = new Size(1020, 590);
|
OAuth2Settings settings = new OAuth2Settings();
|
||||||
oAuth.AccessTokenUrl = GoogleAccountUri + "OAuthGetAccessToken";
|
settings.AuthUrlPattern = AuthUrl;
|
||||||
oAuth.AuthorizeUrl = GoogleAccountUri + "OAuthAuthorizeToken";
|
settings.TokenUrlPattern = TokenUrl;
|
||||||
oAuth.RequestTokenUrl = GoogleAccountUri + "OAuthGetRequestToken";
|
settings.AdditionalAttributes.Add("response_type", "code");
|
||||||
oAuth.LoginTitle = "Picasa authorization";
|
settings.AdditionalAttributes.Add("scope", PicasaScope);
|
||||||
oAuth.Token = Config.PicasaToken;
|
settings.ClientId = PicasaCredentials.ClientId;
|
||||||
oAuth.TokenSecret = Config.PicasaTokenSecret;
|
settings.ClientSecret = PicasaCredentials.ClientSecret;
|
||||||
oAuth.RequestTokenParameters.Add("scope", "https://picasaweb.google.com/data/");
|
|
||||||
oAuth.RequestTokenParameters.Add("xoauth_displayname", "Greenshot");
|
// Copy the settings from the config, which is kept in memory
|
||||||
if (string.IsNullOrEmpty(oAuth.Token)) {
|
settings.RefreshToken = Config.RefreshToken;
|
||||||
if (!oAuth.Authorize()) {
|
settings.AccessToken = Config.AccessToken;
|
||||||
return null;
|
settings.AccessTokenExpires = Config.AccessTokenExpires;
|
||||||
}
|
|
||||||
if (!string.IsNullOrEmpty(oAuth.Token)) {
|
|
||||||
Config.PicasaToken = oAuth.Token;
|
|
||||||
}
|
|
||||||
if (!string.IsNullOrEmpty(oAuth.TokenSecret)) {
|
|
||||||
Config.PicasaTokenSecret = oAuth.TokenSecret;
|
|
||||||
}
|
|
||||||
IniConfig.Save();
|
|
||||||
}
|
|
||||||
try {
|
try {
|
||||||
IDictionary<string, string> headers = new Dictionary<string, string>();
|
// Get Refresh / Access token
|
||||||
headers.Add("slug", OAuthSession.UrlEncode3986(filename));
|
if (string.IsNullOrEmpty(settings.RefreshToken)) {
|
||||||
string response = oAuth.MakeOAuthRequest(HTTPMethod.POST, "https://picasaweb.google.com/data/feed/api/user/default/albumid/default", headers, null, null, new SurfaceContainer(surfaceToUpload, outputSettings, filename));
|
Authenticate(settings);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (settings.IsAccessTokenExpired) {
|
||||||
|
GenerateAccessToken(settings);
|
||||||
|
}
|
||||||
|
|
||||||
|
var webRequest = (HttpWebRequest)NetworkHelper.CreateWebRequest(string.Format(UploadUrl, Config.UploadUser, Config.UploadAlbum));
|
||||||
|
webRequest.Method = "POST";
|
||||||
|
webRequest.KeepAlive = true;
|
||||||
|
webRequest.Credentials = CredentialCache.DefaultCredentials;
|
||||||
|
webRequest.Headers.Add("Authorization", "Bearer " + settings.AccessToken);
|
||||||
|
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);
|
||||||
|
|
||||||
return ParseResponse(response);
|
return ParseResponse(response);
|
||||||
} catch (Exception ex) {
|
|
||||||
LOG.Error("Upload error: ", ex);
|
|
||||||
throw;
|
|
||||||
} finally {
|
} finally {
|
||||||
if (!string.IsNullOrEmpty(oAuth.Token)) {
|
// Copy the settings back to the config
|
||||||
Config.PicasaToken = oAuth.Token;
|
Config.RefreshToken = settings.RefreshToken;
|
||||||
}
|
Config.AccessToken = settings.AccessToken;
|
||||||
if (!string.IsNullOrEmpty(oAuth.TokenSecret)) {
|
Config.AccessTokenExpires = settings.AccessTokenExpires;
|
||||||
Config.PicasaTokenSecret = oAuth.TokenSecret;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Parse the upload URL from the response
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="response"></param>
|
||||||
|
/// <returns></returns>
|
||||||
public static string ParseResponse(string response) {
|
public static string ParseResponse(string response) {
|
||||||
if (response == null) {
|
if (response == null) {
|
||||||
return null;
|
return null;
|
||||||
|
|
|
@ -321,6 +321,21 @@ namespace GreenshotPlugin.Core {
|
||||||
formDataStream.Write(Encoding.UTF8.GetBytes(footer), 0, Encoding.UTF8.GetByteCount(footer));
|
formDataStream.Write(Encoding.UTF8.GetBytes(footer), 0, Encoding.UTF8.GetByteCount(footer));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Post the parameters "x-www-form-urlencoded"
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="webRequest"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static string UploadFormUrlEncoded(HttpWebRequest webRequest, IDictionary<string, object> parameters) {
|
||||||
|
webRequest.ContentType = "application/x-www-form-urlencoded";
|
||||||
|
string urlEncoded = NetworkHelper.GenerateQueryParameters(parameters);
|
||||||
|
using (var requestStream = webRequest.GetRequestStream())
|
||||||
|
using (var streamWriter = new StreamWriter(requestStream, Encoding.UTF8)) {
|
||||||
|
streamWriter.Write(urlEncoded);
|
||||||
|
}
|
||||||
|
return GetResponse(webRequest);
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Process the web response.
|
/// Process the web response.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
|
@ -19,21 +19,25 @@
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
using GreenshotPlugin.Controls;
|
||||||
|
using log4net;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Collections.Specialized;
|
||||||
|
using System.Diagnostics;
|
||||||
using System.Drawing;
|
using System.Drawing;
|
||||||
using System.Globalization;
|
using System.Globalization;
|
||||||
|
using System.IO;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
|
using System.Net.Sockets;
|
||||||
using System.Security.Cryptography;
|
using System.Security.Cryptography;
|
||||||
|
using System.Security.Cryptography.X509Certificates;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using GreenshotPlugin.Controls;
|
|
||||||
using System.Security.Cryptography.X509Certificates;
|
|
||||||
using log4net;
|
|
||||||
|
|
||||||
namespace GreenshotPlugin.Core {
|
namespace GreenshotPlugin.Core {
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Provides a predefined set of algorithms that are supported officially by the protocol
|
/// Provides a predefined set of algorithms that are supported officially by the OAuth 1.x protocol
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public enum OAuthSignatureTypes {
|
public enum OAuthSignatureTypes {
|
||||||
HMACSHA1,
|
HMACSHA1,
|
||||||
|
@ -41,8 +45,144 @@ namespace GreenshotPlugin.Core {
|
||||||
RSASHA1
|
RSASHA1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Used HTTP Method, this is for the OAuth 1.x protocol
|
||||||
|
/// </summary>
|
||||||
public enum HTTPMethod { GET, POST, PUT, DELETE };
|
public enum HTTPMethod { GET, POST, PUT, DELETE };
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Settings for the OAuth 2 protocol
|
||||||
|
/// </summary>
|
||||||
|
public class OAuth2Settings {
|
||||||
|
public OAuth2Settings() {
|
||||||
|
AdditionalAttributes = new Dictionary<string, string>();
|
||||||
|
// Create a default state
|
||||||
|
State = Guid.NewGuid().ToString();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The OAuth 2 client id
|
||||||
|
/// </summary>
|
||||||
|
public string ClientId {
|
||||||
|
get;
|
||||||
|
set;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The OAuth 2 client secret
|
||||||
|
/// </summary>
|
||||||
|
public string ClientSecret {
|
||||||
|
get;
|
||||||
|
set;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The OAuth 2 state, this is something that is passed to the server, is not processed but returned back to the client.
|
||||||
|
/// e.g. a correlation ID
|
||||||
|
/// Default this is filled with a new Guid
|
||||||
|
/// </summary>
|
||||||
|
public string State {
|
||||||
|
get;
|
||||||
|
set;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The autorization URL where the values of this class can be "injected"
|
||||||
|
/// </summary>
|
||||||
|
public string AuthUrlPattern {
|
||||||
|
get;
|
||||||
|
set;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Get formatted Auth url (this will call a FormatWith(this) on the AuthUrlPattern
|
||||||
|
/// </summary>
|
||||||
|
public string FormattedAuthUrl {
|
||||||
|
get {
|
||||||
|
return AuthUrlPattern.FormatWith(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The URL to get a Token
|
||||||
|
/// </summary>
|
||||||
|
public string TokenUrlPattern {
|
||||||
|
get;
|
||||||
|
set;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Get formatted Token url (this will call a FormatWith(this) on the TokenUrlPattern
|
||||||
|
/// </summary>
|
||||||
|
public string FormattedTokenUrl {
|
||||||
|
get {
|
||||||
|
return TokenUrlPattern.FormatWith(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// This is the redirect URL, in some implementations this is automatically set (LocalServerCodeReceiver)
|
||||||
|
/// In some implementations this could be e.g. urn:ietf:wg:oauth:2.0:oob or urn:ietf:wg:oauth:2.0:oob:auto
|
||||||
|
/// </summary>
|
||||||
|
public string RedirectUrl {
|
||||||
|
get;
|
||||||
|
set;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Bearer token for accessing OAuth 2 services
|
||||||
|
/// </summary>
|
||||||
|
public string AccessToken {
|
||||||
|
get;
|
||||||
|
set;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Expire time for the AccessToken, this this time (-60 seconds) is passed a new AccessToken needs to be generated with the RefreshToken
|
||||||
|
/// </summary>
|
||||||
|
public DateTimeOffset AccessTokenExpires {
|
||||||
|
get;
|
||||||
|
set;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Return true if the access token is expired.
|
||||||
|
/// Important "side-effect": if true is returned the AccessToken will be set to null!
|
||||||
|
/// </summary>
|
||||||
|
public bool IsAccessTokenExpired {
|
||||||
|
get {
|
||||||
|
bool expired = true;
|
||||||
|
if (!string.IsNullOrEmpty(AccessToken) && AccessTokenExpires != null) {
|
||||||
|
expired = DateTimeOffset.Now.AddSeconds(60) > AccessTokenExpires;
|
||||||
|
}
|
||||||
|
// Make sure the token is not usable
|
||||||
|
if (expired) {
|
||||||
|
AccessToken = null;
|
||||||
|
}
|
||||||
|
return expired;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Token used to get a new Access Token
|
||||||
|
/// </summary>
|
||||||
|
public string RefreshToken {
|
||||||
|
get;
|
||||||
|
set;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Put anything in here which is needed for the OAuth 2 implementation of this specific service but isn't generic, e.g. for Google there is a "scope"
|
||||||
|
/// </summary>
|
||||||
|
public IDictionary<string, string> AdditionalAttributes {
|
||||||
|
get;
|
||||||
|
set;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// An OAuth 1 session object
|
||||||
|
/// </summary>
|
||||||
public class OAuthSession {
|
public class OAuthSession {
|
||||||
private static readonly ILog LOG = LogManager.GetLogger(typeof(OAuthSession));
|
private static readonly ILog LOG = LogManager.GetLogger(typeof(OAuthSession));
|
||||||
protected const string OAUTH_VERSION = "1.0";
|
protected const string OAUTH_VERSION = "1.0";
|
||||||
|
@ -732,4 +872,177 @@ namespace GreenshotPlugin.Core {
|
||||||
return responseData;
|
return responseData;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// OAuth 2.0 verification code receiver that runs a local server on a free port
|
||||||
|
/// and waits for a call with the authorization verification code.
|
||||||
|
/// </summary>
|
||||||
|
public class LocalServerCodeReceiver {
|
||||||
|
private static readonly ILog LOG = LogManager.GetLogger(typeof(LocalServerCodeReceiver));
|
||||||
|
|
||||||
|
private string _loopbackCallback = "http://localhost:{0}/authorize/";
|
||||||
|
/// <summary>
|
||||||
|
/// The call back format. Expects one port parameter.
|
||||||
|
/// Default: http://localhost:{0}/authorize/
|
||||||
|
/// </summary>
|
||||||
|
public string LoopbackCallbackUrl {
|
||||||
|
get {
|
||||||
|
return _loopbackCallback;
|
||||||
|
}
|
||||||
|
set {
|
||||||
|
_loopbackCallback = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private string _closePageResponse =
|
||||||
|
@"<html>
|
||||||
|
<head><title>OAuth 2.0 Authentication Token Received</title></head>
|
||||||
|
<body>
|
||||||
|
Greenshot received verification code. You can close this browser / tab if it is not closed itself...
|
||||||
|
<script type='text/javascript'>
|
||||||
|
window.setTimeout(function() {
|
||||||
|
window.open('', '_self', '');
|
||||||
|
window.close();
|
||||||
|
}, 1000);
|
||||||
|
if (window.opener) {
|
||||||
|
window.opener.checkToken();
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>";
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// HTML code to to return the browser, default it will try to close the browser / tab, this won't always work.
|
||||||
|
/// </summary>
|
||||||
|
public string ClosePageResponse {
|
||||||
|
get {
|
||||||
|
return _closePageResponse;
|
||||||
|
}
|
||||||
|
set {
|
||||||
|
_closePageResponse = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private string _redirectUri;
|
||||||
|
/// <summary>
|
||||||
|
/// The URL to redirect to
|
||||||
|
/// </summary>
|
||||||
|
protected string RedirectUri {
|
||||||
|
get {
|
||||||
|
if (!string.IsNullOrEmpty(_redirectUri)) {
|
||||||
|
return _redirectUri;
|
||||||
|
}
|
||||||
|
|
||||||
|
return _redirectUri = string.Format(_loopbackCallback, GetRandomUnusedPort());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The OAuth code receiver
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="authorizationUrl"></param>
|
||||||
|
/// <returns>Dictionary with values</returns>
|
||||||
|
public IDictionary<string, string> ReceiveCode(OAuth2Settings oauth2Settings) {
|
||||||
|
// Set the redirect URL on the settings
|
||||||
|
oauth2Settings.RedirectUrl = RedirectUri;
|
||||||
|
using (var listener = new HttpListener()) {
|
||||||
|
listener.Prefixes.Add(oauth2Settings.RedirectUrl);
|
||||||
|
try {
|
||||||
|
listener.Start();
|
||||||
|
|
||||||
|
// Get the formatted FormattedAuthUrl
|
||||||
|
string authorizationUrl = oauth2Settings.FormattedAuthUrl;
|
||||||
|
LOG.DebugFormat("Open a browser with: {0}", authorizationUrl);
|
||||||
|
Process.Start(authorizationUrl);
|
||||||
|
|
||||||
|
// Wait to get the authorization code response.
|
||||||
|
var context = listener.GetContext();
|
||||||
|
try {
|
||||||
|
NameValueCollection nameValueCollection = context.Request.QueryString;
|
||||||
|
|
||||||
|
// Write a "close" response.
|
||||||
|
using (var writer = new StreamWriter(context.Response.OutputStream)) {
|
||||||
|
writer.WriteLine(ClosePageResponse);
|
||||||
|
writer.Flush();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create a new response URL with a dictionary that contains all the response query parameters.
|
||||||
|
IDictionary<string, string> returnValues = new Dictionary<string, string>();
|
||||||
|
foreach (var name in nameValueCollection.AllKeys) {
|
||||||
|
if (!returnValues.ContainsKey(name)) {
|
||||||
|
returnValues.Add(name, nameValueCollection[name]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return returnValues;
|
||||||
|
} finally {
|
||||||
|
context.Response.OutputStream.Close();
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
listener.Close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns a random, unused port.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>port to use</returns>
|
||||||
|
private static int GetRandomUnusedPort() {
|
||||||
|
var listener = new TcpListener(IPAddress.Loopback, 0);
|
||||||
|
try {
|
||||||
|
listener.Start();
|
||||||
|
return ((IPEndPoint)listener.LocalEndpoint).Port;
|
||||||
|
} finally {
|
||||||
|
listener.Stop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Class to hold all the Properties for the OAuth 2 Token request
|
||||||
|
/// </summary>
|
||||||
|
public class OAuth2TokenRequest {
|
||||||
|
public string Code {
|
||||||
|
get;
|
||||||
|
set;
|
||||||
|
}
|
||||||
|
public string ClientId {
|
||||||
|
get;
|
||||||
|
set;
|
||||||
|
}
|
||||||
|
public string ClientSecret {
|
||||||
|
get;
|
||||||
|
set;
|
||||||
|
}
|
||||||
|
public string RedirectUri {
|
||||||
|
get;
|
||||||
|
set;
|
||||||
|
}
|
||||||
|
public string GrantType {
|
||||||
|
get;
|
||||||
|
set;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Class to hold all the Properties for the OAuth 2 Token response
|
||||||
|
/// </summary>
|
||||||
|
public class OAuth2TokenResponse {
|
||||||
|
public string AccessToken {
|
||||||
|
get;
|
||||||
|
set;
|
||||||
|
}
|
||||||
|
public string ExpiresIn {
|
||||||
|
get;
|
||||||
|
set;
|
||||||
|
}
|
||||||
|
public string TokenType {
|
||||||
|
get;
|
||||||
|
set;
|
||||||
|
}
|
||||||
|
public string RefreshToken {
|
||||||
|
get;
|
||||||
|
set;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,18 +18,80 @@
|
||||||
* You should have received a copy of the GNU General Public License
|
* You should have received a copy of the GNU General Public License
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Security.Cryptography;
|
using System.Security.Cryptography;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using log4net;
|
using log4net;
|
||||||
|
using System.Text.RegularExpressions;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Reflection;
|
||||||
|
|
||||||
namespace GreenshotPlugin.Core {
|
namespace GreenshotPlugin.Core {
|
||||||
public static class EncryptionHelper {
|
public static class StringExtensions {
|
||||||
private static readonly ILog LOG = LogManager.GetLogger(typeof(EncryptionHelper));
|
private static readonly ILog LOG = LogManager.GetLogger(typeof(StringExtensions));
|
||||||
private const string RGBIV = "dlgjowejgogkklwj";
|
private const string RGBIV = "dlgjowejgogkklwj";
|
||||||
private const string KEY = "lsjvkwhvwujkagfauguwcsjgu2wueuff";
|
private const string KEY = "lsjvkwhvwujkagfauguwcsjgu2wueuff";
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Format a string with the specified object
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="format">String with formatting, like {name}</param>
|
||||||
|
/// <param name="source">Object used for the formatting</param>
|
||||||
|
/// <returns>Formatted string</returns>
|
||||||
|
public static string FormatWith(this string format, object source) {
|
||||||
|
return FormatWith(format, null, source);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Format the string "format" with the source
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="format"></param>
|
||||||
|
/// <param name="provider"></param>
|
||||||
|
/// <param name="source">object with properties, if a property has the type IDictionary string,string it can used these parameters too</param>
|
||||||
|
/// <returns>Formatted string</returns>
|
||||||
|
public static string FormatWith(this string format, IFormatProvider provider, object source) {
|
||||||
|
if (format == null) {
|
||||||
|
throw new ArgumentNullException("format");
|
||||||
|
}
|
||||||
|
|
||||||
|
IDictionary<string, object> properties = new Dictionary<string, object>();
|
||||||
|
foreach(var propertyInfo in source.GetType().GetProperties()) {
|
||||||
|
if (propertyInfo.CanRead && propertyInfo.CanWrite) {
|
||||||
|
object value = propertyInfo.GetValue(source, null);
|
||||||
|
if (propertyInfo.PropertyType != typeof(IDictionary<string, string>)) {
|
||||||
|
properties.Add(propertyInfo.Name, value);
|
||||||
|
} else {
|
||||||
|
IDictionary<string, string> dictionary = (IDictionary<string, string>)value;
|
||||||
|
foreach (var propertyKey in dictionary.Keys) {
|
||||||
|
properties.Add(propertyKey, dictionary[propertyKey]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Regex r = new Regex(@"(?<start>\{)+(?<property>[\w\.\[\]]+)(?<format>:[^}]+)?(?<end>\})+", RegexOptions.Compiled | RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);
|
||||||
|
|
||||||
|
List<object> values = new List<object>();
|
||||||
|
string rewrittenFormat = r.Replace(format, delegate(Match m) {
|
||||||
|
Group startGroup = m.Groups["start"];
|
||||||
|
Group propertyGroup = m.Groups["property"];
|
||||||
|
Group formatGroup = m.Groups["format"];
|
||||||
|
Group endGroup = m.Groups["end"];
|
||||||
|
|
||||||
|
object value;
|
||||||
|
if (properties.TryGetValue(propertyGroup.Value, out value)) {
|
||||||
|
values.Add(value);
|
||||||
|
} else {
|
||||||
|
values.Add(source);
|
||||||
|
}
|
||||||
|
return new string('{', startGroup.Captures.Count) + (values.Count - 1) + formatGroup.Value + new string('}', endGroup.Captures.Count);
|
||||||
|
});
|
||||||
|
|
||||||
|
return string.Format(provider, rewrittenFormat, values.ToArray());
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// A simply rijndael aes encryption, can be used to store passwords
|
/// A simply rijndael aes encryption, can be used to store passwords
|
||||||
/// </summary>
|
/// </summary>
|
|
@ -155,7 +155,7 @@
|
||||||
<Compile Include="Core\CoreConfiguration.cs" />
|
<Compile Include="Core\CoreConfiguration.cs" />
|
||||||
<Compile Include="Core\CredentialsHelper.cs" />
|
<Compile Include="Core\CredentialsHelper.cs" />
|
||||||
<Compile Include="Core\Effects.cs" />
|
<Compile Include="Core\Effects.cs" />
|
||||||
<Compile Include="Core\EncryptionHelper.cs" />
|
<Compile Include="Core\StringExtensions.cs" />
|
||||||
<Compile Include="Core\FilenameHelper.cs" />
|
<Compile Include="Core\FilenameHelper.cs" />
|
||||||
<Compile Include="Core\ImageOutput.cs" />
|
<Compile Include="Core\ImageOutput.cs" />
|
||||||
<Compile Include="Core\InterfaceUtils.cs" />
|
<Compile Include="Core\InterfaceUtils.cs" />
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue