[tomboy] Abstract-out authentication methods when using WebHelper, and implement for UserInfo.



commit f9c860ef9189bf3ed793d91534dc09076be72e58
Author: Sandy Armstrong <sanfordarmstrong gmail com>
Date:   Mon May 18 06:54:01 2009 -0700

    Abstract-out authentication methods when using WebHelper, and implement for UserInfo.
    
    Also, fixed request URLs to prepend a base URL to the "/username/notes"-type values that are returned from Snowy.
---
 Tomboy/Addins/WebSyncService/Api/UserInfo.cs  |   33 +++++++++++---
 Tomboy/Addins/WebSyncService/Api/WebHelper.cs |   59 +++++++++++++++++++++++-
 Tomboy/Addins/WebSyncService/WebSyncServer.cs |    4 +-
 3 files changed, 85 insertions(+), 11 deletions(-)

diff --git a/Tomboy/Addins/WebSyncService/Api/UserInfo.cs b/Tomboy/Addins/WebSyncService/Api/UserInfo.cs
index 9f41c64..8bc9b0d 100644
--- a/Tomboy/Addins/WebSyncService/Api/UserInfo.cs
+++ b/Tomboy/Addins/WebSyncService/Api/UserInfo.cs
@@ -32,12 +32,19 @@ namespace Tomboy.WebSync.Api
 	{
 		#region Public Static Methods
 		
-		public static UserInfo GetUser (string uri)
+		public static UserInfo GetUser (string serverUrl, string userName, IAuthProvider auth)
 		{
+			// TODO: Clean this up
+			string baseUrl = serverUrl + "/api/1.0/";
+			string uri = baseUrl + userName;
+			
 			// TODO: Error-handling in GET and Deserialize
 			WebHelper helper = new WebHelper ();
-			string jsonString = helper.Get (uri, null);
-			return ParseJson (jsonString);
+			string jsonString = helper.Get (uri, null, auth);
+			UserInfo user = ParseJson (jsonString);
+			user.AuthProvider = auth;
+			user.BaseUrl = baseUrl;
+			return user;
 		}
 
 		public static UserInfo ParseJson (string jsonString)
@@ -103,7 +110,7 @@ namespace Tomboy.WebSync.Api
 			if (sinceRevision >= 0)
 				parameters ["since"] = sinceRevision.ToString ();
 			
-			jsonString = helper.Get (Notes.ApiRef, parameters);
+			jsonString = helper.Get (BaseUrl + Notes.ApiRef, parameters, AuthProvider);
 
 			return ParseJsonNotes (jsonString);
 		}
@@ -114,13 +121,24 @@ namespace Tomboy.WebSync.Api
 			WebHelper helper = new WebHelper ();
 
 			string jsonResponseString =
-				helper.PutJson (Notes.ApiRef, null, CreateNoteChangesJsonString (noteUpdates));
-			
-			ParseJsonNotes (jsonResponseString);	// TODO: What?
+				helper.PutJson (BaseUrl + Notes.ApiRef, null, CreateNoteChangesJsonString (noteUpdates), AuthProvider);
+
+			// TODO: Not working on server yet, but this will let us do
+			//       a sanity check on what revision we pushed, *and*
+			//       let us update any cache of refs that we keep.
+			//ParseJsonNotes (jsonResponseString);
 		}
 
 		#endregion
 
+		#region Public Properties
+
+		public IAuthProvider AuthProvider { get; set; }
+
+		public string BaseUrl { get; set; }
+
+		#endregion
+
 		#region Private Methods
 
 		private IList<NoteInfo> ParseJsonNotes (string jsonString)
@@ -160,6 +178,7 @@ namespace Tomboy.WebSync.Api
 			// TODO: Handle errors
 			Hyena.Json.Serializer serializer =
 				new Hyena.Json.Serializer (noteChangesObj);
+			// TODO: Log this for debugging?
 			return serializer.Serialize ();
 		}
 		
diff --git a/Tomboy/Addins/WebSyncService/Api/WebHelper.cs b/Tomboy/Addins/WebSyncService/Api/WebHelper.cs
index 599fcf6..ba46922 100644
--- a/Tomboy/Addins/WebSyncService/Api/WebHelper.cs
+++ b/Tomboy/Addins/WebSyncService/Api/WebHelper.cs
@@ -33,10 +33,12 @@ namespace Tomboy.WebSync.Api
 {
 	public class WebHelper
 	{
-		public string Get (string uri, IDictionary<string, string> queryParameters)
+		public string Get (string uri, IDictionary<string, string> queryParameters, IAuthProvider auth)
 		{
 			WebRequest request = BuildRequest (uri, queryParameters);
 			request.Method = "GET";
+			if (auth != null)
+				auth.PrepareRequest (request);
 
 			// TODO: Set ContentLength, UserAgent, Timeout, KeepAlive, Proxy, ContentType?
 			//       (May only be available if we cast back to HttpWebRequest)
@@ -44,10 +46,10 @@ namespace Tomboy.WebSync.Api
 			return ProcessResponse (request);
 		}
 
-		public string PutJson (string uri, IDictionary<string, string> queryParameters, string postValue)
+		public string PutJson (string uri, IDictionary<string, string> queryParameters, string postValue, IAuthProvider auth)
 		{
 			WebRequest request = BuildRequest (uri, queryParameters);
-			request.Method = "POST";
+			request.Method = "PUT";
 
 			// TODO: Set ContentLength, UserAgent, Timeout, KeepAlive, Proxy, ContentType?
 			//       (May only be available if we cast back to HttpWebRequest)
@@ -55,6 +57,8 @@ namespace Tomboy.WebSync.Api
 
 			byte [] data = Encoding.UTF8.GetBytes (postValue);
 			request.ContentLength = data.Length;
+			if (auth != null)
+				auth.PrepareRequest (request);
 
 			// TODO: try/finally error handling
 			Stream requestStream = request.GetRequestStream ();
@@ -80,6 +84,10 @@ namespace Tomboy.WebSync.Api
 
 		private WebRequest BuildRequest (string uri, IDictionary<string, string> queryParameters)
 		{
+			// NOTE: This is the proper way, but not yet implemented in Mono 2.0.1
+			//ServicePointManager.ServerCertificateValidationCallback = CheckCertificate;
+			ServicePointManager.CertificatePolicy = new LooseCertificatePolicy ();
+			
 			StringBuilder urlBuilder = new StringBuilder (uri);	// TODO: Capacity?
 			urlBuilder.Append ("?");
 			if (queryParameters != null) {
@@ -95,5 +103,50 @@ namespace Tomboy.WebSync.Api
 
 			return HttpWebRequest.Create (urlBuilder.ToString ());
 		}
+
+		// TODO: Consider moving to IAuthProvider
+//		private bool CheckCertificate (object sender,
+//		                               System.Security.Cryptography.X509Certificates.X509Certificate cert,
+//		                               System.Security.Cryptography.X509Certificates.X509Chain chain,
+//		                               System.Net.Security.SslPolicyErrors policyErrors)
+//		{
+//			return true;
+//		}
+		                               
+	}
+
+	// TODO: Replace with IAuthenticationModule
+	public interface IAuthProvider
+	{
+		void PrepareRequest (WebRequest request);
+	}
+	
+	public class BasicHttpAuthProvider : IAuthProvider
+	{
+		private string username;
+		private string password;
+
+		public BasicHttpAuthProvider (string username, string password)
+		{
+			this.username = username;
+			this.password = password;
+		}
+
+		public void PrepareRequest (WebRequest request)
+		{
+			request.PreAuthenticate = true;
+			request.Credentials = new NetworkCredential (username, password);
+		}
+	}
+
+	public class LooseCertificatePolicy : ICertificatePolicy
+	{
+		public bool CheckValidationResult (ServicePoint srvPoint,
+		                                   System.Security.Cryptography.X509Certificates.X509Certificate certificate,
+		                                   WebRequest request,
+		                                   int certificateProblem)
+		{
+			return true;
+		}
 	}
 }
diff --git a/Tomboy/Addins/WebSyncService/WebSyncServer.cs b/Tomboy/Addins/WebSyncService/WebSyncServer.cs
index 59d60ba..ee1cbec 100644
--- a/Tomboy/Addins/WebSyncService/WebSyncServer.cs
+++ b/Tomboy/Addins/WebSyncService/WebSyncServer.cs
@@ -36,6 +36,7 @@ namespace Tomboy.WebSync
 	{
 		private string serverUrl;
 		private string userName;
+		private IAuthProvider auth;
 		private UserInfo user;
 		private List<NoteInfo> pendingCommits;
 		
@@ -44,6 +45,7 @@ namespace Tomboy.WebSync
 			this.serverUrl = serverUrl;
 			this.userName = userName;
 
+			auth = new BasicHttpAuthProvider (userName, password);
 			pendingCommits = new List<NoteInfo> ();
 		}
 
@@ -133,7 +135,7 @@ namespace Tomboy.WebSync
 		
 		private void RefreshUser ()
 		{
-			user = UserInfo.GetUser (serverUrl + "/api/1.0/" + userName);
+			user = UserInfo.GetUser (serverUrl, userName, auth);
 		}
 
 		#endregion



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