[tomboy/gnome-2-28] [WebSync] Add OAuth 1.0a support (bug #594046)



commit 279182f8f00f295fe64c9d553f39cab9123cf9e3
Author: Rodrigo Moya <rodrigo gnome-db org>
Date:   Wed Oct 28 14:37:08 2009 -0700

    [WebSync] Add OAuth 1.0a support (bug #594046)

 Tomboy/Addins/WebSyncService/Api/OAuth.cs          |    5 ++-
 Tomboy/Addins/WebSyncService/OAuth/Base.cs         |   18 ++++++----
 .../WebSyncService/WebSyncPreferencesWidget.cs     |   39 ++++++++++----------
 3 files changed, 34 insertions(+), 28 deletions(-)
---
diff --git a/Tomboy/Addins/WebSyncService/Api/OAuth.cs b/Tomboy/Addins/WebSyncService/Api/OAuth.cs
index 781886e..954f8cb 100644
--- a/Tomboy/Addins/WebSyncService/Api/OAuth.cs
+++ b/Tomboy/Addins/WebSyncService/Api/OAuth.cs
@@ -192,8 +192,11 @@ namespace Tomboy.WebSync.Api
 			var outUrl = string.Empty;
 			List<IQueryParameter<string>> parameters = null;
 
+			string callbackUrl = string.Empty;
+			if (url.StartsWith (RequestTokenBaseUrl) || url.StartsWith (AccessTokenBaseUrl))
+				callbackUrl = CallbackUrl;
 			var sig = GenerateSignature (uri, ConsumerKey, ConsumerSecret, Token, TokenSecret, Verifier, method,
-				timeStamp, nonce, out outUrl, out parameters);
+			                             timeStamp, nonce, callbackUrl, out outUrl, out parameters);
 
 			if (Debugging)
 				Logger.Debug ("Generated signature {0}", sig);
diff --git a/Tomboy/Addins/WebSyncService/OAuth/Base.cs b/Tomboy/Addins/WebSyncService/OAuth/Base.cs
index d70566a..93f6158 100644
--- a/Tomboy/Addins/WebSyncService/OAuth/Base.cs
+++ b/Tomboy/Addins/WebSyncService/OAuth/Base.cs
@@ -159,7 +159,8 @@ namespace OAuth
 		/// <param name="signatureType">The signature type. To use the default values use <see cref="SignatureType">SignatureType</see>.</param>
 		/// <returns>The signature base.</returns>
 		private string GenerateSignatureBase (Uri url, string consumerKey, string token, string tokenSecret, string verifier,
-			RequestMethod method, TimeSpan timeStamp, string nonce, SignatureType signatureType, out string normalizedUrl,
+						      RequestMethod method, TimeSpan timeStamp, string nonce, SignatureType signatureType, string callbackUrl,
+						      out string normalizedUrl,
 			out List<IQueryParameter<string>> parameters)
 		{
 			log.LogDebug ("Generating signature base for OAuth request.");
@@ -196,6 +197,7 @@ namespace OAuth
 
 			if (!string.IsNullOrEmpty (token)) parameters.Add (new QueryParameter<string> (OAuthTokenKey, token, s => string.IsNullOrEmpty (s)));
 			if (!string.IsNullOrEmpty (verifier)) parameters.Add (new QueryParameter<string> (OAuthVerifierKey, verifier, s => string.IsNullOrEmpty (s)));
+			if (!string.IsNullOrEmpty (callbackUrl)) parameters.Add (new QueryParameter<string> (OAuthCallbackKey, UrlEncode (callbackUrl), s => string.IsNullOrEmpty (s)));
 
 			log.LogDebug ("Normalizing URL for signature.");
 
@@ -251,12 +253,13 @@ namespace OAuth
 		/// <param name="httpMethod">The HTTP method used. Must be valid HTTP method verb (POST, GET, PUT, etc).</param>
 		/// <returns>A Base64 string of the hash value.</returns>
 		protected string GenerateSignature (Uri url, string consumerKey, string consumerSecret, string token,
-			string tokenSecret, string verifier, RequestMethod method, TimeSpan timeStamp, string nonce, out string normalizedUrl,
-			out List<IQueryParameter<string>> parameters)
+		                                    string tokenSecret, string verifier, RequestMethod method, TimeSpan timeStamp, string nonce,
+		                                    string callbackUrl, out string normalizedUrl,
+		                                    out List<IQueryParameter<string>> parameters)
 		{
 			log.LogDebug ("Generating signature using HMAC-SHA1 algorithm.");
 			return GenerateSignature (url, consumerKey, consumerSecret, token, tokenSecret, verifier, method, timeStamp, nonce,
-				SignatureType.HMACSHA1, out normalizedUrl, out parameters);
+			                          SignatureType.HMACSHA1, callbackUrl, out normalizedUrl, out parameters);
 		}
 
 		/// <summary>
@@ -272,8 +275,8 @@ namespace OAuth
 		/// <param name="signatureType">The type of signature to use.</param>
 		/// <returns>A Base64 string of the hash value.</returns>
 		private string GenerateSignature (Uri url, string consumerKey, string consumerSecret, string token,
-			string tokenSecret, string verifier, RequestMethod method, TimeSpan timeStamp, string nonce, SignatureType signatureType,
-			out string normalizedUrl, out List<IQueryParameter<string>> parameters)
+		                                  string tokenSecret, string verifier, RequestMethod method, TimeSpan timeStamp, string nonce, SignatureType signatureType,
+		                                  string callbackUrl, out string normalizedUrl, out List<IQueryParameter<string>> parameters)
 		{
 			log.LogDebug ("Generating signature using signature type {0}", signatureType);
 
@@ -288,7 +291,8 @@ namespace OAuth
 					return signature;
 				case SignatureType.HMACSHA1:
 					string signatureBase = GenerateSignatureBase (url, consumerKey, token, tokenSecret, verifier, method,
-						timeStamp, nonce, SignatureType.HMACSHA1, out normalizedUrl, out parameters);
+					                                              timeStamp, nonce, SignatureType.HMACSHA1, callbackUrl,
+					                                              out normalizedUrl, out parameters);
 
 					var hmacsha1 = new HMACSHA1 ();
 					hmacsha1.Key = Encoding.ASCII.GetBytes (string.Format ("{0}&{1}",
diff --git a/Tomboy/Addins/WebSyncService/WebSyncPreferencesWidget.cs b/Tomboy/Addins/WebSyncService/WebSyncPreferencesWidget.cs
index 5d112e7..01536ae 100644
--- a/Tomboy/Addins/WebSyncService/WebSyncPreferencesWidget.cs
+++ b/Tomboy/Addins/WebSyncService/WebSyncPreferencesWidget.cs
@@ -107,25 +107,24 @@ namespace Tomboy.WebSync
 			}
 
 			// TODO: Move this
-			if (Auth == null) {
-				string rootUri = Server + "/api/1.0";
-				try {
-					RootInfo root = RootInfo.GetRoot (rootUri, new Api.AnonymousConnection ());
+			if (Auth == null)
+				Auth = new Api.OAuth ();
 
-					Auth = new Api.OAuth ();
+			string rootUri = Server + "/api/1.0";
+			try {
+				RootInfo root = RootInfo.GetRoot (rootUri, new Api.AnonymousConnection ());
 
-					Auth.AuthorizeLocation = root.AuthorizeUrl;
-					Auth.AccessTokenBaseUrl = root.AccessTokenUrl;
-					Auth.RequestTokenBaseUrl = root.RequestTokenUrl;
-					Auth.ConsumerKey = "anyone";
-					Auth.ConsumerSecret = "anyone";
-					Auth.Realm = "Snowy";
-				} catch (Exception e) {
-					Logger.Error ("Failed to get Root resource " + rootUri + ". Exception was: " + e.ToString());
-					authButton.Label = Catalog.GetString ("Server not responding. Try again later.");
-					oauth = null;
-					return;
-				}
+				Auth.AuthorizeLocation = root.AuthorizeUrl;
+				Auth.AccessTokenBaseUrl = root.AccessTokenUrl;
+				Auth.RequestTokenBaseUrl = root.RequestTokenUrl;
+				Auth.ConsumerKey = "anyone";
+				Auth.ConsumerSecret = "anyone";
+				Auth.Realm = "Snowy";
+			} catch (Exception e) {
+				Logger.Error ("Failed to get Root resource " + rootUri + ". Exception was: " + e.ToString());
+				authButton.Label = Catalog.GetString ("Server not responding. Try again later.");
+				oauth = null;
+				return;
 			}
 
 			if (!Auth.IsAccessToken) {
@@ -159,6 +158,7 @@ namespace Tomboy.WebSync
 					return;
 				}
 
+				Logger.Debug ("Listening on {0} for OAuth callback", callbackUrl);
 				string authUrl = string.Empty;
 				try {
 					authUrl = Auth.GetAuthorizationUrl ();
@@ -223,7 +223,6 @@ namespace Tomboy.WebSync
 					}
 				}, null);
 
-				Logger.Debug ("Listening on {0} for OAuth callback", callbackUrl);
 				Logger.Debug ("Launching browser to authorize web sync: " + authUrl);
 				authButton.Label = Catalog.GetString ("Authorizing in browser (Press to reset connection)");
 				try {



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]