views:

31

answers:

2

I want to provide a url to an requested user through email for a download request. The url is valid for a minutes, when user tries to access that url after a minutes the web app should redirect him to another page. what is the best logic to go about!!.kindly let me know your views.

+1  A: 
  1. Generate a large random string (GUID)

  2. Write this string with the timestamp to a database

  3. Give the user a link to /download?guid=[your guid]

  4. Write a servlet and map it to /download

  5. in your servlet

5.1. Read the GUID from the request parameter

5.2. check the database that the time is still valid

5.3. if yes, read the file from your server and stream it from the servlet to the user (make sure to set the content type correctly)

5.4. update the db-table to indicate that this GUID was already used

5.3' if not, redirect to error page

Nir Levy
Need the alternative use case for the invalid request as well :)
willcodejavaforfood
@Levy thanks but for an invalid request.is it possible without hitting database.
Suresh S
Pierre's idea is nice as well.
Nir Levy
@Suresh. Use Pierre's idea for initial validation of the timestamp. Pass both timestamp and guid. If the timestamp is valid, query the database (and re-check the time stamp) and the deliver the file (or not).
Nir Levy
@Nir passing timestamp means currenttimestamp when url is created, and then when the url is accessed (current timestamp - passed one) > 10 seconds , then redirect to invalid page.
Suresh S
+2  A: 

Create an URL to the resource to download that contains a query string with an encrypted expiration date. Very simple to manage and you don't have to rely on a database.

http://example.com/download.php?aid=jHYgIK7d

Pierre 303
@Pierre i didnt understand.kindly explain the flow.
Suresh S
One pragmatic way to tell a download page when it expired is to pass it the information in the query string. However you don't want the user to know how to set himself the expiration, so you encrypt it.
Pierre 303
@Pierre but how do u know while file to download ,when the querystring is just an encrypted timestamp.somewhere we need to have relation with encrypted value and the file.
Suresh S