From e81b6e626a980727a717d173088ecbef98dd8cca Mon Sep 17 00:00:00 2001 From: Bryan Housel Date: Sat, 22 Oct 2016 12:17:52 -0400 Subject: [PATCH 1/4] WIP: authenticate OSM API calls if user is logged in --- modules/services/osm.js | 71 +++++++++++++++++++++++------------------ 1 file changed, 40 insertions(+), 31 deletions(-) diff --git a/modules/services/osm.js b/modules/services/osm.js index 44c2afad8..476113e47 100644 --- a/modules/services/osm.js +++ b/modules/services/osm.js @@ -7,18 +7,18 @@ import { geoExtent } from '../geo/index'; import { osmEntity, osmNode, osmRelation, osmWay } from '../osm/index'; import { utilDetect } from '../util/detect'; import { utilRebind } from '../util/rebind'; -import { utilFunctor } from '../util/index'; var dispatch = d3.dispatch('authenticating', 'authenticated', 'auth', 'loading', 'loaded'), useHttps = window.location.protocol === 'https:', protocol = useHttps ? 'https:' : 'http:', - url = protocol + '//www.openstreetmap.org', + apiroot = protocol + '//api.openstreetmap.org', + wwwroot = protocol + '//www.openstreetmap.org', inflight = {}, loadedTiles = {}, tileZoom = 16, oauth = osmAuth({ - url: protocol + '//www.openstreetmap.org', + url: apiroot, oauth_consumer_key: '5A043yRSEugj4DJ5TljuapfnrflWDte8jTOcWLlT', oauth_secret: 'aB3jKq1TRsCOUrfOIZ6oQMEDmv2ptV76PA54NGLL', loading: authenticating, @@ -166,13 +166,13 @@ export default { changesetURL: function(changesetId) { - return url + '/changeset/' + changesetId; + return wwwroot + '/changeset/' + changesetId; }, changesetsURL: function(center, zoom) { var precision = Math.max(0, Math.ceil(Math.log(zoom) / Math.LN2)); - return url + '/history#map=' + + return wwwroot + '/history#map=' + Math.floor(zoom) + '/' + center[1].toFixed(precision) + '/' + center[0].toFixed(precision); @@ -180,20 +180,25 @@ export default { entityURL: function(entity) { - return url + '/' + entity.type + '/' + entity.osmId(); + return wwwroot + '/' + entity.type + '/' + entity.osmId(); }, userURL: function(username) { - return url + '/user/' + username; + return wwwroot + '/user/' + username; }, - loadFromURL: function(url, callback) { + loadFromAPI: function(path, callback) { function done(err, dom) { return callback(err, parse(dom)); } - return d3.xml(url).get(done); + if (this.authenticated()) { + return oauth.xhr({ method: 'GET', path: path }, done); + else { + var url = apiroot + path; + return d3.xml(url).get(done); + } }, @@ -201,11 +206,12 @@ export default { var type = osmEntity.id.type(id), osmID = osmEntity.id.toOSM(id); - this.loadFromURL( - url + '/api/0.6/' + type + '/' + osmID + (type !== 'node' ? '/full' : ''), + this.loadFromAPI( + '/api/0.6/' + type + '/' + osmID + (type !== 'node' ? '/full' : ''), function(err, entities) { - if (callback) callback(err, {data: entities}); - }); + if (callback) callback(err, { data: entities }); + } + ); }, @@ -213,11 +219,12 @@ export default { var type = osmEntity.id.type(id), osmID = osmEntity.id.toOSM(id); - this.loadFromURL( - url + '/api/0.6/' + type + '/' + osmID + '/' + version, + this.loadFromAPI( + '/api/0.6/' + type + '/' + osmID + '/' + version, function(err, entities) { - if (callback) callback(err, {data: entities}); - }); + if (callback) callback(err, { data: entities }); + } + ); }, @@ -228,11 +235,12 @@ export default { osmIDs = _.map(v, osmEntity.id.toOSM); _.each(_.chunk(osmIDs, 150), function(arr) { - that.loadFromURL( - url + '/api/0.6/' + type + '?' + type + '=' + arr.join(), + that.loadFromAPI( + '/api/0.6/' + type + '?' + type + '=' + arr.join(), function(err, entities) { - if (callback) callback(err, {data: entities}); - }); + if (callback) callback(err, { data: entities }); + } + ); }); }); }, @@ -333,7 +341,7 @@ export default { method: 'PUT', path: '/api/0.6/changeset/' + changeset_id + '/close', options: { header: { 'Content-Type': 'text/xml' } } - }, utilFunctor(true)); + }, function() { return true; }); }); }); }, @@ -380,7 +388,7 @@ export default { })); } - d3.xml(url + '/api/0.6/changesets?user=' + user.id).get() + d3.xml(apiroot + '/api/0.6/changesets?user=' + user.id).get() .on('load', done) .on('error', callback); }); @@ -392,7 +400,7 @@ export default { var apiStatus = capabilities.getElementsByTagName('status'); callback(undefined, apiStatus[0].getAttribute('api')); } - d3.xml(url + '/api/capabilities').get() + d3.xml(apiroot + '/api/capabilities').get() .on('load', done) .on('error', callback); }, @@ -414,7 +422,8 @@ export default { ts = 256 * Math.pow(2, z - tileZoom), origin = [ s / 2 - projection.translate()[0], - s / 2 - projection.translate()[1]]; + s / 2 - projection.translate()[1] + ]; var tiles = d3geoTile() .scaleExtent([tileZoom, tileZoom]) @@ -433,10 +442,6 @@ export default { }; }); - function bboxUrl(tile) { - return url + '/api/0.6/map?bbox=' + tile.extent.toParam(); - } - _.filter(inflight, function(v, i) { var wanted = _.find(tiles, function(tile) { return i === tile.id; @@ -454,11 +459,15 @@ export default { dispatch.call('loading'); } - inflight[id] = that.loadFromURL(bboxUrl(tile), function(err, parsed) { + function bboxPath(tile) { + return '/api/0.6/map?bbox=' + tile.extent.toParam(); + } + + inflight[id] = that.loadFromAPI(bboxPath(tile), function(err, parsed) { loadedTiles[id] = true; delete inflight[id]; - if (callback) callback(err, _.extend({data: parsed}, tile)); + if (callback) callback(err, _.extend({ data: parsed }, tile)); if (_.isEmpty(inflight)) { dispatch.call('loaded'); From 282f9e4f691f8aeaa25ddfae33ac15cd23b02b5f Mon Sep 17 00:00:00 2001 From: Bryan Housel Date: Mon, 24 Oct 2016 00:11:36 -0400 Subject: [PATCH 2/4] Upgrade to osm-auth 1.0.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 799a85ad7..26e429294 100644 --- a/package.json +++ b/package.json @@ -27,7 +27,7 @@ "diacritics": "1.2.3", "lodash": "4.16.4", "marked": "0.3.6", - "osm-auth": "0.2.9", + "osm-auth": "1.0.0", "rbush": "2.0.1", "sexagesimal": "0.5.0", "togeojson": "0.16.0", From 45d85e04c5a1d4a4e4cdc41982bc197ba58a63f5 Mon Sep 17 00:00:00 2001 From: Bryan Housel Date: Mon, 24 Oct 2016 01:21:38 -0400 Subject: [PATCH 3/4] Make authenticated api calls if possible --- index.html | 4 ++-- modules/services/osm.js | 49 +++++++++++++++++++++++------------------ 2 files changed, 30 insertions(+), 23 deletions(-) diff --git a/index.html b/index.html index 5de076250..e8638d677 100644 --- a/index.html +++ b/index.html @@ -30,12 +30,12 @@ .call(iD.uiSourceSwitch(id) .keys([ { - 'url': 'http://www.openstreetmap.org', + 'urlroot': 'http://www.openstreetmap.org', 'oauth_consumer_key': '5A043yRSEugj4DJ5TljuapfnrflWDte8jTOcWLlT', 'oauth_secret': 'aB3jKq1TRsCOUrfOIZ6oQMEDmv2ptV76PA54NGLL' }, { - 'url': 'http://api06.dev.openstreetmap.org', + 'urlroot': 'http://api06.dev.openstreetmap.org', 'oauth_consumer_key': 'zwQZFivccHkLs3a8Rq5CoS412fE5aPCXDw9DZj7R', 'oauth_secret': 'aMnOOCwExO2XYtRVWJ1bI9QOdqh1cay2UgpbhA6p' } diff --git a/modules/services/osm.js b/modules/services/osm.js index 476113e47..da309c461 100644 --- a/modules/services/osm.js +++ b/modules/services/osm.js @@ -12,13 +12,12 @@ import { utilRebind } from '../util/rebind'; var dispatch = d3.dispatch('authenticating', 'authenticated', 'auth', 'loading', 'loaded'), useHttps = window.location.protocol === 'https:', protocol = useHttps ? 'https:' : 'http:', - apiroot = protocol + '//api.openstreetmap.org', - wwwroot = protocol + '//www.openstreetmap.org', + urlroot = protocol + '//www.openstreetmap.org', inflight = {}, loadedTiles = {}, tileZoom = 16, oauth = osmAuth({ - url: apiroot, + url: urlroot, oauth_consumer_key: '5A043yRSEugj4DJ5TljuapfnrflWDte8jTOcWLlT', oauth_secret: 'aB3jKq1TRsCOUrfOIZ6oQMEDmv2ptV76PA54NGLL', loading: authenticating, @@ -166,13 +165,13 @@ export default { changesetURL: function(changesetId) { - return wwwroot + '/changeset/' + changesetId; + return urlroot + '/changeset/' + changesetId; }, changesetsURL: function(center, zoom) { var precision = Math.max(0, Math.ceil(Math.log(zoom) / Math.LN2)); - return wwwroot + '/history#map=' + + return urlroot + '/history#map=' + Math.floor(zoom) + '/' + center[1].toFixed(precision) + '/' + center[0].toFixed(precision); @@ -180,12 +179,12 @@ export default { entityURL: function(entity) { - return wwwroot + '/' + entity.type + '/' + entity.osmId(); + return urlroot + '/' + entity.type + '/' + entity.osmId(); }, userURL: function(username) { - return wwwroot + '/user/' + username; + return urlroot + '/user/' + username; }, @@ -195,8 +194,8 @@ export default { } if (this.authenticated()) { return oauth.xhr({ method: 'GET', path: path }, done); - else { - var url = apiroot + path; + } else { + var url = urlroot + path; return d3.xml(url).get(done); } }, @@ -379,18 +378,24 @@ export default { userChangesets: function(callback) { this.userDetails(function(err, user) { - if (err) return callback(err); - - function done(changesets) { - callback(undefined, Array.prototype.map.call(changesets.getElementsByTagName('changeset'), - function (changeset) { - return { tags: getTags(changeset) }; - })); + if (err) { + callback(err); + return; } - d3.xml(apiroot + '/api/0.6/changesets?user=' + user.id).get() - .on('load', done) - .on('error', callback); + function done(err, changesets) { + if (err) { + callback(err); + } else { + callback(undefined, Array.prototype.map.call(changesets.getElementsByTagName('changeset'), + function (changeset) { + return { tags: getTags(changeset) }; + } + )); + } + } + + oauth.xhr({ method: 'GET', path: '/api/0.6/changesets?user=' + user.id }, done); }); }, @@ -400,7 +405,7 @@ export default { var apiStatus = capabilities.getElementsByTagName('status'); callback(undefined, apiStatus[0].getAttribute('api')); } - d3.xml(apiroot + '/api/capabilities').get() + d3.xml(urlroot + '/api/capabilities').get() .on('load', done) .on('error', callback); }, @@ -478,8 +483,10 @@ export default { switch: function(options) { - url = options.url; + urlroot = options.urlroot; + oauth.options(_.extend({ + url: urlroot, loading: authenticating, done: authenticated }, options)); From a601ccc0c9a89a0a9f8d1e4f3f668ff374b8f23e Mon Sep 17 00:00:00 2001 From: Bryan Housel Date: Mon, 24 Oct 2016 18:40:52 -0400 Subject: [PATCH 4/4] Use osm-auth 1.0.1, make authenticated OSM API calls (re: #2262) --- modules/services/osm.js | 24 ++++++++++++------------ package.json | 2 +- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/modules/services/osm.js b/modules/services/osm.js index da309c461..b888f42d4 100644 --- a/modules/services/osm.js +++ b/modules/services/osm.js @@ -464,20 +464,20 @@ export default { dispatch.call('loading'); } - function bboxPath(tile) { - return '/api/0.6/map?bbox=' + tile.extent.toParam(); - } + inflight[id] = that.loadFromAPI( + '/api/0.6/map?bbox=' + tile.extent.toParam(), + function(err, parsed) { + loadedTiles[id] = true; + delete inflight[id]; - inflight[id] = that.loadFromAPI(bboxPath(tile), function(err, parsed) { - loadedTiles[id] = true; - delete inflight[id]; + if (callback) { + callback(err, _.extend({ data: parsed }, tile)); + } - if (callback) callback(err, _.extend({ data: parsed }, tile)); - - if (_.isEmpty(inflight)) { - dispatch.call('loaded'); - } - }); + if (_.isEmpty(inflight)) { + dispatch.call('loaded'); + } + }); }); }, diff --git a/package.json b/package.json index 26e429294..285651f18 100644 --- a/package.json +++ b/package.json @@ -27,7 +27,7 @@ "diacritics": "1.2.3", "lodash": "4.16.4", "marked": "0.3.6", - "osm-auth": "1.0.0", + "osm-auth": "1.0.1", "rbush": "2.0.1", "sexagesimal": "0.5.0", "togeojson": "0.16.0",