Refactor OAuth URL signing into a common utility method
authorAndy Allan <gravitystorm@gmail.com>
Tue, 12 Oct 2010 11:47:05 +0000 (11:47 +0000)
committerAndy Allan <gravitystorm@gmail.com>
Tue, 12 Oct 2010 11:47:05 +0000 (11:47 +0000)
TODO.txt
net/systemeD/halcyon/connection/XMLConnection.as

index 34143b3..763dcd4 100644 (file)
--- a/TODO.txt
+++ b/TODO.txt
@@ -69,7 +69,6 @@ Potlatch 2: main outstanding issues
 
 * Combine two GPX buttons
 * Shouldn't refetch just when opening dialog
-* Refactor sendOAuthGet, sendOAuthPut and signOAuthUrl
 * Make the connection, not the savemanager, in charge of sorting out the oauth stuff?
 * Ensure the oauth dialog pops up if there's no oauth credentials
 * Rework the loading text
index c044590..0c521c5 100644 (file)
@@ -107,14 +107,17 @@ package net.systemeD.halcyon.connection {
                private function changesetCloseError(event:Event):void { }
                // ** TODO: when we get little floating warnings, we can send a happy or sad one up
 
-               private function sendOAuthPut(url:String, xml:XML, onComplete:Function, onError:Function, onStatus:Function):void {
-            // make an OAuth query
+        private function signedOAuthURL(url:String, method:String):String {
+            // method should be PUT, GET, POST or DELETE
             var sig:IOAuthSignatureMethod = new OAuthSignatureMethod_HMAC_SHA1();
-            var oauthRequest:OAuthRequest = new OAuthRequest("PUT", url, null, appID, authToken);
-            var urlStr:Object = oauthRequest.buildRequest(sig, OAuthRequest.RESULT_TYPE_URL_STRING)
+            var oauthRequest:OAuthRequest = new OAuthRequest(method, url, null, appID, authToken);
+            var urlStr:Object = oauthRequest.buildRequest(sig, OAuthRequest.RESULT_TYPE_URL_STRING);
+            return String(urlStr);
+        }
 
-            // build the actual request
-            var urlReq:URLRequest = new URLRequest(String(urlStr));
+               private function sendOAuthPut(url:String, xml:XML, onComplete:Function, onError:Function, onStatus:Function):void {
+            // build the request
+            var urlReq:URLRequest = new URLRequest(signedOAuthURL(url, "PUT"));
             urlReq.method = "POST";
                        if (xml) { urlReq.data = xml.toXMLString(); } else { urlReq.data = true; }
             urlReq.contentType = "application/xml";
@@ -127,11 +130,7 @@ package net.systemeD.halcyon.connection {
                }
 
         private function sendOAuthGet(url:String, onComplete:Function, onError:Function, onStatus:Function):void {
-            var sig:IOAuthSignatureMethod = new OAuthSignatureMethod_HMAC_SHA1();
-            var oauthRequest:OAuthRequest = new OAuthRequest("GET", url, null, appID, authToken);
-            var urlStr:Object = oauthRequest.buildRequest(sig, OAuthRequest.RESULT_TYPE_URL_STRING);
-
-            var urlReq:URLRequest = new URLRequest(String(urlStr));
+            var urlReq:URLRequest = new URLRequest(signedOAuthURL(url, "GET"));
             urlReq.method = "GET";
             var loader:URLLoader = new URLLoader();
             loader.addEventListener(Event.COMPLETE, onComplete);
@@ -141,10 +140,7 @@ package net.systemeD.halcyon.connection {
         }
 
         override public function signOAuthGet(url:String):String {
-            var sig:IOAuthSignatureMethod = new OAuthSignatureMethod_HMAC_SHA1();
-            var oauthRequest:OAuthRequest = new OAuthRequest("GET", url, null, appID, authToken);
-            var urlStr:Object = oauthRequest.buildRequest(sig, OAuthRequest.RESULT_TYPE_URL_STRING);
-            return String(urlStr);
+            return signedOAuthURL(url, "GET");
         }
         
         override public function uploadChanges():void {
@@ -162,13 +158,11 @@ package net.systemeD.halcyon.connection {
 
             // now actually upload them
             // make an OAuth query
-            var sig:IOAuthSignatureMethod = new OAuthSignatureMethod_HMAC_SHA1();
+
             var url:String = Connection.apiBaseURL+"changeset/" + changeset.id + "/upload";
-            var oauthRequest:OAuthRequest = new OAuthRequest("POST", url, null, appID, authToken);
-            var urlStr:Object = oauthRequest.buildRequest(sig, OAuthRequest.RESULT_TYPE_URL_STRING)
 
             // build the actual request
-            var urlReq:URLRequest = new URLRequest(String(urlStr));
+            var urlReq:URLRequest = new URLRequest(signedOAuthURL(url, "POST"));
             urlReq.method = "POST";
             urlReq.data = upload.toXMLString();
             urlReq.contentType = "text/xml";