How to handle file downloads with JWT based authentication?

I’m writing a webapp in Angular where authentication is handled by a JWT token, meaning that every request has an “Authentication” header with all the necessary information.

This works nicely for REST calls, but I don’t understand how I should handle download links for files hosted on the backend (the files reside on the same server where the webservices are hosted).

I can’t use regular <a href='...'/> links since they won’t carry any header and the authentication will fail. Same for the various incantations of

Some solutions I thought of:

  1. Generate a temporary unsecured download link on the server
  2. Pass the authentication information as an url parameter and manually handle the case
  3. Get the data through XHR and save the file client side.

All of the above are less than satisfactory.

1 is the solution I am using right now. I don’t like it for two reasons: first it is not ideal security-wise, second it works but it requires quite a lot of work especially on the server: to download something I need to call a service that generates a new “random” url, stores it somewhere (possibly on the DB) for a some time, and returns it to the client. The client gets the url, and use or similar with it. When requested, the new url should check if it is still valid, and then return the data.

2 seems at least as much work.

3 seems a lot of work, even using available libraries, and lot of potential issues. (I would need to provide my own download status bar, load the whole file in memory and then ask the user to save the file locally).

The task seems a pretty basic one though, so I’m wondering if there is anything much simpler that I can use.

I’m not necessarily looking for a solution “the Angular way”. Regular Javascript would be fine.

38 thoughts on “How to handle file downloads with JWT based authentication?”

  1. 668820 523121For anybody who is considering about external complications, sometimes be tough amaze those to realize to produce just a single weed in this really flowing generally requires eleven liters concerning gasoline to. dc free of charge mommy weblog giveaways family trip home gardening house power wash baby laundry detergent 6604


Leave a Comment