[tomboy] [WebSync] Patch from Rodrigo Moya updating to latest REST API spec



commit 4bc1ff7802a0de4ca327aae264fb1e32c7639736
Author: Sandy Armstrong <sanfordarmstrong gmail com>
Date:   Sat Sep 5 17:44:43 2009 -0700

    [WebSync] Patch from Rodrigo Moya updating to latest REST API spec
    
    In addition to updates to the Root resource, this patch includes
    support for unathenticated GETs, and implements a HTTPS certificate
    policy that blindly accepts all certificates.

 Tomboy.mdp                                         |    2 +
 .../WebSyncService/Api/AnonymousConnection.cs      |  110 ++++++++++++++++++++
 .../WebSyncService/Api/CertificateManager.cs       |   60 +++++++++++
 Tomboy/Addins/WebSyncService/Api/OAuth.cs          |   16 ++--
 Tomboy/Addins/WebSyncService/Api/RootInfo.cs       |   21 +++-
 .../WebSyncService/WebSyncPreferencesWidget.cs     |   27 ++++--
 .../WebSyncService/WebSyncService-mac.csproj       |    2 +
 Tomboy/Addins/WebSyncService/WebSyncService.csproj |    2 +
 8 files changed, 222 insertions(+), 18 deletions(-)
---
diff --git a/Tomboy.mdp b/Tomboy.mdp
index deefaad..d7a1bba 100644
--- a/Tomboy.mdp
+++ b/Tomboy.mdp
@@ -152,6 +152,8 @@
     <File name="Tomboy/Addins/WebSyncService/WebSyncServiceAddin.cs" subtype="Code" buildaction="Compile" />
     <File name="Tomboy/Addins/WebSyncService/WebSyncServer.cs" subtype="Code" buildaction="Compile" />
     <File name="Tomboy/Addins/WebSyncService/Api" subtype="Directory" buildaction="Compile" />
+    <File name="Tomboy/Addins/WebSyncService/Api/AnonymousConnection.cs" subtype="Code" buildaction="Compile" />
+    <File name="Tomboy/Addins/WebSyncService/Api/CertificateManager.cs" subtype="Code" buildaction="Compile" />
     <File name="Tomboy/Addins/WebSyncService/Api/NoteInfo.cs" subtype="Code" buildaction="Compile" />
     <File name="Tomboy/Addins/WebSyncService/Api/UserInfo.cs" subtype="Code" buildaction="Compile" />
     <File name="Tomboy/Addins/WebSyncService/Api/ResourceReference.cs" subtype="Code" buildaction="Compile" />
diff --git a/Tomboy/Addins/WebSyncService/Api/AnonymousConnection.cs b/Tomboy/Addins/WebSyncService/Api/AnonymousConnection.cs
new file mode 100644
index 0000000..11a349d
--- /dev/null
+++ b/Tomboy/Addins/WebSyncService/Api/AnonymousConnection.cs
@@ -0,0 +1,110 @@
+// Permission is hereby granted, free of charge, to any person obtaining 
+// a copy of this software and associated documentation files (the 
+// "Software"), to deal in the Software without restriction, including 
+// without limitation the rights to use, copy, modify, merge, publish, 
+// distribute, sublicense, and/or sell copies of the Software, and to 
+// permit persons to whom the Software is furnished to do so, subject to 
+// the following conditions: 
+//  
+// The above copyright notice and this permission notice shall be 
+// included in all copies or substantial portions of the Software. 
+//  
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 
+// 
+// Copyright (c) 2009 Novell, Inc. (http://www.novell.com)
+// Copyright (c) 2009 Canonical, Ltd (http://www.canonical.com)
+// 
+// Authors: 
+//      Rodrigo Moya <rodrigo moya canonical com>
+// 
+
+using System;
+using System.Collections.Generic;
+using System.Net;
+using System.IO;
+using System.Text;
+using System.Web;
+
+using Mono.Rocks;
+
+namespace Tomboy.WebSync.Api
+{
+
+	public class AnonymousConnection : IWebConnection
+	{
+
+		#region IWebConnection implementation
+		public string Get (string uri, IDictionary<string, string> parameters)
+		{
+			return WebRequest ("GET", BuildUri (uri, parameters));
+		}
+
+		public string Delete (string uri, IDictionary<string, string> parameters)
+		{
+			return null;
+		}
+
+		public string Put (string uri, IDictionary<string, string> parameters, string putValue)
+		{
+			return null;
+		}
+
+		public string Post (string uri, IDictionary<string, string> parameters, string postValue)
+		{
+			return null;
+		}
+		#endregion
+
+		#region Private Methods
+		private string WebRequest (string method, string uri)
+		{
+			string responseData = string.Empty;
+			HttpWebRequest webRequest;
+
+			ServicePointManager.CertificatePolicy = new CertificateManager ();
+
+			try {
+				webRequest = System.Net.WebRequest.Create (uri) as HttpWebRequest;
+
+				webRequest.Method = method;
+				webRequest.ServicePoint.Expect100Continue = false;
+
+				using (var responseReader = new StreamReader (webRequest.GetResponse ().GetResponseStream ())) {
+					responseData = responseReader.ReadToEnd ();
+				}
+			} catch (Exception e) {
+				Logger.Error ("Caught exception. Message: {0}", e.Message);
+				Logger.Error ("Stack trace for previous exception: {0}", e.StackTrace);
+				throw;
+			}
+
+			return responseData;
+		}
+
+		private string BuildUri (string baseUri, IDictionary<string, string> queryParameters)
+		{
+			StringBuilder urlBuilder = new StringBuilder (baseUri);	// TODO: Capacity?
+			urlBuilder.Append ("?");
+			if (queryParameters != null) {
+				foreach (var param in queryParameters) {
+					urlBuilder.Append (param.Key);
+					urlBuilder.Append ("=");
+					urlBuilder.Append (param.Value);
+					urlBuilder.Append ("&");
+				}
+			}
+			// Get rid of trailing ? or &
+			urlBuilder.Remove (urlBuilder.Length - 1, 1);
+			return urlBuilder.ToString ();
+		}
+		#endregion
+
+	}
+
+}
diff --git a/Tomboy/Addins/WebSyncService/Api/CertificateManager.cs b/Tomboy/Addins/WebSyncService/Api/CertificateManager.cs
new file mode 100644
index 0000000..150a66a
--- /dev/null
+++ b/Tomboy/Addins/WebSyncService/Api/CertificateManager.cs
@@ -0,0 +1,60 @@
+// Permission is hereby granted, free of charge, to any person obtaining 
+// a copy of this software and associated documentation files (the 
+// "Software"), to deal in the Software without restriction, including 
+// without limitation the rights to use, copy, modify, merge, publish, 
+// distribute, sublicense, and/or sell copies of the Software, and to 
+// permit persons to whom the Software is furnished to do so, subject to 
+// the following conditions: 
+//  
+// The above copyright notice and this permission notice shall be 
+// included in all copies or substantial portions of the Software. 
+//  
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 
+// 
+// Copyright (c) 2009 Novell, Inc. (http://www.novell.com)
+// Copyright (c) 2009 Canonical, Ltd (http://www.canonical.com)
+// 
+// Authors: 
+//      Rodrigo Moya <rodrigo moya canonical com>
+// 
+
+using System;
+using System.IO;
+using System.Net;
+using System.Security.Cryptography.X509Certificates;
+
+namespace Tomboy.WebSync.Api
+{
+	public class CertificateManager : ICertificatePolicy
+	{
+
+		public bool CheckValidationResult (ServicePoint sp, 
+						   X509Certificate certificate,
+						   WebRequest request,
+						   int error)
+
+		{
+			if (error == 0)
+				return true;
+ 
+			/* FIXME: can't open a dialog, since this is called on a thread
+			Gtk.MessageDialog dialog = new Gtk.MessageDialog (
+				null, 0,
+				Gtk.MessageType.Error,
+				Gtk.ButtonsType.YesNo,
+				"Certificate from web server not known. Do you want to accept it?");
+			int response = dialog.Run ();
+			dialog.Destroy ();
+
+			return (response == (int) Gtk.ResponseType.Yes); */
+
+			return true;
+		}
+	}
+}
diff --git a/Tomboy/Addins/WebSyncService/Api/OAuth.cs b/Tomboy/Addins/WebSyncService/Api/OAuth.cs
index 4afd3c0..f158243 100644
--- a/Tomboy/Addins/WebSyncService/Api/OAuth.cs
+++ b/Tomboy/Addins/WebSyncService/Api/OAuth.cs
@@ -224,6 +224,8 @@ namespace Tomboy.WebSync.Api
 		{
 			var responseData = string.Empty;
 
+			ServicePointManager.CertificatePolicy = new CertificateManager ();
+
 			// TODO: Set UserAgent, Timeout, KeepAlive, Proxy?
 			HttpWebRequest webRequest = System.Net.WebRequest.Create (url) as HttpWebRequest;
 			webRequest.Method = method.ToString ();
@@ -246,14 +248,14 @@ namespace Tomboy.WebSync.Api
 					requestWriter.Write (postData);
 			}
 
-			using (var responseReader = new StreamReader (webRequest.GetResponse ().GetResponseStream ())) {
-				try {
-					responseData = responseReader.ReadToEnd ();
-				} catch (Exception e) {
-					Logger.Error ("Caught exception. Message: {0}", e.Message);
-					Logger.Error ("Stack trace for previous exception: {0}", e.StackTrace);
-					throw;
+			try {
+				using (var responseReader = new StreamReader (webRequest.GetResponse ().GetResponseStream ())) {
+			      		responseData = responseReader.ReadToEnd ();
 				}
+			} catch (Exception e) {
+				Logger.Error ("Caught exception. Message: {0}", e.Message);
+				Logger.Error ("Stack trace for previous exception: {0}", e.StackTrace);
+				throw;
 			}
 
 			if (Debugging)
diff --git a/Tomboy/Addins/WebSyncService/Api/RootInfo.cs b/Tomboy/Addins/WebSyncService/Api/RootInfo.cs
index 21c962a..7f0625d 100644
--- a/Tomboy/Addins/WebSyncService/Api/RootInfo.cs
+++ b/Tomboy/Addins/WebSyncService/Api/RootInfo.cs
@@ -54,10 +54,17 @@ namespace Tomboy.WebSync.Api
 			RootInfo root = new RootInfo ();
 			root.ApiVersion = (string) jsonObj ["api-version"];
 
-			Hyena.Json.JsonObject userRefJsonObj =
-				(Hyena.Json.JsonObject) jsonObj ["user-ref"];
-			root.User =
-				ResourceReference.ParseJson (userRefJsonObj);
+			object val;
+			if (jsonObj.TryGetValue ("user-ref", out val)) {
+				Hyena.Json.JsonObject userRefJsonObj = (Hyena.Json.JsonObject) val;
+
+				root.User =
+					ResourceReference.ParseJson (userRefJsonObj);
+			}
+
+			root.AuthorizeUrl =  (string) jsonObj ["oauth_authorize_url"];
+			root.AccessTokenUrl = (string) jsonObj ["oauth_access_token_url"];
+			root.RequestTokenUrl = (string) jsonObj ["oauth_request_token_url"];
 
 			return root;
 		}
@@ -70,6 +77,12 @@ namespace Tomboy.WebSync.Api
 
 		public string ApiVersion { get; private set; }
 
+		public string AuthorizeUrl { get; private set; }
+
+		public string AccessTokenUrl { get; private set; }
+
+		public string RequestTokenUrl { get; private set; }
+
 		#endregion
 	}
 }
diff --git a/Tomboy/Addins/WebSyncService/WebSyncPreferencesWidget.cs b/Tomboy/Addins/WebSyncService/WebSyncPreferencesWidget.cs
index 091f456..6cff772 100644
--- a/Tomboy/Addins/WebSyncService/WebSyncPreferencesWidget.cs
+++ b/Tomboy/Addins/WebSyncService/WebSyncPreferencesWidget.cs
@@ -26,6 +26,7 @@
 using System;
 
 using Mono.Unix;
+using Tomboy.WebSync.Api;
 
 namespace Tomboy.WebSync
 {
@@ -93,13 +94,24 @@ namespace Tomboy.WebSync
 		{
 			// TODO: Move this
 			if (Auth == null) {
-				Auth = new Api.OAuth ();
-				Auth.AuthorizeLocation = Server + "/oauth/authenticate/";
-				Auth.AccessTokenBaseUrl = Server + "/oauth/access_token/";
-				Auth.RequestTokenBaseUrl = Server + "/oauth/request_token/";
-				Auth.ConsumerKey = "abcdefg";
-				Auth.ConsumerSecret = "1234567";
-				Auth.Realm = "Snowy";
+				string rootUri = Server + "/api/1.0";
+				try {
+					RootInfo root = RootInfo.GetRoot (rootUri, new Api.AnonymousConnection ());
+
+					Auth = new Api.OAuth ();
+
+					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 && !authReqested) {
@@ -109,6 +121,7 @@ namespace Tomboy.WebSync
 				} catch (Exception e) {
 					Logger.Error ("Failed to get auth URL from " + Server + ". Exception was: " + e.ToString ());
 					authButton.Label = Catalog.GetString ("Server not responding. Try again later.");
+					oauth = null;
 					return;
 				}
 				Logger.Debug ("Launching browser to authorize web sync: " + authUrl);
diff --git a/Tomboy/Addins/WebSyncService/WebSyncService-mac.csproj b/Tomboy/Addins/WebSyncService/WebSyncService-mac.csproj
index 816f7e8..6d60778 100644
--- a/Tomboy/Addins/WebSyncService/WebSyncService-mac.csproj
+++ b/Tomboy/Addins/WebSyncService/WebSyncService-mac.csproj
@@ -80,6 +80,8 @@
     <EmbeddedResource Include="WebSyncService.addin.xml" />
   </ItemGroup>
   <ItemGroup>
+    <Compile Include="Api\AnonymousConnection.cs" />
+    <Compile Include="Api\CertificateManager.cs" />
     <Compile Include="Api\NoteInfo.cs" />
     <Compile Include="Api\ResourceReference.cs" />
     <Compile Include="Api\UserInfo.cs" />
diff --git a/Tomboy/Addins/WebSyncService/WebSyncService.csproj b/Tomboy/Addins/WebSyncService/WebSyncService.csproj
index c4dc0a6..a04aaf8 100644
--- a/Tomboy/Addins/WebSyncService/WebSyncService.csproj
+++ b/Tomboy/Addins/WebSyncService/WebSyncService.csproj
@@ -71,6 +71,8 @@
     <EmbeddedResource Include="WebSyncService.addin.xml" />
   </ItemGroup>
   <ItemGroup>
+    <Compile Include="Api\AnonymousConnection.cs" />
+    <Compile Include="Api\CertificateManager.cs" />
     <Compile Include="Api\IWebConnection.cs" />
     <Compile Include="Api\NoteInfo.cs" />
     <Compile Include="Api\OAuth.cs" />



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