From f198203fbe1229060aa4fac3bef983e5b625fa65 Mon Sep 17 00:00:00 2001 From: Tom MacWright Date: Mon, 19 Nov 2012 21:32:15 -0500 Subject: [PATCH] Oauth, working --- Makefile | 6 ++++-- css/app.css | 12 ++++++++++++ index.html | 6 ++++++ js/lib/ohauth.js | 25 ++++++++++++++++++++++--- 4 files changed, 44 insertions(+), 5 deletions(-) diff --git a/Makefile b/Makefile index e0d3bb53a..39a59524f 100644 --- a/Makefile +++ b/Makefile @@ -11,10 +11,12 @@ all: \ iD.min.js .INTERMEDIATE iD.js: \ - js/lib/d3.v2.js \ + js/lib/d3.v3.js \ js/lib/lodash.js \ + js/lib/ohauth.js \ + js/lib/sha.js \ js/lib/jxon.js \ - js/lib/underscore.js \ + js/lib/lodash.js \ js/iD/id.js \ js/iD/Connection.js \ js/iD/Util.js \ diff --git a/css/app.css b/css/app.css index 6f969acde..1d4faef04 100644 --- a/css/app.css +++ b/css/app.css @@ -192,3 +192,15 @@ button small { right: 0; cursor: pointer; } + +.modal { + width:600px; + height:400px; + padding:10px; + position:absolute; + background:#fff; + top:50px; + left:50%; + margin-left:-305px; + box-shadow:0 0 5px #000; +} diff --git a/index.html b/index.html index f727dcfc6..7c86a6a70 100644 --- a/index.html +++ b/index.html @@ -40,6 +40,7 @@ + @@ -75,6 +76,11 @@ }); var controller = iD.Controller(map); + var oauth = iD.OAuth(map); + + d3.selectAll('button#save').on('click', function() { + oauth.authenticate(); + }); d3.selectAll('button#place').on('click', function() { controller.go(iD.actions.AddPlace); diff --git a/js/lib/ohauth.js b/js/lib/ohauth.js index 27b1cdd03..f72dc01f3 100644 --- a/js/lib/ohauth.js +++ b/js/lib/ohauth.js @@ -8,6 +8,8 @@ ohauth.qsString = function(obj) { }).join('&'); }; +ohauth.sha = sha1(); + ohauth.stringQs = function(str) { return str.split('&').reduce(function(obj, pair){ var parts = pair.split('='); @@ -16,13 +18,15 @@ ohauth.stringQs = function(str) { }, {}); }; -ohauth.post = function(url, data, callback) { +ohauth.xhr = function(method, url, auth, data, options, callback) { var xhr = new XMLHttpRequest(); xhr.onreadystatechange = function() { if (4 == xhr.readyState && 0 !== xhr.status) callback(xhr); }; - xhr.open('POST', url, true); - xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); + var headers = (options && options.header) || { 'Content-Type': 'application/x-www-form-urlencoded' }; + xhr.open(method, url, true); + xhr.setRequestHeader('Authorization', 'OAuth ' + ohauth.authHeader(auth)); + for (var h in headers) xhr.setRequestHeader(h, headers[h]); xhr.send(data); }; @@ -33,6 +37,12 @@ ohauth.nonce = function() { return o; }; +ohauth.authHeader = function(obj) { + return Object.keys(obj).sort().map(function(key) { + return encodeURIComponent(key) + '="' + encodeURIComponent(obj[key]) + '"'; + }).join(', '); +}; + ohauth.timestamp = function() { return ~~((+new Date()) / 1000); }; ohauth.percentEncode = function(s) { @@ -42,12 +52,21 @@ ohauth.percentEncode = function(s) { }; ohauth.baseString = function(method, url, params) { + if (params.oauth_signature) delete params.oauth_signature; return [ method, ohauth.percentEncode(url), ohauth.percentEncode(ohauth.qsString(params))].join('&'); }; +ohauth.signature = function(oauth_secret, token_secret, baseString) { + return ohauth.sha.b64_hmac_sha1( + ohauth.percentEncode(oauth_secret) + '&' + + ohauth.percentEncode(token_secret), + baseString); +}; + context.ohauth = ohauth; })(this); +