Skip to content

Commit

Permalink
Support 'withCredentials' option which will include credentials with …
Browse files Browse the repository at this point in the history
…CORS requests (Esri#1233)

* Add support for 'withCredentials' option

when sending cross origin requests to servers that are secured with IWA/AD it is necessary to send request with 'withCredentials' set to true
If the layer is created using this option the requests will be adjusted accordingly

* added tests for 'withCredentials' option

* Apply suggestions from code review

adjust styling for consistency

Co-authored-by: Patrick Arlt <[email protected]>

Co-authored-by: Patrick Arlt <[email protected]>
  • Loading branch information
luiscamachopt and patrickarlt authored Oct 5, 2020
1 parent f82c576 commit 50b9f21
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 2 deletions.
8 changes: 8 additions & 0 deletions spec/Layers/RasterLayerSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ describe('L.esri.RasterLayer', function () {
// Extend 'abstract' RasterLayer object and implement functionality required for tests
var TestRasterLayer = L.esri.RasterLayer.extend({
initialize: function (options) {
L.setOptions(this, options);
this.service = {
metadata: function () {}
};
Expand Down Expand Up @@ -91,6 +92,13 @@ describe('L.esri.RasterLayer', function () {

expect(imageOverlay.off.calledWith('error', sinon.match.func, layer)).to.be.true;
});

it('should send credentials', function () {
var layer = new TestRasterLayer({withCredentials: true});
layer.addTo(map);
var imageOverlay = map.addLayer.getCall(1).args[0];
expect(imageOverlay._image.crossOrigin).to.be.equal('use-credentials');
});
});
});
});
18 changes: 18 additions & 0 deletions spec/RequestSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,24 @@ describe('L.esri request helpers', function () {
requests[0].respond(200, { 'Content-Type': 'text/plain; charset=utf-8' }, JSON.stringify(sampleResponse));
});

it('should be able to make a GET request with CORS and credentials', function (done) {
L.esri.get.CORS('http://services.arcgisonline.com/ArcGIS/rest/info', {}, function (error, response) {
expect(this.foo).to.equal('bar');
expect(response).to.deep.equal(sampleResponse);
done();
}, {
foo: 'bar',
options: {
withCredentials: true
}
});

expect(requests[0].url).to.equal('http://services.arcgisonline.com/ArcGIS/rest/info?f=json');
expect(requests[0].method).to.equal('GET');
expect(requests[0].withCredentials).to.equal(true);
requests[0].respond(200, { 'Content-Type': 'text/plain; charset=utf-8' }, JSON.stringify(sampleResponse));
});

it('should be able to make a GET request with JSONP', function (done) {
var request = L.esri.get.JSONP('http://example.com/foo', {}, function (error, response) {
expect(this.foo).to.equal('bar');
Expand Down
2 changes: 1 addition & 1 deletion src/Layers/RasterLayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ export var RasterLayer = Layer.extend({
// opacity is 0 while the image is loading
var image = new Overlay(url, bounds, {
opacity: 0,
crossOrigin: this.options.useCors,
crossOrigin: this.options.withCredentials ? 'use-credentials' : this.options.useCors,
alt: this.options.alt,
pane: this.options.pane || this.getPane(),
interactive: this.options.interactive
Expand Down
6 changes: 6 additions & 0 deletions src/Request.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ function xmlHttpGet (url, params, callback, context) {
if (typeof context !== 'undefined' && context !== null) {
if (typeof context.options !== 'undefined') {
httpRequest.timeout = context.options.timeout;
if (context.options.withCredentials) {
httpRequest.withCredentials = true;
}
}
}
httpRequest.send(null);
Expand All @@ -128,6 +131,9 @@ export function request (url, params, callback, context) {
if (typeof context !== 'undefined' && context !== null) {
if (typeof context.options !== 'undefined') {
httpRequest.timeout = context.options.timeout;
if (context.options.withCredentials) {
httpRequest.withCredentials = true;
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Util.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export function _findIdAttributeFromFeature (feature) {
export function responseToFeatureCollection (response, idAttribute) {
var objectIdField;
var features = response.features || response.results;
var count = features.length;
var count = features && features.length;

if (idAttribute) {
objectIdField = idAttribute;
Expand Down

0 comments on commit 50b9f21

Please sign in to comment.