I would like to implement a forgotten password scenario in an web application. The system will send out an email to the user containing a unique url that the user can hit to allow them to reset their password. There is loads of guidance on this online. The following is a good linke suggesting how to implement this. Best way of doing code for "Forgotten Password"
The part I do not understand fully is the token generation. What does this mean?? Is this just a guid (or random string) that is stored on the server against the user (maybe in the users db table). The guid is also sent in the url (as querystring) so that when the request hits the web server it can look the guid up and find which user account to reset. Is there more to it than this? Many people talk about token expiration. I could store an expiration time against the guid after which the account reset cannot be done.
Some have suggested a CSRF token, but I cannot understand how this would work in this scenario.
Any guidance would be much appreciated... :)
I used this piece of code to generate my token :
Source : http://www.php.net/manual/en/function.openssl-random-pseudo-bytes.php#96812
Storing a randomly generated token of (at least) 128 bits server-side, together with the username and an expiration date, will work perfectly fine.
Another way to achieve the same (without having to store anything server-side) is computing
where
+
denotes concenation,hash()
is a cryptographically secure hash function (like SHA2) andsecret
is a string of (at least) 128 bits that is only known to you, and send this to the user:Both method achieve the same security, but note that - until the token expires - the user could change his password several times.
In the first case, make sure that
token
is created randomly (e.g. using/dev/random
if you're on linux). The same goes forsecret
in the second. Butsecret
is static (not newly generated for every request).