Merge pull request #3519 from openstreetmap/auth-api-calls

Authenticate OSM API calls if possible
This commit is contained in:
Bryan Housel
2016-10-24 18:46:44 -04:00
committed by GitHub
3 changed files with 66 additions and 50 deletions
+2 -2
View File
@@ -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'
}
+63 -47
View File
@@ -7,18 +7,17 @@ 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',
urlroot = protocol + '//www.openstreetmap.org',
inflight = {},
loadedTiles = {},
tileZoom = 16,
oauth = osmAuth({
url: protocol + '//www.openstreetmap.org',
url: urlroot,
oauth_consumer_key: '5A043yRSEugj4DJ5TljuapfnrflWDte8jTOcWLlT',
oauth_secret: 'aB3jKq1TRsCOUrfOIZ6oQMEDmv2ptV76PA54NGLL',
loading: authenticating,
@@ -166,13 +165,13 @@ export default {
changesetURL: function(changesetId) {
return url + '/changeset/' + changesetId;
return urlroot + '/changeset/' + changesetId;
},
changesetsURL: function(center, zoom) {
var precision = Math.max(0, Math.ceil(Math.log(zoom) / Math.LN2));
return url + '/history#map=' +
return urlroot + '/history#map=' +
Math.floor(zoom) + '/' +
center[1].toFixed(precision) + '/' +
center[0].toFixed(precision);
@@ -180,20 +179,25 @@ export default {
entityURL: function(entity) {
return url + '/' + entity.type + '/' + entity.osmId();
return urlroot + '/' + entity.type + '/' + entity.osmId();
},
userURL: function(username) {
return url + '/user/' + username;
return urlroot + '/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 = urlroot + path;
return d3.xml(url).get(done);
}
},
@@ -201,11 +205,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 +218,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 +234,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 +340,7 @@ export default {
method: 'PUT',
path: '/api/0.6/changeset/' + changeset_id + '/close',
options: { header: { 'Content-Type': 'text/xml' } }
}, utilFunctor(true));
}, function() { return true; });
});
});
},
@@ -371,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(url + '/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);
});
},
@@ -392,7 +405,7 @@ export default {
var apiStatus = capabilities.getElementsByTagName('status');
callback(undefined, apiStatus[0].getAttribute('api'));
}
d3.xml(url + '/api/capabilities').get()
d3.xml(urlroot + '/api/capabilities').get()
.on('load', done)
.on('error', callback);
},
@@ -414,7 +427,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 +447,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,23 +464,29 @@ export default {
dispatch.call('loading');
}
inflight[id] = that.loadFromURL(bboxUrl(tile), function(err, parsed) {
loadedTiles[id] = true;
delete inflight[id];
inflight[id] = that.loadFromAPI(
'/api/0.6/map?bbox=' + tile.extent.toParam(),
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');
}
});
});
},
switch: function(options) {
url = options.url;
urlroot = options.urlroot;
oauth.options(_.extend({
url: urlroot,
loading: authenticating,
done: authenticated
}, options));
+1 -1
View File
@@ -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.1",
"rbush": "2.0.1",
"sexagesimal": "0.5.0",
"togeojson": "0.16.0",