[gnome-maps/wip/osm-edit] osmEdit: WIP, implement OAuth-autorized calls for updating



commit 1aeea17d16633d1c099da215f38e8c37e9aef4ed
Author: Marcus Lundblad <ml update uu se>
Date:   Mon Nov 23 22:56:09 2015 +0100

    osmEdit: WIP, implement OAuth-autorized calls for updating

 src/osmConnection.js |   43 +++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 43 insertions(+), 0 deletions(-)
---
diff --git a/src/osmConnection.js b/src/osmConnection.js
index 9ea289d..8ee08c2 100644
--- a/src/osmConnection.js
+++ b/src/osmConnection.js
@@ -52,8 +52,13 @@ const OSMConnection = new Lang.Class({
 
     _init: function(params) {
         this._session = new Soup.Session();
+        /* OAuth proxy used for enrolling access tokens */
         this._oauthProxy = Rest.OAuthProxy.new(CONSUMER_KEY, CONSUMER_SECRET,
                                                OAUTH_ENDPOINT_URL, false);
+        /* OAuth proxy used for doing authorized update calls */
+        this._callProxy = Rest.OAuthProxy.new(CONSUMER_KEY, CONSUMER_SECRET,
+                                              BASE_URL + '/' + API_VERSION,
+                                              false);
 
         /* TODO: stopgap to supply username/password
            to use with HTTP basic auth, should be
@@ -123,12 +128,50 @@ const OSMConnection = new Lang.Class({
     },
 
     openChangeset: function(comment, callback) {
+        /* we assume that this would only be called if there's already been an
+           OAuth access token enrolled, so, if the currently instanciated
+           proxy instance doesn't have a token set, we could safely count on
+           it being present in the keyring */
+        if (this._oauthProxy.get_token() === null) {
+            Secret.password_lookup(SECRET_SCHEMA, {}, null,
+                                   function(source, result) {
+                                        this._onPasswordLookedUp(result,
+                                                                 comment,
+                                                                 callback);
+                                   }.bind(this));
+        } else {
+            this._doOpenChangeset(comment, callback);
+        }
+    },
+
+    _onPasswordLookedUp: function(result, comment, callback) {
+        let password = Secret.password_lookup_finish(result);
+
+        if (password) {
+            let token = password.split(':')[0];
+            let secret = password.split(':')[1];
+
+            this._callProxy.token = token;
+            this._callProxy.token_secret = secret;
+            this._doOpenChangeset(comment, callback);
+        } else {
+            callback(false, null, null);
+        }
+    },
+
+    _doOpenChangeset: function(comment, callback) {
         let changeset =
             Maps.OSMChangeset.new(comment, 'gnome-maps ' + pkg.version);
         let xml = changeset.serialize();
 
         Utils.debug('about open changeset:\n' + xml + '\n');
 
+        let call = this._callProxy.new_call();
+
+        call.set_function('/changeset/open');
+        call.set_function('PUT');
+        call.add_header('Content-Type', 'text/xml');
+
         let url = this._getOpenChangesetUrl();
         let uri = new Soup.URI(url);
         let msg = new Soup.Message({ method: 'PUT', uri: uri });


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