pdiiif icon indicating copy to clipboard operation
pdiiif copied to clipboard

Can pdiiif download images that require authentication?

Open phil-plencner-hl opened this issue 4 months ago • 10 comments

We're using pdiiif within a Mirador plugin in order to download PDFs of images within the viewer. I am running into issues with pdiiif not passing the authentication cookie for restricted images. When it attempts to download a restricted image it just returns a PDF with all blank pages. The browser console returns a bunch of 401 codes for the images it is attempting to turn into a PDF.

Would you be able to add the ability to pass credentials / cookies using pdiiif?

Let me know what you think. Thanks!

phil-plencner-hl avatar Sep 02 '25 17:09 phil-plencner-hl

Should be doable, at least for client-side PDF generation. It's not possible (for good reason) for the "no cors" server-side generation fallback, though. I'll try to see if I can get at least cover page generation on the server to work.

jbaiter avatar Sep 05 '25 20:09 jbaiter

OK, so I looked into it, I think it's going to work, but will come with quite a list of requirements for the Image API endpoints:

  1. Server Auth Coookie for the Image API endpoints must have SameSite=None and Secure set
  2. Server Response for the Image API endpoints must have the Access-Control-Allow-Credentials:true header
  3. Server Response for the Image API endpoints must include the origin domain for the request (i.e. the domain your Mirador instance is hosted on) in the Access-Control-Allow-Origin header. The header must not be set to *.

Reason for all of that is that pdiiif needs to access the image data in JavaScript to include it in the PDF, which means we have to to deal with CORS for image requests.

Background here: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#including_credentials

jbaiter avatar Sep 05 '25 21:09 jbaiter

I pushed a commit with a preliminary implementation to the credentials branch, could you test it for me? I don't have any manifests with authenticated Image API endpoints myself, so i can't really test it right now.

jbaiter avatar Sep 05 '25 22:09 jbaiter

Thank you so much! I will take a look and test this out today.

phil-plencner-hl avatar Sep 08 '25 15:09 phil-plencner-hl

Did you get a chance to try this @phil-plencner-hl ?

jbaiter avatar Sep 15 '25 20:09 jbaiter

Hi sorry for the delay on testing the credentials branch for pdiiif, I was out of the office most of last week.

I’m trying to install pdiiif as a package using that branch in a mirador plugin and I keep running into problems. The current error I’m seeing is:

Unsupported URL Type "workspace:": workspace:^

I’m not sure how else I can test these changes within a plugin?

This is the plugin: https://github.com/harvard-lts/mirador-pdiiif-plugin/

This is the command I was attempting to run (I realized just trying to install the branch directly at the root of the repo doesn’t seem to work because there is no package.json file there….) npm install "https://gitpkg.now.sh/jbaiter/pdiiif/pdiiif-api?credentials"

I also tried installing pnpm and running a similar command and got the following error:  ERR_PNPM_WORKSPACE_PKG_NOT_FOUND  In : "pdiiif@workspace:^" is in the dependencies but no package named "pdiiif" is present in the workspace

I'm wondering if I just have to build it locally on the branch and include the built code in the plugin somehow?

Let me know if you have any ideas…

phil-plencner-hl avatar Sep 15 '25 21:09 phil-plencner-hl

Sorry about that! The project is setup as a pnpm workspace. If you're only using the library (pdiiif-lib) and not the web interface (pdiiif-web) or the fallback server (pdiiif-api), it should be enough to run npm i ~/path/to/pdiiif-repo/pdiiif-lib. The directory contains a standalone package.json without references to other workspace components.

jbaiter avatar Sep 16 '25 07:09 jbaiter

I couldn't get that to work either, unfortunately. Because we are deploying the plugin to our development environments using Github Actions, it doesn't have access to the local files and fails when trying to build.

I attempted to make a patch to pdiiif in our Mirador plugin using the patch-package module, but I'm running into problems there too. It seems like the credentials branch is off of tag 0.2.7 in Github but the latest tag in npmjs is 0.2.5, so the patching fails.

Would you be able to publish the 0.2.7 tag to npmjs or is that still under development?

phil-plencner-hl avatar Sep 16 '25 21:09 phil-plencner-hl

Actually, I think I was just able to get it patched and deployed in our plugin. I should be able to test it today / tomorrow. I'll let you know how it goes!

phil-plencner-hl avatar Sep 17 '25 19:09 phil-plencner-hl

Hey @phil-plencner-hl, did you get a chance to test this? I'm preparing a new release and would love to get this in, if it solved your problem :-)

jbaiter avatar Sep 28 '25 19:09 jbaiter