views:

73

answers:

2

Hi,

I want to implement a forgot password function in my java web application. I want to implement it like this:

  1. User enters their account email address and presses 'forgot password' button
  2. App generates a unique code of characters and numbers and sends a link with that as a parameter to the user's email address
  3. User clicks the link and they are presented with a form where they can enter their new password

What I want to do is ensure that the link (i.e. the unique code) 'expires' an hour after the user presses the forgot password button so that if an attacker gains access to their email account this link won't work unless they gain access in that first hour.

I don't know how to make the database 'expire' or clear the code for the user's account record. How could I implement this?

Thanks!

A: 

You store the date in the database that the unique code was generated. When the user clicks on the link, if it is more than the allowed time since the unique code was generated, you do not let them change the password.

RedFilter
+3  A: 

Add a ValidUntil column to the table containing the code and check against that before letting the user change the password.

klausbyskov
I find it better to store the date the code was generated because a) you can easily change the allowed time without having to update existing data in the database, and b) during testing, you can simulate the time being exceeded by changing the vlaue of allowed time in your code, rather than having to mess around with data in the database.
RedFilter
@OrbMan, I agree with a) and I don't think this comment box allows enough text for me to explain why I don't think b) applies.
klausbyskov
Cool, nice simple solution - feel a bit silly now :)
Annie