openseadragon: Unable to add auth headers to openseadragon requests with simple image tile source
Hi,
I’m sure I’m missing something fundamental here as no-one else is mentioning issues with adding headers, but I’ll risk the shame and ask for help!
In our project we had openseadragon working fine, however I added security (JWT) to a backend where the images are stored, now we need all requests from openseadragon to contain the token in the http header. I’ve used the sample code from this project located in the tests for adding headers:
var viewer = OpenSeadragon({
id: viewerElementID,
prefixUrl: "./assets/scripts/openseadragon-bin-2.3.1/images/",
zoomInButton: "zoomin",
zoomOutButton: "zoom-out",
homeButton: "home",
fullPageButton: "full-page",
showNavigator: true,
navigatorId: navigatorDivID,
toolbar: toolbarDivID,
showNavigator: true,
loadTilesWithAjax: true,
ajaxHeaders: {
"Authorization": localStorage.getItem('token')
}
});
return viewer;
Setting the tile source is as per the simple example from the website:
$scope.viewer.open([{
type: 'image',
url: $scope.selectedImageSrc
}]);
But in the dev tools in Chrome I can’t see any headers being added to the image http requests (403 - as expected).
Am I able to get some pointers on how this should be done correctly - I would appreciate any help!
Thanks.
About this issue
- Original URL
- State: open
- Created 6 years ago
- Reactions: 2
- Comments: 21 (13 by maintainers)
@alimoeeny Great! Probably good to start with simple image and branch out from there as needed. Here’s where the image is getting loaded:
https://github.com/openseadragon/openseadragon/blob/640526b444536dccb2c78eec7a0b4b7eeb1eb43c/src/imagetilesource.js#L143
As you can see, it’s just using a browser image to load it. As far as I know, you can’t add arbitrary headers to a browser image load, so we’ll have to add the ability to load it via ajax. Here’s where other tiles are loaded via ajax:
https://github.com/openseadragon/openseadragon/blob/640526b444536dccb2c78eec7a0b4b7eeb1eb43c/src/tilesource.js#L780
I wonder if that function (
TileSource.downloadTileStart) could be called directly fromImageTileSource. If not, it would be good to refactor it so we have a common function that can be called from both places, so we don’t have to duplicate all that code.Of course the other part of making this into a PR is making sure that
loadTilesWithAjaxandajaxHeadersare properly piped through.Sound fair?
@iangilman my apologies for diverting the issue. In any case, I do not have any issue with custom tile source so far. Cheers!